Search in sources :

Example 6 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class CommonSyntacticValidator method setPrintStatement.

// -----------------------------------------------------------------
// Helper Functions for exit*FunctionCall*AssignmentStatement
// -----------------------------------------------------------------
protected void setPrintStatement(ParserRuleContext ctx, String functionName, ArrayList<ParameterExpression> paramExpression, StatementInfo thisinfo) {
    if (DMLScript.VALIDATOR_IGNORE_ISSUES == true) {
        // create dummy print statement
        try {
            thisinfo.stmt = new PrintStatement(ctx, functionName, currentFile);
        } catch (LanguageException e) {
            e.printStackTrace();
        }
        return;
    }
    int numParams = paramExpression.size();
    if (numParams == 0) {
        notifyErrorListeners(functionName + "() must have more than 0 parameters", ctx.start);
        return;
    } else if (numParams == 1) {
        Expression expr = paramExpression.get(0).getExpr();
        if (expr == null) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
        try {
            List<Expression> expList = new ArrayList<>();
            expList.add(expr);
            thisinfo.stmt = new PrintStatement(ctx, functionName, expList, currentFile);
        } catch (LanguageException e) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
    } else if (numParams > 1) {
        if ("stop".equals(functionName)) {
            notifyErrorListeners("stop() function cannot have more than 1 parameter", ctx.start);
            return;
        }
        Expression firstExp = paramExpression.get(0).getExpr();
        if (firstExp == null) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
        if (!(firstExp instanceof StringIdentifier)) {
            notifyErrorListeners("printf-style functionality requires first print parameter to be a string", ctx.start);
            return;
        }
        try {
            List<Expression> expressions = new ArrayList<>();
            for (ParameterExpression pe : paramExpression) {
                Expression expression = pe.getExpr();
                expressions.add(expression);
            }
            thisinfo.stmt = new PrintStatement(ctx, functionName, expressions, currentFile);
        } catch (LanguageException e) {
            notifyErrorListeners("cannot process " + functionName + "() function", ctx.start);
            return;
        }
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) StringIdentifier(org.apache.sysml.parser.StringIdentifier) ParameterExpression(org.apache.sysml.parser.ParameterExpression) PrintStatement(org.apache.sysml.parser.PrintStatement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class DMLParserWrapper method createDMLProgram.

private static DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
    DMLProgram dmlPgm = new DMLProgram();
    String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
    dmlPgm.getNamespaces().put(namespace, dmlPgm);
    // First add all the functions
    for (FunctionStatementContext fn : ast.functionBlocks) {
        FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
        functionStmtBlk.addStatement(fn.info.stmt);
        try {
            dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
        } catch (LanguageException e) {
            LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
            return null;
        }
    }
    // Then add all the statements
    for (StatementContext stmtCtx : ast.blocks) {
        org.apache.sysml.parser.Statement current = stmtCtx.info.stmt;
        if (current == null) {
            LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
            return null;
        }
        if (current instanceof ImportStatement) {
            // Handle import statements separately
            if (stmtCtx.info.namespaces != null) {
                // Add the DMLProgram entries into current program
                for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
                    // TODO handle namespace key already exists for different program value instead of overwriting
                    DMLProgram prog = entry.getValue();
                    if (prog != null && prog.getNamespaces().size() > 0) {
                        dmlPgm.getNamespaces().put(entry.getKey(), prog);
                    }
                    // Add dependent programs (handle imported script that also imports scripts)
                    for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
                        String depNamespace = dependency.getKey();
                        DMLProgram depProgram = dependency.getValue();
                        if (dmlPgm.getNamespaces().get(depNamespace) == null) {
                            dmlPgm.getNamespaces().put(depNamespace, depProgram);
                        }
                    }
                }
            } else {
                LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
                return null;
            }
        }
        // Now wrap statement into individual statement block
        // merge statement will take care of merging these blocks
        dmlPgm.addStatementBlock(getStatementBlock(current));
    }
    // post-processing
    dmlPgm.hoistFunctionCallsFromExpressions();
    dmlPgm.mergeStatementBlocks();
    return dmlPgm;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) DMLProgram(org.apache.sysml.parser.DMLProgram) ImportStatement(org.apache.sysml.parser.ImportStatement) Map(java.util.Map) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) StatementContext(org.apache.sysml.parser.dml.DmlParser.StatementContext)

