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,8 @@
1
+ typedef int Arr[2];
2
+
3
+ struct HasOptions
4
+ {
5
+ int* firstOption;
6
+ int *secondOption;
7
+ Arr *thirdOption;
8
+ };
@@ -0,0 +1,10 @@
1
+ typedef hyper int64;
2
+
3
+ struct MyStruct
4
+ {
5
+ int someInt;
6
+ int64 aBigInt;
7
+ opaque someOpaque[10];
8
+ string someString<>;
9
+ string maxString<100>;
10
+ };
@@ -0,0 +1,77 @@
1
+ %#include "generated/FBAXDR.h"
2
+
3
+ namespace MyNamespace {
4
+
5
+ // messages
6
+ typedef opaque uint512[64];
7
+ typedef opaque uint513<64>;
8
+ typedef opaque uint514<>;
9
+ typedef string str<64>;
10
+ typedef string str2<>;
11
+
12
+ typedef opaque Hash[32];
13
+ typedef Hash Hashes1[12];
14
+ typedef Hash Hashes2<12>;
15
+ typedef Hash Hashes3<>;
16
+
17
+
18
+ typedef Hash *optHash1;
19
+ typedef Hash* optHash2;
20
+
21
+ typedef int int1;
22
+ typedef hyper int2;
23
+ typedef unsigned int int3;
24
+ typedef unsigned hyper int4;
25
+
26
+ struct MyStruct
27
+ {
28
+ uint512 field1;
29
+ optHash1 field2;
30
+ int1 field3;
31
+ unsigned int field4;
32
+ float field5;
33
+ double field6;
34
+ bool field7;
35
+ };
36
+
37
+ struct LotsOfMyStructs
38
+ {
39
+ MyStruct members<>;
40
+ };
41
+
42
+ struct HasStuff
43
+ {
44
+ LotsOfMyStructs data;
45
+ };
46
+
47
+ enum Color {
48
+ RED,
49
+ BLUE = 5,
50
+ GREEN
51
+ };
52
+
53
+ const FOO = 1244;
54
+ const BAR = FOO;
55
+
56
+ struct Nester
57
+ {
58
+ enum {
59
+ BLAH_1,
60
+ BLAH_2
61
+ } nestedEnum;
62
+
63
+ struct {
64
+ int blah;
65
+ } nestedStruct;
66
+
67
+ union switch (Color color) {
68
+ case RED:
69
+ void;
70
+ default:
71
+ int blah2;
72
+ } nestedUnion;
73
+
74
+
75
+ };
76
+
77
+ }
@@ -0,0 +1,28 @@
1
+ typedef int Error;
2
+ typedef int Multi;
3
+
4
+ enum UnionKey {
5
+ ERROR,
6
+ MULTI
7
+ };
8
+
9
+ union MyUnion switch (UnionKey type)
10
+ {
11
+ case ERROR:
12
+ Error error;
13
+ case MULTI:
14
+ Multi things<>;
15
+
16
+
17
+ };
18
+
19
+ union IntUnion switch (int type)
20
+ {
21
+ case 0:
22
+ Error error;
23
+ case 1:
24
+ Multi things<>;
25
+
26
+ };
27
+
28
+ typedef IntUnion IntUnion2;
@@ -0,0 +1,14 @@
1
+
2
+ enum AccountFlags
3
+ { // masks for each flag
4
+ AUTH_REQUIRED_FLAG = 0x1
5
+ };
6
+
7
+ /* AccountEntry
8
+
9
+ Main entry representing a user in Stellar. All transactions are performed
10
+ using an account.
11
+
12
+ Other ledger entries created require an account.
13
+
14
+ */
@@ -0,0 +1 @@
1
+ const FOO = 1;
@@ -0,0 +1,35 @@
1
+ enum MessageType
2
+ {
3
+ ERROR_MSG,
4
+ HELLO,
5
+ DONT_HAVE,
6
+
7
+ GET_PEERS, // gets a list of peers this guy knows about
8
+ PEERS,
9
+
10
+ GET_TX_SET, // gets a particular txset by hash
11
+ TX_SET,
12
+
13
+ GET_VALIDATIONS, // gets validations for a given ledger hash
14
+ VALIDATIONS,
15
+
16
+ TRANSACTION, //pass on a tx you have heard about
17
+ JSON_TRANSACTION,
18
+
19
+ // FBA
20
+ GET_FBA_QUORUMSET,
21
+ FBA_QUORUMSET,
22
+ FBA_MESSAGE
23
+ };
24
+
25
+ enum Color {
26
+ RED=0,
27
+ GREEN=1,
28
+ BLUE=2
29
+ };
30
+
31
+ enum Color2 {
32
+ RED2=RED,
33
+ GREEN2=1,
34
+ BLUE2=2
35
+ };
@@ -0,0 +1,26 @@
1
+ union MyUnion switch (UnionKey type)
2
+ {
3
+ case ONE:
4
+ struct {
5
+ int someInt;
6
+ } one;
7
+
8
+ case TWO:
9
+ struct {
10
+ int someInt;
11
+ Foo foo;
12
+ } two;
13
+
14
+ case OFFER:
15
+ void;
16
+ };
17
+
18
+ struct MyStruct
19
+ {
20
+ union switch (int v)
21
+ {
22
+ case 0:
23
+ void;
24
+ }
25
+ ext;
26
+ };
@@ -0,0 +1,8 @@
1
+ typedef Arr int[2];
2
+
3
+ struct HasOptions
4
+ {
5
+ int* firstOption;
6
+ int *secondOption;
7
+ Arr *thirdOption;
8
+ };
@@ -0,0 +1,8 @@
1
+ struct MyStruct
2
+ {
3
+ int someInt;
4
+ int64 aBigInt;
5
+ opaque someOpaque[10];
6
+ string someString<>;
7
+ string maxString<100>;
8
+ };
@@ -0,0 +1,77 @@
1
+ %#include "generated/FBAXDR.h"
2
+
3
+ namespace MyNamespace {
4
+
5
+ // messages
6
+ typedef opaque uint512[64];
7
+ typedef opaque uint513<64>;
8
+ typedef opaque uint514<>;
9
+ typedef string str<64>;
10
+ typedef string str2<>;
11
+
12
+ typedef Hash Hashes1[12];
13
+ typedef Hash Hashes2<12>;
14
+ typedef Hash Hashes3<>;
15
+
16
+
17
+ typedef Hash *optHash1;
18
+ typedef Hash* optHash2;
19
+
20
+ typedef int int1;
21
+ typedef hyper int2;
22
+ typedef unsigned int int3;
23
+ typedef unsigned hyper int4;
24
+
25
+ struct MyStruct
26
+ {
27
+ uint512 field1;
28
+ optHash1 field2;
29
+ int1 field3;
30
+ unsigned int field4;
31
+ float field5;
32
+ double field6;
33
+ quadruple field7;
34
+ bool field8;
35
+ };
36
+
37
+ struct LotsOfMyStructs
38
+ {
39
+ MyStruct members<>;
40
+ };
41
+
42
+ struct HasStuff
43
+ {
44
+ LotsOfMyStructs data;
45
+ };
46
+
47
+ enum Color {
48
+ RED,
49
+ BLUE = 5,
50
+ GREEN
51
+ };
52
+
53
+ const FOO = 1244;
54
+ const BAR = FOO;
55
+
56
+ struct Nester
57
+ {
58
+ enum {
59
+ BLAH_1,
60
+ BLAH_2
61
+ } nestedEnum;
62
+
63
+ struct {
64
+ int blah;
65
+ } nestedStruct;
66
+
67
+ union switch (Color color) {
68
+ case RED:
69
+ void;
70
+ default:
71
+ int blah2;
72
+ } nestedUnion;
73
+
74
+
75
+ };
76
+
77
+ }
@@ -0,0 +1,10 @@
1
+
2
+ union MyUnion switch (UnionKey type)
3
+ {
4
+ case ERROR:
5
+ Error error;
6
+ case MULTI:
7
+ Multi things<>;
8
+
9
+
10
+ };
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Xdrgen::Generators do
4
+ languages = %w(ruby javascript go java elixir)
5
+ focus_language = "" #"go"
6
+ focus_basename = "" #"optional.x"
7
+
8
+ generator_fixture_paths.each do |path|
9
+ languages.each do |lang|
10
+ next if focus_basename.present? && File.basename(path) != focus_basename
11
+ next if focus_language.present? && lang != focus_language
12
+
13
+ it "can generate #{File.basename path} in #{lang}" do
14
+ c = generate lang, path
15
+ end
16
+
17
+ end
18
+ end
19
+
20
+ def generate(language, path)
21
+ compilation = Xdrgen::Compilation.new(
22
+ [path],
23
+ output_dir: "tmp/generator_spec_#{language}/#{File.basename path}",
24
+ language: language,
25
+ namespace: "MyXDR"
26
+ )
27
+ compilation.compile
28
+ compilation
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Xdrgen::OutputFile, "#balance_after" do
4
+ let(:output_path){ "#{SPEC_ROOT}/../tmp/balanced.txt"}
5
+ let(:unbalanced) do
6
+ <<-EOS.strip_heredoc
7
+ attribute :hello, XDR::UnsignedInt
8
+ attribute :i_am_a_long_field, XDR::UnsignedInt
9
+ attribute :s, XDR::UnsignedInt
10
+ EOS
11
+ end
12
+
13
+ let(:actual){ IO.read(output_path) }
14
+ let(:balanced) do
15
+ <<-EOS.strip_heredoc
16
+ attribute :hello, XDR::UnsignedInt
17
+ attribute :i_am_a_long_field, XDR::UnsignedInt
18
+ attribute :s, XDR::UnsignedInt
19
+ EOS
20
+ end
21
+
22
+ subject{ Xdrgen::OutputFile.new(output_path) }
23
+
24
+ after(:each){ FileUtils.rm output_path }
25
+
26
+ it "balanaces the input string on each line after splitting on the provided regex" do
27
+ subject.balance_after /.+?,/ do
28
+ subject.puts unbalanced
29
+ end
30
+ subject.close
31
+ expect(actual).to eq(balanced)
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Xdrgen::Parser, ".parse" do
4
+
5
+ it "can parse all of the fixtures" do
6
+ results = parser_fixture_paths.map do |path|
7
+ content = IO.read(path)
8
+ {path:path}.merge parse(content)
9
+ end
10
+
11
+ failed = results.select{|r| !r[:success]}
12
+
13
+ if failed.any?
14
+ failures = failed.map{|r| "\t#{r[:path]} failed on line #{r[:failure_line]}"}
15
+ fail "couldn't parse:\n#{failures.join("\n")}"
16
+ end
17
+ end
18
+
19
+ def parse(content)
20
+ begin
21
+ subject.parse(content)
22
+ {success: true}
23
+ rescue Xdrgen::ParseError
24
+ {success: false, failure_line: subject.failure_line}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'pry'
5
+ require 'xdrgen'
6
+
7
+ SPEC_ROOT = __dir__
8
+
9
+ Dir["#{__dir__}/support/**/*.rb"].each { |f| require f }
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
@@ -0,0 +1,27 @@
1
+ module FixtureProvider
2
+ extend ActiveSupport::Concern
3
+
4
+ included do |base|
5
+ let(:fixture_paths) { self.class.fixture_paths }
6
+ let(:parser_fixture_paths) { self.class.parser_fixture_paths }
7
+ let(:generator_fixture_paths){ self.class.generator_fixture_paths }
8
+ end
9
+
10
+ class_methods do
11
+ def fixture_paths
12
+ Dir["#{__dir__}/../fixtures/**/*.x"]
13
+ end
14
+
15
+ def parser_fixture_paths
16
+ Dir["#{__dir__}/../fixtures/parser/**/*.x"]
17
+ end
18
+
19
+ def generator_fixture_paths
20
+ Dir["#{__dir__}/../fixtures/generator/**/*.x"]
21
+ end
22
+ end
23
+ end
24
+
25
+ RSpec::configure do |c|
26
+ c.include FixtureProvider
27
+ end