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;
}
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations