@@ -37,7 +37,7 @@ export class Parser {
37
37
* @param tokens tokens
38
38
* @param options parsing options. By default it will exclude comments and include LOC (Line of code)
39
39
*/
40
- parse ( tokens : Token [ ] , name = 'main.jspy' , type : string = 'module' ) : AstBlock {
40
+ parse ( tokens : Token [ ] , name = 'main.jspy' , type = 'module' ) : AstBlock {
41
41
this . _moduleName = name ;
42
42
const ast = { name, type, funcs : [ ] , body : [ ] } as AstBlock ;
43
43
@@ -214,7 +214,6 @@ export class Parser {
214
214
excepts . push ( except ) ;
215
215
}
216
216
217
-
218
217
i ++ ;
219
218
}
220
219
@@ -229,7 +228,10 @@ export class Parser {
229
228
} else if ( getTokenValue ( firstToken ) === 'break' ) {
230
229
ast . body . push ( new BreakNode ( ) ) ;
231
230
} else if ( getTokenValue ( firstToken ) === 'return' ) {
232
- ast . body . push ( new ReturnNode ( this . createExpressionNode ( instruction . tokens . slice ( 1 ) ) , getTokenLoc ( firstToken ) ) ) ;
231
+ ast . body . push ( new ReturnNode (
232
+ instruction . tokens . length > 1 ? this . createExpressionNode ( instruction . tokens . slice ( 1 ) ) : undefined ,
233
+ getTokenLoc ( firstToken ) )
234
+ ) ;
233
235
} else if ( getTokenValue ( firstToken ) === 'raise' ) {
234
236
235
237
if ( instruction . tokens . length === 1 ) {
@@ -291,7 +293,7 @@ export class Parser {
291
293
const body = { } as AstBlock ; // empty for now
292
294
ast . body . push ( new ImportNode ( module , body , undefined , getTokenLoc ( firstToken ) ) )
293
295
} else if ( getTokenValue ( firstToken ) === 'from' ) {
294
- let importIndex = findTokenValueIndex ( instruction . tokens , v => v === 'import' ) ;
296
+ const importIndex = findTokenValueIndex ( instruction . tokens , v => v === 'import' ) ;
295
297
if ( importIndex < 0 ) {
296
298
throw Error ( `'import' must follow 'from'` ) ;
297
299
}
@@ -336,7 +338,7 @@ export class Parser {
336
338
}
337
339
338
340
private groupComparisonOperations ( indexes : number [ ] , tokens : Token [ ] ) : AstNode {
339
- let start = 0 ;
341
+ const start = 0 ;
340
342
341
343
let leftNode : AstNode | null = null ;
342
344
for ( let i = 0 ; i < indexes . length ; i ++ ) {
@@ -352,7 +354,7 @@ export class Parser {
352
354
return leftNode as AstNode ;
353
355
}
354
356
355
- private groupLogicalOperations ( logicOp : number [ ] , tokens : Token [ ] ) {
357
+ private groupLogicalOperations ( logicOp : number [ ] , tokens : Token [ ] ) : LogicalOpNode {
356
358
let start = 0 ;
357
359
const logicItems : LogicalNodeItem [ ] = [ ] ;
358
360
for ( let i = 0 ; i < logicOp . length ; i ++ ) {
@@ -477,15 +479,15 @@ export class Parser {
477
479
const ops = findOperators ( tokens ) ;
478
480
if ( ops . length ) {
479
481
480
- var prevNode : AstNode | null ;
482
+ let prevNode : AstNode | null = null ;
481
483
for ( let i = 0 ; i < ops . length ; i ++ ) {
482
484
const opIndex = ops [ i ] ;
483
485
const op = getTokenValue ( tokens [ opIndex ] ) as Operators ;
484
486
485
487
let nextOpIndex = i + 1 < ops . length ? ops [ i + 1 ] : null ;
486
488
let nextOp = nextOpIndex !== null ? getTokenValue ( tokens [ nextOpIndex ] ) : null ;
487
489
if ( nextOpIndex !== null && ( nextOp === '*' || nextOp === '/' ) ) {
488
- var rightNode : AstNode | null = null ;
490
+ let rightNode : AstNode | null = null ;
489
491
// iterate through all continuous '*', '/' operations
490
492
do {
491
493
const nextOpIndex2 = i + 2 < ops . length ? ops [ i + 2 ] : null ;
@@ -513,7 +515,7 @@ export class Parser {
513
515
} else {
514
516
const leftSlice = prevNode ? [ ] : this . sliceWithBrackets ( tokens , 0 , opIndex ) ;
515
517
const rightSlice = this . sliceWithBrackets ( tokens , opIndex + 1 , nextOpIndex || tokens . length ) ;
516
- const left = prevNode || this . createExpressionNode ( leftSlice , prevNode ) ;
518
+ const left : AstNode = prevNode || this . createExpressionNode ( leftSlice , prevNode ) ;
517
519
const right = this . createExpressionNode ( rightSlice ) ;
518
520
prevNode = new BinOpNode ( left , op as ExpressionOperators , right , getTokenLoc ( tokens [ 0 ] ) ) ;
519
521
}
0 commit comments