Posted By:
Lance_Walton
Posted On:
Monday, October 13, 2003 04:31 AM
Hi. I'm trying to accumulate syntax and semantic errors in my parser and tree walker so that I can display them as a list. It's working fine in my parser, but not my tree walker. This is the relevant parser code: class ThingParser extends Parser; options { exportVocab=Thing; defaultErrorHandler=false; k=4; buildAST=true; } { List syntaxErrors = new ArrayList(); } schemaCreation: { syntaxErrors.clear(); } (creationStatement)* EOF! { if (!syntaxErrors.isEmpty()) { throw new ParseException((ParseError[]) syntaxErrors.toArray(new ParseError[syntaxErrors.size()]));
More>>
Hi.
I'm trying to accumulate syntax and semantic errors in my parser and tree walker so that I can display them as a list. It's working fine in my parser, but not my tree walker.
This is the relevant parser code:
class ThingParser extends Parser;
options {
exportVocab=Thing;
defaultErrorHandler=false;
k=4;
buildAST=true;
}
{
List syntaxErrors = new ArrayList();
}
schemaCreation:
{
syntaxErrors.clear();
}
(creationStatement)* EOF!
{
if (!syntaxErrors.isEmpty()) {
throw new ParseException((ParseError[]) syntaxErrors.toArray(new ParseError[syntaxErrors.size()]));
}
}
;
protected creationStatement:
(
THING thingCreation
| ANOTHER_THING anotherThingCreation
)
(SEMICOLON!)?
; exception
catch [RecognitionException re] {
syntaxErrors.add(new ParseError(re.getLine(), re.getColumn(), re.getMessage()));
BitSet bits = new BitSet(Math.max(THING, ANOTHER_THING));
bits.add(THING);
bits.add(ANOTHER_THING);
consumeUntil(bits);
}
This works fine (it accumulates a list of errors, and at the EOF, it throws a ParseException if there were any errors).
I've tried something similar in my tree walker. Here's the code:
class ThingTreeWalker extends TreeParser;
options {
importVocab=Thing;
defaultErrorHandler=false;
k=1;
buildAST=false;
}
{
List errors = new ArrayList();
}
schemaCreation[Schema schema]:
{
errors.clear();
}
(creationStatement[schema])*
{
if (errors.size() > 0) {
throw new ParseException((ParseError[]) errors.toArray(new ParseError[errors.size()]));
}
}
;
protected creationStatement[Schema schema]:
thingCreation[schema]
| anotherThingCreation[schema]
; exception
catch [SchemaException sex] {
errors.add(new ParseError(sex.getMessage()));
}
There's no consumeUntil() since TreeParser doesn't have this method. Also in this case, rather than RecognitionExceptions, there are my own SchemaExceptions.
The problem is that when the creationStatement rule is terminated early by a SchemaException the schemaCreation rule subsequently exits (i.e. it throws a ParseException with the single ParseError). Since I'm trying to collect all of the errors, this exit occurs too early.
Can anyone help?
Lance
<<Less