code / bp

Lines4.3K C3.3K Markdown541 YAML273 make110 Shell77 Lua54
(92 lines)
1 # This is a file defining the BP grammar using BP syntax
2 #
3 # This is a complete definition of the grammar of BP grammar files, but it's
4 # mainly intended to be used as a proof-of-concept, and a stress-test for BP.
5 # The grammar files provided with BP are not otherwise intended to be full
6 # language grammars.
8 Grammar: __ *(Def [__`;])%__ __ [@error=(+(./\n) => "Could not parse this code: @0")]
9 Def: @name=id __ 1-2`: __ (
10 @definition=extended-pat
11 / $$ @error=(=>"No definition for rule")
12 / @error=(..%\n>(`;/id_`:/$) => "Invalid definition: @0"))
14 # This is used for command line arguments:
15 String-pattern: ..%(\n / Nodent / Identifier-char / Identifier-start / Escape / `\ pat [`;])$$
17 pat: simple-pat !(__("!~"/"~")) / suffixed-pat
18 simple-pat: (Upto-and / Dot / Word-boundary/ String / Chars / Nodent / Curdent
19 / Identifier-char / Identifier-start / Escape-range
20 / Escape / Repeat / Optional / No / After / Before / Capture
21 / Start-of-File / Start-of-Line / End-of-File / End-of-Line / Ref / parens)
23 suffixed-pat: (
24 Match-pat
25 / Not-match-pat
28 Match-pat: @first=(suffixed-pat / simple-pat)__"~"__@second=(pat / @error=(=>"Expected pattern after '~'"))
29 Not-match-pat: @first=(suffixed-pat / simple-pat)__"!~"__@second=(pat / @error=(=>"Expected pattern after '!~'"))
31 Dot: `. !`.
32 String: (
33 `" @s=.. (`" / $ @error=(=>"Expected closing quote here"))
34 / `' @s=.. (`' / $ @error=(=>"Expected closing quote here"))
36 Chars: `` @+(Char-range/Char) % `,
37 Char-range: @low=. `- (@high=. / @error=(=>"Expected a second character to form a character range"))
38 Char: (@s=. / @error=(=>"Expected a character following the '`'"))
39 Escape-range: `\ @low=escape-sequence `- @high=escape-sequence
40 Escape: `\ (@s=escape-sequence
41 / $ @error=(=>"Backslashes are used for escape sequences, not splitting lines")
42 / @error=(. *(Abc/`0-9) => "Invalid escape sequence: '@0'")
44 escape-sequence: (
45 `n,t,r,e,b,a,v
46 / 1-3 `0-7
47 / `x 2 `0-9,a-f,A-F
49 No: `! (__@pat / @error=(=>"Expected a pattern after the exclamation mark"))
50 Nodent: "\N"
51 Curdent: "\C"
52 Word-boundary: `| / "\b"
53 Identifier-char: "\i"
54 Identifier-start: "\I"
55 Upto-and: ".." [__(`%/`=)__@second=simple-pat] [__@first=simple-pat]
56 Repeat: (
57 @min=(=>'0') (`*=>"-") @max=(=>'∞')
58 / @min=int __ `- __ @max=int
59 / @min=(int / =>'1') __ (`+=>"-") @max=(=>'∞')
60 / @min=@max=int
61 ) __ @repeat-pat=pat [__`%__@sep=pat]
62 Optional: `[ __ extended-pat (__`] / @error=(=>"Expected closing square bracket here"))
63 After: `< __ pat
64 Before: `> __ pat
65 Capture: `@ [__ @capture-name=(id/`!) __ !"=>" `=,:] __ (@capture=pat / @error=(=>"Expected pattern to capture"))
66 Replace: (
67 @replace-pat=[Chain-noreplace / pat] __ "=>" (__ @replacement=String / @error=(=>"Expected replacement string"))
69 Replace-chain: Replace-chain __ "=>" (__ @replacement=String / @error=(=>"Expected replacement string"))
70 Empty-replacement: (
71 (=>"EMPTY") @replace-pat=(=>"''") "=>" (__ @replacement=String / @error=(=>"Expected replacement string"))
73 Ref: @name=id !(__`:)
74 Start-of-File: "^^"
75 Start-of-Line: "^"
76 End-of-File: "$$"
77 End-of-Line: "$"
79 parens: `( __ extended-pat (__ `) / @error=(=>"Expected closing parenthesis here"))
81 Chain: @(Replace/pat) __ @(Chain/Replace/pat)
82 Chain-noreplace: @pat __ @(Chain-noreplace/pat)
83 Otherwise: 2+@(Chain / Replace / pat)%(__`/__)
84 extended-pat: Otherwise / Chain / Replace / pat
86 # Special-symbol rules:
87 _: *(` / \t)
88 __: *(` / \t / \r / \n / comment)
90 id: "__" / "_" / `a-z,A-Z *`a-z,A-Z,0-9,-
92 comment: `# .. $