541f752814a50404489650c90508e9137a8f0d0e
[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
81 redef fun accept_literal(v)
82 do
83 if not text.is_int then
84 v.toolcontext.error(hot_location, "Error: invalid literal `{text}`")
85 return
86 end
87 value = text.to_i
88 end
89
90 private fun text: String is abstract
91 end
92
93 redef class ADecIntExpr
94 redef fun text do return self.n_number.text
95 end
96
97 redef class AHexIntExpr
98 redef fun text do return self.n_hex_number.text
99 end
100
101 redef class ABinIntExpr
102 redef fun text do return self.n_bin_number.text
103 end
104
105 redef class AOctIntExpr
106 redef fun text do return self.n_oct_number.text
107 end
108
109 redef class AByteExpr
110 # The value of the literal int once computed.
111 var value: nullable Byte
112
113 redef fun accept_literal(v)
114 do
115 var s = text.substring(0, text.length - 2)
116 if not s.is_int then
117 v.toolcontext.error(hot_location, "Error: invalid byte literalĀ `{text}`")
118 return
119 end
120 value = s.to_i.to_b
121 end
122
123 private fun text: String is abstract
124 end
125
126 redef class ADecByteExpr
127 redef fun text do return self.n_bytenum.text
128 end
129
130 redef class AHexByteExpr
131 redef fun text do return self.n_hex_bytenum.text
132 end
133
134 redef class ABinByteExpr
135 redef fun text do return self.n_bin_bytenum.text
136 end
137
138 redef class AOctByteExpr
139 redef fun text do return self.n_oct_bytenum.text
140 end
141
142 redef class AFloatExpr
143 # The value of the literal float once computed.
144 var value: nullable Float
145 redef fun accept_literal(v)
146 do
147 self.value = self.n_float.text.to_f
148 end
149 end
150
151 redef class ACharExpr
152 # The value of the literal char once computed.
153 var value: nullable Char
154 redef fun accept_literal(v)
155 do
156 var txt = self.n_char.text.unescape_nit
157 if txt.length != 3 then
158 v.toolcontext.error(self.hot_location, "Syntax Error: invalid character literal `{txt}`.")
159 return
160 end
161 self.value = txt.chars[1]
162 end
163 end
164
165 redef class AStringFormExpr
166 # The value of the literal string once computed.
167 var value: nullable String
168 redef fun accept_literal(v)
169 do
170 var txt = self.n_string.text
171 var behead = 1
172 var betail = 1
173 if txt.chars[0] == txt.chars[1] and txt.length >= 6 then
174 behead = 3
175 betail = 3
176 if txt.chars[0] == '"' and txt.chars[3] == '\n' then behead = 4 # ignore first \n in """
177 end
178 self.value = txt.substring(behead, txt.length - behead - betail).unescape_nit
179 end
180 end