xdrgen 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +12 -0
  6. data/LICENSE.txt +202 -0
  7. data/README.md +79 -0
  8. data/Rakefile +6 -0
  9. data/bin/xdrgen +5 -0
  10. data/lib/xdrgen.rb +24 -0
  11. data/lib/xdrgen/ast.rb +81 -0
  12. data/lib/xdrgen/ast/concerns/contained.rb +32 -0
  13. data/lib/xdrgen/ast/concerns/has_children.rb +9 -0
  14. data/lib/xdrgen/ast/concerns/has_definitions.rb +90 -0
  15. data/lib/xdrgen/ast/concerns/named.rb +16 -0
  16. data/lib/xdrgen/ast/concerns/namespace.rb +7 -0
  17. data/lib/xdrgen/ast/concerns/nested_definition.rb +16 -0
  18. data/lib/xdrgen/ast/constant.rb +7 -0
  19. data/lib/xdrgen/ast/decimal_constant.rb +7 -0
  20. data/lib/xdrgen/ast/declarations/array.rb +15 -0
  21. data/lib/xdrgen/ast/declarations/base.rb +28 -0
  22. data/lib/xdrgen/ast/declarations/opaque.rb +11 -0
  23. data/lib/xdrgen/ast/declarations/optional.rb +5 -0
  24. data/lib/xdrgen/ast/declarations/simple.rb +7 -0
  25. data/lib/xdrgen/ast/declarations/string.rb +7 -0
  26. data/lib/xdrgen/ast/declarations/void.rb +7 -0
  27. data/lib/xdrgen/ast/definitions/base.rb +9 -0
  28. data/lib/xdrgen/ast/definitions/const.rb +12 -0
  29. data/lib/xdrgen/ast/definitions/enum.rb +17 -0
  30. data/lib/xdrgen/ast/definitions/enum_member.rb +44 -0
  31. data/lib/xdrgen/ast/definitions/namespace.rb +9 -0
  32. data/lib/xdrgen/ast/definitions/nested_enum.rb +7 -0
  33. data/lib/xdrgen/ast/definitions/nested_struct.rb +7 -0
  34. data/lib/xdrgen/ast/definitions/nested_union.rb +7 -0
  35. data/lib/xdrgen/ast/definitions/struct.rb +18 -0
  36. data/lib/xdrgen/ast/definitions/struct_body.rb +11 -0
  37. data/lib/xdrgen/ast/definitions/struct_member.rb +12 -0
  38. data/lib/xdrgen/ast/definitions/typedef.rb +19 -0
  39. data/lib/xdrgen/ast/definitions/union.rb +55 -0
  40. data/lib/xdrgen/ast/definitions/union_arm.rb +33 -0
  41. data/lib/xdrgen/ast/definitions/union_arm_case.rb +11 -0
  42. data/lib/xdrgen/ast/definitions/union_body.rb +22 -0
  43. data/lib/xdrgen/ast/definitions/union_default_arm.rb +19 -0
  44. data/lib/xdrgen/ast/fixed_size.rb +23 -0
  45. data/lib/xdrgen/ast/hexadecimal_constant.rb +7 -0
  46. data/lib/xdrgen/ast/identifier.rb +5 -0
  47. data/lib/xdrgen/ast/octal_constant.rb +7 -0
  48. data/lib/xdrgen/ast/top.rb +5 -0
  49. data/lib/xdrgen/ast/typespecs/base.rb +27 -0
  50. data/lib/xdrgen/ast/typespecs/bool.rb +5 -0
  51. data/lib/xdrgen/ast/typespecs/double.rb +5 -0
  52. data/lib/xdrgen/ast/typespecs/float.rb +5 -0
  53. data/lib/xdrgen/ast/typespecs/hyper.rb +6 -0
  54. data/lib/xdrgen/ast/typespecs/int.rb +6 -0
  55. data/lib/xdrgen/ast/typespecs/opaque.rb +10 -0
  56. data/lib/xdrgen/ast/typespecs/quadruple.rb +5 -0
  57. data/lib/xdrgen/ast/typespecs/simple.rb +14 -0
  58. data/lib/xdrgen/ast/typespecs/string.rb +9 -0
  59. data/lib/xdrgen/ast/typespecs/unsigned_hyper.rb +5 -0
  60. data/lib/xdrgen/ast/typespecs/unsigned_int.rb +5 -0
  61. data/lib/xdrgen/ast/var_size.rb +26 -0
  62. data/lib/xdrgen/ast/whitespace.rb +5 -0
  63. data/lib/xdrgen/cli.rb +31 -0
  64. data/lib/xdrgen/compilation.rb +31 -0
  65. data/lib/xdrgen/generators.rb +16 -0
  66. data/lib/xdrgen/generators/base.rb +11 -0
  67. data/lib/xdrgen/generators/elixir.rb +260 -0
  68. data/lib/xdrgen/generators/go.rb +578 -0
  69. data/lib/xdrgen/generators/java.rb +810 -0
  70. data/lib/xdrgen/generators/java/XdrDataInputStream.erb +122 -0
  71. data/lib/xdrgen/generators/java/XdrDataOutputStream.erb +96 -0
  72. data/lib/xdrgen/generators/java/XdrElement.erb +10 -0
  73. data/lib/xdrgen/generators/java/XdrString.erb +58 -0
  74. data/lib/xdrgen/generators/javascript.rb +248 -0
  75. data/lib/xdrgen/generators/ruby.rb +283 -0
  76. data/lib/xdrgen/grammar/base.treetop +71 -0
  77. data/lib/xdrgen/grammar/comments.treetop +15 -0
  78. data/lib/xdrgen/grammar/const.treetop +8 -0
  79. data/lib/xdrgen/grammar/declaration.treetop +99 -0
  80. data/lib/xdrgen/grammar/enum.treetop +46 -0
  81. data/lib/xdrgen/grammar/main.treetop +30 -0
  82. data/lib/xdrgen/grammar/namespace.treetop +12 -0
  83. data/lib/xdrgen/grammar/struct.treetop +32 -0
  84. data/lib/xdrgen/grammar/typedef.treetop +12 -0
  85. data/lib/xdrgen/grammar/union.treetop +63 -0
  86. data/lib/xdrgen/output.rb +37 -0
  87. data/lib/xdrgen/output_file.rb +87 -0
  88. data/lib/xdrgen/parser.rb +40 -0
  89. data/lib/xdrgen/version.rb +3 -0
  90. data/spec/fixtures/generator/block_comments.x +14 -0
  91. data/spec/fixtures/generator/const.x +4 -0
  92. data/spec/fixtures/generator/enum.x +36 -0
  93. data/spec/fixtures/generator/nesting.x +24 -0
  94. data/spec/fixtures/generator/optional.x +8 -0
  95. data/spec/fixtures/generator/struct.x +10 -0
  96. data/spec/fixtures/generator/test.x +77 -0
  97. data/spec/fixtures/generator/union.x +28 -0
  98. data/spec/fixtures/parser/block_comments.x +14 -0
  99. data/spec/fixtures/parser/const.x +1 -0
  100. data/spec/fixtures/parser/enum.x +35 -0
  101. data/spec/fixtures/parser/nesting.x +26 -0
  102. data/spec/fixtures/parser/optional.x +8 -0
  103. data/spec/fixtures/parser/struct.x +8 -0
  104. data/spec/fixtures/parser/test.x +77 -0
  105. data/spec/fixtures/parser/union.x +10 -0
  106. data/spec/lib/xdrgen/generator_spec.rb +30 -0
  107. data/spec/lib/xdrgen/output_file_spec.rb +33 -0
  108. data/spec/lib/xdrgen/parser_spec.rb +27 -0
  109. data/spec/spec_helper.rb +13 -0
  110. data/spec/support/fixtures.rb +27 -0
  111. data/xdrgen.gemspec +32 -0
  112. metadata +301 -0
