Code generator for C++ from YAML to generate network protocol parsers
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

118 рядки
2.4 KiB

4 роки тому
  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: Array(BitfieldGenerator),
  22. attributes: Array(AttributeGenerator),
  23. repeats: Array(RepeatGenerator),
  24. attributeafters: Array(AttributeAfterGenerator)
  25. )
  26. def initialize(@name : String)
  27. @bitfields = Array(BitfieldGenerator).new
  28. @attributes = Array(AttributeGenerator).new
  29. @repeats = Array(RepeatGenerator).new
  30. @attributeafters = Array(AttributeAfterGenerator).new
  31. end
  32. def push(attr : AttributeGenerator)
  33. attr.class_name = @name
  34. @attributes.push(attr)
  35. end
  36. def push(bitf : BitfieldGenerator)
  37. end
  38. ECR.def_to_s "src/views/class.ecr"
  39. end
  40. class BitfieldGenerator
  41. YAML.mapping(
  42. class_name: String,
  43. name: String,
  44. out_type: String,
  45. start: Int32,
  46. size: Int32
  47. )
  48. def initialize(@name : String, @out_type : String, @start : Int32, @size : Int32)
  49. @class_name = "no_class"
  50. end
  51. end
  52. class AttributeGenerator
  53. YAML.mapping(
  54. class_name: String,
  55. name: String,
  56. out_type: String,
  57. start: Int32
  58. )
  59. def initialize(@name : String, @out_type : String, @start : Int32)
  60. @class_name = "no_class"
  61. end
  62. ECR.def_to_s "src/views/attribute.ecr"
  63. end
  64. class RepeatGenerator
  65. YAML.mapping(
  66. class_name: String,
  67. name: String,
  68. out_type: String,
  69. start: Int32,
  70. count_name: String
  71. )
  72. def initialize(@name : String, @out_type : String, @start : Int32, @count_name : String)
  73. @class_name = "no_class"
  74. end
  75. end
  76. class AttributeAfterGenerator
  77. YAML.mapping(
  78. class_name: String,
  79. name: String,
  80. out_type: String,
  81. start: Int32,
  82. after_name: String
  83. )
  84. def initialize(@name : String, @out_type : String, @start : Int32, @after_name : String)
  85. @class_name = "no_class"
  86. end
  87. end
  88. #v = ClassGenerator.new("record")
  89. #v.push(AttributeGenerator.new("uuid", "std::array<unsigned char, 16>", 0))
  90. #v.push(AttributeGenerator.new("x", "bitops::regulated<int>", 16))
  91. #v.push(AttributeGenerator.new("y", "bitops::regulated<int>", 20))
  92. #f = FileGenerator.new
  93. #f.classes << v
  94. #f.includes << "<array>"
  95. #f.includes << "\"bitops.hpp\""
  96. #puts f.to_s
  97. File.open(ARGV[0]) do |file|
  98. puts FileGenerator.from_yaml(file).to_s
  99. end