use of org.antlr.v4.runtime.tree.TerminalNode in project elasticsearch by elastic.
the class Walker method visitFunction.
@Override
public ANode visitFunction(FunctionContext ctx) {
reserved.push(new FunctionReserved());
String rtnType = ctx.decltype().getText();
String name = ctx.ID().getText();
List<String> paramTypes = new ArrayList<>();
List<String> paramNames = new ArrayList<>();
List<AStatement> statements = new ArrayList<>();
for (DecltypeContext decltype : ctx.parameters().decltype()) {
paramTypes.add(decltype.getText());
}
for (TerminalNode id : ctx.parameters().ID()) {
paramNames.add(id.getText());
}
for (StatementContext statement : ctx.block().statement()) {
statements.add((AStatement) visit(statement));
}
return new SFunction((FunctionReserved) reserved.pop(), location(ctx), rtnType, name, paramTypes, paramNames, statements, false);
}
use of org.antlr.v4.runtime.tree.TerminalNode in project jetbrick-template-1x by subchen.
the class JetTemplateCodeVisitor method visitType.
@Override
public Code visitType(TypeContext ctx) {
StringBuilder name = new StringBuilder();
for (TerminalNode node : ctx.IDENTIFIER()) {
if (name.length() > 0) {
name.append('.');
}
name.append(node.getText());
}
// 查找 klass
Class<?> klass = resolver.resolveClass(name.toString());
if (klass == null) {
StringBuilder sb = new StringBuilder(128);
sb.append("java.lang.ClassNotFoundException: ").append(name);
sb.append("\n advise: Please define package in 'import.packages' or use full qualified class name.");
throw reportError(sb.toString(), ctx);
}
if (securityManager != null) {
securityManager.checkMemberAccess(klass);
}
// 查找泛型类型 typeArgs
TypedKlass[] typeArgs = TypedKlass.EMPTY_TYPE_ARGS;
Type_argumentsContext type_arguments = ctx.type_arguments();
if (type_arguments != null) {
SegmentListCode c = (SegmentListCode) type_arguments.accept(this);
typeArgs = new TypedKlass[c.size()];
for (int i = 0; i < typeArgs.length; i++) {
typeArgs[i] = c.getChild(i).getTypedKlass();
}
}
// 如果是数组类型,则把 klass 转成数组
String array_suffix = "";
List<Type_array_suffixContext> type_array_suffix = ctx.type_array_suffix();
for (Type_array_suffixContext c : type_array_suffix) {
Code code = c.accept(this);
array_suffix = array_suffix + code.toString();
}
if (array_suffix.length() > 0) {
// 转换成 Array Class, 重新 resolve
String klassName = name.toString() + array_suffix;
klass = resolver.resolveClass(klassName);
if (klass == null) {
throw reportError("java.lang.ClassNotFoundException: " + klassName, ctx);
}
}
// 返回带有的泛型信息的 Class
TypedKlass typedKlass = TypedKlass.create(klass, typeArgs);
return new SegmentCode(typedKlass, typedKlass.toString(), ctx);
}
use of org.antlr.v4.runtime.tree.TerminalNode in project jetbrick-template-1x by subchen.
the class JetTemplateCodeVisitor method visitStatic_type_name.
@Override
public Code visitStatic_type_name(Static_type_nameContext ctx) {
List<TerminalNode> name_list = ctx.IDENTIFIER();
StringBuilder sb = new StringBuilder();
for (TerminalNode node : name_list) {
if (sb.length() > 0) {
sb.append('.');
}
sb.append(node.getText());
}
return new SegmentCode(TypedKlass.NULL, sb.toString(), ctx);
}
use of org.antlr.v4.runtime.tree.TerminalNode in project jetbrick-template-1x by subchen.
the class JetTemplateCodeVisitor method visitConstant.
@Override
public Code visitConstant(ConstantContext ctx) {
Token token = ((TerminalNode) ctx.getChild(0)).getSymbol();
String text = token.getText();
switch(token.getType()) {
case JetTemplateParser.STRING_DOUBLE:
return new SegmentCode(String.class, text, ctx);
case JetTemplateParser.STRING_SINGLE:
text = StringEscapeUtils.asCanonicalJavaString(text);
return new SegmentCode(String.class, text, ctx);
case JetTemplateParser.INTEGER:
case JetTemplateParser.INTEGER_HEX:
case JetTemplateParser.FLOATING_POINT:
Class<?> klass;
if (text.endsWith("l") || text.endsWith("L")) {
klass = Long.TYPE;
} else if (text.endsWith("f") || text.endsWith("F")) {
klass = Float.TYPE;
} else if (text.endsWith("d") || text.endsWith("D")) {
klass = Double.TYPE;
} else if (token.getType() == JetTemplateParser.FLOATING_POINT) {
// 浮点数默认是double
klass = Double.TYPE;
} else {
klass = Integer.TYPE;
}
return new SegmentCode(klass, text, ctx);
case JetTemplateParser.KEYWORD_TRUE:
return new SegmentCode(Boolean.TYPE, text, ctx);
case JetTemplateParser.KEYWORD_FALSE:
return new SegmentCode(Boolean.TYPE, text, ctx);
case JetTemplateParser.KEYWORD_NULL:
return new SegmentCode(TypedKlass.NULL, text, ctx);
default:
throw reportError("Unexpected token type :" + token.getType(), ctx);
}
}
use of org.antlr.v4.runtime.tree.TerminalNode in project antlr4 by antlr.
the class Parser method consume.
/**
* Consume and return the {@linkplain #getCurrentToken current symbol}.
*
* <p>E.g., given the following input with {@code A} being the current
* lookahead symbol, this function moves the cursor to {@code B} and returns
* {@code A}.</p>
*
* <pre>
* A B
* ^
* </pre>
*
* If the parser is not in error recovery mode, the consumed symbol is added
* to the parse tree using {@link ParserRuleContext#addChild(TerminalNode)}, and
* {@link ParseTreeListener#visitTerminal} is called on any parse listeners.
* If the parser <em>is</em> in error recovery mode, the consumed symbol is
* added to the parse tree using {@link #createErrorNode(ParserRuleContext, Token)} then
* {@link ParserRuleContext#addErrorNode(ErrorNode)} and
* {@link ParseTreeListener#visitErrorNode} is called on any parse
* listeners.
*/
public Token consume() {
Token o = getCurrentToken();
if (o.getType() != EOF) {
getInputStream().consume();
}
boolean hasListener = _parseListeners != null && !_parseListeners.isEmpty();
if (_buildParseTrees || hasListener) {
if (_errHandler.inErrorRecoveryMode(this)) {
ErrorNode node = _ctx.addErrorNode(createErrorNode(_ctx, o));
if (_parseListeners != null) {
for (ParseTreeListener listener : _parseListeners) {
listener.visitErrorNode(node);
}
}
} else {
TerminalNode node = _ctx.addChild(createTerminalNode(_ctx, o));
if (_parseListeners != null) {
for (ParseTreeListener listener : _parseListeners) {
listener.visitTerminal(node);
}
}
}
}
return o;
}
Aggregations