Code generator for C++ from YAML to generate network protocol parsers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 line
2.5 KiB

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