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,12 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class StructMember < Base
4
+ delegate :name, to: :declaration
5
+ delegate :type, to: :declaration
6
+
7
+ def optional?
8
+ declaration.is_a?(Declarations::Optional)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class Typedef < Base
4
+ include Concerns::Contained
5
+ delegate :name, to: :declaration
6
+ delegate :type, to: :declaration
7
+ delegate :sub_type, to: :type
8
+
9
+ def resolved_type
10
+ cur = self
11
+
12
+ cur = root.find_definition(cur.type.text_value) while cur.is_a?(Typedef)
13
+
14
+ cur
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class Union < Base
4
+ extend Memoist
5
+ include Concerns::Named
6
+ include Concerns::Namespace
7
+ include Concerns::Contained
8
+
9
+ delegate :discriminant, to: :union_body
10
+ delegate :name, to: :discriminant, prefix:true
11
+ delegate :arms, to: :union_body
12
+ delegate :normal_arms, to: :union_body
13
+ delegate :default_arm, to: :union_body
14
+
15
+ memoize def discriminant_type
16
+ return nil unless discriminant.type.is_a?(Identifier)
17
+
18
+ root.find_definition discriminant.type.name
19
+ end
20
+
21
+ def resolved_case(kase)
22
+ if discriminant_type.nil? then
23
+ # discriminant_type has not been found we need to search for the value in namespace's enum constants.
24
+ # It's a case where union discriminant is a standard type (like `int`):
25
+ #
26
+ # enum StellarValueType
27
+ # {
28
+ # STELLAR_VALUE_BASIC = 0,
29
+ # STELLAR_VALUE_SIGNED = 1
30
+ # };
31
+ #
32
+ # union switch (int v)
33
+ # {
34
+ # case STELLAR_VALUE_BASIC:
35
+ # void;
36
+ # ...
37
+ found = namespace.find_enum_value(kase.value_s)
38
+ raise "Case error: #{kase} (#{kase.value_s}) constant not found" if found.nil?
39
+ else
40
+ found = discriminant_type.members.find{|m| m.name == kase.value_s}
41
+ raise "Case error: #{kase} is not a member of #{discriminant_type.name}" if found.nil?
42
+ end
43
+ found
44
+ end
45
+
46
+ def nested_definitions
47
+ arms.
48
+ map(&:declaration).
49
+ reject{|d| d.is_a?(Declarations::Void)}.
50
+ map(&:type).
51
+ select{|d| d.is_a?(Concerns::NestedDefinition)}
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,33 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class UnionArm < Base
4
+ extend Memoist
5
+ include Concerns::Contained
6
+
7
+ delegate :name, to: :declaration
8
+ delegate :type, to: :declaration
9
+
10
+ memoize def union
11
+ find_ancestors(Union).last
12
+ end
13
+
14
+ def cases
15
+ cases_n.elements
16
+ end
17
+
18
+ def resolved_case(kase)
19
+ union.resolved_case kase
20
+ end
21
+
22
+ def resolved_cases
23
+ enum = union.discriminant_type
24
+
25
+ cases.map{|kase| resolved_case kase}
26
+ end
27
+
28
+ def void?
29
+ declaration.is_a?(Declarations::Void)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class UnionArmCase < Base
4
+
5
+ def value_s
6
+ value.text_value
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class UnionBody < Base
4
+ extend Memoist
5
+
6
+ memoize def arms
7
+ [
8
+ normal_arms,
9
+ default_arm,
10
+ ].flatten.compact
11
+ end
12
+
13
+ def normal_arms
14
+ arms_n.elements.select{|c| c.is_a?(UnionArm)}
15
+ end
16
+
17
+ def default_arm
18
+ default_case_n unless default_case_n.terminal?
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Xdrgen::AST
2
+ module Definitions
3
+ class UnionDefaultArm < Base
4
+ extend Memoist
5
+ include Concerns::Contained
6
+
7
+ delegate :name, to: :declaration
8
+ delegate :type, to: :declaration
9
+
10
+ memoize def union
11
+ find_ancestors(Union).last
12
+ end
13
+
14
+ def void?
15
+ declaration.is_a?(Declarations::Void)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Xdrgen::AST
2
+ class FixedSize < Treetop::Runtime::SyntaxNode
3
+ def size
4
+ size_t.text_value
5
+ end
6
+
7
+ def resolved_size
8
+ return size unless named?
9
+
10
+ resolved = root.find_definition(size)
11
+
12
+ if resolved.blank?
13
+ raise "Could not resolve constant: #{size}"
14
+ end
15
+
16
+ resolved.value
17
+ end
18
+
19
+ def named?
20
+ size_t.is_a?(Xdrgen::AST::Identifier)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Xdrgen::AST
2
+ class HexadecimalConstant < Constant
3
+ def value
4
+ text_value.to_i(16)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST
2
+ class Identifier < Treetop::Runtime::SyntaxNode
3
+ alias_method :name, :text_value
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Xdrgen::AST
2
+ class OctalConstant < Constant
3
+ def value
4
+ text_value.to_i(8)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST
2
+ class Top < Treetop::Runtime::SyntaxNode
3
+ include Concerns::HasDefinitions
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ module Xdrgen::AST::Typespecs
2
+ module Base
3
+ include Xdrgen::AST::Concerns::Contained
4
+
5
+ def decl
6
+ find_ancestors(Xdrgen::AST::Declarations::Base).last
7
+ end
8
+
9
+ def sub_type
10
+ case decl
11
+ when Xdrgen::AST::Declarations::Optional
12
+ :optional
13
+ when Xdrgen::AST::Declarations::Array
14
+ decl.fixed? ? :array : :var_array
15
+ else
16
+ :simple
17
+ end
18
+ end
19
+
20
+ def array_size
21
+ raise "Called array_size on a non-array!" unless decl.is_a?(Xdrgen::AST::Declarations::Array)
22
+
23
+ [decl.size_spec.named?, decl.size]
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Bool < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Double < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Float < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Hyper < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Int < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Opaque < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+
5
+ delegate :size, to: :decl
6
+ delegate :name, to: :decl
7
+ delegate :fixed?, to: :decl
8
+
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class Quadruple < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module Xdrgen::AST::Typespecs
2
+ module Simple
3
+ include Base
4
+
5
+ def resolved_type
6
+ name = self.text_value.split("::").last
7
+ result = root.find_definition name
8
+
9
+ raise Xdrgen::TypeResolutionError, "Cannot resolve type `#{name}`" if result.blank?
10
+ result
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class String < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+
5
+ delegate :size, to: :decl
6
+ delegate :name, to: :decl
7
+
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class UnsignedHyper < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST::Typespecs
2
+ class UnsignedInt < Treetop::Runtime::SyntaxNode
3
+ include Base
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ module Xdrgen::AST
2
+ class VarSize < Treetop::Runtime::SyntaxNode
3
+ include Concerns::Contained
4
+
5
+ def size
6
+ return nil if size_t.text_value.blank?
7
+ size_t.text_value
8
+ end
9
+
10
+ def resolved_size
11
+ return size unless named?
12
+
13
+ resolved = root.find_definition(size)
14
+
15
+ if resolved.blank?
16
+ raise "Could not resolve constant: #{size}"
17
+ end
18
+
19
+ resolved.value
20
+ end
21
+
22
+ def named?
23
+ size_t.is_a?(Xdrgen::AST::Identifier)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Xdrgen::AST
2
+ module Whitespace
3
+
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require 'slop'
2
+
3
+ module Xdrgen
4
+ module CLI
5
+ def self.run(args)
6
+ args = args.dup
7
+ opts = Slop.parse! args do
8
+ banner 'Usage: xdrgen -o OUTPUT_DIR INPUT --gen=ruby'
9
+ on 'o', 'output=', 'The output directory'
10
+ on 'l', 'language=', 'The output language', default: 'ruby'
11
+ on 'n', 'namespace=', '"namespace" to generate code within (language-specific)'
12
+ end
13
+
14
+ fail(opts) if args.blank?
15
+ fail(opts) if opts[:output].blank?
16
+
17
+ compilation = Compilation.new(
18
+ args,
19
+ output_dir: opts[:output],
20
+ language: opts[:language].to_sym,
21
+ namespace: opts[:namespace]
22
+ )
23
+ compilation.compile
24
+ end
25
+
26
+ def self.fail(slop, code=1)
27
+ STDERR.puts slop
28
+ exit(code)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ module Xdrgen
2
+ class Compilation
3
+ extend Memoist
4
+
5
+ def initialize(source_paths, output_dir:".", language: :ruby, namespace: nil)
6
+ @source_paths = source_paths
7
+ @output_dir = output_dir
8
+ @namespace = namespace
9
+ @language = language
10
+ end
11
+
12
+ memoize def source
13
+ @source_paths.map{|p| IO.read(p)}.join("\n")
14
+ end
15
+
16
+ memoize def ast
17
+ parser = Parser.new
18
+ parser.parse(source)
19
+ end
20
+
21
+ def compile
22
+ output = Output.new(@source_paths, @output_dir)
23
+
24
+
25
+ generator = Generators.for_language(@language).new(ast, output, @namespace)
26
+ generator.generate
27
+ ensure
28
+ output.close
29
+ end
30
+ end
31
+ end