metamodel: rename 'universal' to 'enum'
[nit.git] / src / parser / xss / nodes.xss
index 46cfafd..a62df55 100644 (file)
@@ -1,53 +1,58 @@
-/* This file is part of NIT ( http://www.nitlanguage.org ).
- *
- * Copyright 2008 Jean Privat <jean@pryen.org>
- * Based on algorithms developped for ( http://www.sablecc.org/ ).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+$ // This file is part of NIT ( http://www.nitlanguage.org ).
+$ //
+$ // Copyright 2008 Jean Privat <jean@pryen.org>
+$ // Based on algorithms developped for ( http://www.sablecc.org/ ).
+$ //
+$ // Licensed under the Apache License, Version 2.0 (the "License");
+$ // you may not use this file except in compliance with the License.
+$ // You may obtain a copy of the License at
+$ //
+$ //     http://www.apache.org/licenses/LICENSE-2.0
+$ //
+$ // Unless required by applicable law or agreed to in writing, software
+$ // distributed under the License is distributed on an "AS IS" BASIS,
+$ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+$ // See the License for the specific language governing permissions and
+$ // limitations under the License.
 
 $ template make_abs_nodes()
 # Root of the AST hierarchy
 abstract class PNode
+       var _location: nullable Location
+
+       # Location is set during AST building. Once built, location cannon be null
+       # However, manual instanciated nodes may need mode care
+       fun location: Location do return _location.as(not null)
 end
 
 # Ancestor of all tokens
 abstract class Token
-special PNode
+       super PNode
 end
 
 # Ancestor of all productions
 abstract class Prod
-special PNode
+       super PNode
+       fun location=(loc: Location) do _location = loc
 end
 $ end template
 
 $ template make_nodes()
 redef class PNode
        # Parent of the node in the AST
-       readable writable attr _parent: PNode 
+       readable writable var _parent: nullable PNode
 
        # Remove a child from the AST
-       meth remove_child(child: PNode)
+       fun remove_child(child: PNode)
        do
                replace_child(child, null)
        end
 
        # Replace a child with an other node in the AST
-       meth replace_child(old_child: PNode, new_child: PNode) is abstract
+       fun 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)
+       fun replace_with(node: PNode)
        do
                if (_parent != null) then
                        _parent.replace_child(self, node)
@@ -56,82 +61,42 @@ redef class PNode
 
        # Visit all nodes in order.
        # Thus, call "v.visit(e)" for each node e
-       meth visit_all(v: Visitor) is abstract
-
-       # Visit all nodes in reverse order.
-       # Thus, call "v.visit(e)" for each node e starting from the last child
-       meth visit_all_reverse(v: Visitor) is abstract
-
-       # 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
-               print("{locate}: {str}\n")
-       end
+       fun visit_all(v: Visitor) is abstract
 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 locate: String
-       do
-               return "{filename}:{line},{pos}"
-       end
-
-       redef meth line_number do return line
+       redef fun visit_all(v: Visitor) do end
+       redef fun replace_child(old_child: PNode, new_child: nullable PNode) do end
 end
 
 redef class Prod
-       # The first token of the production node
-       readable writable attr _first_token: Token 
-
-       # The last token of the production node
-       readable writable attr _last_token: Token 
-
-       redef meth locate: String
-       do
-               if first_token == null then
-                       return "????"
-               end
-               var lastpos = last_token.pos + last_token.text.length - 1
-               if first_token.line == last_token.line then
-                       return "{first_token.locate}--{lastpos}"
-               else
-                       return "{first_token.locate}--{last_token.line}:{lastpos}"
-               end
-       end
-
-       redef meth replace_with(n: PNode)
+       redef fun replace_with(n: PNode)
         do
                 super
                 assert n isa Prod
-                n.first_token = first_token
-                n.last_token = last_token
+                n.location = location
         end
-
-       redef meth line_number
-       do
-               if first_token != null then
-                       return first_token.line
-               else
-                       return 0
-               end
-       end
 end
 
 # Abstract standard visitor
 class Visitor
+       # What the visitor do when a node is visited
+        # Concrete visitors should redefine this method.
+        protected fun visit(e: nullable PNode) is abstract
+
         # 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
+       # This methos should not be redefined
+        fun enter_visit(e: nullable PNode)
+       do
+               var old = _current_node
+               _current_node = e
+               visit(e)
+               _current_node = old
+       end
+
+       # The current visited node
+       readable var _current_node: nullable PNode = null
 end
 
 $ end template