Rename REAMDE to README.md
[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 node.accept_ast_validation(self)
26 end
27 private var path = new List[ANode]
28 private var seen = new HashSet[ANode]
29 end
30
31 redef class ANode
32 private fun accept_ast_validation(v: ASTValidationVisitor)
33 do
34 var parent = self.parent
35 var path = v.path
36
37 if path.length > 0 then
38 var path_parent = v.path.first
39 if parent == null then
40 self.parent = path_parent
41 #debug "PARENT: expected parent: {path_parent}"
42 v.seen.add(self)
43 else if parent != path_parent then
44 self.parent = path_parent
45 if v.seen.has(self) then
46 debug "DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}."
47 else
48 v.seen.add(self)
49 debug "PARENT: expected parent: {path_parent}, got {parent}"
50 end
51 end
52 end
53
54 if not isset _location then
55 #debug "LOCATION: unlocated node {v.path.join(", ")}"
56 _location = self.parent.location
57 end
58
59 path.unshift(self)
60 visit_all(v)
61 path.shift
62 end
63 end
64
65 redef class AAnnotation
66 redef fun accept_ast_validation(v)
67 do
68 # Do not enter in annotations
69 end
70 end
71
72 redef class AExpr
73 redef fun accept_ast_validation(v)
74 do
75 super
76 if mtype == null and not is_typed then
77 #debug "TYPING: untyped expression"
78 end
79 end
80 end
81
82 redef class APlaceholderExpr
83 redef fun accept_ast_validation(v)
84 do
85 super
86 debug "PARENT: remaining placeholder"
87 end
88 end