diff --git a/core/parser/parser-docs.factor b/core/parser/parser-docs.factor index edaeb21a4c..b036f68db4 100644 --- a/core/parser/parser-docs.factor +++ b/core/parser/parser-docs.factor @@ -140,12 +140,20 @@ HELP: no-word { $values { "name" string } { "newword" word } } { $description "Throws a " { $link no-word-error } "." } ; +HELP: parse-word +{ $values { "string" string } { "word/number" "a word or number" } } +{ $description "If " { $snippet "string" } " is a valid number literal, it is converted to a number, otherwise the current vocabulary search path is searched for a word named by the string." } +{ $errors "Throws an error if the token does not name a word, and does not parse as a number." } +{ $notes "This word is used to implement " { $link scan-word } "." } ; + HELP: scan-word { $values { "word/number/f" "a word, number or " { $link f } } } { $description "Reads the next token from parser input. If the token is a valid number literal, it is converted to a number, otherwise the dictionary is searched for a word named by the token. Outputs " { $link f } " if the end of the input has been reached." } { $errors "Throws an error if the token does not name a word, and does not parse as a number." } $parsing-note ; +{ scan-word parse-word } related-words + HELP: parse-step { $values { "accum" vector } { "end" word } { "?" "a boolean" } } { $description "Parses a token. If the token is a number or an ordinary word, it is added to the accumulator. If it is a parsing word, calls the parsing word with the accumulator on the stack. Outputs " { $link f } " if " { $snippet "end" } " is encountered, " { $link t } " otherwise." } diff --git a/core/parser/parser.factor b/core/parser/parser.factor index 276030d770..3152afc093 100644 --- a/core/parser/parser.factor +++ b/core/parser/parser.factor @@ -40,12 +40,13 @@ SYMBOL: auto-use? [ throw-restarts no-word-restarted ] if ; +: parse-word ( string -- word/number ) + dup search [ ] [ + dup string>number [ ] [ no-word ] ?if + ] ?if ; + : scan-word ( -- word/number/f ) - scan dup [ - dup search [ ] [ - dup string>number [ ] [ no-word ] ?if - ] ?if - ] when ; + scan dup [ parse-word ] when ; ERROR: staging-violation word ;