|
|
@ -3,24 +3,43 @@
|
|
|
|
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 ;
|
|
|
|
words quotations effects memoize ;
|
|
|
|
IN: peg
|
|
|
|
IN: peg
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: parse-result remaining ast ;
|
|
|
|
TUPLE: parse-result remaining ast ;
|
|
|
|
|
|
|
|
|
|
|
|
GENERIC: compile ( parser -- quot )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
: parse ( state parser -- result )
|
|
|
|
|
|
|
|
compile call ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SYMBOL: ignore
|
|
|
|
SYMBOL: ignore
|
|
|
|
|
|
|
|
|
|
|
|
: <parse-result> ( remaining ast -- parse-result )
|
|
|
|
: <parse-result> ( remaining ast -- parse-result )
|
|
|
|
parse-result construct-boa ;
|
|
|
|
parse-result construct-boa ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SYMBOL: compiled-parsers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GENERIC: (compile) ( parser -- quot )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
: compiled-parser ( parser -- word )
|
|
|
|
|
|
|
|
#! Look to see if the given parser has been compiled.
|
|
|
|
|
|
|
|
#! If not, compile it to a temporary word, cache it,
|
|
|
|
|
|
|
|
#! and return it. Otherwise return the existing one.
|
|
|
|
|
|
|
|
dup compiled-parsers get at [
|
|
|
|
|
|
|
|
nip
|
|
|
|
|
|
|
|
] [
|
|
|
|
|
|
|
|
dup (compile) define-temp
|
|
|
|
|
|
|
|
[ swap compiled-parsers get set-at ] keep
|
|
|
|
|
|
|
|
] if* ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MEMO: compile ( parser -- word )
|
|
|
|
|
|
|
|
H{ } clone compiled-parsers [
|
|
|
|
|
|
|
|
[ compiled-parser ] with-compilation-unit
|
|
|
|
|
|
|
|
] with-variable ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
: parse ( state parser -- result )
|
|
|
|
|
|
|
|
compile execute ;
|
|
|
|
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: token-parser symbol ;
|
|
|
|
TUPLE: token-parser symbol ;
|
|
|
|
|
|
|
|
! M: token-parser equal? eq? ;
|
|
|
|
|
|
|
|
|
|
|
|
MATCH-VARS: ?token ;
|
|
|
|
MATCH-VARS: ?token ;
|
|
|
|
|
|
|
|
|
|
|
@ -33,7 +52,7 @@ MATCH-VARS: ?token ;
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: token-parser compile ( parser -- quot )
|
|
|
|
M: token-parser (compile) ( parser -- quot )
|
|
|
|
token-parser-symbol \ ?token token-pattern match-replace ;
|
|
|
|
token-parser-symbol \ ?token token-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: satisfy-parser quot ;
|
|
|
|
TUPLE: satisfy-parser quot ;
|
|
|
@ -53,7 +72,7 @@ MATCH-VARS: ?quot ;
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: satisfy-parser compile ( parser -- quot )
|
|
|
|
M: satisfy-parser (compile) ( parser -- quot )
|
|
|
|
satisfy-parser-quot \ ?quot satisfy-pattern match-replace ;
|
|
|
|
satisfy-parser-quot \ ?quot satisfy-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: range-parser min max ;
|
|
|
|
TUPLE: range-parser min max ;
|
|
|
@ -74,7 +93,7 @@ MATCH-VARS: ?min ?max ;
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: range-parser compile ( parser -- quot )
|
|
|
|
M: range-parser (compile) ( parser -- quot )
|
|
|
|
T{ range-parser _ ?min ?max } range-pattern match-replace ;
|
|
|
|
T{ range-parser _ ?min ?max } range-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: seq-parser parsers ;
|
|
|
|
TUPLE: seq-parser parsers ;
|
|
|
@ -82,7 +101,7 @@ TUPLE: seq-parser parsers ;
|
|
|
|
: seq-pattern ( -- quot )
|
|
|
|
: seq-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
dup [
|
|
|
|
dup [
|
|
|
|
dup parse-result-remaining ?quot call [
|
|
|
|
dup parse-result-remaining ?quot [
|
|
|
|
[ parse-result-remaining swap set-parse-result-remaining ] 2keep
|
|
|
|
[ parse-result-remaining swap set-parse-result-remaining ] 2keep
|
|
|
|
parse-result-ast dup ignore = [
|
|
|
|
parse-result-ast dup ignore = [
|
|
|
|
drop
|
|
|
|
drop
|
|
|
@ -97,10 +116,10 @@ TUPLE: seq-parser parsers ;
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: seq-parser compile ( parser -- quot )
|
|
|
|
M: seq-parser (compile) ( parser -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
|
|
|
[ V{ } clone <parse-result> ] %
|
|
|
|
seq-parser-parsers [ compile \ ?quot seq-pattern match-replace % ] each
|
|
|
|
seq-parser-parsers [ compiled-parser \ ?quot seq-pattern match-replace % ] each
|
|
|
|
] [ ] make ;
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: choice-parser parsers ;
|
|
|
|
TUPLE: choice-parser parsers ;
|
|
|
@ -110,14 +129,14 @@ TUPLE: choice-parser parsers ;
|
|
|
|
dup [
|
|
|
|
dup [
|
|
|
|
|
|
|
|
|
|
|
|
] [
|
|
|
|
] [
|
|
|
|
drop dup ?quot call
|
|
|
|
drop dup ?quot
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: choice-parser compile ( parser -- quot )
|
|
|
|
M: choice-parser (compile) ( parser -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
f ,
|
|
|
|
f ,
|
|
|
|
choice-parser-parsers [ compile \ ?quot choice-pattern match-replace % ] each
|
|
|
|
choice-parser-parsers [ compiled-parser \ ?quot choice-pattern match-replace % ] each
|
|
|
|
\ nip ,
|
|
|
|
\ nip ,
|
|
|
|
] [ ] make ;
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
|
|
|
@ -134,20 +153,20 @@ TUPLE: repeat0-parser p1 ;
|
|
|
|
|
|
|
|
|
|
|
|
: repeat0-pattern ( -- quot )
|
|
|
|
: repeat0-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
?quot swap (repeat0)
|
|
|
|
[ ?quot ] swap (repeat0)
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: repeat0-parser compile ( parser -- quot )
|
|
|
|
M: repeat0-parser (compile) ( parser -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
|
|
|
[ V{ } clone <parse-result> ] %
|
|
|
|
repeat0-parser-p1 compile \ ?quot repeat0-pattern match-replace %
|
|
|
|
repeat0-parser-p1 compiled-parser \ ?quot repeat0-pattern match-replace %
|
|
|
|
] [ ] make ;
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: repeat1-parser p1 ;
|
|
|
|
TUPLE: repeat1-parser p1 ;
|
|
|
|
|
|
|
|
|
|
|
|
: repeat1-pattern ( -- quot )
|
|
|
|
: repeat1-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
?quot swap (repeat0) [
|
|
|
|
[ ?quot ] swap (repeat0) [
|
|
|
|
dup parse-result-ast empty? [
|
|
|
|
dup parse-result-ast empty? [
|
|
|
|
drop f
|
|
|
|
drop f
|
|
|
|
] when
|
|
|
|
] when
|
|
|
@ -156,49 +175,49 @@ TUPLE: repeat1-parser p1 ;
|
|
|
|
] if*
|
|
|
|
] if*
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: repeat1-parser compile ( parser -- quot )
|
|
|
|
M: repeat1-parser (compile) ( parser -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
|
|
|
[ V{ } clone <parse-result> ] %
|
|
|
|
repeat1-parser-p1 compile \ ?quot repeat1-pattern match-replace %
|
|
|
|
repeat1-parser-p1 compiled-parser \ ?quot repeat1-pattern match-replace %
|
|
|
|
] [ ] make ;
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: optional-parser p1 ;
|
|
|
|
TUPLE: optional-parser p1 ;
|
|
|
|
|
|
|
|
|
|
|
|
: optional-pattern ( -- quot )
|
|
|
|
: optional-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
dup ?quot call swap f <parse-result> or
|
|
|
|
dup ?quot swap f <parse-result> or
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: optional-parser compile ( parser -- quot )
|
|
|
|
M: optional-parser (compile) ( parser -- quot )
|
|
|
|
optional-parser-p1 compile \ ?quot optional-pattern match-replace ;
|
|
|
|
optional-parser-p1 compiled-parser \ ?quot optional-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: ensure-parser p1 ;
|
|
|
|
TUPLE: ensure-parser p1 ;
|
|
|
|
|
|
|
|
|
|
|
|
: ensure-pattern ( -- quot )
|
|
|
|
: ensure-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
dup ?quot call [
|
|
|
|
dup ?quot [
|
|
|
|
ignore <parse-result>
|
|
|
|
ignore <parse-result>
|
|
|
|
] [
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
drop f
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: ensure-parser compile ( parser -- quot )
|
|
|
|
M: ensure-parser (compile) ( parser -- quot )
|
|
|
|
ensure-parser-p1 compile \ ?quot ensure-pattern match-replace ;
|
|
|
|
ensure-parser-p1 compiled-parser \ ?quot ensure-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: ensure-not-parser p1 ;
|
|
|
|
TUPLE: ensure-not-parser p1 ;
|
|
|
|
|
|
|
|
|
|
|
|
: ensure-not-pattern ( -- quot )
|
|
|
|
: ensure-not-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
dup ?quot call [
|
|
|
|
dup ?quot [
|
|
|
|
drop f
|
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
] [
|
|
|
|
ignore <parse-result>
|
|
|
|
ignore <parse-result>
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: ensure-not-parser compile ( parser -- quot )
|
|
|
|
M: ensure-not-parser (compile) ( parser -- quot )
|
|
|
|
ensure-not-parser-p1 compile \ ?quot ensure-not-pattern match-replace ;
|
|
|
|
ensure-not-parser-p1 compiled-parser \ ?quot ensure-not-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: action-parser p1 quot ;
|
|
|
|
TUPLE: action-parser p1 quot ;
|
|
|
|
|
|
|
|
|
|
|
@ -206,14 +225,14 @@ MATCH-VARS: ?action ;
|
|
|
|
|
|
|
|
|
|
|
|
: action-pattern ( -- quot )
|
|
|
|
: action-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
?quot call dup [
|
|
|
|
?quot dup [
|
|
|
|
dup parse-result-ast ?action call
|
|
|
|
dup parse-result-ast ?action call
|
|
|
|
swap [ set-parse-result-ast ] keep
|
|
|
|
swap [ set-parse-result-ast ] keep
|
|
|
|
] when
|
|
|
|
] when
|
|
|
|
] ;
|
|
|
|
] ;
|
|
|
|
|
|
|
|
|
|
|
|
M: action-parser compile ( parser -- quot )
|
|
|
|
M: action-parser (compile) ( parser -- quot )
|
|
|
|
{ action-parser-p1 action-parser-quot } get-slots [ compile ] dip
|
|
|
|
{ action-parser-p1 action-parser-quot } get-slots [ compiled-parser ] dip
|
|
|
|
2array { ?quot ?action } action-pattern match-replace ;
|
|
|
|
2array { ?quot ?action } action-pattern match-replace ;
|
|
|
|
|
|
|
|
|
|
|
|
: left-trim-slice ( string -- string )
|
|
|
|
: left-trim-slice ( string -- string )
|
|
|
@ -225,17 +244,22 @@ M: action-parser compile ( parser -- quot )
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: sp-parser p1 ;
|
|
|
|
TUPLE: sp-parser p1 ;
|
|
|
|
|
|
|
|
|
|
|
|
M: sp-parser compile ( parser -- quot )
|
|
|
|
M: sp-parser (compile) ( parser -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
\ left-trim-slice , sp-parser-p1 compile %
|
|
|
|
\ left-trim-slice , sp-parser-p1 compiled-parser ,
|
|
|
|
] [ ] make ;
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
|
|
|
|
TUPLE: delay-parser quot ;
|
|
|
|
TUPLE: delay-parser quot ;
|
|
|
|
|
|
|
|
|
|
|
|
M: delay-parser compile ( parser -- quot )
|
|
|
|
M: delay-parser (compile) ( parser -- quot )
|
|
|
|
|
|
|
|
#! For efficiency we memoize the quotation.
|
|
|
|
|
|
|
|
#! This way it is run only once and the
|
|
|
|
|
|
|
|
#! parser constructed once at run time.
|
|
|
|
[
|
|
|
|
[
|
|
|
|
delay-parser-quot % \ compile , \ call ,
|
|
|
|
delay-parser-quot % \ compile ,
|
|
|
|
] [ ] make ;
|
|
|
|
] [ ] make
|
|
|
|
|
|
|
|
{ } { "word" } <effect> memoize-quot
|
|
|
|
|
|
|
|
[ % \ execute , ] [ ] make ;
|
|
|
|
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
|
|
|
@ -308,7 +332,7 @@ PRIVATE>
|
|
|
|
: PEG:
|
|
|
|
: PEG:
|
|
|
|
(:) [
|
|
|
|
(:) [
|
|
|
|
[
|
|
|
|
[
|
|
|
|
call compile
|
|
|
|
call compile 1quotation
|
|
|
|
[ dup [ parse-result-ast ] [ "Parse failed" throw ] if ]
|
|
|
|
[ dup [ parse-result-ast ] [ "Parse failed" throw ] if ]
|
|
|
|
append define
|
|
|
|
append define
|
|
|
|
] with-compilation-unit
|
|
|
|
] with-compilation-unit
|
|
|
|