lib/standard/text: Move and generalize remove_underscores to Text
[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 # Parses literal values in the whole AST and produces errors if needed
24 var literal_phase: Phase = new LiteralPhase(self, null)
25 end
26
27 private class LiteralPhase
28 super Phase
29
30 redef fun process_nmodule(nmodule) do nmodule.do_literal(toolcontext)
31 end
32
33 redef class AModule
34 # Visit the module to compute the real value of the literal-related node of the AST.
35 # Warnings and errors are displayed on the toolcontext.
36 fun do_literal(toolcontext: ToolContext)
37 do
38 var v = new LiteralVisitor(toolcontext)
39 v.enter_visit(self)
40 end
41 end
42
43 private class LiteralVisitor
44 super Visitor
45
46 var toolcontext: ToolContext
47
48 redef fun visit(n)
49 do
50 n.accept_literal(self)
51 n.visit_all(self)
52 end
53 end
54
55 redef class ANode
56 private fun accept_literal(v: LiteralVisitor) do end
57 end
58
59 redef class AExpr
60 # Get `self` as a `String`.
61 # Return null if not a string.
62 fun as_string: nullable String
63 do
64 if not self isa AStringFormExpr then return null
65 return self.value.as(not null)
66 end
67
68 # Get `self` as an `Int`.
69 # Return null if not an integer.
70 fun as_int: nullable Int
71 do
72 if not self isa AIntExpr then return null
73 return self.value.as(not null)
74 end
75 end
76
77 redef class AIntExpr
78 # The value of the literal int once computed.
79 var value: nullable Int
80 end
81
82 redef class ADecIntExpr
83 redef fun accept_literal(v)
84 do
85 value = self.n_number.text.remove_underscores.to_i
86 end
87 end
88
89 redef class AHexIntExpr
90 redef fun accept_literal(v)
91 do
92 var s = self.n_hex_number.text.substring_from(2).remove_underscores
93 if s.is_empty then
94 v.toolcontext.error(location, "Error: invalid hexadecimal literal")
95 return
96 end
97 value = s.to_hex
98 end
99 end
100
101 redef class ABinIntExpr
102 redef fun accept_literal(v)
103 do
104 var s = self.n_bin_number.text.substring_from(2).remove_underscores
105 if s.is_empty then
106 v.toolcontext.error(location, "Error: invalid binary literal")
107 return
108 end
109 value = s.to_bin
110 end
111 end
112
113 redef class AOctIntExpr
114 redef fun accept_literal(v)
115 do
116 var s = self.n_oct_number.text.substring_from(2).remove_underscores
117 if s.is_empty then
118 v.toolcontext.error(location, "Error: invalid octal literal")
119 return
120 end
121 value = s.to_oct
122 end
123 end
124
125 redef class AByteExpr
126 # The value of the literal int once computed.
127 var value: nullable Byte
128 end
129
130 redef class ADecByteExpr
131 redef fun accept_literal(v)
132 do
133 var t = self.n_bytenum.text
134 value = t.substring(0, t.length - 2).remove_underscores.to_i.to_b
135 end
136 end
137
138 redef class AHexByteExpr
139 redef fun accept_literal(v)
140 do
141 var t = self.n_hex_bytenum.text
142 var s = t.substring(2, t.length - 4).remove_underscores
143 if s.is_empty then
144 v.toolcontext.error(location, "Error: invalid hexadecimal literal")
145 return
146 end
147 value = s.to_hex.to_b
148 end
149 end
150
151 redef class ABinByteExpr
152 redef fun accept_literal(v)
153 do
154 var t = self.n_bin_bytenum.text
155 var s = t.substring(2, t.length - 4).remove_underscores
156 if s.is_empty then
157 v.toolcontext.error(location, "Error: invalid binary literal")
158 return
159 end
160 value = s.to_bin.to_b
161 end
162 end
163
164 redef class AOctByteExpr
165 redef fun accept_literal(v)
166 do
167 var t = self.n_oct_bytenum.text
168 var s = t.substring(2, t.length - 4).remove_underscores
169 if s.is_empty then
170 v.toolcontext.error(location, "Error: invalid octal literal")
171 return
172 end
173 value = s.to_oct.to_b
174 end
175 end
176
177 redef class AFloatExpr
178 # The value of the literal float once computed.
179 var value: nullable Float
180 redef fun accept_literal(v)
181 do
182 self.value = self.n_float.text.to_f
183 end
184 end
185
186 redef class ACharExpr
187 # The value of the literal char once computed.
188 var value: nullable Char
189 redef fun accept_literal(v)
190 do
191 var txt = self.n_char.text.unescape_nit
192 if txt.length != 3 then
193 v.toolcontext.error(self.hot_location, "Syntax Error: invalid character literal `{txt}`.")
194 return
195 end
196 self.value = txt.chars[1]
197 end
198 end
199
200 redef class AStringFormExpr
201 # The value of the literal string once computed.
202 var value: nullable String
203 redef fun accept_literal(v)
204 do
205 var txt = self.n_string.text
206 var behead = 1
207 var betail = 1
208 if txt.chars[0] == txt.chars[1] and txt.length >= 6 then
209 behead = 3
210 betail = 3
211 if txt.chars[0] == '"' and txt.chars[3] == '\n' then behead = 4 # ignore first \n in """
212 end
213 self.value = txt.substring(behead, txt.length - behead - betail).unescape_nit
214 end
215 end