<s> import unittest <EOL> from construct import * <EOL> from construct . text import * <EOL> class NodeAdapter ( Adapter ) : <EOL> def __init__ ( self , factory , subcon ) : <EOL> Adapter . __init__ ( self , subcon ) <EOL> self . factory = factory <EOL> def _decode ( self , obj , context ) : <EOL> return self . factory ( obj ) <EOL> class Node ( Container ) : <EOL> def __init__ ( self , name , ** kw ) : <EOL> Container . __init__ ( self ) <EOL> self . name = name <EOL> for k , v in kw . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> def accept ( self , visitor ) : <EOL> return getattr ( visitor , "<STR_LIT>" % self . name ) ( self ) <EOL> def binop_node ( obj ) : <EOL> lhs , rhs = obj <EOL> if rhs is None : <EOL> return lhs <EOL> else : <EOL> op , rhs = rhs <EOL> return Node ( "<STR_LIT>" , lhs = lhs , op = op , rhs = rhs ) <EOL> def literal_node ( value ) : <EOL> return Node ( "<STR_LIT>" , value = value ) <EOL> ws = Whitespace ( ) <EOL> term = IndexingAdapter ( <EOL> Sequence ( "<STR_LIT>" , <EOL> ws , <EOL> Select ( "<STR_LIT>" , <EOL> NodeAdapter ( literal_node , DecNumber ( "<STR_LIT>" ) ) , <EOL> IndexingAdapter ( <EOL> Sequence ( "<STR_LIT>" , <EOL> Literal ( "<STR_LIT:(>" ) , <EOL> LazyBound ( "<STR_LIT>" , lambda : expr ) , <EOL> Literal ( "<STR_LIT:)>" ) <EOL> ) , <EOL> index = <NUM_LIT:0> <EOL> ) , <EOL> ) , <EOL> ws , <EOL> ) , <EOL> index = <NUM_LIT:0> <EOL> ) <EOL> def OptSeq ( name , * args ) : <EOL> return Optional ( Sequence ( name , * args ) ) <EOL> expr1 = NodeAdapter ( binop_node , <EOL> Sequence ( "<STR_LIT>" , <EOL> term , <EOL> OptSeq ( "<STR_LIT>" , <EOL> CharOf ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> LazyBound ( "<STR_LIT>" , lambda : expr1 ) <EOL> ) , <EOL> ) <EOL> ) <EOL> expr2 = NodeAdapter ( binop_node , <EOL> Sequence ( "<STR_LIT>" , <EOL> expr1 , <EOL> OptSeq ( "<STR_LIT>" , <EOL> CharOf ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> LazyBound ( "<STR_LIT>" , lambda : expr2 ) <EOL> ) , <EOL> ) <EOL> ) <EOL> expr = expr2 <EOL> class EvalVisitor ( object ) : <EOL> def visit_literal ( self , obj ) : <EOL> return obj . value <EOL> def visit_binop ( self , obj ) : <EOL> lhs = obj . lhs . accept ( self ) <EOL> op = obj . op <EOL> rhs = obj . rhs . accept ( self ) <EOL> if op == "<STR_LIT:+>" : <EOL> return lhs + rhs <EOL> elif op == "<STR_LIT:->" : <EOL> return lhs - rhs <EOL> elif op == "<STR_LIT:*>" : <EOL> return lhs * rhs <EOL> elif op == "<STR_LIT:/>" : <EOL> return lhs / rhs <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" , op ) <EOL> ev = EvalVisitor ( ) <EOL> class TestSomethingSomething ( unittest . TestCase ) : <EOL> def test_that_one_thing ( self ) : <EOL> node = expr . parse ( "<STR_LIT>" ) <EOL> self . assertEqual ( node . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . op , "<STR_LIT:+>" ) <EOL> self . assertEqual ( node . rhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . rhs . value , <NUM_LIT:4> ) <EOL> self . assertEqual ( node . lhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . lhs . op , "<STR_LIT:*>" ) <EOL> self . assertEqual ( node . lhs . rhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . lhs . rhs . value , <NUM_LIT:3> ) <EOL> self . assertEqual ( node . lhs . lhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . lhs . lhs . value , <NUM_LIT:2> ) <EOL> def test_that_other_thing ( self ) : <EOL> node = expr . parse ( "<STR_LIT>" ) <EOL> self . assertEqual ( node . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . op , "<STR_LIT:*>" ) <EOL> self . assertEqual ( node . rhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . rhs . op , "<STR_LIT:+>" ) <EOL> self . assertEqual ( node . rhs . rhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . rhs . rhs . value , <NUM_LIT:4> ) <EOL> self . assertEqual ( node . rhs . lhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . rhs . lhs . value , <NUM_LIT:3> ) <EOL> self . assertEqual ( node . lhs . name , "<STR_LIT>" ) <EOL> self . assertEqual ( node . lhs . value , <NUM_LIT:2> ) </s>
