Search in sources :

Example 31 with LanguageException

use of org.apache.sysml.parser.LanguageException in project 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 32 with LanguageException

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

the class CommonSyntacticValidator method exitAssignmentStatementHelper.

protected void exitAssignmentStatementHelper(ParserRuleContext ctx, String lhs, ExpressionInfo dataInfo, Token lhsStart, ExpressionInfo rhs, StatementInfo info) {
    if (lhs.startsWith("$")) {
        notifyErrorListeners("assignment of commandline parameters is not allowed. (Quickfix: try using someLocalVariable=ifdef(" + lhs + ", default value))", ctx.start);
        return;
    }
    DataIdentifier target = null;
    if (dataInfo.expr instanceof DataIdentifier) {
        target = (DataIdentifier) dataInfo.expr;
        Expression source = rhs.expr;
        try {
            info.stmt = new AssignmentStatement(ctx, target, source, currentFile);
        } catch (LanguageException e) {
            // TODO: extract more meaningful info from this exception.
            notifyErrorListeners("invalid assignment", lhsStart);
            return;
        }
    } else {
        notifyErrorListeners("incorrect lvalue in assignment statement", lhsStart);
        return;
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) DataIdentifier(org.apache.sysml.parser.DataIdentifier) 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) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement) MultiAssignmentStatement(org.apache.sysml.parser.MultiAssignmentStatement)

Example 33 with LanguageException

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

the class DMLParserWrapper method doParse.

/**
 * This function is supposed to be called directly only from DmlSyntacticValidator 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 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 {
        DmlLexer lexer = new DmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DmlParser antlr4Parser = new DmlParser(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
    DmlPreprocessor prep = new DmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    DmlSyntacticValidator validator = new DmlSyntacticValidator(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 : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) FileNotFoundException(java.io.FileNotFoundException) ProgramrootContext(org.apache.sysml.parser.dml.DmlParser.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) LanguageException(org.apache.sysml.parser.LanguageException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 34 with LanguageException

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

the class DmlSyntacticValidator method exitIfdefAssignmentStatement.

@Override
public void exitIfdefAssignmentStatement(IfdefAssignmentStatementContext ctx) {
    if (!ctx.commandLineParam.getText().startsWith("$")) {
        notifyErrorListeners("the first argument of ifdef function should be a commandline argument parameter (which starts with $)", ctx.commandLineParam.start);
        return;
    }
    if (ctx.targetList == null) {
        notifyErrorListeners("ifdef assignment needs an lvalue ", ctx.start);
        return;
    }
    String targetListText = ctx.targetList.getText();
    if (targetListText.startsWith("$")) {
        notifyErrorListeners("lhs of ifdef function cannot be a commandline parameters. Use local variable instead", ctx.start);
        return;
    }
    DataIdentifier target = null;
    if (ctx.targetList.dataInfo.expr instanceof DataIdentifier) {
        target = (DataIdentifier) ctx.targetList.dataInfo.expr;
        Expression source = null;
        if (ctx.commandLineParam.dataInfo.expr != null) {
            // Since commandline parameter is set
            // The check of following is done in fillExpressionInfoCommandLineParameters:
            // Command line param cannot be empty string
            // If you want to pass space, please quote it
            source = ctx.commandLineParam.dataInfo.expr;
        } else {
            source = ctx.source.info.expr;
        }
        try {
            ctx.info.stmt = new AssignmentStatement(ctx, target, source, currentFile);
        } catch (LanguageException e) {
            notifyErrorListeners("invalid assignment for ifdef function", ctx.targetList.start);
            return;
        }
    } else {
        notifyErrorListeners("incorrect lvalue in ifdef function ", ctx.targetList.start);
        return;
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) DataIdentifier(org.apache.sysml.parser.DataIdentifier) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement)

Example 35 with LanguageException

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

the class Connection method prepareScript.

/**
 * Prepares (precompiles) a script, sets input parameter values, and registers input and output variables.
 *
 * @param script string representing the DML or PyDML script
 * @param args map of input parameters ($) and their values
 * @param inputs string array of input variables to register
 * @param outputs string array of output variables to register
 * @param parsePyDML {@code true} if PyDML, {@code false} if DML
 * @return PreparedScript object representing the precompiled script
 */
