src: cleanup importations
[nit.git] / src / literal.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Parsing of literal values in the abstract syntax tree.
18 module literal
19
20 import phase
21
22 redef class ToolContext
23 var literal_phase: Phase = new LiteralPhase(self, null)
24 end
25
26 private class LiteralPhase
27 super Phase
28
29 redef fun process_nmodule(nmodule) do nmodule.do_literal(toolcontext)
30 end
31
32 redef class AModule
33 # Visit the module to compute the real value of the literal-related node of the AST.
34 # Warnings and errors are displayed on the toolcontext.
35 fun do_literal(toolcontext: ToolContext)
36 do
37 var v = new LiteralVisitor(toolcontext)
38 v.enter_visit(self)
39 end
40 end
41
42 private class LiteralVisitor
43 super Visitor
44
45 var toolcontext: ToolContext
46
47 init(toolcontext: ToolContext)
48 do
49 self.toolcontext = toolcontext
50 end
51
52 redef fun visit(n)
53 do
54 n.accept_literal(self)
55 n.visit_all(self)
56 end
57 end
58
59 redef class ANode
60 private fun accept_literal(v: LiteralVisitor) do end
61 end
62
63 redef class AIntExpr
64 # The value of the literal int once computed.
65 var value: nullable Int
66 end
67
68 redef class ADecIntExpr
69 redef fun accept_literal(v)
70 do
71 self.value = self.n_number.text.to_i
72 end
73 end
74
75 redef class AHexIntExpr
76 redef fun accept_literal(v)
77 do
78 self.value = self.n_hex_number.text.substring_from(2).to_hex
79 end
80 end
81
82 redef class AFloatExpr
83 # The value of the literal float once computed.
84 var value: nullable Float
85 redef fun accept_literal(v)
86 do
87 self.value = self.n_float.text.to_f
88 end
89 end
90
91 redef class ACharExpr
92 # The value of the literal char once computed.
93 var value: nullable Char
94 redef fun accept_literal(v)
95 do
96 var txt = self.n_char.text.unescape_nit
97 if txt.length != 3 then
98 v.toolcontext.error(self.hot_location, "Invalid character literal {txt}")
99 return
100 end
101 self.value = txt.chars[1]
102 end
103 end
104
105 redef class AStringFormExpr
106 # The value of the literal string once computed.
107 var value: nullable String
108 redef fun accept_literal(v)
109 do
110 var txt = self.n_string.text
111 var behead = 1
112 var betail = 1
113 if txt.chars[0] == txt.chars[1] and txt.length >= 6 then
114 behead = 3
115 betail = 3
116 if txt.chars[0] == '"' and txt.chars[3] == '\n' then behead = 4 # ignore first \n in """
117 end
118 self.value = txt.substring(behead, txt.length - behead - betail).unescape_nit
119 end
120 end