Search in sources :

Example 81 with CharStream

use of org.antlr.v4.runtime.CharStream in project Alpha by alpha-asp.

the class AnswerSetsParser method parse.

public static Set<AnswerSet> parse(CharStream stream) {
    final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(stream)));
    // Try SLL parsing mode (faster but may terminate incorrectly).
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());
    return VISITOR.translate(parser.answer_sets());
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)

Example 82 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.

the class LexerATNSimulator method execATN.

protected int execATN(CharStream input, DFAState ds0) {
    // System.out.println("enter exec index "+input.index()+" from "+ds0.configs);
    if (debug) {
        System.out.format(Locale.getDefault(), "start state closure=%s\n", ds0.configs);
    }
    if (ds0.isAcceptState) {
        // allow zero-length tokens
        captureSimState(prevAccept, input, ds0);
    }
    int t = input.LA(1);
    // s is current/from DFA state
    DFAState s = ds0;
    while (true) {
        // while more work
        if (debug) {
            System.out.format(Locale.getDefault(), "execATN loop starting closure: %s\n", s.configs);
        }
        // As we move src->trg, src->trg, we keep track of the previous trg to
        // avoid looking up the DFA state again, which is expensive.
        // If the previous target was already part of the DFA, we might
        // be able to avoid doing a reach operation upon t. If s!=null,
        // it means that semantic predicates didn't prevent us from
        // creating a DFA state. Once we know s!=null, we check to see if
        // the DFA state has an edge already for t. If so, we can just reuse
        // it's configuration set; there's no point in re-computing it.
        // This is kind of like doing DFA simulation within the ATN
        // simulation because DFA simulation is really just a way to avoid
        // computing reach/closure sets. Technically, once we know that
        // we have a previously added DFA state, we could jump over to
        // the DFA simulator. But, that would mean popping back and forth
        // a lot and making things more complicated algorithmically.
        // This optimization makes a lot of sense for loops within DFA.
        // A character will take us back to an existing DFA state
        // that already has lots of edges out of it. e.g., .* in comments.
        DFAState target = getExistingTargetState(s, t);
        if (target == null) {
            target = computeTargetState(input, s, t);
        }
        if (target == ERROR) {
            break;
        }
        // end of the token.
        if (t != IntStream.EOF) {
            consume(input);
        }
        if (target.isAcceptState) {
            captureSimState(prevAccept, input, target);
            if (t == IntStream.EOF) {
                break;
            }
        }
        t = input.LA(1);
        // flip; current DFA target becomes new src/from state
        s = target;
    }
    return failOrAccept(prevAccept, input, s.configs, t);
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState)

Example 83 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.

the class LexerATNSimulator method match.

public int match(CharStream input, int mode) {
    this.mode = mode;
    int mark = input.mark();
    try {
        this.startIndex = input.index();
        this.prevAccept.reset();
        DFA dfa = decisionToDFA[mode];
        if (dfa.s0 == null) {
            return matchATN(input);
        } else {
            return execATN(input, dfa.s0);
        }
    } finally {
        input.release(mark);
    }
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

Example 84 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.

the class LexerATNSimulator method matchATN.

protected int matchATN(CharStream input) {
    ATNState startState = atn.modeToStartState.get(mode);
    if (debug) {
        System.out.format(Locale.getDefault(), "matchATN mode %d start: %s\n", mode, startState);
    }
    int old_mode = mode;
    ATNConfigSet s0_closure = computeStartState(input, startState);
    boolean suppressEdge = s0_closure.hasSemanticContext;
    s0_closure.hasSemanticContext = false;
    DFAState next = addDFAState(s0_closure);
    if (!suppressEdge) {
        decisionToDFA[mode].s0 = next;
    }
    int predict = execATN(input, next);
    if (debug) {
        System.out.format(Locale.getDefault(), "DFA after matchATN: %s\n", decisionToDFA[old_mode].toLexerString());
    }
    return predict;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState)

Example 85 with CharStream

use of org.antlr.v4.runtime.CharStream in project antlr4 by antlr.

the class TestUnbufferedCharStream method test2CharAhead.

@Test
public void test2CharAhead() throws Exception {
    CharStream input = createStream("xy");
    assertEquals('x', input.LA(1));
    assertEquals('y', input.LA(2));
    assertEquals(IntStream.EOF, input.LA(3));
}
Also used : CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) Test(org.junit.Test)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)187 Test (org.junit.Test)93 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)85 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)46 ParseTree (org.antlr.v4.runtime.tree.ParseTree)38 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)23 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)23 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)22 File (java.io.File)21 TokenStream (org.antlr.v4.runtime.TokenStream)21 StringReader (java.io.StringReader)20 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)18 Token (org.antlr.v4.runtime.Token)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 Utils.toCharStream (clawfc.Utils.toCharStream)15 Path (java.nio.file.Path)14