10. 完整的语法规范

这是完整的 Python 语法规范,直接提取自用于生成 CPython 解析器的语法 (参见 Grammar/python.gram)。 这里显示的版本省略了有关代码生成和错误恢复的细节。

该标记法是 EBNFPEG 的混合体。 特别地,& 后跟一个符号、形符或带括号的分组来表示肯定型前视(即要求匹配但不消耗字符)。而 ! 表示否定型前视(即要求 匹配)。 我们使用 | 分隔符来表示 PEG 的“有序选择”(在传统 PEG 语法中为 / 写法)。 请参阅 PEP 617 了解有关该语法规则的更多细节。

  1. # PEG grammar for Python
  2. # ========================= START OF THE GRAMMAR =========================
  3. # General grammatical elements and rules:
  4. #
  5. # * Strings with double quotes (") denote SOFT KEYWORDS
  6. # * Strings with single quotes (') denote KEYWORDS
  7. # * Upper case names (NAME) denote tokens in the Grammar/Tokens file
  8. # * Rule names starting with "invalid_" are used for specialized syntax errors
  9. # - These rules are NOT used in the first pass of the parser.
  10. # - Only if the first pass fails to parse, a second pass including the invalid
  11. # rules will be executed.
  12. # - If the parser fails in the second phase with a generic syntax error, the
  13. # location of the generic failure of the first pass will be used (this avoids
  14. # reporting incorrect locations due to the invalid rules).
  15. # - The order of the alternatives involving invalid rules matter
  16. # (like any rule in PEG).
  17. #
  18. # Grammar Syntax (see PEP 617 for more information):
  19. #
  20. # rule_name: expression
  21. # Optionally, a type can be included right after the rule name, which
  22. # specifies the return type of the C or Python function corresponding to the
  23. # rule:
  24. # rule_name[return_type]: expression
  25. # If the return type is omitted, then a void * is returned in C and an Any in
  26. # Python.
  27. # e1 e2
  28. # Match e1, then match e2.
  29. # e1 | e2
  30. # Match e1 or e2.
  31. # The first alternative can also appear on the line after the rule name for
  32. # formatting purposes. In that case, a | must be used before the first
  33. # alternative, like so:
  34. # rule_name[return_type]:
  35. # | first_alt
  36. # | second_alt
  37. # ( e )
  38. # Match e (allows also to use other operators in the group like '(e)*')
  39. # [ e ] or e?
  40. # Optionally match e.
  41. # e*
  42. # Match zero or more occurrences of e.
  43. # e+
  44. # Match one or more occurrences of e.
  45. # s.e+
  46. # Match one or more occurrences of e, separated by s. The generated parse tree
  47. # does not include the separator. This is otherwise identical to (e (s e)*).
  48. # &e
  49. # Succeed if e can be parsed, without consuming any input.
  50. # !e
  51. # Fail if e can be parsed, without consuming any input.
  52. # ~
  53. # Commit to the current alternative, even if it fails to parse.
  54. # &&e
  55. # Eager parse e. The parser will not backtrack and will immediately
  56. # fail with SyntaxError if e cannot be parsed.
  57. #
  58. # STARTING RULES
  59. # ==============
  60. file: [statements] ENDMARKER
  61. interactive: statement_newline
  62. eval: expressions NEWLINE* ENDMARKER
  63. func_type: '(' [type_expressions] ')' '->' expression NEWLINE* ENDMARKER
  64. # GENERAL STATEMENTS
  65. # ==================
  66. statements: statement+
  67. statement: compound_stmt | simple_stmts
  68. statement_newline:
  69. | compound_stmt NEWLINE
  70. | simple_stmts
  71. | NEWLINE
  72. | ENDMARKER
  73. simple_stmts:
  74. | simple_stmt !';' NEWLINE # Not needed, there for speedup
  75. | ';'.simple_stmt+ [';'] NEWLINE
  76. # NOTE: assignment MUST precede expression, else parsing a simple assignment
  77. # will throw a SyntaxError.
  78. simple_stmt:
  79. | assignment
  80. | type_alias
  81. | star_expressions
  82. | return_stmt
  83. | import_stmt
  84. | raise_stmt
  85. | 'pass'
  86. | del_stmt
  87. | yield_stmt
  88. | assert_stmt
  89. | 'break'
  90. | 'continue'
  91. | global_stmt
  92. | nonlocal_stmt
  93. compound_stmt:
  94. | function_def
  95. | if_stmt
  96. | class_def
  97. | with_stmt
  98. | for_stmt
  99. | try_stmt
  100. | while_stmt
  101. | match_stmt
  102. # SIMPLE STATEMENTS
  103. # =================
  104. # NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield'
  105. assignment:
  106. | NAME ':' expression ['=' annotated_rhs ]
  107. | ('(' single_target ')'
  108. | single_subscript_attribute_target) ':' expression ['=' annotated_rhs ]
  109. | (star_targets '=' )+ (yield_expr | star_expressions) !'=' [TYPE_COMMENT]
  110. | single_target augassign ~ (yield_expr | star_expressions)
  111. annotated_rhs: yield_expr | star_expressions
  112. augassign:
  113. | '+='
  114. | '-='
  115. | '*='
  116. | '@='
  117. | '/='
  118. | '%='
  119. | '&='
  120. | '|='
  121. | '^='
  122. | '<<='
  123. | '>>='
  124. | '**='
  125. | '//='
  126. return_stmt:
  127. | 'return' [star_expressions]
  128. raise_stmt:
  129. | 'raise' expression ['from' expression ]
  130. | 'raise'
  131. global_stmt: 'global' ','.NAME+
  132. nonlocal_stmt: 'nonlocal' ','.NAME+
  133. del_stmt:
  134. | 'del' del_targets &(';' | NEWLINE)
  135. yield_stmt: yield_expr
  136. assert_stmt: 'assert' expression [',' expression ]
  137. import_stmt:
  138. | import_name
  139. | import_from
  140. # Import statements
  141. # -----------------
  142. import_name: 'import' dotted_as_names
  143. # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
  144. import_from:
  145. | 'from' ('.' | '...')* dotted_name 'import' import_from_targets
  146. | 'from' ('.' | '...')+ 'import' import_from_targets
  147. import_from_targets:
  148. | '(' import_from_as_names [','] ')'
  149. | import_from_as_names !','
  150. | '*'
  151. import_from_as_names:
  152. | ','.import_from_as_name+
  153. import_from_as_name:
  154. | NAME ['as' NAME ]
  155. dotted_as_names:
  156. | ','.dotted_as_name+
  157. dotted_as_name:
  158. | dotted_name ['as' NAME ]
  159. dotted_name:
  160. | dotted_name '.' NAME
  161. | NAME
  162. # COMPOUND STATEMENTS
  163. # ===================
  164. # Common elements
  165. # ---------------
  166. block:
  167. | NEWLINE INDENT statements DEDENT
  168. | simple_stmts
  169. decorators: ('@' named_expression NEWLINE )+
  170. # Class definitions
  171. # -----------------
  172. class_def:
  173. | decorators class_def_raw
  174. | class_def_raw
  175. class_def_raw:
  176. | 'class' NAME [type_params] ['(' [arguments] ')' ] ':' block
  177. # Function definitions
  178. # --------------------
  179. function_def:
  180. | decorators function_def_raw
  181. | function_def_raw
  182. function_def_raw:
  183. | 'def' NAME [type_params] '(' [params] ')' ['->' expression ] ':' [func_type_comment] block
  184. | 'async' 'def' NAME [type_params] '(' [params] ')' ['->' expression ] ':' [func_type_comment] block
  185. # Function parameters
  186. # -------------------
  187. params:
  188. | parameters
  189. parameters:
  190. | slash_no_default param_no_default* param_with_default* [star_etc]
  191. | slash_with_default param_with_default* [star_etc]
  192. | param_no_default+ param_with_default* [star_etc]
  193. | param_with_default+ [star_etc]
  194. | star_etc
  195. # Some duplication here because we can't write (',' | &')'),
  196. # which is because we don't support empty alternatives (yet).
  197. slash_no_default:
  198. | param_no_default+ '/' ','
  199. | param_no_default+ '/' &')'
  200. slash_with_default:
  201. | param_no_default* param_with_default+ '/' ','
  202. | param_no_default* param_with_default+ '/' &')'
  203. star_etc:
  204. | '*' param_no_default param_maybe_default* [kwds]
  205. | '*' param_no_default_star_annotation param_maybe_default* [kwds]
  206. | '*' ',' param_maybe_default+ [kwds]
  207. | kwds
  208. kwds:
  209. | '**' param_no_default
  210. # One parameter. This *includes* a following comma and type comment.
  211. #
  212. # There are three styles:
  213. # - No default
  214. # - With default
  215. # - Maybe with default
  216. #
  217. # There are two alternative forms of each, to deal with type comments:
  218. # - Ends in a comma followed by an optional type comment
  219. # - No comma, optional type comment, must be followed by close paren
  220. # The latter form is for a final parameter without trailing comma.
  221. #
  222. param_no_default:
  223. | param ',' TYPE_COMMENT?
  224. | param TYPE_COMMENT? &')'
  225. param_no_default_star_annotation:
  226. | param_star_annotation ',' TYPE_COMMENT?
  227. | param_star_annotation TYPE_COMMENT? &')'
  228. param_with_default:
  229. | param default ',' TYPE_COMMENT?
  230. | param default TYPE_COMMENT? &')'
  231. param_maybe_default:
  232. | param default? ',' TYPE_COMMENT?
  233. | param default? TYPE_COMMENT? &')'
  234. param: NAME annotation?
  235. param_star_annotation: NAME star_annotation
  236. annotation: ':' expression
  237. star_annotation: ':' star_expression
  238. default: '=' expression | invalid_default
  239. # If statement
  240. # ------------
  241. if_stmt:
  242. | 'if' named_expression ':' block elif_stmt
  243. | 'if' named_expression ':' block [else_block]
  244. elif_stmt:
  245. | 'elif' named_expression ':' block elif_stmt
  246. | 'elif' named_expression ':' block [else_block]
  247. else_block:
  248. | 'else' ':' block
  249. # While statement
  250. # ---------------
  251. while_stmt:
  252. | 'while' named_expression ':' block [else_block]
  253. # For statement
  254. # -------------
  255. for_stmt:
  256. | 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]
  257. | 'async' 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]
  258. # With statement
  259. # --------------
  260. with_stmt:
  261. | 'with' '(' ','.with_item+ ','? ')' ':' [TYPE_COMMENT] block
  262. | 'with' ','.with_item+ ':' [TYPE_COMMENT] block
  263. | 'async' 'with' '(' ','.with_item+ ','? ')' ':' block
  264. | 'async' 'with' ','.with_item+ ':' [TYPE_COMMENT] block
  265. with_item:
  266. | expression 'as' star_target &(',' | ')' | ':')
  267. | expression
  268. # Try statement
  269. # -------------
  270. try_stmt:
  271. | 'try' ':' block finally_block
  272. | 'try' ':' block except_block+ [else_block] [finally_block]
  273. | 'try' ':' block except_star_block+ [else_block] [finally_block]
  274. # Except statement
  275. # ----------------
  276. except_block:
  277. | 'except' expression ['as' NAME ] ':' block
  278. | 'except' ':' block
  279. except_star_block:
  280. | 'except' '*' expression ['as' NAME ] ':' block
  281. finally_block:
  282. | 'finally' ':' block
  283. # Match statement
  284. # ---------------
  285. match_stmt:
  286. | "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT
  287. subject_expr:
  288. | star_named_expression ',' star_named_expressions?
  289. | named_expression
  290. case_block:
  291. | "case" patterns guard? ':' block
  292. guard: 'if' named_expression
  293. patterns:
  294. | open_sequence_pattern
  295. | pattern
  296. pattern:
  297. | as_pattern
  298. | or_pattern
  299. as_pattern:
  300. | or_pattern 'as' pattern_capture_target
  301. or_pattern:
  302. | '|'.closed_pattern+
  303. closed_pattern:
  304. | literal_pattern
  305. | capture_pattern
  306. | wildcard_pattern
  307. | value_pattern
  308. | group_pattern
  309. | sequence_pattern
  310. | mapping_pattern
  311. | class_pattern
  312. # Literal patterns are used for equality and identity constraints
  313. literal_pattern:
  314. | signed_number !('+' | '-')
  315. | complex_number
  316. | strings
  317. | 'None'
  318. | 'True'
  319. | 'False'
  320. # Literal expressions are used to restrict permitted mapping pattern keys
  321. literal_expr:
  322. | signed_number !('+' | '-')
  323. | complex_number
  324. | strings
  325. | 'None'
  326. | 'True'
  327. | 'False'
  328. complex_number:
  329. | signed_real_number '+' imaginary_number
  330. | signed_real_number '-' imaginary_number
  331. signed_number:
  332. | NUMBER
  333. | '-' NUMBER
  334. signed_real_number:
  335. | real_number
  336. | '-' real_number
  337. real_number:
  338. | NUMBER
  339. imaginary_number:
  340. | NUMBER
  341. capture_pattern:
  342. | pattern_capture_target
  343. pattern_capture_target:
  344. | !"_" NAME !('.' | '(' | '=')
  345. wildcard_pattern:
  346. | "_"
  347. value_pattern:
  348. | attr !('.' | '(' | '=')
  349. attr:
  350. | name_or_attr '.' NAME
  351. name_or_attr:
  352. | attr
  353. | NAME
  354. group_pattern:
  355. | '(' pattern ')'
  356. sequence_pattern:
  357. | '[' maybe_sequence_pattern? ']'
  358. | '(' open_sequence_pattern? ')'
  359. open_sequence_pattern:
  360. | maybe_star_pattern ',' maybe_sequence_pattern?
  361. maybe_sequence_pattern:
  362. | ','.maybe_star_pattern+ ','?
  363. maybe_star_pattern:
  364. | star_pattern
  365. | pattern
  366. star_pattern:
  367. | '*' pattern_capture_target
  368. | '*' wildcard_pattern
  369. mapping_pattern:
  370. | '{' '}'
  371. | '{' double_star_pattern ','? '}'
  372. | '{' items_pattern ',' double_star_pattern ','? '}'
  373. | '{' items_pattern ','? '}'
  374. items_pattern:
  375. | ','.key_value_pattern+
  376. key_value_pattern:
  377. | (literal_expr | attr) ':' pattern
  378. double_star_pattern:
  379. | '**' pattern_capture_target
  380. class_pattern:
  381. | name_or_attr '(' ')'
  382. | name_or_attr '(' positional_patterns ','? ')'
  383. | name_or_attr '(' keyword_patterns ','? ')'
  384. | name_or_attr '(' positional_patterns ',' keyword_patterns ','? ')'
  385. positional_patterns:
  386. | ','.pattern+
  387. keyword_patterns:
  388. | ','.keyword_pattern+
  389. keyword_pattern:
  390. | NAME '=' pattern
  391. # Type statement
  392. # ---------------
  393. type_alias:
  394. | "type" NAME [type_params] '=' expression
  395. # Type parameter declaration
  396. # --------------------------
  397. type_params:
  398. | invalid_type_params
  399. | '[' type_param_seq ']'
  400. type_param_seq: ','.type_param+ [',']
  401. type_param:
  402. | NAME [type_param_bound] [type_param_default]
  403. | '*' NAME [type_param_starred_default]
  404. | '**' NAME [type_param_default]
  405. type_param_bound: ':' expression
  406. type_param_default: '=' expression
  407. type_param_starred_default: '=' star_expression
  408. # EXPRESSIONS
  409. # -----------
  410. expressions:
  411. | expression (',' expression )+ [',']
  412. | expression ','
  413. | expression
  414. expression:
  415. | disjunction 'if' disjunction 'else' expression
  416. | disjunction
  417. | lambdef
  418. yield_expr:
  419. | 'yield' 'from' expression
  420. | 'yield' [star_expressions]
  421. star_expressions:
  422. | star_expression (',' star_expression )+ [',']
  423. | star_expression ','
  424. | star_expression
  425. star_expression:
  426. | '*' bitwise_or
  427. | expression
  428. star_named_expressions: ','.star_named_expression+ [',']
  429. star_named_expression:
  430. | '*' bitwise_or
  431. | named_expression
  432. assignment_expression:
  433. | NAME ':=' ~ expression
  434. named_expression:
  435. | assignment_expression
  436. | expression !':='
  437. disjunction:
  438. | conjunction ('or' conjunction )+
  439. | conjunction
  440. conjunction:
  441. | inversion ('and' inversion )+
  442. | inversion
  443. inversion:
  444. | 'not' inversion
  445. | comparison
  446. # Comparison operators
  447. # --------------------
  448. comparison:
  449. | bitwise_or compare_op_bitwise_or_pair+
  450. | bitwise_or
  451. compare_op_bitwise_or_pair:
  452. | eq_bitwise_or
  453. | noteq_bitwise_or
  454. | lte_bitwise_or
  455. | lt_bitwise_or
  456. | gte_bitwise_or
  457. | gt_bitwise_or
  458. | notin_bitwise_or
  459. | in_bitwise_or
  460. | isnot_bitwise_or
  461. | is_bitwise_or
  462. eq_bitwise_or: '==' bitwise_or
  463. noteq_bitwise_or:
  464. | ('!=' ) bitwise_or
  465. lte_bitwise_or: '<=' bitwise_or
  466. lt_bitwise_or: '<' bitwise_or
  467. gte_bitwise_or: '>=' bitwise_or
  468. gt_bitwise_or: '>' bitwise_or
  469. notin_bitwise_or: 'not' 'in' bitwise_or
  470. in_bitwise_or: 'in' bitwise_or
  471. isnot_bitwise_or: 'is' 'not' bitwise_or
  472. is_bitwise_or: 'is' bitwise_or
  473. # Bitwise operators
  474. # -----------------
  475. bitwise_or:
  476. | bitwise_or '|' bitwise_xor
  477. | bitwise_xor
  478. bitwise_xor:
  479. | bitwise_xor '^' bitwise_and
  480. | bitwise_and
  481. bitwise_and:
  482. | bitwise_and '&' shift_expr
  483. | shift_expr
  484. shift_expr:
  485. | shift_expr '<<' sum
  486. | shift_expr '>>' sum
  487. | sum
  488. # Arithmetic operators
  489. # --------------------
  490. sum:
  491. | sum '+' term
  492. | sum '-' term
  493. | term
  494. term:
  495. | term '*' factor
  496. | term '/' factor
  497. | term '//' factor
  498. | term '%' factor
  499. | term '@' factor
  500. | factor
  501. factor:
  502. | '+' factor
  503. | '-' factor
  504. | '~' factor
  505. | power
  506. power:
  507. | await_primary '**' factor
  508. | await_primary
  509. # Primary elements
  510. # ----------------
  511. # Primary elements are things like "obj.something.something", "obj[something]", "obj(something)", "obj" ...
  512. await_primary:
  513. | 'await' primary
  514. | primary
  515. primary:
  516. | primary '.' NAME
  517. | primary genexp
  518. | primary '(' [arguments] ')'
  519. | primary '[' slices ']'
  520. | atom
  521. slices:
  522. | slice !','
  523. | ','.(slice | starred_expression)+ [',']
  524. slice:
  525. | [expression] ':' [expression] [':' [expression] ]
  526. | named_expression
  527. atom:
  528. | NAME
  529. | 'True'
  530. | 'False'
  531. | 'None'
  532. | strings
  533. | NUMBER
  534. | (tuple | group | genexp)
  535. | (list | listcomp)
  536. | (dict | set | dictcomp | setcomp)
  537. | '...'
  538. group:
  539. | '(' (yield_expr | named_expression) ')'
  540. # Lambda functions
  541. # ----------------
  542. lambdef:
  543. | 'lambda' [lambda_params] ':' expression
  544. lambda_params:
  545. | lambda_parameters
  546. # lambda_parameters etc. duplicates parameters but without annotations
  547. # or type comments, and if there's no comma after a parameter, we expect
  548. # a colon, not a close parenthesis. (For more, see parameters above.)
  549. #
  550. lambda_parameters:
  551. | lambda_slash_no_default lambda_param_no_default* lambda_param_with_default* [lambda_star_etc]
  552. | lambda_slash_with_default lambda_param_with_default* [lambda_star_etc]
  553. | lambda_param_no_default+ lambda_param_with_default* [lambda_star_etc]
  554. | lambda_param_with_default+ [lambda_star_etc]
  555. | lambda_star_etc
  556. lambda_slash_no_default:
  557. | lambda_param_no_default+ '/' ','
  558. | lambda_param_no_default+ '/' &':'
  559. lambda_slash_with_default:
  560. | lambda_param_no_default* lambda_param_with_default+ '/' ','
  561. | lambda_param_no_default* lambda_param_with_default+ '/' &':'
  562. lambda_star_etc:
  563. | '*' lambda_param_no_default lambda_param_maybe_default* [lambda_kwds]
  564. | '*' ',' lambda_param_maybe_default+ [lambda_kwds]
  565. | lambda_kwds
  566. lambda_kwds:
  567. | '**' lambda_param_no_default
  568. lambda_param_no_default:
  569. | lambda_param ','
  570. | lambda_param &':'
  571. lambda_param_with_default:
  572. | lambda_param default ','
  573. | lambda_param default &':'
  574. lambda_param_maybe_default:
  575. | lambda_param default? ','
  576. | lambda_param default? &':'
  577. lambda_param: NAME
  578. # LITERALS
  579. # ========
  580. fstring_middle:
  581. | fstring_replacement_field
  582. | FSTRING_MIDDLE
  583. fstring_replacement_field:
  584. | '{' annotated_rhs '='? [fstring_conversion] [fstring_full_format_spec] '}'
  585. fstring_conversion:
  586. | "!" NAME
  587. fstring_full_format_spec:
  588. | ':' fstring_format_spec*
  589. fstring_format_spec:
  590. | FSTRING_MIDDLE
  591. | fstring_replacement_field
  592. fstring:
  593. | FSTRING_START fstring_middle* FSTRING_END
  594. string: STRING
  595. strings: (fstring|string)+
  596. list:
  597. | '[' [star_named_expressions] ']'
  598. tuple:
  599. | '(' [star_named_expression ',' [star_named_expressions] ] ')'
  600. set: '{' star_named_expressions '}'
  601. # Dicts
  602. # -----
  603. dict:
  604. | '{' [double_starred_kvpairs] '}'
  605. double_starred_kvpairs: ','.double_starred_kvpair+ [',']
  606. double_starred_kvpair:
  607. | '**' bitwise_or
  608. | kvpair
  609. kvpair: expression ':' expression
  610. # Comprehensions & Generators
  611. # ---------------------------
  612. for_if_clauses:
  613. | for_if_clause+
  614. for_if_clause:
  615. | 'async' 'for' star_targets 'in' ~ disjunction ('if' disjunction )*
  616. | 'for' star_targets 'in' ~ disjunction ('if' disjunction )*
  617. listcomp:
  618. | '[' named_expression for_if_clauses ']'
  619. setcomp:
  620. | '{' named_expression for_if_clauses '}'
  621. genexp:
  622. | '(' ( assignment_expression | expression !':=') for_if_clauses ')'
  623. dictcomp:
  624. | '{' kvpair for_if_clauses '}'
  625. # FUNCTION CALL ARGUMENTS
  626. # =======================
  627. arguments:
  628. | args [','] &')'
  629. args:
  630. | ','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ [',' kwargs ]
  631. | kwargs
  632. kwargs:
  633. | ','.kwarg_or_starred+ ',' ','.kwarg_or_double_starred+
  634. | ','.kwarg_or_starred+
  635. | ','.kwarg_or_double_starred+
  636. starred_expression:
  637. | '*' expression
  638. kwarg_or_starred:
  639. | NAME '=' expression
  640. | starred_expression
  641. kwarg_or_double_starred:
  642. | NAME '=' expression
  643. | '**' expression
  644. # ASSIGNMENT TARGETS
  645. # ==================
  646. # Generic targets
  647. # ---------------
  648. # NOTE: star_targets may contain *bitwise_or, targets may not.
  649. star_targets:
  650. | star_target !','
  651. | star_target (',' star_target )* [',']
  652. star_targets_list_seq: ','.star_target+ [',']
  653. star_targets_tuple_seq:
  654. | star_target (',' star_target )+ [',']
  655. | star_target ','
  656. star_target:
  657. | '*' (!'*' star_target)
  658. | target_with_star_atom
  659. target_with_star_atom:
  660. | t_primary '.' NAME !t_lookahead
  661. | t_primary '[' slices ']' !t_lookahead
  662. | star_atom
  663. star_atom:
  664. | NAME
  665. | '(' target_with_star_atom ')'
  666. | '(' [star_targets_tuple_seq] ')'
  667. | '[' [star_targets_list_seq] ']'
  668. single_target:
  669. | single_subscript_attribute_target
  670. | NAME
  671. | '(' single_target ')'
  672. single_subscript_attribute_target:
  673. | t_primary '.' NAME !t_lookahead
  674. | t_primary '[' slices ']' !t_lookahead
  675. t_primary:
  676. | t_primary '.' NAME &t_lookahead
  677. | t_primary '[' slices ']' &t_lookahead
  678. | t_primary genexp &t_lookahead
  679. | t_primary '(' [arguments] ')' &t_lookahead
  680. | atom &t_lookahead
  681. t_lookahead: '(' | '[' | '.'
  682. # Targets for del statements
  683. # --------------------------
  684. del_targets: ','.del_target+ [',']
  685. del_target:
  686. | t_primary '.' NAME !t_lookahead
  687. | t_primary '[' slices ']' !t_lookahead
  688. | del_t_atom
  689. del_t_atom:
  690. | NAME
  691. | '(' del_target ')'
  692. | '(' [del_targets] ')'
  693. | '[' [del_targets] ']'
  694. # TYPING ELEMENTS
  695. # ---------------
  696. # type_expressions allow */** but ignore them
  697. type_expressions:
  698. | ','.expression+ ',' '*' expression ',' '**' expression
  699. | ','.expression+ ',' '*' expression
  700. | ','.expression+ ',' '**' expression
  701. | '*' expression ',' '**' expression
  702. | '*' expression
  703. | '**' expression
  704. | ','.expression+
  705. func_type_comment:
  706. | NEWLINE TYPE_COMMENT &(NEWLINE INDENT) # Must be followed by indented block
  707. | TYPE_COMMENT
  708. # ========================= END OF THE GRAMMAR ===========================
  709. # ========================= START OF INVALID RULES =======================