Grammar 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Grammar for Cython, based on the Grammar for Python 3
  2. # Note: This grammar is not yet used by the Cython parser and is subject to change.
  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() 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. decorator: '@' dotted_PY_NAME [ '(' [arglist] ')' ] NEWLINE
  12. decorators: decorator+
  13. decorated: decorators (classdef | funcdef | async_funcdef | cdef_stmt)
  14. async_funcdef: 'async' funcdef
  15. funcdef: 'def' PY_NAME parameters ['->' test] ':' suite
  16. parameters: '(' [typedargslist] ')'
  17. typedargslist: (tfpdef ['=' (test | '*')] (',' tfpdef ['=' (test | '*')])* [','
  18. ['*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef]]
  19. | '*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) [',' ellipsis]
  20. tfpdef: maybe_typed_name [('not' | 'or') 'None'] [':' test]
  21. varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [','
  22. ['*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef]]
  23. | '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
  24. vfpdef: maybe_typed_name ['not' 'None']
  25. stmt: simple_stmt | compound_stmt | cdef_stmt | ctypedef_stmt | DEF_stmt | IF_stmt
  26. simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
  27. small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
  28. import_stmt | global_stmt | nonlocal_stmt | assert_stmt | print_stmt)
  29. expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
  30. ('=' (yield_expr|testlist_star_expr))*)
  31. testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
  32. augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
  33. '<<=' | '>>=' | '**=' | '//=')
  34. print_stmt: 'print' ( [ test (',' test)* [','] ] |
  35. '>>' test [ (',' test)+ [','] ] )
  36. # For normal assignments, additional restrictions enforced by the interpreter
  37. del_stmt: 'del' exprlist
  38. pass_stmt: 'pass'
  39. flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
  40. break_stmt: 'break'
  41. continue_stmt: 'continue'
  42. return_stmt: 'return' [testlist]
  43. yield_stmt: yield_expr
  44. raise_stmt: 'raise' [test ['from' test]]
  45. # raise_stmt: 'raise' [test [',' test [',' test]]]
  46. import_stmt: import_PY_NAME | import_from
  47. import_PY_NAME: ('import' | 'cimport') dotted_as_PY_NAMEs
  48. # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
  49. import_from: ('from' (('.' | '...')* dotted_PY_NAME | ('.' | '...')+)
  50. ('import' | 'cimport') ('*' | '(' import_as_PY_NAMEs ')' | import_as_PY_NAMEs))
  51. import_as_PY_NAME: PY_NAME ['as' PY_NAME]
  52. dotted_as_PY_NAME: dotted_PY_NAME ['as' PY_NAME]
  53. import_as_PY_NAMEs: import_as_PY_NAME (',' import_as_PY_NAME)* [',']
  54. dotted_as_PY_NAMEs: dotted_as_PY_NAME (',' dotted_as_PY_NAME)*
  55. dotted_PY_NAME: PY_NAME ('.' PY_NAME)*
  56. global_stmt: 'global' PY_NAME (',' PY_NAME)*
  57. nonlocal_stmt: 'nonlocal' PY_NAME (',' PY_NAME)*
  58. exec_stmt: 'exec' expr ['in' test [',' test]]
  59. assert_stmt: 'assert' test [',' test]
  60. compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
  61. if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  62. while_stmt: 'while' test ':' suite ['else' ':' suite]
  63. for_stmt: 'for' exprlist ('in' testlist | for_from_clause)':' suite ['else' ':' suite]
  64. for_from_clause: 'from' expr comp_op PY_NAME comp_op expr ['by' expr]
  65. try_stmt: ('try' ':' suite
  66. ((except_clause ':' suite)+
  67. ['else' ':' suite]
  68. ['finally' ':' suite] |
  69. 'finally' ':' suite))
  70. with_stmt: 'with' with_item (',' with_item)* ':' suite
  71. with_item: test ['as' expr]
  72. # NB compile.c makes sure that the default except clause is last
  73. except_clause: 'except' [test [('as' | ',') test]]
  74. suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
  75. test: or_test ['if' or_test 'else' test] | lambdef
  76. test_nocond: or_test | lambdef_nocond
  77. lambdef: 'lambda' [varargslist] ':' test
  78. lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
  79. or_test: and_test ('or' and_test)*
  80. and_test: not_test ('and' not_test)*
  81. not_test: 'not' not_test | comparison
  82. comparison: expr (comp_op expr)*
  83. # <> isn't actually a valid comparison operator in Python. It's here for the
  84. # sake of a __future__ import described in PEP 401
  85. comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
  86. star_expr: '*' expr
  87. expr: xor_expr ('|' xor_expr)*
  88. xor_expr: and_expr ('^' and_expr)*
  89. and_expr: shift_expr ('&' shift_expr)*
  90. shift_expr: arith_expr (('<<'|'>>') arith_expr)*
  91. arith_expr: term (('+'|'-') term)*
  92. term: factor (('*'|'/'|'%'|'//') factor)*
  93. factor: ('+'|'-'|'~') factor | power | address | size_of | cast
  94. power: atom_expr ['**' factor]
  95. atom_expr: ['await'] atom trailer*
  96. atom: ('(' [yield_expr|testlist_comp] ')' |
  97. '[' [testlist_comp] ']' |
  98. '{' [dictorsetmaker] '}' |
  99. new_expr |
  100. PY_NAME | NUMBER | STRING+ | ellipsis | 'None' | 'True' | 'False')
  101. testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
  102. trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' (PY_NAME | 'sizeof')
  103. subscriptlist: subscript (',' subscript)* [',']
  104. subscript: test | [test] ':' [test] [sliceop]
  105. sliceop: ':' [test]
  106. exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
  107. testlist: test (',' test)* [',']
  108. dictorsetmaker: ( ((test ':' test | '**' expr)
  109. (comp_for | (',' (test ':' test | '**' expr))* [','])) |
  110. ((test | star_expr)
  111. (comp_for | (',' (test | star_expr))* [','])) )
  112. classdef: 'class' PY_NAME ['(' [arglist] ')'] ':' suite
  113. arglist: argument (',' argument)* [',']
  114. # The reason that keywords are test nodes instead of NAME is that using NAME
  115. # results in an ambiguity. ast.c makes sure it's a NAME.
  116. # "test '=' test" is really "keyword '=' test", but we have no such token.
  117. # These need to be in a single rule to avoid grammar that is ambiguous
  118. # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
  119. # we explicitly match '*' here, too, to give it proper precedence.
  120. # Illegal combinations and orderings are blocked in ast.c:
  121. # multiple (test comp_for) arguments are blocked; keyword unpackings
  122. # that precede iterable unpackings are blocked; etc.
  123. argument: ( test [comp_for] |
  124. test '=' test |
  125. '**' expr |
  126. star_expr )
  127. comp_iter: comp_for | comp_if
  128. comp_for: 'for' exprlist ('in' or_test | for_from_clause) [comp_iter]
  129. comp_if: 'if' test_nocond [comp_iter]
  130. # not used in grammar, but may appear in "node" passed from Parser to Compiler
  131. encoding_decl: NAME
  132. yield_expr: 'yield' [yield_arg]
  133. yield_arg: 'from' test | testlist
  134. # Cython extensions
  135. # Accommodate to Py2 tokenizer.
  136. ellipsis: '...' | '.' '.' '.'
  137. signedness: 'unsigned' | 'signed'
  138. longness: 'char' | 'short' | 'long' | 'long' 'long'
  139. # TODO: [unsigned] double doesn't make sens, but we need long double
  140. int_type: signedness [longness] | longness | [signedness] [longness] ('int' | 'double') | 'complex'
  141. type: ['const'] (NAME ('.' PY_NAME)* | int_type | '(' type ')') ['complex'] [type_qualifiers]
  142. maybe_typed_name: ['const'] (NAME [('.' PY_NAME)* ['complex'] [type_qualifiers] NAME] | (int_type | '(' type ')') ['complex'] [type_qualifiers] NAME)
  143. teplate_params: '[' NAME (',' NAME)* ']'
  144. type_qualifiers: type_qualifier+
  145. type_qualifier: '*' | '**' | '&' | type_index ('.' NAME [type_index])*
  146. # TODO: old buffer syntax
  147. type_index: '[' [(NUMBER | type (',' type)* | (memory_view_index (',' memory_view_index)*))] ']'
  148. memory_view_index: ':' [':'] [NUMBER]
  149. address: '&' factor
  150. cast: '<' type ['?'] '>' factor
  151. size_of: 'sizeof' '(' (type) ')'
  152. type_id: 'typeid' '(' (type) ')'
  153. new_expr: 'new' type '(' [arglist] ')'
  154. # TODO: Restrict cdef_stmt to "top-level" statements.
  155. cdef_stmt: ('cdef' | 'cpdef') (cvar_def | cdef_type_decl | extern_block)
  156. cdef_type_decl: ctype_decl | fused | cclass
  157. ctype_decl: struct | enum | cppclass
  158. # TODO: Does the cdef/ctypedef distinction even make sense for fused?
  159. ctypedef_stmt: 'ctypedef' (cvar_decl | struct | enum | fused)
  160. # Note: these two are similar but can't be used in an or clause
  161. # as it would cause ambiguity in the LL(1) parser.
  162. # Requires a type
  163. cvar_decl: [visibility] type cname (NEWLINE | cfunc)
  164. # Allows an assignment
  165. cvar_def: [visibility] maybe_typed_name (['=' test] (',' PY_NAME ['=' test])* NEWLINE | cfunc)
  166. visibility: 'public' | 'api' | 'readonly'
  167. # TODO: Standardize gil_spec first or last.
  168. cfunc: [teplate_params] parameters [gil_spec] [exception_value] [gil_spec] (':' suite | NEWLINE)
  169. exception_value: 'except' (['?'] expr | '*' | '+' [PY_NAME])
  170. gil_spec: 'with' ('gil' | 'nogil') | 'nogil'
  171. cname: NAME [STRING]
  172. cclass: classdef
  173. fused: 'fused' PY_NAME ':' NEWLINE INDENT ( type NEWLINE)+ DEDENT
  174. enum: 'enum' [cname] (NEWLINE | ':' enum_suite)
  175. enum_suite: NEWLINE INDENT (cname ['=' NUMBER] NEWLINE | pass_stmt NEWLINE)+ DEDENT
  176. struct: ('struct' | 'union') cname (NEWLINE | (':' struct_suite))
  177. struct_suite: NEWLINE INDENT (cvar_decl | pass_stmt NEWLINE)+ DEDENT
  178. cppclass: 'cppclass' cname [teplate_params] [cppclass_bases] (NEWLINE | ':' cppclass_suite)
  179. cppclass_bases: '(' dotted_PY_NAME (',' dotted_PY_NAME [teplate_params])*')'
  180. cppclass_suite: NEWLINE INDENT (cvar_decl | ctype_decl | pass_stmt NEWLINE)+ DEDENT
  181. # TODO: C++ constructors, operators
  182. extern_block: 'extern' (cvar_decl | 'from' ('*' | STRING) ['namespace' STRING] [gil_spec] ':' (pass_stmt | extern_suite))
  183. extern_suite: NEWLINE INDENT (['cdef' | 'cpdef'] (cvar_decl | cdef_type_decl) | ctypedef_stmt)+ DEDENT
  184. cy_type_kwd: 'struct' | 'union' | 'fused' | 'cppclass' | 'int' | 'double' | 'complex'
  185. cy_kwd: cy_type_kwd | signedness | longness | visibility | 'gil' | 'nogil' | 'namespace' | 'const' | 'by' | 'extern'
  186. PY_NAME: NAME | cy_kwd
  187. # TODO: Do we really want these? Don't play well with include...
  188. DEF_stmt: 'DEF' NAME '=' testlist
  189. IF_stmt: 'IF' test ':' suite ('ELIF' test ':' suite)* ['ELSE' ':' suite]