Search in sources :

Example 21 with LexerInterpreter

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

the class TestTokenStreamRewriter method testReplaceSingleMiddleThenOverlappingSuperset.

@Test
public void testReplaceSingleMiddleThenOverlappingSuperset() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
    String input = "abcba";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.replace(2, 2, "xyz");
    tokens.replace(0, 3, "foo");
    String result = tokens.getText();
    String expecting = "fooa";
    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 22 with LexerInterpreter

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

the class TestTokenStreamRewriter method testReplaceAll.

@Test
public void testReplaceAll() 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(0, 6, "x");
    String result = tokens.getText();
    String expecting = "x";
    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 23 with LexerInterpreter

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

the class TestTokenStreamRewriter method testReplaceThenReplaceSuperset.

@Test
public void testReplaceThenReplaceSuperset() 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, "xyz");
    tokens.replace(3, 5, "foo");
    stream.fill();
    // overlaps, error
    Exception exc = null;
    try {
        tokens.getText();
    } catch (IllegalArgumentException iae) {
        exc = iae;
    }
    String expecting = "replace op boundaries of <ReplaceOp@[@3,3:3='c',<3>,1:3]..[@5,5:5='b',<2>,1:5]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<3>,1:2]..[@4,4:4='c',<3>,1:4]:\"xyz\">";
    assertNotNull(exc);
    assertEquals(expecting, exc.getMessage());
}
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 24 with LexerInterpreter

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

the class TestTokenStreamRewriter method testToStringStartStop.

@Test
public void testToStringStartStop() throws Exception {
    LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "MUL : '*';\n" + "ASSIGN : '=';\n" + "WS : ' '+;\n");
    // Tokens: 0123456789
    // Input:  x = 3 * 0;
    String input = "x = 3 * 0;";
    LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream stream = new CommonTokenStream(lexEngine);
    stream.fill();
    TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
    tokens.replace(4, 8, "0");
    stream.fill();
    // replace 3 * 0 with 0
    String result = tokens.getTokenStream().getText();
    String expecting = "x = 3 * 0;";
    assertEquals(expecting, result);
    result = tokens.getText();
    expecting = "x = 0;";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(0, 9));
    expecting = "x = 0;";
    assertEquals(expecting, result);
    result = tokens.getText(Interval.of(4, 8));
    expecting = "0";
    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 25 with LexerInterpreter

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

the class TestTokenStreamRewriter method test2ReplaceMiddleIndex1InsertBefore.

@Test
public void test2ReplaceMiddleIndex1InsertBefore() 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.insertBefore(0, "_");
    tokens.replace(1, "x");
    tokens.replace(1, "y");
    String result = tokens.getText();
    String expecting = "_ayc";
    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)

Aggregations

LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 Test (org.junit.Test)54 LexerGrammar (org.antlr.v4.tool.LexerGrammar)53 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)52 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 CharStream (org.antlr.v4.runtime.CharStream)11 Token (org.antlr.v4.runtime.Token)6 TokenStream (org.antlr.v4.runtime.TokenStream)6 ParseTree (org.antlr.v4.runtime.tree.ParseTree)6 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)6 BufferedTokenStream (org.antlr.v4.runtime.BufferedTokenStream)5 StringReader (java.io.StringReader)4 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 ArrayList (java.util.ArrayList)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 Grammar (org.antlr.v4.tool.Grammar)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1