Merge branch 'master' of git://double.co.nz/git/factor

db4
Slava Pestov 2008-03-30 12:42:41 -05:00
commit 69e9d9f258
2 changed files with 24 additions and 9 deletions

View File

@ -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 accessors ; splitting accessors effects ;
IN: peg.ebnf IN: peg.ebnf
TUPLE: ebnf-non-terminal symbol ; TUPLE: ebnf-non-terminal symbol ;
@ -19,6 +19,7 @@ TUPLE: ebnf-repeat1 group ;
TUPLE: ebnf-optional group ; 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-var parser name ;
TUPLE: ebnf rules ; TUPLE: ebnf rules ;
C: <ebnf-non-terminal> ebnf-non-terminal C: <ebnf-non-terminal> ebnf-non-terminal
@ -34,6 +35,7 @@ C: <ebnf-repeat1> ebnf-repeat1
C: <ebnf-optional> ebnf-optional C: <ebnf-optional> ebnf-optional
C: <ebnf-rule> ebnf-rule C: <ebnf-rule> ebnf-rule
C: <ebnf-action> ebnf-action C: <ebnf-action> ebnf-action
C: <ebnf-var> ebnf-var
C: <ebnf> ebnf C: <ebnf> ebnf
: syntax ( string -- parser ) : syntax ( string -- parser )
@ -79,6 +81,7 @@ C: <ebnf> ebnf
[ dup CHAR: * = ] [ dup CHAR: * = ]
[ dup CHAR: + = ] [ dup CHAR: + = ]
[ dup CHAR: ? = ] [ dup CHAR: ? = ]
[ dup CHAR: : = ]
} || not nip } || not nip
] satisfy repeat1 [ >string <ebnf-non-terminal> ] action ; ] satisfy repeat1 [ >string <ebnf-non-terminal> ] action ;
@ -200,6 +203,7 @@ DEFER: 'choice'
: 'actioned-sequence' ( -- parser ) : 'actioned-sequence' ( -- parser )
[ [
[ 'sequence' , "=>" syntax , 'action' , ] seq* [ first2 <ebnf-action> ] action , [ 'sequence' , "=>" syntax , 'action' , ] seq* [ first2 <ebnf-action> ] action ,
[ 'sequence' , ":" syntax , "a-zA-Z" range-pattern repeat1 [ >string ] action , ] seq* [ first2 <ebnf-var> ] action ,
'sequence' , 'sequence' ,
] choice* ; ] choice* ;
@ -270,6 +274,9 @@ M: ebnf-action (transform) ( ast -- parser )
[ parser>> (transform) ] keep [ parser>> (transform) ] keep
code>> string-lines [ parse-lines ] with-compilation-unit action ; code>> string-lines [ parse-lines ] with-compilation-unit action ;
M: ebnf-var (transform) ( ast -- parser )
parser>> (transform) ;
M: ebnf-terminal (transform) ( ast -- parser ) M: ebnf-terminal (transform) ( ast -- parser )
symbol>> token sp ; symbol>> token sp ;
@ -303,5 +310,5 @@ M: ebnf-non-terminal (transform) ( ast -- parser )
: EBNF: : EBNF:
CREATE-WORD dup CREATE-WORD dup
";EBNF" parse-multiline-string ";EBNF" parse-multiline-string
ebnf>quot swapd define "ebnf-parser" set-word-prop ; parsing ebnf>quot swapd 1 1 <effect> define-declared "ebnf-parser" set-word-prop ; parsing

View File

@ -3,7 +3,7 @@
USING: kernel sequences strings namespaces math assocs shuffle USING: kernel sequences strings namespaces math assocs shuffle
vectors arrays combinators.lib math.parser match vectors arrays combinators.lib math.parser match
unicode.categories sequences.lib compiler.units parser unicode.categories sequences.lib compiler.units parser
words quotations effects memoize accessors locals ; words quotations effects memoize accessors locals effects ;
IN: peg IN: peg
USE: prettyprint USE: prettyprint
@ -208,7 +208,7 @@ GENERIC: (compile) ( parser -- quot )
:: parser-body ( parser -- quot ) :: parser-body ( parser -- quot )
#! Return the body of the word that is the compiled version #! Return the body of the word that is the compiled version
#! of the parser. #! of the parser.
[let* | rule [ parser (compile) define-temp dup parser "peg" set-word-prop ] [let* | rule [ gensym dup parser (compile) 0 1 <effect> define-declared dup parser "peg" set-word-prop ]
| |
[ [
rule pos get apply-rule dup fail = [ rule pos get apply-rule dup fail = [
@ -229,7 +229,7 @@ GENERIC: (compile) ( parser -- quot )
dup compiled>> [ dup compiled>> [
nip nip
] [ ] [
gensym tuck >>compiled 2dup parser-body define dupd "peg" set-word-prop gensym tuck >>compiled 2dup parser-body 0 1 <effect> define-declared dupd "peg" set-word-prop
] if* ; ] if* ;
: compile ( parser -- word ) : compile ( parser -- word )
@ -490,8 +490,11 @@ M: box-parser (compile) ( parser -- quot )
#! Calls the quotation at compile time #! Calls the quotation at compile time
#! to produce the parser to be compiled. #! to produce the parser to be compiled.
#! This differs from 'delay' which calls #! This differs from 'delay' which calls
#! it at run time. #! it at run time. Due to using the runtime
quot>> call compiled-parser 1quotation ; #! environment at compile time, this parser
#! must not be cached, so we clear out the
#! delgates cache.
f >>compiled quot>> call compiled-parser 1quotation ;
PRIVATE> PRIVATE>
@ -562,7 +565,12 @@ PRIVATE>
delay-parser construct-boa init-parser ; delay-parser construct-boa init-parser ;
: box ( quot -- parser ) : box ( quot -- parser )
box-parser construct-boa init-parser ; #! because a box has its quotation run at compile time
#! it must always have a new parser delgate created,
#! not a cached one. This is because the same box,
#! compiled twice can have a different compiled word
#! due to running at compile time.
box-parser construct-boa next-id f <parser> over set-delegate ;
: PEG: : PEG:
(:) [ (:) [