Search in sources :

Example 76 with CommonTokenStream

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

the class LexerErrorTest method testLexerError_05.

@Test
public void testLexerError_05() 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 77 with CommonTokenStream

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

the class LexerErrorTest method testLexerError_01.

@Test
public void testLexerError_01() throws Exception {
    String model = "a /* incomplete comment *";
    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 comment *", 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 78 with CommonTokenStream

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

the class LexerErrorTest method testLexerError_06.

@Test
public void testLexerError_06() throws Exception {
    String model = "a '";
    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("'", tokens.get(2).getText());
    assertEquals(InternalSimpleExpressionsTestLanguageParser.RULE_ANY_OTHER, 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 79 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drools by kiegroup.

the class DSLTokenizedMappingFile method buildFileMappingWalker.

private DSLMapWalker buildFileMappingWalker(final List<ParserError> errors, CharStream stream) throws RecognitionException {
    DSLMapLexer lexer = new DSLMapLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    DSLMapParser parser = new DSLMapParser(tokens);
    DSLMapParser.mapping_file_return example = parser.mapping_file();
    CommonTree tree = (CommonTree) example.getTree();
    // logger.info(tree.toStringTree());
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    DSLMapWalker walker = new DSLMapWalker(nodes);
    errors.addAll(lexer.getErrors());
    errors.addAll(parser.getErrors());
    return walker;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 80 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drools by kiegroup.

the class DrlExprParser method parse.

/**
 * Parse an expression from text
 */
public ConstraintConnectiveDescr parse(final String text) {
    ConstraintConnectiveDescr constraint = null;
    try {
        DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
        CommonTokenStream input = new CommonTokenStream(lexer);
        RecognizerSharedState state = new RecognizerSharedState();
        helper = new ParserHelper(input, state, languageLevel);
        DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
        parser.setBuildDescr(true);
        // setting initial value just in case
        parser.setLeftMostExpr(null);
        BaseDescr expr = parser.conditionalOrExpression();
        if (expr != null && !parser.hasErrors()) {
            constraint = ConstraintConnectiveDescr.newAnd();
            constraint.addOrMerge(expr);
        }
    } catch (RecognitionException e) {
        helper.reportError(e);
    }
    return constraint;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) DRLExpressions(org.drools.drl.parser.lang.DRLExpressions) DRLLexer(org.drools.drl.parser.lang.DRLLexer) ParserHelper(org.drools.drl.parser.lang.ParserHelper) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

CommonTokenStream (org.antlr.runtime.CommonTokenStream)93 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)62 RecognitionException (org.antlr.runtime.RecognitionException)25 CharStream (org.antlr.runtime.CharStream)22 CommonTree (org.antlr.runtime.tree.CommonTree)21 TokenStream (org.antlr.runtime.TokenStream)17 File (java.io.File)12 Test (org.junit.Test)12 CommonToken (org.antlr.runtime.CommonToken)10 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)10 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)10 ExprParser (org.apache.drill.common.expression.parser.ExprParser)10 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)8 ANTLRInputStream (org.antlr.runtime.ANTLRInputStream)8 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 InputStreamReader (java.io.InputStreamReader)6 Token (org.antlr.runtime.Token)6 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)5 JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)5