| 1 |
# tokens |
| 2 |
|
| 3 |
#IGNORE: "[ \f\t]*|#[^\n]*"; |
| 4 |
#NAME: "[a-zA-Z_][a-zA-Z0-9_]*"; |
| 5 |
#NUMBER: "(0[xX][0-9a-fA-F]*[lL]?)|(0[0-7]*[lL]?)|([1-9][0-9]*[lL]?)" |
| 6 |
#STRING: "\"[^\\\"\n]*\""; |
| 7 |
#NEWLINE: "\$\$NEWLINE" |
| 8 |
#INDENT: "\$\$INDENT" |
| 9 |
#DEDENT: "\$\$DEDENT" |
| 10 |
|
| 11 |
# Start symbols for the grammar: |
| 12 |
# single_input is a single interactive statement; |
| 13 |
# file_input is a module or sequence of commands read from an input file; |
| 14 |
# eval_input is the input for the eval() and input() functions. |
| 15 |
# NB: compound_stmt in single_input is followed by extra NEWLINE! |
| 16 |
file_input: newline_or_stmt* [EOF]; |
| 17 |
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE; |
| 18 |
newline_or_stmt: NEWLINE | <stmt>; |
| 19 |
eval_input: testlist NEWLINE* EOF; |
| 20 |
|
| 21 |
decorator: "@" dotted_name ( "(" [arglist] ")" )? NEWLINE; |
| 22 |
decorators: decorator+; |
| 23 |
funcdef: decorators? ["def"] NAME parameters [":"] suite; |
| 24 |
parameters: ["("] >varargslist< [")"] | ["("] [")"]; |
| 25 |
varargslist: (parameter [","])* >star_or_starstarargs< | |
| 26 |
parameter ([","] parameter)* [","]?; |
| 27 |
parameter: fpdef ("=" test)?; |
| 28 |
star_or_starstarargs: starargs [","] starstarargs | starargs | starstarargs; |
| 29 |
starargs: ["*"] NAME; |
| 30 |
starstarargs: ["**"] NAME; |
| 31 |
fpdef: <NAME> | "(" fplist ")"; |
| 32 |
fplist: fpdef ("," fpdef)* ","?; |
| 33 |
|
| 34 |
stmt: <simple_stmt> | <compound_stmt>; |
| 35 |
simple_stmt: small_stmt ([";"] small_stmt)+ [";"]? [NEWLINE] | |
| 36 |
<small_stmt> [";"]? [NEWLINE]; |
| 37 |
small_stmt: <expr_stmt> | <print_stmt> | <del_stmt> | |
| 38 |
<pass_stmt> | <flow_stmt> | <import_stmt> | <global_stmt> | |
| 39 |
<exec_stmt> | <assert_stmt>; |
| 40 |
expr_stmt: <augassign_stmt> | testlist (["="] testlist)*; |
| 41 |
augassign_stmt: testlist >augassign< testlist; |
| 42 |
augassign: "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | |
| 43 |
">>=" | "**=" | "//="; |
| 44 |
# For normal assignments, additional restrictions enforced by the interpreter |
| 45 |
|
| 46 |
print_stmt: ["print"] print_target (([","] test)+ [","]?)? | |
| 47 |
["print"] (test ([","] test)* [","]?)?; |
| 48 |
print_target: [">>"] test; |
| 49 |
del_stmt: ["del"] exprlist; |
| 50 |
pass_stmt: ["pass"]; |
| 51 |
flow_stmt: <break_stmt> | <continue_stmt> | <return_stmt> | <raise_stmt> | |
| 52 |
<yield_stmt>; |
| 53 |
break_stmt: ["break"]; |
| 54 |
continue_stmt: ["continue"]; |
| 55 |
return_stmt: ["return"] testlist?; |
| 56 |
yield_stmt: ["yield"] testlist; |
| 57 |
raise_stmt: ["raise"] (test ("," test ("," test)?)?)?; |
| 58 |
import_stmt: import_name | import_from; |
| 59 |
import_name: "import" dotted_as_names; |
| 60 |
import_from: "from" dotted_name "import" import_what; |
| 61 |
import_what: "*" | "(" import_as_names ","? ")" | import_as_names; |
| 62 |
|
| 63 |
# the second NAME is the "as" (which is not a keyword in Python) |
| 64 |
import_as_name: NAME ("as" NAME)?; |
| 65 |
dotted_as_name: dotted_name ("as" NAME)?; |
| 66 |
import_as_names: import_as_name ("," import_as_name)*; |
| 67 |
dotted_as_names: dotted_as_name ("," dotted_as_name)*; |
| 68 |
dotted_name: NAME ("." NAME)*; |
| 69 |
global_stmt: "global" NAME ("," NAME)*; |
| 70 |
exec_stmt: "exec" expr ("in" test ("," test)?)?; |
| 71 |
assert_stmt: "assert" test ("," test)?; |
| 72 |
|
| 73 |
compound_stmt: <if_stmt> | <while_stmt> | <for_stmt> | <try_stmt> | |
| 74 |
<funcdef> | <classdef>; |
| 75 |
if_stmt: "if" test ":" suite ("elif" test ":" suite)* ("else" ":" suite)?; |
| 76 |
while_stmt: ["while"] test [":"] suite (["else" ":"] suite)?; |
| 77 |
for_stmt: ["for"] exprlist ["in"] testlist [":"] suite (["else" ":"] suite)?; |
| 78 |
try_stmt: "try" ":" suite (except_clause ":" suite)+ |
| 79 |
("else" ":" suite)? | "try" ":" suite "finally" ":" suite; |
| 80 |
|
| 81 |
except_clause: "except" (test ("," test)?)?; |
| 82 |
suite: >simple_stmt< | [NEWLINE] [INDENT] stmt+ [DEDENT]; |
| 83 |
|
| 84 |
test: and_test "or" test | <and_test> | <lambdef>; |
| 85 |
and_test: not_test "and" and_test | <not_test>; |
| 86 |
not_test: "not" not_test | <comparison>; |
| 87 |
comparison: expr >comp_op< comparison | <expr>; |
| 88 |
comp_op: "<" | ">" | "==" | ">=" | "<=" | "<>" | "!=" | "in" | "not" "in" | |
| 89 |
"is" "not" | "is"; |
| 90 |
expr: xor_expr "|" expr | <xor_expr>; |
| 91 |
xor_expr: and_expr "^" xor_expr | <and_expr>; |
| 92 |
and_expr: shift_expr "&" and_expr | <shift_expr>; |
| 93 |
shift_expr: arith_expr ("<<" arith_expr)+ | # was (("<<"|">>") arith_expr)* |
| 94 |
arith_expr (">>" arith_expr)+ | |
| 95 |
<arith_expr>; |
| 96 |
arith_expr: term ("+" term)+ | # was (("+"|"-") term)* |
| 97 |
term ("-" term)+ | |
| 98 |
<term>; |
| 99 |
term: factor ("*" term)+ | # was (("*"|"/"|"%"|"//") factor)* |
| 100 |
factor ("/" term)+ | |
| 101 |
factor ("%" term)+ | |
| 102 |
factor ("//" term)+ | |
| 103 |
<factor>; |
| 104 |
factor: "+" factor | "-" factor | "~" factor | <power>; |
| 105 |
power: atom trailer+ ("**" factor)? | atom "**" factor | <atom>; |
| 106 |
|
| 107 |
atom: "(" testlist_gexp? ")" | <listliteral> | <dictliteral> | |
| 108 |
"`" testlist1 "`" | <NAME> | <NUMBER> | STRING+; |
| 109 |
listliteral: ["[" "]"] | "[" <listcomprehension> "]" | ["["] >listmaker< ["]"]; |
| 110 |
listcomprehension: test list_for; |
| 111 |
listmaker: test ([","] test)* [","]?; |
| 112 |
testlist_gexp: test gen_for | |
| 113 |
test ("," test)* ","?; |
| 114 |
|
| 115 |
dictliteral: ["{" "}"] | ["{"] dictitem ([","] dictitem)* [","]? ["}"]; |
| 116 |
dictitem: test [":"] test; |
| 117 |
|
| 118 |
lambdef: ["lambda"] varargslist? [":"] test; |
| 119 |
trailer: <call> | <subscription> | <attraccess>; |
| 120 |
call: ["(" ")"] | ["("] arglist [")"]; |
| 121 |
subscription: ["["] >subscriptlist< ["]"]; |
| 122 |
attraccess: ["."] NAME; |
| 123 |
subscriptlist: subscript ("," subscript)* ","?; |
| 124 |
subscript: "." "." "." | test? ":" test? sliceop? | test; |
| 125 |
sliceop: ":" test?; |
| 126 |
exprlist: expr ("," expr)* ","?; |
| 127 |
testlist: test ("," test)* ","?; |
| 128 |
testlist_safe: test (("," test)+ ","?)?; |
| 129 |
|
| 130 |
classdef: ["class"] NAME (["("] testlist [")"])? [":"] suite; |
| 131 |
|
| 132 |
arglist: (argument [","])* arglist_rest; |
| 133 |
arglist_rest: "*" test ("," "**" test)? | "**" test | argument ","?; |
| 134 |
argument: (test "=")? test gen_for?; |
| 135 |
|
| 136 |
list_iter: <list_for> | <list_if>; |
| 137 |
list_for: ["for"] exprlist ["in"] testlist_safe list_iter?; |
| 138 |
list_if: ["if"] test list_iter?; |
| 139 |
|
| 140 |
gen_iter: <gen_for> | <gen_if>; |
| 141 |
gen_for: ["for"] exprlist ["in"] test gen_iter?; |
| 142 |
gen_if: ["if"] test gen_iter?; |
| 143 |
|
| 144 |
testlist1: test ("," test)*; |
| 145 |
|
| 146 |
# not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 147 |
encoding_decl: NAME; |