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. IncludeSvgCtrlSeq,
  52. CaptionCtrlSeq,
  53. DefCtrlSeq,
  54. LetCtrlSeq,
  55. LeftCtrlSeq,
  56. RightCtrlSeq,
  57. NewCommandCtrlSeq,
  58. RenewCommandCtrlSeq,
  59. NewEnvironmentCtrlSeq,
  60. RenewEnvironmentCtrlSeq,
  61. // services/web/frontend/js/features/outline/outline-parser.js
  62. BookCtrlSeq,
  63. PartCtrlSeq,
  64. ChapterCtrlSeq,
  65. SectionCtrlSeq,
  66. SubSectionCtrlSeq,
  67. SubSubSectionCtrlSeq,
  68. ParagraphCtrlSeq,
  69. SubParagraphCtrlSeq,
  70. InputCtrlSeq,
  71. IncludeCtrlSeq,
  72. SubfileCtrlSeq,
  73. ItemCtrlSeq,
  74. NewTheoremCtrlSeq,
  75. TheoremStyleCtrlSeq,
  76. CenteringCtrlSeq,
  77. BibliographyCtrlSeq,
  78. BibliographyStyleCtrlSeq,
  79. AuthorCtrlSeq,
  80. AffilCtrlSeq,
  81. AffiliationCtrlSeq,
  82. DateCtrlSeq,
  83. MaketitleCtrlSeq,
  84. TextColorCtrlSeq,
  85. ColorBoxCtrlSeq,
  86. HLineCtrlSeq,
  87. TopRuleCtrlSeq,
  88. MidRuleCtrlSeq,
  89. BottomRuleCtrlSeq,
  90. MultiColumnCtrlSeq,
  91. ParBoxCtrlSeq,
  92. TextBoldCtrlSeq,
  93. TextItalicCtrlSeq,
  94. TextSmallCapsCtrlSeq,
  95. TextTeletypeCtrlSeq,
  96. TextMediumCtrlSeq,
  97. TextSansSerifCtrlSeq,
  98. TextSuperscriptCtrlSeq,
  99. TextSubscriptCtrlSeq,
  100. TextStrikeOutCtrlSeq,
  101. EmphasisCtrlSeq,
  102. UnderlineCtrlSeq,
  103. SetLengthCtrlSeq,
  104. FootnoteCtrlSeq,
  105. EndnoteCtrlSeq
  106. }
  107. @external specialize {EnvName} specializeEnvName from "./tokens.mjs" {
  108. DocumentEnvName,
  109. TabularEnvName,
  110. EquationEnvName,
  111. EquationArrayEnvName,
  112. VerbatimEnvName,
  113. TikzPictureEnvName,
  114. FigureEnvName,
  115. ListEnvName,
  116. TableEnvName
  117. }
  118. @external specialize {CtrlSym} specializeCtrlSym from "./tokens.mjs" {
  119. OpenParenCtrlSym,
  120. CloseParenCtrlSym,
  121. OpenBracketCtrlSym,
  122. CloseBracketCtrlSym,
  123. LineBreakCtrlSym
  124. }
  125. @tokens {
  126. CtrlSeq { "\\" $[a-zA-Z]+ }
  127. CtrlSym { "\\" ![a-zA-Z] }
  128. // tokens for paragraphs
  129. Whitespace { $[ \t]+ }
  130. NewLine { "\n" }
  131. BlankLine { "\n" "\n"+ }
  132. Normal { ![\\{}\[\]$&~#% \t\n] ![\\{}\[\]$&~#%\t\n]* } // everything is normal text, except these characters
  133. @precedence { CtrlSeq, CtrlSym, BlankLine, NewLine, Whitespace, Normal }
  134. OpenBrace[closedBy=CloseBrace] { "{" }
  135. CloseBrace[openedBy=OpenBrace] { "}" }
  136. OpenBracket[closedBy=CloseBracket] { "[" }
  137. CloseBracket[openedBy=OpenBracket] { "]" }
  138. Comment { "%" ![\n]* "\n"? }
  139. Dollar { "$" }
  140. Number { $[0-9]+ ("." $[0-9]*)? }
  141. MathSpecialChar { $[^_=<>()\-+/*]+ } // FIXME not all of these are special
  142. MathChar { ![0-9^_=<>()\-+/*\\{}\[\]$%&~ \t\n]+ }
  143. @precedence { Number, MathSpecialChar, MathChar }
  144. Ampersand { "&" }
  145. Tilde { "~" }
  146. EnvName { $[a-zA-Z]+ $[*]? }
  147. }
  148. @top LaTeX {
  149. Text
  150. }
  151. @skip { Comment }
  152. // TEXT MODE
  153. optionalWhitespace {
  154. !argument Whitespace
  155. }
  156. OptionalArgument {
  157. !argument OpenBracket ShortOptionalArg CloseBracket
  158. }
  159. TextArgument {
  160. !argument OpenBrace LongArg CloseBrace
  161. }
  162. SectioningArgument {
  163. !argument OpenBrace LongArg CloseBrace
  164. }
  165. LabelArgument {
  166. !argument ShortTextArgument
  167. }
  168. RefArgument {
  169. !argument ShortTextArgument
  170. }
  171. BibKeyArgument {
  172. !argument ShortTextArgument
  173. }
  174. PackageArgument {
  175. !argument ShortTextArgument
  176. }
  177. TabularArgument {
  178. !argument OpenBrace TabularContent CloseBrace
  179. }
  180. UrlArgument {
  181. OpenBrace LiteralArgContent CloseBrace
  182. }
  183. FilePathArgument {
  184. OpenBrace LiteralArgContent CloseBrace
  185. }
  186. BareFilePathArgument {
  187. Whitespace SpaceDelimitedLiteralArgContent
  188. }
  189. DefinitionArgument {
  190. !argument NewLine? Whitespace* OpenBrace DefinitionFragment? CloseBrace
  191. }
  192. MacroParameter {
  193. "#" ("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")
  194. }
  195. OptionalMacroParameter {
  196. OpenBracket MacroParameter CloseBracket
  197. }
  198. // The autocompletion code in services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts
  199. // depends on following the `KnownCommand { Command { CommandCtrlSeq [args] } }`
  200. // structure
  201. KnownCommand<ArgumentType> {
  202. Title {
  203. TitleCtrlSeq optionalWhitespace? OptionalArgument? TextArgument
  204. } |
  205. Author {
  206. AuthorCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  207. } |
  208. Affil {
  209. AffilCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  210. } |
  211. Affiliation {
  212. AffiliationCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  213. } |
  214. Date {
  215. DateCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? ShortTextArgument
  216. } |
  217. DocumentClass {
  218. DocumentClassCtrlSeq optionalWhitespace? OptionalArgument?
  219. DocumentClassArgument { ShortTextArgument }
  220. } |
  221. BibliographyCommand {
  222. BibliographyCtrlSeq optionalWhitespace?
  223. BibliographyArgument { ShortTextArgument }
  224. } |
  225. BibliographyStyleCommand {
  226. BibliographyStyleCtrlSeq optionalWhitespace?
  227. BibliographyStyleArgument { ShortTextArgument }
  228. } |
  229. UsePackage {
  230. UsePackageCtrlSeq optionalWhitespace? OptionalArgument?
  231. PackageArgument
  232. } |
  233. TextColorCommand {
  234. TextColorCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? ArgumentType
  235. } |
  236. ColorBoxCommand {
  237. ColorBoxCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? ArgumentType
  238. } |
  239. HrefCommand {
  240. HrefCtrlSeq optionalWhitespace? UrlArgument ShortTextArgument
  241. } |
  242. NewTheoremCommand {
  243. NewTheoremCtrlSeq "*"? optionalWhitespace? ShortTextArgument ((OptionalArgument? TextArgument) | (TextArgument OptionalArgument))
  244. } |
  245. TheoremStyleCommand {
  246. TheoremStyleCtrlSeq optionalWhitespace? ShortTextArgument
  247. } |
  248. UrlCommand {
  249. UrlCtrlSeq optionalWhitespace? UrlArgument
  250. } |
  251. VerbCommand {
  252. VerbCtrlSeq VerbContent
  253. } |
  254. LstInlineCommand {
  255. LstInlineCtrlSeq optionalWhitespace? OptionalArgument? LstInlineContent
  256. } |
  257. IncludeGraphics {
  258. IncludeGraphicsCtrlSeq optionalWhitespace? OptionalArgument?
  259. IncludeGraphicsArgument { FilePathArgument }
  260. } |
  261. IncludeSvg {
  262. IncludeSvgCtrlSeq optionalWhitespace? OptionalArgument?
  263. IncludeSvgArgument { FilePathArgument }
  264. } |
  265. Caption {
  266. CaptionCtrlSeq "*"? optionalWhitespace? OptionalArgument? TextArgument
  267. } |
  268. Label {
  269. LabelCtrlSeq optionalWhitespace? LabelArgument
  270. } |
  271. Ref {
  272. (RefCtrlSeq | RefStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? RefArgument
  273. } |
  274. Cite {
  275. (CiteCtrlSeq | CiteStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? BibKeyArgument
  276. } |
  277. Def {
  278. // allow more general Csname argument to \def commands, since other symbols such as '@' are often used in definitions
  279. DefCtrlSeq optionalWhitespace? (Csname | CtrlSym) optionalWhitespace? (MacroParameter | OptionalMacroParameter)* optionalWhitespace? DefinitionArgument
  280. } |
  281. Let {
  282. LetCtrlSeq Csname optionalWhitespace? "="? optionalWhitespace? Csname
  283. } |
  284. Hbox {
  285. HboxCtrlSeq optionalWhitespace? TextArgument
  286. } |
  287. NewCommand {
  288. NewCommandCtrlSeq optionalWhitespace?
  289. (Csname | OpenBrace LiteralArgContent CloseBrace)
  290. (OptionalArgument)*
  291. DefinitionArgument
  292. } |
  293. RenewCommand {
  294. RenewCommandCtrlSeq optionalWhitespace?
  295. (Csname | OpenBrace LiteralArgContent CloseBrace)
  296. (OptionalArgument)*
  297. DefinitionArgument
  298. } |
  299. NewEnvironment {
  300. NewEnvironmentCtrlSeq optionalWhitespace?
  301. (OpenBrace LiteralArgContent CloseBrace)
  302. (OptionalArgument)*
  303. DefinitionArgument
  304. DefinitionArgument
  305. } |
  306. RenewEnvironment {
  307. RenewEnvironmentCtrlSeq optionalWhitespace?
  308. (Csname | OpenBrace LiteralArgContent CloseBrace)
  309. (OptionalArgument)*
  310. DefinitionArgument
  311. DefinitionArgument
  312. } |
  313. Input {
  314. InputCtrlSeq InputArgument { ( FilePathArgument | BareFilePathArgument ) }
  315. } |
  316. Include {
  317. IncludeCtrlSeq IncludeArgument { FilePathArgument }
  318. } |
  319. Subfile {
  320. SubfileCtrlSeq SubfileArgument { FilePathArgument }
  321. } |
  322. Centering {
  323. CenteringCtrlSeq
  324. } |
  325. Item {
  326. ItemCtrlSeq OptionalArgument? optionalWhitespace?
  327. } |
  328. Maketitle {
  329. MaketitleCtrlSeq optionalWhitespace?
  330. } |
  331. HorizontalLine {
  332. (HLineCtrlSeq | TopRuleCtrlSeq | MidRuleCtrlSeq | BottomRuleCtrlSeq) optionalWhitespace?
  333. } |
  334. MultiColumn {
  335. MultiColumnCtrlSeq
  336. optionalWhitespace? SpanArgument { ShortTextArgument }
  337. optionalWhitespace? ColumnArgument { ShortTextArgument }
  338. optionalWhitespace? TabularArgument
  339. } |
  340. MathTextCommand {
  341. MathTextCtrlSeq optionalWhitespace? "*"? TextArgument
  342. } |
  343. ParBoxCommand {
  344. ParBoxCtrlSeq
  345. (optionalWhitespace? OptionalArgument)*
  346. ShortTextArgument
  347. optionalWhitespace? TextArgument
  348. } |
  349. TextBoldCommand {
  350. TextBoldCtrlSeq TextArgument
  351. } |
  352. TextItalicCommand {
  353. TextItalicCtrlSeq TextArgument
  354. } |
  355. TextSmallCapsCommand {
  356. TextSmallCapsCtrlSeq TextArgument
  357. } |
  358. TextTeletypeCommand {
  359. TextTeletypeCtrlSeq TextArgument
  360. } |
  361. TextMediumCommand {
  362. TextMediumCtrlSeq TextArgument
  363. } |
  364. TextSansSerifCommand {
  365. TextSansSerifCtrlSeq TextArgument
  366. } |
  367. TextSuperscriptCommand {
  368. TextSuperscriptCtrlSeq TextArgument
  369. } |
  370. TextSubscriptCommand {
  371. TextSubscriptCtrlSeq TextArgument
  372. } |
  373. StrikeOutCommand {
  374. TextStrikeOutCtrlSeq ArgumentType
  375. } |
  376. EmphasisCommand {
  377. EmphasisCtrlSeq ArgumentType
  378. } |
  379. UnderlineCommand {
  380. UnderlineCtrlSeq ArgumentType
  381. } |
  382. SetLengthCommand {
  383. SetLengthCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? ShortTextArgument
  384. } |
  385. FootnoteCommand {
  386. FootnoteCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  387. } |
  388. EndnoteCommand {
  389. EndnoteCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  390. }
  391. }
  392. UnknownCommand {
  393. (CtrlSeq !argument Whitespace (OptionalArgument | TextArgument)+)
  394. | (CtrlSeq (OptionalArgument | TextArgument)+)
  395. | CtrlSeq Whitespace?
  396. | CtrlSym
  397. }
  398. Command {
  399. KnownCommand<TextArgument>
  400. | UnknownCommand
  401. | KnownCtrlSym
  402. // Not technically allowed in normal mode, but not worth failing the parse over
  403. | LeftCtrlSeq
  404. | RightCtrlSeq
  405. }
  406. KnownCtrlSym {
  407. LineBreak {
  408. LineBreakCtrlSym OptionalArgument?
  409. }
  410. }
  411. textBase {
  412. ( Command
  413. | DollarMath
  414. | BracketMath
  415. | ParenMath
  416. | NewLine
  417. | Normal
  418. | Whitespace
  419. | Ampersand
  420. | Tilde
  421. )
  422. }
  423. textWithBrackets {
  424. ( textBase
  425. | OpenBracket
  426. | CloseBracket
  427. )
  428. }
  429. textWithEnvironmentsAndBlankLines {
  430. ( BlankLine
  431. | KnownEnvironment
  432. | Environment
  433. | textWithBrackets
  434. )
  435. }
  436. textWithGroupsEnvironmentsAndBlankLines {
  437. textWithEnvironmentsAndBlankLines
  438. | Group<Text>
  439. }
  440. Content<Element> {
  441. Element
  442. }
  443. SectioningCommand<Command> {
  444. Command optionalWhitespace? "*"? optionalWhitespace? OptionalArgument? optionalWhitespace? SectioningArgument
  445. }
  446. documentSection<Command, Next> {
  447. SectioningCommand<Command> Content<(sectionText | !section Next)*>
  448. }
  449. Book[@isGroup="$Section"] { documentSection<BookCtrlSeq, Part | Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  450. Part[@isGroup="$Section"] { documentSection<PartCtrlSeq, Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  451. Chapter[@isGroup="$Section"] { documentSection<ChapterCtrlSeq, Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  452. Section[@isGroup="$Section"] { documentSection<SectionCtrlSeq, SubSection | SubSubSection | Paragraph | SubParagraph> }
  453. SubSection[@isGroup="$Section"] { documentSection<SubSectionCtrlSeq, SubSubSection | Paragraph | SubParagraph> }
  454. SubSubSection[@isGroup="$Section"] { documentSection<SubSubSectionCtrlSeq, Paragraph | SubParagraph> }
  455. Paragraph[@isGroup="$Section"] { documentSection<ParagraphCtrlSeq, SubParagraph> }
  456. SubParagraph[@isGroup="$Section"] { SectioningCommand<SubParagraphCtrlSeq> Content<sectionText*> }
  457. sectioningCommand {
  458. Book | Part | Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph
  459. }
  460. sectionText {
  461. !section (
  462. textWithGroupsEnvironmentsAndBlankLines
  463. )+
  464. }
  465. Text {
  466. ( sectionText
  467. | sectioningCommand)+
  468. }
  469. LongArg {
  470. ( textWithBrackets
  471. | NonEmptyGroup<LongArg>
  472. | KnownEnvironment
  473. | Environment
  474. | BlankLine
  475. | "#" // macro character
  476. )*
  477. }
  478. ShortTextArgument {
  479. OpenBrace ShortArg CloseBrace
  480. }
  481. ShortArg {
  482. ( textWithBrackets
  483. | NonEmptyGroup<ShortArg>
  484. | "#" // macro character
  485. )*
  486. }
  487. ShortOptionalArg {
  488. ( textBase
  489. | NonEmptyGroup<ShortOptionalArg>
  490. | "#" // macro character
  491. )*
  492. }
  493. TikzPictureContent { /// same as Text but with added allowed characters
  494. ( textWithEnvironmentsAndBlankLines
  495. | NonEmptyGroup<TikzPictureContent>
  496. | "#" // macro character
  497. )+
  498. }
  499. DefinitionFragment {
  500. ( DefinitionFragmentCommand
  501. | Begin
  502. | End
  503. | Group<DefinitionFragment>
  504. | Dollar
  505. | OpenParenCtrlSym
  506. | CloseParenCtrlSym
  507. | OpenBracketCtrlSym
  508. | CloseBracketCtrlSym
  509. | LeftCtrlSeq
  510. | RightCtrlSeq
  511. | BlankLine
  512. | NewLine
  513. | Normal
  514. | Whitespace
  515. | OpenBracket
  516. | CloseBracket
  517. | "#" // macro character
  518. | Ampersand // for tables
  519. | Tilde // unbreakable space
  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. }