EBNF.ebnf 2.09 KB
Newer Older
Eckhart Arnold's avatar
Eckhart Arnold committed
1
2
# EBNF-Grammar in EBNF

3
4
5
@ comment    = /#.*(?:\n|$)/                    # comments start with '#' and eat all chars up to and including '\n'
@ whitespace = /\s*/                            # whitespace includes linefeed
@ literalws  = right                            # trailing whitespace of literals will be ignored tacitly
Eckhart Arnold's avatar
Eckhart Arnold committed
6

7
8
#: top-level

9
10
syntax     = [~//] { definition | directive } §EOF
definition = symbol §"=" expression
eckhart's avatar
eckhart committed
11
directive  = "@" §symbol "=" (regexp | literal | symbol) { "," (regexp | literal | symbol) }
Eckhart Arnold's avatar
Eckhart Arnold committed
12

13
14
#: components

15
16
17
18
19
20
21
22
23
24
25
26
expression = term { "|" term }
term       = { ["§"] factor }+                       # "§" means all following factors mandatory
factor     = [flowmarker] [retrieveop] symbol !"="   # negative lookahead to be sure it's not a definition
           | [flowmarker] literal
           | [flowmarker] plaintext
           | [flowmarker] regexp
           | [flowmarker] whitespace
           | [flowmarker] oneormore
           | [flowmarker] group
           | [flowmarker] unordered
           | repetition
           | option
Eckhart Arnold's avatar
Eckhart Arnold committed
27

28
29
#: flow-operators

30
31
32
flowmarker = "!"  | "&"                         # '!' negative lookahead, '&' positive lookahead
           | "-!" | "-&"                        # '-' negative lookbehind, '-&' positive lookbehind
retrieveop = "::" | ":"                         # '::' pop, ':' retrieve
Eckhart Arnold's avatar
Eckhart Arnold committed
33

34
35
#: groups

36
37
38
39
40
group      = "(" §expression ")"
unordered  = "<" §expression ">"                # elements of expression in arbitrary order
oneormore  = "{" expression "}+"
repetition = "{" §expression "}"
option     = "[" §expression "]"
Eckhart Arnold's avatar
Eckhart Arnold committed
41

42
43
#: leaf-elements

44
45
46
47
symbol     = /(?!\d)\w+/~                       # e.g. expression, factor, parameter_list
literal    = /"(?:[^"]|\\")*?"/~                # e.g. "(", '+', 'while'
           | /'(?:[^']|\\')*?'/~                # whitespace following literals will be ignored tacitly.
plaintext  = /`(?:[^"]|\\")*?`/~                # like literal but does not eat whitespace
48
49
regexp     = /\/(?:\\\/|[^\/])*?\//~            # e.g. /\w+/, ~/#.*(?:\n|$)/~
whitespace = /~/~                               # insignificant whitespace
eckhart's avatar
eckhart committed
50

51
EOF = !/./