@Override public void enterPattern_matcher(SQLParser.Pattern_matcherContext ctx) { super.enterPattern_matcher(ctx); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); if (child instanceof TerminalNodeImpl) { if (isNot(child.getText())) { currentPredicate.negate(); } break; } } if (currentPredicate.isNegated()) { currentPredicate.setOperator("not like"); } else { currentPredicate.setOperator("like"); } skipNextTerminal = true; }
public PlanNode callSql(String sqlstring, ArrayList list) throws FileNotFoundException, IOException { for (int i=0; i< list.size() ; i++) { sqlstring = sqlstring.replaceFirst("\\?", (String) list.get(i)); } ANTLRInputStream input = new ANTLRInputStream( new ByteArrayInputStream(sqlstring.getBytes())); PLSQLLexer lexer = new PLSQLLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); PLSQLParser parser = new PLSQLParser(tokens); parser.setBuildParseTree(true); ParseTree tree = parser.sql_statement(); ParseTreeWalker walker = new ParseTreeWalker(); SQLModel model = new SQLModel(); walker.walk(new MyListener(model), tree); return model.current; }
public GraphParser(InputStream is) throws GraphParserException { DOTLexer lexer = null; DOTParser parser = null; try { lexer = new DOTLexer(new ANTLRInputStream(is)); lexer.addErrorListener(new ErrorListener()); parser = new DOTParser(new CommonTokenStream(lexer)); parser.setErrorHandler(new ExceptionErrorStrategy()); parser.addErrorListener(new ErrorListener()); ParseTree tree = parser.graph(); ParseTreeWalker.DEFAULT.walk(new NodeListener(), tree); ParseTreeWalker.DEFAULT.walk(new EdgeListener(), tree); } catch (Throwable t) { if (mErrMsg != null) throw new GraphParserException(mErrMsg, t); if (lexer != null) { mErrMsg = "at line " + lexer.getLine() + ":" + lexer.getCharPositionInLine(); throw new GraphParserException(mErrMsg, t); } throw new GraphParserException(t); } if (mErrMsg != null) throw new GraphParserException(mErrMsg); }
public Expression getFunctionTypeEndingWithVariadic(String varName, ParseTree node) { String[] params = varName.split("\\$"); if(params.length < 2) return null; for(int i = params.length - 1; i >= 1; i--) { String param = params[i]; for(int j = i + 1; j < params.length; j++) if(!params[j].equals(param)) return null; String subVarName = ""; for(int j = 0; j < i; j++) subVarName += (j > 0 ? "$" : "") + params[j]; subVarName += "$_Array"; CacheObject cache = findCache(subVarName, node); /*if(cache != null && cache.type instanceof FunctionType) { List<AbstractType> parameterTypes = ((FunctionType)cache.type).parameterTypes; if(!parameterTypes.get(parameterTypes.size() - 1).resulting(null).swiftType().equals(param.split("_")[1])) continue; return new Expression(subVarName, cache.type); }*/ } return null; }
@Override public Object visitApiFacets(RAMLParser.ApiFacetsContext ctx) { // TODO move creation of security schemes to 1 pass final RAMLParser.SecuritySchemesFacetContext securitySchemesFacet = ctx.securitySchemesFacet(); if (securitySchemesFacet != null) { visitSecuritySchemesFacet(securitySchemesFacet); } for (int i = 0; i < ctx.getChildCount(); i++) { final ParseTree child = ctx.getChild(i); if (child != securitySchemesFacet) { child.accept(this); } } return scope.eObject(); }
/** * Cloning expression to create new same expression. */ public static ExprContext cloneExprContext(final ExprContext expr) { final ExprContext clone = createContextType(expr); clone.copyFrom(expr); for (final ParseTree child : expr.children) { if (child instanceof TerminalNode) { clone.addChild(new TerminalNodeImpl(((TerminalNode) child).getSymbol())); } else if (child instanceof ExprContext) { final ExprContext cloneChild = cloneExprContext((ExprContext) child); clone.addChild(cloneChild); setLeftRight(clone, cloneChild); } else if (child instanceof Token) { clone.addChild(new CommonToken((Token) child)); } } return clone; }
/** * Checks that no exception is generated for YANG file with valid syntax. */ @Test public void checkValidYangFileForNoSyntaxError() throws IOException { ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang"); // Create a lexer that feeds off of input char stream. GeneratedYangLexer lexer = new GeneratedYangLexer(input); // Create a buffer of tokens pulled from the lexer. CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a parser that feeds off the tokens buffer. GeneratedYangParser parser = new GeneratedYangParser(tokens); // Remove console error listener. parser.removeErrorListeners(); // Create instance of customized error listener. ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); // Add customized error listener to catch errors during parsing. parser.addErrorListener(parseTreeErrorListener); // Begin parsing YANG file and generate parse tree. ParseTree tree = parser.yangfile(); }
/** * Checks that exception is generated for YANG file with invalid syntax. */ @Test public void checkInvalidYangFileForSyntaxError() throws IOException { // Get the exception occurred during parsing. thrown.expect(ParserException.class); thrown.expect(CustomExceptionMatcher.errorLocation(3, 0)); thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'"); ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang"); // Create a lexer that feeds off of input char stream. GeneratedYangLexer lexer = new GeneratedYangLexer(input); // Create a buffer of tokens pulled from the lexer. CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a parser that feeds off the tokens buffer. GeneratedYangParser parser = new GeneratedYangParser(tokens); // Remove console error listener. parser.removeErrorListeners(); // Create instance of customized error listener. ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); // Add customized error listener to catch errors during parsing. parser.addErrorListener(parseTreeErrorListener); // Begin parsing YANG file and generate parse tree. ParseTree tree = parser.yangfile(); }
public AntlrBridge(String input){ // String inputFile = null; // if ( input.length()>0 ) inputFile = input; // InputStream is = input; // if ( inputFile!=null ) is = new FileInputStream(inputFile); ANTLRInputStream antlrInput = new ANTLRInputStream(input); SequenceAnalyzerLexer lexer = new SequenceAnalyzerLexer(antlrInput); CommonTokenStream tokens = new CommonTokenStream(lexer); SequenceAnalyzerParser parser = new SequenceAnalyzerParser(tokens); ParseTree tree = parser.prog(); // parse CommandObjectVisitor coVisitor = new CommandObjectVisitor(); coVisitor.visit(tree); Stack<CommandObject> commandObjectStack = coVisitor.commandObjectStack; commandExecutor.executeStack(commandObjectStack); }
private void inform(String match, String key, String value, ParseTree ctx) { Set<MatcherAction> relevantActions = informMatcherActions.get(match.toLowerCase(Locale.ENGLISH)); if (verbose) { if (relevantActions == null) { LOG.info("--- Have (0): {}", match); } else { LOG.info("+++ Have ({}): {}", relevantActions.size(), match); int count = 1; for (MatcherAction action : relevantActions) { LOG.info("+++ -------> ({}): {}", count, action.toString()); count++; } } } if (relevantActions != null) { for (MatcherAction matcherAction : relevantActions) { matcherAction.inform(key, value, ctx); } } }
@Override public WalkResult walk(ParseTree tree, String value) { String actualValue = getActualValue(tree, value); String filteredValue; if (tree.getChildCount() == 1 && ( tree.getChild(0) instanceof SingleVersionContext || tree.getChild(0) instanceof SingleVersionWithCommasContext)) { filteredValue = VersionSplitter.getInstance().getSplitRange(actualValue, firstWord, lastWord); } else { filteredValue = WordSplitter.getInstance().getSplitRange(actualValue, firstWord, lastWord); } if (filteredValue == null) { return null; } return walkNextStep(tree, filteredValue); }
@Override public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { final ANTLRInputStream is = new ANTLRInputStream(args[0].getStringValue()); final CorpusQLLexer lexer = new CorpusQLLexer(is); final CommonTokenStream tokens = new CommonTokenStream(lexer); final CorpusQLParser parser = new CorpusQLParser(tokens); final ParseTree tree = parser.query(); final MemTreeBuilder builder = new MemTreeBuilder(); builder.startDocument(); final CorpusQLXMLVisitor xmlVisitor = new CorpusQLXMLVisitor(builder); xmlVisitor.visit(tree); builder.endDocument(); return builder.getDocument(); }
@SuppressWarnings("unchecked") static public Generator<ParseTree> getGenerator(ParseTree ctx) throws OrcaException { Class<?> klass = ctx.getClass(); Generator<ParseTree> generator = _generatorByName.get(klass); if (generator == null) { String key = StringUtils.removeStart(klass.getSimpleName(), "FishParser$"); key = StringUtils.removeEnd(key, "Context"); key += "Generator"; try { key = InstructionGenerator.class.getPackage().getName() + "." + key; Class<?> generatorClass = Class.forName(key); generator = (Generator<ParseTree>)generatorClass.newInstance(); _generatorByName.put(klass, generator); } catch (Exception x) { throw new OrcaException("instruction geneartor is not found: " + key, x); } } return generator; }
/** Walk upwards from node until we find a child of p at t's char position. * Don't see alignment with self, t, or element *after* us. * return null if there is no such ancestor p. */ public static Pair<ParserRuleContext,Integer> earliestAncestorWithChildStartingAtCharPos(ParserRuleContext node, Token t, int charpos) { ParserRuleContext p = node; while ( p!=null ) { // check all children of p to see if one of them starts at charpos for (int i = 0; i<p.getChildCount(); i++) { ParseTree child = p.getChild(i); Token start; if ( child instanceof ParserRuleContext ) { start = ((ParserRuleContext) child).getStart(); } else { // must be token start = ((TerminalNode)child).getSymbol(); } // check that we don't see alignment with self or element *after* us if ( start.getTokenIndex()<t.getTokenIndex() && start.getCharPositionInLine()==charpos ) { return new Pair<>(p,i); } } p = p.getParent(); } return null; }
/** * {@inheritDoc} * <p> * <p>The default implementation returns the result of calling * {@link #visitChildren} on {@code ctx}.</p> * * @param ctx */ @Override public String visitATTR_OPER_EXPR(ScimFilterParser.ATTR_OPER_EXPRContext ctx) { logger.info(" ScimGroupFilterVisitor.visitATTR_OPER_EXPR() "); ParseTree parent = ctx.getParent(); while (parent != null) { if (parent.getClass().getSimpleName().equalsIgnoreCase(ScimFilterParser.LBRAC_EXPR_RBRACContext.class.getSimpleName())) { logger.info("********** PARENT = " + parent.getClass().getSimpleName()); String attrName = ((ScimFilterParser.LBRAC_EXPR_RBRACContext)parent).ATTRNAME() + "." + ctx.ATTRNAME().getText(); return attrOperCriteriaResolver(attrName, visit(ctx.operator()), visit(ctx.expression())); } else { parent = parent.getParent(); } } return attrOperCriteriaResolver(ctx.ATTRNAME().getText(), visit(ctx.operator()), visit(ctx.expression())); }
/** * {@inheritDoc} * <p> * <p>The default implementation returns the result of calling * {@link #visitChildren} on {@code ctx}.</p> * * @param ctx */ @Override public String visitATTR_OPER_CRITERIA(ScimFilterParser.ATTR_OPER_CRITERIAContext ctx) { logger.info(" FidoDeviceFilterVisitor.visitATTR_OPER_CRITERIA() "); ParseTree parent = ctx.getParent(); while (parent != null) { if (parent.getClass().getSimpleName().equalsIgnoreCase(ScimFilterParser.LBRAC_EXPR_RBRACContext.class.getSimpleName())) { logger.info("********** PARENT = " + parent.getClass().getSimpleName()); String attrName = ((ScimFilterParser.LBRAC_EXPR_RBRACContext)parent).ATTRNAME() + "." + ctx.ATTRNAME().getText(); return attrOperCriteriaResolver(attrName, visit(ctx.operator()), visit(ctx.criteria())); } else { parent = parent.getParent(); } } return attrOperCriteriaResolver(ctx.ATTRNAME().getText(), visit(ctx.operator()), visit(ctx.criteria())); }
private String inform(ParseTree stateCtx, ParseTree ctx, String name, String value, boolean fakeChild) { State myState = new State(stateCtx, name); if (!fakeChild) { state.put(stateCtx, myState); } PathType childType; switch (name) { case "comments": childType = PathType.COMMENT; break; case "version": childType = PathType.VERSION; break; default: childType = PathType.CHILD; } String path = myState.calculatePath(childType, fakeChild); analyzer.inform(path, value, ctx); return path; }
private OperatorNode<SequenceOperator> convertQuery(ParseTree node, Scope scope) { if (node instanceof Select_statementContext || node instanceof Insert_statementContext || node instanceof Update_statementContext || node instanceof Delete_statementContext) { return convertSelectOrInsertOrUpdateOrDelete(node, scope.getRoot()); } else if (node instanceof Source_statementContext) { //for pipe Source_statementContext sourceStatementContext = (Source_statementContext) node; return convertPipe(sourceStatementContext.query_statement(), sourceStatementContext.pipeline_step(), scope); } else if (node instanceof Merge_statementContext) { return convertMerge(((Merge_statementContext) node).merge_component(), scope); } else if (node instanceof Execute_statementContext) { return convertExecute((Execute_statementContext) node, scope); } else { throw new IllegalArgumentException("Unexpected argument type to convertQueryStatement: " + node.toStringTree()); } }
public GoalOccurrences(CoqFTParser parser, CoqParser.Goal_occurrencesContext ctx) { super(parser, ctx); this.idents = new ArrayList<>(); this.atOccurrences = new HashMap<>(); for(int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); if(child instanceof CoqParser.IdentContext) { idents.add(new Ident(parser, (CoqParser.IdentContext)child)); } else if(child instanceof CoqParser.At_occurrencesContext && child != ctx.selectOccurrences) { Occurrences o = new Occurrences(parser, ((CoqParser.At_occurrencesContext)child).occurrences()); atOccurrences.put(idents.get(idents.size() - 1), o); } } this.isStar = ctx.star != null; this.hasSelect = ctx.TOK_SELECT() != null; this.hasSelectStar = ctx.selectStar != null; if(ctx.selectOccurrences != null) { this.selectAtOccurrences = new Occurrences(parser, ctx.selectOccurrences.occurrences()); } else { this.selectAtOccurrences = null; } }
/** * Quand un terme sauvegardé dans la liste des termes sauvegardés est séléectionné il est possible * de voir son AST via le menu, ou le raccourci Ctrl+I / Cmd+I * * @param evt L'event qui a trigger l'action. */ private void menuViewASTActionPerformed(java.awt.event.ActionEvent evt) { if (!termSavedList.isSelectionEmpty()) { int index = termSavedList.getSelectedIndex(); String term = saveTermModel.getElementAt(index); try { ANTLRInputStream inputStream = new ANTLRInputStream(term); LambdaLexer lexer = new LambdaLexer(inputStream); CommonTokenStream tokens = new CommonTokenStream(lexer); LambdaParser parser = new LambdaParser(tokens); ParseTree tree = parse(term); TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree); viewer.open(); } catch (ParseCancellationException e) { workSpace.append("Don't try to watch AST of illformed term please"); } } }
/** * Creates JavadocNodeImpl node on base of ParseTree node. * * @param parseTree ParseTree node * @param parent DetailNode that will be parent of new node * @param index child index that has new node * @return JavadocNodeImpl node on base of ParseTree node. */ private JavadocNodeImpl createJavadocNode(ParseTree parseTree, DetailNode parent, int index) { final JavadocNodeImpl node = new JavadocNodeImpl(); if (parseTree.getChildCount() == 0 || "Text".equals(getNodeClassNameWithoutContext(parseTree))) { node.setText(parseTree.getText()); } else { node.setText(getFormattedNodeClassNameWithoutContext(parseTree)); } node.setColumnNumber(getColumn(parseTree)); node.setLineNumber(getLine(parseTree) + blockCommentLineNumber); node.setIndex(index); node.setType(getTokenType(parseTree)); node.setParent(parent); node.setChildren((DetailNode[]) new JavadocNodeImpl[parseTree.getChildCount()]); return node; }
/** * {@inheritDoc} * <p> * <p>The default implementation returns the result of calling * {@link #visitChildren} on {@code ctx}.</p> * * @param ctx */ @Override public String visitATTR_OPER_EXPR(ScimFilterParser.ATTR_OPER_EXPRContext ctx) { logger.info(" GroupFilterVisitor.visitATTR_OPER_EXPR() "); ParseTree parent = ctx.getParent(); while (parent != null) { if (parent.getClass().getSimpleName().equalsIgnoreCase(ScimFilterParser.LBRAC_EXPR_RBRACContext.class.getSimpleName())) { logger.info("********** PARENT = " + parent.getClass().getSimpleName()); String attrName = ((ScimFilterParser.LBRAC_EXPR_RBRACContext)parent).ATTRNAME() + "." + ctx.ATTRNAME().getText(); return attrOperCriteriaResolver(attrName, visit(ctx.operator()), visit(ctx.expression())); } else { parent = parent.getParent(); } } return attrOperCriteriaResolver(ctx.ATTRNAME().getText(), visit(ctx.operator()), visit(ctx.expression())); }
/** * Gets token type of ParseTree node from JavadocTokenTypes class. * @param node ParseTree node. * @return token type from JavadocTokenTypes */ private static int getTokenType(ParseTree node) { final int tokenType; if (node.getChildCount() == 0) { tokenType = ((TerminalNode) node).getSymbol().getType(); } else { final String className = getNodeClassNameWithoutContext(node); final String typeName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, className); tokenType = JavadocUtils.getTokenId(typeName); } return tokenType; }
private OperatorNode<ExpressionOperator> readValues(List<Field_defContext> fieldDefs, Scope scope) { List<String> fieldNames; List<OperatorNode<ExpressionOperator>> fieldValues; int numPairs = fieldDefs.size(); fieldNames = Lists.newArrayListWithExpectedSize(numPairs); fieldValues = Lists.newArrayListWithExpectedSize(numPairs); for (int j = 0; j < numPairs; j++) { ParseTree startNode = fieldDefs.get(j); while (startNode.getChildCount() < 3) { startNode = startNode.getChild(0); } fieldNames.add((String) convertExpr(startNode.getChild(0), scope).getArgument(1)); fieldValues.add(convertExpr(startNode.getChild(2), scope)); } return OperatorNode.create(ExpressionOperator.MAP, fieldNames, fieldValues); }
private static CommandResult executeCommand(String command, Transputer[] transputers, PrintWriter output, PrintWriter errOutput) { DebuggerCommandLexer commandLexer = new DebuggerCommandLexer(new ANTLRInputStream(command)); commandLexer.removeErrorListener(ConsoleErrorListener.INSTANCE); CommonTokenStream tokenStream = new CommonTokenStream(commandLexer); ErrorListener errorListener = new ErrorListener(); DebuggerCommandParser commandParser = new DebuggerCommandParser(tokenStream); commandParser.addErrorListener(errorListener); commandParser.removeErrorListener(ConsoleErrorListener.INSTANCE); ParseTree commandTree = commandParser.command(); if (errorListener.errors != 0) { output.println("Command not recognised."); output.flush(); return CommandResult.NOT_RECOGNISED; } CommandExecutor executor = new CommandExecutor(transputers, output, errOutput); return executor.visit(commandTree); }
/** * Special case: STORE operator. * * @param queryArray * @param i * @param file * @return * @throws Exception */ public static ResultSet storeOperator(String[] queryArray, int i, boolean file) throws Exception { ParseTree tree = TriALQLParseQuery.parse(queryArray[i] + ";", file); TriALQLClassListener extractor = new TriALQLClassListener(); ParseTreeWalker.DEFAULT.walk(extractor, tree); theLastQuery = true; if (QueryStruct.oldTableName.get(i).equals(QueryStruct.newTableName.get(i))) { // results = ImpalaDaemon.main(QueryStruct.baseQuery.get(i - 1)); notToDrop = QueryStruct.newTableName.get(i); } else { parseTree = false; queryExecutor(queryArray[i] + ";", file, theLastQuery, parseTree); results = ImpalaDaemon.main(QueryStruct.baseQuery.get(i)); } return results; }
/** * Parses a source file using ANTLR4, and walks over the parse tree to * interpret this source file as a Java object. * * @param c * input character stream * @param path * location of the file * @return an interpreted source file, or null in case of an error. */ @Nullable private ReoFile<T> parse(CharStream c, String path) { ReoLexer lexer = new ReoLexer(c); CommonTokenStream tokens = new CommonTokenStream(lexer); ReoParser parser = new ReoParser(tokens); ErrorListener errListener = new ErrorListener(m); parser.removeErrorListeners(); parser.addErrorListener(errListener); ParseTree tree = parser.file(); if (errListener.hasError) return null; ParseTreeWalker walker = new ParseTreeWalker(); listener.setFileName(path); walker.walk(listener, tree); return listener.getMain(); }
@Override public Object visit(ParseTree tree) { if (tree == null) { System.err.print("visit null"); Exception ex = new Exception(); ex.printStackTrace(System.err); return null; } if(tree instanceof StatContext && returned){ diagnosisReporter.report(Diagnosis.Kind.ERROR ,"unreachable statement" , (StatContext) tree ); } return super.visit(tree); }
@Test public void test(){ KalangSource source = new KalangSource("Test", "class{" + "void main(){" + "}" + "}","Test.kl"); CompilationUnit cu = new CompilationUnit(source,new DefaultCompileContext()); cu.compile(CompilePhase.PHASE_PARSING); AstBuilder astBuilder = cu.getAstBuilder(); ParseTreeNavigator treeNav = new ParseTreeNavigator(astBuilder.getParseTree()); ParseTree tree = treeNav.getParseTree(0); assertNotNull(tree); ParseTree treeMd = treeNav.getParseTree(2); ParseTree treeMdEnd = treeNav.getParseTree(7); //System.out.println(treeMd); //System.out.println(treeMdEnd); assertNotNull(treeMd); assertNotNull(treeMdEnd); //assertEquals(treeMd, treeMdEnd.getParent().getParent()); // AstNode ast = sp.getAstNode(tree); // assertNotNull(ast); // System.out.println("ast:"+ast); // RuleContext treeOfAst = sp.getParseTree(ast); // assertNotNull(treeOfAst); }
@SuppressWarnings("unchecked") static public Generator<ParseTree> getGenerator(ParseTree ctx) throws OrcaException { Class<?> klass = ctx.getClass(); Generator<ParseTree> generator = _generatorByName.get(klass); if (generator == null) { String key = StringUtils.removeStart(klass.getSimpleName(), "MysqlParser$"); key = StringUtils.removeEnd(key, "Context"); key += "Generator"; try { key = InstructionGenerator.class.getPackage().getName() + "." + key; Class<?> generatorClass = Class.forName(key); generator = (Generator<ParseTree>)generatorClass.newInstance(); _generatorByName.put(klass, generator); } catch (Exception x) { throw new OrcaException("instruction geneartor is not found: " + key, x); } } return generator; }
public static FillerRequest createRequest(final SourceLinesProvider linesProvider, final InputFile file, final Charset encoding) throws IOException, FileNotFoundException { final CharStream mainStream = CharStreams.fromPath(file.path(), encoding); final CharStream charStream = new CaseChangingCharStream(mainStream, true); final TSqlLexer lexer = new TSqlLexer(charStream); lexer.removeErrorListeners(); final CommonTokenStream stream = new CommonTokenStream(lexer); stream.fill(); final TSqlParser parser = new TSqlParser(stream); parser.removeErrorListeners(); final ParseTree root = parser.tsql_file(); final FillerRequest antrlFile = new FillerRequest(file, stream, root, linesProvider.getLines(new FileInputStream(file.file()), encoding)); return antrlFile; }
private void printTree(ExpressionsForSquid2Parser parser, ParseTree tree, List<String> parsed) { if (tree.getChildCount() < 1) { parsed.add(tree.toStringTree(parser)); } else { for (int i = 0; i < tree.getChildCount(); i++) { printTree(parser, tree.getChild(i), parsed); } } }
/** * Parses the expression using antlr, aborts the parsing on the first error. * * @param expression * an expression, not {@code null} * @return a {@link ParseTree} for further processing * @throws ExpressionException */ ParseTree parseExpression(String expression) throws ExpressionException { if (expression == null) { throw new IllegalArgumentException("expression must not be null"); } ANTLRInputStream in = new ANTLRInputStream(expression); FunctionExpressionLexer lexer = new CapitulatingFunctionExpressionLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); FunctionExpressionParser parser = new FunctionExpressionParser(tokens); parser.removeErrorListeners(); lexer.removeErrorListeners(); ExpressionErrorListener listener = new ExpressionErrorListener(); parser.addErrorListener(listener); lexer.addErrorListener(listener); parser.setErrorHandler(new CapitulatingErrorStrategy()); try { ParseTree tree = parser.operationExp(); if (listener.containsError()) { throw new ExpressionException(listener.getErrorMessage(), listener.getErrorLine()); } else { return tree; } } catch (CapitulatingRuntimeException e) { if (listener.containsError()) { throw new ExpressionException(listener.getErrorMessage(), listener.getErrorLine()); } else { // cannot happen since the parser and lexer always register the error before trying // to recover throw new ExpressionException("Unknown error"); } } }
@Override public void checkSyntax(String expression) throws ExpressionException { ParseTree tree = parseExpression(expression); ParseTreeWalker walker = new ParseTreeWalker(); FunctionListener listener = new FunctionListener(lookup); try { walker.walk(listener, tree); } catch (ExpressionParsingException e) { throw new ExpressionException(e); } }
@Override public ArrayList<CodeError> getErrors() { ParseTree tree = antlrHandler.getParseTree(); ParseTreeWalker walker = new ParseTreeWalker(); try { walker.walk(lis, tree); } catch (Exception e) { e.printStackTrace(); return new ArrayList<CodeError>(); } return lis.getErrors(); }
/** * gives the parse tree * @return the parse tree */ public ParseTree getCParseTree() { try { String code = pane.getStyledDocument().getText(0, pane.getStyledDocument().getLength()); lexer.setInputStream(new ANTLRInputStream(code)); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); cParser.setTokenStream(commonTokenStream); return cParser.compilationUnit(); } catch (BadLocationException ex) { Logger.getLogger(CAntlrHandler.class.getName()).log(Level.SEVERE, null, ex); } return null; }
static void showGuiTreeView(final String code) { final org.antlr.v4.runtime.CharStream stream = new ANTLRInputStream(code); final FormalPropertyDescriptionLexer lexer = new FormalPropertyDescriptionLexer(stream); final CommonTokenStream tokens = new CommonTokenStream(lexer); final FormalPropertyDescriptionParser parser = new FormalPropertyDescriptionParser(tokens); final ParseTree tree = parser.booleanExpList(); final List<String> ruleNames = Arrays.asList(FormalPropertyDescriptionParser.ruleNames); final TreeViewer view = new TreeViewer(ruleNames, tree); view.open(); }
static void showGuiTreeView(final String code) { final org.antlr.v4.runtime.CharStream stream = new ANTLRInputStream(code); final CLexer lexer = new CLexer(stream); final CommonTokenStream tokens = new CommonTokenStream(lexer); final CParser parser = new CParser(tokens); final ParseTree tree = parser.declaration(); final List<String> ruleNames = Arrays.asList(CParser.ruleNames); final TreeViewer view = new TreeViewer(ruleNames, tree); view.open(); }
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 MemgraphException("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 MemgraphException("unexpected binary expression. expected an op between expressions"); } left = new CypherBinaryExpression(left, op, childObj); } op = null; } } return (CypherBinaryExpression) left; }
@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; } }