e04a4f48dd238ad3790f58c3acc2abe6f3bedbab
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
)
47 fun generate_inspect_method
(nclassdef
: AClassdef)
49 var npropdefs
= nclassdef
.n_propdefs
51 var code
= new Array[String]
52 code
.add
"redef fun inspect"
54 code
.add
" var res = super"
55 code
.add
" res = res.substring(0,res.length-1)"
57 for attribute
in npropdefs
do if attribute
isa AAttrPropdef then
58 var name
= attribute
.n_id2
.text
59 code
.add
""" res += " {{{name}}}: {self.{{{name}}}.inspect}""""
62 code.add " res
+= \
">\""
63 code.add " return res
"
66 # Create method Node and add it to the AST
67 npropdefs.push(toolcontext.parse_propdef(code.join("\n
")))