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