added MIO_JSON_LINE_COMMENT
This commit is contained in:
parent
c42427d6ed
commit
a7ae566736
@ -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], "--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], "--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);
|
mio = mio_open(MIO_NULL, 0, MIO_NULL, 512, MIO_NULL);
|
||||||
|
@ -133,6 +133,7 @@ static int push_read_state (mio_json_t* json, mio_json_state_t state)
|
|||||||
ss->state = state;
|
ss->state = state;
|
||||||
ss->level = json->state_stack->level; /* copy from the parent */
|
ss->level = json->state_stack->level; /* copy from the parent */
|
||||||
ss->index = 0;
|
ss->index = 0;
|
||||||
|
ss->in_comment = 0;
|
||||||
ss->next = json->state_stack;
|
ss->next = json->state_stack;
|
||||||
|
|
||||||
json->state_stack = ss;
|
json->state_stack = ss;
|
||||||
@ -470,14 +471,18 @@ static int handle_start_char (mio_json_t* json, mio_ooci_t c)
|
|||||||
/* do nothing */
|
/* do nothing */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if ((json->option & MIO_JSON_LINE_COMMENT) && c == '#')
|
||||||
|
{
|
||||||
|
/* line comment */
|
||||||
|
json->state_stack->in_comment = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if (c == '\"')
|
else if (c == '\"')
|
||||||
{
|
{
|
||||||
if (push_read_state(json, MIO_JSON_STATE_IN_STRING_VALUE) <= -1) return -1;
|
if (push_read_state(json, MIO_JSON_STATE_IN_STRING_VALUE) <= -1) return -1;
|
||||||
clear_token (json);
|
clear_token (json);
|
||||||
return 1; /* the quote dosn't form a string. so no start-over */
|
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 == '-')
|
else if (mio_is_ooch_digit(c) || c == '+' || c == '-')
|
||||||
{
|
{
|
||||||
if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1;
|
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 */
|
/* do nothing */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if ((json->option & MIO_JSON_LINE_COMMENT) && c == '#')
|
||||||
|
{
|
||||||
|
/* line comment */
|
||||||
|
json->state_stack->in_comment = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if (c == ']')
|
else if (c == ']')
|
||||||
{
|
{
|
||||||
pop_read_state (json);
|
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);
|
clear_token (json);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* TOOD: else if (c == '#') MIO radixed number
|
|
||||||
*/
|
|
||||||
else if (mio_is_ooch_digit(c) || c == '+' || c == '-')
|
else if (mio_is_ooch_digit(c) || c == '+' || c == '-')
|
||||||
{
|
{
|
||||||
if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1;
|
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 */
|
/* do nothing */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
else if ((json->option & MIO_JSON_LINE_COMMENT) && c == '#')
|
||||||
|
{
|
||||||
|
/* line comment */
|
||||||
|
json->state_stack->in_comment = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
else if (c == '}')
|
else if (c == '}')
|
||||||
{
|
{
|
||||||
/* 0 - initial, 1 - got key, 2 -> got colon, 3 -> got value, 0 -> after comma */
|
/* 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);
|
clear_token (json);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* TOOD: else if (c == '#') MIO radixed number
|
|
||||||
*/
|
|
||||||
else if (mio_is_ooch_digit(c) || c == '+' || c == '-')
|
else if (mio_is_ooch_digit(c) || c == '+' || c == '-')
|
||||||
{
|
{
|
||||||
if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1;
|
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++;
|
c = *ptr++;
|
||||||
#endif
|
#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 (json->state_stack->state == MIO_JSON_STATE_START && mio_is_ooch_space(c)) continue; /* skip white space */
|
||||||
|
|
||||||
if (stop_if_ever_completed && ever_completed)
|
if (stop_if_ever_completed && ever_completed)
|
||||||
|
@ -83,6 +83,7 @@ struct mio_json_state_node_t
|
|||||||
mio_json_state_t state;
|
mio_json_state_t state;
|
||||||
mio_oow_t level;
|
mio_oow_t level;
|
||||||
mio_oow_t index;
|
mio_oow_t index;
|
||||||
|
int in_comment;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
struct
|
struct
|
||||||
@ -124,7 +125,12 @@ enum mio_json_option_t
|
|||||||
{
|
{
|
||||||
/* allow an unquoted word as an object key */
|
/* allow an unquoted word as an object key */
|
||||||
MIO_JSON_PERMIT_WORD_KEY = ((mio_bitmask_t)1 << 0),
|
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;
|
typedef enum mio_json_option_t mio_json_option_t;
|
||||||
|
Loading…
Reference in New Issue
Block a user