public PreparedScript prepareScript(String script, Map<String, String> args, String[] inputs, String[] outputs, boolean parsePyDML) {
    DMLScript.SCRIPT_TYPE = parsePyDML ? ScriptType.PYDML : ScriptType.DML;
    // check for valid names of passed arguments
    String[] invalidArgs = args.keySet().stream().filter(k -> k == null || !k.startsWith("$")).toArray(String[]::new);
    if (invalidArgs.length > 0)
        throw new LanguageException("Invalid argument names: " + Arrays.toString(invalidArgs));
    // check for valid names of input and output variables
    String[] invalidVars = UtilFunctions.asSet(inputs, outputs).stream().filter(k -> k == null || k.startsWith("$")).toArray(String[]::new);
    if (invalidVars.length > 0)
        throw new LanguageException("Invalid variable names: " + Arrays.toString(invalidVars));
    setLocalConfigs();
    // simplified compilation chain
    Program rtprog = null;
    try {
        // parsing
        ParserWrapper parser = ParserFactory.createParser(parsePyDML ? ScriptType.PYDML : ScriptType.DML);
        DMLProgram prog = parser.parse(null, script, args);
        // language validate
        DMLTranslator dmlt = new DMLTranslator(prog);
        dmlt.liveVariableAnalysis(prog);
        dmlt.validateParseTree(prog);
        // hop construct/rewrite
        dmlt.constructHops(prog);
        dmlt.rewriteHopsDAG(prog);
        // rewrite persistent reads/writes
        RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs);
        ProgramRewriter rewriter2 = new ProgramRewriter(rewrite);
        rewriter2.rewriteProgramHopDAGs(prog);
        // lop construct and runtime prog generation
        dmlt.constructLops(prog);
        rtprog = dmlt.getRuntimeProgram(prog, _dmlconf);
        // final cleanup runtime prog
        JMLCUtils.cleanupRuntimeProgram(rtprog, outputs);
    } catch (ParseException pe) {
        // don't chain ParseException (for cleaner error output)
        throw pe;
    } catch (Exception ex) {
        throw new DMLException(ex);
    }
    // return newly create precompiled script
    return new PreparedScript(rtprog, inputs, outputs, _dmlconf, _cconf);
}
Also used : ParserFactory(org.apache.sysml.parser.ParserFactory) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) Arrays(java.util.Arrays) DMLTranslator(org.apache.sysml.parser.DMLTranslator) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite) FileSystem(org.apache.hadoop.fs.FileSystem) SpoofCompiler(org.apache.sysml.hops.codegen.SpoofCompiler) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) FrameReader(org.apache.sysml.runtime.io.FrameReader) HashMap(java.util.HashMap) DataConverter(org.apache.sysml.runtime.util.DataConverter) CompilerConfig(org.apache.sysml.conf.CompilerConfig) DMLException(org.apache.sysml.api.DMLException) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) TfMetaUtils(org.apache.sysml.runtime.transform.meta.TfMetaUtils) TfUtils(org.apache.sysml.runtime.transform.TfUtils) MatrixReader(org.apache.sysml.runtime.io.MatrixReader) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) Map(java.util.Map) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) Path(org.apache.hadoop.fs.Path) DMLProgram(org.apache.sysml.parser.DMLProgram) DMLConfig(org.apache.sysml.conf.DMLConfig) Program(org.apache.sysml.runtime.controlprogram.Program) LanguageException(org.apache.sysml.parser.LanguageException) ParserWrapper(org.apache.sysml.parser.ParserWrapper) RUNTIME_PLATFORM(org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM) ConfigurationManager(org.apache.sysml.conf.ConfigurationManager) MatrixReaderFactory(org.apache.sysml.runtime.io.MatrixReaderFactory) FrameReaderFactory(org.apache.sysml.runtime.io.FrameReaderFactory) IOException(java.io.IOException) ScriptType(org.apache.sysml.api.mlcontext.ScriptType) IOUtilFunctions(org.apache.sysml.runtime.io.IOUtilFunctions) InputStreamReader(java.io.InputStreamReader) ConfigType(org.apache.sysml.conf.CompilerConfig.ConfigType) Closeable(java.io.Closeable) JSONObject(org.apache.wink.json4j.JSONObject) DMLScript(org.apache.sysml.api.DMLScript) ParseException(org.apache.sysml.parser.ParseException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) UtilFunctions(org.apache.sysml.runtime.util.UtilFunctions) InputStream(java.io.InputStream) DataExpression(org.apache.sysml.parser.DataExpression) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) DMLException(org.apache.sysml.api.DMLException) DMLTranslator(org.apache.sysml.parser.DMLTranslator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException) LanguageException(org.apache.sysml.parser.LanguageException) DMLProgram(org.apache.sysml.parser.DMLProgram) ParserWrapper(org.apache.sysml.parser.ParserWrapper) ParseException(org.apache.sysml.parser.ParseException) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)

Aggregations

LanguageException (org.apache.sysml.parser.LanguageException)38 DMLProgram (org.apache.sysml.parser.DMLProgram)16 AssignmentStatement (org.apache.sysml.parser.AssignmentStatement)9 BufferedReader (java.io.BufferedReader)8 FileReader (java.io.FileReader)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)8 DMLTranslator (org.apache.sysml.parser.DMLTranslator)8 Expression (org.apache.sysml.parser.Expression)8 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)8 ParameterExpression (org.apache.sysml.parser.ParameterExpression)8 HashMap (java.util.HashMap)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DMLConfig (org.apache.sysml.conf.DMLConfig)6 HopsException (org.apache.sysml.hops.HopsException)6 ProgramRewriter (org.apache.sysml.hops.rewrite.ProgramRewriter)6 BinaryExpression (org.apache.sysml.parser.BinaryExpression)6 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)6 DataExpression (org.apache.sysml.parser.DataExpression)6 DataIdentifier (org.apache.sysml.parser.DataIdentifier)6