latex.grammar 20 KB

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