Add ranges to EBNF syntax

This works:
  <EBNF letter = [a-zA-Z] EBNF>
and
  <EBNF not-digit = [^0-9] EBNF>
db4
Chris Double 2008-03-20 13:55:19 +13:00
parent 264284d0c4
commit 795ef0ae3b
2 changed files with 38 additions and 0 deletions

View File

@ -118,4 +118,28 @@ IN: peg.ebnf.tests
{ V{ 1 2 } } [
"foo=('a') [[ drop 1 ]] ('b') [[ drop 2 ]]" ebnf>quot with-compilation-unit "ab" foo parse parse-result-ast
] 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

View File

@ -9,6 +9,7 @@ IN: peg.ebnf
TUPLE: ebnf-non-terminal symbol ;
TUPLE: ebnf-terminal symbol ;
TUPLE: ebnf-any-character ;
TUPLE: ebnf-range pattern ;
TUPLE: ebnf-ensure-not group ;
TUPLE: ebnf-choice options ;
TUPLE: ebnf-sequence elements ;
@ -22,6 +23,7 @@ TUPLE: ebnf rules ;
C: <ebnf-non-terminal> ebnf-non-terminal
C: <ebnf-terminal> ebnf-terminal
C: <ebnf-any-character> ebnf-any-character
C: <ebnf-range> ebnf-range
C: <ebnf-ensure-not> ebnf-ensure-not
C: <ebnf-choice> ebnf-choice
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 )
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 )
ebnf-choice-options [
generate-parser get-parser
@ -163,6 +168,14 @@ DEFER: 'rhs'
: 'any-character' ( -- parser )
#! A parser to match the symbol for any character match.
[ 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 )
#! An element of a rule. It can be a terminal or a
@ -173,6 +186,7 @@ DEFER: 'rhs'
[
'non-terminal' ,
'terminal' ,
'range-parser' ,
'any-character' ,
] choice* ,
"=" syntax ensure-not ,