Add std/json character classes

This commit is contained in:
Nigel Tao committed 2020-02-22 08:07:26 +11:00
1 parent 9263c80cfb
commit 48e7004636
3 files changed
+116 -40

No files matched your search

+66
View File
@@ -19,6 +19,8 @@ pub status "#unsupported recursion depth"
pri status "#internal error: inconsistent I/O"
pri const backslashes array[256] base.u8 = [
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
@@ -54,9 +56,71 @@ pri const backslashes array[256] base.u8 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
]
// - 0x00 is CLASS_WHITESPACE.
// - 0x01 is CLASS_STRING.
// - 0x02 is CLASS_COMMA.
// - 0x03 is CLASS_COLON.
// - 0x04 is CLASS_NUMBER.
// - 0x05 is CLASS_OPEN_CURLY_BRACE.
// - 0x06 is CLASS_CLOSE_CURLY_BRACE.
// - 0x07 is CLASS_OPEN_SQUARE_BRACKET.
// - 0x08 is CLASS_CLOSE_SQUARE_BRACKET.
// - 0x09 is CLASS_FALSE.
// - 0x0A is CLASS_TRUE.
// - 0x0B is CLASS_NULL.
// - 0x0C is reserved.
// - 0x0D is reserved.
// - 0x0E is reserved.
// - 0x0F is CLASS_BAD_INPUT.
pri const classes array[256] base.u8[..= 0x0F] = [
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x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7
// 8 9 A B C D E F
]
pri const hexadecimal_digits array[256] base.u8 = [
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
@@ -92,4 +156,6 @@ pri const hexadecimal_digits array[256] base.u8 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
]
+15 -19
View File
@@ -57,6 +57,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
var stack_bit : base.u32[..= 31]
var match : base.u32[..= 2]
var c : base.u8
var class : base.u8[..= 0x0F]
var backslash : base.u8
var uni4_ok : base.u8
var uni4_string : base.u32
@@ -96,6 +97,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
// Consume whitespace.
whitespace_length = 0
c = 0
class = 0
while.ws true,
inv args.dst.available() > 0,
post args.src.available() > 0,
@@ -116,15 +118,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
c = args.src.peek_u8()
if c > 0x20 { // 0x20 is ' '.
class = classes[c]
if class <> 0x00 { // 0x00 is CLASS_WHITESPACE.
break.ws
} else if (c <> 0x20) and (c <> 0x0A) and (c <> 0x09) and (c <> 0x0D) {
if whitespace_length > 0 {
args.dst.write_fast_token!(
value: 0,
length: whitespace_length as base.u64)
}
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
@@ -157,7 +153,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
assert args.src.available() > 0
if c <= 0x39 { // 0x39 is '9'.
if c == 0x22 { // 0x22 is '"'.
if class == 0x01 { // 0x01 is CLASS_STRING.
// -------- BEGIN parse strings.
if 0 == (expect & 0x02) { // 0x02 is EXPECT_STRING_VALUE.
return "#bad input"
@@ -333,7 +329,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
break.goto_parsed_a_leaf_value
// -------- END parse strings.
} else if c == 0x2C { // 0x2C is ','.
} else if class == 0x02 { // 0x02 is CLASS_COMMA.
if 0 == (expect & 0x04) { // 0x01 is EXPECT_COMMA.
return "#bad input"
}
@@ -351,7 +347,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
continue.outer
} else if (c >= 0x30) or (c == 0x2D) { // 0x30 is '0', 0x2D is '-'.
} else if class == 0x04 { // 0x04 is CLASS_NUMBER.
// -------- BEGIN parse numbers.
if 0 == (expect & 0x01) { // 0x02 is EXPECT_NON_STRING_VALUE.
return "#bad input"
@@ -399,7 +395,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
// -------- END parse numbers.
}
} else if c == 0x3A { // 0x3A is ':'.
} else if class == 0x03 { // 0x03 is CLASS_COLON.
if 0 == (expect & 0x08) { // 0x08 is EXPECT_COLON.
return "#bad input"
}
@@ -411,7 +407,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
expect = 0x03 // 0x03 is EXPECT_VALUE.
continue.outer
} else if c == 0x5B { // 0x5B is '['.
} else if class == 0x07 { // 0x07 is CLASS_OPEN_SQUARE_BRACKET.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
@@ -431,7 +427,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
expect_after_value = 0x14 // 0x14 is (EXPECT_CLOSE_BRACKET | EXPECT_COMMA).
continue.outer
} else if c == 0x5D { // 0x5D is ']'.
} else if class == 0x08 { // 0x08 is CLASS_CLOSE_SQUARE_BRACKET.
if 0 == (expect & 0x10) { // 0x10 is EXPECT_CLOSE_BRACKET
return "#bad input"
}
@@ -462,7 +458,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
continue.outer
} else if c == 0x7B { // 0x7B is '{'.
} else if class == 0x05 { // 0x05 is CLASS_OPEN_CURLY_BRACE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
@@ -482,7 +478,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
expect_after_value = 0x24 // 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
continue.outer
} else if c == 0x7D { // 0x7D is '}'.
} else if class == 0x06 { // 0x06 is CLASS_CLOSE_CURLY_BRACE.
if 0 == (expect & 0x20) { // 0x20 is EXPECT_CLOSE_BRACE.
return "#bad input"
}
@@ -518,7 +514,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
return "#bad input"
}
match = 2
if c == 0x6E { // 0x6E is 'n'.
if class == 0x0B { // 0x0B is CLASS_NULL.
match = args.src.match7(a: 0x6C_6C75_6E04) // 4 bytes "null".
if match == 0 {
args.dst.write_fast_token!(
@@ -531,7 +527,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
break.goto_parsed_a_leaf_value
}
} else if c == 0x66 { // 0x66 is 'f'.
} else if class == 0x09 { // 0x09 is CLASS_FALSE.
match = args.src.match7(a: 0x6573_6C61_6605) // 5 bytes "false".
if match == 0 {
args.dst.write_fast_token!(
@@ -544,7 +540,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
break.goto_parsed_a_leaf_value
}
} else if c == 0x74 { // 0x74 is 't'.
} else if class == 0x0A { // 0x0A is CLASS_TRUE.
match = args.src.match7(a: 0x65_7572_7404) // 4 bytes "true".
if match == 0 {
args.dst.write_fast_token!(