Lichen

Annotated pyparser/data/Grammar2.7

966:7003a5061590
22 months ago Paul Boddie Reverted spacing fix, hopefully remedied in MoinLight/moinconvert.
paul@437 1
# Grammar for Python
paul@437 2
paul@437 3
# Note:  Changing the grammar specified in this file will most likely
paul@437 4
#        require corresponding changes in the parser module
paul@437 5
#        (../Modules/parsermodule.c).  If you can't make the changes to
paul@437 6
#        that module yourself, please co-ordinate the required changes
paul@437 7
#        with someone who can; ask around on python-dev for help.  Fred
paul@437 8
#        Drake <fdrake@acm.org> will probably be listening there.
paul@437 9
paul@437 10
# NOTE WELL: You should also follow all the steps listed in PEP 306,
paul@437 11
# "How to Change Python's Grammar"
paul@437 12
paul@437 13
# Start symbols for the grammar:
paul@437 14
#	single_input is a single interactive statement;
paul@437 15
#	file_input is a module or sequence of commands read from an input file;
paul@437 16
#	eval_input is the input for the eval() and input() functions.
paul@437 17
# NB: compound_stmt in single_input is followed by extra NEWLINE!
paul@437 18
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
paul@437 19
file_input: (NEWLINE | stmt)* ENDMARKER
paul@437 20
eval_input: testlist NEWLINE* ENDMARKER
paul@437 21
paul@437 22
decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
paul@437 23
decorators: decorator+
paul@437 24
decorated: decorators (classdef | funcdef)
paul@437 25
funcdef: 'def' NAME parameters ':' suite
paul@437 26
parameters: '(' [varargslist] ')'
paul@437 27
varargslist: ((fpdef ['=' test] ',')*
paul@437 28
              ('*' NAME [',' '**' NAME] | '**' NAME) |
paul@437 29
              fpdef ['=' test] (',' fpdef ['=' test])* [','])
paul@437 30
fpdef: NAME | '(' fplist ')'
paul@437 31
fplist: fpdef (',' fpdef)* [',']
paul@437 32
paul@437 33
stmt: simple_stmt | compound_stmt
paul@437 34
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
paul@437 35
small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
paul@437 36
             import_stmt | global_stmt | exec_stmt | assert_stmt)
paul@437 37
expr_stmt: testlist (augassign (yield_expr|testlist) |
paul@437 38
                     ('=' (yield_expr|testlist))*)
paul@437 39
augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
paul@437 40
            '<<=' | '>>=' | '**=' | '//=')
paul@437 41
# For normal assignments, additional restrictions enforced by the interpreter
paul@437 42
print_stmt: 'print' ( [ test (',' test)* [','] ] |
paul@437 43
                      '>>' test [ (',' test)+ [','] ] )
paul@437 44
del_stmt: 'del' exprlist
paul@437 45
pass_stmt: 'pass'
paul@437 46
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
paul@437 47
break_stmt: 'break'
paul@437 48
continue_stmt: 'continue'
paul@437 49
return_stmt: 'return' [testlist]
paul@437 50
yield_stmt: yield_expr
paul@437 51
raise_stmt: 'raise' [test [',' test [',' test]]]
paul@437 52
import_stmt: import_name | import_from
paul@437 53
import_name: 'import' dotted_as_names
paul@437 54
import_from: ('from' ('.'* dotted_name | '.'+)
paul@437 55
              'import' ('*' | '(' import_as_names ')' | import_as_names))
paul@437 56
import_as_name: NAME ['as' NAME]
paul@437 57
dotted_as_name: dotted_name ['as' NAME]
paul@437 58
import_as_names: import_as_name (',' import_as_name)* [',']
paul@437 59
dotted_as_names: dotted_as_name (',' dotted_as_name)*
paul@437 60
dotted_name: NAME ('.' NAME)*
paul@437 61
global_stmt: 'global' NAME (',' NAME)*
paul@437 62
exec_stmt: 'exec' expr ['in' test [',' test]]
paul@437 63
assert_stmt: 'assert' test [',' test]
paul@437 64
paul@437 65
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
paul@437 66
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
paul@437 67
while_stmt: 'while' test ':' suite ['else' ':' suite]
paul@437 68
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
paul@437 69
try_stmt: ('try' ':' suite
paul@437 70
           ((except_clause ':' suite)+
paul@437 71
	    ['else' ':' suite]
paul@437 72
	    ['finally' ':' suite] |
paul@437 73
	   'finally' ':' suite))
