Macaulay2 » Documentation
Packages » Parsing » Parser
next | previous | forward | backward | up | index | toc

Parser -- the class of all parsers

Description

A parser is a type of function that parses a sequence of tokens. Tokens can be anything except null. A parser p is called repeatedly, like this: p t, one token at a time. The return value that indicates acceptance of the input token is a new parser, which replaces the old, and is ready to accept the next token; the original parser p should not change its internal state. The return value that indicates rejection of the input token is null. Rejection may be interpreted as a syntax error.

When the input stream is exhausted, we call p one more time like this: p null. The return value is null if the parser is not in a terminal state. Otherwise the return value is the parsed (and possibly evaluated) result.

Menu

simple parsers

making new parsers from old ones

Methods that use a parser:

  • * Parser -- repetition of a parser
  • + Parser -- repetition of a parser at least once
  • Parser @ Parser -- see andP -- parser conjunction
  • Parser @ String -- see andP -- parser conjunction
  • String @ Parser -- see andP -- parser conjunction
  • Function % Parser -- transform the value returned by a parser
  • Parser | Parser -- see orP -- parsing alternatives
  • Parser | String -- see orP -- parsing alternatives
  • String | Parser -- see orP -- parsing alternatives
  • Parser : Analyzer -- combine a parser with a lexical analyzer to make a complete system

Protected objects of class Parser:

  • deadParser -- a parser which accepts no tokens and is not in a terminal state
  • letterParser -- a parser that accepts a single letter and returns it
  • NNParser -- a parser that accepts (and returns) a natural number, one character at a time
  • nullParser -- a terminal parser that returns the value nil
  • optionalSignParser -- a parser that accepts an optional plus sign or minus sign
  • QQParser -- a parser that accepts (and returns) a rational number, one character at a time
  • ZZParser -- a parser that accepts (and returns) an integer, one character at a time

For the programmer

The object Parser is a self initializing type, with ancestor classes FunctionClosure < Function < Thing.


The source of this document is in Parsing.m2:218:0.