grammar.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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_expression_lhs),
  178. ':=',
  179. field('value', $.expression)
  180. ),
  181. _named_expression_lhs: $ => choice(
  182. $.identifier,
  183. $.keyword_identifier
  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. $.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. $.keyword_identifier,
  466. $.subscript,
  467. $.attribute,
  468. $.list_splat_pattern,
  469. $.tuple_pattern,
  470. $.list_pattern
  471. ),
  472. tuple_pattern: $ => seq(
  473. '(',
  474. optional($._patterns),
  475. ')'
  476. ),
  477. list_pattern: $ => seq(
  478. '[',
  479. optional($._patterns),
  480. ']'
  481. ),
  482. default_parameter: $ => seq(
  483. field('name', $.identifier),
  484. '=',
  485. field('value', $.expression)
  486. ),
  487. typed_default_parameter: $ => prec(PREC.typed_parameter, seq(
  488. field('name', $.identifier),
  489. ':',
  490. field('type', $.type),
  491. '=',
  492. field('value', $.expression)
  493. )),
  494. list_splat_pattern: $ => seq(
  495. '*',
  496. choice($.identifier, $.keyword_identifier, $.subscript, $.attribute)
  497. ),
  498. dictionary_splat_pattern: $ => seq(
  499. '**',
  500. choice($.identifier, $.keyword_identifier, $.subscript, $.attribute)
  501. ),
  502. // Extended patterns (patterns allowed in match statement are far more flexible than simple patterns though still a subset of "expression")
  503. as_pattern: $ => prec.left(seq(
  504. $.expression,
  505. 'as',
  506. field('alias', alias($.expression, $.as_pattern_target))
  507. )),
  508. // Expressions
  509. _expression_within_for_in_clause: $ => choice(
  510. $.expression,
  511. alias($.lambda_within_for_in_clause, $.lambda)
  512. ),
  513. expression: $ => choice(
  514. $.comparison_operator,
  515. $.not_operator,
  516. $.boolean_operator,
  517. $.await,
  518. $.lambda,
  519. $.primary_expression,
  520. $.conditional_expression,
  521. $.named_expression,
  522. $.as_pattern
  523. ),
  524. primary_expression: $ => choice(
  525. $.binary_operator,
  526. $.identifier,
  527. $.keyword_identifier,
  528. $.string,
  529. $.concatenated_string,
  530. $.integer,
  531. $.float,
  532. $.true,
  533. $.false,
  534. $.none,
  535. $.unary_operator,
  536. $.attribute,
  537. $.subscript,
  538. $.call,
  539. $.list,
  540. $.list_comprehension,
  541. $.dictionary,
  542. $.dictionary_comprehension,
  543. $.set,
  544. $.set_comprehension,
  545. $.tuple,
  546. $.parenthesized_expression,
  547. $.generator_expression,
  548. $.ellipsis
  549. ),
  550. not_operator: $ => prec(PREC.not, seq(
  551. 'not',
  552. field('argument', $.expression)
  553. )),
  554. boolean_operator: $ => choice(
  555. prec.left(PREC.and, seq(
  556. field('left', $.expression),
  557. field('operator', 'and'),
  558. field('right', $.expression)
  559. )),
  560. prec.left(PREC.or, seq(
  561. field('left', $.expression),
  562. field('operator', 'or'),
  563. field('right', $.expression)
  564. ))
  565. ),
  566. binary_operator: $ => {
  567. const table = [
  568. [prec.left, '+', PREC.plus],
  569. [prec.left, '-', PREC.plus],
  570. [prec.left, '*', PREC.times],
  571. [prec.left, '@', PREC.times],
  572. [prec.left, '/', PREC.times],
  573. [prec.left, '%', PREC.times],
  574. [prec.left, '//', PREC.times],
  575. [prec.right, '**', PREC.power],
  576. [prec.left, '|', PREC.bitwise_or],
  577. [prec.left, '&', PREC.bitwise_and],
  578. [prec.left, '^', PREC.xor],
  579. [prec.left, '<<', PREC.shift],
  580. [prec.left, '>>', PREC.shift],
  581. ];
  582. return choice(...table.map(([fn, operator, precedence]) => fn(precedence, seq(
  583. field('left', $.primary_expression),
  584. field('operator', operator),
  585. field('right', $.primary_expression)
  586. ))));
  587. },
  588. unary_operator: $ => prec(PREC.unary, seq(
  589. field('operator', choice('+', '-', '~')),
  590. field('argument', $.primary_expression)
  591. )),
  592. comparison_operator: $ => prec.left(PREC.compare, seq(
  593. $.primary_expression,
  594. repeat1(seq(
  595. field('operators',
  596. choice(
  597. '<',
  598. '<=',
  599. '==',
  600. '!=',
  601. '>=',
  602. '>',
  603. '<>',
  604. 'in',
  605. alias(seq('not', 'in'), 'not in'),
  606. 'is',
  607. alias(seq('is', 'not'), 'is not')
  608. )),
  609. $.primary_expression
  610. ))
  611. )),
  612. lambda: $ => prec(PREC.lambda, seq(
  613. 'lambda',
  614. field('parameters', optional($.lambda_parameters)),
  615. ':',
  616. field('body', $.expression)
  617. )),
  618. lambda_within_for_in_clause: $ => seq(
  619. 'lambda',
  620. field('parameters', optional($.lambda_parameters)),
  621. ':',
  622. field('body', $._expression_within_for_in_clause)
  623. ),
  624. assignment: $ => seq(
  625. field('left', $._left_hand_side),
  626. choice(
  627. seq('=', field('right', $._right_hand_side)),
  628. seq(':', field('type', $.type)),
  629. seq(':', field('type', $.type), '=', field('right', $._right_hand_side))
  630. )
  631. ),
  632. augmented_assignment: $ => seq(
  633. field('left', $._left_hand_side),
  634. field('operator', choice(
  635. '+=', '-=', '*=', '/=', '@=', '//=', '%=', '**=',
  636. '>>=', '<<=', '&=', '^=', '|='
  637. )),
  638. field('right', $._right_hand_side)
  639. ),
  640. _left_hand_side: $ => choice(
  641. $.pattern,
  642. $.pattern_list,
  643. ),
  644. pattern_list: $ => seq(
  645. $.pattern,
  646. choice(
  647. ',',
  648. seq(
  649. repeat1(seq(
  650. ',',
  651. $.pattern
  652. )),
  653. optional(',')
  654. )
  655. )
  656. ),
  657. _right_hand_side: $ => choice(
  658. $.expression,
  659. $.expression_list,
  660. $.assignment,
  661. $.augmented_assignment,
  662. $.yield
  663. ),
  664. yield: $ => prec.right(seq(
  665. 'yield',
  666. choice(
  667. seq(
  668. 'from',
  669. $.expression
  670. ),
  671. optional($._expressions)
  672. )
  673. )),
  674. attribute: $ => prec(PREC.call, seq(
  675. field('object', $.primary_expression),
  676. '.',
  677. field('attribute', $.identifier)
  678. )),
  679. subscript: $ => prec(PREC.call, seq(
  680. field('value', $.primary_expression),
  681. '[',
  682. commaSep1(field('subscript', choice($.expression, $.slice))),
  683. optional(','),
  684. ']'
  685. )),
  686. slice: $ => seq(
  687. optional($.expression),
  688. ':',
  689. optional($.expression),
  690. optional(seq(':', optional($.expression)))
  691. ),
  692. ellipsis: $ => '...',
  693. call: $ => prec(PREC.call, seq(
  694. field('function', $.primary_expression),
  695. field('arguments', choice(
  696. $.generator_expression,
  697. $.argument_list
  698. ))
  699. )),
  700. typed_parameter: $ => prec(PREC.typed_parameter, seq(
  701. choice(
  702. $.identifier,
  703. $.list_splat_pattern,
  704. $.dictionary_splat_pattern
  705. ),
  706. ':',
  707. field('type', $.type)
  708. )),
  709. type: $ => $.expression,
  710. keyword_argument: $ => seq(
  711. field('name', choice($.identifier, $.keyword_identifier)),
  712. '=',
  713. field('value', $.expression)
  714. ),
  715. // Literals
  716. list: $ => seq(
  717. '[',
  718. optional($._collection_elements),
  719. ']'
  720. ),
  721. set: $ => seq(
  722. '{',
  723. $._collection_elements,
  724. '}'
  725. ),
  726. tuple: $ => seq(
  727. '(',
  728. optional($._collection_elements),
  729. ')'
  730. ),
  731. dictionary: $ => seq(
  732. '{',
  733. optional(commaSep1(choice($.pair, $.dictionary_splat))),
  734. optional(','),
  735. '}'
  736. ),
  737. pair: $ => seq(
  738. field('key', $.expression),
  739. ':',
  740. field('value', $.expression)
  741. ),
  742. list_comprehension: $ => seq(
  743. '[',
  744. field('body', $.expression),
  745. $._comprehension_clauses,
  746. ']'
  747. ),
  748. dictionary_comprehension: $ => seq(
  749. '{',
  750. field('body', $.pair),
  751. $._comprehension_clauses,
  752. '}'
  753. ),
  754. set_comprehension: $ => seq(
  755. '{',
  756. field('body', $.expression),
  757. $._comprehension_clauses,
  758. '}'
  759. ),
  760. generator_expression: $ => seq(
  761. '(',
  762. field('body', $.expression),
  763. $._comprehension_clauses,
  764. ')'
  765. ),
  766. _comprehension_clauses: $ => seq(
  767. $.for_in_clause,
  768. repeat(choice(
  769. $.for_in_clause,
  770. $.if_clause
  771. ))
  772. ),
  773. parenthesized_expression: $ => prec(PREC.parenthesized_expression, seq(
  774. '(',
  775. choice($.expression, $.yield),
  776. ')'
  777. )),
  778. _collection_elements: $ => seq(
  779. commaSep1(choice(
  780. $.expression, $.yield, $.list_splat, $.parenthesized_list_splat
  781. )),
  782. optional(',')
  783. ),
  784. for_in_clause: $ => prec.left(seq(
  785. optional('async'),
  786. 'for',
  787. field('left', $._left_hand_side),
  788. 'in',
  789. field('right', commaSep1($._expression_within_for_in_clause)),
  790. optional(',')
  791. )),
  792. if_clause: $ => seq(
  793. 'if',
  794. $.expression
  795. ),
  796. conditional_expression: $ => prec.right(PREC.conditional, seq(
  797. $.expression,
  798. 'if',
  799. $.expression,
  800. 'else',
  801. $.expression
  802. )),
  803. concatenated_string: $ => seq(
  804. $.string,
  805. repeat1($.string)
  806. ),
  807. string: $ => seq(
  808. field('prefix', alias($._string_start, '"')),
  809. repeat(choice(
  810. field('interpolation', $.interpolation),
  811. field('string_content', $.string_content)
  812. )),
  813. field('suffix', alias($._string_end, '"'))
  814. ),
  815. string_content: $ => prec.right(0, repeat1(
  816. choice(
  817. $._escape_interpolation,
  818. $.escape_sequence,
  819. $._not_escape_sequence,
  820. $._string_content
  821. ))),
  822. interpolation: $ => seq(
  823. token.immediate('{'),
  824. field('expression', $._f_expression),
  825. optional('='),
  826. optional(field('type_conversion', $.type_conversion)),
  827. optional(field('format_specifier', $.format_specifier)),
  828. '}'
  829. ),
  830. _f_expression: $ => choice(
  831. $.expression,
  832. $.expression_list,
  833. $.yield,
  834. ),
  835. _escape_interpolation: $ => token.immediate(choice('{{', '}}')),
  836. escape_sequence: $ => token.immediate(prec(1, seq(
  837. '\\',
  838. choice(
  839. /u[a-fA-F\d]{4}/,
  840. /U[a-fA-F\d]{8}/,
  841. /x[a-fA-F\d]{2}/,
  842. /\d{3}/,
  843. /\r?\n/,
  844. /['"abfrntv\\]/,
  845. )
  846. ))),
  847. _not_escape_sequence: $ => token.immediate('\\'),
  848. format_specifier: $ => seq(
  849. ':',
  850. repeat(choice(
  851. token(prec(1, /[^{}\n]+/)),
  852. alias($.interpolation, $.format_expression)
  853. ))
  854. ),
  855. type_conversion: $ => /![a-z]/,
  856. integer: $ => token(choice(
  857. seq(
  858. choice('0x', '0X'),
  859. repeat1(/_?[A-Fa-f0-9]+/),
  860. optional(/[Ll]/)
  861. ),
  862. seq(
  863. choice('0o', '0O'),
  864. repeat1(/_?[0-7]+/),
  865. optional(/[Ll]/)
  866. ),
  867. seq(
  868. choice('0b', '0B'),
  869. repeat1(/_?[0-1]+/),
  870. optional(/[Ll]/)
  871. ),
  872. seq(
  873. repeat1(/[0-9]+_?/),
  874. choice(
  875. optional(/[Ll]/), // long numbers
  876. optional(/[jJ]/) // complex numbers
  877. )
  878. )
  879. )),
  880. float: $ => {
  881. const digits = repeat1(/[0-9]+_?/);
  882. const exponent = seq(/[eE][\+-]?/, digits)
  883. return token(seq(
  884. choice(
  885. seq(digits, '.', optional(digits), optional(exponent)),
  886. seq(optional(digits), '.', digits, optional(exponent)),
  887. seq(digits, exponent)
  888. ),
  889. optional(choice(/[Ll]/, /[jJ]/))
  890. ))
  891. },
  892. identifier: $ => /[_\p{XID_Start}][_\p{XID_Continue}]*/,
  893. keyword_identifier: $ => prec(-3, alias(
  894. choice(
  895. 'print',
  896. 'exec',
  897. 'async',
  898. 'await',
  899. 'match'
  900. ),
  901. $.identifier
  902. )),
  903. true: $ => 'True',
  904. false: $ => 'False',
  905. none: $ => 'None',
  906. await: $ => prec(PREC.unary, seq(
  907. 'await',
  908. $.expression
  909. )),
  910. comment: $ => token(seq('#', /.*/)),
  911. positional_separator: $ => '/',
  912. keyword_separator: $ => '*',
  913. }
  914. })
  915. function commaSep1(rule) {
  916. return sep1(rule, ',')
  917. }
  918. function sep1(rule, separator) {
  919. return seq(rule, repeat(seq(separator, rule)))
  920. }