add first-class zero-or-more repetitions
[sbp.git] / tests / latex.g
1 // draft grammar for [La]TeX
2 // note that this is approximate; in reality TeX has no grammar
3
4 Text            = ()
5                 | Token
6                 | Text:: `Text                  Token
7                 | Text:: `Text (WS:: ws)        Token
8                 | Text:: `Text (BlankLine:: br) Token
9
10 Token           = Command
11                 | Comment::     "%" ~[\n]* "\n"
12                 | EscapedChar:: "\\" ~[a-zA-Z\\]
13                 | Environment:: "\\begin{" EnvironmentName "}" Text "\\end{" EnvironmentName! "}"
14                 | Word::        ~[\\\r\n\ %{}]++
15                 | LineBreak::   "\\\\"
16                 | "$"  (Math:: ~[$]+) "$"
17                 | "$$" (Math:: ~[$]+) "$$"
18                 | Verbatim
19                 | Table
20              // | WiX
21
22 Command::       = "\\" CommandName         ws              -> ~[{]
23                 | "\\" CommandName OptArgs ws "{" Text "}"
24 CommandName     = [A-Za-z0-9]++ &~ "begin" | "end"
25 OptArgs::       = ws ("[" Arg "]") +/ ws
26                 | ()
27
28 Table           = "\\begin{tabular}"
29                   TableRow +/ (ws "\\\\" ws)
30                   "\\end{tabular}"
31 TableRow        = TableCol +/ (ws "&" ws)
32 TableCol        = Text &~ ... "\\\\" ...
33
34 EnvironmentName = [A-Za-z0-9]++ &~ "verbatim" | "tabular"
35
36 Verbatim        = "\\begin{verbatim}"
37                   (Verb:: ... &~ ... "\\end{verbatim}")
38                   "\\end{verbatim}"
39
40 hws!            = [ \r]
41 ws!             = (hws* "\n")? hws*                   -> ~[ \n\r]
42 nl!             = [ \r]* "\n"  [ \r]*                 -> ~[ \n\r]
43 br!             = [ \r]* "\n"  [ \r]*  "\n" [ \r\n]*  -> ~[ \n\r]
44
45 // import wix.g as wix
46 // WiX       = "\\begin{wix}\n"
47 //             wix.Pars
48 //             "\n\\end{verbatim}"
49
50
51
52