bamboo/boo — pull request #71 by drslump (2013-10-25 16:26:12+00:00)
Title: Improve parser errors

Improves the error reporting produced by the parser based on ideas from a [paper by Clinton L. Jeffery](http://people.via.ecp.fr/~stilgar/doc/compilo/parser/Generating%20LR%20Syntax%20Error%20Messages.pdf).

The original algorithm is designed for parser tables, in order to adapt it to Boo's parser (a recursive-descend by Antlr) the tracing of rules must be enabled when generating the grammar. This allows to keep a stack of rules being recursed in order to be able to obtain the active rule when an error is reported.

Error examples are provided using a simple syntax inside the `error-patterns.boo` script, which besides holding the examples can be run to generate error patterns in C# for the parser project. It uses the parser to record (and also validate) the error "state" reported when the example code is parsed. With that information it creates the error patterns that can be used when building the parser again.

While the implementation is quite simple the results are excellent, and more importantly it can be easily improved upon by providing new example snippets. I didn't want to go too far with this since I'm already planning an overhaul of how the error reporting is managed in the whole compiler, targeting a solution similar to [clang's diagnostics](http://clang.llvm.org/diagnostics.html).

My biggest fear was having to keep a stack of recursed rules, however I haven't found it to make the compiler slower. I'm sure some micro-benchmarks would show that it's slower but I guess that the benefit is much bigger than the small speed penalty. Anyhow, if for any reason it gives trouble at some point, it can be easily disabled by removing the `-traceParser` switch from antlr.

Overview of the changes:

- Script to define, generate, debug and test error patterns
- Nant build script takes care of generating the error patterns file
- BooParser now accepts a ParserSettings object to customize it
- Parser now keeps a stack of rules being recursed
- Checks if the recursion exceeds a configurable limit to report an error instead of ending with a StackOverflow (see #56)