2007-11-19 22:36:38 -05:00
|
|
|
! Copyright (C) 2007 Chris Double.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2007-11-28 18:35:45 -05:00
|
|
|
USING: kernel sequences strings namespaces math assocs shuffle
|
2008-03-20 08:25:45 -04:00
|
|
|
vectors arrays combinators.lib math.parser match
|
2008-03-03 17:57:30 -05:00
|
|
|
unicode.categories sequences.lib compiler.units parser
|
2008-03-21 08:59:16 -04:00
|
|
|
words quotations effects memoize ;
|
2007-11-19 22:36:38 -05:00
|
|
|
IN: peg
|
|
|
|
|
2007-11-20 21:31:23 -05:00
|
|
|
TUPLE: parse-result remaining ast ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2007-11-26 18:22:33 -05:00
|
|
|
SYMBOL: ignore
|
|
|
|
|
2007-11-20 21:31:23 -05:00
|
|
|
: <parse-result> ( remaining ast -- parse-result )
|
2007-11-19 22:36:38 -05:00
|
|
|
parse-result construct-boa ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
SYMBOL: compiled-parsers
|
|
|
|
|
|
|
|
GENERIC: (compile) ( parser -- quot )
|
|
|
|
|
|
|
|
: compiled-parser ( parser -- word )
|
2008-03-21 08:59:16 -04:00
|
|
|
#! Look to see if the given parser has been compiled.
|
2008-03-20 10:05:21 -04:00
|
|
|
#! 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* ;
|
|
|
|
|
2008-03-21 08:59:16 -04:00
|
|
|
MEMO: compile ( parser -- word )
|
2008-03-20 10:05:21 -04:00
|
|
|
H{ } clone compiled-parsers [
|
|
|
|
[ compiled-parser ] with-compilation-unit
|
|
|
|
] with-variable ;
|
|
|
|
|
|
|
|
: parse ( state parser -- result )
|
2008-03-21 07:58:53 -04:00
|
|
|
compile execute ;
|
2008-03-20 10:05:21 -04:00
|
|
|
|
2007-11-28 18:35:45 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2007-11-19 22:36:38 -05:00
|
|
|
TUPLE: token-parser symbol ;
|
2008-03-21 08:59:16 -04:00
|
|
|
! M: token-parser equal? eq? ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
MATCH-VARS: ?token ;
|
2007-11-26 21:08:16 -05:00
|
|
|
|
2008-03-25 22:40:17 -04:00
|
|
|
: parse-token ( input string -- result )
|
|
|
|
#! Parse the string, returning a parse result
|
|
|
|
2dup head? [
|
|
|
|
dup >r length tail-slice r> <parse-result>
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: token-parser (compile) ( parser -- quot )
|
2008-03-25 22:40:17 -04:00
|
|
|
token-parser-symbol [ parse-token ] curry ;
|
2007-12-20 19:16:14 -05:00
|
|
|
|
|
|
|
TUPLE: satisfy-parser quot ;
|
|
|
|
|
|
|
|
MATCH-VARS: ?quot ;
|
|
|
|
|
|
|
|
: satisfy-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup empty? [
|
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
unclip-slice dup ?quot call [
|
|
|
|
<parse-result>
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: satisfy-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
satisfy-parser-quot \ ?quot satisfy-pattern match-replace ;
|
2007-11-26 21:08:16 -05:00
|
|
|
|
2007-11-19 22:36:38 -05:00
|
|
|
TUPLE: range-parser min max ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
MATCH-VARS: ?min ?max ;
|
|
|
|
|
|
|
|
: range-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup empty? [
|
|
|
|
drop f
|
2007-11-19 22:36:38 -05:00
|
|
|
] [
|
2007-12-20 19:16:14 -05:00
|
|
|
0 over nth dup
|
|
|
|
?min ?max between? [
|
|
|
|
[ 1 tail-slice ] dip <parse-result>
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: range-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
T{ range-parser _ ?min ?max } range-pattern match-replace ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
|
|
|
TUPLE: seq-parser parsers ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: seq-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup [
|
2008-03-20 10:05:21 -04:00
|
|
|
dup parse-result-remaining ?quot [
|
2007-12-20 19:16:14 -05:00
|
|
|
[ parse-result-remaining swap set-parse-result-remaining ] 2keep
|
|
|
|
parse-result-ast dup ignore = [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
swap [ parse-result-ast push ] keep
|
|
|
|
] if
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if*
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if
|
|
|
|
] ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: seq-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
2008-03-20 10:05:21 -04:00
|
|
|
seq-parser-parsers [ compiled-parser \ ?quot seq-pattern match-replace % ] each
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2007-11-19 23:58:11 -05:00
|
|
|
TUPLE: choice-parser parsers ;
|
2007-12-20 19:16:14 -05:00
|
|
|
|
|
|
|
: choice-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup [
|
|
|
|
|
2007-11-19 23:58:11 -05:00
|
|
|
] [
|
2008-03-20 10:05:21 -04:00
|
|
|
drop dup ?quot
|
2007-12-20 19:16:14 -05:00
|
|
|
] if
|
|
|
|
] ;
|
2007-11-19 23:58:11 -05:00
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: choice-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
f ,
|
2008-03-20 10:05:21 -04:00
|
|
|
choice-parser-parsers [ compiled-parser \ ?quot choice-pattern match-replace % ] each
|
2007-12-20 19:16:14 -05:00
|
|
|
\ nip ,
|
|
|
|
] [ ] make ;
|
2007-11-19 23:58:11 -05:00
|
|
|
|
2007-11-20 21:01:44 -05:00
|
|
|
TUPLE: repeat0-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: (repeat0) ( quot result -- result )
|
|
|
|
2dup parse-result-remaining swap call [
|
2007-11-20 21:01:44 -05:00
|
|
|
[ parse-result-remaining swap set-parse-result-remaining ] 2keep
|
2007-11-20 21:31:23 -05:00
|
|
|
parse-result-ast swap [ parse-result-ast push ] keep
|
2007-12-20 19:16:14 -05:00
|
|
|
(repeat0)
|
2007-11-20 21:01:44 -05:00
|
|
|
] [
|
|
|
|
nip
|
2007-12-20 19:16:14 -05:00
|
|
|
] if* ; inline
|
2007-11-20 21:01:44 -05:00
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: repeat0-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
[ ?quot ] swap (repeat0)
|
2007-12-20 19:16:14 -05:00
|
|
|
] ;
|
2007-11-20 21:01:44 -05:00
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: repeat0-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
2008-03-20 10:05:21 -04:00
|
|
|
repeat0-parser-p1 compiled-parser \ ?quot repeat0-pattern match-replace %
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-20 21:01:44 -05:00
|
|
|
|
|
|
|
TUPLE: repeat1-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: repeat1-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
[ ?quot ] swap (repeat0) [
|
2007-12-20 19:16:14 -05:00
|
|
|
dup parse-result-ast empty? [
|
|
|
|
drop f
|
|
|
|
] when
|
|
|
|
] [
|
|
|
|
f
|
|
|
|
] if*
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: repeat1-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
2008-03-20 10:05:21 -04:00
|
|
|
repeat1-parser-p1 compiled-parser \ ?quot repeat1-pattern match-replace %
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-20 21:01:44 -05:00
|
|
|
|
2007-11-20 21:50:47 -05:00
|
|
|
TUPLE: optional-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: optional-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
dup ?quot swap f <parse-result> or
|
2007-12-20 19:16:14 -05:00
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: optional-parser (compile) ( parser -- quot )
|
|
|
|
optional-parser-p1 compiled-parser \ ?quot optional-pattern match-replace ;
|
2007-11-20 21:50:47 -05:00
|
|
|
|
2007-11-20 22:06:02 -05:00
|
|
|
TUPLE: ensure-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: ensure-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
dup ?quot [
|
2007-12-20 19:16:14 -05:00
|
|
|
ignore <parse-result>
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: ensure-parser (compile) ( parser -- quot )
|
|
|
|
ensure-parser-p1 compiled-parser \ ?quot ensure-pattern match-replace ;
|
2007-11-20 22:06:02 -05:00
|
|
|
|
2007-11-20 22:11:49 -05:00
|
|
|
TUPLE: ensure-not-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: ensure-not-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
dup ?quot [
|
2007-12-20 19:16:14 -05:00
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
ignore <parse-result>
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: ensure-not-parser (compile) ( parser -- quot )
|
|
|
|
ensure-not-parser-p1 compiled-parser \ ?quot ensure-not-pattern match-replace ;
|
2007-11-20 22:11:49 -05:00
|
|
|
|
2007-11-20 22:21:23 -05:00
|
|
|
TUPLE: action-parser p1 quot ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
MATCH-VARS: ?action ;
|
|
|
|
|
|
|
|
: action-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
?quot dup [
|
2007-12-20 19:16:14 -05:00
|
|
|
dup parse-result-ast ?action call
|
|
|
|
swap [ set-parse-result-ast ] keep
|
|
|
|
] when
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: action-parser (compile) ( parser -- quot )
|
|
|
|
{ action-parser-p1 action-parser-quot } get-slots [ compiled-parser ] dip
|
2007-12-20 19:16:14 -05:00
|
|
|
2array { ?quot ?action } action-pattern match-replace ;
|
2007-11-20 22:21:23 -05:00
|
|
|
|
2007-11-26 21:36:26 -05:00
|
|
|
: left-trim-slice ( string -- string )
|
|
|
|
#! Return a new string without any leading whitespace
|
|
|
|
#! from the original string.
|
|
|
|
dup empty? [
|
|
|
|
dup first blank? [ 1 tail-slice left-trim-slice ] when
|
|
|
|
] unless ;
|
|
|
|
|
|
|
|
TUPLE: sp-parser p1 ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: sp-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
\ left-trim-slice , sp-parser-p1 compiled-parser ,
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-26 21:36:26 -05:00
|
|
|
|
2007-11-26 22:16:21 -05:00
|
|
|
TUPLE: delay-parser quot ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: delay-parser (compile) ( parser -- quot )
|
2008-03-21 08:59:16 -04:00
|
|
|
#! For efficiency we memoize the quotation.
|
|
|
|
#! This way it is run only once and the
|
|
|
|
#! parser constructed once at run time.
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
2008-03-21 08:59:16 -04:00
|
|
|
delay-parser-quot % \ compile ,
|
|
|
|
] [ ] make
|
|
|
|
{ } { "word" } <effect> memoize-quot
|
|
|
|
[ % \ execute , ] [ ] make ;
|
2007-11-26 22:16:21 -05:00
|
|
|
|
2007-11-26 18:22:33 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: token ( string -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
token-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2007-11-26 21:08:16 -05:00
|
|
|
: satisfy ( quot -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
satisfy-parser construct-boa ;
|
2007-11-26 21:08:16 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: range ( min max -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
range-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
|
|
|
: seq ( seq -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
seq-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-02-26 16:17:17 -05:00
|
|
|
: 2seq ( parser1 parser2 -- parser )
|
|
|
|
2array seq ;
|
|
|
|
|
|
|
|
: 3seq ( parser1 parser2 parser3 -- parser )
|
|
|
|
3array seq ;
|
|
|
|
|
2008-03-03 14:28:53 -05:00
|
|
|
: 4seq ( parser1 parser2 parser3 parser4 -- parser )
|
|
|
|
4array seq ;
|
|
|
|
|
2008-02-13 16:39:37 -05:00
|
|
|
: seq* ( quot -- paser )
|
|
|
|
{ } make seq ; inline
|
|
|
|
|
2007-11-26 18:22:33 -05:00
|
|
|
: choice ( seq -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
choice-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-02-26 18:13:15 -05:00
|
|
|
: 2choice ( parser1 parser2 -- parser )
|
|
|
|
2array choice ;
|
|
|
|
|
|
|
|
: 3choice ( parser1 parser2 parser3 -- parser )
|
|
|
|
3array choice ;
|
|
|
|
|
2008-03-03 14:28:53 -05:00
|
|
|
: 4choice ( parser1 parser2 parser3 parser4 -- parser )
|
|
|
|
4array choice ;
|
|
|
|
|
2008-02-13 16:39:37 -05:00
|
|
|
: choice* ( quot -- paser )
|
|
|
|
{ } make choice ; inline
|
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: repeat0 ( parser -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
repeat0-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: repeat1 ( parser -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
repeat1-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: optional ( parser -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
optional-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: ensure ( parser -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
ensure-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: ensure-not ( parser -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
ensure-not-parser construct-boa ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2007-11-20 22:21:23 -05:00
|
|
|
: action ( parser quot -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
action-parser construct-boa ;
|
2007-11-26 21:36:26 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: sp ( parser -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
sp-parser construct-boa ;
|
2007-11-26 21:45:00 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: hide ( parser -- parser )
|
2007-11-26 21:45:00 -05:00
|
|
|
[ drop ignore ] action ;
|
2007-11-26 22:16:21 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: delay ( quot -- parser )
|
2008-03-21 08:59:16 -04:00
|
|
|
delay-parser construct-boa ;
|
2008-03-03 17:57:30 -05:00
|
|
|
|
|
|
|
: PEG:
|
|
|
|
(:) [
|
|
|
|
[
|
2008-03-21 07:58:53 -04:00
|
|
|
call compile 1quotation
|
2008-03-03 17:57:30 -05:00
|
|
|
[ dup [ parse-result-ast ] [ "Parse failed" throw ] if ]
|
|
|
|
append define
|
|
|
|
] with-compilation-unit
|
|
|
|
] 2curry over push-all ; parsing
|