src: finish documenting some module
[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 AIntExpr
60 # The value of the literal int once computed.
61 var value: nullable Int
62 end
63
64 redef class ADecIntExpr
65 redef fun accept_literal(v)
66 do
67 self.value = self.n_number.text.to_i
68 end
69 end
70
71 redef class AHexIntExpr
72 redef fun accept_literal(v)
73 do
74 self.value = self.n_hex_number.text.substring_from(2).to_hex
75 end
76 end
77
78 redef class AFloatExpr
79 # The value of the literal float once computed.
80 var value: nullable Float
81 redef fun accept_literal(v)
82 do
83 self.value = self.n_float.text.to_f
84 end
85 end
86
87 redef class ACharExpr
88 # The value of the literal char once computed.
89 var value: nullable Char
90 redef fun accept_literal(v)
91 do
92 var txt = self.n_char.text.unescape_nit
93 if txt.length != 3 then
94 v.toolcontext.error(self.hot_location, "Invalid character literal {txt}")
95 return
96 end
97 self.value = txt.chars[1]
98 end
99 end
100
101 redef class AStringFormExpr
102 # The value of the literal string once computed.
103 var value: nullable String
104 redef fun accept_literal(v)
105 do
106 var txt = self.n_string.text
107 var behead = 1
108 var betail = 1
109 if txt.chars[0] == txt.chars[1] and txt.length >= 6 then
110 behead = 3
111 betail = 3
112 if txt.chars[0] == '"' and txt.chars[3] == '\n' then behead = 4 # ignore first \n in """
113 end
114 self.value = txt.substring(behead, txt.length - behead - betail).unescape_nit
115 end
116 end