readme: add information section
[nit.git] / lib / gen_nit.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Support to generate and otherwise manipulate Nit code
16 module gen_nit
17
18 import template
19
20 redef class Sys
21 # Reserved keywords in the Nit language
22 var keywords: Set[String] is lazy do return new HashSet[String].from([
23 "abort", "abstract", "and", "assert", "break", "class", "continue",
24 "do", "else", "end", "enum", "extern", "false", "implies", "import",
25 "init", "interface", "intrude", "if", "in", "is", "isa", "isset",
26 "for", "label", "loop", "module", "new", "not", "null", "nullable",
27 "or", "package", "private", "protected", "public", "return", "self",
28 "super", "then", "true", "type", "var", "while"])
29
30 # Top-level methods from the `Object` class
31 #
32 # This is a non-exaustive list that targets conflict-prone names.
33 var methods_in_object: Array[String] is lazy do return [
34 "class_name", "get_time", "hash", "inspect", "inspect_head",
35 "is_same_type", "is_same_instance", "object_id", "output",
36 "output_class_name", "sys", "to_s"]
37
38 # Methods in the `Pointer` class
39 #
40 # This is a non-exaustive list that targets conflict-prone names.
41 var methods_in_pointer: Array[String] is lazy do return methods_in_object + [
42 "free"]
43 end
44
45 # Template of a Nit module to generate Nit code
46 class NitModule
47 super Template
48
49 # Header on top of the module, usually the documentation
50 var header: nullable Writable = null is writable
51
52 # The module's name
53 var name: Writable is writable
54
55 # Imports from this module
56 var imports = new Array[Writable]
57
58 # Main content of this module
59 var content = new Array[Writable]
60
61 redef fun rendering
62 do
63 var header = header
64 if header != null then add header
65
66 var name = name
67 add "module {name}\n\n"
68
69 for i in imports do add "import {i}\n"
70 add "\n"
71
72 for l in content do
73 add l
74 add "\n"
75 end
76 end
77 end