i1 : 2+ |
Parsing is determined by a triple of numbers attached to each token. The following table (produced by seeParsing), displays each of these numbers.
i2 : seeParsing() |
When an operator or token is encountered, its scope serves as the level for parsing the subsequent expression, unless the current level is higher, in which case it is used.
Consider a binary operator such as *. The relationship between its scope and its precedence turns out to determine whether a*b*c is parsed as (a*b)*c or as a*(b*c). When the parser encounters the second *, the current parsing level is equal to the scope of the first *. If the scope is less than the precedence, then the second * becomes part of the right hand operand of the first *, and the expression is parsed as a*(b*c). Otherwise, the expression is parsed as (a*b)*c.
For unary operators, the strength is used instead of the scope to reset the current level. The reason for having both numbers is that some operators can be either unary or binary, depending on the context. A good example is # which binds as tightly as . when used as an infix operator, and binds as loosely as adjacency or function application when used as a prefix operator.
To handle expressions like b c d, where there are no tokens present which can serve as a binary multiplication operator, after parsing b, the level will be set to 1 less than the precedence of an identifier, so that b c d will be parsed as b (c d).
The exclamation point is allowed as a unary operator either to the right or to the left of its operand. The other unary operators occur to the left of their operands.
Three operators are treated specially, in that the empty expression is allowed to the right of them. These are newline, comma, and semicolon.