! Copyright (C) 2007, 2008 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
+
USING: accessors arrays assocs classes combinators
combinators.short-circuit compiler.units effects.parser fry
generalizations kernel locals make math math.order namespaces
-quotations sequences sets splitting unicode vectors words ;
+quotations sequences sets splitting unicode vectors
+vocabs.loader words ;
+
IN: peg
TUPLE: parse-result remaining ast ;
} cond ;
: merge-errors ( -- )
- error-stack get dup length 1 > [
+ error-stack get dup length 1 > [
[ pop ] [ pop swap (merge-errors) ] [ ] tri push
] [
drop
swap >>ans pos get >>pos drop ;
: stop-growth? ( ast m -- ? )
- [ failed? pos get ] dip
- pos>> <= or ;
+ [ failed? pos get ] dip pos>> <= or ;
: setup-growth ( h p -- )
pos namespaces:set dup involved-set>> clone >>eval-set drop ;
! Wrap a parser tuple around the peg object.
! Look for an existing parser tuple for that
! peg object.
- peg-cache [
- f next-id parser boa
- ] cache ;
+ peg-cache [ f next-id parser boa ] cache ;
TUPLE: token-parser symbol ;
TUPLE: satisfy-parser quot ;
-: parse-satisfy ( input quot -- result )
- swap [
- drop f
- ] [
- unclip-slice dup roll call [
+:: parse-satisfy ( input quot -- result/f )
+ input [ f ] [
+ unclip-slice dup quot call [
<parse-result>
] [
2drop f
TUPLE: range-parser min max ;
-: parse-range ( input min max -- result )
- pick empty? [
- 3drop f
- ] [
- [ dup first ] 2dip between? [
+:: parse-range ( input min max -- result/f )
+ input [ f ] [
+ dup first min max between? [
unclip-slice <parse-result>
] [
drop f
] if
- ] if ;
+ ] if-empty ;
M: range-parser (compile)
[ min>> ] [ max>> ] bi '[ input-slice _ _ parse-range ] ;
TUPLE: seq-parser parsers ;
-: ignore? ( ast -- bool )
- ignore = ;
-
: calc-seq-result ( prev-result current-result -- next-result )
[
[ remaining>> swap remaining<< ] 2keep
- ast>> dup ignore? [
+ ast>> dup ignore = [
drop
] [
swap [ ast>> push ] keep
] if* ;
: parse-seq-element ( result quot -- result )
- over [
- call calc-seq-result
- ] [
- 2drop f
- ] if ; inline
+ '[ @ calc-seq-result ] [ f ] if* ; inline
M: seq-parser (compile)
[
TUPLE: repeat1-parser parser ;
: repeat1-empty-check ( result -- result )
- [
- dup ast>> empty? [ drop f ] when
- ] [
- f
- ] if* ;
+ [ dup ast>> empty? [ drop f ] when ] [ f ] if* ;
M: repeat1-parser (compile)
parser>> compile-parser-quot '[
TUPLE: optional-parser parser ;
: check-optional ( result -- result )
- [ input-slice f <parse-result> ] unless* ;
+ [ input-slice f <parse-result> ] unless* ;
M: optional-parser (compile)
- parser>> compile-parser-quot '[ @ check-optional ] ;
+ parser>> compile-parser-quot '[ @ check-optional ] ;
TUPLE: semantic-parser parser quot ;
: check-semantic ( result quot -- result )
- over [
- over ast>> swap call [ drop f ] unless
- ] [
- drop
- ] if ; inline
+ dupd '[ dup ast>> @ [ drop f ] unless ] when ; inline
M: semantic-parser (compile)
[ parser>> compile-parser-quot ] [ quot>> ] bi
TUPLE: action-parser parser quot ;
: check-action ( result quot -- result )
- over [
- over ast>> swap call( ast -- ast ) >>ast
- ] [
- drop
- ] if ;
+ dupd '[ [ _ call( ast -- ast ) ] change-ast ] when ;
M: action-parser (compile)
- [ parser>> compile-parser-quot ] [ quot>> ] bi '[ @ _ check-action ] ;
+ [ parser>> compile-parser-quot ] [ quot>> ] bi
+ '[ @ _ check-action ] ;
TUPLE: sp-parser parser ;
[
[
def call compile :> compiled-def
- [
+ word [
dup compiled-def compiled-parse
[ ast>> ] [ word parse-failed ] ?if
- ]
- word swap effect define-declared
+ ] effect define-declared
] with-compilation-unit
] append!
] ;
-USE: vocabs.loader
-
{ "debugger" "peg" } "peg.debugger" require-when
! Copyright (C) 2006 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators continuations io io.streams.string kernel
-math memoize namespaces peg sequences strings ;
+literals math namespaces peg sequences strings ;
IN: peg.search
: stream-tree-write ( object stream -- )
: tree-write ( object -- )
output-stream get stream-tree-write ;
-MEMO: any-char-parser ( -- parser )
- [ drop t ] satisfy ;
+<PRIVATE
+
+CONSTANT: any-char-parser $[ [ drop t ] satisfy ]
+
+PRIVATE>
: search ( string parser -- seq )
any-char-parser [ drop f ] action 2choice repeat0
[ parse sift ] [ 3drop { } ] recover ;
-: (replace) ( string parser -- seq )
- any-char-parser 2choice repeat0 parse sift ;
-
: replace ( string parser -- result )
- [ (replace) tree-write ] with-string-writer ;
+ [
+ any-char-parser 2choice repeat0
+ parse sift tree-write
+ ] with-string-writer ;