Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplification of the grammar #422

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 19 additions & 60 deletions parser/src/main/antlr4/org/polystat/py2eo/parser/PythonParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(',', ')', ':')}?
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down