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
*/
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;
}
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;
}
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;
}
}
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(ctx, null, null);
ctx.info.stmt.setEmptyNewLineStatement(true);
} catch (LanguageException e) {
e.printStackTrace();
}
}
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;
}
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;
}
}
use of org.apache.sysml.parser.LanguageException in project incubator-systemml by apache.
the class GenerateClassesForMLContext method addFunctionMethods.
/**
* Add methods to a derived script class to allow invocation of script
* functions.
*
* @param scriptFilePath
* the path to a script file
* @param ctNewScript
* the javassist compile-time class representation of a script
*/
public static void addFunctionMethods(String scriptFilePath, CtClass ctNewScript) {
try {
DMLProgram dmlProgram = dmlProgramFromScriptFilePath(scriptFilePath);
if (dmlProgram == null) {
System.out.println("Could not generate DML Program for: " + scriptFilePath);
return;
}
Map<String, FunctionStatementBlock> defaultNsFsbsMap = dmlProgram.getFunctionStatementBlocks(DMLProgram.DEFAULT_NAMESPACE);
List<FunctionStatementBlock> fsbs = new ArrayList<>();
fsbs.addAll(defaultNsFsbsMap.values());
for (FunctionStatementBlock fsb : fsbs) {
ArrayList<Statement> sts = fsb.getStatements();
for (Statement st : sts) {
if (!(st instanceof FunctionStatement)) {
continue;
}
FunctionStatement fs = (FunctionStatement) st;
String dmlFunctionCall = generateDmlFunctionCall(scriptFilePath, fs);
String functionCallMethod = generateFunctionCallMethod(scriptFilePath, fs, dmlFunctionCall);
CtMethod m = CtNewMethod.make(functionCallMethod, ctNewScript);
ctNewScript.addMethod(m);
addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, false);
addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, true);
}
}
} catch (LanguageException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
} catch (CannotCompileException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
} catch (RuntimeException e) {
System.out.println("Could not add function methods for " + ctNewScript.getName());
}
}
Aggregations