1 # Grammar for Lichen 2 3 # Start symbols for the grammar: 4 # single_input is a single interactive statement; 5 # file_input is a module or sequence of commands read from an input file; 6 # eval_input is the input for the eval() and input() functions. 7 # NB: compound_stmt in single_input is followed by extra NEWLINE! 8 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 9 file_input: (NEWLINE | stmt)* ENDMARKER 10 eval_input: testlist NEWLINE* ENDMARKER 11 12 funcdef: 'def' NAME parameters ':' suite 13 parameters: '(' [varargslist] ')' 14 # NOTE: Remove * and ** support. 15 varargslist: ((fpdef ['=' test] ',')* 16 ('*' NAME [',' '**' NAME] | '**' NAME) | 17 fpdef ['=' test] (',' fpdef ['=' test])* [',']) 18 # NOTE: Replace with flat parameter lists? 19 # Supporting .name for attribute assignment convenience. 20 fpdef: NAME | '.' NAME | '(' fplist ')' 21 fplist: fpdef (',' fpdef)* [','] 22 23 stmt: simple_stmt | compound_stmt 24 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE 25 small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | 26 import_stmt | global_stmt | exec_stmt | assert_stmt) 27 expr_stmt: testlist (augassign testlist | 28 ('=' testlist)*) 29 augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | 30 '<<=' | '>>=' | '**=' | '//=') 31 # For normal assignments, additional restrictions enforced by the interpreter 32 print_stmt: 'print' ( [ test (',' test)* [','] ] | 33 '>>' test [ (',' test)+ [','] ] ) 34 del_stmt: 'del' exprlist 35 pass_stmt: 'pass' 36 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt 37 break_stmt: 'break' 38 continue_stmt: 'continue' 39 return_stmt: 'return' [testlist] 40 raise_stmt: 'raise' [test [',' test [',' test]]] 41 import_stmt: import_name | import_from 42 import_name: 'import' dotted_as_names 43 import_from: ('from' dotted_name 44 'import' ('*' | '(' import_as_names ')' | import_as_names)) 45 import_as_name: NAME ['as' NAME] 46 dotted_as_name: dotted_name ['as' NAME] 47 import_as_names: import_as_name (',' import_as_name)* [','] 48 dotted_as_names: dotted_as_name (',' dotted_as_name)* 49 dotted_name: NAME ('.' NAME)* 50 global_stmt: 'global' NAME (',' NAME)* 51 exec_stmt: 'exec' expr ['in' test [',' test]] 52 assert_stmt: 'assert' test [',' test] 53 54 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef 55 if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 56 while_stmt: 'while' test ':' suite ['else' ':' suite] 57 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] 58 try_stmt: ('try' ':' suite 59 ((except_clause ':' suite)+ 60 ['else' ':' suite] 61 ['finally' ':' suite] | 62 'finally' ':' suite)) 63 # NB compile.c makes sure that the default except clause is last 64 except_clause: 'except' [test [('as' | ',') test]] 65 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT 66 67 # Backward compatibility cruft to support: 68 # [ x for x in lambda: True, lambda: False if x() ] 69 # even while also allowing: 70 # lambda x: 5 if x else 2 71 # (But not a mix of the two) 72 testlist_safe: old_test [(',' old_test)+ [',']] 73 old_test: or_test | old_lambdef 74 old_lambdef: 'lambda' [varargslist] ':' old_test 75 76 test: or_test | lambdef 77 or_test: and_test ('or' and_test)* 78 and_test: not_test ('and' not_test)* 79 not_test: 'not' not_test | comparison 80 comparison: expr (comp_op expr)* 81 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 82 expr: xor_expr ('|' xor_expr)* 83 xor_expr: and_expr ('^' and_expr)* 84 and_expr: shift_expr ('&' shift_expr)* 85 shift_expr: arith_expr (('<<'|'>>') arith_expr)* 86 arith_expr: term (('+'|'-') term)* 87 term: factor (('*'|'/'|'%'|'//') factor)* 88 factor: ('+'|'-'|'~') factor | power 89 power: atom trailer* ['**' factor] 90 atom: ('(' [testlist_comp] ')' | 91 '[' [listmaker] ']' | 92 '{' [dictorsetmaker] '}' | 93 '`' testlist1 '`' | 94 NAME | NUMBER | STRING+) 95 listmaker: test ( (',' test)* [','] ) 96 testlist_comp: test ( (',' test)* [','] ) 97 lambdef: 'lambda' [varargslist] ':' test 98 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 99 subscriptlist: subscript (',' subscript)* [','] 100 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] 101 sliceop: ':' [test] 102 exprlist: expr (',' expr)* [','] 103 testlist: test (',' test)* [','] 104 dictmaker: test ':' test (',' test ':' test)* [','] 105 dictorsetmaker: ( (test ':' test ( (',' test ':' test)* [','])) | 106 (test ( (',' test)* [','])) ) 107 108 classdef: 'class' NAME ['(' [testlist] ')'] ':' suite 109 110 arglist: (argument ',')* (argument [','] 111 |'*' test (',' argument)* [',' '**' test] 112 |'**' test) 113 # The reason that keywords are test nodes instead of NAME is that using NAME 114 # results in an ambiguity. ast.c makes sure it's a NAME. 115 argument: test | test '=' test 116 117 testlist1: test (',' test)* 118 119 # not used in grammar, but may appear in "node" passed from Parser to Compiler 120 encoding_decl: NAME