@@ -0,0 +1,283 @@
1
+ module Xdrgen
2
+ module Generators
3
+
4
+ class Ruby < Xdrgen::Generators::Base
5
+
6
+ def generate
7
+ render_index
8
+ render_definitions(@top)
9
+ end
10
+
11
+ private
12
+ def render_index
13
+ root_file = "#{@namespace}.rb"
14
+ out = @output.open(root_file)
15
+ render_top_matter out
16
+
17
+ render_definitions_index(out, @top)
18
+ end
19
+
20
+ def render_definitions_index(out, node)
21
+
22
+ node.definitions.each do |member|
23
+ case member
24
+ when AST::Definitions::Namespace ;
25
+ render_namespace_index(out, member)
26
+ when AST::Definitions::Typedef ;
27
+ render_typedef(out, member)
28
+ when AST::Definitions::Const ;
29
+ render_const(out, member)
30
+ when AST::Definitions::Struct,
31
+ AST::Definitions::Union,
32
+ AST::Definitions::Enum ;
33
+ render_autoload(out, member)
34
+ end
35
+ end
36
+ end
37
+
38
+ def render_namespace_index(out, ns)
39
+ out.puts "module #{name_string ns.name}"
40
+ out.indent do
41
+ out.puts "include XDR::Namespace"
42
+ out.break
43
+ render_definitions_index(out, ns)
44
+ out.unbreak
45
+ end
46
+ out.puts "end"
47
+ end
48
+
49
+ def render_autoload(out, named)
50
+ out.puts "autoload :#{name_string named.name}"
51
+ end
52
+
53
+ def render_typedef(out, typedef)
54
+ out.puts "#{name_string typedef.name} = #{decl_string(typedef.declaration)}"
55
+ end
56
+
57
+ def render_const(out, const)
58
+ out.puts "#{const.name.underscore.upcase} = #{const.value}"
59
+ end
60
+
61
+ def render_definitions(node)
62
+ node.definitions.each(&method(:render_definition))
63
+ node.namespaces.each(&method(:render_definitions))
64
+ end
65
+
66
+ def render_definition(defn)
67
+ case defn
68
+ when AST::Definitions::Struct ;
69
+ render_struct defn
70
+ when AST::Definitions::Enum ;
71
+ render_enum defn
72
+ when AST::Definitions::Union ;
73
+ render_union defn
74
+ end
75
+ end
76
+
77
+ def render_struct(struct)
78
+ render_element "class", struct, "< XDR::Struct" do |out|
79
+
80
+ render_nested_definitions out, struct
81
+
82
+ out.balance_after /,[\s]*/ do
83
+ struct.members.each do |m|
84
+ out.puts "attribute :#{m.name.underscore}, #{decl_string(m.declaration)}"
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def render_enum(enum)
91
+ render_element "class", enum, "< XDR::Enum" do |out|
92
+ out.balance_after /,[\s]*/ do
93
+ enum.members.each do |em|
94
+ out.puts "member :#{em.name.underscore}, #{em.value}"
95
+ end
96
+ end
97
+ out.break
98
+ out.puts "seal"
99
+ end
100
+ end
101
+
102
+ def render_union(union)
103
+
104
+ render_element "class", union, "< XDR::Union" do |out|
105
+ render_nested_definitions out, union
106
+
107
+ out.puts "switch_on #{type_string union.discriminant.type}, :#{union.discriminant_name}"
108
+ out.break
109
+
110
+ out.balance_after /,[\s]*/ do
111
+ union.normal_arms.each do |arm|
112
+ arm.cases.each do |c|
113
+ value = c.value.text_value
114
+ value = ":#{value.underscore}" if c.value.is_a?(AST::Identifier)
115
+ render_union_switch(out, value, arm)
116
+ end
117
+ end
118
+
119
+ if union.default_arm.present?
120
+ render_union_switch(out, ":default", union.default_arm)
121
+ end
122
+ end
123
+ out.break
124
+
125
+ out.balance_after /,[\s]*/ do
126
+ union.arms.each do |a|
127
+ next if a.void?
128
+ out.puts "attribute :#{a.name.underscore}, #{decl_string(a.declaration)}"
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ def render_union_switch(out, switch, arm)
135
+ if arm.void?
136
+ out.puts "switch #{switch}"
137
+ else
138
+ out.puts "switch #{switch}, :#{arm.name.underscore}"
139
+ end
140
+ end
141
+
142
+ def render_nested_definitions(out, parent)
143
+ ndefn = parent.nested_definitions
144
+ return if ndefn.empty?
145
+ ndefn.each(&method(:render_definition))
146
+
147
+ out.puts "include XDR::Namespace"
148
+ out.break
149
+ ndefn.each{|ndefn| render_autoload out, ndefn}
150
+ out.break
151
+ end
152
+
153
+ # TODO: find a better name
154
+ # This renders the skeletal structure of enums, structs, and unions
155
+ def render_element(type, element, post_name="")
156
+ path = element.fully_qualified_name.map(&:underscore).join("/") + ".rb"
157
+ name = name_string element.name
158
+ out = @output.open(path)
159
+ render_top_matter out
160
+ render_source_comment out, element
161
+
162
+ render_containers out, element.namespaces do
163
+ out.puts "#{type} #{name} #{post_name}"
164
+ out.indent do
165
+ yield out
166
+ out.unbreak
167
+ end
168
+ out.puts "end"
169
+ end
170
+ end
171
+
172
+ def render_source_comment(out, defn)
173
+ return if defn.is_a?(AST::Definitions::Namespace)
174
+
175
+ out.puts <<-EOS.strip_heredoc
176
+ # === xdr source ============================================================
177
+ #
178
+ EOS
179
+
180
+ out.puts "# " + defn.text_value.split("\n").join("\n# ")
181
+
182
+ out.puts <<-EOS.strip_heredoc
183
+ #
184
+ # ===========================================================================
185
+ EOS
186
+ end
187
+
188
+ def render_top_matter(out)
189
+ out.puts <<-EOS.strip_heredoc
190
+ # This code was automatically generated using xdrgen
191
+ # DO NOT EDIT or your changes may be overwritten
192
+
193
+ require 'xdr'
194
+ EOS
195
+ out.break
196
+ end
197
+
198
+
199
+ def render_containers(out, containers, &block)
200
+ cur = containers.first
201
+
202
+ if cur.blank?
203
+ block.call
204
+ return
205
+ end
206
+
207
+ type = case cur
208
+ when AST::Definitions::Union, AST::Definitions::Struct ;
209
+ "class"
210
+ else
211
+ "module"
212
+ end
213
+
214
+ out.puts "#{type} #{name_string cur.name}"
215
+ out.indent do
216
+ render_containers(out, containers.drop(1), &block)
217
+ end
218
+ out.puts "end"
219
+ end
220
+
221
+ private
222
+
223
+ def decl_string(decl)
224
+ case decl
225
+ when AST::Declarations::Opaque ;
226
+ type = decl.fixed? ? "XDR::Opaque" : "XDR::VarOpaque"
227
+ "#{type}[#{decl.size}]"
228
+ when AST::Declarations::String ;
229
+ "XDR::String[#{decl.size}]"
230
+ when AST::Declarations::Array ;
231
+ type = decl.fixed? ? "XDR::Array" : "XDR::VarArray"
232
+ args = [decl.child_type.to_s, decl.size].
233
+ compact.
234
+ map(&:to_s).
235
+ join(", ")
236
+ "#{type}[#{args}]"
237
+ when AST::Declarations::Optional ;
238
+ "XDR::Option[#{name_string decl.type.text_value}]"
239
+ when AST::Declarations::Simple ;
240
+ type_string(decl.type)
241
+ when AST::Declarations::Void ;
242
+ "XDR::Void"
243
+ when AST::Concerns::NestedDefinition ;
244
+ name_string type.name
245
+ else
246
+ raise "Unknown declaration type: #{decl.class.name}"
247
+ end
248
+ end
249
+
250
+ def type_string(type)
251
+ case type
252
+ when AST::Typespecs::Int ;
253
+ "XDR::Int"
254
+ when AST::Typespecs::UnsignedInt ;
255
+ "XDR::UnsignedInt"
256
+ when AST::Typespecs::Hyper ;
257
+ "XDR::Hyper"
258
+ when AST::Typespecs::UnsignedHyper ;
259
+ "XDR::UnsignedHyper"
260
+ when AST::Typespecs::Float ;
261
+ "XDR::Float"
262
+ when AST::Typespecs::Double ;
263
+ "XDR::Double"
264
+ when AST::Typespecs::Quadruple ;
265
+ "XDR::Quadruple"
266
+ when AST::Typespecs::Bool ;
267
+ "XDR::Bool"
268
+ when AST::Typespecs::Simple ;
269
+ name_string type.text_value
270
+ when AST::Concerns::NestedDefinition ;
271
+ name_string type.name
272
+ else
273
+ raise "Unknown type: #{type.class.name}"
274
+ end
275
+ end
276
+
277
+ def name_string(name)
278
+ name.camelize
279
+ end
280
+
281
+ end
282
+ end
283
+ end
@@ -0,0 +1,71 @@
1
+ grammar XdrBaseGrammar
2
+
3
+ rule identifier
4
+ !keyword
5
+ ([a-zA-Z] [0-9a-zA-Z_]*)
6
+ ("::" [a-zA-Z] [0-9a-zA-Z_]*)*
7
+ <Xdrgen::AST::Identifier>
8
+ end
9
+
10
+ rule keyword
11
+ ("bool" /
12
+ "case" /
13
+ "const" /
14
+ "default" /
15
+ "double" /
16
+ "quadruple" /
17
+ "enum" /
18
+ "float" /
19
+ "hyper" /
20
+ "opaque" /
21
+ "string" /
22
+ "struct" /
23
+ "switch" /
24
+ "typedef" /
25
+ "union" /
26
+ "unsigned" /
27
+ "int" /
28
+ "void")
29
+ &space
30
+ end
31
+
32
+ rule value
33
+ identifier / constant
34
+ end
35
+
36
+ rule constant
37
+ ( hexadecimal_constant / octal_constant / decimal_constant )
38
+ end
39
+
40
+ rule decimal_constant
41
+ "0" <Xdrgen::AST::DecimalConstant>
42
+ /
43
+ "-"? [1-9] [0-9]* <Xdrgen::AST::DecimalConstant>
44
+ end
45
+
46
+ rule hexadecimal_constant
47
+ "0x" [a-fA-F0-9]+
48
+ <Xdrgen::AST::HexadecimalConstant>
49
+ end
50
+
51
+ rule octal_constant
52
+ "0" [0-7]+
53
+ <Xdrgen::AST::OctalConstant>
54
+ end
55
+
56
+
57
+ # Misc.
58
+
59
+ rule size
60
+ identifier / decimal_constant
61
+ end
62
+
63
+ rule space
64
+ ([\s] / line_comment / block_comment)+
65
+ <Xdrgen::AST::Whitespace>
66
+ end
67
+
68
+ rule asterisk
69
+ "*"
70
+ end
71
+ end
@@ -0,0 +1,15 @@
1
+ grammar XdrCommentsGrammar
2
+
3
+ rule ampersand_line
4
+ "%" content:(!"\n" .)* "\n"?
5
+ end
6
+
7
+ rule block_comment
8
+ "/*" content:(!"*/" .)* "*/"
9
+ end
10
+
11
+ rule line_comment
12
+ "//" content:(!"\n" .)* "\n"?
13
+ end
14
+
15
+ end
@@ -0,0 +1,8 @@
1
+ grammar XdrConstGrammar
2
+
3
+ rule const_def
4
+ "const" space identifier space? "=" space? value_n:value ";"
5
+ <Xdrgen::AST::Definitions::Const>
6
+ end
7
+
8
+ end
@@ -0,0 +1,99 @@
1
+ grammar XdrDeclarationGrammar
2
+ rule declaration
3
+ void_decl /
4
+ opaque_decl /
5
+ string_decl /
6
+ array_decl /
7
+ optional_decl /
8
+ simple_decl
9
+ end
10
+
11
+ rule void_decl
12
+ "void"
13
+ <Xdrgen::AST::Declarations::Void>
14
+ end
15
+
16
+ rule opaque_decl
17
+ type_s:("opaque" <Xdrgen::AST::Typespecs::Opaque>)
18
+ space
19
+ identifier
20
+ size_spec:(fixed_size_spec / var_size_spec)
21
+ <Xdrgen::AST::Declarations::Opaque>
22
+ end
23
+
24
+ rule string_decl
25
+ type_s:("string" <Xdrgen::AST::Typespecs::String>)
26
+ space
27
+ identifier
28
+ size_spec:var_size_spec
29
+ <Xdrgen::AST::Declarations::String>
30
+ end
31
+
32
+ rule array_decl
33
+ type_s:type_specifier
34
+ space
35
+ identifier
36
+ size_spec:(fixed_size_spec / var_size_spec)
37
+ <Xdrgen::AST::Declarations::Array>
38
+ end
39
+
40
+ rule optional_decl
41
+ type_s:type_specifier
42
+ ("*" space? / space? "*")
43
+ identifier
44
+ <Xdrgen::AST::Declarations::Optional>
45
+ end
46
+
47
+ rule simple_decl
48
+ type_s:type_specifier
49
+ space
50
+ identifier
51
+ <Xdrgen::AST::Declarations::Simple>
52
+ end
53
+
54
+ rule type_specifier
55
+ (integral_type_spec /
56
+ float_type_spec /
57
+ boolean_type_spec /
58
+ enum_type_spec /
59
+ struct_type_spec /
60
+ union_type_spec /
61
+ (identifier <Xdrgen::AST::Typespecs::Simple>))
62
+ end
63
+
64
+ rule integral_type_spec
65
+ (("int" <Xdrgen::AST::Typespecs::Int>) /
66
+ ("hyper" <Xdrgen::AST::Typespecs::Hyper>) /
67
+ ("unsigned int" <Xdrgen::AST::Typespecs::UnsignedInt>)/
68
+ ("unsigned hyper" <Xdrgen::AST::Typespecs::UnsignedHyper>))
69
+ type_suffix
70
+ end
71
+
72
+ rule float_type_spec
73
+ (("float" <Xdrgen::AST::Typespecs::Float>) /
74
+ ("double" <Xdrgen::AST::Typespecs::Double>) /
75
+ ("quadruple" <Xdrgen::AST::Typespecs::Quadruple>) )
76
+ type_suffix
77
+ end
78
+
79
+ rule boolean_type_spec
80
+ "bool"
81
+ type_suffix
82
+ <Xdrgen::AST::Typespecs::Bool>
83
+ end
84
+
85
+ rule type_suffix
86
+ &(space / "*")
87
+ end
88
+
89
+
90
+ rule fixed_size_spec
91
+ "[" size_t:size "]"
92
+ <Xdrgen::AST::FixedSize>
93
+ end
94
+
95
+ rule var_size_spec
96
+ "<" size_t:size? ">"
97
+ <Xdrgen::AST::VarSize>
98
+ end
99
+ end