def test_read_int(self): reader = DataReader( b'\x01\x00\x00\x00\x02\x00\x00\x00\x00\xff\x00\xff') self.assertEqual(reader.read_int(), 0x00000001) self.assertEqual(reader.read_int(), 0x00000002) self.assertEqual(reader.read_int(), 0xFF00FF00) self.assertEqual(reader.read_int(), None)
def test_read_bool(self): reader = DataReader(b'\x00\x01\x02') self.assertEqual(reader.read_bool(), False) self.assertEqual(reader.read_bool(), True) # Not sure about this case self.assertEqual(reader.read_bool(), False) self.assertEqual(reader.read_bool(), None)
def test_read_string_u(self): reader = DataReader( b'\x05\x00\x00\x00\x53\x30\x93\x30\x6B\x30\x61\x30\x6F\x30') self.assertEqual(reader.read_string(), u'こんにちは') self.assertEqual(reader.read_string(), None)
def test_read_string(self): reader = DataReader(b'\x04\x00\x00\x00T\x00E\x00S\x00T\x00') self.assertEqual(reader.read_string(), 'TEST') self.assertEqual(reader.read_string(), None)
def test_read_byte(self): reader = DataReader(b'\x01\x02\xff') self.assertEqual(reader.read_byte(), 0x01) self.assertEqual(reader.read_byte(), 0x02) self.assertEqual(reader.read_byte(), 0xFF) self.assertEqual(reader.read_byte(), None)
def test_read_rk(self): # RK type 0 IEEE reader = DataReader(b'\x00\x00\xF0\x3F') self.assertEqual(reader.read_rk(), 1) self.assertEqual(reader.read_rk(), None) # RK type 1 IEEE x 100 reader = DataReader(b'\x01\xC0\x5E\x40') self.assertEqual(reader.read_rk(), 1.23) self.assertEqual(reader.read_rk(), None) # RK type 2 integer reader = DataReader(b'\x3A\x85\xF1\x02') self.assertEqual(reader.read_rk(), 12345678) self.assertEqual(reader.read_rk(), None) # RK type 3 integer x 100 reader = DataReader(b'\x3B\x85\xF1\x02') self.assertEqual(reader.read_rk(), 123456.78) self.assertEqual(reader.read_rk(), None)
def __init__(self, fp): self._reader = DataReader(fp)
class TokenReader(object): default_ptg = UnknownPtg ptgs = { # Unary operators UPlusPtg.ptg: UPlusPtg, UMinusPtg.ptg: UMinusPtg, PercentPtg.ptg: PercentPtg, # Binary operators AddPtg.ptg: AddPtg, SubstractPtg.ptg: SubstractPtg, MultiplyPtg.ptg: MultiplyPtg, DividePtg.ptg: DividePtg, PowerPtg.ptg: PowerPtg, ConcatPtg.ptg: ConcatPtg, LessPtg.ptg: LessPtg, LessEqualPtg.ptg: LessEqualPtg, EqualPtg.ptg: EqualPtg, GreaterEqualPtg.ptg: GreaterEqualPtg, GreaterPtg.ptg: GreaterPtg, NotEqualPtg.ptg: NotEqualPtg, IntersectionPtg.ptg: IntersectionPtg, UnionPtg.ptg: UnionPtg, RangePtg.ptg: RangePtg, # Operands MissArgPtg.ptg: MissArgPtg, StringPtg.ptg: StringPtg, ErrorPtg.ptg: ErrorPtg, BooleanPtg.ptg: BooleanPtg, IntegerPtg.ptg: IntegerPtg, NumberPtg.ptg: NumberPtg, ArrayPtg.ptg: ArrayPtg, NamePtg.ptg: NamePtg, RefPtg.ptg: RefPtg, AreaPtg.ptg: AreaPtg, MemAreaPtg.ptg: MemAreaPtg, MemErrPtg.ptg: MemErrPtg, RefErrPtg.ptg: RefErrPtg, AreaErrPtg.ptg: AreaErrPtg, RefNPtg.ptg: RefNPtg, AreaNPtg.ptg: AreaNPtg, NameXPtg.ptg: NameXPtg, Ref3dPtg.ptg: Ref3dPtg, Area3dPtg.ptg: Area3dPtg, RefErr3dPtg.ptg: RefErr3dPtg, AreaErr3dPtg.ptg: AreaErr3dPtg, # Control ExpPtg.ptg: ExpPtg, TablePtg.ptg: TablePtg, ParenPtg.ptg: ParenPtg, AttrPtg.ptg: AttrPtg, MemNoMemPtg.ptg: MemNoMemPtg, MemFuncPtg.ptg: MemFuncPtg, MemAreaNPtg.ptg: MemAreaNPtg, MemNoMemNPtg.ptg: MemNoMemNPtg, # Func operators FuncPtg.ptg: FuncPtg, FuncVarPtg.ptg: FuncVarPtg } def __init__(self, fp): self._reader = DataReader(fp) def __iter__(self): return self def __next__(self): return self.next() def next(self): ptg = self._reader.read_byte() if ptg is None: raise StopIteration base = ((ptg | 0x20) if ptg & 0x40 == 0x40 else ptg) & 0x3F return (self.ptgs.get(base) or self.default_ptg).parse(self._reader, ptg)