Parsing Words
Factor handbook ยป Guided tour of Factor

Prev:Defining our first word
Next:Stack Shuffling


If you've been paying close attention so far, you will realize that you have been lied to. Most words act on the stack in order, but there a few words like [, ], : and ; that don't seem to follow this rule.

These are parsing words and they behave differently from ordinary words like 5, [1..b] or drop. We will cover these in more detail when we talk about metaprogramming, but for now it is enough to know that parsing words are special.

They are not defined using the : word, but with the word SYNTAX: instead. When a parsing words is encountered, it can interact with the parser using a well-defined API to influence how successive words are parsed. For instance : asks for the next token from the parser until ; is found and tries to compile that stream of tokens into a word definition.

A common use of parsing words is to define literals. For instance { is a parsing word that starts an array definition and is terminated by }. Everything in-between is part of the array. An example of array that we have seen before is { 1 2 3 4 5 6 7 8 9 10 } .

There are also literals for hashmaps, H{ { "Perl" "Larry Wall" } { "Factor" "Slava Pestov" } { "Scala" "Martin Odersky" } } , and byte arrays, B{ 1 14 18 23 } .

Other uses of parsing words include the module system, the object-oriented features of Factor, enums, memoized functions, privacy modifiers and more. In theory, even SYNTAX: can be defined in terms of itself, but the system has to be bootstrapped somehow.