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