Search in sources :

Example 6 with ParseException

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

the class ScriptExecutor method parseScript.

/**
 * Parse the script into an ANTLR parse tree, and convert this parse tree
 * into a SystemML program. Parsing includes lexical/syntactic analysis.
 */
protected void parseScript() {
    try {
        ParserWrapper parser = ParserFactory.createParser(script.getScriptType());
        Map<String, Object> inputParameters = script.getInputParameters();
        Map<String, String> inputParametersStringMaps = MLContextUtil.convertInputParametersForParser(inputParameters, script.getScriptType());
        String scriptExecutionString = script.getScriptExecutionString();
        dmlProgram = parser.parse(null, scriptExecutionString, inputParametersStringMaps);
    } catch (ParseException e) {
        throw new MLContextException("Exception occurred while parsing script", e);
    }
}
Also used : ParserWrapper(org.apache.sysml.parser.ParserWrapper) ParseException(org.apache.sysml.parser.ParseException)

Example 7 with ParseException

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

the class DMLScript method main.

/**
 * @param args command-line arguments
 * @throws IOException if an IOException occurs
 */
public static void main(String[] args) throws IOException {
    Configuration conf = new Configuration(ConfigurationManager.getCachedJobConf());
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    try {
        DMLScript.executeScript(conf, otherArgs);
    } catch (ParseException pe) {
        System.err.println(pe.getMessage());
    } catch (DMLScriptException e) {
        // In case of DMLScriptException, simply print the error message.
        System.err.println(e.getMessage());
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) MRJobConfiguration(org.apache.sysml.runtime.matrix.mapred.MRJobConfiguration) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) ParseException(org.apache.sysml.parser.ParseException) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser)

Example 8 with ParseException

use of org.apache.sysml.parser.ParseException in project incubator-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 9 with ParseException

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

the class DmlSyntacticValidator method exitImportStatement.

// -----------------------------------------------------------------
// "src" statment
// -----------------------------------------------------------------
@Override
public void exitImportStatement(ImportStatementContext ctx) {
    // prepare import filepath
    String filePath = ctx.filePath.getText();
    String namespace = DMLProgram.DEFAULT_NAMESPACE;
    if (ctx.namespace != null && ctx.namespace.getText() != null && !ctx.namespace.getText().isEmpty()) {
        namespace = ctx.namespace.getText();
    }
    if ((filePath.startsWith("\"") && filePath.endsWith("\"")) || filePath.startsWith("'") && filePath.endsWith("'")) {
        filePath = filePath.substring(1, filePath.length() - 1);
    }
    File file = new File(filePath);
    if (!file.isAbsolute()) {
        // concatenate working directory to filepath
        filePath = _workingDir + File.separator + filePath;
    }
    validateNamespace(namespace, filePath, ctx);
    String scriptID = DMLProgram.constructFunctionKey(namespace, filePath);
    DMLProgram prog = null;
    if (!_scripts.get().containsKey(scriptID)) {
        _scripts.get().put(scriptID, namespace);
        try {
            prog = (new DMLParserWrapper()).doParse(filePath, null, getQualifiedNamespace(namespace), argVals);
        } catch (ParseException e) {
            notifyErrorListeners(e.getMessage(), ctx.start);
            return;
        }
        // Custom logic whether to proceed ahead or not. Better than the current exception handling mechanism
        if (prog == null) {
            notifyErrorListeners("One or more errors found during importing a program from file " + filePath, ctx.start);
            return;
        } else {
            ctx.info.namespaces = new HashMap<>();
            ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
            ctx.info.stmt = new ImportStatement();
            ((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
            ((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
            ((ImportStatement) ctx.info.stmt).setNamespace(namespace);
        }
    } else {
        // Skip redundant parsing (to prevent potential infinite recursion) and
        // create empty program for this context to allow processing to continue.
        prog = new DMLProgram();
        ctx.info.namespaces = new HashMap<>();
        ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
        ctx.info.stmt = new ImportStatement();
        ((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
        ((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
        ((ImportStatement) ctx.info.stmt).setNamespace(namespace);
    }
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ImportStatement(org.apache.sysml.parser.ImportStatement) File(java.io.File)

Example 10 with ParseException

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

the class PydmlSyntacticValidator method exitImportStatement.

// -----------------------------------------------------------------
// "src" statment
// -----------------------------------------------------------------
@Override
public void exitImportStatement(ImportStatementContext ctx) {
    // prepare import filepath
    String filePath = ctx.filePath.getText();
    String namespace = DMLProgram.DEFAULT_NAMESPACE;
    if (ctx.namespace != null && ctx.namespace.getText() != null && !ctx.namespace.getText().isEmpty()) {
        namespace = ctx.namespace.getText();
    }
    if ((filePath.startsWith("\"") && filePath.endsWith("\"")) || filePath.startsWith("'") && filePath.endsWith("'")) {
        filePath = filePath.substring(1, filePath.length() - 1);
    }
    File file = new File(filePath);
    if (!file.isAbsolute()) {
        // concatenate working directory to filepath
        filePath = _workingDir + File.separator + filePath;
    }
    validateNamespace(namespace, filePath, ctx);
    String scriptID = DMLProgram.constructFunctionKey(namespace, filePath);
    DMLProgram prog = null;
    if (!_scripts.get().containsKey(scriptID)) {
        _scripts.get().put(scriptID, namespace);
        try {
            prog = (new PyDMLParserWrapper()).doParse(filePath, null, getQualifiedNamespace(namespace), argVals);
        } catch (ParseException e) {
            notifyErrorListeners(e.getMessage(), ctx.start);
            return;
        }
        // Custom logic whether to proceed ahead or not. Better than the current exception handling mechanism
        if (prog == null) {
            notifyErrorListeners("One or more errors found during importing a program from file " + filePath, ctx.start);
            return;
        } else {
            ctx.info.namespaces = new HashMap<>();
            ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
            ctx.info.stmt = new ImportStatement();
            ((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
            ((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
            ((ImportStatement) ctx.info.stmt).setNamespace(namespace);
        }
    } else {
        // Skip redundant parsing (to prevent potential infinite recursion) and
        // create empty program for this context to allow processing to continue.
        prog = new DMLProgram();
        ctx.info.namespaces = new HashMap<>();
        ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
        ctx.info.stmt = new ImportStatement();
        ((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
        ((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
        ((ImportStatement) ctx.info.stmt).setNamespace(namespace);
    }
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ImportStatement(org.apache.sysml.parser.ImportStatement) File(java.io.File)

Aggregations

ParseException (org.apache.sysml.parser.ParseException)10 IOException (java.io.IOException)5 DMLProgram (org.apache.sysml.parser.DMLProgram)5 LanguageException (org.apache.sysml.parser.LanguageException)5 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)3 DMLScriptException (org.apache.sysml.runtime.DMLScriptException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)2 AlreadySelectedException (org.apache.commons.cli.AlreadySelectedException)2 DMLConfig (org.apache.sysml.conf.DMLConfig)2 ImportStatement (org.apache.sysml.parser.ImportStatement)2