frontend: force order of serialzation and literal phases in frontend
[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 parser
21 import toolcontext
22 import phase
23
24 redef class ToolContext
25 var literal_phase: Phase = new LiteralPhase(self, null)
26 end
27
28 private class LiteralPhase
29 super Phase
30
31 redef fun process_nmodule(nmodule) do nmodule.do_literal(toolcontext)
32 end
33
34 redef class AModule
35 # Visit the module to compute the real value of the literal-related node of the AST.
36 # Warnings and errors are displayed on the toolcontext.
37 fun do_literal(toolcontext: ToolContext)
38 do
39 var v = new LiteralVisitor(toolcontext)
40 v.enter_visit(self)
41 end
42 end
43
44 private class LiteralVisitor
45 super Visitor
46
47 var toolcontext: ToolContext
48
49 init(toolcontext: ToolContext)
50 do
51 self.toolcontext = toolcontext
52 end
53
54 redef fun visit(n)
55 do
56 n.accept_literal(self)
57 n.visit_all(self)
58 end
59 end
60
61 redef class ANode
62 private fun accept_literal(v: LiteralVisitor) do end
63 end
64
65 redef class AIntExpr
66 # The value of the literal int once computed.
67 var value: nullable Int
68 redef fun accept_literal(v)
69 do
70 self.value = self.n_number.text.to_i
71 end
72 end
73
74 redef class AFloatExpr
75 # The value of the literal float once computed.
76 var value: nullable Float
77 redef fun accept_literal(v)
78 do
79 self.value = self.n_float.text.to_f
80 end
81 end
82
83 redef class ACharExpr
84 # The value of the literal char once computed.
85 var value: nullable Char
86 redef fun accept_literal(v)
87 do
88 var txt = self.n_char.text.unescape_nit
89 if txt.length != 3 then
90 v.toolcontext.error(self.hot_location, "Invalid character literal {txt}")
91 return
92 end
93 self.value = txt[1]
94 end
95 end
96
97 redef class AStringFormExpr
98 # The value of the literal string once computed.
99 var value: nullable String
100 redef fun accept_literal(v)
101 do
102 var txt = self.n_string.text
103 var skip = 1
104 if txt[0] == txt[1] and txt.length >= 6 then skip = 3
105 self.value = txt.substring(skip, txt.length-(2*skip)).unescape_nit
106 end
107 end