List of usage examples for org.antlr.v4.runtime ParserRuleContext getStop
public Token getStop()
From source file:nl.lxtreme.libtdl.grammar.Util.java
License:Apache License
/** * Validates whether a given context denotes a numeric value in the given * range. In case the value falls outside the defined range, a problem will * be added to the given problem reporter. * /*ww w . j av a2 s . co m*/ * @param ctx * the parser context that denotes the numeric value to test; * @param lower * the lower bound (inclusive!) of the range that is valid; * @param upper * the upper bound (inclusive!) of the range that is valid; * @param msg * the message to add as problem marker in case the value is * invalid; * @param reporter * the problem reporter to add the marker to. * @return the parsed numeric value of the given parser context, or * <code>null</code> in case it did not denote a valid numeric * value (not inside the given range, or otherwise invalid). */ public static Long validateValue(ParserRuleContext ctx, long lower, long upper, String msg, ProblemReporter reporter) { String text = null; if (ctx != null) { text = ctx.getText(); } Long value = decode(text); if (value == null) { return null; } if (!inRange(value.longValue(), lower, upper)) { int offset = ctx.getStart().getStartIndex(); int length = ctx.getStop().getStopIndex() - offset; MarkerBuilder builder = new MarkerBuilder(); Marker marker = builder.setCategory(Category.SEMANTIC).setType(Type.ERROR) .setLocation(offset, length, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine()) .setDescription(msg).build(); reporter.report(marker); return null; } return value; }
From source file:no.ssb.vtl.script.error.VTLScriptException.java
License:Apache License
private void extractPositionFromContext(ParserRuleContext ctx) { checkNotNull(ctx);/* w w w. ja va 2s . c om*/ startLine = ctx.getStart().getLine(); startColumn = ctx.getStart().getCharPositionInLine(); if (ctx.getStop() != null) { stopLine = ctx.getStop().getLine(); if (ctx.getStart() == ctx.getStop()) { Token token = ctx.getStart(); stopColumn = startColumn + token.getText().length(); } else { stopColumn = ctx.getStop().getCharPositionInLine(); } } }
From source file:oracle.kv.impl.query.compiler.Translator.java
License:Open Source License
private static QueryException.Location getLocation(ParserRuleContext ctx) { int startLine = -1; int startColumn = -1; int endLine = -1; int endColumn = -1; if (ctx != null && ctx.getStart() != null) { startLine = ctx.getStart().getLine(); startColumn = ctx.getStart().getCharPositionInLine(); }//from w ww. j a va 2s . co m if (ctx != null && ctx.getStop() != null) { endLine = ctx.getStop().getLine(); endColumn = ctx.getStop().getCharPositionInLine(); } return new QueryException.Location(startLine, startColumn, endLine, endColumn); }
From source file:org.cfeclipse.cfml.parser.docitems.ScriptItem.java
License:Open Source License
public ScriptItem(ParserRuleContext ctx, String name) { super(ctx.getStart().getLine(), ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), name); // TODO Auto-generated constructor stub }
From source file:org.geotoolkit.cql.JCQLTextPane.java
License:Open Source License
private void syntaxHighLight(ParseTree tree, StyledDocument doc, AtomicInteger position) { if (tree instanceof ParserRuleContext) { final ParserRuleContext prc = (ParserRuleContext) tree; if (prc.exception != null) { //error nodes final Token tokenStart = prc.getStart(); Token tokenEnd = prc.getStop(); if (tokenEnd == null) tokenEnd = tokenStart;/* w ww . j av a 2s . co m*/ final int offset = tokenStart.getStartIndex(); final int length = tokenEnd.getStopIndex() - tokenStart.getStartIndex() + 1; doc.setCharacterAttributes(offset, length, styleError, true); return; } //special case for functions if (prc instanceof CQLParser.ExpressionTermContext) { final CQLParser.ExpressionTermContext ctx = (CQLParser.ExpressionTermContext) prc; if (ctx.NAME() != null && ctx.LPAREN() != null) { final int nbChild = tree.getChildCount(); for (int i = 0; i < nbChild; i++) { final ParseTree pt = tree.getChild(i); if (pt instanceof TerminalNode && ((TerminalNode) pt).getSymbol().getType() == CQLLexer.NAME) { final TerminalNode tn = (TerminalNode) pt; // if index<0 = missing token final Token token = tn.getSymbol(); final int offset = token.getStartIndex(); final int length = token.getStopIndex() - token.getStartIndex() + 1; position.addAndGet(length); doc.setCharacterAttributes(offset, length, styleFunction, true); } else { syntaxHighLight(pt, doc, position); } } return; } } } if (tree instanceof TerminalNode) { final TerminalNode tn = (TerminalNode) tree; // if index<0 = missing token final Token token = tn.getSymbol(); final int offset = token.getStartIndex(); final int length = token.getStopIndex() - token.getStartIndex() + 1; position.addAndGet(length); switch (token.getType()) { case CQLLexer.COMMA: case CQLLexer.UNARY: case CQLLexer.MULT: doc.setCharacterAttributes(offset, length, styleDefault, true); break; // EXpressions ------------------------------------------------- case CQLLexer.TEXT: case CQLLexer.INT: case CQLLexer.FLOAT: case CQLLexer.DATE: case CQLLexer.DURATION_P: case CQLLexer.DURATION_T: case CQLLexer.POINT: case CQLLexer.LINESTRING: case CQLLexer.POLYGON: case CQLLexer.MPOINT: case CQLLexer.MLINESTRING: case CQLLexer.MPOLYGON: doc.setCharacterAttributes(offset, length, styleLiteral, true); break; case CQLLexer.PROPERTY_NAME: doc.setCharacterAttributes(offset, length, stylePropertyName, true); break; case CQLLexer.NAME: if (tree.getChildCount() == 0) { //property name doc.setCharacterAttributes(offset, length, stylePropertyName, true); } else { //function name doc.setCharacterAttributes(offset, length, styleFunction, true); } break; case CQLLexer.RPAREN: case CQLLexer.LPAREN: doc.setCharacterAttributes(offset, length, styleParenthese, true); break; case CQLLexer.COMPARE: case CQLLexer.LIKE: case CQLLexer.IS: case CQLLexer.BETWEEN: case CQLLexer.IN: doc.setCharacterAttributes(offset, length, styleOperator, true); break; case CQLLexer.AND: case CQLLexer.OR: case CQLLexer.NOT: doc.setCharacterAttributes(offset, length, styleBinary, true); break; case CQLLexer.BBOX: case CQLLexer.BEYOND: case CQLLexer.CONTAINS: case CQLLexer.CROSSES: case CQLLexer.DISJOINT: case CQLLexer.DWITHIN: case CQLLexer.EQUALS: case CQLLexer.INTERSECTS: case CQLLexer.OVERLAPS: case CQLLexer.TOUCHES: case CQLLexer.WITHIN: doc.setCharacterAttributes(offset, length, styleBinary, true); break; default: doc.setCharacterAttributes(offset, length, styleError, true); break; } } final int nbChild = tree.getChildCount(); for (int i = 0; i < nbChild; i++) { syntaxHighLight(tree.getChild(i), doc, position); } }
From source file:org.geotoolkit.gui.javafx.filter.FXCQLEditor.java
License:Open Source License
private void syntaxHighLight(ParseTree tree) { if (tree instanceof ParserRuleContext) { final ParserRuleContext prc = (ParserRuleContext) tree; if (prc.exception != null) { //error nodes final Token tokenStart = prc.getStart(); Token tokenEnd = prc.getStop(); if (tokenEnd == null) tokenEnd = tokenStart;/*from w ww .j a va 2 s . c om*/ final int offset = tokenStart.getStartIndex(); final int end = tokenEnd.getStopIndex() + 1; if (end > offset) { codeArea.setStyle(offset, end, STYLE_ERROR); } return; } //special case for functions if (prc instanceof CQLParser.ExpressionTermContext) { final CQLParser.ExpressionTermContext ctx = (CQLParser.ExpressionTermContext) prc; if (ctx.NAME() != null && ctx.LPAREN() != null) { final int nbChild = tree.getChildCount(); for (int i = 0; i < nbChild; i++) { final ParseTree pt = tree.getChild(i); if (pt instanceof TerminalNode && ((TerminalNode) pt).getSymbol().getType() == CQLLexer.NAME) { final TerminalNode tn = (TerminalNode) pt; // if index<0 = missing token final Token token = tn.getSymbol(); final int offset = token.getStartIndex(); final int end = token.getStopIndex() + 1; if (end > offset) { codeArea.setStyle(offset, end, STYLE_FUNCTION); } } else { syntaxHighLight(pt); } } return; } } } if (tree instanceof TerminalNode) { final TerminalNode tn = (TerminalNode) tree; // if index<0 = missing token final Token token = tn.getSymbol(); final int offset = token.getStartIndex(); final int end = token.getStopIndex() + 1; switch (token.getType()) { case CQLLexer.COMMA: case CQLLexer.UNARY: case CQLLexer.MULT: codeArea.setStyle(offset, end, STYLE_DEFAULT); break; // EXpressions ------------------------------------------------- case CQLLexer.TEXT: case CQLLexer.INT: case CQLLexer.FLOAT: case CQLLexer.DATE: case CQLLexer.DURATION_P: case CQLLexer.DURATION_T: case CQLLexer.POINT: case CQLLexer.LINESTRING: case CQLLexer.POLYGON: case CQLLexer.MPOINT: case CQLLexer.MLINESTRING: case CQLLexer.MPOLYGON: codeArea.setStyle(offset, end, STYLE_LITERAL); break; case CQLLexer.PROPERTY_NAME: codeArea.setStyle(offset, end, STYLE_PROPERTY); break; case CQLLexer.NAME: if (tree.getChildCount() == 0) { //property name codeArea.setStyle(offset, end, STYLE_PROPERTY); } else { //function name codeArea.setStyle(offset, end, STYLE_FUNCTION); } break; case CQLLexer.RPAREN: case CQLLexer.LPAREN: codeArea.setStyle(offset, end, STYLE_PARENTHESE); break; case CQLLexer.COMPARE: case CQLLexer.LIKE: case CQLLexer.IS: case CQLLexer.BETWEEN: case CQLLexer.IN: codeArea.setStyle(offset, end, STYLE_OPERATOR); break; case CQLLexer.AND: case CQLLexer.OR: case CQLLexer.NOT: codeArea.setStyle(offset, end, STYLE_BINARY); break; case CQLLexer.BBOX: case CQLLexer.BEYOND: case CQLLexer.CONTAINS: case CQLLexer.CROSSES: case CQLLexer.DISJOINT: case CQLLexer.DWITHIN: case CQLLexer.EQUALS: case CQLLexer.INTERSECTS: case CQLLexer.OVERLAPS: case CQLLexer.TOUCHES: case CQLLexer.WITHIN: codeArea.setStyle(offset, end, STYLE_BINARY); break; default: codeArea.setStyle(offset, end, STYLE_ERROR); break; } } final int nbChild = tree.getChildCount(); for (int i = 0; i < nbChild; i++) { syntaxHighLight(tree.getChild(i)); } }
From source file:org.kie.dmn.feel.lang.ast.BaseNode.java
License:Apache License
public BaseNode(ParserRuleContext ctx) { // DO NOT keep the reference to `ParserRuleContext` to avoid unneeded retention of lexer structures. this.setStartChar(ctx.getStart().getStartIndex()); this.setStartLine(ctx.getStart().getLine()); this.setStartColumn(ctx.getStart().getCharPositionInLine()); this.setEndChar(ctx.getStop().getStopIndex()); this.setEndLine(ctx.getStop().getLine()); this.setEndColumn(ctx.getStop().getCharPositionInLine() + ctx.getStop().getText().length()); this.setText(ParserHelper.getOriginalText(ctx)); }
From source file:org.opencypher.tools.g4processors.BNFListener.java
License:Apache License
private String findHiddenTextWithin(ParserRuleContext ctx) { List<Token> allTokens = tokens.get(ctx.getStart().getTokenIndex(), ctx.getStop().getTokenIndex()); return allTokens.stream().filter(t -> t.getChannel() == BNFLexer.HIDDEN) .map(t -> t.getText().replaceFirst("// ?", "")).collect(Collectors.joining("\n")); }
From source file:org.opencypher.tools.g4processors.BNFListener.java
License:Apache License
private String findHiddenTextAfter(ParserRuleContext ctx) { Token endCtx = ctx.getStop(); int i = endCtx.getTokenIndex(); List<Token> normalTextChannel = tokens.getHiddenTokensToRight(i, BNFLexer.HIDDEN); if (normalTextChannel != null) { // the quasi-comment (description) may be the end of a rule or start of the next. separation is on // a blank line int nextLine = endCtx.getLine() + 1; List<String> content = new ArrayList<>(); for (Token lineToken : normalTextChannel) { if (lineToken.getLine() == nextLine) { content.add(lineToken.getText().replaceFirst("// ?", "")); nextLine++;/*from ww w.ja v a2s . c om*/ } else { break; } } return content.stream().collect(Collectors.joining("\n")); } return ""; }
From source file:org.opencypher.tools.g4processors.G4Listener.java
License:Apache License
private FreeTextItem findHiddenText(ParserRuleContext ctx) { // to suppress lexing, !! normal english text is a special comment //!! -> hidden // not sure i need to do that Token endAlt = ctx.getStop(); int i = endAlt.getTokenIndex(); List<Token> normalTextChannel = tokens.getHiddenTokensToRight(i, Gee4Lexer.HIDDEN); if (normalTextChannel != null) { // there should be only one line now String content = normalTextChannel.stream().map(tk -> tk.getText().replaceFirst("//!!\\s*", "")) .collect(Collectors.joining()); return new FreeTextItem(content); }/*from w w w . java2s . c o m*/ return null; }