latex.grammar 20 KB

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