update NOTICE
[nit.git] / src / astvalidation.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 the consitency of AST
16 module astvalidation
17
18 intrude import parser
19 import astbuilder
20
21 class ASTValidationVisitor
22 super Visitor
23 redef fun visit(node)
24 do
25 path.unshift(node)
26 node.accept_ast_validation(self)
27 path.shift
28 end
29 private var path = new List[ANode]
30 private var seen = new HashSet[ANode]
31 end
32
33 redef class ANode
34 private fun accept_ast_validation(v: ASTValidationVisitor)
35 do
36 var parent = self.parent
37
38 if v.path.length > 1 then
39 var path_parent = v.path[1]
40 if parent == null then
41 self.parent = path_parent
42 #debug "PARENT: expected parent: {path_parent}"
43 else if parent != path_parent then
44 self.parent = path_parent
45 debug "PARENT: expected parent: {path_parent}, got {parent}"
46 end
47 end
48
49 if v.seen.has(self) then
50 debug "DUPLICATE: already seen node. NOTATREE"
51 end
52 v.seen.add(self)
53
54 if not isset _location then
55 #debug "LOCATION: unlocated node {v.path.join(", ")}"
56 _location = self.parent.location
57 end
58
59 visit_all(v)
60 end
61 end
62
63 redef class AAnnotations
64 redef fun accept_ast_validation(v)
65 do
66 # Do not enter in annotations
67 end
68 end
69
70 redef class AExpr
71 redef fun accept_ast_validation(v)
72 do
73 super
74 if mtype == null and not is_typed then
75 debug "TYPING: untyped expression"
76 end
77 end
78 end
79
80 redef class APlaceholderExpr
81 redef fun accept_ast_validation(v)
82 do
83 super
84 debug "PARENT: remaining placeholder"
85 end
86 end