LaTeX.ebnf 1.57 KB
Newer Older
Eckhart Arnold's avatar
Eckhart Arnold committed
1
2
# latex Grammar

3
@ testing    = True
4
@ whitespace = /[ \t]*\n?(?!\s*\n)[ \t]*/  # whitespace, including at most one linefeed
5
@ comment    = /%.*(?:\n|$)/
Eckhart Arnold's avatar
Eckhart Arnold committed
6

7
8
9
latexdoc   = preamble document
preamble   = { command }+

Eckhart Arnold's avatar
Eckhart Arnold committed
10
document   = [PARSEP] { [PARSEP] paragraph } §EOF
11

12
genericenv = beginenv sequence §endenv
13
14
beginenv   = "\begin" §( "{" NAME "}" )
endenv     = "\end" §( "{" ::NAME "}" )
Eckhart Arnold's avatar
Eckhart Arnold committed
15

16
command    = CMDNAME [ config ] block
17
config     = "[" cfgtext §"]"
Eckhart Arnold's avatar
Eckhart Arnold committed
18

19
sequence   = { partext | parblock }
Eckhart Arnold's avatar
Eckhart Arnold committed
20

21
parblock   = "{" paragraph §"}"
22
block      = "{" { text | block } §"}"
Eckhart Arnold's avatar
Eckhart Arnold committed
23

24
25
26
paragraph  = { partext | parblock }

partext    = !blockcmd (text | PARSEP)
27
text       = cfgtext | BRACKETS
28
cfgtext    = word_sequence | ESCAPED | WSPC
29
word_sequence = { TEXTCHUNK }+
30
31

blockcmd   = "\subsection" | "\section" | "\chapter" | "\subsubsection"
32
             | "\paragraph" | "\subparagraph" | "\begin{enumerate}"
33
             | "\begin{itemize}" | "\item" | "\begin{figure}"
34
35
36

CMDNAME    = /\\\w+/~
NAME       = /\w+/~
Eckhart Arnold's avatar
Eckhart Arnold committed
37

Eckhart Arnold's avatar
Eckhart Arnold committed
38
ESCAPED    = /\\[%$&]/
39
BRACKETS   = /[\[\]]/                   # left or right square bracket: [ ]
Eckhart Arnold's avatar
Eckhart Arnold committed
40
TEXTCHUNK  = /[^\\%$&\{\}\[\]\s\n]+/    # some piece of text excluding whitespace,
41
                                        # linefeed and special characters
42
WSPC       = /[ \t]*\n?(?![ \t]*\n)[ \t]*/ # whitespace, including at most one linefeed
43
LF         = /[ \t]*\n(?!\s*\n)/        # a linefeed, but not an empty line (i.e. par)
44
PARSEP     = /\n[ \t]*(?=\n)/~          # at least one empty line, i.e.
45
                                        # [whitespace] linefeed [whitespace] linefeed
46

47
EOF        = !/./