Search in sources :

Example 16 with LanguageException

use of org.apache.sysml.parser.LanguageException in project incubator-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;
        }
        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) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) AssignmentStatement(org.apache.sysml.parser.AssignmentStatement)

Example 17 with LanguageException

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

the class DataTypeChangeTest method runValidateTest.

private void runValidateTest(String fullTestName, boolean expectedException) {
    boolean raisedException = false;
    try {
        // Tell the superclass about the name of this test, so that the superclass can
        // create temporary directories.
        TestConfiguration testConfig = new TestConfiguration(TEST_CLASS_DIR, fullTestName, new String[] {});
        addTestConfiguration(fullTestName, 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(fullTestName))) {
            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.liveVariableAnalysis(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) DMLException(org.apache.sysml.api.DMLException) 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)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