Add --strip-comments option

This commit is contained in:
liquidaty authored and Mattias Wadman committed 2024-02-24 18:58:53 +01:00
1 parent e659a5e508
commit 8b479ca229
5 files changed
+24 -7

No files matched your search

+2 -1
View File
@@ -141,7 +141,7 @@ endif
### Tests (make check) ### Tests (make check)
TESTS = tests/mantest tests/jqtest tests/shtest tests/utf8test tests/base64test TESTS = tests/mantest tests/jqtest tests/shtest tests/utf8test tests/base64test tests/commenttest
if !WIN32 if !WIN32
TESTS += tests/optionaltest TESTS += tests/optionaltest
endif endif
@@ -233,6 +233,7 @@ EXTRA_DIST = $(DOC_FILES) $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) \
tests/setup tests/torture/input0.json \ tests/setup tests/torture/input0.json \
tests/optional.test tests/man.test tests/manonig.test \ tests/optional.test tests/man.test tests/manonig.test \
tests/jq.test tests/onig.test tests/base64.test \ tests/jq.test tests/onig.test tests/base64.test \
tests/commenttest \
tests/utf8-truncate.jq tests/jq-f-test.sh \ tests/utf8-truncate.jq tests/jq-f-test.sh \
tests/no-main-program.jq tests/yes-main-program.jq tests/no-main-program.jq tests/yes-main-program.jq
+1
View File
@@ -234,6 +234,7 @@ enum {
JV_PARSE_SEQ = 1, JV_PARSE_SEQ = 1,
JV_PARSE_STREAMING = 2, JV_PARSE_STREAMING = 2,
JV_PARSE_STREAM_ERRORS = 4, JV_PARSE_STREAM_ERRORS = 4,
JV_PARSE_STRIP_COMMENTS = 8
}; };
jv jv_parse(const char* string); jv jv_parse(const char* string);
+1 -6
View File
@@ -652,7 +652,6 @@ static pfunc scan_line_comment(struct jv_parser* p, char ch, jv* out) {
return OK; return OK;
} }
static pfunc scan_c_comment_close(struct jv_parser* p, char ch, jv* out) { static pfunc scan_c_comment_close(struct jv_parser* p, char ch, jv* out) {
if(ch == '/') { if(ch == '/') {
p->scan = scan_json; p->scan = scan_json;
@@ -703,11 +702,7 @@ static pfunc scan_json(struct jv_parser* p, char ch, jv* out) {
presult answer = 0; presult answer = 0;
p->last_ch_was_ws = 0; p->last_ch_was_ws = 0;
if (p->st == JV_PARSER_NORMAL) { if (p->st == JV_PARSER_NORMAL) {
if(ch == '#') { if(ch == '/' && (p->flags & JV_PARSE_STRIP_COMMENTS)) {
p->scan = scan_line_comment;
return OK;
}
if(ch == '/') {
p->scan = scan_slash_comment; p->scan = scan_slash_comment;
return OK; return OK;
} }
+5
View File
@@ -94,6 +94,7 @@ static void usage(int code, int keep_it_short) {
" --stream-errors implies --stream and report parse error as\n" " --stream-errors implies --stream and report parse error as\n"
" an array;\n" " an array;\n"
" --seq parse input/output as application/json-seq;\n" " --seq parse input/output as application/json-seq;\n"
" --strip-comments accept and strip comments from input;\n"
" -f, --from-file file load filter from the file;\n" " -f, --from-file file load filter from the file;\n"
" -L directory search modules from the directory;\n" " -L directory search modules from the directory;\n"
" --arg name value set $name to the string value;\n" " --arg name value set $name to the string value;\n"
@@ -471,6 +472,10 @@ int main(int argc, char* argv[]) {
parser_flags |= JV_PARSE_STREAMING; parser_flags |= JV_PARSE_STREAMING;
continue; continue;
} }
if (isoption(argv[i], 0, "strip-comments", &short_opts)) {
parser_flags |= JV_PARSE_STRIP_COMMENTS;
continue;
}
if (isoption(argv[i], 0, "stream-errors", &short_opts)) { if (isoption(argv[i], 0, "stream-errors", &short_opts)) {
parser_flags |= JV_PARSE_STREAMING | JV_PARSE_STREAM_ERRORS; parser_flags |= JV_PARSE_STREAMING | JV_PARSE_STREAM_ERRORS;
continue; continue;
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
. "${0%/*}/setup" "$@"
if [ "$(echo '{"hi":"there"/*comment*/}' | $VALGRIND $Q $JQ --strip-comments '.hi')" != '"there"' ]; then
echo "C-style comment test failed"
exit 1
fi
if [ "$(printf '{"hi"://comment\n"there"}' | $VALGRIND $Q $JQ --strip-comments '.hi')" != '"there"' ]; then
echo "C++-style comment test failed"
exit 1
fi
exit 0