Search in sources :

Example 11 with TerminalNode

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);
}
Also used : AStatement(org.elasticsearch.painless.node.AStatement) SFunction(org.elasticsearch.painless.node.SFunction) FunctionReserved(org.elasticsearch.painless.node.SFunction.FunctionReserved) ArrayList(java.util.ArrayList) EString(org.elasticsearch.painless.node.EString) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) DecltypeContext(org.elasticsearch.painless.antlr.PainlessParser.DecltypeContext) StatementContext(org.elasticsearch.painless.antlr.PainlessParser.StatementContext)

Example 12 with TerminalNode

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);
}
Also used : SegmentListCode(jetbrick.template.parser.code.SegmentListCode) ScopeCode(jetbrick.template.parser.code.ScopeCode) BlockCode(jetbrick.template.parser.code.BlockCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) TemplateClassCode(jetbrick.template.parser.code.TemplateClassCode) TextCode(jetbrick.template.parser.code.TextCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) MacroCode(jetbrick.template.parser.code.MacroCode) Code(jetbrick.template.parser.code.Code) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) TagCode(jetbrick.template.parser.code.TagCode) Type_argumentsContext(jetbrick.template.parser.grammer.JetTemplateParser.Type_argumentsContext) TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Type_array_suffixContext(jetbrick.template.parser.grammer.JetTemplateParser.Type_array_suffixContext)

Example 13 with TerminalNode

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);
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 14 with TerminalNode

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);
    }
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) Token(org.antlr.v4.runtime.Token) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 15 with TerminalNode

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;
}
Also used : ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Aggregations

TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)19 SegmentCode (jetbrick.template.parser.code.SegmentCode)7 Token (org.antlr.v4.runtime.Token)6 ParseTree (org.antlr.v4.runtime.tree.ParseTree)5 TextCode (jetbrick.template.parser.code.TextCode)3 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 BlockCode (jetbrick.template.parser.code.BlockCode)2 Code (jetbrick.template.parser.code.Code)2 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)2 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)2 MacroCode (jetbrick.template.parser.code.MacroCode)2 ScopeCode (jetbrick.template.parser.code.ScopeCode)2 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)2 TagCode (jetbrick.template.parser.code.TagCode)2 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)2 RuleContext (org.antlr.v4.runtime.RuleContext)2