public static SemanticContext.Predicate getQidPredicate(@NonNull ATN atn) { int predicateIndex = -1; for (ATNState state : atn.states) { if (state.ruleIndex != GoParser.RULE_qualifiedIdentifier) { continue; } for (int i = 0; i < state.getNumberOfOptimizedTransitions(); i++) { Transition transition = state.getOptimizedTransition(i); if (transition instanceof PredicateTransition) { predicateIndex = ((PredicateTransition)transition).predIndex; } } } assert predicateIndex >= 0 : "Couldn't find the QID predicate transition."; return new SemanticContext.Predicate(GoParser.RULE_qualifiedIdentifier, predicateIndex, false); }
public static String getSemanticContextDisplayString(PredicateEvalInfo pred, PreviewState previewState, SemanticContext semctx, int alt, boolean result) { Grammar g = previewState.g; String semanticContextDisplayString = g.getSemanticContextDisplayString(semctx); if ( semctx instanceof SemanticContext.PrecedencePredicate ) { int ruleIndex = previewState.parsingResult.parser.getATN().decisionToState.get(pred.decision).ruleIndex; Rule rule = g.getRule(ruleIndex); int precedence = ((SemanticContext.PrecedencePredicate) semctx).precedence; // precedence = n - originalAlt + 1, So: int originalAlt = rule.getOriginalNumberOfAlts()-precedence+1; alt = originalAlt; } return semanticContextDisplayString+" => alt "+alt+" is "+result; }
/** Given an arbitrarily complex SemanticContext, walk the "tree" and get display string. * Pull predicates from grammar text. */ public String getSemanticContextDisplayString(SemanticContext semctx) { if ( semctx instanceof SemanticContext.Predicate ) { return getPredicateDisplayString((SemanticContext.Predicate)semctx); } if ( semctx instanceof SemanticContext.AND ) { SemanticContext.AND and = (SemanticContext.AND)semctx; return joinPredicateOperands(and, " and "); } if ( semctx instanceof SemanticContext.OR ) { SemanticContext.OR or = (SemanticContext.OR)semctx; return joinPredicateOperands(or, " or "); } return semctx.toString(); }
public String joinPredicateOperands(SemanticContext.Operator op, String separator) { StringBuilder buf = new StringBuilder(); for (SemanticContext operand : op.getOperands()) { if (buf.length() > 0) { buf.append(separator); } buf.append(getSemanticContextDisplayString(operand)); } return buf.toString(); }
public String getPredicateDisplayString(SemanticContext.Predicate pred) { if ( indexToPredMap==null ) { indexToPredMap = getIndexToPredicateMap(); } ActionAST actionAST = indexToPredMap.get(pred.predIndex); return actionAST.getText(); }
public PredPrediction(SemanticContext pred, int alt) { this.alt = alt; this.pred = pred; }