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

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

syntax     =  [~//] { definition | directive } §EOF
definition =  symbol §"=" expression
directive  =  "@" §symbol §"=" ( regexp | literal | list_ )

expression =  term { "|" term }
12
term       =  { factor }+
13
factor     =  [flowmarker] [retrieveop] symbol !"="   # negative lookahead to be sure it's not a definition
Eckhart Arnold's avatar
Eckhart Arnold committed
14
15
16
            | [flowmarker] literal
            | [flowmarker] regexp
            | [flowmarker] group
17
            | [flowmarker] regexchain
Eckhart Arnold's avatar
Eckhart Arnold committed
18
19
20
21
22
23
24
25
26
            | [flowmarker] oneormore
            | repetition
            | option

flowmarker =  "!"  | "&"  | "§" |                # '!' negative lookahead, '&' positive lookahead, '§' required
              "-!" | "-&"                        # '-' negative lookbehind, '-&' positive lookbehind
retrieveop =  "::" | ":"                         # '::' pop, ':' retrieve

group      =  "(" expression §")"
27
regexchain =  "<" expression §">"                # compiles "expression" into a singular regular expression
28
oneormore  =  "{" expression "}+"
Eckhart Arnold's avatar
Eckhart Arnold committed
29
repetition =  "{" expression §"}"
30
option     =  "[" expression §"]"
Eckhart Arnold's avatar
Eckhart Arnold committed
31

Eckhart Arnold's avatar
Eckhart Arnold committed
32
link       = regexp | symbol | literal           # semantic restriction: symbol must evaluate to a regexp or chain
33

Eckhart Arnold's avatar
Eckhart Arnold committed
34
35
symbol     =  /(?!\d)\w+/~                       # e.g. expression, factor, parameter_list
literal    =  /"(?:[^"]|\\")*?"/~                # e.g. "(", '+', 'while'
36
            | /'(?:[^']|\\')*?'/~                # whitespace following literals will be ignored tacitly.
Eckhart Arnold's avatar
Eckhart Arnold committed
37
38
39
40
regexp     =  /~?\/(?:[^\/]|(?<=\\)\/)*\/~?/~    # e.g. /\w+/, ~/#.*(?:\n|$)/~
                                                 # '~' is a whitespace-marker, if present leading or trailing
                                                 # whitespace of a regular expression will be ignored tacitly.
list_      =  /\w+\s*(?:,\s*\w+\s*)*/~           # comma separated list of symbols, e.g. BEGIN_LIST, END_LIST,
41
                                                 # BEGIN_QUOTE, END_QUOTE ; see CommonMark/markdown.py for an exmaple
Eckhart Arnold's avatar
Eckhart Arnold committed
42
EOF =  !/./