nitc :: ModelBuilder :: load_module_ast
Display an error if there is a problem (IO / lexer / parser) and return null
The AST is loaded as is total independence of the model and its entities.
AST are not cached or reused thus a new AST is returned on success.
# Try to load a module AST using a path.
# Display an error if there is a problem (IO / lexer / parser) and return null
#
# The AST is loaded as is total independence of the model and its entities.
#
# AST are not cached or reused thus a new AST is returned on success.
fun load_module_ast(filename: String): nullable AModule
do
if not filename.has_suffix(".nit") then
self.toolcontext.error(null, "Error: file `{filename}` is not a valid nit module.")
return null
end
if not filename.file_exists then
self.toolcontext.error(null, "Error: file `{filename}` not found.")
return null
end
self.toolcontext.info("load module {filename}", 2)
# Load the file
var file = new FileReader.open(filename)
var lexer = new Lexer(new SourceFile(filename, file))
var parser = new Parser(lexer)
var tree = parser.parse
file.close
# Handle lexer and parser error
var nmodule = tree.n_base
if nmodule == null then
var neof = tree.n_eof
assert neof isa AError
error(neof, neof.message)
return null
end
return nmodule
end
src/loader.nit:673,2--709,4