Search in sources :

Example 11 with LanguageException

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

the class DMLScript method createArgumentsMap.

///////////////////////////////
// private internal utils (argument parsing)
////////
@Deprecated()
protected static /**
	 * Creates an argument map appropriate for consumption by the backend
	 * The only method using this is the legacy {@link MLContext} api.
	 * Once that is removed, this function should be removed as well.
	 * This method uses a fragile position based argument for -args & -nvargs
	 * @param hasNamedArgs true for named arguments, false for positional arguments
	 * @param args in "k=v" format for named arguments and "v" for positional arguments
	 * @return	a map containing either ($K,V) or ($1,V) for named and positional arguments respectively
	 * @throws LanguageException when a named argument is an invalid identifier for DML/PyDML
	 */
Map<String, String> createArgumentsMap(boolean hasNamedArgs, String[] args) throws LanguageException {
    Map<String, String> argMap = new HashMap<String, String>();
    if (args == null)
        return argMap;
    for (int i = 1; i <= args.length; i++) {
        String arg = args[i - 1];
        if (arg.equalsIgnoreCase("-l") || arg.equalsIgnoreCase("-log") || arg.equalsIgnoreCase("-v") || arg.equalsIgnoreCase("-visualize") || arg.equalsIgnoreCase("-explain") || arg.equalsIgnoreCase("-debug") || arg.equalsIgnoreCase("-stats") || arg.equalsIgnoreCase("-exec") || arg.equalsIgnoreCase("-debug") || arg.startsWith("-config")) {
            throw new LanguageException("-args or -nvargs must be the final argument for DMLScript!");
        }
        //parse arguments (named args / args by position)
        if (hasNamedArgs) {
            // CASE: named argument argName=argValue -- must add <argName, argValue> pair to _argVals
            String[] argPieces = arg.split("=");
            if (argPieces.length < 2)
                throw new LanguageException("for -nvargs option, elements in arg list must be named and have form argName=argValue");
            String argName = argPieces[0];
            StringBuilder sb = new StringBuilder();
            for (int jj = 1; jj < argPieces.length; jj++) {
                sb.append(argPieces[jj]);
            }
            String varNameRegex = "^[a-zA-Z]([a-zA-Z0-9_])*$";
            if (!argName.matches(varNameRegex))
                throw new LanguageException("argName " + argName + " must be a valid variable name in DML. Valid variable names in DML start with upper-case or lower-case letter, and contain only letters, digits, or underscores");
            argMap.put("$" + argName, sb.toString());
        } else {
            // CASE: unnamed argument -- use position in arg list for name
            argMap.put("$" + i, arg);
        }
    }
    return argMap;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) HashMap(java.util.HashMap)

Example 12 with LanguageException

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

the class DMLScript method readDMLScript.

/**
	 * Reads the DML/PyDML script into a String
	 * @param isFile	Whether the string argument is a path to a file or the script itself
	 * @param scriptOrFilename script or filename
	 * @return a string representation of the script
	 * @throws IOException	if error
	 * @throws LanguageException	if error
	 */
protected static String readDMLScript(boolean isFile, String scriptOrFilename) throws IOException, LanguageException {
    String dmlScriptStr;
    if (isFile) {
        String fileName = scriptOrFilename;
        //read DML script from file
        if (fileName == null)
            throw new LanguageException("DML script path was not specified!");
        StringBuilder sb = new StringBuilder();
        BufferedReader in = null;
        try {
            //read from hdfs or gpfs file system
            if (fileName.startsWith("hdfs:") || fileName.startsWith("gpfs:")) {
                Path scriptPath = new Path(fileName);
                FileSystem fs = IOUtilFunctions.getFileSystem(scriptPath);
                in = new BufferedReader(new InputStreamReader(fs.open(scriptPath)));
            } else // from local file system
            {
                in = new BufferedReader(new FileReader(fileName));
            }
            //core script reading
            String tmp = null;
            while ((tmp = in.readLine()) != null) {
                sb.append(tmp);
                sb.append("\n");
            }
        } catch (IOException ex) {
            LOG.error("Failed to read the script from the file system", ex);
            throw ex;
        } finally {
            IOUtilFunctions.closeSilently(in);
        }
        dmlScriptStr = sb.toString();
    } else {
        String scriptString = scriptOrFilename;
        //parse given script string 
        if (scriptString == null)
            throw new LanguageException("DML script was not specified!");
        InputStream is = new ByteArrayInputStream(scriptString.getBytes());
        Scanner scan = new Scanner(is);
        dmlScriptStr = scan.useDelimiter("\\A").next();
        scan.close();
    }
    return dmlScriptStr;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) Path(org.apache.hadoop.fs.Path) Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileSystem(org.apache.hadoop.fs.FileSystem) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 13 with LanguageException

use of org.apache.sysml.parser.LanguageException 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
	 * @throws ParseException if ParseException occurs
	 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) throws ParseException {
    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 14 with LanguageException

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

the class PydmlSyntacticValidator method exitIgnoreNewLine.

// -----------------------------------------------------------------
//        PyDML Specific
// -----------------------------------------------------------------
@Override
public void exitIgnoreNewLine(IgnoreNewLineContext ctx) {
    // This is later ignored by PyDMLParserWrapper
    try {
        ctx.info.stmt = new AssignmentStatement(null, null, 0, 0, 0, 0);
        ctx.info.stmt.setEmptyNewLineStatement(true);
    } catch (LanguageException e) {
        e.printStackTrace();
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement)

Example 15 with LanguageException

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

the class PydmlSyntacticValidator 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("incorrect lvalue in ifdef function ", 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;
        }
        int line = ctx.start.getLine();
        int col = ctx.start.getCharPositionInLine();
        try {
            ctx.info.stmt = new AssignmentStatement(target, source, line, col, line, col);
            setFileLineColumn(ctx.info.stmt, ctx);
        } 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) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement)

Aggregations

LanguageException (org.apache.sysml.parser.LanguageException)17 DMLProgram (org.apache.sysml.parser.DMLProgram)7 AssignmentStatement (org.apache.sysml.parser.AssignmentStatement)5 Expression (org.apache.sysml.parser.Expression)4 ParameterExpression (org.apache.sysml.parser.ParameterExpression)4 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 BinaryExpression (org.apache.sysml.parser.BinaryExpression)3 BuiltinFunctionExpression (org.apache.sysml.parser.BuiltinFunctionExpression)3 DMLTranslator (org.apache.sysml.parser.DMLTranslator)3 DataIdentifier (org.apache.sysml.parser.DataIdentifier)3 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)3 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2