955e24a8a7fb4b26df536775e08dfe48e4e64986
[nit.git] / src / frontend / deriving.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 # Injection of automatic method definitions for standard methods, based on the attributes of the classes
16 #
17 # This phase is only a proof of concept and is inherently fragile:
18 #
19 # * syntactic code injection without semantic checking
20 # * ignorance of name conflicts
21 # * attributes are syntactically and locally collected
22 module deriving
23
24 private import parser_util
25 import modelize
26 private import annotation
27
28 redef class ToolContext
29 # Main phase of `deriving`
30 var deriving_phase: Phase = new DerivingPhase(self, null)
31 end
32
33 private class DerivingPhase
34 super Phase
35
36 redef fun process_annotated_node(nclassdef, nat)
37 do
38 if nat.name == "auto_inspect" then
39 if not nclassdef isa AStdClassdef then
40 toolcontext.error(nclassdef.location, "Syntax error: only a concrete class can be `{nat.name}`.")
41 else
42 generate_inspect_method(nclassdef)
43 end
44 end
45
46 if nat.name == "auto_derive" then
47 if not nclassdef isa AStdClassdef then
48 toolcontext.error(nclassdef.location, "Syntax error: only a concrete class can be `{nat.name}`.")
49 else
50 generate_derive_to_map_method(nclassdef, nat)
51 end
52 end
53 end
54
55 fun generate_inspect_method(nclassdef: AClassdef)
56 do
57 var npropdefs = nclassdef.n_propdefs
58
59 var code = new Array[String]
60 code.add "redef fun inspect"
61 code.add "do"
62 code.add " var res = super"
63 code.add " res = res.substring(0,res.length-1)"
64
65 for attribute in npropdefs do if attribute isa AAttrPropdef then
66 var name = attribute.n_id2.text
67 code.add """ res += " {{{name}}}: {self.{{{name}}}.inspect}""""
68 end
69
70 code.add " res += \">\""
71 code.add " return res"
72 code.add "end"
73
74 # Create method Node and add it to the AST
75 npropdefs.push(toolcontext.parse_propdef(code.join("\n")))
76 end
77
78 fun generate_derive_to_map_method(nclassdef: AClassdef, nat: AAnnotation)
79 do
80 var npropdefs = nclassdef.n_propdefs
81
82 var sc = toolcontext.parse_superclass("Derivable")
83 sc.location = nat.location
84 nclassdef.n_propdefs.add sc
85
86 var code = new Array[String]
87 code.add "redef fun derive_to_map"
88 code.add "do"
89 code.add " var res = super"
90
91 for attribute in npropdefs do if attribute isa AAttrPropdef then
92 var name = attribute.n_id2.text
93 code.add """ res["{{{name}}}"] = self.{{{name}}}"""
94 end
95
96 code.add " return res"
97 code.add "end"
98
99 # Create method Node and add it to the AST
100 npropdefs.push(toolcontext.parse_propdef(code.join("\n")))
101 end
102 end