-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasmBinaryParser.g4
174 lines (141 loc) · 3.84 KB
/
WasmBinaryParser.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
parser grammar WasmBinaryParser;
options { tokenVocab=WasmBinaryLexer; }
numtype
: NUMTYPE_I32 # NumtypeI32
| NUMTYPE_I64 # NumtypeI64
| NUMTYPE_F32 # NumtypeF32
| NUMTYPE_F64 # NumtypeF64
;
vectype
: VECTYPE_V128 # VectypeV128
;
reftype
: REFTYPE_FUNCREF # ReftypeFuncref
| REFTYPE_EXTERNREF # ReftypeExternref
;
valtype
: numtype # ValtypeNumtype
| vectype # ValtypeVectype
| reftype # ValtypeReftype
;
limits
: LIMITS_TYPE0 U32 # LimitsMin
| LIMITS_TYPE1 U32 U32 # LimitsMinMax
;
vec
: U32 element+=elementType* # Vector
{
if ($element.size() != Integer.parseInt($U32.text)) {
throw new RuntimeException("Vector length does not match the specified count.");
}
}
;
elementType
: /* parser rule for 'B' */
;
// Parser rules
// Byte: matches any single byte (0x00 to 0xFF)
byte
: BYTE
;
// Unsigned integer (uN): LEB128-encoded unsigned integer of N bits
uN
: lebUnsigned[N] # UnsignedInteger
;
// Signed integer (sN): LEB128-encoded signed integer of N bits
sN
: lebSigned[N] # SignedInteger
;
// Uninterpreted integer (iN): sN interpreted as iN
iN
: sN # UninterpretedInteger
;
// Floating-point value (fN): N-bit IEEE 754 floating-point number
fN
: BYTE_SEQUENCE[N / 8] # FloatingPointValue
;
// Name: UTF-8 encoded string
name
: vec(byte) # Name
{
// Convert byte vector to UTF-8 string
String utf8String = bytesToUtf8($byteList);
// Handle invalid UTF-8 encoding if necessary
}
;
// Vector: generic vector of elements
vec[elementType]
: u32 elements+=elementType*
{
if ($elements.size() != $u32.value) {
throw new RuntimeException("Vector length does not match the specified count.");
}
}
;
// u32: 32-bit unsigned integer (LEB128-encoded)
u32
: lebUnsigned[32] # U32
;
// Lexer rules
// BYTE: Matches any single byte (0x00 to 0xFF)
BYTE
: . // Matches any single byte
;
// Lexer modes and helper rules for LEB128 encoding
// LEB128-encoded unsigned integer
fragment lebUnsigned[int N]
: { int result = 0; int shift = 0; }
( lebByteUnsigned[$result, $shift]
)+
{
// Set the value property
$value = result;
}
;
// LEB128-encoded signed integer
fragment lebSigned[int N]
: { int result = 0; int shift = 0; }
( lebByteSigned[$result, $shift]
)+
{
// Set the value property
$value = result;
}
;
// Helper lexer rules for LEB128 bytes
fragment lebByteUnsigned[int result, int shift]
: BYTE
{
int byteValue = (int) $BYTE.text.charAt(0) & 0xFF;
result |= (byteValue & 0x7F) << shift;
shift += 7;
if ((byteValue & 0x80) == 0) {
$channel = HIDDEN; // Consume the byte
$value = result;
}
}
;
fragment lebByteSigned[int result, int shift]
: BYTE
{
int byteValue = (int) $BYTE.text.charAt(0) & 0xFF;
result |= (byteValue & 0x7F) << shift;
shift += 7;
boolean isLastByte = (byteValue & 0x80) == 0;
if (isLastByte) {
// Sign extend if negative
if ((byteValue & 0x40) != 0 && shift < N) {
result |= - (1 << shift);
}
$channel = HIDDEN; // Consume the byte
$value = result;
}
}
;
// BYTE_SEQUENCE: Matches a sequence of N bytes
fragment BYTE_SEQUENCE[int count]
: (BYTE){count}
;
// Channels
channels { HIDDEN }
// Tokens