1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Injection of automatic method definitions for standard methods, based on the attributes of the classes
17 # This phase is only a proof of concept and is inherently fragile:
19 # * syntactic code injection without semantic checking
20 # * ignorance of name conflicts
21 # * attributes are syntactically and locally collected
24 private import parser_util
26 private import annotation
28 redef class ToolContext
29 # Main phase of `deriving`
30 var deriving_phase
: Phase = new DerivingPhase(self, null)
33 private class DerivingPhase
36 redef fun process_annotated_node
(nclassdef
, nat
)
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}`.")
42 generate_inspect_method
(nclassdef
)
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}`.")
50 generate_derive_to_map_method
(nclassdef
, nat
)
55 fun generate_inspect_method
(nclassdef
: AClassdef)
57 var npropdefs
= nclassdef
.n_propdefs
59 var code
= new Array[String]
60 code
.add
"redef fun inspect"
62 code
.add
" var res = super"
63 code
.add
" res = res.substring(0,res.length-1)"
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}""""
70 code.add " res
+= \
">\""
71 code.add " return res
"
74 # Create method Node and add it to the AST
75 npropdefs.push(toolcontext.parse_propdef(code.join("\n
")))
78 fun generate_derive_to_map_method(nclassdef: AClassdef, nat: AAnnotation)
80 var npropdefs = nclassdef.n_propdefs
82 var sc = toolcontext.parse_superclass("Derivable")
83 sc.location = nat.location
84 nclassdef.n_propdefs.add sc
86 var code = new Array[String]
87 code.add "redef fun derive_to_map
"
89 code.add " var res
= super"
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}}}"""
96 code.add " return res
"
99 # Create method Node and add it to the AST
100 npropdefs.push(toolcontext.parse_propdef(code.join("\n
")))