latex.grammar 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // Track environments
  2. @context elementContext from "./tokens.mjs"
  3. // External tokens must be defined before normal @tokens to take precedence
  4. // over them.
  5. @external tokens verbTokenizer from "./tokens.mjs" {
  6. VerbContent
  7. }
  8. @external tokens lstinlineTokenizer from "./tokens.mjs" {
  9. LstInlineContent
  10. }
  11. @external tokens literalArgTokenizer from "./tokens.mjs" {
  12. LiteralArgContent
  13. }
  14. @external tokens spaceDelimitedLiteralArgTokenizer from "./tokens.mjs" {
  15. SpaceDelimitedLiteralArgContent
  16. }
  17. @external tokens verbatimTokenizer from "./tokens.mjs" {
  18. VerbatimContent
  19. }
  20. @external tokens mathDelimiterTokenizer from "./tokens.mjs" {
  21. MathDelimiter
  22. }
  23. // external tokenizer to read control sequence names including @ signs
  24. // (which are often used in TeX definitions).
  25. @external tokens csnameTokenizer from "./tokens.mjs" {
  26. Csname
  27. }
  28. @external tokens trailingContentTokenizer from "./tokens.mjs" {
  29. TrailingWhitespaceOnly,
  30. TrailingContent
  31. }
  32. // It doesn't seem to be possible to access specialized tokens in the context tracker.
  33. // They have id's which are not exported in the latex.terms.js file.
  34. // This is a workaround: use an external specializer to explicitly choose the terms
  35. // to use for the specialized tokens.
  36. @external specialize {CtrlSeq} specializeCtrlSeq from "./tokens.mjs" {
  37. Begin,
  38. End,
  39. RefCtrlSeq,
  40. RefStarrableCtrlSeq,
  41. CiteCtrlSeq,
  42. CiteStarrableCtrlSeq,
  43. LabelCtrlSeq,
  44. MathTextCtrlSeq,
  45. HboxCtrlSeq,
  46. TitleCtrlSeq,
  47. DocumentClassCtrlSeq,
  48. UsePackageCtrlSeq,
  49. HrefCtrlSeq,
  50. UrlCtrlSeq,
  51. VerbCtrlSeq,
  52. LstInlineCtrlSeq,
  53. IncludeGraphicsCtrlSeq,
  54. CaptionCtrlSeq,
  55. DefCtrlSeq,
  56. LeftCtrlSeq,
  57. RightCtrlSeq,
  58. NewCommandCtrlSeq,
  59. RenewCommandCtrlSeq,
  60. NewEnvironmentCtrlSeq,
  61. RenewEnvironmentCtrlSeq,
  62. // services/web/frontend/js/features/outline/outline-parser.js
  63. BookCtrlSeq,
  64. PartCtrlSeq,
  65. ChapterCtrlSeq,
  66. SectionCtrlSeq,
  67. SubSectionCtrlSeq,
  68. SubSubSectionCtrlSeq,
  69. ParagraphCtrlSeq,
  70. SubParagraphCtrlSeq,
  71. InputCtrlSeq,
  72. IncludeCtrlSeq,
  73. ItemCtrlSeq,
  74. NewTheoremCtrlSeq,
  75. TheoremStyleCtrlSeq,
  76. CenteringCtrlSeq,
  77. BibliographyCtrlSeq,
  78. BibliographyStyleCtrlSeq,
  79. AuthorCtrlSeq,
  80. MaketitleCtrlSeq,
  81. TextColorCtrlSeq,
  82. ColorBoxCtrlSeq,
  83. HLineCtrlSeq,
  84. TopRuleCtrlSeq,
  85. MidRuleCtrlSeq,
  86. BottomRuleCtrlSeq,
  87. MultiColumnCtrlSeq
  88. }
  89. @external specialize {EnvName} specializeEnvName from "./tokens.mjs" {
  90. DocumentEnvName,
  91. TabularEnvName,
  92. EquationEnvName,
  93. EquationArrayEnvName,
  94. VerbatimEnvName,
  95. TikzPictureEnvName,
  96. FigureEnvName,
  97. ListEnvName,
  98. TableEnvName
  99. }
  100. @external specialize {CtrlSym} specializeCtrlSym from "./tokens.mjs" {
  101. OpenParenCtrlSym,
  102. CloseParenCtrlSym,
  103. OpenBracketCtrlSym,
  104. CloseBracketCtrlSym,
  105. LineBreakCtrlSym
  106. }
  107. @tokens {
  108. CtrlSeq { "\\" $[a-zA-Z]+ }
  109. CtrlSym { "\\" ![a-zA-Z] }
  110. // tokens for paragraphs
  111. Whitespace { $[ \t]+ }
  112. NewLine { "\n" }
  113. BlankLine { "\n" "\n"+ }
  114. Normal { ![\\{}\[\]$&~#^_% \t\n] ![\\{}\[\]$&~#^_%\t\n]* } // everything is normal text, except these characters
  115. @precedence { CtrlSeq, CtrlSym, BlankLine, NewLine, Whitespace, Normal }
  116. OpenBrace[closedBy=CloseBrace] { "{" }
  117. CloseBrace[openedBy=OpenBrace] { "}" }
  118. OpenBracket[closedBy=CloseBracket] { "[" }
  119. CloseBracket[openedBy=OpenBracket] { "]" }
  120. Comment { "%" ![\n]* "\n"? }
  121. Dollar { "$" }
  122. Number { $[0-9]+ ("." $[0-9]*)? }
  123. MathSpecialChar { $[^_=<>()\-+/*]+ } // FIXME not all of these are special
  124. MathChar { ![0-9^_=<>()\-+/*\\{}\[\]\n$%&~]+ }
  125. @precedence { Number, MathSpecialChar, MathChar }
  126. Ampersand { "&" }
  127. Tilde { "~" }
  128. EnvName { $[a-zA-Z]+ $[*]? }
  129. }
  130. @top LaTeX {
  131. Text
  132. }
  133. @skip { Comment }
  134. // TEXT MODE
  135. optionalWhitespace {
  136. !argument Whitespace
  137. }
  138. OptionalArgument {
  139. !argument OpenBracket ShortOptionalArg CloseBracket
  140. }
  141. TextArgument {
  142. !argument OpenBrace LongArg CloseBrace
  143. }
  144. SectioningArgument {
  145. !argument OpenBrace LongArg CloseBrace
  146. }
  147. LabelArgument {
  148. !argument ShortTextArgument
  149. }
  150. RefArgument {
  151. !argument ShortTextArgument
  152. }
  153. BibKeyArgument {
  154. !argument ShortTextArgument
  155. }
  156. PackageArgument {
  157. !argument ShortTextArgument
  158. }
  159. TabularArgument {
  160. !argument OpenBrace TabularContent CloseBrace
  161. }
  162. UrlArgument {
  163. OpenBrace LiteralArgContent CloseBrace
  164. }
  165. FilePathArgument {
  166. OpenBrace LiteralArgContent CloseBrace
  167. }
  168. BareFilePathArgument {
  169. Whitespace SpaceDelimitedLiteralArgContent
  170. }
  171. DefinitionArgument {
  172. !argument NewLine? Whitespace* OpenBrace DefinitionFragment CloseBrace
  173. }
  174. argument[@isGroup="$Argument"] {
  175. TextArgument
  176. | SectioningArgument
  177. | LabelArgument
  178. | RefArgument
  179. | BibKeyArgument
  180. | PackageArgument
  181. | UrlArgument
  182. | FilePathArgument
  183. | BareFilePathArgument
  184. | DefinitionArgument
  185. }
  186. MacroParameter {
  187. "#" ("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")
  188. }
  189. OptionalMacroParameter {
  190. OpenBracket MacroParameter CloseBracket
  191. }
  192. // The autocompletion code in services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts
  193. // depends on following the `KnownCommand { Command { CommandCtrlSeq [args] } }`
  194. // structure
  195. KnownCommand {
  196. Title {
  197. TitleCtrlSeq optionalWhitespace? OptionalArgument? TextArgument
  198. } |
  199. Author {
  200. AuthorCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  201. } |
  202. DocumentClass {
  203. DocumentClassCtrlSeq optionalWhitespace? OptionalArgument?
  204. DocumentClassArgument { ShortTextArgument }
  205. } |
  206. BibliographyCommand {
  207. BibliographyCtrlSeq optionalWhitespace?
  208. BibliographyArgument { ShortTextArgument }
  209. } |
  210. BibliographyStyleCommand {
  211. BibliographyStyleCtrlSeq optionalWhitespace?
  212. BibliographyStyleArgument { ShortTextArgument }
  213. } |
  214. UsePackage {
  215. UsePackageCtrlSeq optionalWhitespace? OptionalArgument?
  216. PackageArgument
  217. } |
  218. TextColorCommand {
  219. TextColorCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? TextArgument
  220. } |
  221. ColorBoxCommand {
  222. ColorBoxCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? TextArgument
  223. } |
  224. HrefCommand {
  225. HrefCtrlSeq optionalWhitespace? UrlArgument ShortTextArgument
  226. } |
  227. NewTheoremCommand {
  228. NewTheoremCtrlSeq "*"? optionalWhitespace? ShortTextArgument ((OptionalArgument? TextArgument) | (TextArgument OptionalArgument))
  229. } |
  230. TheoremStyleCommand {
  231. TheoremStyleCtrlSeq optionalWhitespace? ShortTextArgument
  232. } |
  233. UrlCommand {
  234. UrlCtrlSeq optionalWhitespace? UrlArgument
  235. } |
  236. VerbCommand {
  237. VerbCtrlSeq VerbContent
  238. } |
  239. LstInlineCommand {
  240. LstInlineCtrlSeq optionalWhitespace? OptionalArgument? LstInlineContent
  241. } |
  242. IncludeGraphics {
  243. IncludeGraphicsCtrlSeq optionalWhitespace? OptionalArgument?
  244. IncludeGraphicsArgument { FilePathArgument }
  245. } |
  246. Caption {
  247. CaptionCtrlSeq "*"? optionalWhitespace? OptionalArgument? TextArgument
  248. } |
  249. Label {
  250. LabelCtrlSeq optionalWhitespace? LabelArgument
  251. } |
  252. Ref {
  253. (RefCtrlSeq | RefStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? RefArgument
  254. } |
  255. Cite {
  256. (CiteCtrlSeq | CiteStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? BibKeyArgument
  257. } |
  258. Def {
  259. // allow more general Csname argument to \def commands, since other symbols such as '@' are often used in definitions
  260. DefCtrlSeq optionalWhitespace? (Csname | CtrlSym) optionalWhitespace? (MacroParameter | OptionalMacroParameter)* optionalWhitespace? DefinitionArgument
  261. } |
  262. Hbox {
  263. HboxCtrlSeq optionalWhitespace? TextArgument
  264. } |
  265. Left {
  266. LeftCtrlSeq optionalWhitespace?
  267. } |
  268. Right {
  269. RightCtrlSeq optionalWhitespace?
  270. } |
  271. NewCommand {
  272. NewCommandCtrlSeq optionalWhitespace?
  273. (Csname | OpenBrace LiteralArgContent CloseBrace)
  274. (OptionalArgument)*
  275. DefinitionArgument
  276. } |
  277. RenewCommand {
  278. RenewCommandCtrlSeq optionalWhitespace?
  279. (Csname | OpenBrace LiteralArgContent CloseBrace)
  280. (OptionalArgument)*
  281. DefinitionArgument
  282. } |
  283. NewEnvironment {
  284. NewEnvironmentCtrlSeq optionalWhitespace?
  285. (OpenBrace LiteralArgContent CloseBrace)
  286. (OptionalArgument)*
  287. DefinitionArgument
  288. DefinitionArgument
  289. } |
  290. RenewEnvironment {
  291. RenewEnvironmentCtrlSeq optionalWhitespace?
  292. (Csname | OpenBrace LiteralArgContent CloseBrace)
  293. (OptionalArgument)*
  294. DefinitionArgument
  295. DefinitionArgument
  296. } |
  297. Input {
  298. InputCtrlSeq InputArgument { ( FilePathArgument | BareFilePathArgument ) }
  299. } |
  300. Include {
  301. IncludeCtrlSeq IncludeArgument { FilePathArgument }
  302. } |
  303. Centering {
  304. CenteringCtrlSeq
  305. } |
  306. Item {
  307. ItemCtrlSeq optionalWhitespace?
  308. } |
  309. Maketitle {
  310. MaketitleCtrlSeq optionalWhitespace?
  311. } |
  312. HorizontalLine {
  313. (HLineCtrlSeq | TopRuleCtrlSeq | MidRuleCtrlSeq | BottomRuleCtrlSeq) optionalWhitespace?
  314. } |
  315. MultiColumn {
  316. MultiColumnCtrlSeq
  317. optionalWhitespace? SpanArgument { ShortTextArgument }
  318. optionalWhitespace? ColumnArgument { ShortTextArgument }
  319. optionalWhitespace? TabularArgument
  320. }
  321. }
  322. UnknownCommand {
  323. (CtrlSeq !argument Whitespace (OptionalArgument | TextArgument)+)
  324. | ((CtrlSeq | MathTextCtrlSeq) (OptionalArgument | TextArgument)+)
  325. | CtrlSeq Whitespace?
  326. | CtrlSym
  327. }
  328. Command {
  329. KnownCommand
  330. | UnknownCommand
  331. | KnownCtrlSym
  332. }
  333. KnownCtrlSym {
  334. LineBreak {
  335. LineBreakCtrlSym OptionalArgument?
  336. }
  337. }
  338. textBase {
  339. ( Command
  340. | DollarMath
  341. | BracketMath
  342. | ParenMath
  343. | NewLine
  344. | Normal
  345. | Whitespace
  346. | Ampersand
  347. | Tilde
  348. )
  349. }
  350. textWithBrackets {
  351. ( textBase
  352. | OpenBracket
  353. | CloseBracket
  354. )
  355. }
  356. textWithEnvironmentsAndBlankLines {
  357. ( BlankLine
  358. | KnownEnvironment
  359. | Environment
  360. | textWithBrackets
  361. )
  362. }
  363. textWithGroupsEnvironmentsAndBlankLines {
  364. textWithEnvironmentsAndBlankLines
  365. | Group<Text>
  366. }
  367. Content<Element> {
  368. Element
  369. }
  370. SectioningCommand<Command> {
  371. Command optionalWhitespace? "*"? optionalWhitespace? OptionalArgument? optionalWhitespace? SectioningArgument
  372. }
  373. documentSection<Command, Next> {
  374. SectioningCommand<Command> Content<(sectionText | !section Next)*>
  375. }
  376. Book[@isGroup="$SectioningCommand"] { documentSection<BookCtrlSeq, Part | Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  377. Part[@isGroup="$SectioningCommand"] { documentSection<PartCtrlSeq, Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  378. Chapter[@isGroup="$SectioningCommand"] { documentSection<ChapterCtrlSeq, Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  379. Section[@isGroup="$SectioningCommand"] { documentSection<SectionCtrlSeq, SubSection | SubSubSection | Paragraph | SubParagraph> }
  380. SubSection[@isGroup="$SectioningCommand"] { documentSection<SubSectionCtrlSeq, SubSubSection | Paragraph | SubParagraph> }
  381. SubSubSection[@isGroup="$SectioningCommand"] { documentSection<SubSubSectionCtrlSeq, Paragraph | SubParagraph> }
  382. Paragraph[@isGroup="$SectioningCommand"] { documentSection<ParagraphCtrlSeq, SubParagraph> }
  383. SubParagraph[@isGroup="$SectioningCommand"] { SectioningCommand<SubParagraphCtrlSeq> Content<sectionText*> }
  384. sectioningCommand {
  385. Book | Part | Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph
  386. }
  387. sectionText {
  388. !section (
  389. textWithGroupsEnvironmentsAndBlankLines
  390. )+
  391. }
  392. Text {
  393. ( sectionText
  394. | sectioningCommand)+
  395. }
  396. LongArg {
  397. ( textWithBrackets
  398. | NonEmptyGroup<LongArg>
  399. | KnownEnvironment
  400. | Environment
  401. | BlankLine
  402. | "#" // macro character
  403. | "_" | "^" // other math chars
  404. )*
  405. }
  406. ShortTextArgument {
  407. OpenBrace ShortArg CloseBrace
  408. }
  409. ShortArg {
  410. ( textWithBrackets
  411. | NonEmptyGroup<ShortArg>
  412. | "#" // macro character
  413. | "_" | "^" // other math chars
  414. )*
  415. }
  416. ShortOptionalArg {
  417. ( textBase
  418. | NonEmptyGroup<ShortOptionalArg>
  419. | "#" // macro character
  420. )*
  421. }
  422. TikzPictureContent { /// same as Text but with added allowed characters
  423. ( textWithEnvironmentsAndBlankLines
  424. | NonEmptyGroup<TikzPictureContent>
  425. | "#" // macro character
  426. | "_" | "^" // other math chars
  427. )+
  428. }
  429. DefinitionFragment {
  430. ( KnownCommand
  431. | CtrlSeq optionalWhitespace?
  432. | CtrlSym
  433. | Begin
  434. | End
  435. | NonEmptyGroup<DefinitionFragment>
  436. | Dollar
  437. | OpenParenCtrlSym
  438. | CloseParenCtrlSym
  439. | OpenBracketCtrlSym
  440. | CloseBracketCtrlSym
  441. | KnownCtrlSym
  442. | BlankLine
  443. | NewLine
  444. | Normal
  445. | Whitespace
  446. | OpenBracket
  447. | CloseBracket
  448. | "#" // macro character
  449. | Ampersand // for tables
  450. | Tilde // unbreakable space
  451. | "_" | "^" // other math chars
  452. | SectioningCommand<
  453. BookCtrlSeq |
  454. PartCtrlSeq |
  455. ChapterCtrlSeq |
  456. SectionCtrlSeq |
  457. SubSectionCtrlSeq |
  458. SubSubSectionCtrlSeq |
  459. ParagraphCtrlSeq |
  460. SubParagraphCtrlSeq
  461. >
  462. )*
  463. }
  464. KnownEnvironment {
  465. ( DocumentEnvironment
  466. | TabularEnvironment
  467. | EquationEnvironment
  468. | EquationArrayEnvironment
  469. | VerbatimEnvironment
  470. | TikzPictureEnvironment
  471. | FigureEnvironment
  472. | ListEnvironment
  473. | TableEnvironment
  474. )
  475. }
  476. BeginEnv<name> {
  477. Begin
  478. EnvNameGroup<name>
  479. OptionalArgument?
  480. (!argument TextArgument)*
  481. }
  482. EndEnv<name> {
  483. End
  484. EnvNameGroup<name>
  485. }
  486. DocumentEnvironment[@isGroup="$Environment"] {
  487. BeginEnv<DocumentEnvName>
  488. Content<Text>
  489. EndEnv<DocumentEnvName>
  490. (TrailingWhitespaceOnly | TrailingContent)?
  491. }
  492. TabularContent {
  493. (textWithGroupsEnvironmentsAndBlankLines)*
  494. }
  495. TabularEnvironment[@isGroup="$Environment"] {
  496. BeginEnv<TabularEnvName>
  497. Content<TabularContent>
  498. EndEnv<TabularEnvName>
  499. }
  500. TableEnvironment[@isGroup="$Environment"] {
  501. BeginEnv<TableEnvName>
  502. Content<Text>
  503. EndEnv<TableEnvName>
  504. }
  505. EquationEnvironment[@isGroup="$Environment"] {
  506. BeginEnv<EquationEnvName>
  507. Content<Math?>
  508. EndEnv<EquationEnvName>
  509. }
  510. EquationArrayEnvironment[@isGroup="$Environment"] {
  511. BeginEnv<EquationArrayEnvName>
  512. Content<Math?>
  513. EndEnv<EquationArrayEnvName>
  514. }
  515. VerbatimEnvironment[@isGroup="$Environment"] {
  516. BeginEnv<VerbatimEnvName>
  517. Content<VerbatimContent>
  518. EndEnv<VerbatimEnvName>
  519. }
  520. TikzPictureEnvironment[@isGroup="$Environment"] {
  521. BeginEnv<TikzPictureEnvName>
  522. Content<TikzPictureContent>
  523. EndEnv<TikzPictureEnvName>
  524. }
  525. FigureEnvironment[@isGroup="$Environment"] {
  526. BeginEnv<FigureEnvName>
  527. Content<Text>
  528. EndEnv<FigureEnvName>
  529. }
  530. ListEnvironment[@isGroup="$Environment"] {
  531. BeginEnv<ListEnvName>
  532. Content<Text>
  533. EndEnv<ListEnvName>
  534. }
  535. EnvNameGroup<name> {
  536. OpenBrace name CloseBrace
  537. }
  538. Environment[@isGroup="$Environment"] {
  539. BeginEnv<EnvName?>
  540. Content<Text>
  541. EndEnv<EnvName?>
  542. }
  543. Group<GroupContent> {
  544. OpenBrace GroupContent? CloseBrace
  545. }
  546. NonEmptyGroup<GroupContent> {
  547. OpenBrace GroupContent CloseBrace
  548. }
  549. /// MATH MODE
  550. DollarMath {
  551. Dollar (InlineMath | DisplayMath) Dollar
  552. }
  553. InlineMath {
  554. Math
  555. }
  556. DisplayMath {
  557. Dollar Math? Dollar
  558. }
  559. OpenParenMath[closedBy=CloseParenMath] {
  560. OpenParenCtrlSym
  561. }
  562. CloseParenMath[openedBy=OpenParenMath] {
  563. CloseParenCtrlSym
  564. }
  565. // alternative syntax \( math \) for inline math, it is the same as $ math $
  566. ParenMath {
  567. OpenParenMath
  568. Math?
  569. CloseParenMath
  570. }
  571. OpenBracketMath[closedBy=CloseBracketMath] {
  572. OpenBracketCtrlSym
  573. }
  574. CloseBracketMath[openedBy=OpenBracketMath] {
  575. CloseBracketCtrlSym
  576. }
  577. // alternative syntax \[ math \] for display math, it is the same as $$ math $$
  578. BracketMath {
  579. OpenBracketMath
  580. Math?
  581. CloseBracketMath
  582. }
  583. // FIXME: we should have separate math modes for inline and display math,
  584. // because display math can contain blank lines while inline math cannot.
  585. Math {
  586. ( MathTextCommand
  587. | MathCommand
  588. | MathCtrlSym
  589. | MathGroup
  590. | MathDelimitedGroup
  591. | MathSpecialChar
  592. | Number
  593. | NewLine
  594. | KnownEnvironment
  595. | Environment
  596. | MathChar
  597. | OpenBracket
  598. | CloseBracket
  599. | Ampersand
  600. | Tilde
  601. | Label {
  602. LabelCtrlSeq optionalWhitespace? OptionalArgument? LabelArgument
  603. }
  604. | Ref {
  605. (RefCtrlSeq | RefStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? RefArgument
  606. }
  607. )+
  608. }
  609. MathTextCommand {
  610. (MathTextCtrlSeq | HboxCtrlSeq) optionalWhitespace? "*"? TextArgument
  611. }
  612. MathCommand { CtrlSeq }
  613. MathCtrlSym { CtrlSym | KnownCtrlSym }
  614. MathGroup {
  615. OpenBrace Math? CloseBrace
  616. }
  617. MathDelimitedGroup {
  618. MathOpening Math? MathClosing
  619. }
  620. // FIXME: we have the same problem with specialize on \left,\right as the delimiters
  621. MathOpening {
  622. LeftCtrlSeq optionalWhitespace? MathDelimiter
  623. }
  624. MathClosing {
  625. RightCtrlSeq optionalWhitespace? MathDelimiter
  626. }
  627. // NOTE: precedence works differently for rules and token, in the rule
  628. // you have to give a specifier !foo which is defined in the @precedence
  629. // block here.
  630. @precedence {
  631. section @left,
  632. argument @left // make CtrlSeq arguments left associative
  633. }