bp/grammars/bpeg.bp

78 lines
2.5 KiB
Plaintext

# This is a file defining the BP grammar using BP syntax
Grammar: __ *(Def [__`;])%__ __ ($$ / @!=(..$$%\n => "Could not parse this code"))
Def: @name=id __ `: __ (
@definition=extended-pat
/ $$ @!=(''=>"No definition for rule")
/ @!=(..>(`;/id_`:/$)%\n => "Invalid definition: @0"))
# This is used for command line arguments:
String-pattern: ..$$ % (\n / Nodent / Escape / `\ pat [`;])
pat: simple-pat !(__("!="/"==")) / suffixed-pat
simple-pat: Upto-and / Dot / String / Chars / Nodent / Escape-range
/ Escape / Repeat / Optional / No / After / Before / Capture
/ Ref / parens
suffixed-pat: (
Eq-pat
/ Not-eq-pat
)
Eq-pat: @first=pat__"=="__@second=pat
Not-eq-pat: @first=pat__"!="__@second=pat
Dot: `. !`.
String: (
`" @s=*(Escape / !`".) (`" / @!=(''=> "Expected closing quote here"))
/ `' @s=*(Escape / !`'.) (`' / @!=(''=> "Expected closing quote here"))
)
Chars: `` @+(Char-range/Char) % `,
Char-range: @low=. `- (@high=. / @!=(''=> "Expected a second character to form a character range"))
Char: (@s=. / @!=(''=> "Expected a character following the '`'"))
Escape-range: `\ @low=escape-sequence `- @high=escape-sequence
Escape: `\ (@s=escape-sequence
/ $ @!=(''=>"Backslashes are used for escape sequences, not splitting lines")
/ @!=(. *(Abc/`0-9) => "Invalid escape sequence: '@0'")
)
escape-sequence: (
`n,t,r,e,b,a,v
/ 1-3 `0-7
/ `x 2 `0-9,a-f,A-F
)
No: `! (__@pat / @!=(''=>"Expected a pattern after the exclamation mark"))
Nodent: `\ `N
Upto-and: ".." [__@first=simple-pat] [__`%__@second=simple-pat]
Repeat: (
@min=(''=>'0') `* @max=''
/ @min=int __ `- __ @max=int
/ @min=(int / ''=>'1') __ `+ @max=''
/ @min=@max=int
) __ @repeat-pat=pat [__`%__@sep=pat]
Optional: `[ __ extended-pat (__`] / @!=(''=> "Expected closing square bracket here"))
After: `< __ pat
Before: `> __ pat
Capture: `@ [__ @capture-name=(id/`!) __ !"=>" `=] __ (@capture=pat / @!=(''=> "Expected pattern to capture"))
Replace: (
@replace-pat=(Replace / Chain / pat) __ "=>" (__ @replacement=String / @!=(''=> "Expected replacement string"))
)
Ref: @name=id !(__`:)
parens: `( __ extended-pat (__ `) / @!=(''=> "Expected closing parenthesis here"))
Chain: 2+@(pat !(__"=>") / Replace)%__
Otherwise: 2+@(Replace / Chain / pat)%(__`/__)
extended-pat: Otherwise / Replace / Chain / pat
# Special-symbol rules:
_: *(` / \t)
__: *(` / \t / \r / \n / comment)
$$: !(./\n)
$: !.
^^: !<(./\n)
^: !<.
id: "^^" / "^" / "__" / "_" / "$$" / "$" / "|" / `a-z,A-Z *`a-z,A-Z,0-9,-
comment: `# .. $