Search in sources :

Example 76 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseNativeCallExpression.

protected NativeCallExpression parseNativeCallExpression(NativeCallContext ncc) {
    NativeCallExpression nativeExp = null;
    List<ParseTree> list = ncc.children;
    // nativeCall: nativeVarRefChain (nativeMethod|nativeArray| PERIOD nativeVarRefChain)*;
    NativeVarRefChainContext first = (NativeVarRefChainContext) list.get(0);
    List<TerminalNode> ids = first.Identifier();
    StringBuilder clsSb = new StringBuilder();
    // 是类静态调用还是实例调用
    boolean isCls = false;
    int i = 0;
    for (; i < ids.size(); i++) {
        String text = ids.get(i).getText();
        char c = text.charAt(0);
        if (c >= 'A' && c <= 'Z') {
            clsSb.append(text);
            isCls = true;
            break;
        } else {
            clsSb.append(text).append(".");
        }
    }
    ClassNode clsNode = null;
    InstanceNode insNode = null;
    if (isCls) {
        clsNode = new ClassNode(clsSb.toString());
        // 指向下一个属性或者静态方法
        i++;
    } else {
        // 变量的属性引用,回到第一个,构造一个变量
        String varName = ids.get(0).getText();
        VarRef ref = new VarRef(new VarAttribute[0], false, null, this.getBTToken(varName, ncc.start.getLine()));
        this.pbCtx.setVarPosition(varName, ref);
        insNode = new InstanceNode(ref);
        i = 1;
    }
    List<NativeNode> nativeList = new ArrayList<NativeNode>();
    for (int j = i; j < ids.size(); j++) {
        // 剩下的是属性
        NativeAtrributeNode attribute = new NativeAtrributeNode(ids.get(j).getText());
        nativeList.add(attribute);
    }
    for (int z = 1; z < list.size(); z++) {
        ParseTree tree = list.get(z);
        if (tree instanceof NativeMethodContext) {
            NativeMethodContext methodCtx = (NativeMethodContext) tree;
            NativeMethodNode methodNode = null;
            String method = null;
            NativeNode lastNode = nativeList.get(nativeList.size() - 1);
            if (lastNode instanceof NativeAtrributeNode) {
                method = ((NativeAtrributeNode) lastNode).attribute;
                // 
                nativeList.remove(nativeList.size() - 1);
            } else {
                String msg = null;
                if (lastNode instanceof NativeArrayNode) {
                    msg = "[]()";
                } else {
                    msg = "()()";
                }
                BeetlException ex = new BeetlException(BeetlException.PARSER_NATIVE_ERROR, msg);
                ex.pushToken(this.getBTToken(methodCtx.getStart()));
                throw ex;
            }
            // 解析参数
            List<ExpressionContext> expCtxList = methodCtx.expression();
            Expression[] exp = this.parseExpressionCtxList(expCtxList);
            methodNode = new NativeMethodNode(method, exp);
            nativeList.add(methodNode);
        } else if (tree instanceof NativeArrayContext) {
            ExpressionContext expCtx = ((NativeArrayContext) tree).expression();
            Expression exp = this.parseExpress(expCtx);
            NativeArrayNode arrayNode = new NativeArrayNode(exp);
            nativeList.add(arrayNode);
        } else if (tree instanceof NativeVarRefChainContext) {
            List<TerminalNode> nodes = ((NativeVarRefChainContext) tree).Identifier();
            for (TerminalNode node : nodes) {
                NativeAtrributeNode attributeNode = new NativeAtrributeNode(node.getText());
                nativeList.add(attributeNode);
            }
        } else {
            // 其他节点,这段语法写的不好,造成解析困难,但先这样了
            continue;
        }
    }
    NativeNode[] chain = nativeList.toArray(new NativeNode[0]);
    if (clsNode != null) {
        nativeExp = new NativeCallExpression(clsNode, chain, this.getBTToken(ncc.start));
    } else {
        nativeExp = new NativeCallExpression(insNode, chain, this.getBTToken(ncc.start));
    }
    return nativeExp;
}
Also used : VarRef(org.beetl.core.statement.VarRef) NativeArrayNode(org.beetl.core.statement.nat.NativeArrayNode) BeetlException(org.beetl.core.exception.BeetlException) InstanceNode(org.beetl.core.statement.nat.InstanceNode) ArrayList(java.util.ArrayList) NativeNode(org.beetl.core.statement.nat.NativeNode) NativeAtrributeNode(org.beetl.core.statement.nat.NativeAtrributeNode) ClassNode(org.beetl.core.statement.nat.ClassNode) NativeVarRefChainContext(org.beetl.core.parser.BeetlParser.NativeVarRefChainContext) NativeArrayContext(org.beetl.core.parser.BeetlParser.NativeArrayContext) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) NativeMethodNode(org.beetl.core.statement.nat.NativeMethodNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) NativeMethodContext(org.beetl.core.parser.BeetlParser.NativeMethodContext)

