Tidy up ebnf compilation

release
Chris Double 2007-11-29 17:24:02 +13:00
parent f94c280e06
commit a4461ae408
1 changed files with 85 additions and 62 deletions

View File

@ -1,6 +1,7 @@
! Copyright (C) 2007 Chris Double. ! Copyright (C) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel parser words arrays strings math.parser sequences quotations vectors namespaces peg ; USING: kernel parser words arrays strings math.parser sequences
quotations vectors namespaces math assocs continuations peg ;
IN: peg.ebnf IN: peg.ebnf
TUPLE: ebnf-non-terminal symbol ; TUPLE: ebnf-non-terminal symbol ;
@ -23,74 +24,77 @@ C: <ebnf-rule> ebnf-rule
C: <ebnf-action> ebnf-action C: <ebnf-action> ebnf-action
C: <ebnf> ebnf C: <ebnf> ebnf
GENERIC: ebnf-compile ( ast -- quot ) SYMBOL: parsers
SYMBOL: non-terminals
SYMBOL: last-parser
M: ebnf-terminal ebnf-compile ( ast -- quot ) : reset-parser-generation ( -- )
V{ } clone parsers set
H{ } clone non-terminals set
f last-parser set ;
: store-parser ( parser -- number )
parsers get [ push ] keep length 1- ;
: get-parser ( index -- parser )
parsers get nth ;
: non-terminal-index ( name -- number )
dup non-terminals get at [
nip
] [
f store-parser [ swap non-terminals get set-at ] keep
] if* ;
GENERIC: (generate-parser) ( ast -- id )
: generate-parser ( ast -- id )
(generate-parser) dup last-parser set ;
M: ebnf-terminal (generate-parser) ( ast -- id )
ebnf-terminal-symbol token sp store-parser ;
M: ebnf-non-terminal (generate-parser) ( ast -- id )
[ [
ebnf-terminal-symbol , \ token , \ sp , ebnf-non-terminal-symbol dup non-terminal-index ,
] [ ] make ; parsers get , \ nth , [ search ] [ 2drop f ] recover , \ or ,
] [ ] make delay sp store-parser ;
M: ebnf-non-terminal ebnf-compile ( ast -- quot ) M: ebnf-choice (generate-parser) ( ast -- id )
[ ebnf-choice-options [
[ ebnf-non-terminal-symbol , \ search , \ execute , \ sp , ] [ ] make generate-parser get-parser
, \ delay , ] map choice store-parser ;
] [ ] make ;
M: ebnf-choice ebnf-compile ( ast -- quot ) M: ebnf-sequence (generate-parser) ( ast -- id )
[ ebnf-sequence-elements [
[ generate-parser get-parser
ebnf-choice-options [ ] map seq store-parser ;
ebnf-compile ,
] each
] { } make ,
[ call ] , \ map , \ choice ,
] [ ] make ;
M: ebnf-sequence ebnf-compile ( ast -- quot ) M: ebnf-repeat0 (generate-parser) ( ast -- id )
[ ebnf-repeat0-group generate-parser get-parser repeat0 store-parser ;
[
ebnf-sequence-elements [
ebnf-compile ,
] each
] { } make ,
[ call ] , \ map , \ seq ,
] [ ] make ;
M: ebnf-repeat0 ebnf-compile ( ast -- quot ) M: ebnf-optional (generate-parser) ( ast -- id )
[ ebnf-optional-elements generate-parser get-parser optional store-parser ;
ebnf-repeat0-group ebnf-compile % \ repeat0 ,
] [ ] make ;
M: ebnf-optional ebnf-compile ( ast -- quot ) M: ebnf-rule (generate-parser) ( ast -- id )
[ dup ebnf-rule-symbol non-terminal-index swap
ebnf-optional-elements ebnf-compile % \ optional , ebnf-rule-elements generate-parser get-parser ! nt-id body
] [ ] make ; swap [ parsers get set-nth ] keep ;
M: ebnf-rule ebnf-compile ( ast -- quot ) M: ebnf-action (generate-parser) ( ast -- id )
[ ebnf-action-word search 1quotation
dup ebnf-rule-symbol , \ in , \ get , \ create , last-parser get swap action generate-parser ;
ebnf-rule-elements ebnf-compile , \ define-compound ,
] [ ] make ;
M: ebnf-action ebnf-compile ( ast -- quot ) M: vector (generate-parser) ( ast -- id )
[ [ generate-parser ] map peek ;
ebnf-action-word search 1quotation , \ action ,
] [ ] make ;
M: vector ebnf-compile ( ast -- quot ) M: f (generate-parser) ( ast -- id )
[ drop last-parser get ;
[ ebnf-compile % ] each
] [ ] make ;
M: f ebnf-compile ( ast -- quot ) M: ebnf (generate-parser) ( ast -- id )
drop [ ] ; ebnf-rules [
generate-parser
M: ebnf ebnf-compile ( ast -- quot ) ] map peek ;
[
ebnf-rules [
ebnf-compile %
] each
] [ ] make ;
DEFER: 'rhs' DEFER: 'rhs'
@ -124,7 +128,12 @@ DEFER: 'choice'
3array seq [ first <ebnf-optional> ] action ; 3array seq [ first <ebnf-optional> ] action ;
: 'sequence' ( -- parser ) : 'sequence' ( -- parser )
'element' sp 'group' sp 'repeat0' sp 'optional' sp 4array choice [
'element' sp ,
'group' sp ,
'repeat0' sp ,
'optional' sp ,
] { } make choice
repeat1 [ repeat1 [
dup length 1 = [ first ] [ <ebnf-sequence> ] if dup length 1 = [ first ] [ <ebnf-sequence> ] if
] action ; ] action ;
@ -153,7 +162,21 @@ DEFER: 'choice'
: ebnf>quot ( string -- quot ) : ebnf>quot ( string -- quot )
'ebnf' parse [ 'ebnf' parse [
parse-result-ast ebnf-compile parse-result-ast [
reset-parser-generation
generate-parser drop
[
non-terminals get
[
get-parser [
swap , \ in , \ get , \ create ,
1quotation , \ define-compound ,
] [
drop
] if*
] assoc-each
] [ ] make
] with-scope
] [ ] [
f f
] if* ; ] if* ;