List of usage examples for org.antlr.v4.runtime Token getText
String getText();
From source file:org.kaazing.k3po.lang.regex.NamedGroupPattern.java
License:Open Source License
public static NamedGroupPattern compile(final String regexWithGroupNames) { try {/* w w w . j a v a 2 s. c o m*/ ByteArrayInputStream input = new ByteArrayInputStream(regexWithGroupNames.getBytes(UTF_8)); CharStream ais = new ANTLRInputStream(input); Lexer lexer = new RegexLexer(ais); TokenStream tokens = new CommonTokenStream(lexer); RegexParser parser = new RegexParser(tokens); parser.setErrorHandler(new BailErrorStrategy()); final List<String> groupNames = new ArrayList<String>(); parser.addParseListener(new RegexBaseListener() { @Override public void exitGroupN(GroupNContext ctx) { Token captureVar = ctx.capture; // Not every entry in groupN populates groupNames if (captureVar != null) { String capture = captureVar.getText(); String groupName = capture.substring(2, capture.length() - 1); groupNames.add(groupName); } } }); LiteralContext literal = parser.literal(); String regex = literal.regex.getText(); return new NamedGroupPattern(Pattern.compile(regex), groupNames); } catch (IOException ioe) { PatternSyntaxException pse = new PatternSyntaxException("I/O exception", regexWithGroupNames, 0); pse.initCause(ioe); throw pse; } catch (ParseCancellationException e) { Throwable cause = e.getCause(); if (cause instanceof RecognitionException) { RecognitionException re = (RecognitionException) cause; PatternSyntaxException pse = new PatternSyntaxException("Unexpected type", regexWithGroupNames, re.getInputStream().index()); pse.initCause(re); throw pse; } throw e; } catch (RecognitionException re) { PatternSyntaxException pse = new PatternSyntaxException("Unexpected type", regexWithGroupNames, re.getInputStream().index()); pse.initCause(re); throw pse; } }
From source file:org.kaazing.robot.lang.parser.ScriptParserImpl.java
License:Open Source License
private ScriptParseException createScriptParseException(RobotParser parser, RecognitionException re) { if (re instanceof InputMismatchException) { return createScriptParseException(parser, (InputMismatchException) re); } else if (re instanceof NoViableAltException) { return createScriptParseException(parser, (NoViableAltException) re); } else {/*from ww w . j ava 2 s .co m*/ Token token = re.getOffendingToken(); String desc = String.format("line %d:%d: ", token.getLine(), token.getCharPositionInLine()); String tokenText = token.getText(); String msg = null; if (tokenText == null) { msg = "error: end of input"; } else { desc = String.format("%s'%s'", desc, tokenText); @SuppressWarnings("unused") String unexpectedTokenName = token.getType() != -1 ? parser.getTokenNames()[token.getType()] : parser.getTokenNames()[0]; msg = String.format("error: unexpected keyword '%s'", tokenText); } return new ScriptParseException(msg, re); } }
From source file:org.kaazing.robot.lang.regex.NamedGroupPattern.java
License:Open Source License
public static NamedGroupPattern compile(String regexWithGroupNames) { try {//from w w w.ja va 2 s . c o m ByteArrayInputStream input = new ByteArrayInputStream(regexWithGroupNames.getBytes(UTF_8)); CharStream ais = new ANTLRInputStream(input); Lexer lexer = new RegexLexer(ais); TokenStream tokens = new CommonTokenStream(lexer); RegexParser parser = new RegexParser(tokens); final List<String> groupNames = new ArrayList<String>(); parser.addParseListener(new RegexBaseListener() { @Override public void exitGroupN(GroupNContext ctx) { Token captureVar = ctx.capture; // Not every entry in groupN populates groupNames if (captureVar != null) { String capture = captureVar.getText(); String groupName = capture.substring(2, capture.length() - 1); groupNames.add(groupName); } } }); LiteralContext literal = parser.literal(); String regex = literal.regex.getText(); return new NamedGroupPattern(Pattern.compile(regex), groupNames); } catch (IOException e) { PatternSyntaxException pse = new PatternSyntaxException("I/O exception", regexWithGroupNames, 0); pse.initCause(e); throw pse; } catch (RecognitionException e) { PatternSyntaxException pse = new PatternSyntaxException("Unexpected type", regexWithGroupNames, e.getInputStream().index()); pse.initCause(e); throw pse; } }
From source file:org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor.java
License:Apache License
@Override public BaseNode visitNameDefinition(FEEL_1_1Parser.NameDefinitionContext ctx) { List<String> tokenStrs = new ArrayList<>(); List<Token> tokens = new ArrayList<>(); for (int i = 0; i < ctx.getChildCount(); i++) { visit(ctx.getChild(i));/*from www . j a v a 2 s. c o m*/ } ParserHelper.getAllTokens(ctx, tokens); for (Token t : tokens) { tokenStrs.add(t.getText()); } return ASTBuilderFactory.newNameDefNode(ctx, tokenStrs); }
From source file:org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor.java
License:Apache License
@Override public BaseNode visitIterationNameDefinition(FEEL_1_1Parser.IterationNameDefinitionContext ctx) { List<String> tokenStrs = new ArrayList<>(); List<Token> tokens = new ArrayList<>(); for (int i = 0; i < ctx.getChildCount(); i++) { visit(ctx.getChild(i));/*from w w w. j ava 2 s . c o m*/ } ParserHelper.getAllTokens(ctx, tokens); for (Token t : tokens) { tokenStrs.add(t.getText()); } return ASTBuilderFactory.newNameDefNode(ctx, tokenStrs); }
From source file:org.kie.dmn.feel.parser.feel11.ParserHelper.java
License:Apache License
public void startVariable(Token t) { this.currentScope.start(t.getText()); }
From source file:org.kie.dmn.feel.parser.feel11.ParserHelper.java
License:Apache License
public boolean followUp(Token t, boolean isPredict) { boolean dynamicResolutionResult = isDynamicResolution() && FEELParser.isVariableNamePartValid(t.getText(), currentScope); boolean follow = dynamicResolutionResult || this.currentScope.followUp(t.getText(), isPredict); // in case isPredict == false, will need to followUp in the currentScope, so that the TokenTree currentNode is updated as per expectations, // this is because the `follow` variable above, in the case of short-circuited on `dynamicResolutionResult`, // would skip performing any necessary update in the second part of the || predicate if (dynamicResolutionResult && !isPredict) { this.currentScope.followUp(t.getText(), isPredict); }//from ww w . ja va2s . com return follow; }
From source file:org.ledyba.sora.parser.FortranParser.java
License:Open Source License
/** * Check for include and end of file. T_INCLUDE is not in the grammar * so this method must be called after every statement (and initially * at the beginning of program unit file). *///from w ww. j a v a 2 s . c om public void checkForInclude() { // consume bare T_EOS while (_input.LA(1) == FortranLexer.T_EOS) { _input.consume(); } if (_input.LA(1) == FortranLexer.T_INCLUDE) { String files[]; _input.consume(); // consume T_INCLUDE // get include filename from token stream files = _input.LT(1).getText().split(":"); _input.consume(); // consume T_INCLUDE_NAME // check for empty include file (no statements) if (_input.LA(1) == FortranLexer.T_EOF) { Token tk = _input.LT(1); _input.consume(); files = tk.getText().split(":"); } // include acts like a statement so need to see if another include follows checkForInclude(); } else if (_input.LA(1) == FortranLexer.T_EOF) { Token tk = _input.LT(1); String[] files = tk.getText().split(":"); _input.consume(); // unwind T_EOFs for include files containing includes checkForInclude(); } }
From source file:org.mar9000.pe.PEASTListener.java
License:Apache License
@Override public void enterExtension(ExtensionContext ctx) { PEExtension extension = EcoreFactory.INSTANCE.createPEExtension(); extension.setExtensionName(ctx.extensionName.getText()); language.getExtensions().add(extension); for (Token t : ctx.rootNodes) { String nodeName = t.getText(); PENodeType peNodeType = resolvePENodeType(nodeName); peNodeType.setRoot(true);/*from w ww .ja va 2 s . c o m*/ extension.getRootNodes().add(peNodeType); } }
From source file:org.netbeans.gnu.buildsystem.autoconf.lexer.ACLexer.java
License:Open Source License
@Override public org.netbeans.api.lexer.Token<ACTokenId> nextToken() { Token token = lexer.nextToken(); logger.fine((token == null) ? "token: null" : token.getText() + ":" + token.getType()); if (token == null) { throw new IllegalStateException("Token unexpectedly null."); }// w ww . j av a2 s . c o m if (token.getType() == autoconfLexer.EOF) { return null; } return info.tokenFactory().createToken(ACLanguageHierarchy.getToken(token.getType())); }