From: Alexis Laferrière Date: Thu, 18 Jun 2015 14:25:53 +0000 (-0400) Subject: src/frontend: add a phase to validate regular expression X-Git-Tag: v0.7.6~22^2~1 X-Git-Url: http://nitlanguage.org src/frontend: add a phase to validate regular expression Signed-off-by: Alexis Laferrière --- diff --git a/src/frontend/frontend.nit b/src/frontend/frontend.nit index 68ea75b..42cbccd 100644 --- a/src/frontend/frontend.nit +++ b/src/frontend/frontend.nit @@ -27,6 +27,7 @@ import check_annotation import glsl_validation import parallelization_phase import i18n_phase +import regex_phase redef class ToolContext # FIXME: there is conflict in linex in nitc, so use this trick to force invocation diff --git a/src/frontend/regex_phase.nit b/src/frontend/regex_phase.nit new file mode 100644 index 0000000..feafa77 --- /dev/null +++ b/src/frontend/regex_phase.nit @@ -0,0 +1,74 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Check for error in regular expressions from string literals +module regex_phase + +private import parser_util +private import literal +import modelize + +redef class ToolContext + + # Phase checking for errors in regex + var regex_phase: Phase = new RegexPhase(self, [literal_phase]) +end + +private class RegexPhase + super Phase + + redef fun process_nmodule(nmodule) + do + var visitor = new RegexVisitor(toolcontext) + nmodule.accept_regex_visitor visitor + end +end + +private class RegexVisitor + super Visitor + + var toolcontext: ToolContext + + redef fun visit(n) do n.accept_regex_visitor(self) +end + +redef class ANode + private fun accept_regex_visitor(v: RegexVisitor) do visit_all v +end + +redef class ACallExpr + + redef fun accept_regex_visitor(v) + do + var method = n_id + var receiver = n_expr + + # Check only string literals on which `to_re` is called + if method.text == "to_re" and receiver isa AStringFormExpr then + + # Retrieve regex source + var re_src = receiver.value + if re_src == null then return + + # Check for errors by compiling it right now + var re = re_src.to_re + var error = re.compile + re.finalize + + if error != null then + v.toolcontext.error(self.location, "Regex Error: " + error.to_s) + end + end + end +end