latex.grammar 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. | "_" | "^" // other math chars
  477. )*
  478. }
  479. ShortTextArgument {
  480. OpenBrace ShortArg CloseBrace
  481. }
  482. ShortArg {
  483. ( textWithBrackets
  484. | NonEmptyGroup<ShortArg>
  485. | "#" // macro character
  486. | "_" | "^" // other math chars
  487. )*
  488. }
  489. ShortOptionalArg {
  490. ( textBase
  491. | NonEmptyGroup<ShortOptionalArg>
  492. | "#" // macro character
  493. | "_" // underscore is used in some parameter names
  494. )*
  495. }
  496. TikzPictureContent { /// same as Text but with added allowed characters
  497. ( textWithEnvironmentsAndBlankLines
  498. | NonEmptyGroup<TikzPictureContent>
  499. | "#" // macro character
  500. | "_" | "^" // other math chars
  501. )+
  502. }
  503. DefinitionFragment {
  504. ( DefinitionFragmentCommand
  505. | Begin
  506. | End
  507. | Group<DefinitionFragment>
  508. | Dollar
  509. | OpenParenCtrlSym
  510. | CloseParenCtrlSym
  511. | OpenBracketCtrlSym
  512. | CloseBracketCtrlSym
  513. | LeftCtrlSeq
  514. | RightCtrlSeq
  515. | BlankLine
  516. | NewLine
  517. | Normal
  518. | Whitespace
  519. | OpenBracket
  520. | CloseBracket
  521. | "#" // macro character
  522. | Ampersand // for tables
  523. | Tilde // unbreakable space
  524. | "_" | "^" // other math chars
  525. | SectioningCommand<
  526. BookCtrlSeq |
  527. PartCtrlSeq |
  528. ChapterCtrlSeq |
  529. SectionCtrlSeq |
  530. SubSectionCtrlSeq |
  531. SubSubSectionCtrlSeq |
  532. ParagraphCtrlSeq |
  533. SubParagraphCtrlSeq
  534. >
  535. )+
  536. }
  537. DefinitionFragmentArgument {
  538. OpenBrace DefinitionFragment? CloseBrace
  539. }
  540. DefinitionFragmentCommand {
  541. KnownCommand<TextArgument>
  542. | DefinitionFragmentUnknownCommand { genericUnknownCommandWithOptionalArguments<DefinitionFragmentArgument, OptionalArgument> }
  543. | KnownCtrlSym
  544. }
  545. KnownEnvironment {
  546. ( DocumentEnvironment
  547. | TabularEnvironment
  548. | EquationEnvironment
  549. | EquationArrayEnvironment
  550. | VerbatimEnvironment
  551. | TikzPictureEnvironment
  552. | FigureEnvironment
  553. | ListEnvironment
  554. | TableEnvironment
  555. )
  556. }
  557. BeginEnv<name> {
  558. Begin
  559. EnvNameGroup<name>
  560. OptionalArgument?
  561. (!argument TextArgument)*
  562. }
  563. EndEnv<name> {
  564. End
  565. EnvNameGroup<name>
  566. }
  567. DocumentEnvironment[@isGroup="$Environment"] {
  568. BeginEnv<DocumentEnvName>
  569. Content<Text>
  570. EndEnv<DocumentEnvName>
  571. (TrailingWhitespaceOnly | TrailingContent)?
  572. }
  573. TabularContent {
  574. (textWithGroupsEnvironmentsAndBlankLines)*
  575. }
  576. TabularEnvironment[@isGroup="$Environment"] {
  577. BeginEnv<TabularEnvName>
  578. Content<TabularContent>
  579. EndEnv<TabularEnvName>
  580. }
  581. TableEnvironment[@isGroup="$Environment"] {
  582. BeginEnv<TableEnvName>
  583. Content<Text>
  584. EndEnv<TableEnvName>
  585. }
  586. EquationEnvironment[@isGroup="$Environment"] {
  587. BeginEnv<EquationEnvName>
  588. Content<Math?>
  589. EndEnv<EquationEnvName>
  590. }
  591. EquationArrayEnvironment[@isGroup="$Environment"] {
  592. BeginEnv<EquationArrayEnvName>
  593. Content<Math?>
  594. EndEnv<EquationArrayEnvName>
  595. }
  596. VerbatimEnvironment[@isGroup="$Environment"] {
  597. BeginEnv<VerbatimEnvName>
  598. Content<VerbatimContent>
  599. EndEnv<VerbatimEnvName>
  600. }
  601. TikzPictureEnvironment[@isGroup="$Environment"] {
  602. BeginEnv<TikzPictureEnvName>
  603. Content<TikzPictureContent>
  604. EndEnv<TikzPictureEnvName>
  605. }
  606. FigureEnvironment[@isGroup="$Environment"] {
  607. BeginEnv<FigureEnvName>
  608. Content<Text>
  609. EndEnv<FigureEnvName>
  610. }
  611. ListEnvironment[@isGroup="$Environment"] {
  612. BeginEnv<ListEnvName>
  613. Content<Text>
  614. EndEnv<ListEnvName>
  615. }
  616. EnvNameGroup<name> {
  617. OpenBrace name CloseBrace
  618. }
  619. Environment[@isGroup="$Environment"] {
  620. BeginEnv<EnvName?>
  621. Content<Text>
  622. EndEnv<EnvName?>
  623. }
  624. Group<GroupContent> {
  625. OpenBrace GroupContent? CloseBrace
  626. }
  627. NonEmptyGroup<GroupContent> {
  628. OpenBrace GroupContent CloseBrace
  629. }
  630. /// MATH MODE
  631. DollarMath[@isGroup="$MathContainer"] {
  632. Dollar (InlineMath | DisplayMath) Dollar
  633. }
  634. InlineMath {
  635. Math
  636. }
  637. DisplayMath {
  638. Dollar Math? Dollar
  639. }
  640. OpenParenMath[closedBy=CloseParenMath] {
  641. OpenParenCtrlSym
  642. }
  643. CloseParenMath[openedBy=OpenParenMath] {
  644. CloseParenCtrlSym
  645. }
  646. // alternative syntax \( math \) for inline math, it is the same as $ math $
  647. ParenMath[@isGroup="$MathContainer"] {
  648. OpenParenMath
  649. Math?
  650. CloseParenMath
  651. }
  652. OpenBracketMath[closedBy=CloseBracketMath] {
  653. OpenBracketCtrlSym
  654. }
  655. CloseBracketMath[openedBy=OpenBracketMath] {
  656. CloseBracketCtrlSym
  657. }
  658. // alternative syntax \[ math \] for display math, it is the same as $$ math $$
  659. BracketMath[@isGroup="$MathContainer"] {
  660. OpenBracketMath
  661. Math?
  662. CloseBracketMath
  663. }
  664. // FIXME: we should have separate math modes for inline and display math,
  665. // because display math can contain blank lines while inline math cannot.
  666. Math {
  667. ( MathCommand
  668. | Group<Math>
  669. | MathDelimitedGroup
  670. | MathSpecialChar
  671. | Number
  672. | NewLine
  673. | Whitespace
  674. | KnownEnvironment
  675. | Environment
  676. | MathChar
  677. | OpenBracket
  678. | CloseBracket
  679. | Ampersand
  680. | Tilde
  681. )+
  682. }
  683. MathCommand {
  684. KnownCommand<MathArgument>
  685. | MathUnknownCommand { genericUnknownCommand<MathArgument> }
  686. | KnownCtrlSym
  687. }
  688. @external tokens argumentListTokenizer from "./tokens.mjs" {
  689. hasMoreArguments,
  690. endOfArguments
  691. }
  692. @external tokens argumentListWithOptionalTokenizer from "./tokens.mjs" {
  693. hasMoreArgumentsOrOptionals,
  694. endOfArgumentsAndOptionals
  695. }
  696. genericUnknownCommand<ArgumentType> {
  697. CtrlSeq (hasMoreArguments optionalWhitespace? ArgumentType)* endOfArguments
  698. | CtrlSym
  699. }
  700. genericUnknownCommandWithOptionalArguments<ArgumentType, OptionalArgumentType> {
  701. CtrlSeq (hasMoreArgumentsOrOptionals optionalWhitespace? (ArgumentType | OptionalArgumentType))* endOfArgumentsAndOptionals
  702. | CtrlSym
  703. }
  704. MathArgument {
  705. OpenBrace Math? CloseBrace
  706. }
  707. MathDelimitedGroup {
  708. MathOpening Math? MathClosing
  709. }
  710. // FIXME: we have the same problem with specialize on \left,\right as the delimiters
  711. MathOpening {
  712. LeftCtrlSeq optionalWhitespace? MathDelimiter
  713. }
  714. MathClosing {
  715. RightCtrlSeq optionalWhitespace? MathDelimiter
  716. }
  717. MathDelimiter {
  718. // Allowed delimiters, from the LaTeX manual, table 3.10
  719. "/" | "|" | "(" | ")" | "[" | "]" |
  720. "\\{" | "\\}" | "\\|" |
  721. "\\lfloor" | "\\rfloor" |
  722. "\\lceil" | "\\rceil" |
  723. "\\langle" | "\\rangle" |
  724. "\\backslash" | "\\uparrow" |
  725. "\\Uparrow" | "\\Downarrow" |
  726. "\\updownarrow" | "\\Updownarrow" |
  727. "\\downarrow" | "\\lvert" |
  728. "\\lVert" | "\\rVert" |
  729. "\\rvert" | "\\vert" | "\\Vert" |
  730. "\\lbrace" | "\\rbrace" |
  731. "\\lbrack" | "\\rbrack" |
  732. // Also allow the empty match
  733. "."
  734. }
  735. // NOTE: precedence works differently for rules and token, in the rule
  736. // you have to give a specifier !foo which is defined in the @precedence
  737. // block here.
  738. @precedence {
  739. section @left,
  740. argument @left // make CtrlSeq arguments left associative
  741. }