4.1.6 Grammar Rule Definition
Here are procedures for defining rules for the syntax types introduced
in Precedence Parsing Overview.
For the rule-defining procedures that follow, the variable tk may
be a character, string, or symbol, or a list composed of characters,
strings, and symbols. Each element of tk is treated as though the
procedure were called for each element.
For procedures prec:delim, …, prec:prestfix, if the sop
argument is #f
, then the token which triggered this rule is
converted to a symbol and returned. A false sop argument to the
procedures prec:commentfix, prec:matchfix, or prec:inmatchfix has a
different meaning.
Character tk arguments will match only character tokens;
i.e. characters for which no token-group is assigned. Symbols and
strings will both match token strings; i.e. tokens resulting from token
groups.
- Function: prec:delim tk
Returns a rule specifying that tk should not be returned from
parsing; i.e. tk’s function is purely syntactic. The end-of-file
is always treated as a delimiter.
- Function: prec:nofix tk sop
Returns a rule specifying the following actions take place when tk
is parsed:
- If sop is a procedure, it is called with no arguments; the
resulting value is incorporated into the expression being built.
Otherwise, the list of sop is incorporated.
- Function: prec:prefix tk sop bp rule1 …
Returns a rule specifying the following actions take place when tk
is parsed:
- The rules rule1 … augment and, in case of conflict, override
rules currently in effect.
-
prec:parse1
is called with binding-power bp.
- If sop is a procedure, it is called with the expression returned
from
prec:parse1
; the resulting value is incorporated into the
expression being built. Otherwise, the list of sop and the
expression returned from prec:parse1
is incorporated.
- The ruleset in effect before tk was parsed is restored;
rule1 … are forgotten.
- Function: prec:infix tk sop lbp bp rule1 …
Returns a rule declaring the left-binding-precedence of the token
tk is lbp and specifying the following actions take place
when tk is parsed:
- The rules rule1 … augment and, in case of conflict, override
rules currently in effect.
- One expression is parsed with binding-power lbp. If instead a
delimiter is encountered, a warning is issued.
- If sop is a procedure, it is applied to the list of left and
the parsed expression; the resulting value is incorporated into the
expression being built. Otherwise, the list of sop, the
left expression, and the parsed expression is incorporated.
- The ruleset in effect before tk was parsed is restored;
rule1 … are forgotten.
- Function: prec:nary tk sop bp
Returns a rule declaring the left-binding-precedence of the token
tk is bp and specifying the following actions take place
when tk is parsed:
- Expressions are parsed with binding-power bp as far as they are
interleaved with the token tk.
- If sop is a procedure, it is applied to the list of left and
the parsed expressions; the resulting value is incorporated into the
expression being built. Otherwise, the list of sop, the
left expression, and the parsed expressions is incorporated.
- Function: prec:postfix tk sop lbp
Returns a rule declaring the left-binding-precedence of the token
tk is lbp and specifying the following actions take place
when tk is parsed:
- If sop is a procedure, it is called with the left expression;
the resulting value is incorporated into the expression being built.
Otherwise, the list of sop and the left expression is
incorporated.
- Function: prec:prestfix tk sop bp rule1 …
Returns a rule specifying the following actions take place when tk
is parsed:
- The rules rule1 … augment and, in case of conflict, override
rules currently in effect.
- Expressions are parsed with binding-power bp until a delimiter is
reached.
- If sop is a procedure, it is applied to the list of parsed
expressions; the resulting value is incorporated into the expression
being built. Otherwise, the list of sop and the parsed
expressions is incorporated.
- The ruleset in effect before tk was parsed is restored;
rule1 … are forgotten.
- Function: prec:commentfix tk stp match rule1 …
Returns rules specifying the following actions take place when tk
is parsed:
- The rules rule1 … augment and, in case of conflict, override
rules currently in effect.
- Characters are read until and end-of-file or a sequence of characters
is read which matches the string match.
- If stp is a procedure, it is called with the string of all that
was read between the tk and match (exclusive).
- The ruleset in effect before tk was parsed is restored;
rule1 … are forgotten.
Parsing of commentfix syntax differs from the others in several ways.
It reads directly from input without tokenizing; It calls stp but
does not return its value; nay any value. I added the stp
argument so that comment text could be echoed.
- Function: prec:matchfix tk sop sep match rule1 …
Returns a rule specifying the following actions take place when tk
is parsed:
- The rules rule1 … augment and, in case of conflict, override
rules currently in effect.
- A rule declaring the token match a delimiter takes effect.
- Expressions are parsed with binding-power
0
until the token
match is reached. If the token sep does not appear between
each pair of expressions parsed, a warning is issued.
- If sop is a procedure, it is applied to the list of parsed
expressions; the resulting value is incorporated into the expression
being built. Otherwise, the list of sop and the parsed
expressions is incorporated.
- The ruleset in effect before tk was parsed is restored;
rule1 … are forgotten.
- Function: prec:inmatchfix tk sop sep match lbp rule1 …
Returns a rule declaring the left-binding-precedence of the token
tk is lbp and specifying the following actions take place
when tk is parsed:
- The rules rule1 … augment and, in case of conflict, override
rules currently in effect.
- A rule declaring the token match a delimiter takes effect.
- Expressions are parsed with binding-power
0
until the token
match is reached. If the token sep does not appear between
each pair of expressions parsed, a warning is issued.
- If sop is a procedure, it is applied to the list of left and
the parsed expressions; the resulting value is incorporated into the
expression being built. Otherwise, the list of sop, the
left expression, and the parsed expressions is incorporated.
- The ruleset in effect before tk was parsed is restored;
rule1 … are forgotten.