examples: annotate examples
[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 # Annotations on the module declaration
56 var annotations = new Array[Writable]
57
58 # Importation declarations
59 #
60 # Accepts two formats:
61 # * Module name only, short or qualified: `json`, `gamnit::flat`, etc.
62 # * Full importation declaration: `import json`, `private import gamnit::flat`, etc.
63 var imports = new Set[Writable]
64
65 # Main content of this module
66 var content = new Array[Writable]
67
68 redef fun rendering
69 do
70 var header = header
71 if header != null then add header
72
73 var name = name
74 if annotations.is_empty then
75 add "module {name}\n\n"
76 else
77 add "module {name} is\n"
78 for annotation in annotations do add "\t{annotation}\n"
79 add "end\n\n"
80 end
81
82 for i in imports do
83 if i.to_s.has("import ") then
84 add i
85 else
86 add "import "
87 add i
88 end
89 add "\n"
90 end
91 add "\n"
92
93 for l in content do
94 add l
95 add "\n"
96 end
97 end
98 end