Reduce size of generated code by emitting predicate text instead of full failed predicate message

This commit is contained in:
Sam Harwell 2012-02-29 08:14:42 -06:00
parent dd69a7532d
commit c3cd99858c
2 changed files with 6 additions and 3 deletions

View File

@ -46,13 +46,13 @@ public class FailedPredicateException extends RecognitionException {
this(recognizer, null);
}
public FailedPredicateException(Parser recognizer, @Nullable String msg) {
public FailedPredicateException(Parser recognizer, @Nullable String predicate) {
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s);
PredicateTransition trans = (PredicateTransition)s.transition(0);
ruleIndex = trans.ruleIndex;
predIndex = trans.predIndex;
this.msg = msg;
this.msg = String.format("failed predicate: {%s}?", predicate);
Token la = recognizer.getCurrentToken();
this.offendingToken = la;
}

View File

@ -50,7 +50,10 @@ public class SemPred extends Action {
GrammarAST failNode = ((PredAST)ast).getOption("fail");
CodeGenerator gen = factory.getGenerator();
if ( failNode==null ) {
msg = "failed predicate: "+ast.getText();
msg = ast.getText();
if (msg.startsWith("{") && msg.endsWith("}?")) {
msg = msg.substring(1, msg.length() - 2);
}
msg = gen.target.getTargetStringLiteralFromString(msg);
return;
}