moduleDefnNode = ((SourceModelModuleSource)sourceDef).getParseTreeNode();
moduleDefnSourceModel = ((SourceModelModuleSource)sourceDef).getModuleDefn();
} else {
Reader moduleReader = sourceDef.getSourceReader(new Status("Read module status."));
if (moduleReader == null) {
logMessage(new CompilerMessage(new SourceRange(moduleSourceName), new MessageKind.Error.CouldNotReadModuleSource(moduleSourceName)));
return null;
}
moduleReader = new BufferedReader(moduleReader);
setInputStream(moduleReader, moduleSourceName);
try {
// Call the parser to parse the module definition
parser.module();
moduleDefnNode = (ParseTreeNode)parser.getAST();
} catch (antlr.RecognitionException e) {
// Recognition (syntax) error
final SourceRange sourceRange = CALParser.makeSourceRangeFromException(e);
logMessage(new CompilerMessage(sourceRange, new MessageKind.Error.SyntaxError(), e));
} catch (antlr.TokenStreamException e) {
// Bad token stream. Maybe a bad token (eg. a stray "$" sign)
logMessage(new CompilerMessage(new SourceRange(moduleSourceName), new MessageKind.Error.BadTokenStream(), e));
} finally {
// close the reader if any.
if (moduleReader != null) {
try {
moduleReader.close();
} catch (IOException ioe) {
COMPILER_LOGGER.log(Level.FINE, "Problem closing file for module \"" + moduleSourceName + "\"");
}
}
}
// Only try to create a source model if the parse was successful
if (moduleDefnNode != null && msgLogger.getNErrors() == nOldErrors) {
moduleDefnSourceModel = SourceModelBuilder.buildModuleDefn(moduleDefnNode);
}
}
// If parsing succeeded to produce an AST, continue with this module node..
// Note: only invoke the tree parser if there are no errors when parsing the module.
// Otherwise the tree parser is sure to fail (in a fatal error) and so there is no point doing this.
if (moduleDefnNode != null && msgLogger.getNErrors() == nOldErrors) {
// Debug..
if (DEBUG_SOURCE_MODEL || inUnitTestDebugSourceModelMode) {
//use the generated ParseTree to create a SourceModel.ModuleDefn.
SourceModel.ModuleDefn moduleDefn = SourceModelBuilder.buildModuleDefn(moduleDefnNode);
if (DEBUG_SOURCE_MODEL_VISITOR || inUnitTestDebugSourceModelMode) {
SourceModel.ModuleDefn newModuleDefn = (SourceModel.ModuleDefn)moduleDefn.accept(new SourceModelCopier<Void>(), null);
moduleDefn = newModuleDefn;
moduleDefn.accept(new SourceModelTraverser<Void, Void>(), null);
}
if (DEBUG_SOURCE_MODEL_TEXT || inUnitTestDebugSourceModelMode) {
// ...then create module text from the source model. It should work when parsed again
// i.e. the source model should represent the same CAL as the original CAL text.
ModuleSourceDefinition sourceBuilderModuleSource = new SourceModelModuleSource(moduleDefn);
Reader sourceBuilderModuleReader = sourceBuilderModuleSource.getSourceReader(new Status("Read module status."));
if (sourceBuilderModuleReader == null) {
logMessage(new CompilerMessage(new SourceRange(moduleSourceName), new MessageKind.Error.CouldNotReadModuleSource(moduleSourceName)));
return null;
}
sourceBuilderModuleReader = new BufferedReader(sourceBuilderModuleReader);