Use new slots in peg.ebnf
parent
8569d18068
commit
1ec945ba4c
|
@ -3,7 +3,7 @@
|
||||||
USING: kernel compiler.units parser words arrays strings math.parser sequences
|
USING: kernel compiler.units parser words arrays strings math.parser sequences
|
||||||
quotations vectors namespaces math assocs continuations peg
|
quotations vectors namespaces math assocs continuations peg
|
||||||
peg.parsers unicode.categories multiline combinators.lib
|
peg.parsers unicode.categories multiline combinators.lib
|
||||||
splitting ;
|
splitting accessors ;
|
||||||
IN: peg.ebnf
|
IN: peg.ebnf
|
||||||
|
|
||||||
TUPLE: ebnf-non-terminal symbol ;
|
TUPLE: ebnf-non-terminal symbol ;
|
||||||
|
@ -16,7 +16,7 @@ TUPLE: ebnf-choice options ;
|
||||||
TUPLE: ebnf-sequence elements ;
|
TUPLE: ebnf-sequence elements ;
|
||||||
TUPLE: ebnf-repeat0 group ;
|
TUPLE: ebnf-repeat0 group ;
|
||||||
TUPLE: ebnf-repeat1 group ;
|
TUPLE: ebnf-repeat1 group ;
|
||||||
TUPLE: ebnf-optional elements ;
|
TUPLE: ebnf-optional group ;
|
||||||
TUPLE: ebnf-rule symbol elements ;
|
TUPLE: ebnf-rule symbol elements ;
|
||||||
TUPLE: ebnf-action parser code ;
|
TUPLE: ebnf-action parser code ;
|
||||||
TUPLE: ebnf rules ;
|
TUPLE: ebnf rules ;
|
||||||
|
@ -198,7 +198,7 @@ DEFER: 'choice'
|
||||||
|
|
||||||
: 'rule' ( -- parser )
|
: 'rule' ( -- parser )
|
||||||
[
|
[
|
||||||
'non-terminal' [ ebnf-non-terminal-symbol ] action ,
|
'non-terminal' [ symbol>> ] action ,
|
||||||
"=" syntax ,
|
"=" syntax ,
|
||||||
'choice' ,
|
'choice' ,
|
||||||
] seq* [ first2 <ebnf-rule> ] action ;
|
] seq* [ first2 <ebnf-rule> ] action ;
|
||||||
|
@ -215,49 +215,53 @@ SYMBOL: main
|
||||||
H{ } clone dup dup [ parser set swap (transform) main set ] bind ;
|
H{ } clone dup dup [ parser set swap (transform) main set ] bind ;
|
||||||
|
|
||||||
M: ebnf (transform) ( ast -- parser )
|
M: ebnf (transform) ( ast -- parser )
|
||||||
ebnf-rules [ (transform) ] map peek ;
|
rules>> [ (transform) ] map peek ;
|
||||||
|
|
||||||
M: ebnf-rule (transform) ( ast -- parser )
|
M: ebnf-rule (transform) ( ast -- parser )
|
||||||
dup ebnf-rule-elements (transform) [
|
dup elements>> (transform) [
|
||||||
swap ebnf-rule-symbol set
|
swap symbol>> set
|
||||||
] keep ;
|
] keep ;
|
||||||
|
|
||||||
M: ebnf-sequence (transform) ( ast -- parser )
|
M: ebnf-sequence (transform) ( ast -- parser )
|
||||||
ebnf-sequence-elements [ (transform) ] map seq ;
|
elements>> [ (transform) ] map seq ;
|
||||||
|
|
||||||
M: ebnf-choice (transform) ( ast -- parser )
|
M: ebnf-choice (transform) ( ast -- parser )
|
||||||
ebnf-choice-options [ (transform) ] map choice ;
|
options>> [ (transform) ] map choice ;
|
||||||
|
|
||||||
M: ebnf-any-character (transform) ( ast -- parser )
|
M: ebnf-any-character (transform) ( ast -- parser )
|
||||||
drop any-char ;
|
drop any-char ;
|
||||||
|
|
||||||
M: ebnf-range (transform) ( ast -- parser )
|
M: ebnf-range (transform) ( ast -- parser )
|
||||||
ebnf-range-pattern range-pattern ;
|
pattern>> range-pattern ;
|
||||||
|
|
||||||
|
: transform-group ( ast -- parser )
|
||||||
|
#! convert a ast node with groups to a parser for that group
|
||||||
|
group>> (transform) ;
|
||||||
|
|
||||||
M: ebnf-ensure (transform) ( ast -- parser )
|
M: ebnf-ensure (transform) ( ast -- parser )
|
||||||
ebnf-ensure-group (transform) ensure ;
|
transform-group ensure ;
|
||||||
|
|
||||||
M: ebnf-ensure-not (transform) ( ast -- parser )
|
M: ebnf-ensure-not (transform) ( ast -- parser )
|
||||||
ebnf-ensure-not-group (transform) ensure-not ;
|
transform-group ensure-not ;
|
||||||
|
|
||||||
M: ebnf-repeat0 (transform) ( ast -- parser )
|
M: ebnf-repeat0 (transform) ( ast -- parser )
|
||||||
ebnf-repeat0-group (transform) repeat0 ;
|
transform-group repeat0 ;
|
||||||
|
|
||||||
M: ebnf-repeat1 (transform) ( ast -- parser )
|
M: ebnf-repeat1 (transform) ( ast -- parser )
|
||||||
ebnf-repeat1-group (transform) repeat1 ;
|
transform-group repeat1 ;
|
||||||
|
|
||||||
M: ebnf-optional (transform) ( ast -- parser )
|
M: ebnf-optional (transform) ( ast -- parser )
|
||||||
ebnf-optional-elements (transform) optional ;
|
transform-group optional ;
|
||||||
|
|
||||||
M: ebnf-action (transform) ( ast -- parser )
|
M: ebnf-action (transform) ( ast -- parser )
|
||||||
[ ebnf-action-parser (transform) ] keep
|
[ parser>> (transform) ] keep
|
||||||
ebnf-action-code string-lines [ parse-lines ] with-compilation-unit action ;
|
code>> string-lines [ parse-lines ] with-compilation-unit action ;
|
||||||
|
|
||||||
M: ebnf-terminal (transform) ( ast -- parser )
|
M: ebnf-terminal (transform) ( ast -- parser )
|
||||||
ebnf-terminal-symbol token sp ;
|
symbol>> token sp ;
|
||||||
|
|
||||||
M: ebnf-non-terminal (transform) ( ast -- parser )
|
M: ebnf-non-terminal (transform) ( ast -- parser )
|
||||||
ebnf-non-terminal-symbol [
|
symbol>> [
|
||||||
, parser get , \ at ,
|
, parser get , \ at ,
|
||||||
] [ ] make delay sp ;
|
] [ ] make delay sp ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue