1 ! Copyright (C) 2007 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: kernel tools.test strings namespaces arrays sequences
5 peg peg.private accessors words math accessors ;
9 "endbegin" "begin" token parse
13 "beginend" "begin" token (parse)
14 [ ast>> ] [ remaining>> ] bi
19 "" CHAR: a CHAR: z range parse
23 "1bcd" CHAR: a CHAR: z range parse
27 "abcd" CHAR: a CHAR: z range parse
31 "zbcd" CHAR: a CHAR: z range parse
35 "bad" "a" token "b" token 2array seq parse
39 "good" "g" token "o" token 2array seq parse
43 "abcd" "a" token "b" token 2array choice parse
47 "bbcd" "a" token "b" token 2array choice parse
51 "cbcd" "a" token "b" token 2array choice parse
55 "" "a" token "b" token 2array choice parse
59 "" "a" token repeat0 parse length
63 "b" "a" token repeat0 parse length
66 { V{ "a" "a" "a" } } [
67 "aaab" "a" token repeat0 parse
71 "" "a" token repeat1 parse
75 "b" "a" token repeat1 parse
78 { V{ "a" "a" "a" } } [
79 "aaab" "a" token repeat1 parse
83 "ab" "a" token optional "b" token 2array seq parse
87 "b" "a" token optional "b" token 2array seq parse
91 "cb" "a" token optional "b" token 2array seq parse
94 { V{ CHAR: a CHAR: b } } [
95 "ab" "a" token ensure CHAR: a CHAR: z range dup 3array seq parse
99 "bb" "a" token ensure CHAR: a CHAR: z range 2array seq parse
104 "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
110 "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
116 "a" token "+" token "++" token 2array choice "b" token 3array seq
122 "a" token "+" token "++" token 2array choice "b" token 3array seq
127 "a" "a" token [ drop 1 ] action parse
131 "aa" "a" token [ drop 1 ] action dup 2array seq parse
135 "b" "a" token [ drop 1 ] action parse
139 "b" [ CHAR: a = ] satisfy parse
143 "a" [ CHAR: a = ] satisfy parse
147 " a" "a" token sp parse
151 "a" "a" token sp parse
155 "[a]" "[" token hide "a" token "]" token hide 3array seq parse
159 "a]" "[" token hide "a" token "]" token hide 3array seq parse
163 { V{ "1" "-" "1" } V{ "1" "+" "1" } } [
165 [ "1" token , "-" token , "1" token , ] seq* ,
166 [ "1" token , "+" token , "1" token , ] seq* ,
168 "1-1" over parse swap
173 #! Test direct left recursion. Currently left recursion should cause a
174 #! failure of that parser.
175 [ expr ] delay "+" token "1" token 3seq "1" token 2choice ;
177 { V{ V{ "1" "+" "1" } "+" "1" } } [
182 #! Ensure a circular parser doesn't loop infinitely
183 [ f , "a" token , ] seq*
185 dupd 0 swap set-nth compile word?
189 "A" [ drop t ] satisfy [ 66 >= ] semantic parse
193 "B" [ drop t ] satisfy [ 66 >= ] semantic parse
196 { f } [ \ + T{ parser f f f } equal? ] unit-test