1 ! Copyright (C) 2007 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: kernel tools.test strings namespaces make arrays sequences
5 peg peg.private peg.parsers accessors words math accessors ;
10 [ ] [ reset-pegs ] unit-test
13 "endbegin" "begin" token parse
17 "beginend" "begin" token (parse)
18 [ ast>> ] [ remaining>> ] bi
23 "" CHAR: a CHAR: z range parse
27 "1bcd" CHAR: a CHAR: z range parse
31 "abcd" CHAR: a CHAR: z range parse
35 "zbcd" CHAR: a CHAR: z range parse
39 "bad" "a" token "b" token 2array seq parse
43 "good" "g" token "o" token 2array seq parse
47 "abcd" "a" token "b" token 2array choice parse
51 "bbcd" "a" token "b" token 2array choice parse
55 "cbcd" "a" token "b" token 2array choice parse
59 "" "a" token "b" token 2array choice parse
63 "" "a" token repeat0 parse length
67 "b" "a" token repeat0 parse length
70 { V{ "a" "a" "a" } } [
71 "aaab" "a" token repeat0 parse
75 "" "a" token repeat1 parse
79 "b" "a" token repeat1 parse
82 { V{ "a" "a" "a" } } [
83 "aaab" "a" token repeat1 parse
87 "ab" "a" token optional "b" token 2array seq parse
91 "b" "a" token optional "b" token 2array seq parse
95 "cb" "a" token optional "b" token 2array seq parse
98 { V{ CHAR: a CHAR: b } } [
99 "ab" "a" token ensure CHAR: a CHAR: z range dup 3array seq parse
103 "bb" "a" token ensure CHAR: a CHAR: z range 2array seq parse
108 "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
114 "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
120 "a" token "+" token "++" token 2array choice "b" token 3array seq
126 "a" token "+" token "++" token 2array choice "b" token 3array seq
131 "a" "a" token [ drop 1 ] action parse
135 "aa" "a" token [ drop 1 ] action dup 2array seq parse
139 "b" "a" token [ drop 1 ] action parse
143 "b" [ CHAR: a = ] satisfy parse
147 "a" [ CHAR: a = ] satisfy parse
151 " a" "a" token sp parse
155 "a" "a" token sp parse
159 "[a]" "[" token hide "a" token "]" token hide 3array seq parse
163 "a]" "[" token hide "a" token "]" token hide 3array seq parse
167 { V{ "1" "-" "1" } V{ "1" "+" "1" } } [
169 [ "1" token , "-" token , "1" token , ] seq* ,
170 [ "1" token , "+" token , "1" token , ] seq* ,
172 "1-1" over parse swap
177 #! Test direct left recursion. Currently left recursion should cause a
178 #! failure of that parser.
179 [ expr ] delay "+" token "1" token 3seq "1" token 2choice ;
181 { V{ V{ "1" "+" "1" } "+" "1" } } [
186 #! Ensure a circular parser doesn't loop infinitely
187 [ f , "a" token , ] seq*
189 dupd 0 swap set-nth compile word?
193 "A" [ drop t ] satisfy [ 66 >= ] semantic parse
197 "B" [ drop t ] satisfy [ 66 >= ] semantic parse
200 { f } [ \ + T{ parser f f f } equal? ] unit-test
204 [ ] [ disable-compiler ] unit-test
206 [ ] [ "" epsilon parse drop ] unit-test
208 [ ] [ enable-compiler ] unit-test
210 [ [ ] ] [ "" epsilon [ drop [ [ ] ] call ] action parse ] unit-test
212 [ [ ] ] [ "" epsilon [ drop [ [ ] ] ] action [ call ] action parse ] unit-test