Commits

Frank Wierzbicki  committed cae9bb3

Update to 3.2 grammar.

  • Participants
  • Parent commits faca35e

Comments (0)

Files changed (1)

File 3.2/grammar/Python.g

 int startPos=-1;
 }
 
+//single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
 single_input
     : NEWLINE* EOF
     | simple_stmt NEWLINE* EOF
     | compound_stmt NEWLINE+ EOF
     ;
 
+//file_input: (NEWLINE | stmt)* ENDMARKER
 file_input
     : (NEWLINE
       | stmt
       )*
     ;
 
+//eval_input: testlist NEWLINE* ENDMARKER
 eval_input
     : LEADING_WS? (NEWLINE)* testlist (NEWLINE)* EOF 
     ;
 
+//Not in CPython Grammar.
 dotted_attr
     : NAME
       ( (DOT NAME)+
       )
     ;
 
+//Not in CPython Grammar.
 //attr is here for Java  compatibility.  A Java foo.getIf() can be called from Jython as foo.if
 //     so we need to support any keyword as an attribute.
 attr
     | ELIF
     | EXCEPT
     | EXEC
+    | FALSE
     | FINALLY
     | FROM
     | FOR
     | IN
     | IS
     | LAMBDA
+    | NONE
     | NONLOCAL
     | NOT
     | OR
     | PRINT
     | RAISE
     | RETURN
+    | TRUE
     | TRY
     | WHILE
     | WITH
     | YIELD
     ;
 
+//decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
 decorator
     : AT dotted_attr 
     ( LPAREN
     ) NEWLINE
     ;
 
+//decorators: decorator+
 decorators
     : decorator+
     ;
 
+//funcdef: 'def' NAME parameters ['->' test] ':' suite
 funcdef
-    : decorators? DEF NAME parameters COLON suite
+    : decorators? DEF NAME parameters (ARROW test)? COLON suite
     ;
 
+//parameters: '(' [typedargslist] ')'
 parameters
     : LPAREN 
