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
94 { "scratchpad" "arrays" } set-use
96 ! This shouldn't modify in/use in the outer scope!
99 use get { "scratchpad" "arrays" } set-use use get =
104 "IN: parser.tests USING: math prettyprint ; SYNTAX: foo 2 2 + . ;" eval( -- )
106 [ ] [ "USE: parser.tests foo" eval( -- ) ] unit-test
108 "IN: parser.tests USING: math prettyprint ; : foo ( -- ) 2 2 + . ;" eval( -- )
111 "USE: parser.tests \\ foo" eval( -- word )
112 "foo" "parser.tests" lookup eq?
118 "IN: parser.tests : smudge-me ( -- ) ;" <string-reader> "foo"
121 "foo" source-file definitions>> first assoc-size
124 [ t ] [ "smudge-me" "parser.tests" lookup >boolean ] unit-test
127 "IN: parser.tests : smudge-me-more ( -- ) ;" <string-reader> "foo"
131 [ t ] [ "smudge-me-more" "parser.tests" lookup >boolean ] unit-test
132 [ f ] [ "smudge-me" "parser.tests" lookup >boolean ] unit-test
135 "IN: parser.tests USING: math strings ; GENERIC: smudge-me ( a -- b ) M: integer smudge-me ; M: string smudge-me ;" <string-reader> "foo"
138 "foo" source-file definitions>> first assoc-size
142 "IN: parser.tests USING: arrays ; M: array smudge-me ;" <string-reader> "bar"
145 "bar" source-file definitions>> first assoc-size
149 "IN: parser.tests USING: math strings ; GENERIC: smudge-me ( a -- b ) M: integer smudge-me ;" <string-reader> "foo"
152 "foo" source-file definitions>> first assoc-size
156 array "smudge-me" "parser.tests" lookup order memq?
160 integer "smudge-me" "parser.tests" lookup order memq?
164 string "smudge-me" "parser.tests" lookup order memq?
168 "IN: parser.tests USE: math 2 2 +" <string-reader> "a"
173 "a" <pathname> \ + usage member?
177 "IN: parser.tests USE: math 2 2 -" <string-reader> "a"
182 "a" <pathname> \ + usage member?
186 "a" source-files get delete-at
188 "IN: parser.tests DEFER: x : y ( -- ) x ; : x ( -- ) y ;"
189 <string-reader> "a" parse-stream drop
193 "a" source-files get delete-at
196 "IN: parser.tests : x ( -- ) ; : y ( -- * ) 3 throw ; this is an error"
197 <string-reader> "a" parse-stream
198 ] [ source-file-error? ] must-fail-with
201 "y" "parser.tests" lookup >boolean
205 "IN: parser.tests : x ( -- ) ;"
206 <string-reader> "a" parse-stream drop
208 "y" "parser.tests" lookup
211 ! Test new forward definition logic
213 "IN: axx : axx ( -- ) ;"
214 <string-reader> "axx" parse-stream drop
218 "USE: axx IN: bxx : bxx ( -- ) ; : cxx ( -- ) axx bxx ;"
219 <string-reader> "bxx" parse-stream drop
222 ! So we move the bxx word to axx...
224 "IN: axx : axx ( -- ) ; : bxx ( -- ) ;"
225 <string-reader> "axx" parse-stream drop
228 [ t ] [ "bxx" "axx" lookup >boolean ] unit-test
230 ! And reload the file that uses it...
232 "USE: axx IN: bxx ( -- ) : cxx ( -- ) axx bxx ;"
233 <string-reader> "bxx" parse-stream drop
236 ! And hope not to get a forward-error!
238 ! Turning a generic into a non-generic could cause all
241 "IN: ayy USE: kernel GENERIC: ayy ( a -- b ) M: object ayy ;"
242 <string-reader> "ayy" parse-stream drop
246 "IN: ayy USE: kernel : ayy ( -- ) ;"
247 <string-reader> "ayy" parse-stream drop
251 "IN: azz TUPLE: my-class ; GENERIC: a-generic ( a -- b )"
252 <string-reader> "azz" parse-stream drop
256 "USE: azz M: my-class a-generic ;"
257 <string-reader> "azz-2" parse-stream drop
261 "IN: azz GENERIC: a-generic ( a -- b )"
262 <string-reader> "azz" parse-stream drop
266 "USE: azz USE: math M: integer a-generic ;"
267 <string-reader> "azz-2" parse-stream drop
271 "IN: parser.tests : <bogus-error> ( -- ) ; : bogus ( -- error ) <bogus-error> ;"
272 <string-reader> "bogus-error" parse-stream drop
276 "IN: parser.tests TUPLE: bogus-error ; C: <bogus-error> bogus-error : bogus ( -- error ) <bogus-error> ;"
277 <string-reader> "bogus-error" parse-stream drop
280 ! Problems with class predicates -vs- ordinary words
282 "IN: parser.tests TUPLE: killer ;"
283 <string-reader> "removing-the-predicate" parse-stream drop
287 "IN: parser.tests GENERIC: killer? ( a -- b )"
288 <string-reader> "removing-the-predicate" parse-stream drop
292 "killer?" "parser.tests" lookup >boolean
296 "IN: parser.tests TUPLE: another-pred-test ; GENERIC: another-pred-test? ( a -- b )"
297 <string-reader> "removing-the-predicate" parse-stream
298 ] [ error>> error>> error>> redefine-error? ] must-fail-with
301 "IN: parser.tests TUPLE: class-redef-test ; TUPLE: class-redef-test ;"
302 <string-reader> "redefining-a-class-1" parse-stream
303 ] [ error>> error>> error>> redefine-error? ] must-fail-with
306 "IN: parser.tests TUPLE: class-redef-test ; SYMBOL: class-redef-test"
307 <string-reader> "redefining-a-class-2" parse-stream drop
311 "IN: parser.tests TUPLE: class-redef-test ; SYMBOL: class-redef-test : class-redef-test ( -- ) ;"
312 <string-reader> "redefining-a-class-3" parse-stream drop
313 ] [ error>> error>> error>> redefine-error? ] must-fail-with
316 "IN: parser.tests TUPLE: class-fwd-test ;"
317 <string-reader> "redefining-a-class-3" parse-stream drop
321 "IN: parser.tests \\ class-fwd-test"
322 <string-reader> "redefining-a-class-3" parse-stream drop
323 ] [ error>> error>> error>> no-word-error? ] must-fail-with
326 "IN: parser.tests TUPLE: class-fwd-test ; SYMBOL: class-fwd-test"
327 <string-reader> "redefining-a-class-3" parse-stream drop
331 "IN: parser.tests \\ class-fwd-test"
332 <string-reader> "redefining-a-class-3" parse-stream drop
333 ] [ error>> error>> error>> no-word-error? ] must-fail-with
336 "IN: parser.tests : foo ( -- ) ; TUPLE: foo ;"
337 <string-reader> "redefining-a-class-4" parse-stream drop
338 ] [ error>> error>> error>> redefine-error? ] must-fail-with
341 "IN: parser.tests : foo ( x y -- z ) 1 2 ; : bar ( a -- b ) ;" eval( -- )
345 "IN: parser.tests : foo ( x y -- z) 1 2 ; : bar ( a -- b ) ;" eval( -- )
350 "IN: parser.tests USE: kernel PREDICATE: foo < object ( x -- y ) ;" eval( -- )
354 "foo?" "parser.tests" lookup word eq?
359 "redefining-a-class-5" forget-source
360 "redefining-a-class-6" forget-source
361 "redefining-a-class-7" forget-source
362 ] with-compilation-unit
367 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
368 <string-reader> "redefining-a-class-5" parse-stream drop
372 "IN: parser.tests M: f foo ;"
373 <string-reader> "redefining-a-class-6" parse-stream drop
376 [ f ] [ f "foo" "parser.tests" lookup execute ] unit-test
379 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
380 <string-reader> "redefining-a-class-5" parse-stream drop
383 [ f ] [ f "foo" "parser.tests" lookup execute ] unit-test
386 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
387 <string-reader> "redefining-a-class-7" parse-stream drop
390 [ f ] [ f "foo" "parser.tests" lookup execute ] unit-test
393 "IN: parser.tests TUPLE: foo ;"
394 <string-reader> "redefining-a-class-7" parse-stream drop
397 [ t ] [ "foo" "parser.tests" lookup symbol? ] unit-test
400 [ "vocab:parser/test/assert-depth.factor" run-file ] must-fail
404 "IN: parser.tests DEFER: d-f-s d-f-s SYMBOL: d-f-s d-f-s"
405 <string-reader> "d-f-s-test" parse-stream drop
409 "IN: parser.tests DEFER: d-f-s d-f-s FORGET: d-f-s SYMBOL: d-f-s d-f-s"
410 <string-reader> "d-f-s-test" parse-stream drop
414 "IN: parser.tests DEFER: d-f-s d-f-s SYMBOL: d-f-s d-f-s"
415 <string-reader> "d-f-s-test" parse-stream drop
420 [ "this-better-not-exist" forget-vocab ] with-compilation-unit
424 "USE: this-better-not-exist" eval( -- )
427 [ ": foo ;" eval( -- ) ] [ error>> error>> no-current-vocab? ] must-fail-with
429 [ 92 ] [ "CHAR: \\" eval( -- n ) ] unit-test
430 [ 92 ] [ "CHAR: \\\\" eval( -- n ) ] unit-test
435 "USING: math arrays kernel ;"
436 "GENERIC: change-combination ( obj a -- b )"
437 "M: integer change-combination 2drop 1 ;"
438 "M: array change-combination 2drop 2 ;"
439 } "\n" join <string-reader> "change-combination-test" parse-stream drop
445 "USING: math arrays kernel ;"
446 "GENERIC# change-combination 1 ( obj a -- b )"
447 "M: integer change-combination 2drop 1 ;"
448 "M: array change-combination 2drop 2 ;"
449 } "\n" join <string-reader> "change-combination-test" parse-stream drop
453 "change-combination" "parser.tests" lookup
454 "methods" word-prop assoc-size
459 "IN: parser.tests DEFER: twice-fails FORGET: twice-fails MIXIN: twice-fails"
460 <string-reader> "twice-fails-test" parse-stream drop
465 "IN: parser.tests : staging-problem-test-1 ( -- a ) 1 ; : staging-problem-test-2 ( -- a ) staging-problem-test-1 ;"
466 <string-reader> "staging-problem-test" parse-stream
469 [ t ] [ "staging-problem-test-1" "parser.tests" lookup >boolean ] unit-test
471 [ t ] [ "staging-problem-test-2" "parser.tests" lookup >boolean ] unit-test
474 "IN: parser.tests << : staging-problem-test-1 ( -- a ) 1 ; >> : staging-problem-test-2 ( -- a ) staging-problem-test-1 ;"
475 <string-reader> "staging-problem-test" parse-stream
478 [ t ] [ "staging-problem-test-1" "parser.tests" lookup >boolean ] unit-test
480 [ t ] [ "staging-problem-test-2" "parser.tests" lookup >boolean ] unit-test
482 [ "DEFER: blahy" eval( -- ) ] [ error>> error>> no-current-vocab? ] must-fail-with
485 "IN: parser.tests SYNTAX: blahy ; FORGET: blahy" eval( -- )
487 error>> staging-violation?
490 ! Bogus error message
493 [ "IN: parser.tests USE: kernel TUPLE: blahy < tuple ; : blahy ( -- ) ; TUPLE: blahy < tuple ; : blahy ( -- ) ;" eval( -- ) ]
494 [ error>> error>> def>> \ blahy eq? ] must-fail-with
496 [ ] [ f lexer set f file set "Hello world" note. ] unit-test
498 [ "CHAR: \\u9999999999999" eval( -- n ) ] must-fail
502 [ a ] [ a ] unit-test
503 [ b ] [ b ] unit-test
504 [ c ] [ c ] unit-test
508 [ ] [ "IN: parser.tests GENERIC: blah ( -- )" eval( -- ) ] unit-test
509 [ ] [ "IN: parser.tests SYMBOLS: blah ;" eval( -- ) ] unit-test
511 [ f ] [ \ blah generic? ] unit-test
512 [ t ] [ \ blah symbol? ] unit-test
516 [ "IN: parser.tests SINGLETONS: blah1 blah1 blah1 ;" eval( -- ) ]
517 [ error>> error>> def>> \ blah1 eq? ]
520 IN: qualified.tests.foo
523 IN: qualified.tests.bar
526 IN: qualified.tests.baz
529 QUALIFIED: qualified.tests.foo
530 QUALIFIED: qualified.tests.bar
531 [ 1 2 3 ] [ qualified.tests.foo:x qualified.tests.bar:x x ] unit-test
533 QUALIFIED-WITH: qualified.tests.bar p
534 [ 2 ] [ p:x ] unit-test
536 RENAME: x qualified.tests.baz => y
537 [ 3 ] [ y ] unit-test
539 FROM: qualified.tests.baz => x ;
540 [ 3 ] [ x ] unit-test
541 [ 3 ] [ y ] unit-test
543 EXCLUDE: qualified.tests.bar => x ;
544 [ 3 ] [ x ] unit-test
545 [ 4 ] [ y ] unit-test
547 [ "IN: qualified.tests FROM: qualified.tests => doesnotexist ;" eval( -- ) ]
548 [ error>> no-word-error? ] must-fail-with
550 [ "IN: qualified.tests RENAME: doesnotexist qualified.tests => blahx" eval( -- ) ]
551 [ error>> no-word-error? ] must-fail-with
555 ! Replace : def with something in << >>
557 "IN: parser.tests : was-once-a-word-bug ( -- ) ;"
558 <string-reader> "was-once-a-word-test" parse-stream
561 [ t ] [ "was-once-a-word-bug" "parser.tests" lookup >boolean ] unit-test
564 "IN: parser.tests USE: words << \"was-once-a-word-bug\" \"parser.tests\" create [ ] (( -- )) define-declared >>"
565 <string-reader> "was-once-a-word-test" parse-stream
568 [ t ] [ "was-once-a-word-bug" "parser.tests" lookup >boolean ] unit-test */
570 ! Replace : def with DEFER:
572 "IN: parser.tests : is-not-deferred ( -- ) ;"
573 <string-reader> "is-not-deferred" parse-stream
576 [ t ] [ "is-not-deferred" "parser.tests" lookup >boolean ] unit-test
577 [ f ] [ "is-not-deferred" "parser.tests" lookup deferred? ] unit-test
580 "IN: parser.tests DEFER: is-not-deferred"
581 <string-reader> "is-not-deferred" parse-stream
584 [ t ] [ "is-not-deferred" "parser.tests" lookup >boolean ] unit-test
585 [ t ] [ "is-not-deferred" "parser.tests" lookup deferred? ] unit-test
587 ! Forward-reference resolution case iterated using list in the wrong direction
589 "IN: parser.tests.forward-ref-1 DEFER: x DEFER: y"
590 <string-reader> "forward-ref-1" parse-stream
594 "IN: parser.tests.forward-ref-2 DEFER: x DEFER: y"
595 <string-reader> "forward-ref-2" parse-stream
599 "IN: parser.tests.forward-ref-3 USING: parser.tests.forward-ref-1 parser.tests.forward-ref-2 ; : z ( -- ) x y ;"
600 <string-reader> "forward-ref-3" parse-stream
604 "z" "parser.tests.forward-ref-3" lookup def>> [ vocabulary>> ] map all-equal?
608 "USING: parser.tests.forward-ref-1 parser.tests.forward-ref-2 ; IN: parser.tests.forward-ref-3 : x ( -- ) ; : z ( -- ) x y ;"
609 <string-reader> "forward-ref-3" parse-stream
613 "z" "parser.tests.forward-ref-3" lookup def>> [ vocabulary>> ] map all-equal?
617 "IN: parser.tests.forward-ref-3 USING: parser.tests.forward-ref-1 parser.tests.forward-ref-2 ; : z ( -- ) x y ;"
618 <string-reader> "forward-ref-3" parse-stream
622 "z" "parser.tests.forward-ref-3" lookup def>> [ vocabulary>> ] map all-equal?