Example 77 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project vertexium by visallo.

the class CypherCstToAstVisitor method visitExpression4.

// + - prefix
@Override
public CypherAstBase visitExpression4(CypherParser.Expression4Context ctx) {
    int neg = 0;
    for (ParseTree child : ctx.children) {
        if (child instanceof TerminalNode && child.getText().equals("-")) {
            neg++;
        }
    }
    CypherAstBase expr = visitExpression3(ctx.expression3());
    if (neg % 2 == 1) {
        return new CypherNegateExpression(expr);
    } else {
        return expr;
    }
}
Also used : TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 78 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project vertexium by visallo.

the class CypherCstToAstVisitor method toBinaryExpressions.

private <T extends ParseTree> CypherBinaryExpression toBinaryExpressions(List<ParseTree> children, Function<T, CypherAstBase> itemTransform) {
    CypherAstBase left = null;
    CypherBinaryExpression.Op op = null;
    for (int i = 0; i < children.size(); i++) {
        ParseTree child = children.get(i);
        if (child instanceof TerminalNode) {
            CypherBinaryExpression.Op newOp = CypherBinaryExpression.Op.parseOrNull(child.getText());
            if (newOp != null) {
                if (op == null) {
                    op = newOp;
                } else {
                    throw new VertexiumException("unexpected op, found too many ops in a row");
                }
            }
        } else {
            // noinspection unchecked
            CypherAstBase childObj = itemTransform.apply((T) child);
            if (left == null) {
                left = childObj;
            } else {
                if (op == null) {
                    throw new VertexiumException("unexpected binary expression. expected an op between expressions");
                }
                left = new CypherBinaryExpression(left, op, childObj);
            }
            op = null;
        }
    }
    return (CypherBinaryExpression) left;
}
Also used : TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) VertexiumException(org.vertexium.VertexiumException)

Example 79 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class LoggingTreeSubPage method addPluginToList.

/**
 * Adds a new logging plugin to the list of logging plugins available
 * for a given component. If necessary also creates the list of logging
 * plugins.
 *
 * @param componentName
 *                the name of the component to add the plugin to.
 * @param pluginName
 *                the name of the plugin to add
 * @param path
 *                the path of the plugin to add, or null if none.
 */
