Search in sources :

Example 46 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project kie-wb-common by kiegroup.

the class JavaParserFactory method newParser.

private static JavaParser newParser(final CharStream charStream, StringBuilder source, final JavaParserBase.ParserMode mode) {
    final JavaLexer lexer = new JavaLexer(charStream);
    final TokenStream tokenStream = new CommonTokenStream(lexer);
    final JavaParser parser = new JavaParser(tokenStream, source, mode);
    return parser;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream)

Example 47 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project cuba by cuba-platform.

the class Jpa2GrammarTest method testEscape.

@Test
public void testEscape() throws Exception {
    String query = "c.name like :pattern escape '/'";
    CharStream cs = new AntlrNoCaseStringStream(query);
    JPA2Lexer lexer = new JPA2Lexer(cs);
    TokenStream tstream = new CommonTokenStream(lexer);
    JPA2Parser jpa2Parser = new JPA2Parser(tstream);
    JPA2Parser.like_expression_return aReturn = jpa2Parser.like_expression();
    Assertions.assertNotNull(aReturn);
    query = "c.name like :pattern escape '.'";
    cs = new AntlrNoCaseStringStream(query);
    lexer = new JPA2Lexer(cs);
    tstream = new CommonTokenStream(lexer);
    jpa2Parser = new JPA2Parser(tstream);
    aReturn = jpa2Parser.like_expression();
    Assertions.assertTrue(isValid((CommonTree) aReturn.getTree()));
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) CharStream(org.antlr.runtime.CharStream) Test(org.junit.jupiter.api.Test)

Example 48 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project scheduler by btrplace.

the class ScriptBuilder method build.

/**
 * Internal method to check a script from a stream.
 *
 * @param cs the stream to analyze
 * @return the built script
 * @throws ScriptBuilderException in an error occurred while building the script
 */
// For the UnsupportedOperationException
@SuppressWarnings("squid:S1166")
private Script build(CharStream cs) throws ScriptBuilderException {
    Script v = new Script();
    ANTLRBtrplaceSL2Lexer lexer = new ANTLRBtrplaceSL2Lexer(cs);
    ErrorReporter errorReporter = errBuilder.build(v);
    lexer.setErrorReporter(errorReporter);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ANTLRBtrplaceSL2Parser parser = new ANTLRBtrplaceSL2Parser(tokens);
    parser.setErrorReporter(errorReporter);
    SymbolsTable t = new SymbolsTable();
    parser.setTreeAdaptor(new BtrPlaceTreeAdaptor(v, model, namingServiceNodes, namingServiceVMs, tpls, errorReporter, t, includes, catalog));
    try {
        BtrPlaceTree tree = (BtrPlaceTree) parser.script_decl().getTree();
        if (tree != null) {
            if (tree.token != null) {
                // Single instruction
                tree.go(tree);
            } else {
                for (int i = 0; i < tree.getChildCount(); i++) {
                    tree.getChild(i).go(tree);
                }
            }
        }
    } catch (RecognitionException e) {
        throw new ScriptBuilderException(e.getMessage(), e);
    } catch (UnsupportedOperationException e) {
        // We only keep the error message
        errorReporter.append(0, 0, e.getMessage());
    }
    if (!errorReporter.getErrors().isEmpty()) {
        throw new ScriptBuilderException(errorReporter);
    }
    return v;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) BtrPlaceTree(org.btrplace.btrpsl.tree.BtrPlaceTree) ANTLRBtrplaceSL2Lexer(org.btrplace.btrpsl.antlr.ANTLRBtrplaceSL2Lexer) ANTLRBtrplaceSL2Parser(org.btrplace.btrpsl.antlr.ANTLRBtrplaceSL2Parser) BtrPlaceTreeAdaptor(org.btrplace.btrpsl.tree.BtrPlaceTreeAdaptor) RecognitionException(org.antlr.runtime.RecognitionException)

Example 49 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project alfresco-remote-api by Alfresco.

the class WhereCompiler method compileSelectClause.

public static CommonTree compileSelectClause(String selectParam) throws RecognitionException {
    // lexer splits input into tokens
    ANTLRStringStream input = new ANTLRStringStream(selectParam);
    TokenStream tokens = new CommonTokenStream(new WhereClauseLexer(input));
    // parser generates abstract syntax tree
    WhereClauseParser parser = new WhereClauseParser(tokens);
    WhereClauseParser.selectClause_return ret = parser.selectClause();
    // acquire parse result
    CommonTree ast = (CommonTree) ret.getTree();
    if (logger.isDebugEnabled())
        print(ast, 0);
    return ast;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) WhereClauseParser(org.alfresco.rest.antlr.WhereClauseParser) WhereClauseLexer(org.alfresco.rest.antlr.WhereClauseLexer)

Example 50 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project pentaho-kettle by pentaho.

