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 the consitency of AST
21 class ASTValidationVisitor
25 node
.accept_ast_validation
(self)
27 private var path
= new CircularArray[ANode]
28 private var seen
= new HashSet[ANode]
32 # Recursively validate a AST node.
33 # This ensure that location and parenting are defined and coherent.
35 # After complex low-level AST manipulation and construction,
36 # it is recommended to call it.
38 # Note: this just instantiate and run an `ASTValidationVisitor`.
41 (new ASTValidationVisitor).enter_visit
(self)
44 private fun accept_ast_validation
(v
: ASTValidationVisitor)
46 var parent
= self.parent
49 if path
.length
> 0 then
50 var path_parent
= v
.path
.first
51 if parent
== null then
52 self.parent
= path_parent
53 #debug "PARENT: expected parent: {path_parent}"
55 else if parent
!= path_parent
then
56 self.parent
= path_parent
57 if v
.seen
.has
(self) then
58 debug
"DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}."
61 debug
"PARENT: expected parent: {path_parent}, got {parent}"
66 if not isset _location
then
67 #debug "LOCATION: unlocated node {v.path.join(", ")}"
68 _location
= self.parent
.location
77 redef class AAnnotation
78 redef fun accept_ast_validation
(v
)
80 # Do not enter in annotations
85 redef fun accept_ast_validation
(v
)
88 if mtype
== null and not is_typed
then
89 #debug "TYPING: untyped expression"
94 redef class APlaceholderExpr
95 redef fun accept_ast_validation
(v
)
98 debug
"PARENT: remaining placeholder"