Compare commits

..
Author SHA1 Message Date
Nigel Tao 1b9a6cb85b wuffs gen -version=0.3.0-beta.8 2021-07-20 14:21:21 +10:00
Nigel Tao 77f855206d Have JSON line-comment tokens exclude the "\n"
Before this commit, line-comment token chains included the final "\n"
the way block-comments included the final "*/". The "\n" was mandatory.

However, when parsing with both QUIRK_ALLOW_COMMENT_LINE and
QUIRK_ALLOW_TRAILING_FILLER, input like "123//xyz" (without a "\n"
before the EOF) was rejected before and accepted after this commit.

So that line-comments are consistent, in that they either always or
never include the final "\n", they now never include the final "\n".

An input fragment (parsed with QUIRK_ALLOW_COMMENT_LINE) like
"//abc\n\t\t" was, before this commit, parsed as a 6 byte comment token
and a 2 byte other-filler token. That split is now 5+3.
2021-07-20 14:17:09 +10:00
Nigel Tao 40ffa334ee Add more json allow_trailing_comments test cases 2021-07-18 23:27:56 +10:00
7 changed files with 69 additions and 66 deletions

No files matched your search

+13 -31
View File
@@ -1074,7 +1074,7 @@ write_dst(const void* s, size_t n) {
return write_dst_slow(s, n);
}
#define TRY_INDENT_WITH_LEADING_NEW_LINE \
#define TRY_INDENT \
do { \
uint32_t adj = (g_num_input_blank_lines > 1) ? 1 : 0; \
g_num_input_blank_lines = 0; \
@@ -1086,20 +1086,6 @@ write_dst(const void* s, size_t n) {
} \
} while (false)
// TRY_INDENT_SANS_LEADING_NEW_LINE is used after comments, which print their
// own "\n".
#define TRY_INDENT_SANS_LEADING_NEW_LINE \
do { \
uint32_t adj = (g_num_input_blank_lines > 1) ? 1 : 0; \
g_num_input_blank_lines = 0; \
uint32_t indent = g_depth * g_bytes_per_indent_depth; \
TRY(write_dst(g_two_new_lines_then_256_indent_bytes + 2 - adj, \
adj + (indent & 0xFF))); \
for (indent >>= 8; indent > 0; indent--) { \
TRY(write_dst(g_two_new_lines_then_256_indent_bytes + 2, 0x100)); \
} \
} while (false)
// ----
const char* //
@@ -1155,12 +1141,12 @@ handle_token(wuffs_base__token t, bool start_of_token_chain) {
(g_ctx != context::in_dict_after_brace) &&
!g_flags.compact_output) {
if (g_is_after_comment) {
TRY_INDENT_SANS_LEADING_NEW_LINE;
TRY_INDENT;
} else {
if (g_flags.output_extra_comma) {
TRY(write_dst(",", 1));
}
TRY_INDENT_WITH_LEADING_NEW_LINE;
TRY_INDENT;
}
} else {
g_num_input_blank_lines = 0;
@@ -1181,7 +1167,7 @@ handle_token(wuffs_base__token t, bool start_of_token_chain) {
// continuation of a multi-token chain.
if (start_of_token_chain) {
if (g_is_after_comment) {
TRY_INDENT_SANS_LEADING_NEW_LINE;
TRY_INDENT;
} else if (g_ctx == context::in_dict_after_key) {
TRY(write_dst(": ", g_flags.compact_output ? 1 : 2));
} else if (g_ctx != context::none) {
@@ -1190,7 +1176,7 @@ handle_token(wuffs_base__token t, bool start_of_token_chain) {
TRY(write_dst(",", 1));
}
if (!g_flags.compact_output) {
TRY_INDENT_WITH_LEADING_NEW_LINE;
TRY_INDENT;
}
}
@@ -1336,11 +1322,16 @@ main1(int argc, char** argv) {
if (g_flags.compact_output) {
TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length,
token_length));
if (!t.continued() &&
(t.value_base_detail() &
WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE)) {
TRY(write_dst("\n", 1));
}
} else {
if (start_of_token_chain) {
if (g_is_after_comment) {
TRY_INDENT_SANS_LEADING_NEW_LINE;
TRY_INDENT;
} else if (g_ctx != context::none) {
if (g_ctx == context::in_dict_after_key) {
TRY(write_dst(":", 1));
@@ -1349,16 +1340,11 @@ main1(int argc, char** argv) {
(g_ctx != context::end_of_data)) {
TRY(write_dst(",", 1));
}
TRY_INDENT_WITH_LEADING_NEW_LINE;
TRY_INDENT;
}
}
TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length,
token_length));
if (!t.continued() &&
(t.value_base_detail() &
WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK)) {
TRY(write_dst("\n", 1));
}
g_is_after_comment = true;
}
if (g_ctx == context::in_list_after_bracket) {
@@ -1366,11 +1352,7 @@ main1(int argc, char** argv) {
} else if (g_ctx == context::in_dict_after_brace) {
g_ctx = context::in_dict_after_value;
}
g_num_input_blank_lines =
(t.value_base_detail() &
WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE)
? 1
: 0;
g_num_input_blank_lines = 0;
} else {
uint8_t* p = g_src.data.ptr + g_cursor_index - token_length;
+9 -7
View File
@@ -33096,16 +33096,19 @@ wuffs_json__decoder__decode_comment(
}
while (true) {
if (((uint64_t)(io2_a_src - iop_a_src)) <= 0) {
if (v_length > 0) {
if (a_src && a_src->meta.closed) {
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(4)) << WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) |
(((uint64_t)(v_length)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
self->private_impl.f_comment_type = 2;
status = wuffs_base__make_status(NULL);
goto ok;
} else if (v_length > 0) {
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(4)) << WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__CONTINUED__SHIFT) |
(((uint64_t)(v_length)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
}
if (a_src && a_src->meta.closed) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(6);
v_length = 0;
@@ -33113,10 +33116,9 @@ wuffs_json__decoder__decode_comment(
}
v_c = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
if (v_c == 10) {
iop_a_src += 1;
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(4)) << WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) |
(((uint64_t)((v_length + 1))) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
(((uint64_t)(v_length)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
self->private_impl.f_comment_type = 2;
status = wuffs_base__make_status(NULL);
goto ok;
+14 -12
View File
@@ -84,15 +84,15 @@ extern "C" {
// each major.minor branch, the commit count should increase monotonically.
//
// WUFFS_VERSION was overridden by "wuffs gen -version" based on revision
// 6a30c1ede0dc765fbbc443b2536b7691485be9fb committed on 2021-07-18.
// 77f855206d6b458ffe892dd5cf19150b1de63f24 committed on 2021-07-20.
#define WUFFS_VERSION 0x000030000
#define WUFFS_VERSION_MAJOR 0
#define WUFFS_VERSION_MINOR 3
#define WUFFS_VERSION_PATCH 0
#define WUFFS_VERSION_PRE_RELEASE_LABEL "beta.7"
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 3069
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 20210718
#define WUFFS_VERSION_STRING "0.3.0-beta.7+3069.20210718"
#define WUFFS_VERSION_PRE_RELEASE_LABEL "beta.8"
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 3072
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 20210720
#define WUFFS_VERSION_STRING "0.3.0-beta.8+3072.20210720"
// ---------------- Configuration
@@ -33097,16 +33097,19 @@ wuffs_json__decoder__decode_comment(
}
while (true) {
if (((uint64_t)(io2_a_src - iop_a_src)) <= 0) {
if (v_length > 0) {
if (a_src && a_src->meta.closed) {
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(4)) << WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) |
(((uint64_t)(v_length)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
self->private_impl.f_comment_type = 2;
status = wuffs_base__make_status(NULL);
goto ok;
} else if (v_length > 0) {
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(4)) << WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__CONTINUED__SHIFT) |
(((uint64_t)(v_length)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
}
if (a_src && a_src->meta.closed) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(6);
v_length = 0;
@@ -33114,10 +33117,9 @@ wuffs_json__decoder__decode_comment(
}
v_c = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
if (v_c == 10) {
iop_a_src += 1;
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(4)) << WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) |
(((uint64_t)((v_length + 1))) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
(((uint64_t)(v_length)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
self->private_impl.f_comment_type = 2;
status = wuffs_base__make_status(NULL);
goto ok;
+11 -6
View File
@@ -1530,7 +1530,16 @@ pri func decoder.decode_comment?(dst: base.token_writer, src: base.io_reader) {
pre args.dst.length() > 0,
{
if args.src.length() <= 0 {
if length > 0 {
if args.src.is_closed() {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: (base.TOKEN__VBC__FILLER << 21) |
base.TOKEN__VBD__FILLER__COMMENT_LINE,
continued: 0,
length: length)
this.comment_type = 2
return ok
} else if length > 0 {
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: (base.TOKEN__VBC__FILLER << 21) |
@@ -1538,9 +1547,6 @@ pri func decoder.decode_comment?(dst: base.token_writer, src: base.io_reader) {
continued: 1,
length: length)
}
if args.src.is_closed() {
return "#bad input"
}
yield? base."$short read"
length = 0
continue.comment_line
@@ -1548,13 +1554,12 @@ pri func decoder.decode_comment?(dst: base.token_writer, src: base.io_reader) {
c = args.src.peek_u8()
if c == '\n' {
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_simple_token_fast!(
value_major: 0,
value_minor: (base.TOKEN__VBC__FILLER << 21) |
base.TOKEN__VBD__FILLER__COMMENT_LINE,
continued: 0,
length: length + 1)
length: length)
this.comment_type = 2
return ok
}
+7 -5
View File
@@ -109,15 +109,17 @@ pub const QUIRK_ALLOW_COMMENT_BLOCK : base.u32 = 0x4909_9400 | 0x0B
// When this quirk is enabled, "// C/C++ style line comments\n" are accepted
// anywhere whitespace would be. See also QUIRK_ALLOW_TRAILING_FILLER.
//
// A line comment may not omit the ending "\n", even if there is no input
// afterwards (i.e. the prospective line comment ends with the end-of-file).
// Line comments are terminated by a '\n' byte. '\r' bytes are irrelevant. When
// combined with QUIRK_ALLOW_TRAILING_FILLER, a line comment at the end of the
// file may omit the final '\n' byte.
//
// They produce WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE tokens. The token
// chain's source bytes includes the starting "//" and the ending "\n".
// chain's source bytes includes the starting "//" but always excludes the
// ending "\n", regardless of whether it met the end of the file instead.
//
// Even if the line comments are on consecutive lines, each line comment is a
// separate token chain. There may be whitespace tokens between one line
// comment's ending "\n" and the next one's starting "//".
// separate token chain. Between them is at least one "\n" and maybe other
// whitespace, which are not part of a line comment token chain.
//
// To avoid ambiguity (as comments can contain new lines), this quirk cannot be
// combined with QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF.
+15 -5
View File
@@ -3427,6 +3427,14 @@ test_wuffs_json_decode_quirk_allow_trailing_comments() {
// - '4' means that there is non-filler after eof-or-'\n'
// - '5' means that there is non-filler before eof-or-'\n'
//
// The second and third bytes are digits such that the overall string starts
// with a three digit number, which is parsed as the top-level JSON value and
// everything afterwards is trailer.
//
// That three digit number modulo 100 also acts as a 'line number' (matching
// the tc loop variable), starting counting from zero, so that the 6th test
// case, tc == 5, is the line starting with "something hundred and five".
//
// WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER (together with
// WUFFS_JSON__QUIRK_ALLOW_COMMENT_ETC) should decode the '1's, '2's and '3's
// completely and the '4's and '5's up to but excluding the non-filler.
@@ -3443,11 +3451,13 @@ test_wuffs_json_decode_quirk_allow_trailing_comments() {
"306 /*foo*/ \n", //
"307 /*foo*/ \n\n", //
"308/*bar\nbaz*/\n\n", //
"309 // qux\n\n", //
"310 /*c0*/ /*c1*/\n\n", //
"311 /*c0*/ \n\n // c2 \n\n", //
"412 \n9", //
"513 9", //
"309 // qux", //
"310 // qux\n", //
"311 // qux\n\n", //
"312 /*c0*/ /*c1*/\n\n", //
"313 /*c0*/ \n\n // c2 \n\n", //
"414 \n9", //
"515 9", //
};
int tc;
Binary file not shown.