org.antlr.v4.runtime.misc.Interval Java Examples
The following examples show how to use
org.antlr.v4.runtime.misc.Interval.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AntlrUtils.java From sonar-tsql-plugin with GNU General Public License v3.0 | 7 votes |
public static void print(final ParseTree node, final int level, CommonTokenStream stream) { final Interval sourceInterval = node.getSourceInterval(); final Token firstToken = stream.get(sourceInterval.a); int line = firstToken.getLine(); int charStart = firstToken.getCharPositionInLine(); int endLine = line; int endChar = charStart + firstToken.getText().length(); String data = "@(" + line + ":" + charStart + "," + endLine + ":" + endChar + ") with text: " + firstToken.getText(); final int tmp = level + 1; final StringBuilder sb = new StringBuilder(); sb.append(StringUtils.repeat("\t", level)); sb.append(node.getClass().getSimpleName() + ": " + data + " :" + node.getText()); System.out.println(sb.toString()); final int n = node.getChildCount(); for (int i = 0; i < n; i++) { final ParseTree c = node.getChild(i); print(c, tmp, stream); } }
Example #2
Source File: InputPanel.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void highlightAndOfferHint(Editor editor, int offset, Interval sourceInterval, JBColor color, EffectType effectType, String hintText) { CaretModel caretModel = editor.getCaretModel(); final TextAttributes attr = new TextAttributes(); attr.setForegroundColor(color); attr.setEffectColor(color); attr.setEffectType(effectType); MarkupModel markupModel = editor.getMarkupModel(); markupModel.addRangeHighlighter( sourceInterval.a, sourceInterval.b, InputPanel.TOKEN_INFO_LAYER, // layer attr, HighlighterTargetArea.EXACT_RANGE ); if ( hintText.contains("<") ) { hintText = hintText.replaceAll("<", "<"); } // HINT caretModel.moveToOffset(offset); // info tooltip only shows at cursor :( HintManager.getInstance().showInformationHint(editor, hintText); }
Example #3
Source File: LexerAdaptor.java From ast with Apache License 2.0 | 6 votes |
@Override public Token emit() { if (_type == ANTLRv4Lexer.ID) { String firstChar = _input.getText(Interval.of(_tokenStartCharIndex, _tokenStartCharIndex)); if (Character.isUpperCase(firstChar.charAt(0))) { _type = ANTLRv4Lexer.TOKEN_REF; } else { _type = ANTLRv4Lexer.RULE_REF; } if (_currentRuleType == Token.INVALID_TYPE) { // if outside of rule def _currentRuleType = _type; // set to inside lexer or parser rule } } else if (_type == ANTLRv4Lexer.SEMI) { // exit rule def _currentRuleType = Token.INVALID_TYPE; } return super.emit(); }
Example #4
Source File: CQLErrorStrategy.java From PoseidonX with Apache License 2.0 | 6 votes |
@NotNull private String getText(TokenStream tokens, Interval interval) { int start = interval.a; int stop = interval.b; if (start < 0 || stop < 0) return ""; if (stop >= tokens.size()) stop = tokens.size() - 1; StringBuilder buf = new StringBuilder(); for (int i = start; i <= stop; i++) { Token t = tokens.get(i); if (t.getType() == Token.EOF) break; buf.append(t.getText()); if (i != stop) { buf.append(" "); } } return buf.toString(); }
Example #5
Source File: SwiftSupport.java From swift-js-transpiler with MIT License | 6 votes |
/** "If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator." "If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an example, the ++ operator in a++.b is treated as a postfix unary operator (a++ .b rather than a ++ .b)." */ public static boolean isPostfixOp(TokenStream tokens) { int stop = getLastOpTokenIndex(tokens); if ( stop==-1 ) return false; int start = tokens.index(); Token prevToken = tokens.get(start-1); // includes hidden-channel tokens Token nextToken = tokens.get(stop+1); boolean prevIsWS = isLeftOperatorWS(prevToken); boolean nextIsWS = isRightOperatorWS(nextToken); boolean result = !prevIsWS && nextIsWS || !prevIsWS && nextToken.getType()==SwiftParser.DOT; String text = tokens.getText(Interval.of(start, stop)); //System.out.println("isPostfixOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result); return result; }
Example #6
Source File: KsqlResource.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 6 votes |
public List<String> getStatementStrings(String ksqlString) { List<SqlBaseParser.SingleStatementContext> statementContexts = new KsqlParser().getStatements(ksqlString); List<String> result = new ArrayList<>(statementContexts.size()); for (SqlBaseParser.SingleStatementContext statementContext : statementContexts) { // Taken from http://stackoverflow // .com/questions/16343288/how-do-i-get-the-original-text-that-an-antlr4-rule-matched CharStream charStream = statementContext.start.getInputStream(); result.add( charStream.getText( new Interval( statementContext.start.getStartIndex(), statementContext.stop.getStopIndex() ) ) ); } return result; }
Example #7
Source File: InputPanel.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void setCursorToGrammarElement(Project project, PreviewState previewState, int offset) { Token tokenUnderCursor = ParsingUtils.getTokenUnderCursor(previewState, offset); if ( tokenUnderCursor==null ) { return; } PreviewParser parser = (PreviewParser) previewState.parsingResult.parser; Integer atnState = parser.inputTokenToStateMap.get(tokenUnderCursor); if ( atnState==null ) { // likely an error token //LOG.error("no ATN state for input token " + tokenUnderCursor); return; } Interval region = previewState.g.getStateToGrammarRegion(atnState); CommonToken token = (CommonToken) previewState.g.tokenStream.get(region.a); jumpToGrammarPosition(project, token.getStartIndex()); }
Example #8
Source File: AstBuilder.java From sylph with Apache License 2.0 | 6 votes |
@Override public Node visitCreateStreamAsSelect(SqlBaseParser.CreateStreamAsSelectContext context) { Optional<String> comment = Optional.empty(); // 词法分析后 获取原始输入字符串 SqlBaseParser.QueryStreamContext queryContext = context.queryStream(); int a = queryContext.start.getStartIndex(); int b = queryContext.stop.getStopIndex(); Interval interval = new Interval(a, b); String viewSql = context.start.getInputStream().getText(interval); return new CreateStreamAsSelect( getLocation(context), getQualifiedName(context.qualifiedName()), context.EXISTS() != null, comment, visitIfPresent(context.watermark(), WaterMark.class), viewSql ); }
Example #9
Source File: RangeUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
private static IntervalSet intervalsToIntervalSet(List<Interval> intervals) { IntervalSet intervalSet = new IntervalSet(); for (Interval interval : intervals) { intervalSet.add(interval.a, interval.b); } return intervalSet; }
Example #10
Source File: SwiftSupport.java From swift-js-transpiler with MIT License | 5 votes |
/** "If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator." */ public static boolean isBinaryOp(TokenStream tokens) { int stop = getLastOpTokenIndex(tokens); if ( stop==-1 ) return false; int start = tokens.index(); Token prevToken = tokens.get(start-1); // includes hidden-channel tokens Token nextToken = tokens.get(stop+1); boolean prevIsWS = isLeftOperatorWS(prevToken); boolean nextIsWS = isRightOperatorWS(nextToken); boolean result = prevIsWS && nextIsWS || (!prevIsWS && !nextIsWS); String text = tokens.getText(Interval.of(start, stop)); //System.out.println("isBinaryOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result); return result; }
Example #11
Source File: SwiftSupport.java From swift-js-transpiler with MIT License | 5 votes |
public static boolean isOpNext(TokenStream tokens) { int start = tokens.index(); Token lt = tokens.get(start); int stop = getLastOpTokenIndex(tokens); if ( stop==-1 ) return false; System.out.printf("isOpNext: i=%d t='%s'", start, lt.getText()); System.out.printf(", op='%s'\n", tokens.getText(Interval.of(start,stop))); return true; }
Example #12
Source File: CharBufferStream.java From antsdb with GNU Lesser General Public License v3.0 | 5 votes |
@Override public String getText(Interval interval) { StringBuffer sbuf = new StringBuffer(); for (int i=interval.a; i<=interval.b; i++) { sbuf.append((char)this.buf.get(i)); } return sbuf.toString(); }
Example #13
Source File: SwiftSupport.java From swift-js-transpiler with MIT License | 5 votes |
public static boolean isOperator(TokenStream tokens, String op) { int stop = getLastOpTokenIndex(tokens); if ( stop==-1 ) return false; int start = tokens.index(); String text = tokens.getText(Interval.of(start, stop)); return text.equals(op); }
Example #14
Source File: RangeUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
private static List<Range> intervalsToRanges(List<Interval> intervals) { List<Range> ranges = new ArrayList<Range>(); for (Interval interval : intervals) { ranges.add(Range.newBuilder().setBegin(interval.a).setEnd(interval.b).build()); } return ranges; }
Example #15
Source File: RangeUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
private static List<Interval> rangesToIntervals(List<Range> ranges) { List<Interval> intervals = new ArrayList<Interval>(); for (Range range : ranges) { intervals.add(rangeToInterval(range)); } return intervals; }
Example #16
Source File: RangeUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * Returns whether the provided value is encompassed by any of the provided ranges. */ public static boolean isInAny(List<Range> ranges, long value) { for (Interval interval : rangesToIntervals(ranges)) { if (interval.a <= value && value <= interval.b) { return true; } } return false; }
Example #17
Source File: Grammar.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** Given an ATN state number, return the token index range within the grammar from which that ATN state was derived. */ public Interval getStateToGrammarRegion(int atnStateNumber) { if ( stateToGrammarRegionMap==null ) { stateToGrammarRegionMap = getStateToGrammarRegionMap(ast, null); // map all nodes with non-null atn state ptr } if ( stateToGrammarRegionMap==null ) return Interval.INVALID; return stateToGrammarRegionMap.get(atnStateNumber); }
Example #18
Source File: KsqlEngine.java From ksql-fork-with-deep-learning-function with Apache License 2.0 | 5 votes |
public static String getStatementString( final SqlBaseParser.SingleStatementContext singleStatementContext ) { CharStream charStream = singleStatementContext.start.getInputStream(); return charStream.getText(new Interval( singleStatementContext.start.getStartIndex(), singleStatementContext.stop.getStopIndex() )); }
Example #19
Source File: JavaFileAnalyzer2.java From steady with Apache License 2.0 | 5 votes |
/** * Retrieves content for constructs of type Method, Constructor and Class. * @param ctx - ParseRuleContex * @return Extracted construct Body */ private final String getConstructContent(ParserRuleContext ctx){ final int a = ctx.start.getStartIndex(); final int b = ctx.stop.getStopIndex(); final Interval interval = new Interval(a,b); final String text = this.input.getText(interval); return text; }
Example #20
Source File: SwiftSupport.java From swift-js-transpiler with MIT License | 5 votes |
/** "If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator." */ public static boolean isPrefixOp(TokenStream tokens) { int stop = getLastOpTokenIndex(tokens); if ( stop==-1 ) return false; int start = tokens.index(); Token prevToken = tokens.get(start-1); // includes hidden-channel tokens Token nextToken = tokens.get(stop+1); boolean prevIsWS = isLeftOperatorWS(prevToken); boolean nextIsWS = isRightOperatorWS(nextToken); boolean result = prevIsWS && !nextIsWS; String text = tokens.getText(Interval.of(start, stop)); //System.out.println("isPrefixOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result); return result; }
Example #21
Source File: Grammar.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
public static Map<Integer, Interval> getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes) { Map<Integer, Interval> stateToGrammarRegionMap = new HashMap<Integer, Interval>(); if ( ast==null ) return stateToGrammarRegionMap; List<GrammarAST> nodes = ast.getNodesWithType(grammarTokenTypes); for (GrammarAST n : nodes) { if (n.atnState != null) { Interval tokenRegion = Interval.of(n.getTokenStartIndex(), n.getTokenStopIndex()); org.antlr.runtime.tree.Tree ruleNode = null; // RULEs, BLOCKs of transformed recursive rules point to original token interval switch ( n.getType() ) { case ANTLRParser.RULE : ruleNode = n; break; case ANTLRParser.BLOCK : case ANTLRParser.CLOSURE : ruleNode = n.getAncestor(ANTLRParser.RULE); break; } if ( ruleNode instanceof RuleAST ) { String ruleName = ((RuleAST) ruleNode).getRuleName(); Rule r = ast.g.getRule(ruleName); if ( r instanceof LeftRecursiveRule ) { RuleAST originalAST = ((LeftRecursiveRule) r).getOriginalAST(); tokenRegion = Interval.of(originalAST.getTokenStartIndex(), originalAST.getTokenStopIndex()); } } stateToGrammarRegionMap.put(n.atnState.stateNumber, tokenRegion); } } return stateToGrammarRegionMap; }
Example #22
Source File: GroupTreeBuilder.java From batfish with Apache License 2.0 | 5 votes |
@Override public void exitS_groups_named(S_groups_namedContext ctx) { String groupName = unquote(ctx.name.getText()); HierarchyTree tree = _hierarchy.getTree(groupName); if (tree == null) { tree = _hierarchy.newTree(groupName); } StatementContext statement = ctx.s_groups_tail().statement(); if (statement == null) { return; } Interval interval = ctx.s_groups_tail().getSourceInterval(); List<Token> unfilteredTokens = _combinedParser.getTokens().getTokens(interval.a, interval.b); HierarchyPath path = new HierarchyPath(); for (Token currentToken : unfilteredTokens) { if (currentToken.getChannel() != Lexer.HIDDEN) { String text = currentToken.getText(); int line = currentToken.getLine(); if (currentToken.getType() == FlatJuniperLexer.WILDCARD) { path.addWildcardNode(text, line); } else { path.addNode(text, line); } } } path.setStatement(statement); tree.addPath(path, _currentSetLine, null); }
Example #23
Source File: DefaultLinesProvider.java From sonar-tsql-plugin with GNU General Public License v3.0 | 5 votes |
@Override public int getLine(final IParsedNode node) { if (node == null || node.getItem() == null) { return 0; } final Interval sourceInterval = node.getItem().getSourceInterval(); final Token firstToken = stream.get(sourceInterval.a); final int line = firstToken.getLine(); return line; }
Example #24
Source File: CharSequenceCharStream.java From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License | 5 votes |
@Override public String getText(Interval interval) { int start = interval.a; int stop = interval.b; int n = size(); if ( stop >= n ) stop = n-1; if ( start >= n ) return ""; return buffer.subSequence(start, stop + 1).toString(); }
Example #25
Source File: LocalFileReaderTest.java From protostuff-compiler with Apache License 2.0 | 5 votes |
@Test public void testRead() throws Exception { LocalFileReader reader = new LocalFileReader(tempDirectory1, tempDirectory2); CharStream a = reader.read("1.proto"); CharStream b = reader.read("2.proto"); CharStream c = reader.read("3.proto"); assertNotNull(a); assertNotNull(b); assertNull(c); assertEquals("1", a.getText(Interval.of(0, 1))); assertEquals("2", b.getText(Interval.of(0, 1))); }
Example #26
Source File: CompositeFileReaderTest.java From protostuff-compiler with Apache License 2.0 | 5 votes |
@Test public void testRead() throws Exception { TestReader r1 = new TestReader("1.proto", "1"); TestReader r2 = new TestReader("2.proto", "2"); CompositeFileReader reader = new CompositeFileReader(r1, r2); CharStream s1 = reader.read("1.proto"); CharStream s2 = reader.read("2.proto"); CharStream s3 = reader.read("3.proto"); assertNotNull(s1); assertNotNull(s2); assertNull(s3); assertEquals("1", s1.getText(Interval.of(0, 1))); assertEquals("2", s2.getText(Interval.of(0, 1))); }
Example #27
Source File: DescriptiveErrorStrategy.java From groovy with Apache License 2.0 | 5 votes |
protected String createNoViableAlternativeErrorMessage(Parser recognizer, NoViableAltException e) { TokenStream tokens = recognizer.getInputStream(); String input; if (tokens != null) { if (e.getStartToken().getType() == Token.EOF) { input = "<EOF>"; } else { input = charStream.getText(Interval.of(e.getStartToken().getStartIndex(), e.getOffendingToken().getStopIndex())); } } else { input = "<unknown input>"; } return "Unexpected input: " + escapeWSAndQuote(input); }
Example #28
Source File: ADLErrorListener.java From archie with Apache License 2.0 | 5 votes |
@Override public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) { String input = recognizer.getInputStream().getText(new Interval(startIndex, stopIndex)); String warning = String.format("FULL AMBIGUITY: %d-%d, exact: %b, input: %s", startIndex, stopIndex, exact, input); logger.debug(warning); errors.addWarning(warning); }
Example #29
Source File: PhysicalParser.java From Cubert with Apache License 2.0 | 5 votes |
private void deprecation(ParserRuleContext ctx, String msg) { System.err.println("DEPRECATION: " + msg); System.err.println("At: " + input.getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()))); }
Example #30
Source File: PhysicalParser.java From Cubert with Apache License 2.0 | 5 votes |
private void addLine(ParserRuleContext ctx, JsonNode node) { String line = input.getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex())); ((ObjectNode) node).put("line", line); }