From: Jean Privat Date: Fri, 9 Aug 2013 13:41:13 +0000 (-0400) Subject: parser: move things from parser_prod to parser_nodes X-Git-Tag: v0.6.1~54 X-Git-Url: http://nitlanguage.org parser: move things from parser_prod to parser_nodes Signed-off-by: Jean Privat --- diff --git a/src/parser/parser_abs.nit b/src/parser/parser_abs.nit index 1aee132..7241e0f 100644 --- a/src/parser/parser_abs.nit +++ b/src/parser/parser_abs.nit @@ -4,25 +4,6 @@ package parser_abs import location -# Root of the AST hierarchy -abstract class ANode - 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 - super ANode -end - -# Ancestor of all productions -abstract class Prod - super ANode - fun location=(loc: Location) do _location = loc -end class TEol super Token end diff --git a/src/parser/parser_nodes.nit b/src/parser/parser_nodes.nit index 3ecaac2..e6c2b80 100644 --- a/src/parser/parser_nodes.nit +++ b/src/parser/parser_nodes.nit @@ -36,6 +36,30 @@ abstract class ANode do print "{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}" end + + # Parent of the node in the AST + readable writable var _parent: nullable ANode + + # Remove a child from the AST + fun remove_child(child: ANode) + do + replace_child(child, null) + end + + # Replace a child with an other node in the AST + fun replace_child(old_child: ANode, new_child: nullable ANode) is abstract + + # Replace itself with an other node in the AST + fun replace_with(node: ANode) + do + if _parent != null then + _parent.replace_child(self, node) + end + end + + # Visit all nodes in order. + # Thus, call "v.visit(e)" for each node e + fun visit_all(v: Visitor) is abstract end # Ancestor of all tokens @@ -47,6 +71,9 @@ abstract class Token redef fun to_s: String do return "'{text}'" end + + redef fun visit_all(v: Visitor) do end + redef fun replace_child(old_child: ANode, new_child: nullable ANode) do end end # Ancestor of all productions @@ -54,7 +81,36 @@ abstract class Prod super ANode fun location=(l: Location) do _location = l readable var _n_annotations: nullable AAnnotations = null + + redef fun replace_with(n: ANode) + do + super + assert n isa Prod + n.location = location + end end + +# Abstract standard visitor +abstract class Visitor + # What the visitor do when a node is visited + # Concrete visitors should redefine this method. + protected fun visit(e: nullable ANode) is abstract + + # Ask the visitor to visit a given node. + # Usually automatically called by visit_all* methods. + # This methos should not be redefined + fun enter_visit(e: nullable ANode) + do + var old = _current_node + _current_node = e + visit(e) + _current_node = old + end + + # The current visited node + readable var _current_node: nullable ANode = null +end + class TEol super Token redef fun to_s diff --git a/src/parser/parser_prod.nit b/src/parser/parser_prod.nit index 1abc515..e43d64e 100644 --- a/src/parser/parser_prod.nit +++ b/src/parser/parser_prod.nit @@ -6,67 +6,6 @@ import lexer intrude import parser_nodes private import tables -redef class ANode - # Parent of the node in the AST - readable writable var _parent: nullable ANode - - # Remove a child from the AST - fun remove_child(child: ANode) - do - replace_child(child, null) - end - - # Replace a child with an other node in the AST - fun replace_child(old_child: ANode, new_child: nullable ANode) is abstract - - # Replace itself with an other node in the AST - fun replace_with(node: ANode) - do - if _parent != null then - _parent.replace_child(self, node) - end - end - - # Visit all nodes in order. - # Thus, call "v.visit(e)" for each node e - fun visit_all(v: Visitor) is abstract -end - -redef class Token - redef fun visit_all(v: Visitor) do end - redef fun replace_child(old_child: ANode, new_child: nullable ANode) do end -end - -redef class Prod - redef fun replace_with(n: ANode) - do - super - assert n isa Prod - n.location = location - end -end - -# Abstract standard visitor -abstract class Visitor - # What the visitor do when a node is visited - # Concrete visitors should redefine this method. - protected fun visit(e: nullable ANode) is abstract - - # Ask the visitor to visit a given node. - # Usually automatically called by visit_all* methods. - # This methos should not be redefined - fun enter_visit(e: nullable ANode) - do - var old = _current_node - _current_node = e - visit(e) - _current_node = old - end - - # The current visited node - readable var _current_node: nullable ANode = null -end - redef class AModule private init empty_init do end diff --git a/src/parser/xss/main.xss b/src/parser/xss/main.xss index d6bd9e8..ccf7c12 100644 --- a/src/parser/xss/main.xss +++ b/src/parser/xss/main.xss @@ -15,7 +15,6 @@ $ // 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. -$ include 'nodes.xss' $ include 'lexer.xss' $ include 'parser.xss' $ include 'tokens.xss' @@ -28,7 +27,6 @@ package parser_abs import location -$ call make_abs_nodes() $ call make_abs_tokens() $ call make_abs_prods() $ end output @@ -61,7 +59,6 @@ intrude import parser_abs $ end private import tables -$ call make_nodes() $ call make_prods() $ end output diff --git a/src/parser/xss/nodes.xss b/src/parser/xss/nodes.xss deleted file mode 100644 index a07d4ef..0000000 --- a/src/parser/xss/nodes.xss +++ /dev/null @@ -1,102 +0,0 @@ -$ // This file is part of NIT ( http://www.nitlanguage.org ). -$ // -$ // Copyright 2008 Jean Privat -$ // 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 - super PNode -end - -# Ancestor of all productions -abstract class Prod - 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 var _parent: nullable PNode - - # Remove a child from the AST - fun remove_child(child: PNode) - do - replace_child(child, null) - end - - # Replace a child with an other node in the AST - fun replace_child(old_child: PNode, new_child: nullable PNode) is abstract - - # Replace itself with an other node in the AST - fun replace_with(node: PNode) - do - if _parent != null then - _parent.replace_child(self, node) - end - end - - # Visit all nodes in order. - # Thus, call "v.visit(e)" for each node e - fun visit_all(v: Visitor) is abstract -end - -redef class Token - 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 - redef fun replace_with(n: PNode) - do - super - assert n isa Prod - n.location = location - end -end - -# Abstract standard visitor -abstract 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. - # 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