private void addPluginToList(final String componentName, final String pluginName, final String path) {
    if (loggingSectionHandler == null) {
        return;
    }
    final StringBuilder pluginBuilder = new StringBuilder();
    pluginBuilder.append(pluginName);
    if (path != null && path.length() != 0) {
        pluginBuilder.append(" := \"").append(path).append('\"');
    }
    /*
		 *   loggingSectionHandler.getLastSectionRoot()
		 *     entry.getLoggerPluginsRoot()
		 *       entry.getLoggerPluginsListRoot()
		 *         {
		 *           pluginEntry.getLoggerPluginRoot()
		 *         }
		 */
    LoggingSectionHandler.LoggerPluginsEntry entry = loggingSectionHandler.getLoggerPluginsTree().get(componentName);
    if (entry == null) {
        entry = new LoggingSectionHandler.LoggerPluginsEntry();
        loggingSectionHandler.getLoggerPluginsTree().put(componentName, entry);
        final ParseTree loggerPluginsRoot = new ParserRuleContext();
        ConfigTreeNodeUtilities.addChild(loggingSectionHandler.getLastSectionRoot(), loggerPluginsRoot);
        entry.setLoggerPluginsRoot(loggerPluginsRoot);
        final StringBuilder builder = new StringBuilder();
        builder.append('\n').append(componentName).append(".LoggerPlugins := ");
        ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, new AddedParseTree(builder.toString()));
        ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, new AddedParseTree("{"));
        final ParseTree loggerPluginsListRoot = new ParserRuleContext();
        entry.setLoggerPluginsListRoot(loggerPluginsListRoot);
        final LoggingSectionHandler.LoggerPluginEntry pluginEntry = new LoggingSectionHandler.LoggerPluginEntry();
        final ParseTree pluginRoot = new ParserRuleContext();
        pluginEntry.setLoggerPluginRoot(pluginRoot);
        pluginEntry.setName(pluginName);
        pluginEntry.setPath(path);
        ConfigTreeNodeUtilities.addChild(pluginRoot, new AddedParseTree(pluginBuilder.toString()));
        entry.setPluginRoots(new HashMap<String, LoggingSectionHandler.LoggerPluginEntry>(1));
        entry.getPluginRoots().put(pluginName, pluginEntry);
        ConfigTreeNodeUtilities.addChild(loggerPluginsListRoot, pluginRoot);
        ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, loggerPluginsListRoot);
        ConfigTreeNodeUtilities.addChild(loggerPluginsRoot, new AddedParseTree("}"));
        return;
    }
    final int childCount = entry.getLoggerPluginsListRoot().getChildCount();
    ConfigTreeNodeUtilities.addChild(entry.getLoggerPluginsListRoot(), new AddedParseTree((childCount > 0 ? ", " : "") + pluginBuilder.toString()));
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LoggingSectionHandler(org.eclipse.titan.common.parsers.cfg.indices.LoggingSectionHandler) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 80 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class ModuleParameterSectionDropTargetListener method drop.

@Override
public void drop(final DropTargetEvent event) {
    if (ModuleParameterTransfer.getInstance().isSupportedType(event.currentDataType)) {
        if (event.item != null && viewer.getInput() != null) {
            ModuleParameterSectionHandler moduleParameterSectionHandler = (ModuleParameterSectionHandler) viewer.getInput();
            ModuleParameter element = (ModuleParameter) event.item.getData();
            ModuleParameter[] items = (ModuleParameter[]) event.data;
            int baseindex = moduleParameterSectionHandler.getModuleParameters().indexOf(element);
            final ParseTree parent = moduleParameterSectionHandler.getLastSectionRoot();
            ConfigTreeNodeUtilities.removeChild(parent, element.getRoot());
            ConfigTreeNodeUtilities.addChild(parent, element.getRoot(), baseindex);
            if (items.length > 0) {
                for (int i = 0; i < items.length - 1; i++) {
                    moduleParameterSectionHandler.getModuleParameters().add(++baseindex, items[i]);
                }
                moduleParameterSectionHandler.getModuleParameters().add(++baseindex, items[items.length - 1]);
            }
            viewer.refresh(true);
            editor.setDirty();
        }
    }
}
Also used : ModuleParameterSectionHandler(org.eclipse.titan.common.parsers.cfg.indices.ModuleParameterSectionHandler) ModuleParameter(org.eclipse.titan.common.parsers.cfg.indices.ModuleParameterSectionHandler.ModuleParameter) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)311 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)104 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)60 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)49 AddedParseTree (org.eclipse.titan.common.parsers.AddedParseTree)46 CharStream (org.antlr.v4.runtime.CharStream)43 Test (org.junit.Test)43 CommonToken (org.antlr.v4.runtime.CommonToken)35 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)22 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)22 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 Grammar (org.antlr.v4.tool.Grammar)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 IOException (java.io.IOException)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)16