use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class OutputModelController method buildRuleFunction.
/**
* Create RuleFunction per rule and update sempreds,actions of parser
* output object with stuff found in r.
*/
public void buildRuleFunction(Parser parser, Rule r) {
RuleFunction function = rule(r);
parser.funcs.add(function);
pushCurrentRule(function);
function.fillNamedActions(delegate, r);
if (r instanceof LeftRecursiveRule) {
buildLeftRecursiveRuleFunction((LeftRecursiveRule) r, (LeftRecursiveRuleFunction) function);
} else {
buildNormalRuleFunction(r, function);
}
Grammar g = getGrammar();
for (ActionAST a : r.actions) {
if (a instanceof PredAST) {
PredAST p = (PredAST) a;
RuleSempredFunction rsf = parser.sempredFuncs.get(r);
if (rsf == null) {
rsf = new RuleSempredFunction(delegate, r, function.ctxType);
parser.sempredFuncs.put(r, rsf);
}
rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
}
}
popCurrentRule();
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class ParserFactory method ruleRef.
@Override
public List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
InvokeRule invokeOp = new InvokeRule(this, ID, label);
// If no manual label and action refs as token/rule not label, we need to define implicit label
if (controller.needsImplicitLabel(ID, invokeOp))
defineImplicitLabel(ID, invokeOp);
AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(invokeOp, label);
return list(invokeOp, listLabelOp);
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class RuleDependencyChecker method getRuleVersions.
private static int[] getRuleVersions(Class<? extends Recognizer<?, ?>> recognizerClass, String[] ruleNames) {
int[] versions = new int[ruleNames.length];
Field[] fields = recognizerClass.getFields();
for (Field field : fields) {
boolean isStatic = (field.getModifiers() & Modifier.STATIC) != 0;
boolean isInteger = field.getType() == Integer.TYPE;
if (isStatic && isInteger && field.getName().startsWith("RULE_")) {
try {
String name = field.getName().substring("RULE_".length());
if (name.isEmpty() || !Character.isLowerCase(name.charAt(0))) {
continue;
}
int index = field.getInt(null);
if (index < 0 || index >= versions.length) {
Object[] params = { index, field.getName(), recognizerClass.getSimpleName() };
LOGGER.log(Level.WARNING, "Rule index {0} for rule ''{1}'' out of bounds for recognizer {2}.", params);
continue;
}
Method ruleMethod = getRuleMethod(recognizerClass, name);
if (ruleMethod == null) {
Object[] params = { name, recognizerClass.getSimpleName() };
LOGGER.log(Level.WARNING, "Could not find rule method for rule ''{0}'' in recognizer {1}.", params);
continue;
}
RuleVersion ruleVersion = ruleMethod.getAnnotation(RuleVersion.class);
int version = ruleVersion != null ? ruleVersion.value() : 0;
versions[index] = version;
} catch (IllegalArgumentException ex) {
LOGGER.log(Level.WARNING, null, ex);
} catch (IllegalAccessException ex) {
LOGGER.log(Level.WARNING, null, ex);
}
}
}
return versions;
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class Trees method getNodeText.
public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) {
if (ruleNames != null) {
if (t instanceof RuleNode) {
RuleContext ruleContext = ((RuleNode) t).getRuleContext();
int ruleIndex = ruleContext.getRuleIndex();
String ruleName = ruleNames.get(ruleIndex);
int altNumber = ruleContext.getAltNumber();
if (altNumber != ATN.INVALID_ALT_NUMBER) {
return ruleName + ":" + altNumber;
}
return ruleName;
} else if (t instanceof ErrorNode) {
return t.toString();
} else if (t instanceof TerminalNode) {
Token symbol = ((TerminalNode) t).getSymbol();
if (symbol != null) {
String s = symbol.getText();
return s;
}
}
}
// no recog for rule names
Object payload = t.getPayload();
if (payload instanceof Token) {
return ((Token) payload).getText();
}
return t.getPayload().toString();
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class XPath method split.
// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
XPathLexer lexer = new XPathLexer(CharStreams.fromString(path)) {
@Override
public void recover(LexerNoViableAltException e) {
throw e;
}
};
lexer.removeErrorListeners();
lexer.addErrorListener(new XPathLexerErrorListener());
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
try {
tokenStream.fill();
} catch (LexerNoViableAltException e) {
int pos = lexer.getCharPositionInLine();
String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
throw new IllegalArgumentException(msg, e);
}
List<Token> tokens = tokenStream.getTokens();
// System.out.println("path="+path+"=>"+tokens);
List<XPathElement> elements = new ArrayList<XPathElement>();
int n = tokens.size();
int i = 0;
loop: while (i < n) {
Token el = tokens.get(i);
Token next = null;
switch(el.getType()) {
case XPathLexer.ROOT:
case XPathLexer.ANYWHERE:
boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
i++;
next = tokens.get(i);
boolean invert = next.getType() == XPathLexer.BANG;
if (invert) {
i++;
next = tokens.get(i);
}
XPathElement pathElement = getXPathElement(next, anywhere);
pathElement.invert = invert;
elements.add(pathElement);
i++;
break;
case XPathLexer.TOKEN_REF:
case XPathLexer.RULE_REF:
case XPathLexer.WILDCARD:
elements.add(getXPathElement(el, false));
i++;
break;
case Token.EOF:
break loop;
default:
throw new IllegalArgumentException("Unknowth path element " + el);
}
}
return elements.toArray(new XPathElement[0]);
}
Aggregations