Programming Language for Old Timers


by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.


Previous page   Table of Contents   Next page


Parsing Helper Functions

The following methods are useful helpers for parsing. Their names are in the compiler module (except for =).

(x is name) = (y is name)
Two names are equal if they must refer to the same definition when used in the same scope. In other words, their spellings are the same and their contexts are eq. If either name is a simple-name, = is the same as eq.

same-spelling?(x is name, y is name)
True if the two names' spellings are the same, ignoring alphabetic case. Use this to compare absolute particles.

punctuation?(x is name)
True if x is punctuation.

match?(x is name, ts is token-stream, key: skip-newline = true,
       result: match? is boolean)
If skip-newline is true and the next token is a newline, look at the token after that, otherwise look at the next token. If this token is a name and matches x using same-spelling?, advance the token-stream past this token and return true, else false. There are analogous methods for keywords and literals that compare with =.

match!(x is name, ts is token-stream, key: skip-newline = true,
       result: match? is boolean)
If skip-newline is true and the next token is a newline, look at the token after that, otherwise look at the next token. If this token is a name and matches x using same-spelling?, advance the token-stream past this token and return true, else call wrong-token-error. There are analogous methods for keywords and literals that compare with =.

match(x, ts is token-stream, error? is boolean, key: skip-newline = true,
      result: match? is boolean)
Call match? or match! depending on error?.

match-bound-particle?(value, ts is token-stream, key: skip-newline = true,
                      result: match? is boolean)
If skip-newline is true and the next token is a newline, look at the token after that, otherwise look at the next token. If this token is a name and has a known definition of value, advance the token-stream past this token and return true, else false. This is how you match an optional bound particle.

match-bound-particle!(name, value, ts is token-stream, key: skip-newline = true,
                      result: match? is boolean)
If skip-newline is true and the next token is a newline, look at the token after that, otherwise look at the next token. If this token is a name and has a known definition of value, advance the token-stream past this token and return true, else call wrong-token-error. This is how you match a required bound particle.

match-bound-particle(name, value, ts is token-stream, error? is boolean, key: skip-newline = true,
                     result: match? is boolean)
Call match-bound-particle? or match-bound-particle! depending on error?.

begin-line-group?(ts is token-stream,
                  result: previous-indentation? is integer or boolean)
Implement pattern matching rules for the start of a line group. The result is the previous indentation if the match succeeds or false if the match fails. The result is 1's complemented if there was no newline.

continue-line-group?(ts is token-stream, previous-indentation is integer,
                     result: match? is boolean)
Implement pattern matching rules for the next repetition of a line group.

end-line-group(ts is token-stream, previous-indentation is integer)
Leave a line group during pattern matching. This restores the previous current-indentation. When the line-group began on a new line, it must end on a new line that is not a continuation line.

match-line-group?(ts is token-stream, previous-indentation,
                  error? is boolean, repeat? is boolean,
                  result: previous-indentation? is integer or boolean)
Call begin-line-group? or continue-line-group? depending on repeat? and deal with failure to match according to error?. If repeat? is false, the result is the previous indentation if it matches or false if it does not match. If repeat? is true, the result is true if it matches or false if it doesn't. This implements the line-break pattern element ^.

wrong-token-error(ts is token-stream, expected is anything)
Signal a parsing error. The error message indicates that expected was expected but next-after-newline(ts) was seen. The expected argument can be a string or a collection of names or strings. The ts argument also supplies the source-location.


Previous page   Table of Contents   Next page