paul@437 74
with_stmt: 'with' with_item (',' with_item)*  ':' suite
paul@437 75
with_item: test ['as' expr]
paul@437 76
# NB compile.c makes sure that the default except clause is last
paul@437 77
except_clause: 'except' [test [('as' | ',') test]]
paul@437 78
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
paul@437 79
paul@437 80
# Backward compatibility cruft to support:
paul@437 81
# [ x for x in lambda: True, lambda: False if x() ]
paul@437 82
# even while also allowing:
paul@437 83
# lambda x: 5 if x else 2
paul@437 84
# (But not a mix of the two)
paul@437 85
testlist_safe: old_test [(',' old_test)+ [',']]
paul@437 86
old_test: or_test | old_lambdef
paul@437 87
old_lambdef: 'lambda' [varargslist] ':' old_test
paul@437 88
paul@437 89
test: or_test ['if' or_test 'else' test] | lambdef
paul@437 90
or_test: and_test ('or' and_test)*
paul@437 91
and_test: not_test ('and' not_test)*
paul@437 92
not_test: 'not' not_test | comparison
paul@437 93
comparison: expr (comp_op expr)*
paul@437 94
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
paul@437 95
expr: xor_expr ('|' xor_expr)*
paul@437 96
xor_expr: and_expr ('^' and_expr)*
paul@437 97
and_expr: shift_expr ('&' shift_expr)*
paul@437 98
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
paul@437 99
arith_expr: term (('+'|'-') term)*
paul@437 100
term: factor (('*'|'/'|'%'|'//') factor)*
paul@437 101
factor: ('+'|'-'|'~') factor | power
paul@437 102
power: atom trailer* ['**' factor]
paul@437 103
atom: ('(' [yield_expr|testlist_comp] ')' |
paul@437 104
       '[' [listmaker] ']' |
paul@437 105
       '{' [dictorsetmaker] '}' |
paul@437 106
       '`' testlist1 '`' |
paul@437 107
       NAME | NUMBER | STRING+)
paul@437 108
listmaker: test ( list_for | (',' test)* [','] )
paul@437 109
testlist_comp: test ( comp_for | (',' test)* [','] )
paul@437 110
lambdef: 'lambda' [varargslist] ':' test
paul@437 111
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
paul@437 112
subscriptlist: subscript (',' subscript)* [',']
paul@437 113
subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
paul@437 114
sliceop: ':' [test]
paul@437 115
exprlist: expr (',' expr)* [',']
paul@437 116
testlist: test (',' test)* [',']
paul@437 117
dictmaker: test ':' test (',' test ':' test)* [',']
paul@437 118
dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
paul@437 119
                  (test (comp_for | (',' test)* [','])) )
paul@437 120
paul@437 121
classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
paul@437 122
paul@437 123
arglist: (argument ',')* (argument [',']
paul@437 124
                         |'*' test (',' argument)* [',' '**' test] 
paul@437 125
                         |'**' test)
paul@437 126
# The reason that keywords are test nodes instead of NAME is that using NAME
paul@437 127
# results in an ambiguity. ast.c makes sure it's a NAME.
paul@437 128
argument: test [comp_for] | test '=' test
paul@437 129
paul@437 130
list_iter: list_for | list_if
paul@437 131
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
paul@437 132
list_if: 'if' old_test [list_iter]
paul@437 133
paul@437 134
comp_iter: comp_for | comp_if
paul@437 135
comp_for: 'for' exprlist 'in' or_test [comp_iter]
paul@437 136
comp_if: 'if' old_test [comp_iter]
paul@437 137
paul@437 138
testlist1: test (',' test)*
paul@437 139
paul@437 140
# not used in grammar, but may appear in "node" passed from Parser to Compiler
paul@437 141
encoding_decl: NAME
paul@437 142
paul@437 143
yield_expr: 'yield' [testlist]