Search in sources :

Example 81 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestTokenStreamRewriter method testReplaceRangeThenInsertAfterRightEdge.

@Test
public void testReplaceRangeThenInsertAfterRightEdge() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
    String input = "abcccba";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.replace(2, 4, "x");
    tokens.insertAfter(4, "y");
    String result = tokens.getText();
    String expecting = "abxyba";
    assertEquals(expecting, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) TokenStreamRewriter(org.antlr.v4.runtime.TokenStreamRewriter) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Example 82 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestTokenStreamRewriter method testReplaceThenDeleteMiddleIndex.

@Test
public void testReplaceThenDeleteMiddleIndex() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
    String input = "abc";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.replace(1, "x");
    tokens.delete(1);
    String result = tokens.getText();
    String expecting = "ac";
    assertEquals(expecting, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) TokenStreamRewriter(org.antlr.v4.runtime.TokenStreamRewriter) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Example 83 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestUnicodeGrammar method binaryGrammar.

@Test
public void binaryGrammar() throws Exception {
    String grammarText = "grammar Binary;\n" + "r : HEADER PACKET+ FOOTER;\n" + "HEADER : '\\u0002\\u0000\\u0001\\u0007';\n" + "PACKET : '\\u00D0' ('\\u00D1' | '\\u00D2' | '\\u00D3') +;\n" + "FOOTER : '\\u00FF';\n";
    byte[] toParse = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x07, (byte) 0xD0, (byte) 0xD2, (byte) 0xD2, (byte) 0xD3, (byte) 0xD3, (byte) 0xD3, (byte) 0xD0, (byte) 0xD3, (byte) 0xD3, (byte) 0xD1, (byte) 0xFF };
    CharStream charStream;
    try (ByteArrayInputStream is = new ByteArrayInputStream(toParse);
        // U+0000 to U+00FF.
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.ISO_8859_1)) {
        charStream = new ANTLRInputStream(isr);
    }
    Grammar grammar = new Grammar(grammarText);
    LexerInterpreter lexEngine = grammar.createLexerInterpreter(charStream);
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = grammar.createGrammarParserInterpreter(tokens);
    ParseTree parseTree = parser.parse(grammar.rules.get("r").index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(grammar.getRuleNames());
    String result = Trees.toStringTree(parseTree, nodeTextProvider);
    assertEquals("(r:1  ÐÒÒÓÓÓ ÐÓÓÑ ÿ)", result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Grammar(org.antlr.v4.tool.Grammar) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 84 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestUnbufferedCharStream method testAFewTokens.

@Test
public void testAFewTokens() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
    // Tokens: 012345678901234567
    // Input:  x = 3 * 0 + 2 * 0;
    TestingUnbufferedCharStream input = createStream("x = 302 * 91 + 20234234 * 0;");
    LexerInterpreter lexEngine = g.createLexerInterpreter(input);
    // copy text into tokens from char stream
    lexEngine.setTokenFactory(new CommonTokenFactory(true));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    String result = tokens.LT(1).getText();
    String expecting = "x";
    assertEquals(expecting, result);
    tokens.fill();
    expecting = "[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1], [@2,2:2='=',<4>,1:2]," + " [@3,3:3=' ',<7>,1:3], [@4,4:6='302',<2>,1:4], [@5,7:7=' ',<7>,1:7]," + " [@6,8:8='*',<6>,1:8], [@7,9:9=' ',<7>,1:9], [@8,10:11='91',<2>,1:10]," + " [@9,12:12=' ',<7>,1:12], [@10,13:13='+',<5>,1:13], [@11,14:14=' ',<7>,1:14]," + " [@12,15:22='20234234',<2>,1:15], [@13,23:23=' ',<7>,1:23]," + " [@14,24:24='*',<6>,1:24], [@15,25:25=' ',<7>,1:25], [@16,26:26='0',<2>,1:26]," + " [@17,27:27=';',<3>,1:27], [@18,28:27='',<-1>,1:28]]";
    assertEquals(expecting, tokens.getTokens().toString());
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 85 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project L42 by ElvisResearchGroup.

the class ReplState method add.

public ReplState add(String code) {
    Expression.ClassB cbEmpty = new ClassB(Doc.empty(), new ast.Ast.InterfaceHeader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Position.noInfo);
    try {
        //parse
        Expression.ClassB codeTmp = (ClassB) Parser.parse("Repl", "{" + code + "}");
        //new original
        ClassReuse newOriginal = this.originalL;
        List<ast.Expression.ClassB.Member> newOriginalMs = newOriginal.getInner().getMs();
        newOriginalMs.addAll(codeTmp.getMs());
        newOriginal.withInner(newOriginal.getInner().withMs(newOriginalMs));
        //new src to desugar
        List<ClassB.Member> newMs = new ArrayList<>();
        int nestedAdded = 0;
        for (Member m : this.desugaredL.getMs()) {
            if (!(m instanceof NestedClass)) {
                continue;
            }
            NestedClass nc = (NestedClass) m;
            newMs.add(new ClassB.NestedClass(Doc.empty(), nc.getName(), cbEmpty, nc.getP()));
            nestedAdded += 1;
        }
        newMs.addAll(codeTmp.getMs());
        codeTmp = codeTmp.withMs(newMs);
        Expression code2 = Desugar.of(codeTmp);
        ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
        // TODO: will die after new reduction Refresh of position identities, it is used to generate correct Java code.
        code3 = (ExpCore.ClassB) code3.accept(new CloneVisitor() {

            @Override
            public ExpCore visit(ExpCore.ClassB cb) {
                Position p = cb.getP();
                cb = cb.withP(new Position(p.getFile(), p.getLine1(), p.getPos1(), p.getLine2(), p.getPos2(), p.get_next()));
                return super.visit(cb);
            }
        });
        //integrate new desugared src with old desugared code
        List<Member> resultMs = new ArrayList<>(this.desugaredL.getMs());
        for (int i = nestedAdded; i < code3.getMs().size(); i++) {
            resultMs.add(code3.getMs().get(i));
        }
        code3 = code3.withMs(resultMs);
        //call the repl and return
        ExpCore.ClassB result = ProgramReduction.allSteps(code3);
        return new ReplState(this.originalS + "\n" + code, newOriginal, result);
    } catch (ParseCancellationException parser) {
        System.out.println(parser.getMessage());
        return null;
    } catch (ErrorMessage msg) {
        ErrorFormatter.topFormatErrorMessage(msg);
        return null;
    }
}
Also used : ExpCore(ast.ExpCore) Position(ast.Ast.Position) ClassB(ast.Expression.ClassB) ArrayList(java.util.ArrayList) NestedClass(ast.ExpCore.ClassB.NestedClass) CloneVisitor(coreVisitors.CloneVisitor) NestedClass(ast.ExpCore.ClassB.NestedClass) Expression(ast.Expression) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ClassReuse(ast.Expression.ClassReuse) ErrorMessage(ast.ErrorMessage) Member(ast.ExpCore.ClassB.Member) ClassB(ast.Expression.ClassB) InjectionOnCore(sugarVisitors.InjectionOnCore)

Aggregations

Test (org.junit.Test)131 LexerGrammar (org.antlr.v4.tool.LexerGrammar)78 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)52 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)49 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)44 ATN (org.antlr.v4.runtime.atn.ATN)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)38 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)38 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)38 ParseTree (org.antlr.v4.runtime.tree.ParseTree)29 ATNState (org.antlr.v4.runtime.atn.ATNState)11 Grammar (org.antlr.v4.tool.Grammar)10 STGroupString (org.stringtemplate.v4.STGroupString)10 ByteBuffer (java.nio.ByteBuffer)8 IntBuffer (java.nio.IntBuffer)8 UTF8CodePointDecoder (org.antlr.v4.runtime.UTF8CodePointDecoder)8 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 DOTGenerator (org.antlr.v4.tool.DOTGenerator)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 Rule (org.antlr.v4.tool.Rule)7