|
| 1 | +# Grammar for Python |
| 2 | + |
| 3 | +# Note: Changing the grammar specified in this file will most likely |
| 4 | +# require corresponding changes in the parser module |
| 5 | +# (../Modules/parsermodule.c). If you can't make the changes to |
| 6 | +# that module yourself, please co-ordinate the required changes |
| 7 | +# with someone who can; ask around on python-dev for help. Fred |
| 8 | +# Drake <fdrake@acm.org> will probably be listening there. |
| 9 | + |
| 10 | +# NOTE WELL: You should also follow all the steps listed at |
| 11 | +# https://docs.python.org/devguide/grammar.html |
| 12 | + |
| 13 | +# Start symbols for the grammar: |
| 14 | +# file_input is a module or sequence of commands read from an input file; |
| 15 | +# single_input is a single interactive statement; |
| 16 | +# eval_input is the input for the eval() functions. |
| 17 | +# NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 18 | +file_input: (NEWLINE | stmt)* ENDMARKER |
| 19 | +single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 20 | +eval_input: testlist NEWLINE* ENDMARKER |
| 21 | + |
| 22 | +decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 23 | +decorators: decorator+ |
| 24 | +decorated: decorators (classdef | funcdef | async_funcdef) |
| 25 | + |
| 26 | +# NOTE: Francisco Souza/Reinoud Elhorst, using ASYNC/'await' keywords instead of |
| 27 | +# skipping python3.5+ compatibility, in favour of 3.7 solution |
| 28 | +async_funcdef: 'async' funcdef |
| 29 | +funcdef: 'def' NAME parameters ['->' test] ':' suite |
| 30 | + |
| 31 | +parameters: '(' [typedargslist] ')' |
| 32 | +typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [ |
| 33 | + '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 34 | + | '**' tfpdef [',']]] |
| 35 | + | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] |
| 36 | + | '**' tfpdef [',']) |
| 37 | +tfpdef: NAME [':' test] |
| 38 | +varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [ |
| 39 | + '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 40 | + | '**' vfpdef [',']]] |
| 41 | + | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] |
| 42 | + | '**' vfpdef [','] |
| 43 | +) |
| 44 | +vfpdef: NAME |
| 45 | + |
| 46 | +stmt: simple_stmt | compound_stmt |
| 47 | +simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 48 | +small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt | |
| 49 | + import_stmt | global_stmt | nonlocal_stmt | assert_stmt) |
| 50 | +expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 51 | + ('=' (yield_expr|testlist_star_expr))*) |
| 52 | +annassign: ':' test ['=' test] |
| 53 | +testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 54 | +augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 55 | + '<<=' | '>>=' | '**=' | '//=') |
| 56 | +# For normal and annotated assignments, additional restrictions enforced by the interpreter |
| 57 | +del_stmt: 'del' exprlist |
| 58 | +pass_stmt: 'pass' |
| 59 | +flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| 60 | +break_stmt: 'break' |
| 61 | +continue_stmt: 'continue' |
| 62 | +return_stmt: 'return' [testlist] |
| 63 | +yield_stmt: yield_expr |
| 64 | +raise_stmt: 'raise' [test ['from' test]] |
| 65 | +import_stmt: import_name | import_from |
| 66 | +import_name: 'import' dotted_as_names |
| 67 | +# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 68 | +import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) |
| 69 | + 'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 70 | +import_as_name: NAME ['as' NAME] |
| 71 | +dotted_as_name: dotted_name ['as' NAME] |
| 72 | +import_as_names: import_as_name (',' import_as_name)* [','] |
| 73 | +dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 74 | +dotted_name: NAME ('.' NAME)* |
| 75 | +global_stmt: 'global' NAME (',' NAME)* |
| 76 | +nonlocal_stmt: 'nonlocal' NAME (',' NAME)* |
| 77 | +assert_stmt: 'assert' test [',' test] |
| 78 | + |
| 79 | +compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt |
| 80 | +async_stmt: 'async' (funcdef | with_stmt | for_stmt) |
| 81 | +if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 82 | +while_stmt: 'while' test ':' suite ['else' ':' suite] |
| 83 | +for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] |
| 84 | +try_stmt: ('try' ':' suite |
| 85 | + ((except_clause ':' suite)+ |
| 86 | + ['else' ':' suite] |
| 87 | + ['finally' ':' suite] | |
| 88 | + 'finally' ':' suite)) |
| 89 | +with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 90 | +with_item: test ['as' expr] |
| 91 | +# NB compile.c makes sure that the default except clause is last |
| 92 | +except_clause: 'except' [test ['as' NAME]] |
| 93 | +# Edit by Francisco Souza/David Halter: The stmt is now optional. This reflects |
| 94 | +# how Jedi allows classes and functions to be empty, which is beneficial for |
| 95 | +# autocompletion. |
| 96 | +suite: simple_stmt | NEWLINE INDENT stmt* DEDENT |
| 97 | + |
| 98 | +test: or_test ['if' or_test 'else' test] | lambdef |
| 99 | +test_nocond: or_test | lambdef_nocond |
| 100 | +lambdef: 'lambda' [varargslist] ':' test |
| 101 | +lambdef_nocond: 'lambda' [varargslist] ':' test_nocond |
| 102 | +or_test: and_test ('or' and_test)* |
| 103 | +and_test: not_test ('and' not_test)* |
| 104 | +not_test: 'not' not_test | comparison |
| 105 | +comparison: expr (comp_op expr)* |
| 106 | +# <> isn't actually a valid comparison operator in Python. It's here for the |
| 107 | +# sake of a __future__ import described in PEP 401 (which really works :-) |
| 108 | +comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 109 | +star_expr: '*' expr |
| 110 | +expr: xor_expr ('|' xor_expr)* |
| 111 | +xor_expr: and_expr ('^' and_expr)* |
| 112 | +and_expr: shift_expr ('&' shift_expr)* |
| 113 | +shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 114 | +arith_expr: term (('+'|'-') term)* |
| 115 | +term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
| 116 | +factor: ('+'|'-'|'~') factor | power |
| 117 | +power: atom_expr ['**' factor] |
| 118 | +atom_expr: ['await'] atom trailer* |
| 119 | +atom: ('(' [yield_expr|testlist_comp] ')' | |
| 120 | + '[' [testlist_comp] ']' | |
| 121 | + '{' [dictorsetmaker] '}' | |
| 122 | + NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False') |
| 123 | +testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] ) |
| 124 | +trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 125 | +subscriptlist: subscript (',' subscript)* [','] |
| 126 | +subscript: test | [test] ':' [test] [sliceop] |
| 127 | +sliceop: ':' [test] |
| 128 | +exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
| 129 | +testlist: test (',' test)* [','] |
| 130 | +dictorsetmaker: ( ((test ':' test | '**' expr) |
| 131 | + (comp_for | (',' (test ':' test | '**' expr))* [','])) | |
| 132 | + ((test | star_expr) |
| 133 | + (comp_for | (',' (test | star_expr))* [','])) ) |
| 134 | + |
| 135 | +classdef: 'class' NAME ['(' [arglist] ')'] ':' suite |
| 136 | + |
| 137 | +arglist: argument (',' argument)* [','] |
| 138 | + |
| 139 | +# The reason that keywords are test nodes instead of NAME is that using NAME |
| 140 | +# results in an ambiguity. ast.c makes sure it's a NAME. |
| 141 | +# "test '=' test" is really "keyword '=' test", but we have no such token. |
| 142 | +# These need to be in a single rule to avoid grammar that is ambiguous |
| 143 | +# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, |
| 144 | +# we explicitly match '*' here, too, to give it proper precedence. |
| 145 | +# Illegal combinations and orderings are blocked in ast.c: |
| 146 | +# multiple (test comp_for) arguments are blocked; keyword unpackings |
| 147 | +# that precede iterable unpackings are blocked; etc. |
| 148 | +argument: ( test [comp_for] | |
| 149 | + test '=' test | |
| 150 | + '**' test | |
| 151 | + '*' test ) |
| 152 | + |
| 153 | +comp_iter: comp_for | comp_if |
| 154 | +comp_for: ['async'] 'for' exprlist 'in' or_test [comp_iter] |
| 155 | +comp_if: 'if' test_nocond [comp_iter] |
| 156 | + |
| 157 | +# not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 158 | +encoding_decl: NAME |
| 159 | + |
| 160 | +yield_expr: 'yield' [yield_arg] |
| 161 | +yield_arg: 'from' test | testlist |
0 commit comments