From 03240424a62b4fe1aee00f47a8e8c423e828e9a9 Mon Sep 17 00:00:00 2001 From: Florian Deljarry Date: Wed, 27 Feb 2019 11:31:02 -0500 Subject: [PATCH] Moving the astvalidation module in astbuilder Signed-off-by: Florian Deljarry --- src/astbuilder.nit | 77 ++++++++++++++++++++++++ src/astvalidation.nit | 100 -------------------------------- src/frontend/explain_assert.nit | 1 - src/frontend/parallelization_phase.nit | 1 - src/transform.nit | 1 - 5 files changed, 77 insertions(+), 103 deletions(-) delete mode 100644 src/astvalidation.nit diff --git a/src/astbuilder.nit b/src/astbuilder.nit index 79d0ba1..6340ad2 100644 --- a/src/astbuilder.nit +++ b/src/astbuilder.nit @@ -159,6 +159,14 @@ redef class AExpr print "add not implemented in {inspect}" abort end + + redef fun accept_ast_validation(v) + do + super + if mtype == null and not is_typed then + #debug "TYPING: untyped expression" + end + end end # A placeholder for a `AExpr` node @@ -173,6 +181,12 @@ class APlaceholderExpr private init make do end + + redef fun accept_ast_validation(v) + do + super + debug "PARENT: remaining placeholder" + end end redef class ABlockExpr @@ -354,3 +368,66 @@ redef class AVarAssignExpr end end +# Check the consitency of AST +class ASTValidationVisitor + super Visitor + redef fun visit(node) + do + node.accept_ast_validation(self) + end + private var path = new CircularArray[ANode] + private var seen = new HashSet[ANode] +end + +redef class ANode + # Recursively validate a AST node. + # This ensure that location and parenting are defined and coherent. + # + # After complex low-level AST manipulation and construction, + # it is recommended to call it. + # + # Note: this just instantiate and run an `ASTValidationVisitor`. + fun validate + do + (new ASTValidationVisitor).enter_visit(self) + end + + private fun accept_ast_validation(v: ASTValidationVisitor) + do + var parent = self.parent + var path = v.path + + if path.length > 0 then + var path_parent = v.path.first + if parent == null then + self.parent = path_parent + #debug "PARENT: expected parent: {path_parent}" + v.seen.add(self) + else if parent != path_parent then + self.parent = path_parent + if v.seen.has(self) then + debug "DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}." + else + v.seen.add(self) + debug "PARENT: expected parent: {path_parent}, got {parent}" + end + end + end + + if not isset _location then + #debug "LOCATION: unlocated node {v.path.join(", ")}" + _location = self.parent.location + end + + path.unshift(self) + visit_all(v) + path.shift + end +end + +redef class AAnnotation + redef fun accept_ast_validation(v) + do + # Do not enter in annotations + end +end diff --git a/src/astvalidation.nit b/src/astvalidation.nit deleted file mode 100644 index cf3da76..0000000 --- a/src/astvalidation.nit +++ /dev/null @@ -1,100 +0,0 @@ -# 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 the consitency of AST -module astvalidation - -intrude import parser -import astbuilder - -class ASTValidationVisitor - super Visitor - redef fun visit(node) - do - node.accept_ast_validation(self) - end - private var path = new CircularArray[ANode] - private var seen = new HashSet[ANode] -end - -redef class ANode - # Recursively validate a AST node. - # This ensure that location and parenting are defined and coherent. - # - # After complex low-level AST manipulation and construction, - # it is recommended to call it. - # - # Note: this just instantiate and run an `ASTValidationVisitor`. - fun validate - do - (new ASTValidationVisitor).enter_visit(self) - end - - private fun accept_ast_validation(v: ASTValidationVisitor) - do - var parent = self.parent - var path = v.path - - if path.length > 0 then - var path_parent = v.path.first - if parent == null then - self.parent = path_parent - #debug "PARENT: expected parent: {path_parent}" - v.seen.add(self) - else if parent != path_parent then - self.parent = path_parent - if v.seen.has(self) then - debug "DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}." - else - v.seen.add(self) - debug "PARENT: expected parent: {path_parent}, got {parent}" - end - end - end - - if not isset _location then - #debug "LOCATION: unlocated node {v.path.join(", ")}" - _location = self.parent.location - end - - path.unshift(self) - visit_all(v) - path.shift - end -end - -redef class AAnnotation - redef fun accept_ast_validation(v) - do - # Do not enter in annotations - end -end - -redef class AExpr - redef fun accept_ast_validation(v) - do - super - if mtype == null and not is_typed then - #debug "TYPING: untyped expression" - end - end -end - -redef class APlaceholderExpr - redef fun accept_ast_validation(v) - do - super - debug "PARENT: remaining placeholder" - end -end diff --git a/src/frontend/explain_assert.nit b/src/frontend/explain_assert.nit index 326f588..f8b4710 100644 --- a/src/frontend/explain_assert.nit +++ b/src/frontend/explain_assert.nit @@ -35,7 +35,6 @@ module explain_assert import astbuilder intrude import literal # for value= intrude import typing # for mtype= -import astvalidation import explain_assert_api diff --git a/src/frontend/parallelization_phase.nit b/src/frontend/parallelization_phase.nit index 080efe6..b5cc9fd 100644 --- a/src/frontend/parallelization_phase.nit +++ b/src/frontend/parallelization_phase.nit @@ -22,7 +22,6 @@ private import parser_util import modelize import astbuilder private import annotation -private import astvalidation redef class ToolContext # Transforms a function annotated with "threaded" diff --git a/src/transform.nit b/src/transform.nit index 3e5865b..3499d9b 100644 --- a/src/transform.nit +++ b/src/transform.nit @@ -17,7 +17,6 @@ module transform import astbuilder -import astvalidation import semantize intrude import semantize::scope intrude import semantize::typing -- 1.7.9.5