Merge branch 'master' of git://double.co.nz/git/factor
commit
69e9d9f258
extra/peg
|
@ -3,7 +3,7 @@
|
|||
USING: kernel compiler.units parser words arrays strings math.parser sequences
|
||||
quotations vectors namespaces math assocs continuations peg
|
||||
peg.parsers unicode.categories multiline combinators.lib
|
||||
splitting accessors ;
|
||||
splitting accessors effects ;
|
||||
IN: peg.ebnf
|
||||
|
||||
TUPLE: ebnf-non-terminal symbol ;
|
||||
|
@ -19,6 +19,7 @@ TUPLE: ebnf-repeat1 group ;
|
|||
TUPLE: ebnf-optional group ;
|
||||
TUPLE: ebnf-rule symbol elements ;
|
||||
TUPLE: ebnf-action parser code ;
|
||||
TUPLE: ebnf-var parser name ;
|
||||
TUPLE: ebnf rules ;
|
||||
|
||||
C: <ebnf-non-terminal> ebnf-non-terminal
|
||||
|
@ -34,6 +35,7 @@ C: <ebnf-repeat1> ebnf-repeat1
|
|||
C: <ebnf-optional> ebnf-optional
|
||||
C: <ebnf-rule> ebnf-rule
|
||||
C: <ebnf-action> ebnf-action
|
||||
C: <ebnf-var> ebnf-var
|
||||
C: <ebnf> ebnf
|
||||
|
||||
: syntax ( string -- parser )
|
||||
|
@ -79,6 +81,7 @@ C: <ebnf> ebnf
|
|||
[ dup CHAR: * = ]
|
||||
[ dup CHAR: + = ]
|
||||
[ dup CHAR: ? = ]
|
||||
[ dup CHAR: : = ]
|
||||
} || not nip
|
||||
] satisfy repeat1 [ >string <ebnf-non-terminal> ] action ;
|
||||
|
||||
|
@ -200,6 +203,7 @@ DEFER: 'choice'
|
|||
: 'actioned-sequence' ( -- parser )
|
||||
[
|
||||
[ 'sequence' , "=>" syntax , 'action' , ] seq* [ first2 <ebnf-action> ] action ,
|
||||
[ 'sequence' , ":" syntax , "a-zA-Z" range-pattern repeat1 [ >string ] action , ] seq* [ first2 <ebnf-var> ] action ,
|
||||
'sequence' ,
|
||||
] choice* ;
|
||||
|
||||
|
@ -270,6 +274,9 @@ M: ebnf-action (transform) ( ast -- parser )
|
|||
[ parser>> (transform) ] keep
|
||||
code>> string-lines [ parse-lines ] with-compilation-unit action ;
|
||||
|
||||
M: ebnf-var (transform) ( ast -- parser )
|
||||
parser>> (transform) ;
|
||||
|
||||
M: ebnf-terminal (transform) ( ast -- parser )
|
||||
symbol>> token sp ;
|
||||
|
||||
|
@ -303,5 +310,5 @@ M: ebnf-non-terminal (transform) ( ast -- parser )
|
|||
: EBNF:
|
||||
CREATE-WORD dup
|
||||
";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
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
USING: kernel sequences strings namespaces math assocs shuffle
|
||||
vectors arrays combinators.lib math.parser match
|
||||
unicode.categories sequences.lib compiler.units parser
|
||||
words quotations effects memoize accessors locals ;
|
||||
words quotations effects memoize accessors locals effects ;
|
||||
IN: peg
|
||||
|
||||
USE: prettyprint
|
||||
|
@ -208,7 +208,7 @@ GENERIC: (compile) ( parser -- quot )
|
|||
:: parser-body ( parser -- quot )
|
||||
#! Return the body of the word that is the compiled version
|
||||
#! 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 = [
|
||||
|
@ -218,7 +218,7 @@ GENERIC: (compile) ( parser -- quot )
|
|||
] if
|
||||
]
|
||||
] ;
|
||||
|
||||
|
||||
: compiled-parser ( parser -- word )
|
||||
#! Look to see if the given parser has been compiled.
|
||||
#! If not, compile it to a temporary word, cache it,
|
||||
|
@ -229,7 +229,7 @@ GENERIC: (compile) ( parser -- quot )
|
|||
dup compiled>> [
|
||||
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* ;
|
||||
|
||||
: compile ( parser -- word )
|
||||
|
@ -490,8 +490,11 @@ M: box-parser (compile) ( parser -- quot )
|
|||
#! Calls the quotation at compile time
|
||||
#! to produce the parser to be compiled.
|
||||
#! This differs from 'delay' which calls
|
||||
#! it at run time.
|
||||
quot>> call compiled-parser 1quotation ;
|
||||
#! it at run time. Due to using the runtime
|
||||
#! 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>
|
||||
|
||||
|
@ -562,7 +565,12 @@ PRIVATE>
|
|||
delay-parser construct-boa init-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:
|
||||
(:) [
|
||||
|
|
Loading…
Reference in New Issue