aboutsummaryrefslogtreecommitdiff
path: root/bpeg.bpeg
blob: ef082391e18a0ee5dffd5c57bcd3d6dda55dc162 (plain)
1
2
3
4
5
6
7
8
9
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This is a file defining the BPEG grammar using BPEG syntax

grammar;
grammar = __ @[mainPattern]extendedPat __ (*def % (__`;__)) ?(__ `;) __;
def = @[name]ref __ `= __ @[definition]extendedPat;

# This is used for command line arguments:
stringGrammar = *(`\ pat ?`; / .);

pat = empty / dot / string / charRange / char / escapeRange / escape / no / anythingBut
    / uptoAnd / repeat / after / before / capture / replace / ref / parens;

empty = `/ >(__ (`}/`}));
dot = `.;
string = (
        `" @[s]*(escape / ~`") `"
      / `' @[s]*(escape / ~`') `'
    );
charRange = `` @[low]. `- @[high].;
char = `` @[s].;
escapeRange = `\ @[low]escapeSequence `- @[high]escapeSequence;
escape = `\ @[s]escapeSequence;
escapeSequence = (
        1-3 `0-7
      / `x 2 (`0-9/`a-f/`A-F)
      /`a/`b/`e/`n/`r/`t/`v / . / \n
    );
no = `! _ @pat;
anythingBut = `~ _ @pat;
uptoAnd = `& _ @pat;
repeat = (
           @[min]int _ `- _ @[max]int
      /    @[min]{->"0"}    @[max]int _ `-
      /    @[min]int _ `+   @[max](/)
      /    @[min]           @[max]int
      / `+ @[min]{->"1"}    @[max](/)
      / `* @[min]{->"0"}    @[max](/)
      / `? @[min]{->"0"}    @[max]{->"1"}
    ) _ @[repeatPat]pat ?( __ `% __ @[sep]pat);
after = `< _ pat;
before = `> _ pat;
capture = `@ ?(_ `[ @[captureName]ref `]) _ @[capture]pat;
replace = `{ __ (
      ?(@[replacePat]extendedPat __) "=>" ?(__ @[replacement]string)
    ) __ `};
ref = @[name](
        "^^" / "^" / "__" / "_" / "$$" / "$" /
        (`a-z/`A-Z) *(`a-z/`A-Z/`0-9));

parens = `( __ extendedPat __ `);

chain = +@pat % (__);
otherwise = +@(chain/pat) % (__`/__);
extendedPat = otherwise / chain / pat;

_ = *(`  / \t);
__ = *(`  / \t / \r / \n / comment);
hashComment = `# *.;

# Note: comments are undefined by default in regular BPEG
comment = hashComment;