factor/extra/peg/peg.factor

369 lines
8.0 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-26 18:23:58 -04:00
:: run-packrat-parser ( input quot c -- result )
input slice? [ input slice-from ] [ 0 ] if
quot c [ drop H{ } clone ] cache
[
drop input quot call
2008-03-26 19:58:53 -04:00
] cache ; 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
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.
compiled-parsers [
2008-03-26 21:55:14 -04:00
(compile) [ run-parser ] curry define-temp
] cache ;
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-21 07:58:53 -04:00
compile execute ;
2008-03-20 10:05:21 -04:00
: with-packrat ( quot -- result )
#! Run the quotation with a packrat cache active.
[ H{ } clone packrat ] dip with-variable ;
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? 2drop f ;
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 )
2008-03-25 23:08:14 -04:00
symbol>> [ parse-token ] curry ;
TUPLE: satisfy-parser quot ;
M: satisfy-parser equal? 2drop f ;
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 ;
M: range-parser equal? 2drop f ;
2007-11-19 22:36:38 -05:00
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 ;
M: seq-parser equal? 2drop f ;
2007-11-19 22:36:38 -05:00
: 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 ;
M: choice-parser equal? 2drop f ;
: 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 ;
M: repeat0-parser equal? 2drop f ;
2007-11-20 21:01:44 -05:00
: (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 ;
M: repeat1-parser equal? 2drop f ;
2007-11-20 21:01:44 -05:00
: 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 ;
M: optional-parser equal? 2drop f ;
2007-11-20 21:50:47 -05:00
: 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 ;
M: ensure-parser equal? 2drop f ;
2007-11-20 22:06:02 -05:00
: 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 ;
M: ensure-not-parser equal? 2drop f ;
2007-11-20 22:11:49 -05:00
: 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 ;
M: action-parser equal? 2drop f ;
2007-11-20 22:21:23 -05:00
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 ;
M: sp-parser equal? 2drop f ;
2007-11-26 21:36:26 -05:00
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 ;
M: delay-parser equal? 2drop f ;
2007-11-26 22:16:21 -05:00
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>
MEMO: token ( string -- parser )
token-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: satisfy ( quot -- parser )
satisfy-parser construct-boa ;
2007-11-26 21:08:16 -05:00
MEMO: range ( min max -- parser )
range-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: seq ( seq -- parser )
seq-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: 2seq ( parser1 parser2 -- parser )
2008-02-26 16:17:17 -05:00
2array seq ;
MEMO: 3seq ( parser1 parser2 parser3 -- parser )
2008-02-26 16:17:17 -05:00
3array seq ;
MEMO: 4seq ( parser1 parser2 parser3 parser4 -- parser )
4array seq ;
MEMO: seq* ( quot -- paser )
2008-02-13 16:39:37 -05:00
{ } make seq ; inline
MEMO: choice ( seq -- parser )
choice-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: 2choice ( parser1 parser2 -- parser )
2array choice ;
MEMO: 3choice ( parser1 parser2 parser3 -- parser )
3array choice ;
MEMO: 4choice ( parser1 parser2 parser3 parser4 -- parser )
4array choice ;
MEMO: choice* ( quot -- paser )
2008-02-13 16:39:37 -05:00
{ } make choice ; inline
MEMO: repeat0 ( parser -- parser )
repeat0-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: repeat1 ( parser -- parser )
repeat1-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: optional ( parser -- parser )
optional-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: ensure ( parser -- parser )
ensure-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: ensure-not ( parser -- parser )
ensure-not-parser construct-boa ;
2007-11-26 18:22:33 -05:00
MEMO: action ( parser quot -- parser )
action-parser construct-boa ;
2007-11-26 21:36:26 -05:00
MEMO: 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
MEMO: 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