Implement simple values
parent
4872fc4471
commit
e7f4426062
|
@ -51,19 +51,19 @@ tests = [
|
||||||
# { %(Infinity), "fb 7f f0 00 00 00 00 00 00" },
|
# { %(Infinity), "fb 7f f0 00 00 00 00 00 00" },
|
||||||
# { %(NaN), "fb 7f f8 00 00 00 00 00 00" },
|
# { %(NaN), "fb 7f f8 00 00 00 00 00 00" },
|
||||||
# { %(-Infinity), "fb ff f0 00 00 00 00 00 00" },
|
# { %(-Infinity), "fb ff f0 00 00 00 00 00 00" },
|
||||||
# { %(false), "f4" },
|
{ %(false), "f4" },
|
||||||
# { %(true), "f5" },
|
{ %(true), "f5" },
|
||||||
# { %(null), "f6" },
|
{ %(null), "f6" },
|
||||||
# { %(undefined), "f7" },
|
{ %(undefined), "f7" },
|
||||||
# { %(simple(16)), "f0" },
|
{ %(simple(16)), "f0" },
|
||||||
# { %(simple(24)), "f8 18" },
|
{ %(simple(24)), "f8 18" },
|
||||||
# { %(simple(255)), "f8 ff" },
|
{ %(simple(255)), "f8 ff" },
|
||||||
# { %(0("2013-03-21T20:04:00Z")), "c0 74 32 30 31 33 2d 30 33 2d 32 31 54 32 30 3a 30 34 3a 30 30 5a" },
|
{ %(0("2013-03-21T20:04:00Z")), "c0 74 32 30 31 33 2d 30 33 2d 32 31 54 32 30 3a 30 34 3a 30 30 5a" },
|
||||||
# { %(1(1363896240)), "c1 1a 51 4b 67 b0" },
|
{ %(1(1363896240)), "c1 1a 51 4b 67 b0" },
|
||||||
# { %(1(1363896240.5)), "c1 fb 41 d4 52 d9 ec 20 00 00" },
|
# { %(1(1363896240.5)), "c1 fb 41 d4 52 d9 ec 20 00 00" },
|
||||||
# { %(23(h'01020304')), "d7 44 01 02 03 04" },
|
{ %(23(h'01020304')), "d7 44 01 02 03 04" },
|
||||||
# { %(24(h'6449455446')), "d8 18 45 64 49 45 54 46" },
|
{ %(24(h'6449455446')), "d8 18 45 64 49 45 54 46" },
|
||||||
# { %(32("http://www.example.com")), "d8 20 76 68 74 74 70 3a 2f 2f 77 77 77 2e 65 78 61 6d 70 6c 65 2e 63 6f 6d" },
|
{ %(32("http://www.example.com")), "d8 20 76 68 74 74 70 3a 2f 2f 77 77 77 2e 65 78 61 6d 70 6c 65 2e 63 6f 6d" },
|
||||||
{ %(h''), "40" },
|
{ %(h''), "40" },
|
||||||
{ %(h'01020304'), "44 01 02 03 04" },
|
{ %(h'01020304'), "44 01 02 03 04" },
|
||||||
{ %(""), "60" },
|
{ %(""), "60" },
|
||||||
|
|
|
@ -50,9 +50,10 @@ class CBOR::Diagnostic
|
||||||
hash_body = read_hash(token.size).join(", ")
|
hash_body = read_hash(token.size).join(", ")
|
||||||
return "{#{hash_body}}" if token.size
|
return "{#{hash_body}}" if token.size
|
||||||
"{_ #{hash_body}}"
|
"{_ #{hash_body}}"
|
||||||
when Token::BoolT
|
when Token::SimpleValueT
|
||||||
return "true" if token.value
|
token.value.to_diagnostic
|
||||||
"false"
|
when Token::TagT
|
||||||
|
"#{token.value.value.to_s}(#{next_value})"
|
||||||
else
|
else
|
||||||
token.inspect
|
token.inspect
|
||||||
end
|
end
|
||||||
|
|
|
@ -82,10 +82,8 @@ class CBOR::Lexer
|
||||||
when 0xc0..0xdb
|
when 0xc0..0xdb
|
||||||
consume_tag(read_size(byte - 0xc0))
|
consume_tag(read_size(byte - 0xc0))
|
||||||
##################
|
##################
|
||||||
when 0xf4
|
when 0xe0..0xf8
|
||||||
Token::BoolT.new(value: false)
|
consume_simple_value(read_size(byte - 0xe0))
|
||||||
when 0xf5
|
|
||||||
Token::BoolT.new(value: true)
|
|
||||||
else
|
else
|
||||||
raise ParseError.new("Unexpected first byte 0x#{byte.to_s(16)}")
|
raise ParseError.new("Unexpected first byte 0x#{byte.to_s(16)}")
|
||||||
end
|
end
|
||||||
|
@ -172,9 +170,14 @@ class CBOR::Lexer
|
||||||
Token::MapT.new(size: size.to_i32)
|
Token::MapT.new(size: size.to_i32)
|
||||||
end
|
end
|
||||||
|
|
||||||
private def consume_tag(size) : Token::TagT
|
private def consume_tag(id) : Token::TagT
|
||||||
raise ParseError.new("Maximum size for tag exceeded") if size > UInt32::MAX
|
raise ParseError.new("Maximum size for tag ID exceeded") if id > UInt32::MAX
|
||||||
Token::TagT.new(id: size.to_u32)
|
Token::TagT.new(value: Tag.new(id.to_u32))
|
||||||
|
end
|
||||||
|
|
||||||
|
private def consume_simple_value(id) : Token::SimpleValueT
|
||||||
|
raise ParseError.new("Invalid simple value #{id.to_s}") if id > 255
|
||||||
|
Token::SimpleValueT.new(value: SimpleValue.new(id.to_u8))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a method overloaded for each UInt sizes to convert the UInt into
|
# Creates a method overloaded for each UInt sizes to convert the UInt into
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
enum CBOR::SimpleValue : UInt8
|
||||||
|
False = 20
|
||||||
|
True
|
||||||
|
Null
|
||||||
|
Undefined
|
||||||
|
|
||||||
|
def to_diagnostic : String
|
||||||
|
case self
|
||||||
|
when False
|
||||||
|
"false"
|
||||||
|
when True
|
||||||
|
"true"
|
||||||
|
when Null
|
||||||
|
"null"
|
||||||
|
when Undefined
|
||||||
|
"undefined"
|
||||||
|
else
|
||||||
|
"simple(#{self.value.to_s})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,21 +1,21 @@
|
||||||
module CBOR::Tag
|
enum CBOR::Tag : UInt32
|
||||||
enum Kind
|
RFC3339Time
|
||||||
Unassigned
|
EpochTime
|
||||||
RFC3339Time
|
PositiveBigNum
|
||||||
EpochTime
|
NegativeBigNum
|
||||||
PositiveBigNum
|
Decimal
|
||||||
NegativeBigNum
|
BigFloat
|
||||||
DecimalFraction
|
|
||||||
BigFloat
|
ConvertBase64URL = 21
|
||||||
ExpectBase64URLConversion
|
ConvertBase64
|
||||||
ExpectBase64Conversion
|
ConvertBase16
|
||||||
ExpectBase16Conversion
|
CBOREncoded
|
||||||
EncodedCBOR
|
|
||||||
URI
|
URI = 32
|
||||||
Base64URL
|
Base64URL
|
||||||
Base64
|
Base64
|
||||||
RegularExpresion
|
RegularExpression
|
||||||
MIME
|
MimeMessage
|
||||||
SelfDescribeCBOR
|
|
||||||
end
|
CBORMarker = 55799
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
module CBOR::Token
|
module CBOR::Token
|
||||||
record NullT, undefined : Bool = false
|
|
||||||
record BoolT, value : Bool
|
|
||||||
record IntT, value : Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 | Int64 | UInt64 | Int128
|
record IntT, value : Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 | Int64 | UInt64 | Int128
|
||||||
record FloatT, value : Float32 | Float64
|
record FloatT, value : Float32 | Float64
|
||||||
record BytesT, value : Bytes, chunks : Array(Int32)? = nil
|
record BytesT, value : Bytes, chunks : Array(Int32)? = nil
|
||||||
record StringT, value : String, chunks : Array(Int32)? = nil
|
record StringT, value : String, chunks : Array(Int32)? = nil
|
||||||
record ArrayT, size : Int32? = nil
|
record ArrayT, size : Int32? = nil
|
||||||
record MapT, size : Int32? = nil
|
record MapT, size : Int32? = nil
|
||||||
record TagT, id : UInt32
|
record TagT, value : Tag
|
||||||
|
record SimpleValueT, value : SimpleValue
|
||||||
|
|
||||||
alias T = NullT |
|
alias T = IntT |
|
||||||
BoolT |
|
|
||||||
IntT |
|
|
||||||
FloatT |
|
FloatT |
|
||||||
BytesT |
|
BytesT |
|
||||||
StringT |
|
StringT |
|
||||||
ArrayT |
|
ArrayT |
|
||||||
MapT |
|
MapT |
|
||||||
TagT
|
TagT |
|
||||||
|
SimpleValueT
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue