2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

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

3
@ testing    = True
4
@ whitespace = /[ \t]*(?:\n(?![ \t]*\n)[ \t]*)?/    # optional 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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
document   = [PARSEP] "\begin{document}" [PARSEP] frontpages [PARSEP] (chapters | sections)
             [bibliography] [index] "\end{document}" §EOF
frontpages = sequence

#######################################################################
#
# document structure
#
#######################################################################

Chapters   = { Chapter [PARSEP] }+
Chapter    = "\Chapter" block [PARSEP] { sequence | Sections }

Sections   = { Section [PARSEP] }+
Section    = "\Section" block [PARSEP] { sequence | SubSections }

SubSections = { SubSection [PARSEP] }+
SubSection  = "\SubSection" block [PARSEP] { sequence | SubSubSections }

SubSubsections = { SubSubSection [PARSEP] }+
SubSubSection  = "\SubSubSection" block [PARSEP] { sequence | Paragraphs }

Paragraphs = { Paragraph [PARSEP] }+
Paragraph  = "\paragraph" block [PARSEP] { sequence | SubParagraphs }

SubParagraphs = { SubParagraph [PARSEP] }+
SubParagraph  = "\subparagpaph" block [PARSEP] { sequence }

bibliography = "\bibliography" block [PARSEP]
index      = "\printindex" [PARSEP


#######################################################################
#
# document content
#
#######################################################################
47

48
blockenv   = beginenv sequence §endenv
Eckhart Arnold's avatar
Eckhart Arnold committed
49
parblock   = /{/ sequence §/}/
Eckhart Arnold's avatar
Eckhart Arnold committed
50

Eckhart Arnold's avatar
Eckhart Arnold committed
51
sequence   = { (paragraph | itemize | enumeration | figure | table | blockenv ) [PARSEP] }+
Eckhart Arnold's avatar
Eckhart Arnold committed
52

Eckhart Arnold's avatar
Eckhart Arnold committed
53
54
paragraph  = { !blockcmd textelements //~ }+
textelemts = command | text | block | inlinemath | inlineenv
Eckhart Arnold's avatar
Eckhart Arnold committed
55

Eckhart Arnold's avatar
Eckhart Arnold committed
56
inlineenv  = beginenv { textelements }+ endenv
57
58
59
beginenv   = "\begin{" §NAME §"}"
endenv     = "\end{" §::NAME §"}"

Eckhart Arnold's avatar
Eckhart Arnold committed
60
61
62
command    = specialcmd | plaincmd
specialcmd = "\footnote" parblock
plaincmd   = CMDNAME [[ //~ config ] //~ block ]
63
config     = "[" cfgtext §"]"
Eckhart Arnold's avatar
Eckhart Arnold committed
64
block      = /{/ { textelements } §/}/
Eckhart Arnold's avatar
Eckhart Arnold committed
65

66
67
68
text       = { cfgtext | (BRACKETS //~) }+
cfgtext    = { word_sequence | (ESCAPED //~) }+
word_sequence = { TEXTCHUNK //~ }+
69
70

blockcmd   = "\subsection" | "\section" | "\chapter" | "\subsubsection"
71
             | "\paragraph" | "\subparagraph" | "\begin{enumerate}"
72
             | "\begin{itemize}" | "\item" | "\begin{figure}"
73

74
CMDNAME    = /\\(?:(?!_)\w)+/~
75
NAME       = /\w+/~
Eckhart Arnold's avatar
Eckhart Arnold committed
76

77
ESCAPED    = /\\[%$&_\/]/
78
79
80
81
82
83
84
BRACKETS   = /[\[\]]/                       # left or right square bracket: [ ]
TEXTCHUNK  = /[^\\%$&\{\}\[\]\s\n]+/        # some piece of text excluding whitespace,
                                            # linefeed and special characters
WSPC       = /[ \t]+/                       # (horizontal) whitespace
LF         = !PARSEP /[ \t]*\n[ \t]*/       # LF but not an empty line
PARSEP     = /[ \t]*(?:\n[ \t]*)+\n[ \t]*/  # at least one empty line, i.e.
                                            # [whitespace] linefeed [whitespace] linefeed
85

86
EOF        = !/./