Code generator for C++ from YAML to generate network protocol parsers
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
2.4 KiB

  1. require "ecr"
  2. require "yaml"
  3. module Andrew
  4. VERSION = "0.1.0"
  5. # TODO: Put your code here
  6. end
  7. class FileGenerator
  8. YAML.mapping(
  9. classes: Array(ClassGenerator),
  10. includes: Array(String)
  11. )
  12. def initialize
  13. @classes = Array(ClassGenerator).new
  14. @includes = Array(String).new
  15. end
  16. ECR.def_to_s "src/views/file.ecr"
  17. end
  18. class ClassGenerator
  19. YAML.mapping(
  20. name: String,
  21. bitfields: {
  22. type: Array(BitfieldGenerator),
  23. nilable: true
  24. },
  25. attributes: {
  26. type: Array(AttributeGenerator),
  27. nilable: true
  28. },
  29. repeats: {
  30. type: Array(RepeatGenerator),
  31. nilable: true
  32. },
  33. attributeafters: {
  34. type: Array(AttributeAfterGenerator),
  35. nilable: true
  36. }
  37. )
  38. def initialize(@name : String)
  39. @bitfields = Array(BitfieldGenerator).new
  40. @attributes = Array(AttributeGenerator).new
  41. @repeats = Array(RepeatGenerator).new
  42. @attributeafters = Array(AttributeAfterGenerator).new
  43. end
  44. def push(attr : AttributeGenerator)
  45. attr.class_name = @name
  46. @attributes.push(attr)
  47. end
  48. def push(bitf : BitfieldGenerator)
  49. end
  50. ECR.def_to_s "src/views/class.ecr"
  51. end
  52. class BitfieldGenerator
  53. YAML.mapping(
  54. class_name: {
  55. type: String,
  56. default: "no_class"
  57. },
  58. name: String,
  59. out_type: String,
  60. start: Int32,
  61. size: Int32
  62. )
  63. def initialize(@name : String, @out_type : String, @start : Int32, @size : Int32)
  64. @class_name = "no_class"
  65. end
  66. end
  67. class AttributeGenerator
  68. YAML.mapping(
  69. class_name: {
  70. type: String,
  71. default: "no_class"
  72. },
  73. name: String,
  74. out_type: String,
  75. start: Int32
  76. )
  77. def initialize(@name : String, @out_type : String, @start : Int32)
  78. @class_name = "no_class"
  79. end
  80. ECR.def_to_s "src/views/attribute.ecr"
  81. end
  82. class RepeatGenerator
  83. YAML.mapping(
  84. class_name: {
  85. type: String,
  86. default: "no_class"
  87. },
  88. name: String,
  89. out_type: String,
  90. start: Int32,
  91. count_name: String
  92. )
  93. def initialize(@name : String, @out_type : String, @start : Int32, @count_name : String)
  94. @class_name = "no_class"
  95. end
  96. end
  97. class AttributeAfterGenerator
  98. YAML.mapping(
  99. class_name: {
  100. type: String,
  101. default: "no_class"
  102. },
  103. name: String,
  104. out_type: String,
  105. start: Int32,
  106. after_name: String
  107. )
  108. def initialize(@name : String, @out_type : String, @start : Int32, @after_name : String)
  109. @class_name = "no_class"
  110. end
  111. end
  112. if(ARGV.size != 0)
  113. File.open(ARGV[0]) do |file|
  114. puts FileGenerator.from_yaml(file).to_s
  115. end
  116. else
  117. puts "Andrew code generator\n\tExpects a single YAML file as argument"
  118. end