Better error reporting in FROM: and RENAME: words

db4
Slava Pestov 2008-11-08 19:33:15 -06:00
parent 98487c630e
commit 57242951b9
6 changed files with 50 additions and 30 deletions

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Slava Pestov, Eduardo Cavazos. ! Copyright (C) 2008 Slava Pestov, Eduardo Cavazos.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences combinators parser splitting math USING: kernel sequences combinators parser splitting math
quotations arrays make qualified words ; quotations arrays make words ;
IN: fry IN: fry
: _ ( -- * ) "Only valid inside a fry" throw ; : _ ( -- * ) "Only valid inside a fry" throw ;

View File

@ -32,3 +32,14 @@ HELP: RENAME:
"RENAME: + math => -" "RENAME: + math => -"
"2 3 - ! => 5" } } ; "2 3 - ! => 5" } } ;
ARTICLE: "qualified" "Qualified word lookup"
"The " { $vocab-link "qualified" } " vocabulary provides a handful of parsing words which give more control over word lookup than is offered by " { $link POSTPONE: USE: } " and " { $link POSTPONE: USING: } "."
$nl
"These words are useful when there is no way to avoid using two vocabularies with identical word names in the same source file."
{ $subsection POSTPONE: QUALIFIED: }
{ $subsection POSTPONE: QUALIFIED-WITH: }
{ $subsection POSTPONE: FROM: }
{ $subsection POSTPONE: EXCLUDE: }
{ $subsection POSTPONE: RENAME: } ;
ABOUT: "qualified"

View File

@ -1,24 +1,33 @@
USING: tools.test qualified ; USING: tools.test qualified eval accessors parser ;
IN: foo IN: qualified.tests.foo
: x 1 ; : x 1 ;
IN: bar : y 5 ;
IN: qualified.tests.bar
: x 2 ; : x 2 ;
IN: baz : y 4 ;
IN: qualified.tests.baz
: x 3 ; : x 3 ;
QUALIFIED: foo QUALIFIED: qualified.tests.foo
QUALIFIED: bar QUALIFIED: qualified.tests.bar
[ 1 2 3 ] [ foo:x bar:x x ] unit-test [ 1 2 3 ] [ qualified.tests.foo:x qualified.tests.bar:x x ] unit-test
QUALIFIED-WITH: bar p QUALIFIED-WITH: qualified.tests.bar p
[ 2 ] [ p:x ] unit-test [ 2 ] [ p:x ] unit-test
RENAME: x baz => y RENAME: x qualified.tests.baz => y
[ 3 ] [ y ] unit-test [ 3 ] [ y ] unit-test
FROM: baz => x ; FROM: qualified.tests.baz => x ;
[ 3 ] [ x ] unit-test [ 3 ] [ x ] unit-test
[ 3 ] [ y ] unit-test
EXCLUDE: bar => x ; EXCLUDE: qualified.tests.bar => x ;
[ 3 ] [ x ] unit-test [ 3 ] [ x ] unit-test
[ 4 ] [ y ] unit-test
[ "USE: qualified IN: qualified.tests FROM: qualified.tests => doesnotexist ;" eval ]
[ error>> no-word-error? ] must-fail-with
[ "USE: qualified IN: qualified.tests RENAME: doesnotexist qualified.tests => blah" eval ]
[ error>> no-word-error? ] must-fail-with

View File

@ -1,12 +1,12 @@
! Copyright (C) 2007, 2008 Daniel Ehrenberg. ! Copyright (C) 2007, 2008 Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences assocs hashtables parser lexer USING: kernel sequences assocs hashtables parser lexer
vocabs words namespaces vocabs.loader debugger sets ; vocabs words namespaces vocabs.loader debugger sets fry ;
IN: qualified IN: qualified
: define-qualified ( vocab-name prefix-name -- ) : define-qualified ( vocab-name prefix-name -- )
[ load-vocab vocab-words ] [ CHAR: : suffix ] bi* [ load-vocab vocab-words ] [ CHAR: : suffix ] bi*
[ -rot >r append r> ] curry assoc-map '[ [ [ _ ] dip append ] dip ] assoc-map
use get push ; use get push ;
: QUALIFIED: : QUALIFIED:
@ -19,27 +19,27 @@ IN: qualified
: expect=> ( -- ) scan "=>" assert= ; : expect=> ( -- ) scan "=>" assert= ;
: partial-vocab ( words name -- assoc ) : partial-vocab ( words vocab -- assoc )
dupd [ '[ dup _ lookup [ no-word-error ] unless* ]
lookup [ "No such word: " swap append throw ] unless* { } map>assoc ;
] curry map zip ;
: partial-vocab-ignoring ( words name -- assoc )
[ load-vocab vocab-words keys swap diff ] keep partial-vocab ;
: EXCLUDE:
#! Syntax: EXCLUDE: vocab => words ... ;
scan expect=>
";" parse-tokens swap partial-vocab-ignoring use get push ; parsing
: FROM: : FROM:
#! Syntax: FROM: vocab => words... ; #! Syntax: FROM: vocab => words... ;
scan dup load-vocab drop expect=> scan dup load-vocab drop expect=>
";" parse-tokens swap partial-vocab use get push ; parsing ";" parse-tokens swap partial-vocab use get push ; parsing
: partial-vocab-excluding ( words vocab -- assoc )
[ load-vocab vocab-words keys swap diff ] keep partial-vocab ;
: EXCLUDE:
#! Syntax: EXCLUDE: vocab => words ... ;
scan expect=>
";" parse-tokens swap partial-vocab-excluding use get push ; parsing
: RENAME: : RENAME:
#! Syntax: RENAME: word vocab => newname #! Syntax: RENAME: word vocab => newname
scan scan dup load-vocab drop lookup [ "No such word" throw ] unless* scan scan dup load-vocab drop
dupd lookup [ ] [ no-word-error ] ?if
expect=> expect=>
scan associate use get push ; parsing scan associate use get push ; parsing

View File

@ -69,7 +69,7 @@ $nl
{ $subsection POSTPONE: PRIVATE> } { $subsection POSTPONE: PRIVATE> }
{ $subsection "vocabulary-search-errors" } { $subsection "vocabulary-search-errors" }
{ $subsection "vocabulary-search-shadow" } { $subsection "vocabulary-search-shadow" }
{ $see-also "words" } ; { $see-also "words" "qualified" } ;
ARTICLE: "reading-ahead" "Reading ahead" ARTICLE: "reading-ahead" "Reading ahead"
"Parsing words can consume input:" "Parsing words can consume input:"

View File

@ -71,10 +71,10 @@ ERROR: no-current-vocab ;
] keep ] keep
] { } map>assoc ; ] { } map>assoc ;
TUPLE: no-word-error name ; ERROR: no-word-error name ;
: no-word ( name -- newword ) : no-word ( name -- newword )
dup no-word-error boa dup \ no-word-error boa
swap words-named [ forward-reference? not ] filter swap words-named [ forward-reference? not ] filter
word-restarts throw-restarts word-restarts throw-restarts
dup vocabulary>> (use+) ; dup vocabulary>> (use+) ;