Start of EBNF parser
parent
9f2f45cd71
commit
31d57422da
|
@ -0,0 +1 @@
|
||||||
|
Chris Double
|
|
@ -0,0 +1,17 @@
|
||||||
|
! Copyright (C) 2007 Chris Double.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
!
|
||||||
|
USING: kernel tools.test peg peg.ebnf ;
|
||||||
|
IN: temporary
|
||||||
|
|
||||||
|
{ T{ ebnf-non-terminal f "abc" } } [
|
||||||
|
"abc" 'non-terminal' parse parse-result-ast
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ T{ ebnf-terminal f "55" } } [
|
||||||
|
"\"55\"" 'terminal' parse parse-result-ast
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
! { } [
|
||||||
|
! "digit = \"0\" | \"1\" | \"2\"" 'rule' parse parse-result-ast
|
||||||
|
! ] unit-test
|
|
@ -0,0 +1,65 @@
|
||||||
|
! Copyright (C) 2007 Chris Double.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: kernel arrays strings math.parser sequences namespaces peg ;
|
||||||
|
IN: peg.ebnf
|
||||||
|
|
||||||
|
TUPLE: ebnf-non-terminal symbol ;
|
||||||
|
TUPLE: ebnf-terminal symbol ;
|
||||||
|
TUPLE: ebnf-choice options ;
|
||||||
|
|
||||||
|
C: <ebnf-non-terminal> ebnf-non-terminal
|
||||||
|
C: <ebnf-terminal> ebnf-terminal
|
||||||
|
C: <ebnf-choice> ebnf-choice
|
||||||
|
|
||||||
|
GENERIC: ebnf-compile ( ast -- quot )
|
||||||
|
|
||||||
|
M: ebnf-terminal ebnf-compile ( ast -- quot )
|
||||||
|
[
|
||||||
|
ebnf-terminal-symbol , \ token ,
|
||||||
|
] [ ] make ;
|
||||||
|
|
||||||
|
M: ebnf-choice ebnf-compile ( ast -- quot )
|
||||||
|
[
|
||||||
|
[
|
||||||
|
ebnf-choice-options [
|
||||||
|
ebnf-compile ,
|
||||||
|
] each
|
||||||
|
] { } make ,
|
||||||
|
[ call ] , \ map ,
|
||||||
|
] [ ] make ;
|
||||||
|
|
||||||
|
DEFER: 'rhs'
|
||||||
|
|
||||||
|
: 'non-terminal' ( -- parser )
|
||||||
|
CHAR: a CHAR: z range repeat1 [ >string <ebnf-non-terminal> ] action ;
|
||||||
|
|
||||||
|
: 'terminal' ( -- parser )
|
||||||
|
"\"" token hide [ CHAR: " = not ] satisfy repeat1 "\"" token hide 3array seq [ first >string <ebnf-terminal> ] action ;
|
||||||
|
|
||||||
|
: 'element' ( -- parser )
|
||||||
|
'non-terminal' 'terminal' 2array choice ;
|
||||||
|
|
||||||
|
: 'sequence' ( -- parser )
|
||||||
|
'element' sp repeat1 ;
|
||||||
|
|
||||||
|
: 'choice' ( -- parser )
|
||||||
|
'element' sp "|" token sp list-of [ <ebnf-choice> ] action ;
|
||||||
|
|
||||||
|
: 'repeat0' ( -- parser )
|
||||||
|
"{" token sp hide
|
||||||
|
[ 'rhs' sp ] delay
|
||||||
|
"}" token sp hide
|
||||||
|
3array seq ;
|
||||||
|
|
||||||
|
: 'rhs' ( -- parser )
|
||||||
|
'repeat0'
|
||||||
|
'choice'
|
||||||
|
'sequence'
|
||||||
|
'element'
|
||||||
|
4array choice ;
|
||||||
|
|
||||||
|
: 'rule' ( -- parser )
|
||||||
|
'non-terminal'
|
||||||
|
"=" token sp
|
||||||
|
'rhs'
|
||||||
|
3array seq ;
|
|
@ -0,0 +1 @@
|
||||||
|
Grammar for parsing EBNF
|
|
@ -216,3 +216,6 @@ PRIVATE>
|
||||||
|
|
||||||
: delay ( parser -- parser )
|
: delay ( parser -- parser )
|
||||||
delay-parser construct-boa init-parser ;
|
delay-parser construct-boa init-parser ;
|
||||||
|
|
||||||
|
: list-of ( items separator -- parser )
|
||||||
|
hide over 2array seq repeat0 [ concat ] action 2array seq [ unclip 1vector swap first append ] action ;
|
||||||
|
|
Loading…
Reference in New Issue