the class Edi2Xml method processRow.

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (Edi2XmlMeta) smi;
    data = (Edi2XmlData) sdi;
    // get row, blocks when needed!
    Object[] r = getRow();
    if (r == null) {
        // no more input to be expected...
        setOutputDone();
        return false;
    }
    String inputValue = "";
    if (first) {
        first = false;
        data.inputRowMeta = getInputRowMeta().clone();
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        String realInputField = environmentSubstitute(meta.getInputField());
        String realOutputField = environmentSubstitute(meta.getOutputField());
        data.inputFieldIndex = getInputRowMeta().indexOfValue(realInputField);
        int numErrors = 0;
        if (data.inputFieldIndex < 0) {
            logError(BaseMessages.getString(PKG, "Edi2Xml.Log.CouldNotFindInputField", realInputField));
            numErrors++;
        }
        if (!data.inputRowMeta.getValueMeta(data.inputFieldIndex).isString()) {
            logError(BaseMessages.getString(PKG, "Edi2Xml.Log.InputFieldIsNotAString", realInputField));
            numErrors++;
        }
        if (numErrors > 0) {
            setErrors(numErrors);
            stopAll();
            return false;
        }
        data.inputMeta = data.inputRowMeta.getValueMeta(data.inputFieldIndex);
        if (Utils.isEmpty(meta.getOutputField())) {
            // same field
            data.outputMeta = data.outputRowMeta.getValueMeta(data.inputFieldIndex);
            data.outputFieldIndex = data.inputFieldIndex;
        } else {
            // new field
            data.outputMeta = data.outputRowMeta.searchValueMeta(realOutputField);
            data.outputFieldIndex = data.outputRowMeta.size() - 1;
        }
        // create instances of lexer/tokenstream/parser
        // treat null values as empty strings for parsing purposes
        inputValue = Const.NVL(data.inputMeta.getString(r[data.inputFieldIndex]), "");
        lexer = new FastSimpleGenericEdifactDirectXMLLexer(new ANTLRStringStream(inputValue));
        tokens = new CommonTokenStream(lexer);
        parser = new FastSimpleGenericEdifactDirectXMLParser(tokens);
    } else {
        // treat null values as empty strings for parsing purposes
        inputValue = Const.NVL(data.inputMeta.getString(r[data.inputFieldIndex]), "");
        lexer.setCharStream(new ANTLRStringStream(inputValue));
        tokens.setTokenSource(lexer);
        parser.setTokenStream(tokens);
    }
    try {
        parser.edifact();
        // make sure the row is big enough
        r = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
        // place parsing result into output field
        r[data.outputFieldIndex] = parser.buf.toString();
        putRow(data.outputRowMeta, r);
    } catch (MismatchedTokenException e) {
        StringBuilder errorMessage = new StringBuilder(180);
        errorMessage.append("error parsing edi on line " + e.line + " position " + e.charPositionInLine);
        errorMessage.append(": expecting " + ((e.expecting > -1) ? parser.getTokenNames()[e.expecting] : "<UNKNOWN>") + " but found ");
        errorMessage.append((e.token.getType() >= 0) ? parser.getTokenNames()[e.token.getType()] : "<EOF>");
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), r, 1L, errorMessage.toString(), environmentSubstitute(meta.getInputField()), "MALFORMED_EDI");
        } else {
            logError(errorMessage.toString());
            // try to determine the error line
            String errorline = "<UNKNOWN>";
            try {
                errorline = inputValue.split("\\r?\\n")[e.line - 1];
            } catch (Exception ee) {
            // Ignore pattern syntax errors
            }
            logError("Problem line: " + errorline);
            logError(StringUtils.leftPad("^", e.charPositionInLine + "Problem line: ".length() + 1));
            throw new KettleException(e);
        }
    } catch (RecognitionException e) {
        StringBuilder errorMessage = new StringBuilder(180);
        errorMessage.append("error parsing edi on line ").append(e.line).append(" position ").append(e.charPositionInLine).append(". ").append(e.toString());
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), r, 1L, errorMessage.toString(), environmentSubstitute(meta.getInputField()), "MALFORMED_EDI");
        } else {
            logError(errorMessage.toString());
            // try to determine the error line
            String errorline = "<UNKNOWN>";
            try {
                errorline = inputValue.split("\\r?\\n")[e.line - 1];
            } catch (Exception ee) {
            // Ignore pattern syntax errors
            }
            logError("Problem line: " + errorline);
            logError(StringUtils.leftPad("^", e.charPositionInLine + "Problem line: ".length() + 1));
            throw new KettleException(e);
        }
    }
    if (checkFeedback(getLinesRead())) {
        logBasic("Linenr " + getLinesRead());
    }
    return true;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) KettleException(org.pentaho.di.core.exception.KettleException) FastSimpleGenericEdifactDirectXMLParser(org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLParser) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) KettleException(org.pentaho.di.core.exception.KettleException) RecognitionException(org.antlr.runtime.RecognitionException) FastSimpleGenericEdifactDirectXMLLexer(org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLLexer) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) 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