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
Nigel Tao e88e28259e wuffs gen -version=0.3.0-beta.7 2021-07-18 11:36:39 +10:00
Nigel Tao 6a30c1ede0 Inline wuffs_aux::DecodeJson work_buf variable 2021-07-18 11:25:46 +10:00
Nigel Tao 9c07523a84 Have wuffs_aux::DecodeJson consume trailing filler
Prior to this commit, wuffs_aux::DecodeJson would almost always consume
(optional) trailing filler, but in rare coincidences, it might not if a
wuffs_base__suspension__short_write coroutine suspension was needed
between the last token of the actual JSON value and the first token of
the filler after that JSON value.

This commit should change behavior only if the wuffs_aux::DecodeJson
caller had opted into WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER or
WUFFS_JSON__QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF.

Also remove an unnecessary std::move on a size_t typed value.
2021-07-18 11:12:41 +10:00
Nigel Tao 816475b285 Add "b" (for "binary") to fopen calls
Updates #50
2021-07-07 20:28:35 +10:00
15 changed files with 288 additions and 236 deletions

No files matched your search

+1 -1
View File
@@ -747,7 +747,7 @@ main1(int argc, char** argv) {
if (g_flags.remaining_argc > 1) {
return g_usage;
} else if (g_flags.remaining_argc == 1) {
in = fopen(g_flags.remaining_argv[0], "r");
in = fopen(g_flags.remaining_argv[0], "rb");
if (!in) {
return std::string("main: cannot read input file");
}
+1 -1
View File
@@ -94,7 +94,7 @@ load_image(const char* filename) {
FILE* file = stdin;
const char* adj_filename = "<stdin>";
if (filename) {
FILE* f = fopen(filename, "r");
FILE* f = fopen(filename, "rb");
if (f == NULL) {
printf("%s: could not open file\n", filename);
return false;
+1 -1
View File
@@ -339,7 +339,7 @@ main1(int argc, char** argv) {
if (g_flags.remaining_argc > 1) {
return g_usage;
} else if (g_flags.remaining_argc == 1) {
in = fopen(g_flags.remaining_argv[0], "r");
in = fopen(g_flags.remaining_argv[0], "rb");
if (!in) {
return std::string("main: cannot read input file");
}
+1 -1
View File
@@ -548,7 +548,7 @@ main1(int argc, char** argv) {
if (g_flags.remaining_argc > 1) {
return g_usage;
} else if (g_flags.remaining_argc == 1) {
in = fopen(g_flags.remaining_argv[0], "r");
in = fopen(g_flags.remaining_argv[0], "rb");
if (!in) {
return std::string("main: cannot read input file");
}
+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;
+67 -51
View File
@@ -40,54 +40,50 @@ const char DecodeJson_NoMatch[] = "wuffs_aux::DecodeJson: no match";
// --------
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN"; \
goto done; \
} \
wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8(); \
tok_status = dec->decode_tokens(&tok_buf, io_buf, work_buf); \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
goto done; \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
tok_status = \
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8()); \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
cursor_index += static_cast<size_t>(token_len)
// --------
@@ -358,6 +354,10 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
if (!dec) {
ret_error_message = "wuffs_aux::DecodeJson: out of memory";
goto done;
} else if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) {
ret_error_message =
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN";
goto done;
}
bool allow_tilde_n_tilde_r_tilde_t = false;
for (size_t i = 0; i < quirks.len; i++) {
@@ -373,7 +373,8 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
wuffs_base__token_buffer tok_buf =
wuffs_base__slice_token__writer(wuffs_base__make_slice_token(
&tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));
wuffs_base__status tok_status = wuffs_base__make_status(nullptr);
wuffs_base__status tok_status =
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8());
// Prepare other state.
uint32_t depth = 0;
@@ -387,7 +388,7 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
}
std::pair<std::string, size_t> split = DecodeJson_SplitJsonPointer(
json_pointer, i + 1, allow_tilde_n_tilde_r_tilde_t);
i = std::move(split.second);
i = split.second;
if (i == 0) {
ret_error_message = DecodeJson_BadJsonPointer;
goto done;
@@ -522,7 +523,22 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
goto done;
parsed_a_value:
if (!ret_error_message.empty() || (depth == 0)) {
// If an error was encountered, we are done. Otherwise, (depth == 0)
// after parsing a value is equivalent to having decoded the entire JSON
// value (for an empty json_pointer query) or having decoded the
// pointed-to JSON value (for a non-empty json_pointer query). In the
// latter case, we are also done.
//
// However, if quirks like WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER or
// WUFFS_JSON__QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF are passed, decoding
// the entire JSON value should also consume any trailing filler, in case
// the DecodeJson caller wants to subsequently check that the input is
// completely exhausted (and otherwise raise "valid JSON followed by
// further (unexpected) data"). We aren't done yet. Instead, keep the
// loop running until WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN's
// decode_tokens returns an ok status.
if (!ret_error_message.empty() ||
((depth == 0) && !json_pointer.empty())) {
goto done;
}
}
+12 -11
View File
@@ -857,10 +857,10 @@ const AuxImageHh = "" +
const AuxJsonCc = "" +
"// ---------------- Auxiliary - JSON\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__AUX__JSON)\n\n#include <utility>\n\nnamespace wuffs_aux {\n\nDecodeJsonResult::DecodeJsonResult(std::string&& error_message0,\n uint64_t cursor_position0)\n : error_message(std::move(error_message0)),\n cursor_position(cursor_position0) {}\n\nDecodeJsonCallbacks::~DecodeJsonCallbacks() {}\n\nvoid //\nDecodeJsonCallbacks::Done(DecodeJsonResult& result,\n sync_io::Input& input,\n IOBuffer& buffer) {}\n\nconst char DecodeJson_BadJsonPointer[] =\n \"wuffs_aux::DecodeJson: bad JSON Pointer\";\nconst char DecodeJson_NoMatch[] = \"wuffs_aux::DecodeJson: no match\";\n\n" +
"" +
"// --------\n\n#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \\\n while (tok_buf.meta.ri >= tok_buf.meta.wi) { \\\n if (tok_status.repr == nullptr) { \\\n } else if (tok_status.repr == wuffs_base__suspension__short_write) { \\\n tok_buf.compact(); \\\n } else if (tok_status.repr == wuffs_base__suspension__short_read) { \\\n if (!io_error_message.empty()) { \\\n ret_error_message = std::move(io_error_message); \\\n goto done; \\\n } else if (cursor_index != io_buf->meta.ri) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: bad cursor_index\"; \\\n goto done; \\\n } else if (io_buf->meta.closed" +
") { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: io_buf is closed\"; \\\n goto done; \\\n } \\\n io_buf->compact(); \\\n if (io_buf->meta.wi >= io_buf->data.len) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: io_buf is full\"; \\\n goto done; \\\n } \\\n cursor_index = io_buf->meta.ri; \\\n io_error_message = input.CopyIn(io_buf); \\\n } else { \\\n ret_" +
"error_message = tok_status.message(); \\\n goto done; \\\n } \\\n if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN\"; \\\n goto done; \\\n } \\\n wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8(); \\\n tok_status = dec->decode_tokens(&tok_buf, io_buf, work_buf); \\\n } \\\n wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \\\n uint64_t token_len = token.length(); \\\n if ((io_buf->meta.ri < cursor_index) || " +
" \\\n ((io_buf->meta.ri - cursor_index) < token_len)) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: bad token indexes\"; \\\n goto done; \\\n } \\\n uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \\\n (void)(token_ptr); \\\n cursor_index += static_cast<size_t>(token_len)\n\n" +
"// --------\n\n#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \\\n while (tok_buf.meta.ri >= tok_buf.meta.wi) { \\\n if (tok_status.repr == nullptr) { \\\n goto done; \\\n } else if (tok_status.repr == wuffs_base__suspension__short_write) { \\\n tok_buf.compact(); \\\n } else if (tok_status.repr == wuffs_base__suspension__short_read) { \\\n if (!io_error_message.empty()) { \\\n ret_error_message = std::move(io_error_message); \\\n goto done; \\\n } else if (cursor_index != io_buf->meta.ri) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: bad cursor_index\"; " +
" \\\n goto done; \\\n } else if (io_buf->meta.closed) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: io_buf is closed\"; \\\n goto done; \\\n } \\\n io_buf->compact(); \\\n if (io_buf->meta.wi >= io_buf->data.len) { \\\n ret_error_message = \\\n \"wuffs_aux::DecodeJson: internal error: io_buf is full\"; \\\n goto done; \\\n } \\\n cursor_index = io_buf->meta.ri; \\\n i" +
"o_error_message = input.CopyIn(io_buf); \\\n } else { \\\n ret_error_message = tok_status.message(); \\\n goto done; \\\n } \\\n tok_status = \\\n dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8()); \\\n } \\\n wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \\\n uint64_t token_len = token.length(); \\\n if ((io_buf->meta.ri < cursor_index) || \\\n ((io_buf->meta.ri - cursor_index) < token_len)) { \\\n ret_error_message = \\\n \"wuffs_au" +
"x::DecodeJson: internal error: bad token indexes\"; \\\n goto done; \\\n } \\\n uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \\\n (void)(token_ptr); \\\n cursor_index += static_cast<size_t>(token_len)\n\n" +
"" +
"// --------\n\nnamespace {\n\n// DecodeJson_SplitJsonPointer returns (\"bar\", 8) for (\"/foo/bar/b~1z/qux\", 5,\n// etc). It returns a 0 size_t when s has invalid JSON Pointer syntax.\n//\n// The string returned is unescaped. If calling it again, this time with i=8,\n// the \"b~1z\" substring would be returned as \"b/z\".\nstd::pair<std::string, size_t> //\nDecodeJson_SplitJsonPointer(std::string& s,\n size_t i,\n bool allow_tilde_n_tilde_r_tilde_t) {\n std::string fragment;\n while (i < s.size()) {\n char c = s[i];\n if (c == '/') {\n break;\n } else if (c != '~') {\n fragment.push_back(c);\n i++;\n continue;\n }\n i++;\n if (i >= s.size()) {\n return std::make_pair(std::string(), 0);\n }\n c = s[i];\n if (c == '0') {\n fragment.push_back('~');\n i++;\n continue;\n } else if (c == '1') {\n fragment.push_back('/');\n i++;\n continue;\n } else if (allow_tilde_n_tilde_r_tilde_t) {\n if (c == 'n') {\n " +
"fragment.push_back('\\n');\n i++;\n continue;\n } else if (c == 'r') {\n fragment.push_back('\\r');\n i++;\n continue;\n } else if (c == 't') {\n fragment.push_back('\\t');\n i++;\n continue;\n }\n }\n return std::make_pair(std::string(), 0);\n }\n return std::make_pair(std::move(fragment), i);\n}\n\n" +
@@ -873,13 +873,14 @@ const AuxJsonCc = "" +
"__GET_THE_NEXT_TOKEN;\n\n int64_t vbc = token.value_base_category();\n uint64_t vbd = token.value_base_detail();\n if (vbc == WUFFS_BASE__TOKEN__VBC__FILLER) {\n continue;\n }\n\n // Undo the last part of WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN, so that\n // we're only peeking at the next token.\n tok_buf.meta.ri--;\n cursor_index -= static_cast<size_t>(token_len);\n\n if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&\n (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) {\n return DecodeJson_NoMatch;\n }\n return \"\";\n } // check_that_a_value_follows\n\nfail:\n return \"wuffs_aux::DecodeJson: internal error: unexpected token\";\ndone:\n return ret_error_message;\n}\n\n} // namespace\n\n" +
"" +
"// --------\n\nDecodeJsonResult //\nDecodeJson(DecodeJsonCallbacks& callbacks,\n sync_io::Input& input,\n wuffs_base__slice_u32 quirks,\n std::string json_pointer) {\n // Prepare the wuffs_base__io_buffer and the resultant error_message.\n wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();\n wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();\n std::unique_ptr<uint8_t[]> fallback_io_array(nullptr);\n if (!io_buf) {\n fallback_io_array = std::unique_ptr<uint8_t[]>(new uint8_t[4096]);\n fallback_io_buf = wuffs_base__ptr_u8__writer(fallback_io_array.get(), 4096);\n io_buf = &fallback_io_buf;\n }\n // cursor_index is discussed at\n // https://nigeltao.github.io/blog/2020/jsonptr.html#the-cursor-index\n size_t cursor_index = 0;\n std::string ret_error_message;\n std::string io_error_message;\n\n do {\n // Prepare the low-level JSON decoder.\n wuffs_json__decoder::unique_ptr dec = wuffs_json__decoder::alloc();\n if (!dec) {\n ret_error_message = " +
"\"wuffs_aux::DecodeJson: out of memory\";\n goto done;\n }\n bool allow_tilde_n_tilde_r_tilde_t = false;\n for (size_t i = 0; i < quirks.len; i++) {\n dec->set_quirk_enabled(quirks.ptr[i], true);\n if (quirks.ptr[i] ==\n WUFFS_JSON__QUIRK_JSON_POINTER_ALLOW_TILDE_N_TILDE_R_TILDE_T) {\n allow_tilde_n_tilde_r_tilde_t = true;\n }\n }\n\n // Prepare the wuffs_base__tok_buffer. 256 tokens is 2KiB.\n wuffs_base__token tok_array[256];\n wuffs_base__token_buffer tok_buf =\n wuffs_base__slice_token__writer(wuffs_base__make_slice_token(\n &tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));\n wuffs_base__status tok_status = wuffs_base__make_status(nullptr);\n\n // Prepare other state.\n uint32_t depth = 0;\n std::string str;\n\n // Walk the (optional) JSON Pointer.\n for (size_t i = 0; i < json_pointer.size();) {\n if (json_pointer[i] != '/') {\n ret_error_message = DecodeJson_BadJsonPointer;\n goto done;\n }\n std::pair<" +
"std::string, size_t> split = DecodeJson_SplitJsonPointer(\n json_pointer, i + 1, allow_tilde_n_tilde_r_tilde_t);\n i = std::move(split.second);\n if (i == 0) {\n ret_error_message = DecodeJson_BadJsonPointer;\n goto done;\n }\n ret_error_message = DecodeJson_WalkJsonPointerFragment(\n tok_buf, tok_status, dec, io_buf, io_error_message, cursor_index,\n input, split.first);\n if (!ret_error_message.empty()) {\n goto done;\n }\n }\n\n // Loop, doing these two things:\n // 1. Get the next token.\n // 2. Process that token.\n while (true) {\n WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;\n\n int64_t vbc = token.value_base_category();\n uint64_t vbd = token.value_base_detail();\n switch (vbc) {\n case WUFFS_BASE__TOKEN__VBC__FILLER:\n continue;\n\n case WUFFS_BASE__TOKEN__VBC__STRUCTURE: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {\n ret_error_message = callbacks.Push(static_cas" +
"t<uint32_t>(vbd));\n if (!ret_error_message.empty()) {\n goto done;\n }\n depth++;\n continue;\n }\n ret_error_message = callbacks.Pop(static_cast<uint32_t>(vbd));\n depth--;\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__STRING: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) {\n // No-op.\n } else if (vbd &\n WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {\n const char* ptr = // Convert from (uint8_t*).\n static_cast<const char*>(static_cast<void*>(token_ptr));\n str.append(ptr, static_cast<size_t>(token_len));\n } else {\n goto fail;\n }\n if (token.continued()) {\n continue;\n }\n ret_error_message = callbacks.AppendTextString(std::move(str));\n str.clear();\n goto parsed_a_value;\n }\n\n case WUFFS_" +
"BASE__TOKEN__VBC__UNICODE_CODE_POINT: {\n uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];\n size_t n = wuffs_base__utf_8__encode(\n wuffs_base__make_slice_u8(\n &u[0], WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),\n static_cast<uint32_t>(vbd));\n const char* ptr = // Convert from (uint8_t*).\n static_cast<const char*>(static_cast<void*>(&u[0]));\n str.append(ptr, n);\n if (token.continued()) {\n continue;\n }\n goto fail;\n }\n\n case WUFFS_BASE__TOKEN__VBC__LITERAL: {\n ret_error_message =\n (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__NULL)\n ? callbacks.AppendNull()\n : callbacks.AppendBool(vbd &\n WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE);\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__NUMBER: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT) {\n " +
" if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED) {\n wuffs_base__result_i64 r = wuffs_base__parse_number_i64(\n wuffs_base__make_slice_u8(token_ptr,\n static_cast<size_t>(token_len)),\n WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);\n if (r.status.is_ok()) {\n ret_error_message = callbacks.AppendI64(r.value);\n goto parsed_a_value;\n }\n }\n if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT) {\n wuffs_base__result_f64 r = wuffs_base__parse_number_f64(\n wuffs_base__make_slice_u8(token_ptr,\n static_cast<size_t>(token_len)),\n WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);\n if (r.status.is_ok()) {\n ret_error_message = callbacks.AppendF64(r.value);\n goto parsed_a_value;\n }" +
"\n }\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0xFFF0000000000000ul));\n goto parsed_a_value;\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0x7FF0000000000000ul));\n goto parsed_a_value;\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0xFFFFFFFFFFFFFFFFul));\n goto parsed_a_value;\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee" +
"_754_bit_representation__from_u64_to_f64(\n 0x7FFFFFFFFFFFFFFFul));\n goto parsed_a_value;\n }\n goto fail;\n }\n }\n\n fail:\n ret_error_message =\n \"wuffs_aux::DecodeJson: internal error: unexpected token\";\n goto done;\n\n parsed_a_value:\n if (!ret_error_message.empty() || (depth == 0)) {\n goto done;\n }\n }\n } while (false);\n\ndone:\n DecodeJsonResult result(\n std::move(ret_error_message),\n wuffs_base__u64__sat_add(io_buf->meta.pos, cursor_index));\n callbacks.Done(result, input, *io_buf);\n return result;\n}\n\n#undef WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN\n\n} // namespace wuffs_aux\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__AUX__JSON)\n" +
"\"wuffs_aux::DecodeJson: out of memory\";\n goto done;\n } else if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) {\n ret_error_message =\n \"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN\";\n goto done;\n }\n bool allow_tilde_n_tilde_r_tilde_t = false;\n for (size_t i = 0; i < quirks.len; i++) {\n dec->set_quirk_enabled(quirks.ptr[i], true);\n if (quirks.ptr[i] ==\n WUFFS_JSON__QUIRK_JSON_POINTER_ALLOW_TILDE_N_TILDE_R_TILDE_T) {\n allow_tilde_n_tilde_r_tilde_t = true;\n }\n }\n\n // Prepare the wuffs_base__tok_buffer. 256 tokens is 2KiB.\n wuffs_base__token tok_array[256];\n wuffs_base__token_buffer tok_buf =\n wuffs_base__slice_token__writer(wuffs_base__make_slice_token(\n &tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));\n wuffs_base__status tok_status =\n dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8());\n\n // Prepare other state.\n uint32_t depth = 0;\n std::string str;" +
"\n\n // Walk the (optional) JSON Pointer.\n for (size_t i = 0; i < json_pointer.size();) {\n if (json_pointer[i] != '/') {\n ret_error_message = DecodeJson_BadJsonPointer;\n goto done;\n }\n std::pair<std::string, size_t> split = DecodeJson_SplitJsonPointer(\n json_pointer, i + 1, allow_tilde_n_tilde_r_tilde_t);\n i = split.second;\n if (i == 0) {\n ret_error_message = DecodeJson_BadJsonPointer;\n goto done;\n }\n ret_error_message = DecodeJson_WalkJsonPointerFragment(\n tok_buf, tok_status, dec, io_buf, io_error_message, cursor_index,\n input, split.first);\n if (!ret_error_message.empty()) {\n goto done;\n }\n }\n\n // Loop, doing these two things:\n // 1. Get the next token.\n // 2. Process that token.\n while (true) {\n WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN;\n\n int64_t vbc = token.value_base_category();\n uint64_t vbd = token.value_base_detail();\n switch (vbc) {\n case WUFFS_" +
"BASE__TOKEN__VBC__FILLER:\n continue;\n\n case WUFFS_BASE__TOKEN__VBC__STRUCTURE: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH) {\n ret_error_message = callbacks.Push(static_cast<uint32_t>(vbd));\n if (!ret_error_message.empty()) {\n goto done;\n }\n depth++;\n continue;\n }\n ret_error_message = callbacks.Pop(static_cast<uint32_t>(vbd));\n depth--;\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__STRING: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) {\n // No-op.\n } else if (vbd &\n WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {\n const char* ptr = // Convert from (uint8_t*).\n static_cast<const char*>(static_cast<void*>(token_ptr));\n str.append(ptr, static_cast<size_t>(token_len));\n } else {\n goto fail;\n }\n " +
"if (token.continued()) {\n continue;\n }\n ret_error_message = callbacks.AppendTextString(std::move(str));\n str.clear();\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: {\n uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];\n size_t n = wuffs_base__utf_8__encode(\n wuffs_base__make_slice_u8(\n &u[0], WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),\n static_cast<uint32_t>(vbd));\n const char* ptr = // Convert from (uint8_t*).\n static_cast<const char*>(static_cast<void*>(&u[0]));\n str.append(ptr, n);\n if (token.continued()) {\n continue;\n }\n goto fail;\n }\n\n case WUFFS_BASE__TOKEN__VBC__LITERAL: {\n ret_error_message =\n (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__NULL)\n ? callbacks.AppendNull()\n : callbacks.AppendBool(vbd &\n " +
" WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE);\n goto parsed_a_value;\n }\n\n case WUFFS_BASE__TOKEN__VBC__NUMBER: {\n if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT) {\n if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED) {\n wuffs_base__result_i64 r = wuffs_base__parse_number_i64(\n wuffs_base__make_slice_u8(token_ptr,\n static_cast<size_t>(token_len)),\n WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);\n if (r.status.is_ok()) {\n ret_error_message = callbacks.AppendI64(r.value);\n goto parsed_a_value;\n }\n }\n if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT) {\n wuffs_base__result_f64 r = wuffs_base__parse_number_f64(\n wuffs_base__make_slice_u8(token_ptr,\n static_cast<size_t>(token_len)),\n " +
" WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);\n if (r.status.is_ok()) {\n ret_error_message = callbacks.AppendF64(r.value);\n goto parsed_a_value;\n }\n }\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0xFFF0000000000000ul));\n goto parsed_a_value;\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0x7FF0000000000000ul));\n goto parsed_a_value;\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0xF" +
"FFFFFFFFFFFFFFFul));\n goto parsed_a_value;\n } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN) {\n ret_error_message = callbacks.AppendF64(\n wuffs_base__ieee_754_bit_representation__from_u64_to_f64(\n 0x7FFFFFFFFFFFFFFFul));\n goto parsed_a_value;\n }\n goto fail;\n }\n }\n\n fail:\n ret_error_message =\n \"wuffs_aux::DecodeJson: internal error: unexpected token\";\n goto done;\n\n parsed_a_value:\n // If an error was encountered, we are done. Otherwise, (depth == 0)\n // after parsing a value is equivalent to having decoded the entire JSON\n // value (for an empty json_pointer query) or having decoded the\n // pointed-to JSON value (for a non-empty json_pointer query). In the\n // latter case, we are also done.\n //\n // However, if quirks like WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER or\n // WUFFS_JSON__QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF are pass" +
"ed, decoding\n // the entire JSON value should also consume any trailing filler, in case\n // the DecodeJson caller wants to subsequently check that the input is\n // completely exhausted (and otherwise raise \"valid JSON followed by\n // further (unexpected) data\"). We aren't done yet. Instead, keep the\n // loop running until WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN's\n // decode_tokens returns an ok status.\n if (!ret_error_message.empty() ||\n ((depth == 0) && !json_pointer.empty())) {\n goto done;\n }\n }\n } while (false);\n\ndone:\n DecodeJsonResult result(\n std::move(ret_error_message),\n wuffs_base__u64__sat_add(io_buf->meta.pos, cursor_index));\n callbacks.Done(result, input, *io_buf);\n return result;\n}\n\n#undef WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN\n\n} // namespace wuffs_aux\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__AUX__JSON)\n" +
""
const AuxJsonHh = "" +
+76 -58
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;
@@ -40053,54 +40055,50 @@ const char DecodeJson_NoMatch[] = "wuffs_aux::DecodeJson: no match";
// --------
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN"; \
goto done; \
} \
wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8(); \
tok_status = dec->decode_tokens(&tok_buf, io_buf, work_buf); \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
goto done; \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
tok_status = \
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8()); \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
cursor_index += static_cast<size_t>(token_len)
// --------
@@ -40371,6 +40369,10 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
if (!dec) {
ret_error_message = "wuffs_aux::DecodeJson: out of memory";
goto done;
} else if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) {
ret_error_message =
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN";
goto done;
}
bool allow_tilde_n_tilde_r_tilde_t = false;
for (size_t i = 0; i < quirks.len; i++) {
@@ -40386,7 +40388,8 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
wuffs_base__token_buffer tok_buf =
wuffs_base__slice_token__writer(wuffs_base__make_slice_token(
&tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));
wuffs_base__status tok_status = wuffs_base__make_status(nullptr);
wuffs_base__status tok_status =
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8());
// Prepare other state.
uint32_t depth = 0;
@@ -40400,7 +40403,7 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
}
std::pair<std::string, size_t> split = DecodeJson_SplitJsonPointer(
json_pointer, i + 1, allow_tilde_n_tilde_r_tilde_t);
i = std::move(split.second);
i = split.second;
if (i == 0) {
ret_error_message = DecodeJson_BadJsonPointer;
goto done;
@@ -40535,7 +40538,22 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
goto done;
parsed_a_value:
if (!ret_error_message.empty() || (depth == 0)) {
// If an error was encountered, we are done. Otherwise, (depth == 0)
// after parsing a value is equivalent to having decoded the entire JSON
// value (for an empty json_pointer query) or having decoded the
// pointed-to JSON value (for a non-empty json_pointer query). In the
// latter case, we are also done.
//
// However, if quirks like WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER or
// WUFFS_JSON__QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF are passed, decoding
// the entire JSON value should also consume any trailing filler, in case
// the DecodeJson caller wants to subsequently check that the input is
// completely exhausted (and otherwise raise "valid JSON followed by
// further (unexpected) data"). We aren't done yet. Instead, keep the
// loop running until WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN's
// decode_tokens returns an ok status.
if (!ret_error_message.empty() ||
((depth == 0) && !json_pointer.empty())) {
goto done;
}
}
+81 -63
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
// 3842c519c9961500d96efee0ff7aafbd20833570 committed on 2021-07-05.
// 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.6"
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 3065
#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 20210705
#define WUFFS_VERSION_STRING "0.3.0-beta.6+3065.20210705"
#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;
@@ -40054,54 +40056,50 @@ const char DecodeJson_NoMatch[] = "wuffs_aux::DecodeJson: no match";
// --------
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN"; \
goto done; \
} \
wuffs_base__slice_u8 work_buf = wuffs_base__empty_slice_u8(); \
tok_status = dec->decode_tokens(&tok_buf, io_buf, work_buf); \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
goto done; \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact(); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
tok_status = \
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8()); \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
cursor_index += static_cast<size_t>(token_len)
// --------
@@ -40372,6 +40370,10 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
if (!dec) {
ret_error_message = "wuffs_aux::DecodeJson: out of memory";
goto done;
} else if (WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE != 0) {
ret_error_message =
"wuffs_aux::DecodeJson: internal error: bad WORKBUF_LEN";
goto done;
}
bool allow_tilde_n_tilde_r_tilde_t = false;
for (size_t i = 0; i < quirks.len; i++) {
@@ -40387,7 +40389,8 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
wuffs_base__token_buffer tok_buf =
wuffs_base__slice_token__writer(wuffs_base__make_slice_token(
&tok_array[0], (sizeof(tok_array) / sizeof(tok_array[0]))));
wuffs_base__status tok_status = wuffs_base__make_status(nullptr);
wuffs_base__status tok_status =
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8());
// Prepare other state.
uint32_t depth = 0;
@@ -40401,7 +40404,7 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
}
std::pair<std::string, size_t> split = DecodeJson_SplitJsonPointer(
json_pointer, i + 1, allow_tilde_n_tilde_r_tilde_t);
i = std::move(split.second);
i = split.second;
if (i == 0) {
ret_error_message = DecodeJson_BadJsonPointer;
goto done;
@@ -40536,7 +40539,22 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
goto done;
parsed_a_value:
if (!ret_error_message.empty() || (depth == 0)) {
// If an error was encountered, we are done. Otherwise, (depth == 0)
// after parsing a value is equivalent to having decoded the entire JSON
// value (for an empty json_pointer query) or having decoded the
// pointed-to JSON value (for a non-empty json_pointer query). In the
// latter case, we are also done.
//
// However, if quirks like WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER or
// WUFFS_JSON__QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF are passed, decoding
// the entire JSON value should also consume any trailing filler, in case
// the DecodeJson caller wants to subsequently check that the input is
// completely exhausted (and otherwise raise "valid JSON followed by
// further (unexpected) data"). We aren't done yet. Instead, keep the
// loop running until WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN's
// decode_tokens returns an ok status.
if (!ret_error_message.empty() ||
((depth == 0) && !json_pointer.empty())) {
goto done;
}
}
+1 -1
View File
@@ -261,7 +261,7 @@ process_file(char* filename) {
g_file = NULL;
}
g_filename = filename;
g_file = fopen(g_filename, "r");
g_file = fopen(g_filename, "rb");
if (!g_file) {
fprintf(stderr, "main: could not open %s\n", g_filename);
return false;
+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;
+1 -1
View File
@@ -671,7 +671,7 @@ read_file(wuffs_base__io_buffer* dst, const char* path) {
original_path);
}
FILE* f = fopen(path, "r");
FILE* f = fopen(path, "rb");
if (!f) {
RETURN_FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno),
errno);
Binary file not shown.