1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Check for error in regular expressions from string literals
18 private import parser_util
19 private import literal
22 redef class ToolContext
24 # Phase checking for errors in regex
25 var regex_phase
: Phase = new RegexPhase(self, [literal_phase
])
28 private class RegexPhase
31 redef fun process_nmodule
(nmodule
)
33 var visitor
= new RegexVisitor(toolcontext
)
34 nmodule
.accept_regex_visitor visitor
38 private class RegexVisitor
41 var toolcontext
: ToolContext
43 redef fun visit
(n
) do n
.accept_regex_visitor
(self)
47 private fun accept_regex_visitor
(v
: RegexVisitor) do visit_all v
52 redef fun accept_regex_visitor
(v
)
54 var method
= n_qid
.n_id
57 # Check only string literals on which `to_re` is called
58 if method
.text
== "to_re" and receiver
isa AStringFormExpr then
60 # Retrieve regex source
61 var re_src
= receiver
.value
62 if re_src
== null then return
64 # Check for errors by compiling it right now
66 var error
= re
.compile
70 v
.toolcontext
.error
(self.location
, "Regex Error: " + error
.to_s
)