<s> """<STR_LIT>""" <EOL> import operator <EOL> class TokenBase ( object ) : <EOL> """<STR_LIT>""" <EOL> id = None <EOL> value = None <EOL> first = second = None <EOL> def nud ( self , parser ) : <EOL> raise parser . error_class ( <EOL> "<STR_LIT>" % self . id <EOL> ) <EOL> def led ( self , left , parser ) : <EOL> raise parser . error_class ( <EOL> "<STR_LIT>" % self . id <EOL> ) <EOL> def display ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . id <EOL> def __repr__ ( self ) : <EOL> out = [ str ( x ) for x in [ self . id , self . first , self . second ] if x is not None ] <EOL> return "<STR_LIT:(>" + "<STR_LIT:U+0020>" . join ( out ) + "<STR_LIT:)>" <EOL> def infix ( bp , func ) : <EOL> """<STR_LIT>""" <EOL> class Operator ( TokenBase ) : <EOL> lbp = bp <EOL> def led ( self , left , parser ) : <EOL> self . first = left <EOL> self . second = parser . expression ( bp ) <EOL> return self <EOL> def eval ( self , context ) : <EOL> try : <EOL> return func ( context , self . first , self . second ) <EOL> except Exception : <EOL> return False <EOL> return Operator <EOL> def prefix ( bp , func ) : <EOL> """<STR_LIT>""" <EOL> class Operator ( TokenBase ) : <EOL> lbp = bp <EOL> def nud ( self , parser ) : <EOL> self . first = parser . expression ( bp ) <EOL> self . second = None <EOL> return self <EOL> def eval ( self , context ) : <EOL> try : <EOL> return func ( context , self . first ) <EOL> except Exception : <EOL> return False <EOL> return Operator <EOL> OPERATORS = { <EOL> '<STR_LIT>' : infix ( <NUM_LIT:6> , lambda context , x , y : x . eval ( context ) or y . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:7> , lambda context , x , y : x . eval ( context ) and y . eval ( context ) ) , <EOL> '<STR_LIT>' : prefix ( <NUM_LIT:8> , lambda context , x : not x . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:9> , lambda context , x , y : x . eval ( context ) in y . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:9> , lambda context , x , y : x . eval ( context ) not in y . eval ( context ) ) , <EOL> '<STR_LIT:=>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) == y . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) == y . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) != y . eval ( context ) ) , <EOL> '<STR_LIT:>>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) > y . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) >= y . eval ( context ) ) , <EOL> '<STR_LIT:<>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) < y . eval ( context ) ) , <EOL> '<STR_LIT>' : infix ( <NUM_LIT:10> , lambda context , x , y : x . eval ( context ) <= y . eval ( context ) ) , <EOL> } <EOL> for key , op in OPERATORS . items ( ) : <EOL> op . id = key <EOL> class Literal ( TokenBase ) : <EOL> """<STR_LIT>""" <EOL> id = "<STR_LIT>" <EOL> lbp = <NUM_LIT:0> <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def display ( self ) : <EOL> return repr ( self . value ) <EOL> def nud ( self , parser ) : <EOL> return self <EOL> def eval ( self , context ) : <EOL> return self . value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . id , self . value ) <EOL> class EndToken ( TokenBase ) : <EOL> lbp = <NUM_LIT:0> <EOL> def nud ( self , parser ) : <EOL> raise parser . error_class ( "<STR_LIT>" ) <EOL> EndToken = EndToken ( ) <EOL> class IfParser ( object ) : <EOL> error_class = ValueError <EOL> def __init__ ( self , tokens ) : <EOL> l = len ( tokens ) <EOL> mapped_tokens = [ ] <EOL> i = <NUM_LIT:0> <EOL> while i < l : <EOL> token = tokens [ i ] <EOL> if token == "<STR_LIT>" and i + <NUM_LIT:1> < l and tokens [ i + <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> token = "<STR_LIT>" <EOL> i += <NUM_LIT:1> <EOL> mapped_tokens . append ( self . translate_token ( token ) ) <EOL> i += <NUM_LIT:1> <EOL> self . tokens = mapped_tokens <EOL> self . pos = <NUM_LIT:0> <EOL> self . current_token = self . next ( ) <EOL> def translate_token ( self , token ) : <EOL> try : <EOL> op = OPERATORS [ token ] <EOL> except ( KeyError , TypeError ) : <EOL> return self . create_var ( token ) <EOL> else : <EOL> return op ( ) <EOL> def next ( self ) : <EOL> if self . pos >= len ( self . tokens ) : <EOL> return EndToken <EOL> else : <EOL> retval = self . tokens [ self . pos ] <EOL> self . pos += <NUM_LIT:1> <EOL> return retval <EOL> def parse ( self ) : <EOL> retval = self . expression ( ) <EOL> if self . current_token is not EndToken : <EOL> raise self . error_class ( "<STR_LIT>" % <EOL> self . current_token . display ( ) ) <EOL> return retval <EOL> def expression ( self , rbp = <NUM_LIT:0> ) : <EOL> t = self . current_token <EOL> self . current_token = self . next ( ) <EOL> left = t . nud ( self ) <EOL> while rbp < self . current_token . lbp : <EOL> t = self . current_token <EOL> self . current_token = self . next ( ) <EOL> left = t . led ( left , self ) <EOL> return left <EOL> def create_var ( self , value ) : <EOL> return Literal ( value ) </s>
