3
3
GetSingleVarNode , FunctionCallNode , getTokenType , getTokenValue , isTokenTypeLiteral , getStartLine ,
4
4
getStartColumn , getEndColumn , getEndLine , findOperators , splitTokens , DotObjectAccessNode , BracketObjectAccessNode ,
5
5
findTokenValueIndex , FunctionDefNode , CreateObjectNode , ObjectPropertyInfo , CreateArrayNode , ArrowFuncDefNode ,
6
- ExpressionOperators , IfNode , ForNode , WhileNode , ImportNode , NameAlias , ContinueNode , BreakNode , ReturnNode , CommentNode , getTokenLoc , OperationTypes , LogicalNodeItem , LogicalOperators , LogicalOpNode , ComparisonOperators
6
+ ExpressionOperators , IfNode , ForNode , WhileNode , ImportNode , NameAlias , ContinueNode , BreakNode , ReturnNode , CommentNode ,
7
+ getTokenLoc , OperationTypes , LogicalNodeItem , LogicalOperators , LogicalOpNode , ComparisonOperators
7
8
} from '../common' ;
8
9
import { JspyParserError } from '../common/utils' ;
9
10
@@ -96,7 +97,6 @@ export class Parser {
96
97
const comparisonOpIndexs : number [ ] = [ ] ;
97
98
const assignTokenIndexes : number [ ] = [ ] ;
98
99
99
-
100
100
if ( getTokenType ( firstToken ) === TokenTypes . Comment ) {
101
101
ast . body . push ( new CommentNode ( getTokenValue ( firstToken ) as string , getTokenLoc ( firstToken ) ) ) ;
102
102
} else if ( getTokenValue ( firstToken ) === 'def'
@@ -220,9 +220,7 @@ export class Parser {
220
220
const source = this . createExpressionNode ( assignTokens [ 1 ] ) ;
221
221
ast . body . push ( new AssignNode ( target , source , getTokenLoc ( assignTokens [ 0 ] [ 0 ] ) ) ) ;
222
222
} else if ( findIndexes ( instruction . tokens , OperationTypes . Logical , logicOpIndexes ) ) {
223
- ast . body . push ( this . groupLogicalOperations ( logicOpIndexes , instruction ) ) ;
224
- } else if ( findIndexes ( instruction . tokens , OperationTypes . Comparison , comparisonOpIndexs ) ) {
225
- ast . body . push ( this . groupComparisonOperations ( comparisonOpIndexs , instruction ) ) ;
223
+ ast . body . push ( this . groupLogicalOperations ( logicOpIndexes , instruction . tokens ) ) ;
226
224
} else {
227
225
ast . body . push ( this . createExpressionNode ( instruction . tokens ) )
228
226
}
@@ -240,29 +238,29 @@ export class Parser {
240
238
return a . slice ( begin , end ) ;
241
239
}
242
240
243
- private groupComparisonOperations ( indexes : number [ ] , instruction : InstructionLine ) : AstNode {
241
+ private groupComparisonOperations ( indexes : number [ ] , tokens : Token [ ] ) : AstNode {
244
242
let start = 0 ;
245
243
246
244
let leftNode : AstNode | null = null ;
247
245
for ( let i = 0 ; i < indexes . length ; i ++ ) {
248
- const opToken = getTokenValue ( instruction . tokens [ indexes [ i ] ] ) as ComparisonOperators ;
249
- leftNode = ( leftNode ) ? leftNode : this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , start , indexes [ i ] ) )
246
+ const opToken = getTokenValue ( tokens [ indexes [ i ] ] ) as ComparisonOperators ;
247
+ leftNode = ( leftNode ) ? leftNode : this . createExpressionNode ( this . sliceWithBrackets ( tokens , start , indexes [ i ] ) )
250
248
251
- const endInd = ( i + 1 < indexes . length ) ? indexes [ i + 1 ] : instruction . tokens . length ;
252
- const rightNode = this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , indexes [ i ] + 1 , endInd ) )
249
+ const endInd = ( i + 1 < indexes . length ) ? indexes [ i + 1 ] : tokens . length ;
250
+ const rightNode = this . createExpressionNode ( this . sliceWithBrackets ( tokens , indexes [ i ] + 1 , endInd ) )
253
251
254
- leftNode = new BinOpNode ( leftNode , opToken , rightNode , getTokenLoc ( instruction . tokens [ 0 ] ) ) ;
252
+ leftNode = new BinOpNode ( leftNode , opToken , rightNode , getTokenLoc ( tokens [ 0 ] ) ) ;
255
253
}
256
254
257
255
return leftNode as AstNode ;
258
256
}
259
257
260
- private groupLogicalOperations ( logicOp : number [ ] , instruction : InstructionLine ) {
258
+ private groupLogicalOperations ( logicOp : number [ ] , tokens : Token [ ] ) {
261
259
let start = 0 ;
262
260
const logicItems : LogicalNodeItem [ ] = [ ] ;
263
261
for ( let i = 0 ; i < logicOp . length ; i ++ ) {
264
- const opToken = instruction . tokens [ logicOp [ i ] ] ;
265
- const logicalSlice = this . sliceWithBrackets ( instruction . tokens , start , logicOp [ i ] ) ;
262
+ const opToken = tokens [ logicOp [ i ] ] ;
263
+ const logicalSlice = this . sliceWithBrackets ( tokens , start , logicOp [ i ] ) ;
266
264
logicItems . push ( {
267
265
node : this . createExpressionNode ( logicalSlice ) ,
268
266
op : getTokenValue ( opToken ) as LogicalOperators
@@ -272,10 +270,10 @@ export class Parser {
272
270
}
273
271
274
272
logicItems . push ( {
275
- node : this . createExpressionNode ( this . sliceWithBrackets ( instruction . tokens , start , instruction . tokens . length ) )
273
+ node : this . createExpressionNode ( this . sliceWithBrackets ( tokens , start , tokens . length ) )
276
274
} as LogicalNodeItem ) ;
277
275
278
- const lop = new LogicalOpNode ( logicItems , getTokenLoc ( instruction . tokens [ 0 ] ) ) ;
276
+ const lop = new LogicalOpNode ( logicItems , getTokenLoc ( tokens [ 0 ] ) ) ;
279
277
return lop ;
280
278
}
281
279
@@ -368,6 +366,12 @@ export class Parser {
368
366
return new ArrowFuncDefNode ( funcAst , params , getTokenLoc ( tokens [ 0 ] ) ) ;
369
367
}
370
368
369
+ // comparison operations
370
+ const comparissonIndexes = findOperators ( tokens , OperationTypes . Comparison ) ;
371
+ if ( comparissonIndexes . length ) {
372
+ return this . groupComparisonOperations ( comparissonIndexes , tokens ) ;
373
+ }
374
+
371
375
// create arithmetic expression
372
376
const ops = findOperators ( tokens ) ;
373
377
if ( ops . length ) {
0 commit comments