44
44
import com .dfsek .terra .api .structures .tokenizer .Position ;
45
45
import com .dfsek .terra .api .structures .tokenizer .Token ;
46
46
import com .dfsek .terra .api .structures .tokenizer .Tokenizer ;
47
- import com .dfsek .terra .api .structures .tokenizer .exceptions .TokenizerException ;
48
47
import com .dfsek .terra .api .util .GlueList ;
49
48
50
49
import java .util .Collections ;
@@ -79,18 +78,7 @@ public String getID() {
79
78
* @throws ParseException If parsing fails.
80
79
*/
81
80
public Block parse () throws ParseException {
82
- Tokenizer tokenizer = new Tokenizer (data );
83
-
84
- TokenHolder tokens = new TokenHolder ();
85
- try {
86
- Token t = tokenizer .fetch ();
87
- while (t != null ) {
88
- tokens .add (t );
89
- t = tokenizer .fetch ();
90
- }
91
- } catch (TokenizerException e ) {
92
- throw new ParseException ("Failed to tokenize input" , new Position (0 , 0 ), e );
93
- }
81
+ Tokenizer tokens = new Tokenizer (data );
94
82
95
83
// Parse ID
96
84
ParserUtil .checkType (tokens .consume (), Token .Type .ID ); // First token must be ID
@@ -99,21 +87,12 @@ public Block parse() throws ParseException {
99
87
ParserUtil .checkType (tokens .consume (), Token .Type .STATEMENT_END );
100
88
this .id = idToken .getContent ();
101
89
102
- // Check for dangling brackets
103
- int blockLevel = 0 ;
104
- for (Token t : tokens .getTokens ()) {
105
- if (t .getType ().equals (Token .Type .BLOCK_BEGIN )) blockLevel ++;
106
- else if (t .getType ().equals (Token .Type .BLOCK_END )) blockLevel --;
107
- if (blockLevel < 0 ) throw new ParseException ("Dangling closing brace" , t .getPosition ());
108
- }
109
- if (blockLevel != 0 )
110
- throw new ParseException ("Dangling opening brace" , tokens .getTokens ().get (tokens .getTokens ().size () - 1 ).getPosition ());
111
90
112
91
return parseBlock (tokens , new HashMap <>(), false );
113
92
}
114
93
115
94
116
- private Keyword <?> parseLoopLike (TokenHolder tokens , Map <String , Variable <?>> variableMap , boolean loop ) throws ParseException {
95
+ private Keyword <?> parseLoopLike (Tokenizer tokens , Map <String , Variable <?>> variableMap , boolean loop ) throws ParseException {
117
96
118
97
Token identifier = tokens .consume ();
119
98
ParserUtil .checkType (identifier , Token .Type .IF_STATEMENT , Token .Type .WHILE_LOOP , Token .Type .FOR_LOOP );
@@ -132,7 +111,7 @@ private Keyword<?> parseLoopLike(TokenHolder tokens, Map<String, Variable<?>> va
132
111
}
133
112
}
134
113
135
- private WhileKeyword parseWhileLoop (TokenHolder tokens , Map <String , Variable <?>> variableMap , Position start ) throws ParseException {
114
+ private WhileKeyword parseWhileLoop (Tokenizer tokens , Map <String , Variable <?>> variableMap , Position start ) throws ParseException {
136
115
Returnable <?> first = parseExpression (tokens , true , variableMap );
137
116
ParserUtil .checkReturnType (first , Returnable .ReturnType .BOOLEAN );
138
117
@@ -141,7 +120,7 @@ private WhileKeyword parseWhileLoop(TokenHolder tokens, Map<String, Variable<?>>
141
120
return new WhileKeyword (parseStatementBlock (tokens , variableMap , true ), (Returnable <Boolean >) first , start ); // While loop
142
121
}
143
122
144
- private IfKeyword parseIfStatement (TokenHolder tokens , Map <String , Variable <?>> variableMap , Position start , boolean loop ) throws ParseException {
123
+ private IfKeyword parseIfStatement (Tokenizer tokens , Map <String , Variable <?>> variableMap , Position start , boolean loop ) throws ParseException {
145
124
Returnable <?> condition = parseExpression (tokens , true , variableMap );
146
125
ParserUtil .checkReturnType (condition , Returnable .ReturnType .BOOLEAN );
147
126
@@ -168,7 +147,7 @@ private IfKeyword parseIfStatement(TokenHolder tokens, Map<String, Variable<?>>
168
147
return new IfKeyword (statement , (Returnable <Boolean >) condition , elseIf , elseBlock , start ); // If statement
169
148
}
170
149
171
- private Block parseStatementBlock (TokenHolder tokens , Map <String , Variable <?>> variableMap , boolean loop ) throws ParseException {
150
+ private Block parseStatementBlock (Tokenizer tokens , Map <String , Variable <?>> variableMap , boolean loop ) throws ParseException {
172
151
173
152
if (tokens .get ().getType ().equals (Token .Type .BLOCK_BEGIN )) {
174
153
ParserUtil .checkType (tokens .consume (), Token .Type .BLOCK_BEGIN );
@@ -183,7 +162,7 @@ private Block parseStatementBlock(TokenHolder tokens, Map<String, Variable<?>> v
183
162
}
184
163
}
185
164
186
- private ForKeyword parseForLoop (TokenHolder tokens , Map <String , Variable <?>> old , Position start ) throws ParseException {
165
+ private ForKeyword parseForLoop (Tokenizer tokens , Map <String , Variable <?>> old , Position start ) throws ParseException {
187
166
Map <String , Variable <?>> variableMap = new HashMap <>(old ); // New scope
188
167
Token f = tokens .get ();
189
168
ParserUtil .checkType (f , Token .Type .NUMBER_VARIABLE , Token .Type .STRING_VARIABLE , Token .Type .BOOLEAN_VARIABLE , Token .Type .IDENTIFIER );
@@ -216,7 +195,7 @@ private ForKeyword parseForLoop(TokenHolder tokens, Map<String, Variable<?>> old
216
195
return new ForKeyword (parseStatementBlock (tokens , variableMap , true ), initializer , (Returnable <Boolean >) conditional , incrementer , start );
217
196
}
218
197
219
- private Returnable <?> parseExpression (TokenHolder tokens , boolean full , Map <String , Variable <?>> variableMap ) throws ParseException {
198
+ private Returnable <?> parseExpression (Tokenizer tokens , boolean full , Map <String , Variable <?>> variableMap ) throws ParseException {
220
199
boolean booleanInverted = false ; // Check for boolean not operator
221
200
boolean negate = false ;
222
201
if (tokens .get ().getType ().equals (Token .Type .BOOLEAN_NOT )) {
@@ -259,7 +238,7 @@ else if(variableMap.containsKey(id.getContent())) {
259
238
return expression ;
260
239
}
261
240
262
- private ConstantExpression <?> parseConstantExpression (TokenHolder tokens ) throws ParseException {
241
+ private ConstantExpression <?> parseConstantExpression (Tokenizer tokens ) throws ParseException {
263
242
Token constantToken = tokens .consume ();
264
243
Position position = constantToken .getPosition ();
265
244
switch (constantToken .getType ()) {
@@ -275,15 +254,15 @@ private ConstantExpression<?> parseConstantExpression(TokenHolder tokens) throws
275
254
}
276
255
}
277
256
278
- private Returnable <?> parseGroup (TokenHolder tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
257
+ private Returnable <?> parseGroup (Tokenizer tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
279
258
ParserUtil .checkType (tokens .consume (), Token .Type .GROUP_BEGIN );
280
259
Returnable <?> expression = parseExpression (tokens , true , variableMap ); // Parse inside of group as a separate expression
281
260
ParserUtil .checkType (tokens .consume (), Token .Type .GROUP_END );
282
261
return expression ;
283
262
}
284
263
285
264
286
- private BinaryOperation <?, ?> parseBinaryOperation (Returnable <?> left , TokenHolder tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
265
+ private BinaryOperation <?, ?> parseBinaryOperation (Returnable <?> left , Tokenizer tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
287
266
Token binaryOperator = tokens .consume ();
288
267
ParserUtil .checkBinaryOperator (binaryOperator );
289
268
@@ -337,7 +316,7 @@ private Returnable<?> parseGroup(TokenHolder tokens, Map<String, Variable<?>> va
337
316
}
338
317
}
339
318
340
- private Variable <?> parseVariableDeclaration (TokenHolder tokens , Returnable .ReturnType type ) throws ParseException {
319
+ private Variable <?> parseVariableDeclaration (Tokenizer tokens , Returnable .ReturnType type ) throws ParseException {
341
320
ParserUtil .checkVarType (tokens .get (), type ); // Check for type mismatch
342
321
switch (type ) {
343
322
case NUMBER :
@@ -350,7 +329,7 @@ private Variable<?> parseVariableDeclaration(TokenHolder tokens, Returnable.Retu
350
329
throw new UnsupportedOperationException ("Unsupported variable type: " + type );
351
330
}
352
331
353
- private Block parseBlock (TokenHolder tokens , Map <String , Variable <?>> superVars , boolean loop ) throws ParseException {
332
+ private Block parseBlock (Tokenizer tokens , Map <String , Variable <?>> superVars , boolean loop ) throws ParseException {
354
333
List <Item <?>> parsedItems = new GlueList <>();
355
334
356
335
Map <String , Variable <?>> parsedVariables = new HashMap <>(superVars ); // New hashmap as to not mutate parent scope's declarations.
@@ -366,7 +345,7 @@ private Block parseBlock(TokenHolder tokens, Map<String, Variable<?>> superVars,
366
345
return new Block (parsedItems , first .getPosition ());
367
346
}
368
347
369
- private Item <?> parseItem (TokenHolder tokens , Map <String , Variable <?>> variableMap , boolean loop ) throws ParseException {
348
+ private Item <?> parseItem (Tokenizer tokens , Map <String , Variable <?>> variableMap , boolean loop ) throws ParseException {
370
349
Token token = tokens .get ();
371
350
if (loop ) ParserUtil .checkType (token , Token .Type .IDENTIFIER , Token .Type .IF_STATEMENT , Token .Type .WHILE_LOOP , Token .Type .FOR_LOOP ,
372
351
Token .Type .NUMBER_VARIABLE , Token .Type .STRING_VARIABLE , Token .Type .BOOLEAN_VARIABLE , Token .Type .RETURN , Token .Type .BREAK , Token .Type .CONTINUE , Token .Type .FAIL );
@@ -401,7 +380,7 @@ private Item<?> parseItem(TokenHolder tokens, Map<String, Variable<?>> variableM
401
380
else throw new UnsupportedOperationException ("Unexpected token " + token .getType () + ": " + token .getPosition ());
402
381
}
403
382
404
- private Assignment <?> parseAssignment (Variable <?> variable , TokenHolder tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
383
+ private Assignment <?> parseAssignment (Variable <?> variable , Tokenizer tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
405
384
Token name = tokens .get ();
406
385
407
386
ParserUtil .checkType (tokens .consume (), Token .Type .IDENTIFIER );
@@ -415,7 +394,7 @@ private Assignment<?> parseAssignment(Variable<?> variable, TokenHolder tokens,
415
394
return new Assignment <>((Variable <Object >) variable , (Returnable <Object >) expression , name .getPosition ());
416
395
}
417
396
418
- private Function <?> parseFunction (TokenHolder tokens , boolean fullStatement , Map <String , Variable <?>> variableMap ) throws ParseException {
397
+ private Function <?> parseFunction (Tokenizer tokens , boolean fullStatement , Map <String , Variable <?>> variableMap ) throws ParseException {
419
398
Token identifier = tokens .consume ();
420
399
ParserUtil .checkType (identifier , Token .Type .IDENTIFIER ); // First token must be identifier
421
400
@@ -449,7 +428,7 @@ private Function<?> parseFunction(TokenHolder tokens, boolean fullStatement, Map
449
428
}
450
429
451
430
452
- private List <Returnable <?>> getArgs (TokenHolder tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
431
+ private List <Returnable <?>> getArgs (Tokenizer tokens , Map <String , Variable <?>> variableMap ) throws ParseException {
453
432
List <Returnable <?>> args = new GlueList <>();
454
433
455
434
while (!tokens .get ().getType ().equals (Token .Type .GROUP_END )) {
0 commit comments