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 ;
8 [ ] [ reset-pegs ] unit-test
11 "endbegin" "begin" token parse
15 "beginend" "begin" token (parse)
16 [ ast>> ] [ remaining>> ] bi
21 "" CHAR: a CHAR: z range parse
25 "1bcd" CHAR: a CHAR: z range parse
29 "abcd" CHAR: a CHAR: z range parse
33 "zbcd" CHAR: a CHAR: z range parse
37 "bad" "a" token "b" token 2array seq parse
41 "good" "g" token "o" token 2array seq parse
45 "abcd" "a" token "b" token 2array choice parse
49 "bbcd" "a" token "b" token 2array choice parse
53 "cbcd" "a" token "b" token 2array choice parse
57 "" "a" token "b" token 2array choice parse
61 "" "a" token repeat0 parse length
65 "b" "a" token repeat0 parse length
68 { V{ "a" "a" "a" } } [
69 "aaab" "a" token repeat0 parse
73 "" "a" token repeat1 parse
77 "b" "a" token repeat1 parse
80 { V{ "a" "a" "a" } } [
81 "aaab" "a" token repeat1 parse
85 "ab" "a" token optional "b" token 2array seq parse
89 "b" "a" token optional "b" token 2array seq parse
93 "cb" "a" token optional "b" token 2array seq parse
96 { V{ CHAR: a CHAR: b } } [
97 "ab" "a" token ensure CHAR: a CHAR: z range dup 3array seq parse
101 "bb" "a" token ensure CHAR: a CHAR: z range 2array seq parse
106 "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
112 "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
118 "a" token "+" token "++" token 2array choice "b" token 3array seq
124 "a" token "+" token "++" token 2array choice "b" token 3array seq
129 "a" "a" token [ drop 1 ] action parse
133 "aa" "a" token [ drop 1 ] action dup 2array seq parse
137 "b" "a" token [ drop 1 ] action parse
141 "b" [ CHAR: a = ] satisfy parse
145 "a" [ CHAR: a = ] satisfy parse
149 " a" "a" token sp parse
153 "a" "a" token sp parse
157 "[a]" "[" token hide "a" token "]" token hide 3array seq parse
161 "a]" "[" token hide "a" token "]" token hide 3array seq parse
165 { V{ "1" "-" "1" } V{ "1" "+" "1" } } [
167 [ "1" token , "-" token , "1" token , ] seq* ,
168 [ "1" token , "+" token , "1" token , ] seq* ,
170 "1-1" over parse swap
175 #! Test direct left recursion. Currently left recursion should cause a
176 #! failure of that parser.
177 [ expr ] delay "+" token "1" token 3seq "1" token 2choice ;
179 { V{ V{ "1" "+" "1" } "+" "1" } } [
184 #! Ensure a circular parser doesn't loop infinitely
185 [ f , "a" token , ] seq*
187 dupd 0 swap set-nth compile word?
191 "A" [ drop t ] satisfy [ 66 >= ] semantic parse
195 "B" [ drop t ] satisfy [ 66 >= ] semantic parse
198 { f } [ \ + T{ parser f f f } equal? ] unit-test
202 [ ] [ disable-compiler ] unit-test
204 [ ] [ "" epsilon parse drop ] unit-test
206 [ ] [ enable-compiler ] unit-test
208 [ [ ] ] [ "" epsilon [ drop [ [ ] ] call ] action parse ] unit-test
210 [ [ ] ] [ "" epsilon [ drop [ [ ] ] ] action [ call ] action parse ] unit-test