Tweak JSON trailing comment/new-line quirks

This commit is contained in:
Nigel Tao committed 2020-09-22 23:03:49 +10:00
1 parent 3e8cb4ebf8
commit cd4cbc9102
7 files changed
+287 -205

No files matched your search

+38 -26
View File
@@ -15,14 +15,15 @@
pub struct decoder? implements base.token_decoder(
quirks : array[QUIRKS_COUNT] base.bool,
allow_leading_ars : base.bool,
allow_leading_ubom : base.bool,
allow_trailing_comment : base.bool,
allow_leading_ars : base.bool,
allow_leading_ubom : base.bool,
end_of_data : base.bool,
trailer_stop : base.u8,
// comment_type is set as a side-effect of decode_comment?.
// - 0 means failure.
// - 0 means no comment.
// - 1 means a block comment.
// - 2 means a line comment.
comment_type : base.u8,
@@ -123,6 +124,14 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader, wor
return base."@end of data"
}
if this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
if this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] or
this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] or
this.quirks[QUIRK_ALLOW_TRAILING_FILLER - QUIRKS_BASE] {
return "#bad quirk combination"
}
}
if this.quirks[QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR - QUIRKS_BASE] or
this.quirks[QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK - QUIRKS_BASE] {
this.decode_leading?(dst: args.dst, src: args.src)
@@ -1176,7 +1185,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader, wor
if this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] or
this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] {
this.decode_comment?(dst: args.dst, src: args.src)
continue.outer
if this.comment_type > 0 {
continue.outer
}
}
}
@@ -1191,8 +1202,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader, wor
expect = expect_after_value
} endwhile.outer
if this.quirks[QUIRK_ALLOW_TRAILING_NEW_LINE - QUIRKS_BASE] {
this.decode_trailing_new_line?(dst: args.dst, src: args.src)
if this.quirks[QUIRK_ALLOW_TRAILING_FILLER - QUIRKS_BASE] or
this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
this.decode_trailer?(dst: args.dst, src: args.src)
}
this.end_of_data = true
@@ -1438,7 +1450,7 @@ pri func decoder.decode_comment?(dst: base.token_writer, src: base.io_reader) {
continue
}
if args.src.is_closed() {
return "#bad input"
return ok
}
yield? base."$short read"
} endwhile
@@ -1562,8 +1574,6 @@ pri func decoder.decode_comment?(dst: base.token_writer, src: base.io_reader) {
} endwhile
} endwhile.comment_line
}
return "#bad input"
}
pri func decoder.decode_inf_nan?(dst: base.token_writer, src: base.io_reader) {
@@ -1677,11 +1687,15 @@ pri func decoder.decode_inf_nan?(dst: base.token_writer, src: base.io_reader) {
} endwhile
}
pri func decoder.decode_trailing_new_line?(dst: base.token_writer, src: base.io_reader) {
pri func decoder.decode_trailer?(dst: base.token_writer, src: base.io_reader) {
var c : base.u8
var whitespace_length : base.u32[..= 0xFFFE]
this.allow_trailing_comment = this.quirks[QUIRK_ALLOW_TRAILING_COMMENT - QUIRKS_BASE]
if this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
this.trailer_stop = '\n'
} else {
this.trailer_stop = 0
}
while.outer true {
if args.dst.length() <= 0 {
@@ -1714,27 +1728,25 @@ pri func decoder.decode_trailing_new_line?(dst: base.token_writer, src: base.io_
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length)
whitespace_length = 0
}
if this.allow_trailing_comment {
this.decode_comment?(dst: args.dst, src: args.src)
c = 0
whitespace_length = 0
if this.comment_type == 1 { // Block comment.
this.allow_trailing_comment = false
continue.outer
} else if this.comment_type == 2 { // Line comment.
break.outer
}
if this.trailer_stop > 0 {
return "#bad input"
}
return "#bad input"
this.decode_comment?(dst: args.dst, src: args.src)
c = 0
whitespace_length = 0
if this.comment_type > 0 {
continue.outer
}
return ok
}
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
if (whitespace_length >= 0xFFFE) or (c == '\n') {
if (whitespace_length >= 0xFFFE) or (c == this.trailer_stop) {
args.dst.write_simple_token_fast!(
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length + 1)
whitespace_length = 0
if c == '\n' {
break.outer
if c == this.trailer_stop {
return ok
}
continue.outer
}