1 USING: arrays math parser tools.test kernel generic words
2 io.streams.string namespaces classes effects source-files assocs
3 sequences strings io.files io.pathnames definitions
4 continuations sorting classes.tuple compiler.units debugger
5 vocabs vocabs.loader accessors eval combinators lexer
6 vocabs.parser words.symbol multiline source-files.errors
7 tools.crossref grouping ;
12 [ "1\n[\n2\n[\n3\n]\n4\n]\n5" eval( -- a b c ) ]
16 [ "t t f f" eval( -- ? ? ? ? ) ]
20 [ "\"hello world\"" eval( -- string ) ]
24 [ "\"\\n\\r\\t\\\\\"" eval( -- string ) ]
29 "IN: parser.tests : hello ( -- str ) \"hello world\" ;"
30 eval( -- ) "USE: parser.tests hello" eval( -- string )
34 [ "! This is a comment, people." eval( -- ) ]
40 [ "\"\\u000020\"" eval( -- string ) ]
44 [ "\"\\u000027\"" eval( -- string ) ]
47 ! Test EOL comments in multiline strings.
48 [ "Hello" ] [ "#! This calls until-eol.\n\"Hello\"" eval( -- string ) ] unit-test
50 [ word ] [ \ f class ] unit-test
52 ! Test stack effect parsing
54 : effect-parsing-test ( a b -- c ) + ;
57 "effect-parsing-test" "parser.tests" lookup
58 \ effect-parsing-test eq?
61 [ T{ effect f { "a" "b" } { "c" } f } ]
62 [ \ effect-parsing-test "declared-effect" word-prop ] unit-test
64 : baz ( a b -- * ) 2array throw ;
67 [ \ baz "declared-effect" word-prop terminated?>> ]
70 [ ] [ "IN: parser.tests USE: math : effect-parsing-test ( a b -- d ) - ;" eval( -- ) ] unit-test
73 "effect-parsing-test" "parser.tests" lookup
74 \ effect-parsing-test eq?
77 [ T{ effect f { "a" "b" } { "d" } f } ]
78 [ \ effect-parsing-test "declared-effect" word-prop ] unit-test
81 [ 2 ] [ "IN: parser.tests : \0. ( -- x ) 2 ; \0." eval( -- n ) ] unit-test
83 [ "IN: parser.tests : missing-- ( a b ) ;" eval( -- ) ] must-fail
85 ! These should throw errors
86 [ "HEX: zzz" eval( -- obj ) ] must-fail
87 [ "OCT: 999" eval( -- obj ) ] must-fail
88 [ "BIN: --0" eval( -- obj ) ] must-fail
92 "IN: parser.tests USING: math prettyprint ; SYNTAX: foo 2 2 + . ;" eval( -- )
94 [ ] [ "USE: parser.tests foo" eval( -- ) ] unit-test
96 "IN: parser.tests USING: math prettyprint ; : foo ( -- ) 2 2 + . ;" eval( -- )
99 "USE: parser.tests \\ foo" eval( -- word )
100 "foo" "parser.tests" lookup eq?
106 "IN: parser.tests : smudge-me ( -- ) ;" <string-reader> "foo"
109 "foo" source-file definitions>> first assoc-size
112 [ t ] [ "smudge-me" "parser.tests" lookup >boolean ] unit-test
115 "IN: parser.tests : smudge-me-more ( -- ) ;" <string-reader> "foo"
119 [ t ] [ "smudge-me-more" "parser.tests" lookup >boolean ] unit-test
120 [ f ] [ "smudge-me" "parser.tests" lookup >boolean ] unit-test
123 "IN: parser.tests USING: math strings ; GENERIC: smudge-me ( a -- b ) M: integer smudge-me ; M: string smudge-me ;" <string-reader> "foo"
126 "foo" source-file definitions>> first assoc-size
130 "IN: parser.tests USING: arrays ; M: array smudge-me ;" <string-reader> "bar"
133 "bar" source-file definitions>> first assoc-size
137 "IN: parser.tests USING: math strings ; GENERIC: smudge-me ( a -- b ) M: integer smudge-me ;" <string-reader> "foo"
140 "foo" source-file definitions>> first assoc-size
144 array "smudge-me" "parser.tests" lookup order memq?
148 integer "smudge-me" "parser.tests" lookup order memq?
152 string "smudge-me" "parser.tests" lookup order memq?
156 "IN: parser.tests USE: math 2 2 +" <string-reader> "a"
161 "a" <pathname> \ + usage member?
165 "IN: parser.tests USE: math 2 2 -" <string-reader> "a"
170 "a" <pathname> \ + usage member?
174 "a" source-files get delete-at
176 "IN: parser.tests DEFER: x : y ( -- ) x ; : x ( -- ) y ;"
177 <string-reader> "a" parse-stream drop
181 "a" source-files get delete-at
184 "IN: parser.tests : x ( -- ) ; : y ( -- * ) 3 throw ; this is an error"
185 <string-reader> "a" parse-stream
186 ] [ source-file-error? ] must-fail-with
189 "y" "parser.tests" lookup >boolean
193 "IN: parser.tests : x ( -- ) ;"
194 <string-reader> "a" parse-stream drop
196 "y" "parser.tests" lookup
199 ! Test new forward definition logic
201 "IN: axx : axx ( -- ) ;"
202 <string-reader> "axx" parse-stream drop
206 "USE: axx IN: bxx : bxx ( -- ) ; : cxx ( -- ) axx bxx ;"
207 <string-reader> "bxx" parse-stream drop
210 ! So we move the bxx word to axx...
212 "IN: axx : axx ( -- ) ; : bxx ( -- ) ;"
213 <string-reader> "axx" parse-stream drop
216 [ t ] [ "bxx" "axx" lookup >boolean ] unit-test
218 ! And reload the file that uses it...
220 "USE: axx IN: bxx ( -- ) : cxx ( -- ) axx bxx ;"
221 <string-reader> "bxx" parse-stream drop
224 ! And hope not to get a forward-error!
226 ! Turning a generic into a non-generic could cause all
229 "IN: ayy USE: kernel GENERIC: ayy ( a -- b ) M: object ayy ;"
230 <string-reader> "ayy" parse-stream drop
234 "IN: ayy USE: kernel : ayy ( -- ) ;"
235 <string-reader> "ayy" parse-stream drop
239 "IN: azz TUPLE: my-class ; GENERIC: a-generic ( a -- b )"
240 <string-reader> "azz" parse-stream drop
244 "USE: azz M: my-class a-generic ;"
245 <string-reader> "azz-2" parse-stream drop
249 "IN: azz GENERIC: a-generic ( a -- b )"
250 <string-reader> "azz" parse-stream drop
254 "USE: azz USE: math M: integer a-generic ;"
255 <string-reader> "azz-2" parse-stream drop
259 "IN: parser.tests : <bogus-error> ( -- ) ; : bogus ( -- error ) <bogus-error> ;"
260 <string-reader> "bogus-error" parse-stream drop
264 "IN: parser.tests TUPLE: bogus-error ; C: <bogus-error> bogus-error : bogus ( -- error ) <bogus-error> ;"
265 <string-reader> "bogus-error" parse-stream drop
268 ! Problems with class predicates -vs- ordinary words
270 "IN: parser.tests TUPLE: killer ;"
271 <string-reader> "removing-the-predicate" parse-stream drop
275 "IN: parser.tests GENERIC: killer? ( a -- b )"
276 <string-reader> "removing-the-predicate" parse-stream drop
280 "killer?" "parser.tests" lookup >boolean
284 "IN: parser.tests TUPLE: another-pred-test ; GENERIC: another-pred-test? ( a -- b )"
285 <string-reader> "removing-the-predicate" parse-stream
286 ] [ error>> error>> error>> redefine-error? ] must-fail-with
289 "IN: parser.tests TUPLE: class-redef-test ; TUPLE: class-redef-test ;"
290 <string-reader> "redefining-a-class-1" parse-stream
291 ] [ error>> error>> error>> redefine-error? ] must-fail-with
294 "IN: parser.tests TUPLE: class-redef-test ; SYMBOL: class-redef-test"
295 <string-reader> "redefining-a-class-2" parse-stream drop
299 "IN: parser.tests TUPLE: class-redef-test ; SYMBOL: class-redef-test : class-redef-test ( -- ) ;"
300 <string-reader> "redefining-a-class-3" parse-stream drop
301 ] [ error>> error>> error>> redefine-error? ] must-fail-with
304 "IN: parser.tests TUPLE: class-fwd-test ;"
305 <string-reader> "redefining-a-class-3" parse-stream drop
309 "IN: parser.tests \\ class-fwd-test"
310 <string-reader> "redefining-a-class-3" parse-stream drop
311 ] [ error>> error>> error>> no-word-error? ] must-fail-with
314 "IN: parser.tests TUPLE: class-fwd-test ; SYMBOL: class-fwd-test"
315 <string-reader> "redefining-a-class-3" parse-stream drop
319 "IN: parser.tests \\ class-fwd-test"
320 <string-reader> "redefining-a-class-3" parse-stream drop
321 ] [ error>> error>> error>> no-word-error? ] must-fail-with
324 "IN: parser.tests : foo ( -- ) ; TUPLE: foo ;"
325 <string-reader> "redefining-a-class-4" parse-stream drop
326 ] [ error>> error>> error>> redefine-error? ] must-fail-with
329 "IN: parser.tests : foo ( x y -- z ) 1 2 ; : bar ( a -- b ) ;" eval( -- )
333 "IN: parser.tests : foo ( x y -- z) 1 2 ; : bar ( a -- b ) ;" eval( -- )
338 "IN: parser.tests USE: kernel PREDICATE: foo < object ( x -- y ) ;" eval( -- )
342 "foo?" "parser.tests" lookup word eq?
347 "redefining-a-class-5" forget-source
348 "redefining-a-class-6" forget-source
349 "redefining-a-class-7" forget-source
350 ] with-compilation-unit
355 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
356 <string-reader> "redefining-a-class-5" parse-stream drop
360 "IN: parser.tests M: f foo ;"
361 <string-reader> "redefining-a-class-6" parse-stream drop
364 [ f ] [ f "foo" "parser.tests" lookup execute ] unit-test
367 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
368 <string-reader> "redefining-a-class-5" parse-stream drop
371 [ f ] [ f "foo" "parser.tests" lookup execute ] unit-test
374 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
375 <string-reader> "redefining-a-class-7" parse-stream drop
378 [ f ] [ f "foo" "parser.tests" lookup execute ] unit-test
381 "IN: parser.tests TUPLE: foo ;"
382 <string-reader> "redefining-a-class-7" parse-stream drop
385 [ t ] [ "foo" "parser.tests" lookup symbol? ] unit-test
388 [ "vocab:parser/test/assert-depth.factor" run-file ] must-fail
392 "IN: parser.tests DEFER: d-f-s d-f-s SYMBOL: d-f-s d-f-s"
393 <string-reader> "d-f-s-test" parse-stream drop
397 "IN: parser.tests DEFER: d-f-s d-f-s FORGET: d-f-s SYMBOL: d-f-s d-f-s"
398 <string-reader> "d-f-s-test" parse-stream drop
402 "IN: parser.tests DEFER: d-f-s d-f-s SYMBOL: d-f-s d-f-s"
403 <string-reader> "d-f-s-test" parse-stream drop
408 [ "this-better-not-exist" forget-vocab ] with-compilation-unit
412 "USE: this-better-not-exist" eval( -- )
415 [ ": foo ;" eval( -- ) ] [ error>> error>> no-current-vocab? ] must-fail-with
417 [ 92 ] [ "CHAR: \\" eval( -- n ) ] unit-test
418 [ 92 ] [ "CHAR: \\\\" eval( -- n ) ] unit-test
423 "USING: math arrays kernel ;"
424 "GENERIC: change-combination ( obj a -- b )"
425 "M: integer change-combination 2drop 1 ;"
426 "M: array change-combination 2drop 2 ;"
427 } "\n" join <string-reader> "change-combination-test" parse-stream drop
433 "USING: math arrays kernel ;"
434 "GENERIC# change-combination 1 ( obj a -- b )"
435 "M: integer change-combination 2drop 1 ;"
436 "M: array change-combination 2drop 2 ;"
437 } "\n" join <string-reader> "change-combination-test" parse-stream drop
441 "change-combination" "parser.tests" lookup
442 "methods" word-prop assoc-size
447 "IN: parser.tests DEFER: twice-fails FORGET: twice-fails MIXIN: twice-fails"
448 <string-reader> "twice-fails-test" parse-stream drop
453 "IN: parser.tests : staging-problem-test-1 ( -- a ) 1 ; : staging-problem-test-2 ( -- a ) staging-problem-test-1 ;"
454 <string-reader> "staging-problem-test" parse-stream
457 [ t ] [ "staging-problem-test-1" "parser.tests" lookup >boolean ] unit-test
459 [ t ] [ "staging-problem-test-2" "parser.tests" lookup >boolean ] unit-test
462 "IN: parser.tests << : staging-problem-test-1 ( -- a ) 1 ; >> : staging-problem-test-2 ( -- a ) staging-problem-test-1 ;"
463 <string-reader> "staging-problem-test" parse-stream
466 [ t ] [ "staging-problem-test-1" "parser.tests" lookup >boolean ] unit-test
468 [ t ] [ "staging-problem-test-2" "parser.tests" lookup >boolean ] unit-test
470 [ "DEFER: blahy" eval( -- ) ] [ error>> error>> no-current-vocab? ] must-fail-with
473 "IN: parser.tests SYNTAX: blahy ; FORGET: blahy" eval( -- )
475 error>> staging-violation?
478 ! Bogus error message
481 [ "IN: parser.tests USE: kernel TUPLE: blahy < tuple ; : blahy ( -- ) ; TUPLE: blahy < tuple ; : blahy ( -- ) ;" eval( -- ) ]
482 [ error>> error>> def>> \ blahy eq? ] must-fail-with
484 [ ] [ f lexer set f file set "Hello world" note. ] unit-test
486 [ "CHAR: \\u9999999999999" eval( -- n ) ] must-fail
490 [ a ] [ a ] unit-test
491 [ b ] [ b ] unit-test
492 [ c ] [ c ] unit-test
496 [ ] [ "IN: parser.tests GENERIC: blah ( -- )" eval( -- ) ] unit-test
497 [ ] [ "IN: parser.tests SYMBOLS: blah ;" eval( -- ) ] unit-test
499 [ f ] [ \ blah generic? ] unit-test
500 [ t ] [ \ blah symbol? ] unit-test
504 [ "IN: parser.tests SINGLETONS: blah1 blah1 blah1 ;" eval( -- ) ]
505 [ error>> error>> def>> \ blah1 eq? ]
508 IN: qualified.tests.foo
511 IN: qualified.tests.bar
514 IN: qualified.tests.baz
517 QUALIFIED: qualified.tests.foo
518 QUALIFIED: qualified.tests.bar
519 [ 1 2 3 ] [ qualified.tests.foo:x qualified.tests.bar:x x ] unit-test
521 QUALIFIED-WITH: qualified.tests.bar p
522 [ 2 ] [ p:x ] unit-test
524 RENAME: x qualified.tests.baz => y
525 [ 3 ] [ y ] unit-test
527 FROM: qualified.tests.baz => x ;
528 [ 3 ] [ x ] unit-test
529 [ 3 ] [ y ] unit-test
531 EXCLUDE: qualified.tests.bar => x ;
532 [ 3 ] [ x ] unit-test
533 [ 4 ] [ y ] unit-test
535 [ "IN: qualified.tests FROM: qualified.tests => doesnotexist ;" eval( -- ) ]
536 [ error>> no-word-error? ] must-fail-with
538 [ "IN: qualified.tests RENAME: doesnotexist qualified.tests => blahx" eval( -- ) ]
539 [ error>> no-word-error? ] must-fail-with
543 ! Replace : def with something in << >>
545 "IN: parser.tests : was-once-a-word-bug ( -- ) ;"
546 <string-reader> "was-once-a-word-test" parse-stream
549 [ t ] [ "was-once-a-word-bug" "parser.tests" lookup >boolean ] unit-test
552 "IN: parser.tests USE: words << \"was-once-a-word-bug\" \"parser.tests\" create [ ] (( -- )) define-declared >>"
553 <string-reader> "was-once-a-word-test" parse-stream
556 [ t ] [ "was-once-a-word-bug" "parser.tests" lookup >boolean ] unit-test */
558 ! Replace : def with DEFER:
560 "IN: parser.tests : is-not-deferred ( -- ) ;"
561 <string-reader> "is-not-deferred" parse-stream
564 [ t ] [ "is-not-deferred" "parser.tests" lookup >boolean ] unit-test
565 [ f ] [ "is-not-deferred" "parser.tests" lookup deferred? ] unit-test
568 "IN: parser.tests DEFER: is-not-deferred"
569 <string-reader> "is-not-deferred" parse-stream
572 [ t ] [ "is-not-deferred" "parser.tests" lookup >boolean ] unit-test
573 [ t ] [ "is-not-deferred" "parser.tests" lookup deferred? ] unit-test
575 ! Forward-reference resolution case iterated using list in the wrong direction
577 "IN: parser.tests.forward-ref-1 DEFER: x DEFER: y"
578 <string-reader> "forward-ref-1" parse-stream
582 "IN: parser.tests.forward-ref-2 DEFER: x DEFER: y"
583 <string-reader> "forward-ref-2" parse-stream
587 "IN: parser.tests.forward-ref-3 FROM: parser.tests.forward-ref-1 => x y ; FROM: parser.tests.forward-ref-2 => x y ; : z ( -- ) x y ;"
588 <string-reader> "forward-ref-3" parse-stream
592 "z" "parser.tests.forward-ref-3" lookup def>> [ vocabulary>> ] map all-equal?
596 "FROM: parser.tests.forward-ref-1 => x y ; FROM: parser.tests.forward-ref-2 => x y ; IN: parser.tests.forward-ref-3 : x ( -- ) ; : z ( -- ) x y ;"
597 <string-reader> "forward-ref-3" parse-stream
601 "z" "parser.tests.forward-ref-3" lookup def>> [ vocabulary>> ] map all-equal?
605 "IN: parser.tests.forward-ref-3 FROM: parser.tests.forward-ref-1 => x y ; FROM: parser.tests.forward-ref-2 => x y ; : z ( -- ) x y ;"
606 <string-reader> "forward-ref-3" parse-stream
610 "z" "parser.tests.forward-ref-3" lookup def>> [ vocabulary>> ] map all-equal?