List of usage examples for org.antlr.v4.runtime Token getText
String getText();
From source file:com.satisfyingstructures.J2S.J2SConverter.java
License:Open Source License
private void mapModifierToken(Token modifierToken) { String stringInJava = modifierToken.getText(); String stringInSwift = modifiers.map(stringInJava); if (null != stringInSwift && !stringInSwift.equals(stringInJava)) { if (0 == stringInSwift.length()) rewriter.deleteAndAdjustWhitespace(modifierToken); else/*from w ww . ja va 2 s . c om*/ rewriter.replace(modifierToken, stringInSwift); } }
From source file:com.satisfyingstructures.J2S.J2SConverter.java
License:Open Source License
private void mapClassIdentifierInContext(ParserRuleContext ctx) { TerminalNode tn = ctx.getToken(Java8Parser.Identifier, 0); Token classIdentifierToken = null != tn ? tn.getSymbol() : null; if (null == classIdentifierToken) return;/*from w ww . ja va 2s .com*/ String stringInJava = classIdentifierToken.getText(); String stringInSwift = types.map(stringInJava); if (null != stringInSwift && !stringInSwift.equals(stringInJava)) rewriter.replace(classIdentifierToken, stringInSwift); }
From source file:com.satisfyingstructures.J2S.J2SConverter.java
License:Open Source License
@Override public void exitLiteral(Java8Parser.LiteralContext ctx) { TerminalNode tn = ctx.getChild(TerminalNode.class, 0); Token token = tn.getSymbol(); boolean isInteger = false; switch (token.getType()) { case Java8Parser.IntegerLiteral: isInteger = true;/* w w w. j a v a 2 s .c om*/ case Java8Parser.FloatingPointLiteral: String numberLiteral = token.getText(); int len = numberLiteral.length(); int idx; boolean replace = false; // Check for and strip trailing number width/precision indicators switch (numberLiteral.charAt(len - 1)) { case 'D': case 'F': case 'L': case 'd': case 'f': case 'l': numberLiteral = numberLiteral.substring(0, --len); replace = true; break; } // Check for uppercase non-decimal radix indicators Swift wants them to be lowercase. Also ensure that // non-hexadecimal, non-scientific notation floating point numbers have a decimal point present and // not orphaned at either end. if (numberLiteral.length() > 1) switch (numberLiteral.charAt(1)) { case 'B': case 'O': case 'X': numberLiteral = numberLiteral.toLowerCase(); replace = true; break; case 'b': case 'o': case 'x': break; default: if (isInteger) break; idx = numberLiteral.indexOf("."); if (idx <= 0 || idx >= len - 1) if (-1 == numberLiteral.indexOf("e") && -1 == numberLiteral.indexOf("E")) { replace = true; if (idx == -1) numberLiteral += ".0"; else if (idx == 0) numberLiteral = "0" + numberLiteral; else if (idx == len - 1) numberLiteral += "0"; } break; } if (replace) rewriter.replace(token, numberLiteral); break; case Java8Parser.BooleanLiteral: break; case Java8Parser.CharacterLiteral: // Swift has narrower set of backslash-escaped characters, and no octal; these need translating to // unicode form, but to obscure for effort. break; case Java8Parser.StringLiteral: // ditto. break; case Java8Parser.NullLiteral: rewriter.replace(token, "nil"); break; } }
From source file:com.satisfyingstructures.J2S.J2SConverter.java
License:Open Source License
@Override public void exitSwitchBlock(Java8Parser.SwitchBlockContext ctx) { if (ctx.getChildCount() <= 2) return; // (...assure the compiler) // Ensure any trailing switch labels are grouped and end with a break, and we add a default clause if missing List<Java8Parser.SwitchLabelContext> switchLabels = ctx.switchLabel(); Java8Parser.SwitchLabelContext switchLabelCtx; Token appendToToken = null;/* ww w .j a v a 2 s . co m*/ String append = ""; // First need newline + indent for our insertions String wrap = rewriter.lineBreak; Token token = rewriter.getTokenPreceding(ctx.getChild(ParserRuleContext.class, 0).start); if (null != token && token.getType() == Java8Parser.WS) wrap += token.getText(); // Get the last switch label if (!switchLabels.isEmpty()) { groupConsecutiveSwitchLabels(switchLabels); // Check if the last label is a default switchLabelCtx = switchLabels.get(switchLabels.size() - 1); if (switchLabelCtx.getChildCount() != 2) // two tokens for default + :, three for case + value + : { if (switchLabels.size() > 1) // can't merge previous case labels with default, so have to fallthrough append = wrap + rewriter.singleIndent + "fallthrough"; append += wrap + "default:"; } append += wrap + rewriter.singleIndent + "break"; appendToToken = switchLabelCtx.stop; } else { // No orphan switch labels, so child count - 2 is count of statement groups. Get the last one. Java8Parser.SwitchBlockStatementGroupContext grp = ctx .switchBlockStatementGroup(ctx.getChildCount() - 3); switchLabels = grp.switchLabels().switchLabel(); // Check if the last label is a default switchLabelCtx = switchLabels.get(switchLabels.size() - 1); if (switchLabelCtx.getChildCount() != 2) // two tokens for default + :, three for case + value + : { append += wrap + "default:" + wrap + rewriter.singleIndent + "break"; appendToToken = grp.stop; } } if (null != appendToToken) rewriter.insertAfter(appendToToken, append); }
From source file:com.satisfyingstructures.J2S.J2SConverter.java
License:Open Source License
private void groupConsecutiveSwitchLabels(List<Java8Parser.SwitchLabelContext> switchLabels) { if (switchLabels.size() < 2) return;//from www. j a v a 2 s . c o m // Ensure consecutive switch labels are joined by comma instead of :\n\s+case int index = 0, indexLast = switchLabels.size(); TerminalNode colonNode = null; for (Java8Parser.SwitchLabelContext switchLabel : switchLabels) { if (1 == ++index) { colonNode = switchLabel.getToken(Java8Parser.COLON, 0); continue; } if (switchLabel.getChildCount() == 2) // ==> default { Token token = rewriter.getTokenPreceding(switchLabel.start); String indent = null != token && token.getType() == Java8Parser.WS ? token.getText() : ""; rewriter.insertAfter(colonNode, rewriter.lineBreak + indent + rewriter.singleIndent + "fallthrough"); } else { rewriter.replace(colonNode, ","); colonNode = switchLabel.getToken(Java8Parser.COLON, 0); rewriter.replace(switchLabel.getToken(Java8Parser.CASE, 0), " "); } } }
From source file:com.satisfyingstructures.J2S.J2SRewriter.java
License:Open Source License
static String discoverLineBreakType(TokenStream tokens) { String s = "\n"; for (int i = 0, sz = tokens.size(); i < sz; i++) { Token t = tokens.get(i); if (null == t || t.getType() != Java8Parser.LB) continue; s = t.getText(); if (-1 != (i = s.indexOf(s.charAt(0), 1))) // remove any later repetitions s = s.substring(0, i);/*ww w . ja v a 2 s. c o m*/ break; } return s; }
From source file:com.satisfyingstructures.J2S.J2SRewriter.java
License:Open Source License
static String discoverSingelIndentType(TokenStream tokens) { String singleIndent = "\t"; int depth = 0; List<Token> recentIndentsByDepth = new ArrayList<>(); Map<String, Integer> countOfIndentStyle = new HashMap<>(); int countOfIndents = 0; for (int i = 0, sz = tokens.size(); i < sz; i++) { Token t = tokens.get(i);/*w w w.j a v a 2 s . co m*/ if (null == t) continue; switch (t.getType()) { case Java8Parser.RBRACE: break; case Java8Parser.LBRACE: depth++; for (int j = recentIndentsByDepth.size(); j <= depth; j++) recentIndentsByDepth.add(j, null); default: continue; } if (i > 2) { Token tWS = tokens.get(i - 1); if (tWS.getType() == Java8Parser.WS && tokens.get(i - 2).getType() == Java8Parser.LB) { recentIndentsByDepth.set(depth, tWS); if (depth + 1 < recentIndentsByDepth.size() && null != (t = recentIndentsByDepth.get(depth + 1))) { String deep = tWS.getText(); String deeper = t.getText(); if (deeper.startsWith(deep)) { singleIndent = deeper.substring(deep.length()); Integer count = countOfIndentStyle.get(singleIndent); if (null == count) countOfIndentStyle.put(singleIndent, 1); else { float share = count.floatValue() / (float) countOfIndents; if (countOfIndents >= 4 && share == 1) return singleIndent; // winner, consistent use if (countOfIndents > 10 && share > .9) return singleIndent; // winner, inconsistent use if (countOfIndents > 20 && share > .7) return singleIndent; // winner, variable use countOfIndentStyle.put(singleIndent, count + 1); } countOfIndents++; } recentIndentsByDepth.set(depth + 1, null); } } } depth--; } // No early winner, so pick most frequent int best = 0; for (Map.Entry<String, Integer> indentStyle : countOfIndentStyle.entrySet()) { int count = indentStyle.getValue(); countOfIndents -= count; // --> remaining if (best < count) { best = count; singleIndent = indentStyle.getKey(); if (countOfIndents < count) return singleIndent; // cant be beaten now } } return singleIndent; }
From source file:com.shelloid.script.Compiler.java
void compileError(Token token, String msg) { String errorMsg = "Syntax Error at: " + token.getLine() + ": " + token.getCharPositionInLine() + " near " + token.getText() + ", cause: " + msg; errorMsgs.add(errorMsg);/*from ww w . j av a2 s . c o m*/ }
From source file:com.shelloid.script.TokenInfo.java
public TokenInfo(Token token) { line = token.getLine(); charPos = token.getCharPositionInLine(); text = token.getText(); }
From source file:com.spotify.heroic.grammar.CoreQueryParser.java
License:Apache License
private QueryListener parse(Function<HeroicQueryParser, ParserRuleContext> op, String input) { final HeroicQueryLexer lexer = new HeroicQueryLexer(new ANTLRInputStream(input)); final CommonTokenStream tokens = new CommonTokenStream(lexer); final HeroicQueryParser parser = new HeroicQueryParser(tokens); parser.removeErrorListeners();/*from w w w . ja v a 2 s. c o m*/ parser.setErrorHandler(new BailErrorStrategy()); final ParserRuleContext context; try { context = op.apply(parser); } catch (final ParseCancellationException e) { if (!(e.getCause() instanceof RecognitionException)) { throw e; } throw toParseException((RecognitionException) e.getCause()); } final QueryListener listener = new QueryListener(); ParseTreeWalker.DEFAULT.walk(listener, context); final Token last = lexer.getToken(); if (last.getType() != Token.EOF) { throw new ParseException(String.format("garbage at end of string: '%s'", last.getText()), null, last.getLine(), last.getCharPositionInLine()); } return listener; }