$ template make_abs_nodes()
# Root of the AST hierarchy
abstract class PNode
+ readable var _location: nullable Location
end
# Ancestor of all tokens
# Ancestor of all productions
abstract class Prod
special PNode
+ fun location=(loc: nullable Location) do _location = loc
end
$ end template
# Thus, call "v.visit(e)" for each node e starting from the last child
fun visit_all_reverse(v: Visitor) is abstract
- # Give a human readable location of the node.
- fun locate: String is abstract
-
- # Return only the line number of the node
- fun line_number: Int is abstract
-
# Debug method: output a message prefixed with the location.
fun printl(str: String)
do
- print("{locate}: {str}\n")
+ if location == null then
+ print("???: {str}\n")
+ else
+ print("{location}: {str}\n")
+ end
end
end
redef fun visit_all(v: Visitor) do end
redef fun visit_all_reverse(v: Visitor) do end
redef fun replace_child(old_child: PNode, new_child: nullable PNode) do end
-
- redef fun locate: String
- do
- return "{filename}:{line},{pos}"
- end
-
- redef fun line_number do return line
end
redef class Prod
# The last token of the production node
readable writable var _last_token: nullable Token
- redef fun 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}"
- else
- return "{first_token.locate}--{last_token.line}:{lastpos}"
- end
- end
-
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 fun 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.
- fun visit(e: nullable 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