Example usage for org.antlr.v4.runtime RecognitionException getInputStream

List of usage examples for org.antlr.v4.runtime RecognitionException getInputStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime RecognitionException getInputStream.

Prototype

public IntStream getInputStream() 

Source Link

Document

Gets the input stream which is the symbol source for the recognizer where this exception was thrown.

Usage

From source file:excecoes.SyntaxError.java

public SyntaxError(String message, RecognitionException e, int line, int charPositionInLine) {
    super(message, e.getRecognizer(), e.getInputStream(), (ParserRuleContext) e.getCtx());
    this.setOffendingToken(e.getOffendingToken());
    this.initCause(e);
    this.setLine(line);
    this.setCharPositionInLine(charPositionInLine);

}

From source file:org.kaazing.k3po.lang.internal.regex.NamedGroupPattern.java

License:Open Source License

public static NamedGroupPattern compile(final String regexWithGroupNames) {
    try {/*from  w  ww.  j av  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<>();
        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.k3po.lang.regex.NamedGroupPattern.java

License:Open Source License

public static NamedGroupPattern compile(final String regexWithGroupNames) {
    try {/*from  w w  w.ja v  a2  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;
    }
}