latex.grammar 20 KB

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