Add ranges to EBNF syntax
This works: <EBNF letter = [a-zA-Z] EBNF> and <EBNF not-digit = [^0-9] EBNF>db4
parent
264284d0c4
commit
795ef0ae3b
|
@ -119,3 +119,27 @@ IN: peg.ebnf.tests
|
||||||
{ V{ 1 2 } } [
|
{ V{ 1 2 } } [
|
||||||
"foo=('a') [[ drop 1 ]] ('b') [[ drop 2 ]]" ebnf>quot with-compilation-unit "ab" foo parse parse-result-ast
|
"foo=('a') [[ drop 1 ]] ('b') [[ drop 2 ]]" ebnf>quot with-compilation-unit "ab" foo parse parse-result-ast
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
{ CHAR: A } [
|
||||||
|
"foo=[A-Z]" ebnf>quot with-compilation-unit "A" foo parse parse-result-ast
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ CHAR: Z } [
|
||||||
|
"foo=[A-Z]" ebnf>quot with-compilation-unit "Z" foo parse parse-result-ast
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ f } [
|
||||||
|
"foo=[A-Z]" ebnf>quot with-compilation-unit "0" foo parse
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ CHAR: 0 } [
|
||||||
|
"foo=[^A-Z]" ebnf>quot with-compilation-unit "0" foo parse parse-result-ast
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ f } [
|
||||||
|
"foo=[^A-Z]" ebnf>quot with-compilation-unit "A" foo parse
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ f } [
|
||||||
|
"foo=[^A-Z]" ebnf>quot with-compilation-unit "Z" foo parse
|
||||||
|
] unit-test
|
|
@ -9,6 +9,7 @@ IN: peg.ebnf
|
||||||
TUPLE: ebnf-non-terminal symbol ;
|
TUPLE: ebnf-non-terminal symbol ;
|
||||||
TUPLE: ebnf-terminal symbol ;
|
TUPLE: ebnf-terminal symbol ;
|
||||||
TUPLE: ebnf-any-character ;
|
TUPLE: ebnf-any-character ;
|
||||||
|
TUPLE: ebnf-range pattern ;
|
||||||
TUPLE: ebnf-ensure-not group ;
|
TUPLE: ebnf-ensure-not group ;
|
||||||
TUPLE: ebnf-choice options ;
|
TUPLE: ebnf-choice options ;
|
||||||
TUPLE: ebnf-sequence elements ;
|
TUPLE: ebnf-sequence elements ;
|
||||||
|
@ -22,6 +23,7 @@ TUPLE: ebnf rules ;
|
||||||
C: <ebnf-non-terminal> ebnf-non-terminal
|
C: <ebnf-non-terminal> ebnf-non-terminal
|
||||||
C: <ebnf-terminal> ebnf-terminal
|
C: <ebnf-terminal> ebnf-terminal
|
||||||
C: <ebnf-any-character> ebnf-any-character
|
C: <ebnf-any-character> ebnf-any-character
|
||||||
|
C: <ebnf-range> ebnf-range
|
||||||
C: <ebnf-ensure-not> ebnf-ensure-not
|
C: <ebnf-ensure-not> ebnf-ensure-not
|
||||||
C: <ebnf-choice> ebnf-choice
|
C: <ebnf-choice> ebnf-choice
|
||||||
C: <ebnf-sequence> ebnf-sequence
|
C: <ebnf-sequence> ebnf-sequence
|
||||||
|
@ -69,6 +71,9 @@ M: ebnf-non-terminal (generate-parser) ( ast -- id )
|
||||||
M: ebnf-any-character (generate-parser) ( ast -- id )
|
M: ebnf-any-character (generate-parser) ( ast -- id )
|
||||||
drop [ drop t ] satisfy store-parser ;
|
drop [ drop t ] satisfy store-parser ;
|
||||||
|
|
||||||
|
M: ebnf-range (generate-parser) ( ast -- id )
|
||||||
|
ebnf-range-pattern range-pattern store-parser ;
|
||||||
|
|
||||||
M: ebnf-choice (generate-parser) ( ast -- id )
|
M: ebnf-choice (generate-parser) ( ast -- id )
|
||||||
ebnf-choice-options [
|
ebnf-choice-options [
|
||||||
generate-parser get-parser
|
generate-parser get-parser
|
||||||
|
@ -164,6 +169,14 @@ DEFER: 'rhs'
|
||||||
#! A parser to match the symbol for any character match.
|
#! A parser to match the symbol for any character match.
|
||||||
[ CHAR: . = ] satisfy [ drop <ebnf-any-character> ] action ;
|
[ CHAR: . = ] satisfy [ drop <ebnf-any-character> ] action ;
|
||||||
|
|
||||||
|
: 'range-parser' ( -- parser )
|
||||||
|
#! Match the syntax for declaring character ranges
|
||||||
|
[
|
||||||
|
"[" syntax ,
|
||||||
|
[ CHAR: ] = not ] satisfy repeat1 ,
|
||||||
|
"]" syntax ,
|
||||||
|
] seq* [ first >string <ebnf-range> ] action ;
|
||||||
|
|
||||||
: 'element' ( -- parser )
|
: 'element' ( -- parser )
|
||||||
#! An element of a rule. It can be a terminal or a
|
#! An element of a rule. It can be a terminal or a
|
||||||
#! non-terminal but must not be followed by a "=".
|
#! non-terminal but must not be followed by a "=".
|
||||||
|
@ -173,6 +186,7 @@ DEFER: 'rhs'
|
||||||
[
|
[
|
||||||
'non-terminal' ,
|
'non-terminal' ,
|
||||||
'terminal' ,
|
'terminal' ,
|
||||||
|
'range-parser' ,
|
||||||
'any-character' ,
|
'any-character' ,
|
||||||
] choice* ,
|
] choice* ,
|
||||||
"=" syntax ensure-not ,
|
"=" syntax ensure-not ,
|
||||||
|
|
Loading…
Reference in New Issue