latex.grammar 20 KB

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