factor/extra/peg/peg.factor

476 lines
11 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
2008-03-26 18:23:58 -04:00
words quotations effects memoize accessors
combinators.cleave locals ;
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-26 18:23:58 -04:00
SYMBOL: packrat
2008-03-20 10:05:21 -04:00
: compiled-parsers ( -- cache )
\ compiled-parsers get-global [ H{ } clone dup \ compiled-parsers set-global ] unless* ;
: reset-compiled-parsers ( -- )
H{ } clone \ compiled-parsers set-global ;
2008-03-20 10:05:21 -04:00
GENERIC: (compile) ( parser -- quot )
2008-03-27 00:24:05 -04:00
: input-from ( input -- n )
#! Return the index from the original string that the
#! input slice is based on.
dup slice? [ slice-from ] [ drop 0 ] if ;
: input-cache ( id -- cache )
2008-03-27 00:45:59 -04:00
#! From the packrat cache, obtain the cache for the parser quotation
#! that maps the input string position to the parser result.
packrat get [ drop H{ } clone ] cache ;
2008-03-27 00:45:59 -04:00
TUPLE: left-recursion detected? ;
C: <left-recursion> left-recursion
USE: prettyprint
2008-03-27 20:40:26 -04:00
USE: io
:: handle-left-recursive-result ( result -- result )
#! If the result is from a left-recursive call,
#! note this and fail, otherwise return normal result
#! See figure 4 of packrat_TR-2007-002.pdf.
2008-03-27 20:40:26 -04:00
">>handle-left-recursive-result " write result .
result [
[let* | ast [ result ast>> ] |
ast left-recursion? [ t ast (>>detected?) f ] [ result ] if
]
] [
f
2008-03-27 20:40:26 -04:00
] if
"<<handle-left-recursive-result " write dup . ;
2008-03-27 20:40:26 -04:00
:: grow-lr ( input quot parser m -- result )
#! 'Grow the Seed' algorithm to handle left recursion
2008-03-27 20:40:26 -04:00
">>grow-lr " write input . " for parser " write parser . " m is " write m .
[let* | ans [ input quot call ] |
[ ans not ] [ ans [ ans remaining>> input-from m remaining>> input-from <= ] [ f ] if ] 2array || [
"recursion exiting with = " write ans . "m was " write m .
ans
] [
"recursion with = " write ans .
input quot ans grow-lr
] if
2008-03-27 20:40:26 -04:00
]
"<<grow-lr " write input . " for parser " write parser . " m is " write m . " result is " write dup .
;
2008-03-27 20:40:26 -04:00
:: cached-result ( input-cache input quot parser -- result )
#! Get the cached result for input position
2008-03-27 00:45:59 -04:00
#! from the input cache. If the item is not in the cache,
#! call 'quot' with 'input' on the stack to get the result
#! and store that in the cache and return it.
#! See figure 4 of packrat_TR-2007-002.pdf.
2008-03-27 20:40:26 -04:00
">>cached-result " write input . " for parser " write parser .
input input-from input-cache [
2008-03-27 06:54:34 -04:00
drop
[let* | lr [ f <left-recursion> ]
m [ input lr <parse-result> ]
ans [ m input input-from input-cache set-at input quot call ]
|
2008-03-27 20:40:26 -04:00
"--lr is " write lr . " ans is " write ans . " for parser " write parser .
ans input input-from input-cache set-at
lr detected?>> ans and [
2008-03-27 20:40:26 -04:00
input quot parser ans grow-lr
] [
ans
] if
]
] cache
2008-03-27 20:40:26 -04:00
dup [ handle-left-recursive-result ] when
"<<cached-result " write dup . " for parser " write parser . ;
2008-03-27 00:45:59 -04:00
2008-03-27 20:40:26 -04:00
:: run-packrat-parser ( input quot parser -- result )
parser id>> input-cache
input quot parser cached-result ; inline
2008-03-26 18:23:58 -04:00
2008-03-26 00:38:30 -04:00
: run-parser ( input quot -- result )
2008-03-26 18:23:58 -04:00
#! If a packrat cache is available, use memoization for
#! packrat parsing, otherwise do a standard peg call.
packrat get [ run-packrat-parser ] [ call ] if* ; inline
2008-03-26 00:38:30 -04:00
:: parser-body ( parser -- quot )
#! Return the body of the word that is the compiled version
#! of the parser.
[let* | parser-quot [ parser (compile) ]
|
[
packrat get [
2008-03-27 20:40:26 -04:00
parser-quot parser run-packrat-parser
] [
parser-quot call
] if
]
] ;
2008-03-20 10:05:21 -04:00
: 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.
2008-03-27 21:10:33 -04:00
#! Circular parsers are supported by getting the word
#! name and storing it in the cache, before compiling,
#! so it is picked up when re-entered.
dup id>> compiled-parsers [
drop dup gensym swap 2dup id>> compiled-parsers set-at
2dup parser-body define
dupd "peg" set-word-prop
] cache nip ;
2008-03-20 10:05:21 -04:00
: compile ( parser -- word )
[ compiled-parser ] with-compilation-unit ;
2008-03-20 10:05:21 -04:00
: parse ( state parser -- result )
2008-03-27 07:04:08 -04:00
compile execute ; inline
2008-03-20 10:05:21 -04:00
: with-packrat ( quot -- result )
#! Run the quotation with a packrat cache active.
2008-03-27 07:04:08 -04:00
[ H{ } clone packrat ] dip with-variable ; inline
: packrat-parse ( state parser -- result )
[ parse ] with-packrat ;
: packrat-call ( state quot -- result )
with-packrat ; inline
2007-11-28 18:35:45 -05:00
<PRIVATE
SYMBOL: id
: next-id ( -- n )
#! Return the next unique id for a parser
id get-global [
dup 1+ id set-global
] [
1 id set-global 0
] if* ;
TUPLE: parser id ;
M: parser equal? [ id>> ] 2apply = ;
C: <parser> parser
: delegates ( -- cache )
\ delegates get-global [ H{ } clone dup \ delegates set-global ] unless* ;
: reset-delegates ( -- )
H{ } clone \ delegates set-global ;
: init-parser ( parser -- parser )
#! Set the delegate for the parser. Equivalent parsers
#! get a delegate with the same id.
dup clone delegates [
drop next-id <parser>
] cache over set-delegate ;
2007-11-19 22:36:38 -05:00
TUPLE: token-parser symbol ;
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 )
2008-03-25 23:08:14 -04:00
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 )
2008-03-25 23:08:14 -04:00
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-25 23:08:14 -04:00
dup remaining>> ?quot [
[ remaining>> swap (>>remaining) ] 2keep
ast>> dup ignore = [
drop
] [
2008-03-25 23:08:14 -04:00
swap [ 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-25 23:08:14 -04:00
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-25 23:08:14 -04:00
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 )
2008-03-25 23:08:14 -04:00
2dup remaining>> swap call [
[ remaining>> swap (>>remaining) ] 2keep
ast>> swap [ 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-25 23:08:14 -04:00
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) [
2008-03-25 23:08:14 -04:00
dup 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-25 23:08:14 -04:00
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 )
2008-03-25 23:08:14 -04:00
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 )
2008-03-25 23:08:14 -04:00
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 )
2008-03-25 23:08:14 -04:00
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 [
2008-03-25 23:08:14 -04:00
dup ast>> ?action call
>>ast
] when
] ;
2008-03-20 10:05:21 -04:00
M: action-parser (compile) ( parser -- quot )
2008-03-25 23:21:33 -04:00
{ [ p1>> ] [ quot>> ] } cleave [ 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-25 23:08:14 -04:00
\ left-trim-slice , 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.
[
2008-03-25 23:08:14 -04:00
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 init-parser ;
2007-11-26 18:22:33 -05:00
: satisfy ( quot -- parser )
satisfy-parser construct-boa init-parser ;
2007-11-26 21:08:16 -05:00
: range ( min max -- parser )
range-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: seq ( seq -- parser )
seq-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: 2seq ( parser1 parser2 -- parser )
2008-02-26 16:17:17 -05:00
2array seq ;
: 3seq ( parser1 parser2 parser3 -- parser )
2008-02-26 16:17:17 -05:00
3array seq ;
: 4seq ( parser1 parser2 parser3 parser4 -- parser )
4array seq ;
2008-03-27 18:30:46 -04:00
: seq* ( quot -- paser )
2008-02-13 16:39:37 -05:00
{ } make seq ; inline
: choice ( seq -- parser )
choice-parser construct-boa init-parser ;
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-03-27 18:30:46 -04:00
: choice* ( quot -- paser )
2008-02-13 16:39:37 -05:00
{ } make choice ; inline
: repeat0 ( parser -- parser )
repeat0-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: repeat1 ( parser -- parser )
repeat1-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: optional ( parser -- parser )
optional-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: ensure ( parser -- parser )
ensure-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: ensure-not ( parser -- parser )
ensure-not-parser construct-boa init-parser ;
2007-11-26 18:22:33 -05:00
: action ( parser quot -- parser )
action-parser construct-boa init-parser ;
2007-11-26 21:36:26 -05:00
: sp ( parser -- parser )
sp-parser construct-boa init-parser ;
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 init-parser ;
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