-      (varargslist
+      (typedargslist
       |
       )
       RPAREN
     ;
 
-defparameter
-    : fpdef (ASSIGN test)?
+//typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [','
+//       ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]]
+//     |  '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
+typedargslist
+    : tpdefparameter (options {greedy=true;}:COMMA tpdefparameter)*
+      (COMMA
+          (STAR (tfpdef)? (options {greedy=true;}:COMMA tpdefparameter)* (COMMA DOUBLESTAR tfpdef)?
+          | DOUBLESTAR tfpdef
+          )?
+      )?
+    | STAR (tfpdef)? (options {greedy=true;}:COMMA tpdefparameter)* (COMMA DOUBLESTAR tfpdef)?
+    | DOUBLESTAR tfpdef
     ;
 
+//tfpdef: NAME [':' test]
+tfpdef
+    : NAME (COLON test)?
+    ;
+
+//Not in CPython Grammar.
+tpdefparameter
+    : tfpdef (ASSIGN test)?
+    ;
+
+//varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [','
+//       ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]]
+//     |  '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
 varargslist
     : defparameter (options {greedy=true;}:COMMA defparameter)*
       (COMMA
-          (STAR NAME (COMMA DOUBLESTAR NAME)?
+          (STAR (NAME)? (options {greedy=true;}:COMMA defparameter)* (COMMA DOUBLESTAR NAME)?
           | DOUBLESTAR NAME
           )?
       )?
-    | STAR NAME (COMMA DOUBLESTAR NAME)?
+    | STAR (NAME)? (options {greedy=true;}:COMMA defparameter)* (COMMA DOUBLESTAR NAME)?
     | DOUBLESTAR NAME
     ;
 
-fpdef
-    : NAME 
-    | LPAREN fplist RPAREN
+//Not in CPython Grammar.
+defparameter
+    : NAME (ASSIGN test)?
     ;
 
-fplist
-    : fpdef
-      (options {greedy=true;}:COMMA fpdef)* (COMMA)?
-    ;
-
+//stmt: simple_stmt | compound_stmt
 stmt
     : simple_stmt
     | compound_stmt
     ;
 
+//simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
 simple_stmt
     : small_stmt (options {greedy=true;}:SEMI small_stmt)* (SEMI)? NEWLINE
     ;
 
-small_stmt : expr_stmt
-           | print_stmt
-           | del_stmt
-           | pass_stmt
-           | flow_stmt
-           | import_stmt
-           | global_stmt
-           | nonlocal_stmt
-           | exec_stmt
-           | assert_stmt
-           ;
+//small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
+//             import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
+small_stmt
+    : expr_stmt
+    | del_stmt
+    | pass_stmt
+    | flow_stmt
+    | import_stmt
+    | global_stmt
+    | nonlocal_stmt
+    | assert_stmt
+    ;
 
+//expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
+//                     ('=' (yield_expr|testlist_star_expr))*)
 expr_stmt 
-    : ((testlist augassign) => lhs=testlist
+    : ((testlist_star_expr augassign) => lhs=testlist_star_expr
         ( (augassign yield_expr 
           )
         | (augassign testlist
           )
         )
-    | (testlist ASSIGN) => lhs=testlist
+    | (testlist_star_expr ASSIGN) => lhs=testlist_star_expr
         (
-        | ((ASSIGN testlist)+
+        | ((ASSIGN testlist_star_expr)+
           )
         | ((ASSIGN yield_expr)+
           )
         )
-    | lhs=testlist
+    | lhs=testlist_star_expr
     )
     ;
 
+//testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
+testlist_star_expr
+    : (test | star_expr) (options {greedy=true;}:COMMA (test | star_expr))* (COMMA)?
+    ;
+
+//augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
+//            '<<=' | '>>=' | '**=' | '//=')
 augassign
     : PLUSEQUAL
     | MINUSEQUAL
     | DOUBLESLASHEQUAL
     ;
 
-print_stmt
-    : PRINT 
-      (printlist
-      | RIGHTSHIFT printlist2
-      |
-      )
-    ;
-
-//not in CPython's Grammar file
-printlist
-    : (test COMMA) =>
-       test (options {k=2;}: COMMA test)*
-         (trailcomma=COMMA)?
-    | test
-    ;
-
-//XXX: would be nice if printlist and printlist2 could be merged.
-//not in CPython's Grammar file
-printlist2
-    : (test COMMA test) =>
-       test (options {k=2;}: COMMA test)*
-         (trailcomma=COMMA)?
-    | test
-    ;
-
+//XXX: do we need del_list?
+//del_stmt: 'del' exprlist
 del_stmt
     : DELETE del_list
     ;
 
+//pass_stmt: 'pass'
 pass_stmt
     : PASS
     ;
 
+//flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
 flow_stmt
     : break_stmt
     | continue_stmt
     | yield_stmt
     ;
 
+//break_stmt: 'break'
 break_stmt
     : BREAK
     ;
 
+//continue_stmt: 'continue'
 continue_stmt
     : CONTINUE
     ;
 
+//return_stmt: 'return' [testlist]
 return_stmt
     : RETURN 
-      (testlist
-      |
-      )
-      ;
+        (testlist
+        |
+        )
+    ;
 
+//yield_stmt: yield_expr
 yield_stmt
     : yield_expr
     ;
 
+//raise_stmt: 'raise' [test ['from' test]]
 raise_stmt
-    : RAISE (test (COMMA test
-        (COMMA test)?)?)?
+    : RAISE (test (FROM test)?)?
     ;
 
+//import_stmt: import_name | import_from
 import_stmt
     : import_name
     | import_from
     ;
 
+//import_name: 'import' dotted_as_names
 import_name
     : IMPORT dotted_as_names
     ;
 
+//import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
+//              'import' ('*' | '(' import_as_names ')' | import_as_names))
 import_from
     : FROM (DOT* dotted_name | DOT+) IMPORT 
         (STAR
         )
     ;
 
+//import_as_names: import_as_name (',' import_as_name)* [',']
 import_as_names
     : import_as_name (COMMA import_as_name)*
     ;
 
+//import_as_name: NAME ['as' NAME]
 import_as_name
     : name=NAME (AS asname=NAME)?
     ;
 
+//dotted_as_name: dotted_name ['as' NAME]
 dotted_as_name
     : dotted_name (AS NAME)?
     ;
 
+//dotted_as_names: dotted_as_name (',' dotted_as_name)*
 dotted_as_names
     : dotted_as_name (COMMA dotted_as_name)*
     ;
 
+//dotted_name: NAME ('.' NAME)*
 dotted_name
     : NAME (DOT attr)*
     ;
 
+//global_stmt: 'global' NAME (',' NAME)*
 global_stmt
     : GLOBAL NAME (COMMA NAME)*
     ;
 
+//nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
 nonlocal_stmt
     : NONLOCAL NAME (COMMA NAME)*
     ;
 
-exec_stmt
-    : EXEC expr (IN test
-        (COMMA test)?)?
-    ;
-
+//assert_stmt: 'assert' test [',' test]
 assert_stmt
     : ASSERT test (COMMA test)?
     ;
 
+//compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
 compound_stmt
     : if_stmt
     | while_stmt
     | for_stmt
     | try_stmt
     | with_stmt
-    | funcdef
+    | (decorators? DEF) => funcdef
     | classdef
     ;
 
+//if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
 if_stmt
     : IF test COLON suite elif_clause*
         (ORELSE COLON suite)?
     : ELIF test COLON suite
     ;
 
+//while_stmt: 'while' test ':' suite ['else' ':' suite]
 while_stmt
     : WHILE test COLON suite (ORELSE COLON suite)?
     ;
 
+//for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
 for_stmt
     : FOR exprlist IN testlist COLON suite
         (ORELSE COLON suite)?
     ;
 
+//try_stmt: ('try' ':' suite
+//           ((except_clause ':' suite)+
+//            ['else' ':' suite]
+//            ['finally' ':' suite] |
+//           'finally' ':' suite))
 try_stmt
     : TRY COLON suite
       ( except_clause+ (ORELSE COLON suite)? (FINALLY COLON suite)?
     : EXCEPT (test (AS test)?)? COLON suite
     ;
 
+//suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
 suite
     : simple_stmt
     | NEWLINE INDENT
       )+ DEDENT
     ;
 
+//test: or_test ['if' or_test 'else' test] | lambdef
 test
     :or_test
       ( (IF or_test ORELSE) => IF o2=or_test ORELSE e=test
     | lambdef
     ;
 
+//test_nocond: or_test | lambdef_nocond
+test_nocond
+    : or_test
+    | lambdef_nocond
+    ;
+
+//lambdef: 'lambda' [varargslist] ':' test
+lambdef
+    : LAMBDA (varargslist)? COLON test
+    ;
+
+//lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
+lambdef_nocond
+    : LAMBDA (varargslist)? COLON test_nocond
+    ;
+
+//or_test: and_test ('or' and_test)*
 or_test
     : left=and_test
         ( (OR and_test
         )
     ;
 
+//and_test: not_test ('and' not_test)*
 and_test
     : not_test
         ( (AND not_test
         )
     ;
 
+//not_test: 'not' not_test | comparison
 not_test
     : NOT nt=not_test
     | comparison
     ;
 
+//comparison: expr (comp_op expr)*
 comparison
     : left=expr
        ( ( comp_op expr
        )
     ;
 
+//comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
 comp_op
     : LESS
     | GREATER
     | IS NOT
     ;
 
+//star_expr: '*' expr
+star_expr
+    : STAR expr
+    ;
+
+//expr: xor_expr ('|' xor_expr)*
 expr
     : left=xor_expr
         ( (VBAR xor_expr
         )
     ;
 
+//xor_expr: and_expr ('^' and_expr)*
 xor_expr
     : left=and_expr
         ( (CIRCUMFLEX and_expr
         )
     ;
 
+//and_expr: shift_expr ('&' shift_expr)*
 and_expr
     : shift_expr
         ( (AMPER shift_expr
         )
     ;
 
+//shift_expr: arith_expr (('<<'|'>>') arith_expr)*
 shift_expr
     : left=arith_expr
         ( ( shift_op arith_expr
         )
     ;
 
+//Not in CPython Grammar.
 shift_op
     : LEFTSHIFT
     | RIGHTSHIFT
     ;
 
+//arith_expr: term (('+'|'-') term)*
 arith_expr
     : left=term
         ( (arith_op term
         )
     ;
 
+//Not in CPython Grammar.
 arith_op
     : PLUS
     | MINUS
     ;
 
+//term: factor (('*'|'/'|'%'|'//') factor)*
 term
     : factor
         ( (term_op factor
         )
     ;
 
+//Not in CPython Grammar.
 term_op
     :STAR
     |SLASH
     |DOUBLESLASH
     ;
 
+//factor: ('+'|'-'|'~') factor | power
 factor
     : PLUS factor
     | MINUS factor
     | power
     ;
 
+//power: atom trailer* ['**' factor]
 power
     : atom (trailer)* (options {greedy=true;}:DOUBLESTAR factor)?
     ;
 
+//XXX: we still need Ellipsis
+//atom: ('(' [yield_expr|testlist_comp] ')' |
+//       '[' [testlist_comp] ']' |
+//       '{' [dictorsetmaker] '}' |
+//       NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
 atom
     : LPAREN 
       ( yield_expr
-      | testlist_gexp
+      | testlist_comp
       |
       )
       RPAREN
     | LBRACK
-      (listmaker
+      (testlist_comp
       |
       )
       RBRACK
     | LCURLY 
-       (dictmaker
+       (dictorsetmaker
        |
        )
        RCURLY
-     | BACKQUOTE testlist BACKQUOTE
-     | NAME
-     | INT
-     | LONGINT
-     | FLOAT
-     | COMPLEX
-     | (STRING)+ 
-     ;
-
-listmaker
-    : test 
-        (list_for
-        | (options {greedy=true;}:COMMA test)*
-        ) (COMMA)?
-          ;
-
-testlist_gexp
-    : test
-        ( ((options {k=2;}: COMMA test)* (COMMA)?
-          )
-        | (gen_for
-          )
-        )
+    | NAME
+    | INT
+    | LONGINT
+    | FLOAT
+    | COMPLEX
+    | (STRING)+ 
+    | NONE
+    | TRUE
+    | FALSE
     ;
 
-lambdef
-    : LAMBDA (varargslist)? COLON test
+//testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
+testlist_comp
+    : (test | star_expr)
+        (comp_for
+        | (options {greedy=true;}:COMMA (test | star_expr))*
+        ) (COMMA)?
     ;
 
+//testlist: test (',' test)* [',']
+
+//trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
 trailer
     : LPAREN 
         (arglist
     | DOT attr
     ;
 
+//subscriptlist: subscript (',' subscript)* [',']
 subscriptlist
     : subscript (options {greedy=true;}:COMMA subscript)* (COMMA)?
     ;
 
+//subscript: test | [test] ':' [test] [sliceop]
 subscript
     : DOT DOT DOT
     | (test COLON)
     | test
     ;
 
+//sliceop: ':' [test]
 sliceop
     : COLON
       (test
       )?
     ;
 
+//exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
 exprlist
-    : (expr COMMA) => expr (options {k=2;}: COMMA expr)* (COMMA)?
-    | expr
+    : ((expr | star_expr) COMMA)
+   => (expr | star_expr) (options {k=2;}: COMMA (expr | star_expr) )* (COMMA)?
+    | (expr | star_expr)
     ;
 
 //not in CPython's Grammar file
     : expr (options {k=2;}: COMMA expr)* (COMMA)?
     ;
 
+//testlist: test (',' test)* [',']
 testlist
     : (test COMMA)
    => test (options {k=2;}: COMMA test)* (COMMA)?
     | test
     ;
 
-dictmaker
-    : test COLON test
-        (options {k=2;}:COMMA test COLON test)*
-        (COMMA)?
+//dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
+//                  (test (comp_for | (',' test)* [','])) )
+dictorsetmaker
+    : test ( COLON test
+             (
+                 comp_for
+             |
+                 (options {k=2;}:COMMA test COLON test)*
+                 (COMMA)?
+             )
+           | COMMA test
+                 (options {k=2;}:COMMA test)*
+                 (COMMA)?
+           | comp_for 
+           |
+           )
     ;
 
+//classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
 classdef
-    : CLASS NAME (LPAREN testlist? RPAREN)? COLON suite
+    : decorators? CLASS NAME (LPAREN arglist? RPAREN)? COLON suite
     ;
 
+//arglist: (argument ',')* (argument [',']
+//                         |'*' test (',' argument)* [',' '**' test] 
+//                         |'**' test)
 arglist
     : argument (COMMA argument)*
           (COMMA
-              ( STAR test (COMMA DOUBLESTAR test)?
+              ( STAR test (COMMA argument)* (COMMA DOUBLESTAR test)?
               | DOUBLESTAR test
               )?
           )?
-    | STAR test (COMMA DOUBLESTAR test)?
+    | STAR test (COMMA argument)* (COMMA DOUBLESTAR test)?
     | DOUBLESTAR test
     ;
 
+//argument: test [comp_for] | test '=' test  # Really [keyword '='] test
 argument
     : t1=test
         ((ASSIGN t2=test)
-        | gen_for
+        | comp_for
         |
         )
     ;
 
-list_iter
-    : list_for
-    | list_if
+//comp_iter: comp_for | comp_if
+comp_iter
+    : comp_for
+    | comp_if
     ;
 
-list_for
-    : FOR exprlist IN testlist (list_iter)?
+//comp_for: 'for' exprlist 'in' or_test [comp_iter]
+comp_for
+    : FOR exprlist IN testlist (comp_iter)?
     ;
 
-list_if
-    : IF test (list_iter)?
+//comp_if: 'if' test_nocond [comp_iter]
+comp_if
+    : IF test_nocond (comp_iter)?
     ;
 
-gen_iter
-    : gen_for
-    | gen_if
-    ;
-
-gen_for
-    : FOR exprlist IN or_test gen_iter?
-    ;
-
-gen_if
-    : IF test gen_iter?
-    ;
-
+//yield_expr: 'yield' [testlist]
 yield_expr
     : YIELD testlist?
     ;
 DELETE    : 'del' ;
 ELIF      : 'elif' ;
 EXCEPT    : 'except' ;
-EXEC      : 'exec' ;
+FALSE     : 'False' ;
 FINALLY   : 'finally' ;
 FROM      : 'from' ;
 FOR       : 'for' ;
 IN        : 'in' ;
 IS        : 'is' ;
 LAMBDA    : 'lambda' ;
+NONE      : 'None' ;
 NONLOCAL  : 'nonlocal' ;
 ORELSE    : 'else' ;
 PASS      : 'pass'  ;
-PRINT     : 'print' ;
 RAISE     : 'raise' ;
 RETURN    : 'return' ;
+TRUE      : 'True' ;
 TRY       : 'try' ;
 WHILE     : 'while' ;
 WITH      : 'with' ;
 
 DOUBLESLASHEQUAL    : '//=' ;
 
+ARROW : '->' ;
+
 DOT : '.' ;
 
 AT : '@' ;
 INT :   // Hex
         '0' ('x' | 'X') ( '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' )+
     |   // Octal
-        '0'  ( '0' .. '7' )*
-    |   '1'..'9' DIGITS*
-    ;
+        '0' ('o' | 'O') ( '0' .. '7' )*
+    |   '0'  ( '0' .. '7' )*
+    |   // Binary
+        '0' ('b' | 'B') ( '0' .. '1' )*
+    |   // Decimal
+        '1'..'9' DIGITS*
+;
 
 COMPLEX
     :   DIGITS+ ('j'|'J')
  *  should make us exit loop not continue.
  */
 STRING
-    :   ('r'|'u'|'ur'|'R'|'U'|'UR'|'uR'|'Ur')?
+    :   ('r'|'u'|'b'|'ur'|'br'|'R'|'U'|'B'|'UR'|'BR'|'uR'|'Ur'|'Br'|'bR')?
         (   '\'\'\'' (options {greedy=false;}:TRIAPOS)* '\'\'\''
         |   '"""' (options {greedy=false;}:TRIQUOTE)* '"""'
         |   '"' (ESC|~('\\'|'\n'|'"'))* '"'