From 23fc44246f68dbb903ada2506a8c8ae77bf3c9d1 Mon Sep 17 00:00:00 2001 From: dours Date: Thu, 3 Nov 2022 09:44:10 +0200 Subject: [PATCH] simplification of the grammar --- .../org/polystat/py2eo/parser/PythonParser.g4 | 79 +++++-------------- .../py2eo/parser/MapExpressions.scala | 49 ++++-------- .../polystat/py2eo/parser/MapStatements.scala | 4 +- .../py2eo/parser/MapStatementsTest.scala | 4 +- 4 files changed, 40 insertions(+), 96 deletions(-) diff --git a/parser/src/main/antlr4/org/polystat/py2eo/parser/PythonParser.g4 b/parser/src/main/antlr4/org/polystat/py2eo/parser/PythonParser.g4 index c4cb5ce7c..226549d7a 100644 --- a/parser/src/main/antlr4/org/polystat/py2eo/parser/PythonParser.g4 +++ b/parser/src/main/antlr4/org/polystat/py2eo/parser/PythonParser.g4 @@ -109,8 +109,9 @@ augassign | '**=' | '//='; -global_stmt: 'global' NAME (',' NAME)*; -nonlocal_stmt: 'nonlocal' NAME (',' NAME)*; +namelist: NAME (',' NAME)*; +global_stmt: 'global' namelist; +nonlocal_stmt: 'nonlocal' namelist; yield_stmt: yield_expr; @@ -154,14 +155,11 @@ while_stmt : 'while' named_expression ':' block else_block?; for_stmt - : 'for' star_targets 'in' star_expressions ':' TYPE_COMMENT? block else_block? - | ASYNC 'for' star_targets 'in' star_expressions ':' TYPE_COMMENT? block else_block?; + : ASYNC? 'for' star_targets 'in' star_expressions ':' TYPE_COMMENT? block else_block?; with_stmt - : 'with' '(' with_item (',' with_item)* ','? ')' ':' block - | 'with' with_item (',' with_item)* ':' TYPE_COMMENT? block - | ASYNC 'with' '(' with_item (',' with_item)* ','? ')' ':' block - | ASYNC 'with' with_item (',' with_item)* ':' TYPE_COMMENT? block; + : ASYNC? 'with' '(' with_item (',' with_item)* ','? ')' ':' block + | ASYNC? 'with' with_item (',' with_item)* ':' TYPE_COMMENT? block; with_item : expression 'as' star_target {isNextToken(',', ')', ':')}? @@ -171,8 +169,7 @@ try_stmt : 'try' ':' block finally_block | 'try' ':' block except_block+ else_block? finally_block?; except_block - : 'except' expression ('as' NAME )? ':' block - | 'except' ':' block; + : 'except' (expression ('as' NAME )?)? ':' block; finally_block : 'finally' ':' block; @@ -228,12 +225,10 @@ complex_number | signed_real_number '-' imaginary_number; signed_number - : NUMBER - | '-' NUMBER; + : '-'? NUMBER; signed_real_number - : real_number - | '-' real_number; + : '-'? real_number; real_number : NUMBER; @@ -303,16 +298,14 @@ return_stmt : 'return' star_expressions?; raise_stmt - : 'raise' expression ('from' expression )? - | 'raise'; + : 'raise' (expression ('from' expression )?)?; function_def : decorators function_def_raw | function_def_raw; function_def_raw - : 'def' NAME '(' params? ')' ('->' expression )? ':' func_type_comment? block - | ASYNC 'def' NAME '(' params? ')' ('->' expression )? ':' func_type_comment? block; + : ASYNC? 'def' NAME '(' params? ')' ('->' expression )? ':' func_type_comment? block; func_type_comment : NEWLINE TYPE_COMMENT {areNextTokens(NEWLINE, INDENT)}? // Must be followed by indented block | TYPE_COMMENT; @@ -383,9 +376,7 @@ block | simple_stmts; star_expressions - : l+=star_expression (',' l+=star_expression )+ ','? - | l+=star_expression ',' - | l+=star_expression; + : l+=star_expression (',' l+=star_expression )* ','?; star_expression : '*' bitwise_or | expression; @@ -406,9 +397,7 @@ named_expression annotated_rhs: yield_expr | star_expressions; expressions - : expression (',' expression )+ ','? - | expression ',' - | expression; + : expression (',' expression )* ','?; expression : disjunction 'if' disjunction 'else' expression | disjunction @@ -466,30 +455,8 @@ inversion : 'not' inversion | comparison; comparison - : bitwise_or compare_op_bitwise_or_pair+ - | bitwise_or; -compare_op_bitwise_or_pair - : eq_bitwise_or - | noteq_bitwise_or - | lte_bitwise_or - | lt_bitwise_or - | gte_bitwise_or - | gt_bitwise_or - | notin_bitwise_or - | in_bitwise_or - | isnot_bitwise_or - | is_bitwise_or; -eq_bitwise_or: '==' bitwise_or; -noteq_bitwise_or - : ('!=' ) bitwise_or; -lte_bitwise_or: '<=' bitwise_or; -lt_bitwise_or: '<' bitwise_or; -gte_bitwise_or: '>=' bitwise_or; -gt_bitwise_or: '>' bitwise_or; -notin_bitwise_or: 'not' 'in' bitwise_or; -in_bitwise_or: 'in' bitwise_or; -isnot_bitwise_or: 'is' 'not' bitwise_or; -is_bitwise_or: 'is' bitwise_or; + : arggs+=bitwise_or (ops+=comp_op arggs+=bitwise_or)*; +comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'; // |'<>'; bitwise_or : bitwise_or '|' bitwise_xor @@ -506,20 +473,13 @@ shift_expr | sum; sum - : sum '+' term - | sum '-' term + : sum ('+' | '-') term | term; term - : term '*' factor - | term '/' factor - | term '//' factor - | term '%' factor - | term '@' factor + : term ('*' | '/' | '//' | '%' | '@') factor | factor; factor - : '+' factor - | '-' factor - | '~' factor + : ('+' | '-' | '~') factor | power; power : await_primary '**' factor @@ -613,8 +573,7 @@ star_targets | l+=star_target (',' l+=star_target )* ','?; star_targets_list_seq: star_target (',' star_target)* ','?; star_targets_tuple_seq - : l+=star_target (',' l+=star_target )+ ','? - | l+=star_target ','; + : l+=star_target (',' l+=star_target )* ','?; star_target : '*' ({is_notNextToken('*')}? star_target) | target_with_star_atom; diff --git a/parser/src/main/scala/org/polystat/py2eo/parser/MapExpressions.scala b/parser/src/main/scala/org/polystat/py2eo/parser/MapExpressions.scala index a99c4f5aa..da88abcfa 100644 --- a/parser/src/main/scala/org/polystat/py2eo/parser/MapExpressions.scala +++ b/parser/src/main/scala/org/polystat/py2eo/parser/MapExpressions.scala @@ -57,39 +57,22 @@ object MapExpressions { } def mapComparison(context: PythonParser.ComparisonContext): T = { - if (context.compare_op_bitwise_or_pair().size() == 0) { - mapBitwiseOr(context.bitwise_or()) - } else { - val l = toList(context.compare_op_bitwise_or_pair()) - val l1 = l.map( - c => { - if (c.eq_bitwise_or() != null) { - (Compops.Eq, mapBitwiseOr(c.eq_bitwise_or().bitwise_or())) - } else if (c.noteq_bitwise_or() != null) { - (Compops.Neq, mapBitwiseOr(c.noteq_bitwise_or().bitwise_or())) - } else if (c.lte_bitwise_or() != null) { - (Compops.Le, mapBitwiseOr(c.lte_bitwise_or().bitwise_or())) - } else if (c.lt_bitwise_or() != null) { - (Compops.Lt, mapBitwiseOr(c.lt_bitwise_or().bitwise_or())) - } else if (c.gte_bitwise_or() != null) { - (Compops.Ge, mapBitwiseOr(c.gte_bitwise_or().bitwise_or())) - } else if (c.gt_bitwise_or() != null) { - (Compops.Gt, mapBitwiseOr(c.gt_bitwise_or().bitwise_or())) - } else if (c.notin_bitwise_or() != null) { - (Compops.NotIn, mapBitwiseOr(c.notin_bitwise_or().bitwise_or())) - } else if (c.in_bitwise_or() != null) { - (Compops.In, mapBitwiseOr(c.in_bitwise_or().bitwise_or())) - } else if (c.isnot_bitwise_or() != null) { - (Compops.IsNot, mapBitwiseOr(c.isnot_bitwise_or().bitwise_or())) - } else if (c.is_bitwise_or() != null) { - (Compops.Is, mapBitwiseOr(c.is_bitwise_or().bitwise_or())) - } else { - throw new ASTMapperException("Unsupported comparison operation?") - } - } - ) - FreakingComparison(l1.map(_._1), mapBitwiseOr(context.bitwise_or()) :: l1.map(_._2), ga(context)) - } + val args = asScala(context.arggs).toList.map(mapBitwiseOr) + val ops = asScala(context.ops).toList.map(c => { + if (c.LESS() != null) Compops.Lt else + if (c.GREATER() != null) Compops.Gt else + if (c.EQEQUAL() != null) Compops.Eq else + if (c.GREATEREQUAL() != null) Compops.Ge else + if (c.LESSEQUAL() != null) Compops.Le else + if (c.NOTEQUAL() != null) Compops.Neq else + if (c.NOT() != null && c.IN() != null) Compops.NotIn else + if (c.IN() != null) {Compops.In} else + if (c.NOT() != null && c.IS() != null) {Compops.IsNot} else + { + if (c.IS() != null) {Compops.Is} else {???} + } + }) + if (ops.isEmpty) args.head else FreakingComparison(ops, args, ga(context)) } def mapBitwiseOr(c: PythonParser.Bitwise_orContext): T = { diff --git a/parser/src/main/scala/org/polystat/py2eo/parser/MapStatements.scala b/parser/src/main/scala/org/polystat/py2eo/parser/MapStatements.scala index 988ad4166..c42efdbee 100644 --- a/parser/src/main/scala/org/polystat/py2eo/parser/MapStatements.scala +++ b/parser/src/main/scala/org/polystat/py2eo/parser/MapStatements.scala @@ -284,10 +284,10 @@ object MapStatements { } def mapGlobalStmt(context: PythonParser.Global_stmtContext): Global = - Global(toList(context.NAME()).map(_.getText), ga(context)) + Global(toList(context.namelist().NAME()).map(_.getText), ga(context)) def mapNonlocalStmt(context: PythonParser.Nonlocal_stmtContext): NonLocal = - NonLocal(toList(context.NAME()).map(_.getText), ga(context)) + NonLocal(toList(context.namelist().NAME()).map(_.getText), ga(context)) def mapRaiseStmt(context: PythonParser.Raise_stmtContext): Raise = { val l = toListNullable(context.expression()).map(mapExpression) diff --git a/parser/src/test/scala/org/polystat/py2eo/parser/MapStatementsTest.scala b/parser/src/test/scala/org/polystat/py2eo/parser/MapStatementsTest.scala index 8d7eb9249..bab8987ad 100644 --- a/parser/src/test/scala/org/polystat/py2eo/parser/MapStatementsTest.scala +++ b/parser/src/test/scala/org/polystat/py2eo/parser/MapStatementsTest.scala @@ -12,7 +12,9 @@ class MapStatementsTest { case Some(Suite(List(Suite(List( Assign(List(Ident("a", _), Ident("b", _)), _) ), _)), _)) => () - case _ => fail() + case x : Any => + println(x) + fail() } }