xdrgen 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,122 @@
|
|
|
1
|
+
package <%= @namespace %>;
|
|
2
|
+
|
|
3
|
+
import java.io.DataInputStream;
|
|
4
|
+
import java.io.IOException;
|
|
5
|
+
import java.io.InputStream;
|
|
6
|
+
import java.nio.charset.Charset;
|
|
7
|
+
|
|
8
|
+
public class XdrDataInputStream extends DataInputStream {
|
|
9
|
+
|
|
10
|
+
// The underlying input stream
|
|
11
|
+
private final XdrInputStream mIn;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a XdrDataInputStream that uses the specified
|
|
15
|
+
* underlying InputStream.
|
|
16
|
+
*
|
|
17
|
+
* @param in the specified input stream
|
|
18
|
+
*/
|
|
19
|
+
public XdrDataInputStream(InputStream in) {
|
|
20
|
+
super(new XdrInputStream(in));
|
|
21
|
+
mIn = (XdrInputStream) super.in;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public int[] readIntArray() throws IOException {
|
|
25
|
+
int l = readInt();
|
|
26
|
+
return readIntArray(l);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private int[] readIntArray(int l) throws IOException {
|
|
30
|
+
int[] arr = new int[l];
|
|
31
|
+
for (int i = 0; i < l; i++) {
|
|
32
|
+
arr[i] = readInt();
|
|
33
|
+
}
|
|
34
|
+
return arr;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public float[] readFloatArray() throws IOException {
|
|
38
|
+
int l = readInt();
|
|
39
|
+
return readFloatArray(l);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private float[] readFloatArray(int l) throws IOException {
|
|
43
|
+
float[] arr = new float[l];
|
|
44
|
+
for (int i = 0; i < l; i++) {
|
|
45
|
+
arr[i] = readFloat();
|
|
46
|
+
}
|
|
47
|
+
return arr;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public double[] readDoubleArray() throws IOException {
|
|
51
|
+
int l = readInt();
|
|
52
|
+
return readDoubleArray(l);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private double[] readDoubleArray(int l) throws IOException {
|
|
56
|
+
double[] arr = new double[l];
|
|
57
|
+
for (int i = 0; i < l; i++) {
|
|
58
|
+
arr[i] = readDouble();
|
|
59
|
+
}
|
|
60
|
+
return arr;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@Override
|
|
64
|
+
public int read() throws IOException {
|
|
65
|
+
return super.read();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Need to provide a custom impl of InputStream as DataInputStream's read methods
|
|
70
|
+
* are final and we need to keep track of the count for padding purposes.
|
|
71
|
+
*/
|
|
72
|
+
private static final class XdrInputStream extends InputStream {
|
|
73
|
+
|
|
74
|
+
// The underlying input stream
|
|
75
|
+
private final InputStream mIn;
|
|
76
|
+
|
|
77
|
+
// The amount of bytes read so far.
|
|
78
|
+
private int mCount;
|
|
79
|
+
|
|
80
|
+
public XdrInputStream(InputStream in) {
|
|
81
|
+
mIn = in;
|
|
82
|
+
mCount = 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@Override
|
|
86
|
+
public int read() throws IOException {
|
|
87
|
+
int read = mIn.read();
|
|
88
|
+
if (read >= 0) {
|
|
89
|
+
mCount++;
|
|
90
|
+
}
|
|
91
|
+
return read;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Override
|
|
95
|
+
public int read(byte[] b) throws IOException {
|
|
96
|
+
return read(b, 0, b.length);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Override
|
|
100
|
+
public int read(byte[] b, int off, int len) throws IOException {
|
|
101
|
+
int read = mIn.read(b, off, len);
|
|
102
|
+
mCount += read;
|
|
103
|
+
pad();
|
|
104
|
+
return read;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public void pad() throws IOException {
|
|
108
|
+
int pad = 0;
|
|
109
|
+
int mod = mCount % 4;
|
|
110
|
+
if (mod > 0) {
|
|
111
|
+
pad = 4-mod;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
while (pad-- > 0) {
|
|
115
|
+
int b = read();
|
|
116
|
+
if (b != 0) {
|
|
117
|
+
throw new IOException("non-zero padding");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
package <%= @namespace %>;
|
|
2
|
+
|
|
3
|
+
import java.io.DataOutputStream;
|
|
4
|
+
import java.io.IOException;
|
|
5
|
+
import java.io.OutputStream;
|
|
6
|
+
import java.nio.charset.Charset;
|
|
7
|
+
|
|
8
|
+
public class XdrDataOutputStream extends DataOutputStream {
|
|
9
|
+
|
|
10
|
+
private final XdrOutputStream mOut;
|
|
11
|
+
|
|
12
|
+
public XdrDataOutputStream(OutputStream out) {
|
|
13
|
+
super(new XdrOutputStream(out));
|
|
14
|
+
mOut = (XdrOutputStream) super.out;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public void writeIntArray(int[] a) throws IOException {
|
|
18
|
+
writeInt(a.length);
|
|
19
|
+
writeIntArray(a, a.length);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private void writeIntArray(int[] a, int l) throws IOException {
|
|
23
|
+
for (int i = 0; i < l; i++) {
|
|
24
|
+
writeInt(a[i]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public void writeFloatArray(float[] a) throws IOException {
|
|
29
|
+
writeInt(a.length);
|
|
30
|
+
writeFloatArray(a, a.length);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private void writeFloatArray(float[] a, int l) throws IOException {
|
|
34
|
+
for (int i = 0; i < l; i++) {
|
|
35
|
+
writeFloat(a[i]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public void writeDoubleArray(double[] a) throws IOException {
|
|
40
|
+
writeInt(a.length);
|
|
41
|
+
writeDoubleArray(a, a.length);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private void writeDoubleArray(double[] a, int l) throws IOException {
|
|
45
|
+
for (int i = 0; i < l; i++) {
|
|
46
|
+
writeDouble(a[i]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private static final class XdrOutputStream extends OutputStream {
|
|
51
|
+
|
|
52
|
+
private final OutputStream mOut;
|
|
53
|
+
|
|
54
|
+
// Number of bytes written
|
|
55
|
+
private int mCount;
|
|
56
|
+
|
|
57
|
+
public XdrOutputStream(OutputStream out) {
|
|
58
|
+
mOut = out;
|
|
59
|
+
mCount = 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@Override
|
|
63
|
+
public void write(int b) throws IOException {
|
|
64
|
+
mOut.write(b);
|
|
65
|
+
// https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int):
|
|
66
|
+
// > The byte to be written is the eight low-order bits of the argument b.
|
|
67
|
+
// > The 24 high-order bits of b are ignored.
|
|
68
|
+
mCount++;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@Override
|
|
72
|
+
public void write(byte[] b) throws IOException {
|
|
73
|
+
// https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(byte[]):
|
|
74
|
+
// > The general contract for write(b) is that it should have exactly the same effect
|
|
75
|
+
// > as the call write(b, 0, b.length).
|
|
76
|
+
write(b, 0, b.length);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public void write(byte[] b, int offset, int length) throws IOException {
|
|
80
|
+
mOut.write(b, offset, length);
|
|
81
|
+
mCount += length;
|
|
82
|
+
pad();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public void pad() throws IOException {
|
|
86
|
+
int pad = 0;
|
|
87
|
+
int mod = mCount % 4;
|
|
88
|
+
if (mod > 0) {
|
|
89
|
+
pad = 4-mod;
|
|
90
|
+
}
|
|
91
|
+
while (pad-- > 0) {
|
|
92
|
+
write(0);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
package <%= @namespace %>;
|
|
2
|
+
|
|
3
|
+
import java.io.IOException;
|
|
4
|
+
import java.io.InvalidClassException;
|
|
5
|
+
import java.nio.charset.Charset;
|
|
6
|
+
import java.util.Arrays;
|
|
7
|
+
|
|
8
|
+
public class XdrString implements XdrElement {
|
|
9
|
+
private byte[] bytes;
|
|
10
|
+
|
|
11
|
+
public XdrString(byte[] bytes) {
|
|
12
|
+
this.bytes = bytes;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public XdrString(String text) {
|
|
16
|
+
this.bytes = text.getBytes(Charset.forName("UTF-8"));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@Override
|
|
20
|
+
public void encode(XdrDataOutputStream stream) throws IOException {
|
|
21
|
+
stream.writeInt(this.bytes.length);
|
|
22
|
+
stream.write(this.bytes, 0, this.bytes.length);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
|
|
26
|
+
int size = stream.readInt();
|
|
27
|
+
if (size > maxSize) {
|
|
28
|
+
throw new InvalidClassException("String length "+size+" exceeds max size "+maxSize);
|
|
29
|
+
}
|
|
30
|
+
byte[] bytes = new byte[size];
|
|
31
|
+
stream.read(bytes);
|
|
32
|
+
return new XdrString(bytes);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public byte[] getBytes() {
|
|
36
|
+
return this.bytes;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@Override
|
|
40
|
+
public int hashCode() {
|
|
41
|
+
return Arrays.hashCode(this.bytes);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Override
|
|
45
|
+
public boolean equals(Object object) {
|
|
46
|
+
if (object == null || !(object instanceof XdrString)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
XdrString other = (XdrString) object;
|
|
51
|
+
return Arrays.equals(this.bytes, other.bytes);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
@Override
|
|
55
|
+
public String toString() {
|
|
56
|
+
return new String(bytes, Charset.forName("UTF-8"));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
module Xdrgen
|
|
2
|
+
module Generators
|
|
3
|
+
|
|
4
|
+
class Javascript < Xdrgen::Generators::Base
|
|
5
|
+
MAX_INT = (2**31) - 1
|
|
6
|
+
def generate
|
|
7
|
+
path = "#{@namespace}_generated.js"
|
|
8
|
+
out = @output.open(path)
|
|
9
|
+
|
|
10
|
+
render_top_matter out
|
|
11
|
+
render_define_block(out) do
|
|
12
|
+
render_definitions(out, @top)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
def render_definitions(out, node)
|
|
18
|
+
node.definitions.each{|n| render_definition out, n }
|
|
19
|
+
node.namespaces.each{|n| render_definitions out, n }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def render_nested_definitions(out, defn)
|
|
23
|
+
return unless defn.respond_to? :nested_definitions
|
|
24
|
+
defn.nested_definitions.each{|ndefn| render_definition out, ndefn}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def render_definition(out, defn)
|
|
28
|
+
render_nested_definitions(out, defn)
|
|
29
|
+
render_source_comment(out, defn)
|
|
30
|
+
|
|
31
|
+
case defn
|
|
32
|
+
when AST::Definitions::Struct ;
|
|
33
|
+
render_struct out, defn
|
|
34
|
+
when AST::Definitions::Enum ;
|
|
35
|
+
render_enum out, defn
|
|
36
|
+
when AST::Definitions::Union ;
|
|
37
|
+
render_union out, defn
|
|
38
|
+
when AST::Definitions::Typedef ;
|
|
39
|
+
render_typedef out, defn
|
|
40
|
+
when AST::Definitions::Const ;
|
|
41
|
+
render_const out, defn
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
out.break
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def render_source_comment(out, defn)
|
|
48
|
+
return if defn.is_a?(AST::Definitions::Namespace)
|
|
49
|
+
|
|
50
|
+
out.puts <<-EOS.strip_heredoc
|
|
51
|
+
// === xdr source ============================================================
|
|
52
|
+
//
|
|
53
|
+
EOS
|
|
54
|
+
|
|
55
|
+
out.puts "// " + defn.text_value.split("\n").join("\n// ")
|
|
56
|
+
|
|
57
|
+
out.puts <<-EOS.strip_heredoc
|
|
58
|
+
//
|
|
59
|
+
// ===========================================================================
|
|
60
|
+
EOS
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def render_top_matter(out)
|
|
65
|
+
out.puts <<-EOS.strip_heredoc
|
|
66
|
+
// Automatically generated on #{Time.now.iso8601}
|
|
67
|
+
// DO NOT EDIT or your changes may be overwritten
|
|
68
|
+
|
|
69
|
+
/* jshint maxstatements:2147483647 */
|
|
70
|
+
/* jshint esnext:true */
|
|
71
|
+
|
|
72
|
+
import * as XDR from 'js-xdr';
|
|
73
|
+
|
|
74
|
+
EOS
|
|
75
|
+
out.break
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def render_define_block(out)
|
|
79
|
+
out.puts "var types = XDR.config(xdr => {"
|
|
80
|
+
yield
|
|
81
|
+
ensure
|
|
82
|
+
out.puts "});"
|
|
83
|
+
out.puts "export default types;"
|
|
84
|
+
out.break
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def render_typedef(out, typedef)
|
|
89
|
+
out.puts "xdr.typedef(\"#{name typedef}\", #{reference typedef.declaration.type});"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def render_const(out, const)
|
|
93
|
+
out.puts "xdr.const(\"#{const_name const}\", #{const.value});"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def render_struct(out, struct)
|
|
97
|
+
out.puts "xdr.struct(\"#{name struct}\", ["
|
|
98
|
+
out.indent do
|
|
99
|
+
struct.members.each do |m|
|
|
100
|
+
out.puts "[\"#{member_name m}\", #{reference m.type}],"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
out.puts "]);"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def render_enum(out, enum)
|
|
107
|
+
out.puts "xdr.enum(\"#{name enum}\", {"
|
|
108
|
+
|
|
109
|
+
out.indent do
|
|
110
|
+
enum.members.each do |m|
|
|
111
|
+
out.puts "#{member_name m}: #{m.value},"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
out.puts "});"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def render_union(out, union)
|
|
119
|
+
out.puts "xdr.union(\"#{name union}\", {"
|
|
120
|
+
out.indent do
|
|
121
|
+
out.puts "switchOn: #{reference union.discriminant.type},"
|
|
122
|
+
out.puts "switchName: \"#{member_name union.discriminant}\","
|
|
123
|
+
out.puts "switches: ["
|
|
124
|
+
|
|
125
|
+
out.indent do
|
|
126
|
+
union.normal_arms.each do |arm|
|
|
127
|
+
arm_name = arm.void? ? "xdr.void()" : "\"#{member_name(arm)}\""
|
|
128
|
+
|
|
129
|
+
arm.cases.each do |acase|
|
|
130
|
+
switch = if acase.value.is_a?(AST::Identifier)
|
|
131
|
+
if union.discriminant.type.is_a?(AST::Typespecs::Int)
|
|
132
|
+
member = union.resolved_case(acase)
|
|
133
|
+
"#{member.value}"
|
|
134
|
+
else
|
|
135
|
+
'"' + member_name(acase.value) + '"'
|
|
136
|
+
end
|
|
137
|
+
else
|
|
138
|
+
acase.value.text_value
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
out.puts "[#{switch}, #{arm_name}],"
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
out.puts "],"
|
|
147
|
+
out.puts "arms: {"
|
|
148
|
+
|
|
149
|
+
out.indent do
|
|
150
|
+
union.arms.each do |arm|
|
|
151
|
+
next if arm.void?
|
|
152
|
+
out.puts "#{member_name arm}: #{reference arm.type},"
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
out.puts "},"
|
|
157
|
+
|
|
158
|
+
if union.default_arm.present?
|
|
159
|
+
arm = union.default_arm
|
|
160
|
+
arm_name = arm.void? ? "xdr.void()" : member_name(arm)
|
|
161
|
+
out.puts "defaultArm: #{arm_name},"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
end
|
|
165
|
+
out.puts "});"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private
|
|
169
|
+
def name(named)
|
|
170
|
+
parent = name named.parent_defn if named.is_a?(AST::Concerns::NestedDefinition)
|
|
171
|
+
|
|
172
|
+
# NOTE: classify will strip plurality, so we restore it if necessary
|
|
173
|
+
plural = named.name.pluralize == named.name
|
|
174
|
+
base = named.name.underscore.classify
|
|
175
|
+
result = plural ? base.pluralize : base
|
|
176
|
+
|
|
177
|
+
"#{parent}#{result}"
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def const_name(named)
|
|
181
|
+
# NOTE: classify will strip plurality, so we restore it if necessary
|
|
182
|
+
plural = named.name.pluralize == named.name
|
|
183
|
+
base = named.name.underscore.upcase
|
|
184
|
+
plural ? base.pluralize : base
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def member_name(member)
|
|
188
|
+
name(member).camelize(:lower)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def reference(type)
|
|
192
|
+
baseReference = case type
|
|
193
|
+
when AST::Typespecs::Bool
|
|
194
|
+
"xdr.bool()"
|
|
195
|
+
when AST::Typespecs::Double
|
|
196
|
+
"xdr.double()"
|
|
197
|
+
when AST::Typespecs::Float
|
|
198
|
+
"xdr.float()"
|
|
199
|
+
when AST::Typespecs::Hyper
|
|
200
|
+
"xdr.hyper()"
|
|
201
|
+
when AST::Typespecs::Int
|
|
202
|
+
"xdr.int()"
|
|
203
|
+
when AST::Typespecs::Opaque
|
|
204
|
+
if type.fixed?
|
|
205
|
+
"xdr.opaque(#{type.size})"
|
|
206
|
+
else
|
|
207
|
+
"xdr.varOpaque(#{type.size})"
|
|
208
|
+
end
|
|
209
|
+
when AST::Typespecs::Quadruple
|
|
210
|
+
raise "no quadruple support for javascript"
|
|
211
|
+
when AST::Typespecs::String
|
|
212
|
+
"xdr.string(#{type.size})"
|
|
213
|
+
when AST::Typespecs::UnsignedHyper
|
|
214
|
+
"xdr.uhyper()"
|
|
215
|
+
when AST::Typespecs::UnsignedInt
|
|
216
|
+
"xdr.uint()"
|
|
217
|
+
when AST::Typespecs::Simple
|
|
218
|
+
"xdr.lookup(\"#{name type}\")"
|
|
219
|
+
when AST::Definitions::Base
|
|
220
|
+
"xdr.lookup(\"#{name type}\")"
|
|
221
|
+
when AST::Concerns::NestedDefinition
|
|
222
|
+
"xdr.lookup(\"#{name type}\")"
|
|
223
|
+
else
|
|
224
|
+
raise "Unknown reference type: #{type.class.name}, #{type.class.ancestors}"
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
case type.sub_type
|
|
228
|
+
when :simple
|
|
229
|
+
baseReference
|
|
230
|
+
when :optional
|
|
231
|
+
"xdr.option(#{baseReference})"
|
|
232
|
+
when :array
|
|
233
|
+
is_named, size = type.array_size
|
|
234
|
+
size = is_named ? "xdr.lookup(\"#{size}\")" : size
|
|
235
|
+
"xdr.array(#{baseReference}, #{size})"
|
|
236
|
+
when :var_array
|
|
237
|
+
is_named, size = type.array_size
|
|
238
|
+
size = is_named ? "xdr.lookup(\"#{size}\")" : (size || MAX_INT)
|
|
239
|
+
"xdr.varArray(#{baseReference}, #{size})"
|
|
240
|
+
else
|
|
241
|
+
raise "Unknown sub_type: #{type.sub_type}"
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
end
|