Fixed and updated grammar

This commit is contained in:
Bruce Hill 2021-01-13 01:33:24 -08:00
parent b95f4087a8
commit 7e4b287d9b

View File

@ -1,23 +1,22 @@
# This is a file defining the BP grammar using BP syntax
Grammar: __ *(Def [__`;])%__ __ ($$ / @!=(..%\n => "Could not parse this code"))
Def: @name=id _ `: __ (
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 [`;])
String-pattern: ..$$ % (\n / Nodent / Escape / `\ pat [`;])
pat: simple-pat !(__("!="/"=="/"=>")) / suffixed-pat
pat: simple-pat !(__("!="/"==")) / suffixed-pat
simple-pat: Upto-and / Dot / String / Chars / Nodent / Escape-range
/ Escape / Repeat / Optional / After / Before / Capture
/ Escape / Repeat / Optional / No / After / Before / Capture
/ Ref / parens
suffixed-pat: (
Eq-pat
/ Not-eq-pat
/ Replace
)
Eq-pat: @first=pat__"=="__@second=pat
@ -28,7 +27,7 @@ String: (
`" @s=*(Escape / !`".) (`" / @!=(''=> "Expected closing quote here"))
/ `' @s=*(Escape / !`'.) (`' / @!=(''=> "Expected closing quote here"))
)
Chars: `` @+(Char/Char-range) % `,
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
@ -41,29 +40,29 @@ escape-sequence: (
/ 1-3 `0-7
/ `x 2 `0-9,a-f,A-F
)
No: `! (_@pat / @!=(''=>"Expected a pattern after the exclamation mark"))
No: `! (__@pat / @!=(''=>"Expected a pattern after the exclamation mark"))
Nodent: `\ `N
Upto-and: 2-3`. [_@first=simple-pat] [__`%__@second=simple-pat]
Upto-and: ".." [__@first=simple-pat] [__`%__@second=simple-pat]
Repeat: (
@min='' `* @max=''
/ @min=int _ `- _ @max=int
/ @min=int _ `+ @max=''
@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"))
After: `< __ pat
Before: `> __ pat
Capture: `@ [__ @capture-name=(id/`!) __ !"=>" `=] __ (@capture=pat / @!=(''=> "Expected pattern to capture"))
Replace: (
@replace-pat=pat __ "=>" (__ @replacement=String / @!=(''=> "Expected replacement string"))
@replace-pat=(Replace / Chain / pat) __ "=>" (__ @replacement=String / @!=(''=> "Expected replacement string"))
)
Ref: @name=id !(_`:)
Ref: @name=id !(__`:)
parens: `( __ extended-pat (__ `) / @!=(''=> "Expected closing parenthesis here"))
Chain: 2+@pat%__
Otherwise: 2+@(Chain/pat)%(__`/__)
extended-pat: Otherwise / Chain / pat
Chain: 2+@(pat !(__"=>") / Replace)%__
Otherwise: 2+@(Replace / Chain / pat)%(__`/__)
extended-pat: Otherwise / Replace / Chain / pat
# Special-symbol rules:
_: *(` / \t)