nitc/ffi: adds the extern_inline module to manage foreign code blocks
[nit.git] / src / syntax / syntax.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Full syntax analysis of NIT AST.
18 # Detect syntax and some metamodel errors.
19 package syntax
20
21 import mmloader
22 import mmbuilder
23 import typing
24 import icode_generation
25 import extern_inline
26
27 # Loader of nit source files
28 class SrcModuleLoader
29 super ModuleLoader
30 redef type MODULE: MMSrcModule
31
32 redef fun file_type do return "nit"
33
34 redef fun parse_file(context, file, filename, name, dir)
35 do
36 var name_is_valid = name.to_s.length > 0 and name.to_s[0].is_lower
37 for char in name.to_s do if not char.is_digit and not char.is_letter and not char == '_'
38 then
39 name_is_valid = false
40 break
41 end
42 if not name_is_valid then
43 context.error( null, "{filename}: Error module name \"{name}\", must start with a lower case letter and contain only letters, digits and '_'." )
44 end
45
46 var source = new SourceFile(filename, file)
47 var lexer = new Lexer(source)
48 var parser = new Parser(lexer)
49 var node_tree = parser.parse
50 if node_tree.n_base == null then
51 var err = node_tree.n_eof
52 assert err isa AError
53 context.fatal_error(err.location, err.message)
54 end
55 var node_module = node_tree.n_base
56 assert node_module != null
57 var module_loc = new Location.with_file(source)
58 var mod = new MMSrcModule(context, node_module, dir, name, module_loc)
59 return mod
60 end
61
62 redef fun process_metamodel(context, mod)
63 do
64 mod.process_supermodules(context)
65 context.info("Syntax analysis for module: {mod.name}", 2)
66 mod.process_syntax(context)
67 end
68
69 init do end
70 end
71
72 redef class MMSrcModule
73 # Loading and syntax analysis of super modules
74 private fun process_supermodules(tc: ToolContext)
75 do
76 node.import_super_modules(tc, self)
77 end
78
79 # Syntax analysis and MM construction for the module
80 # Require than supermodules are processed
81 private fun process_syntax(tc: ToolContext)
82 do
83 do_mmbuilder(tc)
84 tc.check_errors
85
86 do_typing(tc)
87 tc.check_errors
88
89 generate_icode(tc)
90 tc.check_errors
91
92 if not tc.keep_ast then clear_ast
93 end
94 end
95
96 redef class ToolContext
97 # Should the AST be preserved in source modules after syntax processing?
98 # Default is false.
99 readable writable var _keep_ast: Bool = false
100 end