Home · Docs · Primer · Examples · Prefixing · Syntax · Rules · Parser · Tree · Actions · Debug


Hapy parser is a C++ object capable of producing a parsing tree (a Pree object) based on the given grammar and input data. This page documents public methods of the Parser class. Please see Primer and Prefix Parsing documents (in that order) for typical Parser API usage.

Parser class member Semantics
void grammar(const Rule &start) Specifies the grammar for the parser to use. The start rule is remembered but the grammar is not checked or optimized until the begin() method is called.
bool parse(const string &content) Parsers supplied string and returns true iff the entire string has matched the grammar. The parsing tree is stored in result().pree. The method assumes that the supplied content corresponds to the entire available input data. That is, it calls sawDataEnd(true) internally.
const Result &result() Returns result structure containing the result status code and parsing tree. The structure is set only when the method end() or parse() is called.
bool nextMatch() Returns true iff the same input can be parsed differently using the same grammar, implying grammar ambiguities. Note that ambiguity detection is implemented via standard Hapy backtracking mechanism and can be at least as "expensive" (e.g., slow and memory consuming) as finding the first match. The method updates result structure just like the parse() method does. This method must not be called unless a match was found during the last parse(), begin-step-end, or nextMatch() call.
bool begin() Checks and optimizes the grammar if that has not been done before. Returns false if the grammar is invalid. Parses already available data (which could be empty at the time). Returns true iff more data is expected and needed to declare (distinguish) a miss, match, or an error outcome. This method must be called before step() or end().
bool step() Resumes parsing, presumably after more information has been made available after the call to begin(). Returns true iff more data is expected and needed to declare (distinguish) a miss, match, or an error outcome. This method must be called after successful begin() or step() calls and before end(). Thus, it may be called multiple times.
bool end() Terminates parsing algorithm and determines the final result if that has not happened earlier. Always returns false (because more data is not expected after the call to end()). This method must be called after all begin() and step() calls.
void moveOn() Adjusts internal parser state so that begin-step-end sequence can be repeated again, for the remaining (unparsed) portion of the data, without recompiling the grammar and without re-supplying end-of-data information. This method must be called before repeated calls to begin() and may be called before the very first call to begin().
void pushData(const string &newData) Adds data to the parser buffer. This method may be called at any time.
bool hasData() Returns true iff the parser's buffer contains unparsed data.
bool sawDataEnd() Returns true iff sawDataEnd(true) has already been called.
void sawDataEnd(bool did) With a true argument, instructs the parser to assume that no more data is coming. The methods begin() and step() are guaranteed to return false after a sawDataEnd(true) call has been made.

The parsing tree interface is likely to evolve with time. For example, adding rule-specific node data may be needed.

SourceForge Home