nullable: convert lib, tools and tests
[nit.git] / src / parser / xss / nodes.xss
index 3d59406..8b0d044 100644 (file)
@@ -35,7 +35,7 @@ $ end template
 $ template make_nodes()
 redef class PNode
        # Parent of the node in the AST
-       readable writable attr _parent: PNode 
+       readable writable attr _parent: nullable PNode
 
        # Remove a child from the AST
        meth remove_child(child: PNode)
@@ -44,7 +44,7 @@ redef class PNode
        end
 
        # Replace a child with an other node in the AST
-       meth replace_child(old_child: PNode, new_child: PNode) is abstract
+       meth replace_child(old_child: PNode, new_child: nullable PNode) is abstract
 
        # Replace itself with an other node in the AST
        meth replace_with(node: PNode)
@@ -65,6 +65,9 @@ redef class PNode
        # Give a human readable location of the node.
        meth locate: String is abstract
 
+       # Return only the line number of the node
+       meth line_number: Int is abstract
+
        # Debug method: output a message prefixed with the location.
        meth printl(str: String)
        do
@@ -75,26 +78,31 @@ end
 redef class Token
        redef meth visit_all(v: Visitor) do end
        redef meth visit_all_reverse(v: Visitor) do end
-       redef meth replace_child(old_child: PNode, new_child: PNode) do end
+       redef meth replace_child(old_child: PNode, new_child: nullable PNode) do end
 
        redef meth locate: String
        do
                return "{filename}:{line},{pos}"
        end
+
+       redef meth line_number do return line
 end
 
 redef class Prod
        # The first token of the production node
-       readable writable attr _first_token: Token 
+       readable writable attr _first_token: nullable Token
 
        # The last token of the production node
-       readable writable attr _last_token: Token 
+       readable writable attr _last_token: nullable Token
 
        redef meth locate: String
        do
                if first_token == null then
                        return "????"
                end
+               if last_token == null then
+                       return "{first_token.locate}--????"
+               end
                var lastpos = last_token.pos + last_token.text.length - 1
                if first_token.line == last_token.line then
                        return "{first_token.locate}--{lastpos}"
@@ -104,11 +112,20 @@ redef class Prod
        end
 
        redef meth replace_with(n: PNode)
+        do
+                super
+                assert n isa Prod
+                n.first_token = first_token
+                n.last_token = last_token
+        end
+
+       redef meth line_number
        do
-               super
-               assert n isa Prod
-               n.first_token = first_token
-               n.last_token = last_token
+               if first_token != null then
+                       return first_token.line
+               else
+                       return 0
+               end
        end
 end
 
@@ -117,7 +134,7 @@ class Visitor
         # Ask the visitor to visit a given node.
         # Usually automatically called by visit_all* methods.
         # Concrete visitors should redefine this method.
-        meth visit(e: PNode) is abstract
+        meth visit(e: nullable PNode) is abstract
 end
 
 $ end template