1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Parsing of literal values in the abstract syntax tree.
24 # Visit the module to compute the real value of the literal-related node of the AST.
25 # Warnings and errors are displayed on the toolcontext.
26 fun do_literal
(toolcontext
: ToolContext)
28 var v
= new LiteralVisitor(toolcontext
)
33 private class LiteralVisitor
36 var toolcontext
: ToolContext
38 init(toolcontext
: ToolContext)
40 self.toolcontext
= toolcontext
46 n
.accept_literal
(self)
53 private fun accept_literal
(v
: LiteralVisitor) do end
57 # The value of the literal int once computed.
58 var value
: nullable Int
59 redef fun accept_literal
(v
)
61 self.value
= self.n_number
.text
.to_i
65 redef class AFloatExpr
66 # The value of the literal float once computed.
67 var value
: nullable Float
68 redef fun accept_literal
(v
)
70 self.value
= self.n_float
.text
.to_f
75 # The value of the literal char once computed.
76 var value
: nullable Char
77 redef fun accept_literal
(v
)
79 var txt
= self.n_char
.text
.unescape_nit
80 if txt
.length
!= 3 then
81 v
.toolcontext
.error
(self.hot_location
, "Invalid character literal {txt}")
88 redef class AStringFormExpr
89 # The value of the literal string once computed.
90 var value
: nullable String
91 redef fun accept_literal
(v
)
94 if self isa AStringExpr then
95 txt
= self.n_string
.text
96 else if self isa AStartStringExpr then
97 txt
= self.n_string
.text
98 else if self isa AMidStringExpr then
99 txt
= self.n_string
.text
100 else if self isa AEndStringExpr then
101 txt
= self.n_string
.text
103 self.value
= txt
.substring
(1, txt
.length-2
).unescape_nit
108 # Return a string where Nit escape sequences are transformed.
112 # print s.length # -> 2
113 # var u = s.unescape_nit
114 # print s.length # -> 1
115 # print s[0].ascii # -> 10 (the ASCII value of the "new line" character)
116 fun unescape_nit
: String
118 var res
= new Buffer.with_capacity
(self.length
)
119 var was_slash
= false
121 if not was_slash
then
132 else if c
== 'r' then
134 else if c
== 't' then
136 else if c
== '0' then