factor/extra/peg/peg.factor

264 lines
6.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
2007-11-29 05:49:34 -05:00
vectors arrays combinators.lib 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-28 18:35:45 -05:00
GENERIC: (parse) ( state parser -- result )
2007-11-26 18:22:33 -05:00
<PRIVATE
2007-11-28 18:35:45 -05:00
SYMBOL: packrat-cache
2007-11-26 18:22:33 -05:00
SYMBOL: ignore
2007-11-29 05:42:46 -05:00
SYMBOL: not-in-cache
: not-in-cache? ( result -- ? )
not-in-cache = ;
2007-11-26 18:22:33 -05:00
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 ;
SYMBOL: next-id
: get-next-id ( -- number )
next-id get-global 0 or dup 1+ next-id set-global ;
TUPLE: parser id ;
: init-parser ( parser -- parser )
get-next-id parser construct-boa over set-delegate ;
2007-11-28 18:35:45 -05:00
: from ( slice-or-string -- index )
dup slice? [ slice-from ] [ drop 0 ] if ;
: get-cached ( input parser -- result )
2007-11-29 05:42:46 -05:00
[ from ] dip parser-id packrat-cache get at at* [
drop not-in-cache
] unless ;
2007-11-28 18:35:45 -05:00
: put-cached ( result input parser -- )
parser-id dup packrat-cache get at [
nip
] [
H{ } clone dup >r swap packrat-cache get set-at r>
] if*
[ from ] dip set-at ;
PRIVATE>
: parse ( input parser -- result )
packrat-cache get [
2007-11-29 05:42:46 -05:00
2dup get-cached dup not-in-cache? [
! "cache missed: " write over parser-id number>string write " - " write nl ! pick .
drop [ (parse) dup ] 2keep put-cached
] [
! "cache hit: " write over parser-id number>string write " - " write nl ! pick .
2nip
] if
2007-11-28 18:35:45 -05:00
] [
(parse)
] if ;
: packrat-parse ( input parser -- result )
H{ } clone packrat-cache [ parse ] with-variable ;
<PRIVATE
2007-11-19 22:36:38 -05:00
TUPLE: token-parser symbol ;
2007-11-28 18:35:45 -05:00
M: token-parser (parse) ( input parser -- result )
2007-11-26 17:57:08 -05:00
token-parser-symbol 2dup head? [
dup >r length tail-slice r> <parse-result>
2007-11-19 22:36:38 -05:00
] [
2drop f
] if ;
2007-11-28 18:35:45 -05:00
2007-11-26 21:08:16 -05:00
TUPLE: satisfy-parser quot ;
2007-11-28 18:35:45 -05:00
M: satisfy-parser (parse) ( state parser -- result )
2007-11-26 21:08:16 -05:00
over empty? [
2drop f
] [
satisfy-parser-quot [ unclip-slice dup ] dip call [
<parse-result>
] [
2drop f
] if
] if ;
2007-11-19 22:36:38 -05:00
TUPLE: range-parser min max ;
2007-11-28 18:35:45 -05:00
M: range-parser (parse) ( state parser -- result )
2007-11-26 17:57:08 -05:00
over empty? [
2007-11-19 22:36:38 -05:00
2drop f
] [
2007-11-26 17:57:08 -05:00
0 pick nth dup rot
2007-11-19 22:36:38 -05:00
{ range-parser-min range-parser-max } get-slots between? [
2007-11-26 17:57:08 -05:00
[ 1 tail-slice ] dip <parse-result>
2007-11-19 22:36:38 -05:00
] [
2drop f
] if
] if ;
TUPLE: seq-parser parsers ;
: do-seq-parser ( result parser -- result )
[ dup parse-result-remaining ] dip parse [
[ parse-result-remaining swap set-parse-result-remaining ] 2keep
2007-11-20 22:06:02 -05:00
parse-result-ast dup ignore = [ drop ] [ swap [ parse-result-ast push ] keep ] if
2007-11-19 22:36:38 -05:00
] [
drop f
] if* ;
: (seq-parser) ( result parsers -- result )
dup empty? not pick and [
unclip swap [ do-seq-parser ] dip (seq-parser)
] [
drop
] if ;
2007-11-28 18:35:45 -05:00
M: seq-parser (parse) ( state parser -- result )
2007-11-20 21:31:23 -05:00
seq-parser-parsers [ V{ } clone <parse-result> ] dip (seq-parser) ;
2007-11-19 22:36:38 -05:00
2007-11-19 23:58:11 -05:00
TUPLE: choice-parser parsers ;
: (choice-parser) ( state parsers -- result )
dup empty? [
2drop f
] [
unclip pick swap parse [
2nip
] [
(choice-parser)
] if*
] if ;
2007-11-28 18:35:45 -05:00
M: choice-parser (parse) ( state parser -- result )
2007-11-19 23:58:11 -05:00
choice-parser-parsers (choice-parser) ;
2007-11-20 21:01:44 -05:00
TUPLE: repeat0-parser p1 ;
: (repeat-parser) ( parser result -- result )
2dup parse-result-remaining swap parse [
[ 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-11-20 21:01:44 -05:00
(repeat-parser)
] [
nip
] if* ;
: clone-result ( result -- result )
2007-11-20 21:31:23 -05:00
{ parse-result-remaining parse-result-ast }
get-slots 1vector <parse-result> ;
2007-11-20 21:01:44 -05:00
2007-11-28 18:35:45 -05:00
M: repeat0-parser (parse) ( state parser -- result )
2007-11-20 21:01:44 -05:00
repeat0-parser-p1 2dup parse [
nipd clone-result (repeat-parser)
] [
2007-11-20 21:31:23 -05:00
drop V{ } clone <parse-result>
2007-11-20 21:01:44 -05:00
] if* ;
TUPLE: repeat1-parser p1 ;
2007-11-28 18:35:45 -05:00
M: repeat1-parser (parse) ( state parser -- result )
2007-11-20 21:50:47 -05:00
repeat1-parser-p1 tuck parse dup [ clone-result (repeat-parser) ] [ nip ] if ;
2007-11-20 21:01:44 -05:00
2007-11-20 21:50:47 -05:00
TUPLE: optional-parser p1 ;
2007-11-28 18:35:45 -05:00
M: optional-parser (parse) ( state parser -- result )
2007-11-20 21:50:47 -05:00
dupd optional-parser-p1 parse swap f <parse-result> or ;
2007-11-20 22:06:02 -05:00
TUPLE: ensure-parser p1 ;
2007-11-28 18:35:45 -05:00
M: ensure-parser (parse) ( state parser -- result )
2007-11-20 22:06:02 -05:00
dupd ensure-parser-p1 parse [
ignore <parse-result>
] [
drop f
] if ;
2007-11-20 22:11:49 -05:00
TUPLE: ensure-not-parser p1 ;
2007-11-28 18:35:45 -05:00
M: ensure-not-parser (parse) ( state parser -- result )
2007-11-20 22:11:49 -05:00
dupd ensure-not-parser-p1 parse [
drop f
] [
ignore <parse-result>
] if ;
2007-11-20 22:21:23 -05:00
TUPLE: action-parser p1 quot ;
2007-11-28 18:35:45 -05:00
M: action-parser (parse) ( state parser -- result )
2007-11-20 22:21:23 -05:00
tuck action-parser-p1 parse dup [
dup parse-result-ast rot action-parser-quot call
swap [ set-parse-result-ast ] keep
] [
nip
] if ;
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 ;
2007-11-28 18:35:45 -05:00
M: sp-parser (parse) ( state parser -- result )
2007-11-26 21:36:26 -05:00
[ left-trim-slice ] dip sp-parser-p1 parse ;
2007-11-26 22:16:21 -05:00
TUPLE: delay-parser quot ;
2007-11-28 18:35:45 -05:00
M: delay-parser (parse) ( state parser -- result )
2007-11-26 22:16:21 -05:00
delay-parser-quot call parse ;
2007-11-26 18:22:33 -05:00
PRIVATE>
2007-11-29 05:49:34 -05:00
MEMO: token ( string -- parser )
2007-11-26 18:22:33 -05:00
token-parser construct-boa init-parser ;
2007-11-26 21:08:16 -05:00
: satisfy ( quot -- parser )
satisfy-parser construct-boa init-parser ;
2007-11-29 05:49:34 -05:00
MEMO: range ( min max -- parser )
2007-11-26 18:22:33 -05:00
range-parser construct-boa init-parser ;
: seq ( seq -- parser )
seq-parser construct-boa init-parser ;
: choice ( seq -- parser )
choice-parser construct-boa init-parser ;
2007-11-29 05:49:34 -05:00
MEMO: repeat0 ( parser -- parser )
2007-11-26 18:22:33 -05:00
repeat0-parser construct-boa init-parser ;
2007-11-29 05:49:34 -05:00
MEMO: repeat1 ( parser -- parser )
2007-11-26 18:22:33 -05:00
repeat1-parser construct-boa init-parser ;
2007-11-29 05:49:34 -05:00
MEMO: optional ( parser -- parser )
2007-11-26 18:22:33 -05:00
optional-parser construct-boa init-parser ;
2007-11-29 05:49:34 -05:00
MEMO: ensure ( parser -- parser )
2007-11-26 18:22:33 -05:00
ensure-parser construct-boa init-parser ;
2007-11-29 05:49:34 -05:00
MEMO: ensure-not ( parser -- parser )
2007-11-26 18:22:33 -05:00
ensure-not-parser construct-boa init-parser ;
2007-11-20 22:21:23 -05:00
: action ( parser quot -- parser )
action-parser construct-boa init-parser ;
2007-11-26 21:36:26 -05:00
2007-11-29 05:49:34 -05:00
MEMO: sp ( parser -- parser )
2007-11-26 21:36:26 -05:00
sp-parser construct-boa init-parser ;
2007-11-26 21:45:00 -05:00
2007-11-29 05:49:34 -05:00
MEMO: hide ( parser -- parser )
2007-11-26 21:45:00 -05:00
[ drop ignore ] action ;
2007-11-26 22:16:21 -05:00
2007-11-29 05:49:34 -05:00
MEMO: delay ( parser -- parser )
2007-11-26 22:16:21 -05:00
delay-parser construct-boa init-parser ;
2007-11-27 00:13:36 -05:00
2007-11-29 05:49:34 -05:00
MEMO: list-of ( items separator -- parser )
2007-11-27 00:13:36 -05:00
hide over 2array seq repeat0 [ concat ] action 2array seq [ unclip 1vector swap first append ] action ;