Search in sources :

Example 26 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project xtext-core by eclipse.

the class LexerErrorTest method testLexerError_02.

@Test
public void testLexerError_02() throws Exception {
    String model = "a 'incomplete string";
    InternalSimpleExpressionsTestLanguageLexer lexer = new InternalSimpleExpressionsTestLanguageLexer();
    lexer.setCharStream(new ANTLRStringStream(model));
    CommonTokenStream stream = new CommonTokenStream(lexer);
    @SuppressWarnings("unchecked") List<CommonToken> tokens = stream.getTokens();
    assertEquals(tokens.toString(), 3, tokens.size());
    assertEquals("a", tokens.get(0).getText());
    assertEquals(" ", tokens.get(1).getText());
    assertEquals("'incomplete string", tokens.get(2).getText());
    assertEquals(0, tokens.get(2).getType());
}
Also used : InternalSimpleExpressionsTestLanguageLexer(org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonToken(org.antlr.runtime.CommonToken) Test(org.junit.Test)

Example 27 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project xtext-core by eclipse.

the class LexerErrorTest method testLexerError_03.

@Test
public void testLexerError_03() throws Exception {
    String model = "a '\\ incomplete string with bad escape sequence";
    InternalSimpleExpressionsTestLanguageLexer lexer = new InternalSimpleExpressionsTestLanguageLexer();
    lexer.setCharStream(new ANTLRStringStream(model));
    CommonTokenStream stream = new CommonTokenStream(lexer);
    @SuppressWarnings("unchecked") List<CommonToken> tokens = stream.getTokens();
    assertEquals(tokens.toString(), 3, tokens.size());
    assertEquals("a", tokens.get(0).getText());
    assertEquals(" ", tokens.get(1).getText());
    assertEquals("'\\ incomplete string with bad escape sequence", tokens.get(2).getText());
    assertEquals(0, tokens.get(2).getType());
}
Also used : InternalSimpleExpressionsTestLanguageLexer(org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonToken(org.antlr.runtime.CommonToken) Test(org.junit.Test)

Example 28 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project xtext-core by eclipse.

the class LexerErrorTest method testLexerError_04.

@Test
public void testLexerError_04() throws Exception {
    String model = "a 'incomplete string with bad escape sequence \\";
    InternalSimpleExpressionsTestLanguageLexer lexer = new InternalSimpleExpressionsTestLanguageLexer();
    lexer.setCharStream(new ANTLRStringStream(model));
    CommonTokenStream stream = new CommonTokenStream(lexer);
    @SuppressWarnings("unchecked") List<CommonToken> tokens = stream.getTokens();
    assertEquals(tokens.toString(), 3, tokens.size());
    assertEquals("a", tokens.get(0).getText());
    assertEquals(" ", tokens.get(1).getText());
    assertEquals("'incomplete string with bad escape sequence \\", tokens.get(2).getText());
    assertEquals(0, tokens.get(2).getType());
}
Also used : InternalSimpleExpressionsTestLanguageLexer(org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonToken(org.antlr.runtime.CommonToken) Test(org.junit.Test)

Example 29 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project xtext-core by eclipse.

the class TokenRegionProviderTest method getTokens.

protected List<CommonToken> getTokens(String model) {
    Lexer lexer = get(Lexer.class);
    lexer.setCharStream(new ANTLRStringStream(model));
    List<CommonToken> tokens = newArrayList();
    CommonToken currentToken;
    do {
        currentToken = (CommonToken) lexer.nextToken();
        // System.out.println(currentToken);
        tokens.add(currentToken);
    } while (currentToken != Token.EOF_TOKEN);
    return tokens;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) Lexer(org.eclipse.xtext.parser.antlr.Lexer) CommonToken(org.antlr.runtime.CommonToken)

Example 30 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project xtext-core by eclipse.

the class TokenRegionProvider method getTokenRegion.

public ITextRegion getTokenRegion(String text, ITextRegion region) {
    Lexer lexer = lexerProvider.get();
    lexer.setCharStream(new ANTLRStringStream(text));
    int currentStart = 0;
    int currentEnd = 0;
    CommonToken nextToken = (CommonToken) lexer.nextToken();
    int regionStart = region.getOffset();
    int regionEnd = regionStart + region.getLength();
    while (nextToken != Token.EOF_TOKEN && currentEnd <= regionStart) {
        currentStart = nextToken.getStartIndex();
        currentEnd = nextToken.getStopIndex() + 1;
        nextToken = (CommonToken) lexer.nextToken();
    }
    // nextToken is either EOF or the first token that follows the start of the given region
    while (nextToken != Token.EOF_TOKEN && currentEnd < regionEnd) {
        currentEnd = nextToken.getStopIndex() + 1;
        nextToken = (CommonToken) lexer.nextToken();
    }
    if (currentStart != regionStart || currentEnd != regionEnd)
        return new TextRegion(currentStart, currentEnd - currentStart);
    else
        return region;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) Lexer(org.eclipse.xtext.parser.antlr.Lexer) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) CommonToken(org.antlr.runtime.CommonToken)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)60 CommonTokenStream (org.antlr.runtime.CommonTokenStream)36 Test (org.junit.Test)20 CommonToken (org.antlr.runtime.CommonToken)15 Token (org.antlr.runtime.Token)13 CharStream (org.antlr.runtime.CharStream)11 Lexer (org.eclipse.xtext.parser.antlr.Lexer)10 RecognitionException (org.antlr.runtime.RecognitionException)9 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 CommonTree (org.antlr.runtime.tree.CommonTree)6 ActionSplitter (org.antlr.v4.parse.ActionSplitter)6 TokenStream (org.antlr.runtime.TokenStream)5 InternalXtendLexer (org.eclipse.xtend.core.parser.antlr.internal.InternalXtendLexer)4 WindowingException (com.sap.hadoop.windowing.WindowingException)3 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)3 ExprParser (org.apache.drill.common.expression.parser.ExprParser)3 CeylonLexer (com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer)2 CeylonParser (com.redhat.ceylon.compiler.typechecker.parser.CeylonParser)2 LineMap (com.sun.tools.javac.util.Position.LineMap)2 File (java.io.File)2