Example 8 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class PyDMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name
 * @param dmlScript script file contents
 * @param sourceNamespace namespace from source statement
 * @param argVals script arguments
 * @return dml program, or null if at least one error
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) {
    DMLProgram dmlPgm = null;
    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }
        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }
    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();
    try {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(tokens);
        // For now no optimization, since it is not able to parse integer value.
        boolean tryOptimizedParsing = false;
        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
            // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);
            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }
    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    PydmlPreprocessor prep = new PydmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    PydmlSyntacticValidator validator = new PydmlSyntacticValidator(errorListener, argVals, sourceNamespace, prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);
    return dmlPgm;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) LanguageException(org.apache.sysml.parser.LanguageException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ProgramrootContext(org.apache.sysml.parser.pydml.PydmlParser.ProgramrootContext) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.sysml.parser.ParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 9 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class PyDMLParserWrapper method createDMLProgram.

private static DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
    DMLProgram dmlPgm = new DMLProgram();
    String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
    dmlPgm.getNamespaces().put(namespace, dmlPgm);
    // First add all the functions
    for (FunctionStatementContext fn : ast.functionBlocks) {
        FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
        functionStmtBlk.addStatement(fn.info.stmt);
        try {
            dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
        } catch (LanguageException e) {
            LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
            return null;
        }
    }
    // Then add all the statements
    for (StatementContext stmtCtx : ast.blocks) {
        Statement current = stmtCtx.info.stmt;
        if (current == null) {
            LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
            return null;
        }
        // Ignore Newline logic
        if (current.isEmptyNewLineStatement()) {
            continue;
        }
        if (current instanceof ImportStatement) {
            // Handle import statements separately
            if (stmtCtx.info.namespaces != null) {
                // Add the DMLProgram entries into current program
                for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
                    // TODO handle namespace key already exists for different program value instead of overwriting
                    DMLProgram prog = entry.getValue();
                    if (prog != null && prog.getNamespaces().size() > 0) {
                        dmlPgm.getNamespaces().put(entry.getKey(), prog);
                    }
                    // Add dependent programs (handle imported script that also imports scripts)
                    for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
                        String depNamespace = dependency.getKey();
                        DMLProgram depProgram = dependency.getValue();
                        if (dmlPgm.getNamespaces().get(depNamespace) == null) {
                            dmlPgm.getNamespaces().put(depNamespace, depProgram);
                        }
                    }
                }
            } else {
                LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
                return null;
            }
        }
        // Now wrap statement into individual statement block
        // merge statement will take care of merging these blocks
        dmlPgm.addStatementBlock(getStatementBlock(current));
    }
    // post-processing
    dmlPgm.hoistFunctionCallsFromExpressions();
    dmlPgm.mergeStatementBlocks();
    return dmlPgm;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ImportStatement(org.apache.sysml.parser.ImportStatement) Statement(org.apache.sysml.parser.Statement) DMLProgram(org.apache.sysml.parser.DMLProgram) ImportStatement(org.apache.sysml.parser.ImportStatement) Map(java.util.Map) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext)

Example 10 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.

the class ParForDependencyAnalysisTest method runTest.

private void runTest(String scriptFilename, boolean expectedException) {
    boolean raisedException = false;
    try {
        // Tell the superclass about the name of this test, so that the superclass can
        // create temporary directories.
        int index = scriptFilename.lastIndexOf(".dml");
        String testName = scriptFilename.substring(0, index > 0 ? index : scriptFilename.length());
        TestConfiguration testConfig = new TestConfiguration(TEST_CLASS_DIR, testName, new String[] {});
        addTestConfiguration(testName, testConfig);
        loadTestConfiguration(testConfig);
        DMLConfig conf = new DMLConfig(getCurConfigFile().getPath());
        ConfigurationManager.setLocalConfig(conf);
        String dmlScriptString = "";
        HashMap<String, String> argVals = new HashMap<String, String>();
        // read script
        try (BufferedReader in = new BufferedReader(new FileReader(HOME + scriptFilename))) {
            String s1 = null;
            while ((s1 = in.readLine()) != null) dmlScriptString += s1 + "\n";
        }
        // parsing and dependency analysis
        ParserWrapper parser = ParserFactory.createParser(org.apache.sysml.api.mlcontext.ScriptType.DML);
        DMLProgram prog = parser.parse(DMLScript.DML_FILE_PATH_ANTLR_PARSER, dmlScriptString, argVals);
        DMLTranslator dmlt = new DMLTranslator(prog);
        dmlt.validateParseTree(prog);
    } catch (LanguageException ex) {
        raisedException = true;
        if (raisedException != expectedException)
            ex.printStackTrace();
    } catch (Exception ex2) {
        ex2.printStackTrace();
        throw new RuntimeException(ex2);
    // Assert.fail( "Unexpected exception occured during test run." );
    }
    // check correctness
    Assert.assertEquals(expectedException, raisedException);
}
Also used : DMLConfig(org.apache.sysml.conf.DMLConfig) HashMap(java.util.HashMap) TestConfiguration(org.apache.sysml.test.integration.TestConfiguration) DMLTranslator(org.apache.sysml.parser.DMLTranslator) LanguageException(org.apache.sysml.parser.LanguageException) LanguageException(org.apache.sysml.parser.LanguageException) BufferedReader(java.io.BufferedReader) DMLProgram(org.apache.sysml.parser.DMLProgram) FileReader(java.io.FileReader) ParserWrapper(org.apache.sysml.parser.ParserWrapper)

Aggregations

LanguageException (org.apache.sysml.parser.LanguageException)20 DMLProgram (org.apache.sysml.parser.DMLProgram)8 AssignmentStatement (org.apache.sysml.parser.AssignmentStatement)5 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 DMLTranslator (org.apache.sysml.parser.DMLTranslator)4 Expression (org.apache.sysml.parser.Expression)4 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)4 ParameterExpression (org.apache.sysml.parser.ParameterExpression)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 DMLConfig (org.apache.sysml.conf.DMLConfig)3 ProgramRewriter (org.apache.sysml.hops.rewrite.ProgramRewriter)3 BinaryExpression (org.apache.sysml.parser.BinaryExpression)3 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)3 DataExpression (org.apache.sysml.parser.DataExpression)3 ParseException (org.apache.sysml.parser.ParseException)3