src: update AST client to handle qualified identifiers (no semantic change)
[nit.git] / src / frontend / regex_phase.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Check for error in regular expressions from string literals
16 module regex_phase
17
18 private import parser_util
19 private import literal
20 import modelize
21
22 redef class ToolContext
23
24 # Phase checking for errors in regex
25 var regex_phase: Phase = new RegexPhase(self, [literal_phase])
26 end
27
28 private class RegexPhase
29 super Phase
30
31 redef fun process_nmodule(nmodule)
32 do
33 var visitor = new RegexVisitor(toolcontext)
34 nmodule.accept_regex_visitor visitor
35 end
36 end
37
38 private class RegexVisitor
39 super Visitor
40
41 var toolcontext: ToolContext
42
43 redef fun visit(n) do n.accept_regex_visitor(self)
44 end
45
46 redef class ANode
47 private fun accept_regex_visitor(v: RegexVisitor) do visit_all v
48 end
49
50 redef class ACallExpr
51
52 redef fun accept_regex_visitor(v)
53 do
54 var method = n_qid.n_id
55 var receiver = n_expr
56
57 # Check only string literals on which `to_re` is called
58 if method.text == "to_re" and receiver isa AStringFormExpr then
59
60 # Retrieve regex source
61 var re_src = receiver.value
62 if re_src == null then return
63
64 # Check for errors by compiling it right now
65 var re = re_src.to_re
66 var error = re.compile
67 re.finalize
68
69 if error != null then
70 v.toolcontext.error(self.location, "Regex Error: " + error.to_s)
71 end
72 end
73 end
74 end