1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Full syntax analysis of NIT AST.
18 # Detect syntax and some metamodel errors.
24 import icode_generation
26 # Loader of nit source files
29 redef type MODULE: MMSrcModule
31 redef fun file_type
do return "nit"
33 redef fun parse_file
(context
, file
, filename
, name
, dir
)
35 var name_is_valid
= name
.to_s
.length
> 0 and name
.to_s
[0].is_lower
36 for char
in name
.to_s
do if not char
.is_digit
and not char
.is_letter
and not char
== '_'
41 if not name_is_valid
then
42 context
.error
( null, "{filename}: Error package name \"{name}\
", must start with a lower case letter and contain only letters, digits and '_'." )
45 var lexer
= new Lexer(file
, filename
)
46 var parser
= new Parser(lexer
)
47 var node_tree
= parser
.parse
48 if node_tree
.n_base
== null then
49 var err
= node_tree
.n_eof
51 context
.fatal_error
(err
.location
, err
.message
)
53 var node_module
= node_tree
.n_base
54 assert node_module
!= null
55 var module_loc
= new Location.with_file
(filename
)
56 var mod
= new MMSrcModule(context
, node_module
, dir
, name
, module_loc
)
60 redef fun process_metamodel
(context
, mod
)
62 mod
.process_supermodules
(context
)
63 context
.info
("Syntax analysis for module: {mod.name}", 2)
64 mod
.process_syntax
(context
)
70 redef class MMSrcModule
71 # Loading and syntax analysis of super modules
72 private fun process_supermodules
(tc
: ToolContext)
74 node
.import_super_modules
(tc
, self)
77 # Syntax analysis and MM construction for the module
78 # Require than supermodules are processed
79 private fun process_syntax
(tc
: ToolContext)
90 if not tc
.keep_ast
then clear_ast
94 redef class ToolContext
95 # Should the AST be preserved in source modules after syntax processing?
97 readable writable var _keep_ast
: Bool = false