grammar.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. const PREC = {
  2. // this resolves a conflict between the usage of ':' in a lambda vs in a
  3. // typed parameter. In the case of a lambda, we don't allow typed parameters.
  4. lambda: -2,
  5. typed_parameter: -1,
  6. conditional: -1,
  7. parenthesized_expression: 1,
  8. parenthesized_list_splat: 1,
  9. or: 10,
  10. and: 11,
  11. not: 12,
  12. compare: 13,
  13. bitwise_or: 14,
  14. bitwise_and: 15,
  15. xor: 16,
  16. shift: 17,
  17. plus: 18,
  18. times: 19,
  19. unary: 20,
  20. power: 21,
  21. call: 22,
  22. }
  23. const SEMICOLON = ';'
  24. module.exports = grammar({
  25. name: 'python',
  26. extras: $ => [
  27. $.comment,
  28. /[\s\f\uFEFF\u2060\u200B]|\\\r?\n/
  29. ],
  30. conflicts: $ => [
  31. [$.primary_expression, $.pattern],
  32. [$.primary_expression, $.list_splat_pattern],
  33. [$.tuple, $.tuple_pattern],
  34. [$.list, $.list_pattern],
  35. [$.with_item, $._collection_elements],
  36. [$.named_expression, $.as_pattern],
  37. [$.match_statement, $.primary_expression],
  38. ],
  39. supertypes: $ => [
  40. $._simple_statement,
  41. $._compound_statement,
  42. $.expression,
  43. $.primary_expression,
  44. $.pattern,
  45. $.parameter,
  46. ],
  47. externals: $ => [
  48. $._newline,
  49. $._indent,
  50. $._dedent,
  51. $._string_start,
  52. $._string_content,
  53. $._string_end,
  54. // Mark comments as external tokens so that the external scanner is always
  55. // invoked, even if no external token is expected. This allows for better
  56. // error recovery, because the external scanner can maintain the overall
  57. // structure by returning dedent tokens whenever a dedent occurs, even
  58. // if no dedent is expected.
  59. $.comment,
  60. // Allow the external scanner to check for the validity of closing brackets
  61. // so that it can avoid returning dedent tokens between brackets.
  62. ']',
  63. ')',
  64. '}',
  65. ],
  66. inline: $ => [
  67. $._simple_statement,
  68. $._compound_statement,
  69. $._suite,
  70. $._expressions,
  71. $._left_hand_side,
  72. $.keyword_identifier,
  73. ],
  74. word: $ => $.identifier,
  75. rules: {
  76. module: $ => repeat($._statement),
  77. _statement: $ => choice(
  78. $._simple_statements,
  79. $._compound_statement
  80. ),
  81. // Simple statements
  82. _simple_statements: $ => seq(
  83. sep1($._simple_statement, SEMICOLON),
  84. optional(SEMICOLON),
  85. $._newline
  86. ),
  87. _simple_statement: $ => choice(
  88. $.future_import_statement,
  89. $.import_statement,
  90. $.import_from_statement,
  91. $.print_statement,
  92. $.assert_statement,
  93. $.expression_statement,
  94. $.return_statement,
  95. $.delete_statement,
  96. $.raise_statement,
  97. $.pass_statement,
  98. $.break_statement,
  99. $.continue_statement,
  100. $.global_statement,
  101. $.nonlocal_statement,
  102. $.exec_statement
  103. ),
  104. import_statement: $ => seq(
  105. 'import',
  106. $._import_list
  107. ),
  108. import_prefix: $ => repeat1('.'),
  109. relative_import: $ => seq(
  110. $.import_prefix,
  111. optional($.dotted_name)
  112. ),
  113. future_import_statement: $ => seq(
  114. 'from',
  115. '__future__',
  116. 'import',
  117. choice(
  118. $._import_list,
  119. seq('(', $._import_list, ')'),
  120. )
  121. ),
  122. import_from_statement: $ => seq(
  123. 'from',
  124. field('module_name', choice(
  125. $.relative_import,
  126. $.dotted_name
  127. )),
  128. 'import',
  129. choice(
  130. $.wildcard_import,
  131. $._import_list,
  132. seq('(', $._import_list, ')')
  133. )
  134. ),
  135. _import_list: $ => seq(
  136. commaSep1(field('name', choice(
  137. $.dotted_name,
  138. $.aliased_import
  139. ))),
  140. optional(',')
  141. ),
  142. aliased_import: $ => seq(
  143. field('name', $.dotted_name),
  144. 'as',
  145. field('alias', $.identifier)
  146. ),
  147. wildcard_import: $ => '*',
  148. print_statement: $ => choice(
  149. prec(1, seq(
  150. 'print',
  151. $.chevron,
  152. repeat(seq(',', field('argument', $.expression))),
  153. optional(','))
  154. ),
  155. prec(-10, seq(
  156. 'print',
  157. commaSep1(field('argument', $.expression)),
  158. optional(',')
  159. ))
  160. ),
  161. chevron: $ => seq(
  162. '>>',
  163. $.expression
  164. ),
  165. assert_statement: $ => seq(
  166. 'assert',
  167. commaSep1($.expression)
  168. ),
  169. expression_statement: $ => choice(
  170. $.expression,
  171. seq(commaSep1($.expression), optional(',')),
  172. $.assignment,
  173. $.augmented_assignment,
  174. $.yield
  175. ),
  176. named_expression: $ => seq(
  177. field('name', $._named_expresssion_lhs),
  178. ':=',
  179. field('value', $.expression)
  180. ),
  181. _named_expresssion_lhs: $ => choice(
  182. $.identifier,
  183. alias('match', $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword
  184. ),
  185. return_statement: $ => seq(
  186. 'return',
  187. optional($._expressions)
  188. ),
  189. delete_statement: $ => seq(
  190. 'del',
  191. $._expressions
  192. ),
  193. _expressions: $ => choice(
  194. $.expression,
  195. $.expression_list
  196. ),
  197. raise_statement: $ => seq(
  198. 'raise',
  199. optional($._expressions),
  200. optional(seq('from', field('cause', $.expression)))
  201. ),
  202. pass_statement: $ => prec.left('pass'),
  203. break_statement: $ => prec.left('break'),
  204. continue_statement: $ => prec.left('continue'),
  205. // Compound statements
  206. _compound_statement: $ => choice(
  207. $.if_statement,
  208. $.for_statement,
  209. $.while_statement,
  210. $.try_statement,
  211. $.with_statement,
  212. $.function_definition,
  213. $.class_definition,
  214. $.decorated_definition,
  215. $.match_statement,
  216. ),
  217. if_statement: $ => seq(
  218. 'if',
  219. field('condition', $.expression),
  220. ':',
  221. field('consequence', $._suite),
  222. repeat(field('alternative', $.elif_clause)),
  223. optional(field('alternative', $.else_clause))
  224. ),
  225. elif_clause: $ => seq(
  226. 'elif',
  227. field('condition', $.expression),
  228. ':',
  229. field('consequence', $._suite)
  230. ),
  231. else_clause: $ => seq(
  232. 'else',
  233. ':',
  234. field('body', $._suite)
  235. ),
  236. match_statement: $ => seq(
  237. 'match',
  238. commaSep1(field('subject', $.expression)),
  239. optional(','),
  240. ':',
  241. repeat(field('alternative', $.case_clause))),
  242. case_clause: $ => seq(
  243. 'case',
  244. commaSep1(
  245. field(
  246. 'pattern',
  247. alias(choice($.expression, $.list_splat_pattern), $.case_pattern),
  248. )
  249. ),
  250. optional(','),
  251. optional(field('guard', $.if_clause)),
  252. ':',
  253. field('consequence', $._suite)
  254. ),
  255. for_statement: $ => seq(
  256. optional('async'),
  257. 'for',
  258. field('left', $._left_hand_side),
  259. 'in',
  260. field('right', $._expressions),
  261. ':',
  262. field('body', $._suite),
  263. field('alternative', optional($.else_clause))
  264. ),
  265. while_statement: $ => seq(
  266. 'while',
  267. field('condition', $.expression),
  268. ':',
  269. field('body', $._suite),
  270. optional(field('alternative', $.else_clause))
  271. ),
  272. try_statement: $ => seq(
  273. 'try',
  274. ':',
  275. field('body', $._suite),
  276. choice(
  277. seq(
  278. repeat1($.except_clause),
  279. optional($.else_clause),
  280. optional($.finally_clause)
  281. ),
  282. seq(
  283. repeat1($.except_group_clause),
  284. optional($.else_clause),
  285. optional($.finally_clause)
  286. ),
  287. $.finally_clause
  288. )
  289. ),
  290. except_clause: $ => seq(
  291. 'except',
  292. optional(seq(
  293. $.expression,
  294. optional(seq(
  295. choice('as', ','),
  296. $.expression
  297. ))
  298. )),
  299. ':',
  300. $._suite
  301. ),
  302. except_group_clause: $ => seq(
  303. 'except*',
  304. seq(
  305. $.expression,
  306. optional(seq(
  307. 'as',
  308. $.expression
  309. ))
  310. ),
  311. ':',
  312. $._suite
  313. ),
  314. finally_clause: $ => seq(
  315. 'finally',
  316. ':',
  317. $._suite
  318. ),
  319. with_statement: $ => seq(
  320. optional('async'),
  321. 'with',
  322. $.with_clause,
  323. ':',
  324. field('body', $._suite)
  325. ),
  326. with_clause: $ => choice(
  327. seq(commaSep1($.with_item), optional(',')),
  328. seq('(', commaSep1($.with_item), optional(','), ')')
  329. ),
  330. with_item: $ => prec.dynamic(1, seq(
  331. field('value', $.expression),
  332. )),
  333. function_definition: $ => seq(
  334. optional('async'),
  335. 'def',
  336. field('name', $.identifier),
  337. field('parameters', $.parameters),
  338. optional(
  339. seq(
  340. '->',
  341. field('return_type', $.type)
  342. )
  343. ),
  344. ':',
  345. field('body', $._suite)
  346. ),
  347. parameters: $ => seq(
  348. '(',
  349. optional($._parameters),
  350. ')'
  351. ),
  352. lambda_parameters: $ => $._parameters,
  353. list_splat: $ => seq(
  354. '*',
  355. $.expression,
  356. ),
  357. dictionary_splat: $ => seq(
  358. '**',
  359. $.expression
  360. ),
  361. global_statement: $ => seq(
  362. 'global',
  363. commaSep1($.identifier)
  364. ),
  365. nonlocal_statement: $ => seq(
  366. 'nonlocal',
  367. commaSep1($.identifier)
  368. ),
  369. exec_statement: $ => seq(
  370. 'exec',
  371. field('code', $.string),
  372. optional(
  373. seq(
  374. 'in',
  375. commaSep1($.expression)
  376. )
  377. )
  378. ),
  379. class_definition: $ => seq(
  380. 'class',
  381. field('name', $.identifier),
  382. field('superclasses', optional($.argument_list)),
  383. ':',
  384. field('body', $._suite)
  385. ),
  386. parenthesized_list_splat: $ => prec(PREC.parenthesized_list_splat, seq(
  387. '(',
  388. choice(
  389. alias($.parenthesized_list_splat, $.parenthesized_expression),
  390. $.list_splat,
  391. ),
  392. ')',
  393. )),
  394. argument_list: $ => seq(
  395. '(',
  396. optional(commaSep1(
  397. choice(
  398. $.expression,
  399. $.list_splat,
  400. $.dictionary_splat,
  401. alias($.parenthesized_list_splat, $.parenthesized_expression),
  402. $.keyword_argument
  403. )
  404. )),
  405. optional(','),
  406. ')'
  407. ),
  408. decorated_definition: $ => seq(
  409. repeat1($.decorator),
  410. field('definition', choice(
  411. $.class_definition,
  412. $.function_definition
  413. ))
  414. ),
  415. decorator: $ => seq(
  416. '@',
  417. $.primary_expression,
  418. $._newline
  419. ),
  420. _suite: $ => choice(
  421. alias($._simple_statements, $.block),
  422. seq($._indent, $.block),
  423. alias($._newline, $.block)
  424. ),
  425. block: $ => seq(
  426. repeat($._statement),
  427. $._dedent
  428. ),
  429. expression_list: $ => prec.right(seq(
  430. $.expression,
  431. choice(
  432. ',',
  433. seq(
  434. repeat1(seq(
  435. ',',
  436. $.expression
  437. )),
  438. optional(',')
  439. ),
  440. )
  441. )),
  442. dotted_name: $ => sep1($.identifier, '.'),
  443. // Patterns
  444. _parameters: $ => seq(
  445. commaSep1($.parameter),
  446. optional(',')
  447. ),
  448. _patterns: $ => seq(
  449. commaSep1($.pattern),
  450. optional(',')
  451. ),
  452. parameter: $ => choice(
  453. $.identifier,
  454. $.typed_parameter,
  455. $.default_parameter,
  456. $.typed_default_parameter,
  457. $.list_splat_pattern,
  458. $.tuple_pattern,
  459. $.keyword_separator,
  460. $.positional_separator,
  461. $.dictionary_splat_pattern
  462. ),
  463. pattern: $ => choice(
  464. $.identifier,
  465. alias('match', $.identifier), // ambiguity with match statement: only ":" at end of line decides if "match" keyword
  466. $.keyword_identifier,
  467. $.subscript,
  468. $.attribute,
  469. $.list_splat_pattern,
  470. $.tuple_pattern,
  471. $.list_pattern
  472. ),
  473. tuple_pattern: $ => seq(
  474. '(',
  475. optional($._patterns),
  476. ')'
  477. ),
  478. list_pattern: $ => seq(
  479. '[',
  480. optional($._patterns),
  481. ']'
  482. ),
  483. default_parameter: $ => seq(
  484. field('name', $.identifier),
  485. '=',
  486. field('value', $.expression)
  487. ),
  488. typed_default_parameter: $ => prec(PREC.typed_parameter, seq(
  489. field('name', $.identifier),
  490. ':',
  491. field('type', $.type),
  492. '=',
  493. field('value', $.expression)
  494. )),
  495. list_splat_pattern: $ => seq(
  496. '*',
  497. choice($.identifier, $.keyword_identifier, $.subscript, $.attribute)
  498. ),
  499. dictionary_splat_pattern: $ => seq(
  500. '**',
  501. choice($.identifier, $.keyword_identifier, $.subscript, $.attribute)
  502. ),
  503. // Extended patterns (patterns allowed in match statement are far more flexible than simple patterns though still a subset of "expression")
  504. as_pattern: $ => prec.left(seq(
  505. $.expression,
  506. 'as',
  507. field('alias', alias($.expression, $.as_pattern_target))
  508. )),
  509. // Expressions
  510. _expression_within_for_in_clause: $ => choice(
  511. $.expression,
  512. alias($.lambda_within_for_in_clause, $.lambda)
  513. ),
  514. expression: $ => choice(
  515. $.comparison_operator,
  516. $.not_operator,
  517. $.boolean_operator,
  518. $.await,
  519. $.lambda,
  520. $.primary_expression,
  521. $.conditional_expression,
  522. $.named_expression,
  523. $.as_pattern
  524. ),
  525. primary_expression: $ => choice(
  526. $.binary_operator,
  527. $.identifier,
  528. alias("match", $.identifier),
  529. $.keyword_identifier,
  530. $.string,
  531. $.concatenated_string,
  532. $.integer,
  533. $.float,
  534. $.true,
  535. $.false,
  536. $.none,
  537. $.unary_operator,
  538. $.attribute,
  539. $.subscript,
  540. $.call,
  541. $.list,
  542. $.list_comprehension,
  543. $.dictionary,
  544. $.dictionary_comprehension,
  545. $.set,
  546. $.set_comprehension,
  547. $.tuple,
  548. $.parenthesized_expression,
  549. $.generator_expression,
  550. $.ellipsis
  551. ),
  552. not_operator: $ => prec(PREC.not, seq(
  553. 'not',
  554. field('argument', $.expression)
  555. )),
  556. boolean_operator: $ => choice(
  557. prec.left(PREC.and, seq(
  558. field('left', $.expression),
  559. field('operator', 'and'),
  560. field('right', $.expression)
  561. )),
  562. prec.left(PREC.or, seq(
  563. field('left', $.expression),
  564. field('operator', 'or'),
  565. field('right', $.expression)
  566. ))
  567. ),
  568. binary_operator: $ => {
  569. const table = [
  570. [prec.left, '+', PREC.plus],
  571. [prec.left, '-', PREC.plus],
  572. [prec.left, '*', PREC.times],
  573. [prec.left, '@', PREC.times],
  574. [prec.left, '/', PREC.times],
  575. [prec.left, '%', PREC.times],
  576. [prec.left, '//', PREC.times],
  577. [prec.right, '**', PREC.power],
  578. [prec.left, '|', PREC.bitwise_or],
  579. [prec.left, '&', PREC.bitwise_and],
  580. [prec.left, '^', PREC.xor],
  581. [prec.left, '<<', PREC.shift],
  582. [prec.left, '>>', PREC.shift],
  583. ];
  584. return choice(...table.map(([fn, operator, precedence]) => fn(precedence, seq(
  585. field('left', $.primary_expression),
  586. field('operator', operator),
  587. field('right', $.primary_expression)
  588. ))));
  589. },
  590. unary_operator: $ => prec(PREC.unary, seq(
  591. field('operator', choice('+', '-', '~')),
  592. field('argument', $.primary_expression)
  593. )),
  594. comparison_operator: $ => prec.left(PREC.compare, seq(
  595. $.primary_expression,
  596. repeat1(seq(
  597. field('operators',
  598. choice(
  599. '<',
  600. '<=',
  601. '==',
  602. '!=',
  603. '>=',
  604. '>',
  605. '<>',
  606. 'in',
  607. seq('not', 'in'),
  608. 'is',
  609. seq('is', 'not')
  610. )),
  611. $.primary_expression
  612. ))
  613. )),
  614. lambda: $ => prec(PREC.lambda, seq(
  615. 'lambda',
  616. field('parameters', optional($.lambda_parameters)),
  617. ':',
  618. field('body', $.expression)
  619. )),
  620. lambda_within_for_in_clause: $ => seq(
  621. 'lambda',
  622. field('parameters', optional($.lambda_parameters)),
  623. ':',
  624. field('body', $._expression_within_for_in_clause)
  625. ),
  626. assignment: $ => seq(
  627. field('left', $._left_hand_side),
  628. choice(
  629. seq('=', field('right', $._right_hand_side)),
  630. seq(':', field('type', $.type)),
  631. seq(':', field('type', $.type), '=', field('right', $._right_hand_side))
  632. )
  633. ),
  634. augmented_assignment: $ => seq(
  635. field('left', $._left_hand_side),
  636. field('operator', choice(
  637. '+=', '-=', '*=', '/=', '@=', '//=', '%=', '**=',
  638. '>>=', '<<=', '&=', '^=', '|='
  639. )),
  640. field('right', $._right_hand_side)
  641. ),
  642. _left_hand_side: $ => choice(
  643. $.pattern,
  644. $.pattern_list,
  645. ),
  646. pattern_list: $ => seq(
  647. $.pattern,
  648. choice(
  649. ',',
  650. seq(
  651. repeat1(seq(
  652. ',',
  653. $.pattern
  654. )),
  655. optional(',')
  656. )
  657. )
  658. ),
  659. _right_hand_side: $ => choice(
  660. $.expression,
  661. $.expression_list,
  662. $.assignment,
  663. $.augmented_assignment,
  664. $.yield
  665. ),
  666. yield: $ => prec.right(seq(
  667. 'yield',
  668. choice(
  669. seq(
  670. 'from',
  671. $.expression
  672. ),
  673. optional($._expressions)
  674. )
  675. )),
  676. attribute: $ => prec(PREC.call, seq(
  677. field('object', $.primary_expression),
  678. '.',
  679. field('attribute', $.identifier)
  680. )),
  681. subscript: $ => prec(PREC.call, seq(
  682. field('value', $.primary_expression),
  683. '[',
  684. commaSep1(field('subscript', choice($.expression, $.slice))),
  685. optional(','),
  686. ']'
  687. )),
  688. slice: $ => seq(
  689. optional($.expression),
  690. ':',
  691. optional($.expression),
  692. optional(seq(':', optional($.expression)))
  693. ),
  694. ellipsis: $ => '...',
  695. call: $ => prec(PREC.call, seq(
  696. field('function', $.primary_expression),
  697. field('arguments', choice(
  698. $.generator_expression,
  699. $.argument_list
  700. ))
  701. )),
  702. typed_parameter: $ => prec(PREC.typed_parameter, seq(
  703. choice(
  704. $.identifier,
  705. $.list_splat_pattern,
  706. $.dictionary_splat_pattern
  707. ),
  708. ':',
  709. field('type', $.type)
  710. )),
  711. type: $ => $.expression,
  712. keyword_argument: $ => seq(
  713. field('name', choice($.identifier, $.keyword_identifier, alias("match", $.identifier))),
  714. '=',
  715. field('value', $.expression)
  716. ),
  717. // Literals
  718. list: $ => seq(
  719. '[',
  720. optional($._collection_elements),
  721. ']'
  722. ),
  723. set: $ => seq(
  724. '{',
  725. $._collection_elements,
  726. '}'
  727. ),
  728. tuple: $ => seq(
  729. '(',
  730. optional($._collection_elements),
  731. ')'
  732. ),
  733. dictionary: $ => seq(
  734. '{',
  735. optional(commaSep1(choice($.pair, $.dictionary_splat))),
  736. optional(','),
  737. '}'
  738. ),
  739. pair: $ => seq(
  740. field('key', $.expression),
  741. ':',
  742. field('value', $.expression)
  743. ),
  744. list_comprehension: $ => seq(
  745. '[',
  746. field('body', $.expression),
  747. $._comprehension_clauses,
  748. ']'
  749. ),
  750. dictionary_comprehension: $ => seq(
  751. '{',
  752. field('body', $.pair),
  753. $._comprehension_clauses,
  754. '}'
  755. ),
  756. set_comprehension: $ => seq(
  757. '{',
  758. field('body', $.expression),
  759. $._comprehension_clauses,
  760. '}'
  761. ),
  762. generator_expression: $ => seq(
  763. '(',
  764. field('body', $.expression),
  765. $._comprehension_clauses,
  766. ')'
  767. ),
  768. _comprehension_clauses: $ => seq(
  769. $.for_in_clause,
  770. repeat(choice(
  771. $.for_in_clause,
  772. $.if_clause
  773. ))
  774. ),
  775. parenthesized_expression: $ => prec(PREC.parenthesized_expression, seq(
  776. '(',
  777. choice($.expression, $.yield),
  778. ')'
  779. )),
  780. _collection_elements: $ => seq(
  781. commaSep1(choice(
  782. $.expression, $.yield, $.list_splat, $.parenthesized_list_splat
  783. )),
  784. optional(',')
  785. ),
  786. for_in_clause: $ => prec.left(seq(
  787. optional('async'),
  788. 'for',
  789. field('left', $._left_hand_side),
  790. 'in',
  791. field('right', commaSep1($._expression_within_for_in_clause)),
  792. optional(',')
  793. )),
  794. if_clause: $ => seq(
  795. 'if',
  796. $.expression
  797. ),
  798. conditional_expression: $ => prec.right(PREC.conditional, seq(
  799. $.expression,
  800. 'if',
  801. $.expression,
  802. 'else',
  803. $.expression
  804. )),
  805. concatenated_string: $ => seq(
  806. $.string,
  807. repeat1($.string)
  808. ),
  809. string: $ => seq(
  810. alias($._string_start, '"'),
  811. repeat(choice($.interpolation, $._escape_interpolation, $.escape_sequence, $._not_escape_sequence, $._string_content)),
  812. alias($._string_end, '"')
  813. ),
  814. interpolation: $ => seq(
  815. '{',
  816. $.expression,
  817. optional('='),
  818. optional($.type_conversion),
  819. optional($.format_specifier),
  820. '}'
  821. ),
  822. _escape_interpolation: $ => choice('{{', '}}'),
  823. escape_sequence: $ => token(prec(1, seq(
  824. '\\',
  825. choice(
  826. /u[a-fA-F\d]{4}/,
  827. /U[a-fA-F\d]{8}/,
  828. /x[a-fA-F\d]{2}/,
  829. /\d{3}/,
  830. /\r?\n/,
  831. /['"abfrntv\\]/,
  832. )
  833. ))),
  834. _not_escape_sequence: $ => '\\',
  835. format_specifier: $ => seq(
  836. ':',
  837. repeat(choice(
  838. token(prec(1, /[^{}\n]+/)),
  839. $.format_expression
  840. ))
  841. ),
  842. format_expression: $ => seq('{', $.expression, '}'),
  843. type_conversion: $ => /![a-z]/,
  844. integer: $ => token(choice(
  845. seq(
  846. choice('0x', '0X'),
  847. repeat1(/_?[A-Fa-f0-9]+/),
  848. optional(/[Ll]/)
  849. ),
  850. seq(
  851. choice('0o', '0O'),
  852. repeat1(/_?[0-7]+/),
  853. optional(/[Ll]/)
  854. ),
  855. seq(
  856. choice('0b', '0B'),
  857. repeat1(/_?[0-1]+/),
  858. optional(/[Ll]/)
  859. ),
  860. seq(
  861. repeat1(/[0-9]+_?/),
  862. choice(
  863. optional(/[Ll]/), // long numbers
  864. optional(/[jJ]/) // complex numbers
  865. )
  866. )
  867. )),
  868. float: $ => {
  869. const digits = repeat1(/[0-9]+_?/);
  870. const exponent = seq(/[eE][\+-]?/, digits)
  871. return token(seq(
  872. choice(
  873. seq(digits, '.', optional(digits), optional(exponent)),
  874. seq(optional(digits), '.', digits, optional(exponent)),
  875. seq(digits, exponent)
  876. ),
  877. optional(choice(/[Ll]/, /[jJ]/))
  878. ))
  879. },
  880. identifier: $ => /[_\p{XID_Start}][_\p{XID_Continue}]*/,
  881. keyword_identifier: $ => prec(-3, alias(
  882. choice(
  883. 'print',
  884. 'exec',
  885. 'async',
  886. 'await',
  887. ),
  888. $.identifier
  889. )),
  890. true: $ => 'True',
  891. false: $ => 'False',
  892. none: $ => 'None',
  893. await: $ => prec(PREC.unary, seq(
  894. 'await',
  895. $.expression
  896. )),
  897. comment: $ => token(seq('#', /.*/)),
  898. positional_separator: $ => '/',
  899. keyword_separator: $ => '*',
  900. }
  901. })
  902. function commaSep1(rule) {
  903. return sep1(rule, ',')
  904. }
  905. function sep1(rule, separator) {
  906. return seq(rule, repeat(seq(separator, rule)))
  907. }