Next: , Previous: , Up: The Text Interpreter   [Contents][Index]


6.14.5 Recognizers

The standard Forth text interpreter recognizes the following types of tokens: words in the dictionary, integer numbers, and floating point numbers. Defining new types of tokens isn’t yet standardized. Gforth provides recognizers to make the text interpreter extensible as well.

Recognizers take a string and return some data and a translator for interpreting that data. Gforth implements that translator as xt (executing it will perform the appropriate action to handle the token in the current state), but other Forth systems may implement it as actual table, with three xts inside. The first xt is the interpretation/run-time xt, it performs the interpretation semantics on the data (usually, this means it just leaves the data on the stack). The second xt performs the compilation semantics, it gets the data and the run-time semantics xt. The third xt perfoms the postpone semantics, it also gets the data and the run-time semantics xt. You can use >postpone to postpone the run-time xt.

Recognizers are organized as stack, so you can arrange the sequence of recognizers in the same way as the vocabulary stack. Recognizer stacks are themselves recognizers, i.e. they are executable, take a string and return a translator.

notfound ( state –  ) gforth-experimental “notfound”

If a recognizer fails, it returns notfound

rec-nt ( addr u – nt translate-nt | notfound  ) gforth-experimental “rec-nt”

recognize a name token

rec-num ( addr u – n/d table | notfound  ) gforth-experimental “rec-num”

converts a number to a single/double integer

rec-float ( addr u – r translate-float | notfound  ) gforth-experimental “rec-float”

recognize floating point numbers

rec-string ( addr u – addr u’ r:string | rectype-null  ) gforth-experimental “rec-string”

Convert strings enclosed in double quotes into string literals, escapes are treated as in S\".

rec-to ( addr u – xt r:to | rectype-null  ) gforth-experimental “rec-to”

words prefixed with -> are treated as if preceeded by TO or IS, with +> as +TO and with '> as ADDR.

rec-tick ( addr u – xt rectype-num | rectype-null  ) gforth-experimental “rec-tick”

words prefixed with ` return their xt. Example: `dup gives the xt of dup

rec-dtick ( addr u – nt rectype-num | rectype-null  ) gforth-experimental “rec-dtick”

words prefixed with `` return their nt. Example: ``S" gives the nt of S"

rec-body ( addr u – xt translate-tick | translate-null  ) gforth-experimental “rec-body”

words bracketed with '<' '>' return their body. Example: <dup> gives the body of dup

get-recognizers ( – xt1 .. xtn n  ) gforth-experimental “get-recognizers”

push the content on the recognizer stack

set-recognizers ( xt1 .. xtn n –  ) gforth-experimental “set-recognizers”

set the recognizer stack from content on the stack

recognize ( addr u rec-addr – ... rectype  ) gforth-experimental “recognize”

apply a recognizer stack to a string, delivering a token

recognizer-sequence: ( xt1 .. xtn n "name" –  ) gforth-experimental “recognizer-sequence:”

concatenate a stack of recognizers to one recognizer with the name "name". xtn is tried first, xt1 last, just like on the recognizer stack

forth-recognize ( c-addr u – ... translate-xt  ) recognizer “forth-recognize”

The system recognizer

forth-recognizer ( – xt  ) gforth-experimental “forth-recognizer”

backward compatible to Matthias Trute recognizer API

set-forth-recognize ( xt –  ) recognizer “set-forth-recognize”

Change the system recognizer

translate: ( int-xt comp-xt post-xt "name" –  ) gforth-experimental “translate:”

create a new recognizer table. Items are in order of STATE value, which are 0 or negative. Up to 7 slots are available for extensions.

translate-nt ( i*x nt – j*x  ) gforth-experimental “translate-nt”

translate a name token

translate-num ( x – | x  ) gforth-experimental “translate-num”

translate a number

translate-dnum ( dx – | dx  ) gforth-experimental “translate-dnum”

translate a double number

doc-translate-float

The following extension is only avaiable if you load recognizer-ext.fs:

doc-interpret-translator doc-compile-translator doc-postpone-translator doc-translator:


Next: Text Interpreter Hooks, Previous: Interpreter Directives, Up: The Text Interpreter   [Contents][Index]