From c3cd99858c006b78e1ac6f5b9845c68e5ffea5cb Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 29 Feb 2012 08:14:42 -0600 Subject: [PATCH] Reduce size of generated code by emitting predicate text instead of full failed predicate message --- .../src/org/antlr/v4/runtime/FailedPredicateException.java | 4 ++-- tool/src/org/antlr/v4/codegen/model/SemPred.java | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java index ff6cc5576..0c577dc7d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java +++ b/runtime/Java/src/org/antlr/v4/runtime/FailedPredicateException.java @@ -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; } diff --git a/tool/src/org/antlr/v4/codegen/model/SemPred.java b/tool/src/org/antlr/v4/codegen/model/SemPred.java index 7bb2bb76f..344a09505 100644 --- a/tool/src/org/antlr/v4/codegen/model/SemPred.java +++ b/tool/src/org/antlr/v4/codegen/model/SemPred.java @@ -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; }