xdrgen 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Guardfile +12 -0
- data/LICENSE.txt +202 -0
- data/README.md +79 -0
- data/Rakefile +6 -0
- data/bin/xdrgen +5 -0
- data/lib/xdrgen.rb +24 -0
- data/lib/xdrgen/ast.rb +81 -0
- data/lib/xdrgen/ast/concerns/contained.rb +32 -0
- data/lib/xdrgen/ast/concerns/has_children.rb +9 -0
- data/lib/xdrgen/ast/concerns/has_definitions.rb +90 -0
- data/lib/xdrgen/ast/concerns/named.rb +16 -0
- data/lib/xdrgen/ast/concerns/namespace.rb +7 -0
- data/lib/xdrgen/ast/concerns/nested_definition.rb +16 -0
- data/lib/xdrgen/ast/constant.rb +7 -0
- data/lib/xdrgen/ast/decimal_constant.rb +7 -0
- data/lib/xdrgen/ast/declarations/array.rb +15 -0
- data/lib/xdrgen/ast/declarations/base.rb +28 -0
- data/lib/xdrgen/ast/declarations/opaque.rb +11 -0
- data/lib/xdrgen/ast/declarations/optional.rb +5 -0
- data/lib/xdrgen/ast/declarations/simple.rb +7 -0
- data/lib/xdrgen/ast/declarations/string.rb +7 -0
- data/lib/xdrgen/ast/declarations/void.rb +7 -0
- data/lib/xdrgen/ast/definitions/base.rb +9 -0
- data/lib/xdrgen/ast/definitions/const.rb +12 -0
- data/lib/xdrgen/ast/definitions/enum.rb +17 -0
- data/lib/xdrgen/ast/definitions/enum_member.rb +44 -0
- data/lib/xdrgen/ast/definitions/namespace.rb +9 -0
- data/lib/xdrgen/ast/definitions/nested_enum.rb +7 -0
- data/lib/xdrgen/ast/definitions/nested_struct.rb +7 -0
- data/lib/xdrgen/ast/definitions/nested_union.rb +7 -0
- data/lib/xdrgen/ast/definitions/struct.rb +18 -0
- data/lib/xdrgen/ast/definitions/struct_body.rb +11 -0
- data/lib/xdrgen/ast/definitions/struct_member.rb +12 -0
- data/lib/xdrgen/ast/definitions/typedef.rb +19 -0
- data/lib/xdrgen/ast/definitions/union.rb +55 -0
- data/lib/xdrgen/ast/definitions/union_arm.rb +33 -0
- data/lib/xdrgen/ast/definitions/union_arm_case.rb +11 -0
- data/lib/xdrgen/ast/definitions/union_body.rb +22 -0
- data/lib/xdrgen/ast/definitions/union_default_arm.rb +19 -0
- data/lib/xdrgen/ast/fixed_size.rb +23 -0
- data/lib/xdrgen/ast/hexadecimal_constant.rb +7 -0
- data/lib/xdrgen/ast/identifier.rb +5 -0
- data/lib/xdrgen/ast/octal_constant.rb +7 -0
- data/lib/xdrgen/ast/top.rb +5 -0
- data/lib/xdrgen/ast/typespecs/base.rb +27 -0
- data/lib/xdrgen/ast/typespecs/bool.rb +5 -0
- data/lib/xdrgen/ast/typespecs/double.rb +5 -0
- data/lib/xdrgen/ast/typespecs/float.rb +5 -0
- data/lib/xdrgen/ast/typespecs/hyper.rb +6 -0
- data/lib/xdrgen/ast/typespecs/int.rb +6 -0
- data/lib/xdrgen/ast/typespecs/opaque.rb +10 -0
- data/lib/xdrgen/ast/typespecs/quadruple.rb +5 -0
- data/lib/xdrgen/ast/typespecs/simple.rb +14 -0
- data/lib/xdrgen/ast/typespecs/string.rb +9 -0
- data/lib/xdrgen/ast/typespecs/unsigned_hyper.rb +5 -0
- data/lib/xdrgen/ast/typespecs/unsigned_int.rb +5 -0
- data/lib/xdrgen/ast/var_size.rb +26 -0
- data/lib/xdrgen/ast/whitespace.rb +5 -0
- data/lib/xdrgen/cli.rb +31 -0
- data/lib/xdrgen/compilation.rb +31 -0
- data/lib/xdrgen/generators.rb +16 -0
- data/lib/xdrgen/generators/base.rb +11 -0
- data/lib/xdrgen/generators/elixir.rb +260 -0
- data/lib/xdrgen/generators/go.rb +578 -0
- data/lib/xdrgen/generators/java.rb +810 -0
- data/lib/xdrgen/generators/java/XdrDataInputStream.erb +122 -0
- data/lib/xdrgen/generators/java/XdrDataOutputStream.erb +96 -0
- data/lib/xdrgen/generators/java/XdrElement.erb +10 -0
- data/lib/xdrgen/generators/java/XdrString.erb +58 -0
- data/lib/xdrgen/generators/javascript.rb +248 -0
- data/lib/xdrgen/generators/ruby.rb +283 -0
- data/lib/xdrgen/grammar/base.treetop +71 -0
- data/lib/xdrgen/grammar/comments.treetop +15 -0
- data/lib/xdrgen/grammar/const.treetop +8 -0
- data/lib/xdrgen/grammar/declaration.treetop +99 -0
- data/lib/xdrgen/grammar/enum.treetop +46 -0
- data/lib/xdrgen/grammar/main.treetop +30 -0
- data/lib/xdrgen/grammar/namespace.treetop +12 -0
- data/lib/xdrgen/grammar/struct.treetop +32 -0
- data/lib/xdrgen/grammar/typedef.treetop +12 -0
- data/lib/xdrgen/grammar/union.treetop +63 -0
- data/lib/xdrgen/output.rb +37 -0
- data/lib/xdrgen/output_file.rb +87 -0
- data/lib/xdrgen/parser.rb +40 -0
- data/lib/xdrgen/version.rb +3 -0
- data/spec/fixtures/generator/block_comments.x +14 -0
- data/spec/fixtures/generator/const.x +4 -0
- data/spec/fixtures/generator/enum.x +36 -0
- data/spec/fixtures/generator/nesting.x +24 -0
- data/spec/fixtures/generator/optional.x +8 -0
- data/spec/fixtures/generator/struct.x +10 -0
- data/spec/fixtures/generator/test.x +77 -0
- data/spec/fixtures/generator/union.x +28 -0
- data/spec/fixtures/parser/block_comments.x +14 -0
- data/spec/fixtures/parser/const.x +1 -0
- data/spec/fixtures/parser/enum.x +35 -0
- data/spec/fixtures/parser/nesting.x +26 -0
- data/spec/fixtures/parser/optional.x +8 -0
- data/spec/fixtures/parser/struct.x +8 -0
- data/spec/fixtures/parser/test.x +77 -0
- data/spec/fixtures/parser/union.x +10 -0
- data/spec/lib/xdrgen/generator_spec.rb +30 -0
- data/spec/lib/xdrgen/output_file_spec.rb +33 -0
- data/spec/lib/xdrgen/parser_spec.rb +27 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/fixtures.rb +27 -0
- data/xdrgen.gemspec +32 -0
- metadata +301 -0
@@ -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,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,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
|
data/spec/spec_helper.rb
ADDED
@@ -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
|