diff --git a/mio/bin/t03.c b/mio/bin/t03.c index e67b751..e9aff40 100644 --- a/mio/bin/t03.c +++ b/mio/bin/t03.c @@ -158,6 +158,7 @@ int main (int argc, char* argv[]) { if (strcmp(argv[i], "--permit-word-key") == 0) o |= MIO_JSON_PERMIT_WORD_KEY; if (strcmp(argv[i], "--optional-comma") == 0) o |= MIO_JSON_OPTIONAL_COMMA; + if (strcmp(argv[i], "--line-comment") == 0) o |= MIO_JSON_LINE_COMMENT; } mio = mio_open(MIO_NULL, 0, MIO_NULL, 512, MIO_NULL); diff --git a/mio/lib/json.c b/mio/lib/json.c index 1c0f883..3d36e6a 100644 --- a/mio/lib/json.c +++ b/mio/lib/json.c @@ -133,6 +133,7 @@ static int push_read_state (mio_json_t* json, mio_json_state_t state) ss->state = state; ss->level = json->state_stack->level; /* copy from the parent */ ss->index = 0; + ss->in_comment = 0; ss->next = json->state_stack; json->state_stack = ss; @@ -470,14 +471,18 @@ static int handle_start_char (mio_json_t* json, mio_ooci_t c) /* do nothing */ return 1; } + else if ((json->option & MIO_JSON_LINE_COMMENT) && c == '#') + { + /* line comment */ + json->state_stack->in_comment = 1; + return 1; + } else if (c == '\"') { if (push_read_state(json, MIO_JSON_STATE_IN_STRING_VALUE) <= -1) return -1; clear_token (json); return 1; /* the quote dosn't form a string. so no start-over */ } - /* TOOD: else if (c == '#') MIO radixed number - */ else if (mio_is_ooch_digit(c) || c == '+' || c == '-') { if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1; @@ -515,6 +520,12 @@ static int handle_char_in_array (mio_json_t* json, mio_ooci_t c) /* do nothing */ return 1; } + else if ((json->option & MIO_JSON_LINE_COMMENT) && c == '#') + { + /* line comment */ + json->state_stack->in_comment = 1; + return 1; + } else if (c == ']') { pop_read_state (json); @@ -552,8 +563,6 @@ static int handle_char_in_array (mio_json_t* json, mio_ooci_t c) clear_token (json); return 1; } - /* TOOD: else if (c == '#') MIO radixed number - */ else if (mio_is_ooch_digit(c) || c == '+' || c == '-') { if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1; @@ -592,6 +601,12 @@ static int handle_char_in_object (mio_json_t* json, mio_ooci_t c) /* do nothing */ return 1; } + else if ((json->option & MIO_JSON_LINE_COMMENT) && c == '#') + { + /* line comment */ + json->state_stack->in_comment = 1; + return 1; + } else if (c == '}') { /* 0 - initial, 1 - got key, 2 -> got colon, 3 -> got value, 0 -> after comma */ @@ -650,8 +665,6 @@ static int handle_char_in_object (mio_json_t* json, mio_ooci_t c) clear_token (json); return 1; } - /* TOOD: else if (c == '#') MIO radixed number - */ else if (mio_is_ooch_digit(c) || c == '+' || c == '-') { if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1; @@ -785,6 +798,11 @@ static int feed_json_data (mio_json_t* json, const mio_bch_t* data, mio_oow_t le c = *ptr++; #endif + if (json->state_stack->in_comment) + { + if (c == '\n') json->state_stack->in_comment = 0; + continue; + } if (json->state_stack->state == MIO_JSON_STATE_START && mio_is_ooch_space(c)) continue; /* skip white space */ if (stop_if_ever_completed && ever_completed) diff --git a/mio/lib/mio-json.h b/mio/lib/mio-json.h index 266af83..c559561 100644 --- a/mio/lib/mio-json.h +++ b/mio/lib/mio-json.h @@ -83,6 +83,7 @@ struct mio_json_state_node_t mio_json_state_t state; mio_oow_t level; mio_oow_t index; + int in_comment; union { struct @@ -124,7 +125,12 @@ enum mio_json_option_t { /* allow an unquoted word as an object key */ MIO_JSON_PERMIT_WORD_KEY = ((mio_bitmask_t)1 << 0), - MIO_JSON_OPTIONAL_COMMA = ((mio_bitmask_t)1 << 1) + + /* a comma as a separator is not mandatory */ + MIO_JSON_OPTIONAL_COMMA = ((mio_bitmask_t)1 << 1), + + /* support the line comment. the text beginning with # is a comment to the end of the line */ + MIO_JSON_LINE_COMMENT = ((mio_bitmask_t)1 << 2) }; typedef enum mio_json_option_t mio_json_option_t;