<s> """<STR_LIT>""" <EOL> class BaseNode ( object ) : <EOL> """<STR_LIT>""" <EOL> parent = None <EOL> def _eq ( self , other ) : <EOL> raise NotImplementedError <EOL> def __eq__ ( self , other ) : <EOL> if self . __class__ is not other . __class__ : <EOL> return NotImplemented <EOL> return self . _eq ( other ) <EOL> def __ne__ ( self , other ) : <EOL> if self . __class__ is not other . __class__ : <EOL> return NotImplemented <EOL> return not self . _eq ( other ) <EOL> __hash__ = None <EOL> def get_prev_sibling ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . parent is None : <EOL> return None <EOL> for i , child in enumerate ( self . parent . children ) : <EOL> if child is self : <EOL> if i == <NUM_LIT:0> : <EOL> return None <EOL> return self . parent . children [ i - <NUM_LIT:1> ] <EOL> def get_next_sibling ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . parent is None : <EOL> return None <EOL> for i , child in enumerate ( self . parent . children ) : <EOL> if child is self : <EOL> try : <EOL> return self . parent . children [ i + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> return None <EOL> def get_prev_leaf ( self ) : <EOL> """<STR_LIT>""" <EOL> def last_child ( node ) : <EOL> if isinstance ( node , Leaf ) : <EOL> return node <EOL> elif not node . children : <EOL> return None <EOL> else : <EOL> return last_child ( node . children [ - <NUM_LIT:1> ] ) <EOL> if self . parent is None : <EOL> return None <EOL> prev = self . get_prev_sibling ( ) <EOL> if isinstance ( prev , Leaf ) : <EOL> return prev <EOL> elif prev is not None : <EOL> return last_child ( prev ) <EOL> return self . parent . get_prev_leaf ( ) <EOL> def get_next_leaf ( self ) : <EOL> """<STR_LIT>""" <EOL> node = self <EOL> while not isinstance ( node , Leaf ) : <EOL> assert node . children <EOL> node = node . children [ <NUM_LIT:0> ] <EOL> return node <EOL> def get_lineno ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_next_leaf ( ) . lineno <EOL> def get_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_next_leaf ( ) . prefix <EOL> class Node ( BaseNode ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type , children , context = None ) : <EOL> self . type = type <EOL> self . children = list ( children ) <EOL> for ch in self . children : <EOL> ch . parent = self <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . type , self . children ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( map ( str , self . children ) ) <EOL> def _eq ( self , other ) : <EOL> return ( self . type , self . children ) == ( other . type , other . children ) <EOL> def __getitem__ ( self , index ) : <EOL> return self . children [ index ] <EOL> def __iter__ ( self ) : <EOL> return iter ( self . children ) <EOL> def __len__ ( self ) : <EOL> return len ( self . children ) <EOL> class Leaf ( BaseNode ) : <EOL> """<STR_LIT>""" <EOL> prefix = '<STR_LIT>' <EOL> lineno = <NUM_LIT:0> <EOL> column = <NUM_LIT:0> <EOL> def __init__ ( self , type , value , context = None ) : <EOL> self . type = type <EOL> self . value = value <EOL> if context is not None : <EOL> self . prefix , ( self . lineno , self . column ) = context <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . type , self . value , self . prefix ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . prefix + str ( self . value ) <EOL> def _eq ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return ( self . type , self . value ) == ( other . type , other . value ) <EOL> def convert ( grammar , raw_node ) : <EOL> """<STR_LIT>""" <EOL> type , value , context , children = raw_node <EOL> if children or type in grammar . number2symbol : <EOL> if len ( children ) == <NUM_LIT:1> : <EOL> return children [ <NUM_LIT:0> ] <EOL> return Node ( type , children , context = context ) <EOL> else : <EOL> return Leaf ( type , value , context = context ) <EOL> def nice_repr ( node , number2name , prefix = False ) : <EOL> def _repr ( node ) : <EOL> if isinstance ( node , Leaf ) : <EOL> return "<STR_LIT>" % ( number2name [ node . type ] , node . value ) <EOL> else : <EOL> return "<STR_LIT>" % ( number2name [ node . type ] , <EOL> '<STR_LIT:U+002CU+0020>' . join ( map ( _repr , node . children ) ) ) <EOL> def _prepr ( node ) : <EOL> if isinstance ( node , Leaf ) : <EOL> return "<STR_LIT>" % ( number2name [ node . type ] , <EOL> node . prefix , node . value ) <EOL> else : <EOL> return "<STR_LIT>" % ( number2name [ node . type ] , <EOL> '<STR_LIT:U+002CU+0020>' . join ( map ( _prepr , node . children ) ) ) <EOL> return ( prefix and _prepr or _repr ) ( node ) <EOL> class NodeVisitor ( object ) : <EOL> def __init__ ( self , number2name , * args ) : <EOL> self . number2name = number2name <EOL> self . init ( * args ) <EOL> def init ( self , * args ) : <EOL> pass <EOL> def visit ( self , node ) : <EOL> """<STR_LIT>""" <EOL> method = '<STR_LIT>' + self . number2name [ node . type ] <EOL> visitor = getattr ( self , method , self . generic_visit ) <EOL> return visitor ( node ) <EOL> def generic_visit ( self , node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node , Node ) : <EOL> for child in node : <EOL> self . visit ( child ) </s>
