Offline parsers are created simply by creating a parser factory using
new
. The parser factory is then used to create the actual parser instances.
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
ParserFactory<MyParser> pf =
new ParserFactory<MyParser>();
MyParser p = pf.CreateInstance();
p.ErrStream = new StringWriter();
MyTokeniser t = new MyTokeniser(input1, p.Tokens);
bool result1 = p.Parse(t);
// Create the second parser
MyParser q = pf.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.