Search in sources :

Example 36 with Lexer

use of org.antlr.v4.runtime.Lexer in project ballerina by ballerina-lang.

the class TomlProcessor method parseTomlContent.

/**
 * Generate the proxy object by passing in the toml file.
 *
 * @param stream charstream object containing the content
 * @return proxy object
 */
public static ParseTree parseTomlContent(CharStream stream) {
    TomlLexer lexer = new TomlLexer(stream);
    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Pass the tokens to the parser
    TomlParser parser = new TomlParser(tokens);
    return parser.toml();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TomlLexer(org.ballerinalang.toml.antlr4.TomlLexer) TomlParser(org.ballerinalang.toml.antlr4.TomlParser)

Example 37 with Lexer

use of org.antlr.v4.runtime.Lexer in project graphql-java by graphql-java.

the class Parser method parseDocument.

public Document parseDocument(String input) {
    GraphqlLexer lexer = new GraphqlLexer(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    GraphqlParser parser = new GraphqlParser(tokens);
    parser.removeErrorListeners();
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.setErrorHandler(new BailErrorStrategy());
    GraphqlParser.DocumentContext document = parser.document();
    GraphqlAntlrToLanguage antlrToLanguage = new GraphqlAntlrToLanguage(tokens);
    antlrToLanguage.visitDocument(document);
    Token stop = document.getStop();
    List<Token> allTokens = tokens.getTokens();
    if (stop != null && allTokens != null && !allTokens.isEmpty()) {
        Token last = allTokens.get(allTokens.size() - 1);
        // 
        // do we have more tokens in the stream than we consumed in the parse?
        // if yes then its invalid.  We make sure its the same channel
        boolean notEOF = last.getType() != Token.EOF;
        boolean lastGreaterThanDocument = last.getTokenIndex() > stop.getTokenIndex();
        boolean sameChannel = last.getChannel() == stop.getChannel();
        if (notEOF && lastGreaterThanDocument && sameChannel) {
            throw new ParseCancellationException("There are more tokens in the query that have not been consumed");
        }
    }
    return antlrToLanguage.getResult();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) GraphqlLexer(graphql.parser.antlr.GraphqlLexer) GraphqlParser(graphql.parser.antlr.GraphqlParser) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) Token(org.antlr.v4.runtime.Token)

Example 38 with Lexer

use of org.antlr.v4.runtime.Lexer in project robozonky by RoboZonky.

the class SideEffectFreeParser method modifyInterpreter.

private static void modifyInterpreter(final NaturalLanguageStrategyLexer l) {
    final int originalSize = l.getInterpreter().decisionToDFA.length;
    // give our own array so the static one isn't used
    final DFA[] emptyDFA = new DFA[originalSize];
    final LexerATNSimulator newInterpreter = new LexerATNSimulator(l, l.getATN(), emptyDFA, new PredictionContextCache());
    // initialize our array so that the lexer functions properly
    newInterpreter.clearDFA();
    // replace the interpreter to bypass all static caches
    l.setInterpreter(newInterpreter);
}
Also used : LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) PredictionContextCache(org.antlr.v4.runtime.atn.PredictionContextCache) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 39 with Lexer

use of org.antlr.v4.runtime.Lexer in project titan.EclipsePlug-ins by eclipse.

the class CfgParseTreePrinter method printHiddenTokensBefore.

/**
 * Builds hidden tokens before the token
 * @param aToken the token, this will NOT be printed
 * @param aTokens token list from the lexer (all, hidden and not hidden also)
 */
private void printHiddenTokensBefore(final Token aToken, final List<Token> aTokens) {
    final int tokenIndex = aToken.getTokenIndex();
    if (tokenIndex == -1) {
        // because token has no index in the token list.
        return;
    }
    int startHiddenIndex = tokenIndex;
    while (isHiddenToken(startHiddenIndex - 1, aTokens)) {
        startHiddenIndex--;
    }
    for (int i = startHiddenIndex; i < tokenIndex; i++) {
        final Token t = aTokens.get(i);
        final String tokenText = t.getText();
        mSb.append(tokenText != null ? tokenText : "");
    }
}
Also used : Token(org.antlr.v4.runtime.Token)

Example 40 with Lexer

use of org.antlr.v4.runtime.Lexer in project titan.EclipsePlug-ins by eclipse.

the class ASN1ReferenceParser method parseReference.

private Reference parseReference(final IFile file, final String input, final int line, final int offset) {
    Reference reference = null;
    StringReader reader = new StringReader(input);
    CharStream charStream = new UnbufferedCharStream(reader);
    Asn1Lexer lexer = new Asn1Lexer(charStream);
    lexer.setTokenFactory(new TokenWithIndexAndSubTokensFactory(true));
    ASN1Listener lexerListener = new ASN1Listener();
    // remove ConsoleErrorListener
    lexer.removeErrorListeners();
    lexer.addErrorListener(lexerListener);
    ModuleLevelTokenStreamTracker tracker = new ModuleLevelTokenStreamTracker(lexer);
    tracker.discard(Asn1Lexer.WS);
    tracker.discard(Asn1Lexer.MULTILINECOMMENT);
    tracker.discard(Asn1Lexer.SINGLELINECOMMENT);
    Asn1Parser parser = new Asn1Parser(tracker);
    parser.setProject(file.getProject());
    parser.setActualFile(file);
    parser.setLine(line);
    parser.setOffset(offset);
    parser.setBuildParseTree(false);
    ASN1Listener parserListener = new ASN1Listener();
    // remove ConsoleErrorListener
    parser.removeErrorListeners();
    parser.addErrorListener(parserListener);
    reference = parser.pr_parseReference().reference;
    return reference;
}
Also used : Asn1Parser(org.eclipse.titan.designer.parsers.asn1parser.Asn1Parser) Reference(org.eclipse.titan.designer.AST.Reference) Asn1Lexer(org.eclipse.titan.designer.parsers.asn1parser.Asn1Lexer) StringReader(java.io.StringReader) ASN1Listener(org.eclipse.titan.designer.parsers.asn1parser.ASN1Listener) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) ModuleLevelTokenStreamTracker(org.eclipse.titan.designer.parsers.asn1parser.ModuleLevelTokenStreamTracker) TokenWithIndexAndSubTokensFactory(org.eclipse.titan.designer.parsers.asn1parser.TokenWithIndexAndSubTokensFactory) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Aggregations

Test (org.junit.Test)427 LexerGrammar (org.antlr.v4.tool.LexerGrammar)407 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)279 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)143 Grammar (org.antlr.v4.tool.Grammar)125 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)108 CharStream (org.antlr.v4.runtime.CharStream)103 ParseTree (org.antlr.v4.runtime.tree.ParseTree)91 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)56 IOException (java.io.IOException)45 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 Token (org.antlr.v4.runtime.Token)41 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)39 ArrayList (java.util.ArrayList)37 RecognitionException (org.antlr.v4.runtime.RecognitionException)26 StringReader (java.io.StringReader)23 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)23 TokenStream (org.antlr.v4.runtime.TokenStream)23 Lexer (org.antlr.v4.runtime.Lexer)22