]> gitweb.factorcode.org Git - factor.git/blob - basis/peg/peg-tests.factor
ogg plays but 1) sound is broken and 2) it doesn't recognize EOF anymore, so it hangs...
[factor.git] / basis / peg / peg-tests.factor
1 ! Copyright (C) 2007 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 !
4 USING: kernel tools.test strings namespaces arrays sequences 
5        peg peg.private accessors words math accessors ;
6 IN: peg.tests
7
8 [
9   "endbegin" "begin" token parse
10 ] must-fail
11
12 { "begin" "end" } [
13   "beginend" "begin" token (parse) 
14   [ ast>> ] [ remaining>> ] bi
15   >string
16 ] unit-test
17
18 [
19   "" CHAR: a CHAR: z range parse
20 ] must-fail
21
22 [
23   "1bcd" CHAR: a CHAR: z range parse
24 ] must-fail
25
26 { CHAR: a } [
27   "abcd" CHAR: a CHAR: z range parse
28 ] unit-test
29
30 { CHAR: z } [
31   "zbcd" CHAR: a CHAR: z range parse
32 ] unit-test
33
34 [
35   "bad" "a" token "b" token 2array seq parse
36 ] must-fail
37
38 { V{ "g" "o" } } [
39   "good" "g" token "o" token 2array seq parse
40 ] unit-test
41
42 { "a" } [
43   "abcd" "a" token "b" token 2array choice parse
44 ] unit-test
45
46 { "b" } [
47   "bbcd" "a" token "b" token 2array choice parse
48 ] unit-test
49
50 [
51   "cbcd" "a" token "b" token 2array choice parse 
52 ] must-fail
53
54 [
55   "" "a" token "b" token 2array choice parse 
56 ] must-fail
57
58 { 0 } [
59   "" "a" token repeat0 parse length
60 ] unit-test
61
62 { 0 } [
63   "b" "a" token repeat0 parse length
64 ] unit-test
65
66 { V{ "a" "a" "a" } } [
67   "aaab" "a" token repeat0 parse 
68 ] unit-test
69
70 [
71   "" "a" token repeat1 parse 
72 ] must-fail
73
74 [
75   "b" "a" token repeat1 parse 
76 ] must-fail
77
78 { V{ "a" "a" "a" } } [
79   "aaab" "a" token repeat1 parse
80 ] unit-test
81
82 { V{ "a" "b" } } [ 
83   "ab" "a" token optional "b" token 2array seq parse 
84 ] unit-test
85
86 { V{ f "b" } } [ 
87   "b" "a" token optional "b" token 2array seq parse 
88 ] unit-test
89
90
91   "cb" "a" token optional "b" token 2array seq parse  
92 ] must-fail
93
94 { V{ CHAR: a CHAR: b } } [
95   "ab" "a" token ensure CHAR: a CHAR: z range dup 3array seq parse
96 ] unit-test
97
98 [
99   "bb" "a" token ensure CHAR: a CHAR: z range 2array seq parse 
100 ] must-fail
101
102 { t } [
103   "a+b" 
104   "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
105   parse [ t ] [ f ] if
106 ] unit-test
107
108 { t } [
109   "a++b" 
110   "a" token "+" token dup ensure-not 2array seq "++" token 2array choice "b" token 3array seq
111   parse [ t ] [ f ] if
112 ] unit-test
113
114 { t } [
115   "a+b" 
116   "a" token "+" token "++" token 2array choice "b" token 3array seq
117   parse [ t ] [ f ] if
118 ] unit-test
119
120 [
121   "a++b" 
122   "a" token "+" token "++" token 2array choice "b" token 3array seq
123   parse [ t ] [ f ] if
124 ] must-fail
125
126 { 1 } [
127   "a" "a" token [ drop 1 ] action parse 
128 ] unit-test
129
130 { V{ 1 1 } } [
131   "aa" "a" token [ drop 1 ] action dup 2array seq parse 
132 ] unit-test
133
134 [
135   "b" "a" token [ drop 1 ] action parse 
136 ] must-fail
137
138
139   "b" [ CHAR: a = ] satisfy parse 
140 ] must-fail
141
142 { CHAR: a } [ 
143   "a" [ CHAR: a = ] satisfy parse
144 ] unit-test
145
146 { "a" } [
147   "    a" "a" token sp parse
148 ] unit-test
149
150 { "a" } [
151   "a" "a" token sp parse
152 ] unit-test
153
154 { V{ "a" } } [
155   "[a]" "[" token hide "a" token "]" token hide 3array seq parse
156 ] unit-test
157
158 [
159   "a]" "[" token hide "a" token "]" token hide 3array seq parse 
160 ] must-fail
161
162
163 { V{ "1" "-" "1" } V{ "1" "+" "1" } } [
164   [
165     [ "1" token , "-" token , "1" token , ] seq* ,
166     [ "1" token , "+" token , "1" token , ] seq* ,
167   ] choice* 
168   "1-1" over parse swap
169   "1+1" swap parse
170 ] unit-test
171
172 : expr ( -- parser ) 
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 ;
176
177 { V{ V{ "1" "+" "1" } "+" "1" } } [
178   "1+1+1" expr parse   
179 ] unit-test
180
181 { t } [
182   #! Ensure a circular parser doesn't loop infinitely
183   [ f , "a" token , ] seq*
184   dup peg>> parsers>>
185   dupd 0 swap set-nth compile word?
186 ] unit-test
187
188 [
189   "A" [ drop t ] satisfy [ 66 >= ] semantic parse 
190 ] must-fail
191
192 { CHAR: B } [
193   "B" [ drop t ] satisfy [ 66 >= ] semantic parse
194 ] unit-test
195
196 { f } [ \ + T{ parser f f f } equal? ] unit-test