Continue work on lexer

dev
Alberto Restifo 2020-04-17 19:50:26 +02:00
parent b5f4b0d368
commit 3b7d42c242
1 changed files with 32 additions and 0 deletions

View File

@ -19,6 +19,14 @@ class CBOR::Lexer
Token.new(kind: Token::Kind::UInt, value: @curren_byte) Token.new(kind: Token::Kind::UInt, value: @curren_byte)
when 0x18 when 0x18
read_uint8 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
end end
@ -29,6 +37,30 @@ class CBOR::Lexer
Token.new(kind: Token::Kind::UInt, value: byte) Token.new(kind: Token::Kind::UInt, value: byte)
end 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 private def unexpected_eof
raise "Unexpected EOF" raise "Unexpected EOF"
end end