# 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.

## 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

## Fixed 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 , with ancestor classes FunctionClosure < Function < Thing.

### Menu

#### simple parsers

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

#### making new parsers from old ones

• optP -- making a parser optional
• orP -- parsing alternatives
• andP -- parser conjunction
• * Parser -- repetition of a parser
• + Parser -- repetition of a parser at least once
• Function % Parser -- transform the value returned by a parser