Continue work on lexer
parent
b5f4b0d368
commit
3b7d42c242
|
@ -19,6 +19,14 @@ class CBOR::Lexer
|
|||
Token.new(kind: Token::Kind::UInt, value: @curren_byte)
|
||||
when 0x18
|
||||
read_uint8
|
||||
when 0x19
|
||||
read_uint16
|
||||
when 0x1a
|
||||
read_uint32
|
||||
when 0x1b
|
||||
read_uint64
|
||||
when .<= 0x37
|
||||
Token.new(kind: Token::Kind::NInt, value: Int8(@curren_byte))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -29,6 +37,30 @@ class CBOR::Lexer
|
|||
Token.new(kind: Token::Kind::UInt, value: byte)
|
||||
end
|
||||
|
||||
private def read_uint16
|
||||
value = UInt16.from_io(read_next(2), IO::ByteFormat::BigEndian)
|
||||
Token.new(kind: Token::Kind::UInt, value: value)
|
||||
end
|
||||
|
||||
private def read_uint32
|
||||
value = UInt32.from_io(read_next(4), IO::ByteFormat::BigEndian)
|
||||
Token.new(kind: Token::Kind::UInt, value: value)
|
||||
end
|
||||
|
||||
private def read_uint64
|
||||
value = UInt64.from_io(read_next(8), IO::ByteFormat::BigEndian)
|
||||
Token.new(kind: Token::Kind::UInt, value: value)
|
||||
end
|
||||
|
||||
private def read_next(n : Int)
|
||||
slice = Bytes.new(n)
|
||||
|
||||
read = @io.read(slice)
|
||||
return unexpect_eof if read == 0
|
||||
|
||||
slice
|
||||
end
|
||||
|
||||
private def unexpected_eof
|
||||
raise "Unexpected EOF"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue