-
Notifications
You must be signed in to change notification settings - Fork 5
/
BnfLine.js
288 lines (262 loc) · 8.88 KB
/
BnfLine.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Types of tokens from (A)BNF Language Parser.
*/
const types = {
identifier : 1,
literal : 2,
alt : 3,
group : 4,
optional : 5,
repeats : 6,
range : 7,
subBnf : 8,
not : 9
};
exports.types = types;
//This can be turned into the syntax rules that it produces and executed as a compiled language, @LHF.
/**
* Compiles a single line of (A)BNF into a token map that can be distilled into a parser.
*/
exports.BnfLine = class BnfLine{
/**
* Constructor for BnfLine.
* @param {string} bnfLineSyntax - The statement of (a)bnf syntax to compile into a token mapper.
* @param {string[]} literalMap - Copy of literalMap for whole (a)bnf script, used to set literals when performing token extraction.
*/
constructor( bnfLineSyntax, literalMap ){
this.literalMap = literalMap;
this.rawBnf = Buffer.from( bnfLineSyntax.trim() );
this.rawStringBnf = bnfLineSyntax.trim();
this.Tokenize();
}
Tokenize(){
this._bnfTokens = [];
this.seekPoint = 0;
while( this.seekPoint < this.rawBnf.length ){
if( this.rawBnf[this.seekPoint] === 0x3c/*<*/ ){
this.seekPoint++;
this._bnfTokens.push({
type : types.identifier,
value : this.GetIdentifier()
});
if( this.rawBnf[this.seekPoint] !== 0x3e/*>*/ ){
throw "Bad identifier name";
}
this.seekPoint++;
}
else if( ( this.rawBnf[this.seekPoint] >= 0x41 && this.rawBnf[this.seekPoint] <= 0x5a )
|| ( this.rawBnf[this.seekPoint] >= 0x61 && this.rawBnf[this.seekPoint] <= 0x7a ) || this.rawBnf[this.seekPoint] === 0x5f/*_*/ ){
this._bnfTokens.push({
type : types.identifier,
value : this.GetIdentifier()
});
}
else if( this.rawBnf[this.seekPoint] === 0x40/*@*/){
this.seekPoint++;
this._bnfTokens.push({
type : types.literal,
value : this.literalMap[this.GetNumber()]
});
}
else if( this.rawBnf[this.seekPoint] === 0x7c/*|*/ || this.rawBnf[this.seekPoint] === 0x2f/*/*/ ){
this._bnfTokens.push({
type : types.alt
});
this.seekPoint++;
}
else if( this.rawBnf[this.seekPoint] === 0x28/*(*/){
this._bnfTokens.push({
type : types.group,
value : ( new BnfLine( this.GetInnerLine( 0x28/*(*/, 0x29/*)*/ ), this.literalMap ) ).GetTokenMap()
});
}
else if( this.rawBnf[this.seekPoint] === 0x5b/*[*/){
this._bnfTokens.push({
type : types.optional,
value : ( new BnfLine( this.GetInnerLine( 0x5b/*[*/, 0x5d/*]*/ ), this.literalMap ) ).GetTokenMap()
});
}
else if( ( this.rawBnf[this.seekPoint] >= 0x30/*0*/ && this.rawBnf[this.seekPoint] <= 0x39/*9*/ ) || this.rawBnf[this.seekPoint] === 0x2a/***/ ){
//TODO sure this rule up by making sure it can't be repeated one after another, which can yeild unexpected results.
let from = 0;
let to = -1;
if( this.rawBnf[this.seekPoint] === 0x2a/***/ ){
this.seekPoint++;
to = this.GetNumber( -1 );
}
else{
from = this.GetNumber();
if( this.rawBnf[this.seekPoint] === 0x2a/***/ ){
this.seekPoint++;
to = this.GetNumber( -1 );
}
else{
to = from;
}
}
this._bnfTokens.push({
type : types.repeats,
value : [ from, to ]
});
}
else if( this.rawBnf[this.seekPoint] === 0x25/*%*/ ){
this.seekPoint++;
let range = [ 0, 0 ];
if( this.rawBnf[this.seekPoint] === 0x64/*d*/ || this.rawBnf[this.seekPoint] === 0x44/*D*/ ){
this.seekPoint++;
range = this.GetNumberRange( 'd' );
}
else if( this.rawBnf[this.seekPoint] === 0x62/*b*/ || this.rawBnf[this.seekPoint] === 0x42/*B*/ ){
this.seekPoint++;
range = this.GetNumberRange( 'b' );
}
else if( this.rawBnf[this.seekPoint] === 0x6f/*o*/ || this.rawBnf[this.seekPoint] === 0x4f/*O*/ ){
this.seekPoint++;
range = this.GetNumberRange( 'o' );
}
else if( this.rawBnf[this.seekPoint] === 0x78/*x*/ || this.rawBnf[this.seekPoint] === 0x58/*X*/ ){
this.seekPoint++;
range = this.GetNumberRange( 'x' );
}
else{
throw "Bad syntax for terminal value";
}
this._bnfTokens.push({
type : types.range,
value : range
});
}
else if( this.rawBnf[this.seekPoint] === 0x23/*#*/ ){
this.seekPoint++;
//Get Literal Until End//
let bnfScript = this.GetIdentifier();
this._bnfTokens.push({
type : types.subBnf,
value : bnfScript
});
}
else if( this.rawBnf[this.seekPoint] === 0x21/*!*/ ){
this.seekPoint++;
this._bnfTokens.push({
type : types.not
});
}
else{
this.seekPoint++;
}
}
}
GetTokenMap(){
return this._bnfTokens;
}
GetInnerLine( digChar, endingChar ){
let dig = 0;
let peekChar = 1;
while( ( ( this.rawBnf[this.seekPoint + peekChar] !== endingChar && dig === 0 ) || dig > 0 ) && this.seekPoint + peekChar !== this.rawBnf.length ){
if( this.rawBnf[this.seekPoint + peekChar] === digChar ){
dig++;
}
else if( this.rawBnf[this.seekPoint + peekChar] === endingChar ){
dig--;
}
peekChar++;
}
let innerLine = this.rawStringBnf.substring( this.seekPoint + 1, this.seekPoint + peekChar );
this.seekPoint += peekChar;
return innerLine;
}
GetNumberRange( type ){
let maxDigit = 0;
let maxLetter = null;
let parseDigets = 0;
switch( type ){
case "b":
maxDigit = 1;
parseDigets = 2;
break;
case "x":
maxDigit = 9;
maxLetter = 6;
parseDigets = 16;
break;
case "o":
maxDigit = 7;
parseDigets = 8;
break;
case "d":
maxDigit = 9;
parseDigets = 10;
break;
}
let charRange = [];
for( let i = 0; i <= maxDigit; i++ ){
charRange.push( 48 + i );
}
if( maxLetter !== null ){
for( let i = 0; i <= maxLetter; i++ ){
charRange.push( 65 + i );
charRange.push( 97 + i );
}
}
let peekChar = 0;
while( charRange.indexOf( this.rawBnf[this.seekPoint + peekChar] ) !== -1 ){
peekChar++;
}
let firstNumberString = this.rawStringBnf.substring( this.seekPoint, this.seekPoint + peekChar );
let firstRealNumber = parseInt( firstNumberString, parseDigets );
if( isNaN( firstRealNumber ) ){
throw "First number in range was out of range or malformed.";
}
let secondRealNumber = firstRealNumber;
this.seekPoint += peekChar;
if( this.rawBnf[this.seekPoint] >= 0x2d/*-*/ ){
this.seekPoint++;
peekChar = 0;
while( charRange.indexOf( this.rawBnf[this.seekPoint + peekChar] ) !== -1 ){
peekChar++;
}
let firstNumberString = this.rawStringBnf.substring( this.seekPoint, this.seekPoint + peekChar );
secondRealNumber = parseInt( firstNumberString, parseDigets );
if( isNaN( secondRealNumber ) ){
throw "Second number in range was out of range or malformed.";
}
this.seekPoint += peekChar;
}
return [ firstRealNumber, secondRealNumber ];
}
GetNumber( defaultReturn ){
let peekChar = 0;
while( this.rawBnf[this.seekPoint + peekChar] >= 0x30 && this.rawBnf[this.seekPoint + peekChar] <= 0x39 && this.seekPoint + peekChar !== this.rawBnf.length ){
peekChar++;
}
let numberString = this.rawStringBnf.substring( this.seekPoint, this.seekPoint + peekChar );
let realNumber = parseInt( numberString );
if( isNaN( realNumber ) ){
if( defaultReturn === undefined ){
throw "Number was expected and was not found.";
}
realNumber = defaultReturn;
}
this.seekPoint += peekChar;
return realNumber;
}
GetIdentifier(){
let peekChar = 0;
while( this.seekPoint + peekChar !== this.rawBnf.length ){
let char = this.rawBnf[this.seekPoint + peekChar];
if( ( char >= 0x41 && char <= 0x5a ) || ( char >= 0x61 && char <= 0x7a ) || char === 0x5f/*_*/
|| ( peekChar > 0 && ( ( char >= 0x30 && char <= 0x39 ) || char === 0x2d/*-*/ ) ) ){
peekChar++;
}
else{
break;
}
}
let identifierBuffer = this.rawStringBnf.substring( this.seekPoint, this.seekPoint + peekChar );
if( identifierBuffer === "" ){
throw "Missing identifier";
}
this.seekPoint += peekChar;
return identifierBuffer;
}
}