factor/extra/peg/peg.factor

339 lines
7.2 KiB
Factor
Raw Normal View History

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
vectors arrays combinators.lib math.parser match
2008-03-03 17:57:30 -05:00
unicode.categories sequences.lib compiler.units parser
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 )
#! 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* ;
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 ;
! M: token-parser equal? eq? ;
2007-11-19 22:36:38 -05:00
MATCH-VARS: ?token ;
2007-11-26 21:08:16 -05: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 )
token-parser-symbol [ parse-token ] curry ;
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 )
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 ;
MATCH-VARS: ?min ?max ;
: range-pattern ( -- quot )
[
dup empty? [
drop f
2007-11-19 22:36:38 -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 )
T{ range-parser _ ?min ?max } range-pattern match-replace ;
2007-11-19 22:36:38 -05:00
TUPLE: seq-parser parsers ;
: seq-pattern ( -- quot )
[
dup [
2008-03-20 10:05:21 -04:00
dup parse-result-remaining ?quot [
[ 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 )
[
[ V{ } clone <parse-result> ] %
2008-03-20 10:05:21 -04:00
seq-parser-parsers [ compiled-parser \ ?quot seq-pattern match-replace % ] each
] [ ] make ;
2007-11-19 22:36:38 -05:00
2007-11-19 23:58:11 -05:00
TUPLE: choice-parser parsers ;
: choice-pattern ( -- quot )
[
dup [
2007-11-19 23:58:11 -05:00
] [
2008-03-20 10:05:21 -04:00
drop dup ?quot
] if
] ;
2007-11-19 23:58:11 -05:00
2008-03-20 10:05:21 -04:00
M: choice-parser (compile) ( parser -- quot )
[
f ,
2008-03-20 10:05:21 -04:00
choice-parser-parsers [ compiled-parser \ ?quot choice-pattern match-replace % ] each
\ nip ,
] [ ] make ;
2007-11-19 23:58:11 -05:00
2007-11-20 21:01:44 -05:00
TUPLE: repeat0-parser p1 ;
: (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
(repeat0)
2007-11-20 21:01:44 -05:00
] [
nip
] if* ; inline
2007-11-20 21:01:44 -05:00
: repeat0-pattern ( -- quot )
[
2008-03-20 10:05:21 -04:00
[ ?quot ] swap (repeat0)
] ;
2007-11-20 21:01:44 -05:00
2008-03-20 10:05:21 -04:00
M: repeat0-parser (compile) ( parser -- quot )
[
[ V{ } clone <parse-result> ] %
2008-03-20 10:05:21 -04:00
repeat0-parser-p1 compiled-parser \ ?quot repeat0-pattern match-replace %
] [ ] make ;
2007-11-20 21:01:44 -05:00
TUPLE: repeat1-parser p1 ;
: repeat1-pattern ( -- quot )
[
2008-03-20 10:05:21 -04:00
[ ?quot ] swap (repeat0) [
dup parse-result-ast empty? [
drop f
] when
] [
f
] if*
] ;
2008-03-20 10:05:21 -04:00
M: repeat1-parser (compile) ( parser -- quot )
[
[ V{ } clone <parse-result> ] %
2008-03-20 10:05:21 -04:00
repeat1-parser-p1 compiled-parser \ ?quot repeat1-pattern match-replace %
] [ ] make ;
2007-11-20 21:01:44 -05:00
2007-11-20 21:50:47 -05:00
TUPLE: optional-parser p1 ;
: optional-pattern ( -- quot )
[
2008-03-20 10:05:21 -04:00
dup ?quot swap f <parse-result> or
] ;
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 ;
: ensure-pattern ( -- quot )
[
2008-03-20 10:05:21 -04:00
dup ?quot [
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 ;
: ensure-not-pattern ( -- quot )
[
2008-03-20 10:05:21 -04:00
dup ?quot [
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 ;
MATCH-VARS: ?action ;
: action-pattern ( -- quot )
[
2008-03-20 10:05:21 -04:00
?quot dup [
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
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 )
[
2008-03-20 10:05:21 -04:00
\ left-trim-slice , sp-parser-p1 compiled-parser ,
] [ ] 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 )
#! 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 ,
] [ ] make
{ } { "word" } <effect> memoize-quot
[ % \ execute , ] [ ] make ;
2007-11-26 22:16:21 -05:00
2007-11-26 18:22:33 -05:00
PRIVATE>
: token ( string -- parser )
token-parser construct-boa ;
2007-11-26 18:22:33 -05:00
2007-11-26 21:08:16 -05:00
: satisfy ( quot -- parser )
satisfy-parser construct-boa ;
2007-11-26 21:08:16 -05:00
: range ( min max -- parser )
range-parser construct-boa ;
2007-11-26 18:22:33 -05:00
: seq ( seq -- parser )
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 ;
: 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 )
choice-parser construct-boa ;
2007-11-26 18:22:33 -05:00
: 2choice ( parser1 parser2 -- parser )
2array choice ;
: 3choice ( parser1 parser2 parser3 -- parser )
3array choice ;
: 4choice ( parser1 parser2 parser3 parser4 -- parser )
4array choice ;
2008-02-13 16:39:37 -05:00
: choice* ( quot -- paser )
{ } make choice ; inline
: repeat0 ( parser -- parser )
repeat0-parser construct-boa ;
2007-11-26 18:22:33 -05:00
: repeat1 ( parser -- parser )
repeat1-parser construct-boa ;
2007-11-26 18:22:33 -05:00
: optional ( parser -- parser )
optional-parser construct-boa ;
2007-11-26 18:22:33 -05:00
: ensure ( parser -- parser )
ensure-parser construct-boa ;
2007-11-26 18:22:33 -05:00
: ensure-not ( parser -- parser )
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 )
action-parser construct-boa ;
2007-11-26 21:36:26 -05:00
: sp ( parser -- parser )
sp-parser construct-boa ;
2007-11-26 21:45:00 -05:00
: hide ( parser -- parser )
2007-11-26 21:45:00 -05:00
[ drop ignore ] action ;
2007-11-26 22:16:21 -05:00
: delay ( quot -- parser )
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