Instantiating Offline Parsers - RopleyIT/GLRParser GitHub Wiki
Offline parsers are created simply by invoking a static method of a parser factory.
Each offline parser will need to have its optional error and debug output
streams opened and attached after construction. Similarly, each offline parser
instance will need to have an input tokeniser created and attached by passing a
reference to it as an argument to the Parse
method of the parser.
In all other respects, the parser behaves and is programmed like the in-line parser. A simple example showing the creation and use of two parsers is given below:
public void DemoTwoParsers(TextReader input1, TextReader input2)
{
// Create and use the first parser
MyParser p = ParserFactory<MyParser>.CreateInstance();
p.ErrStream = new StringWriter();
MyTokeniser t = new MyTokeniser(input1, p.Tokens);
bool result1 = p.Parse(t);
// Create the second parser
MyParser q = ParserFactory<MyParser>.CreateInstance();
q.ErrStream = new StringWriter();
MyTokeniser u = new MyTokeniser(input2, q.Tokens);
bool result2 = q.Parse(u);
}
Careful use of thread pooling, or demultiplexing of input tokens based on which parser they should be fed to, can enable the use of multiple parsers in the same application for workflow scenarios very easily. Note that a parser itself is not written to be thread safe, but shares no writable data structures with other parsers while it is running, as the shared parser data tables are read only. Hence multiple parsers can be used within the same application on separate threads provided you take care to ensure thread safety among the application-specific parts of your parser class and other application classes.
Parser objects have an ErrorLevel
property that can be set to one of
the levels NONE, ERROR, WARN, VERBOSE, or DEBUG. These levels are members of the
MessageLevel enumeration. Later levels in the list include all messages that are
rendered by the earlier (lower) levels. NONE forces the parser to remain silent,
even in the presence of errors. ERROR reports errors, but skips warnings. WARN
includes non-fatal warnings in the output, while VERBOSE describes all the
shifts and reductions executed by the parser, albeit in a more succinct form than
DEBUG. The DEBUG level is always used on the parser's DebugStream if this is
not null. The default level if not otherwise set is WARN. See the example below:
MyParser p = ParserFactory<MyParser>.CreateInstance();
p.ErrorLevel = MessageLevel.VERBOSE;