diff --git a/README.md b/README.md index 3234142..79e5ea0 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ You’re not alone: I also wish the code were documented. `cfg` has no comments, but let’s pretend we had `#` comments. ``` -# lists start with a name, then an indent, then a hyphen, +# lists start with a name, then an indent, then a hyphen, a space, # and then a value my_list - -"value" - -12 + - "value" + - 12 # strings are quoted my_string "this is a string" @@ -31,7 +31,7 @@ my_section my_second_numer 23.0 ``` -Keys cannot contain spaces. Indents are always two spaces. There is no space +Keys cannot contain spaces. Indents are always two spaces. There is one space between the hyphen and the value and an array. This is all. It is a simple format, some might think it is too simple. It is, however, diff --git a/cfg.h b/cfg.h index 98c5982..2212f55 100644 --- a/cfg.h +++ b/cfg.h @@ -425,9 +425,9 @@ string config_value_str(config_value* c, size_t indent) { for (i = 0; i < c->l->len; i++) { string_cappend(&res, "\n"); for (j = 0; j < indent; j++) string_cappend(&res, " "); - string_cappend(&res, " -"); + string_cappend(&res, " - "); val = list_nth_config_value(c->l, i); - sub = config_value_str(&val, 0); + sub = config_value_str(&val, indent+2); string_append(&res, &sub); free_string(sub); } @@ -571,6 +571,9 @@ parsed_value config_parse_list(string* s, size_t* line, size_t* col, size_t inde *l =new_list_config_value(); while(true) { + if (s->str[0] != ' ') return config_value_parse_error("expected space to start list element", *line, *col); + s->str++; + consumed += 1; elem = config_parse_value(s, line, col, indent); if (!elem.ok) return elem; @@ -579,6 +582,7 @@ parsed_value config_parse_list(string* s, size_t* line, size_t* col, size_t inde consumed += elem.consumed; s->str += elem.consumed; + if (s->str[0] == '\0') break; if (!config_parse_line(s, line, col)) return config_value_parse_error("expected newline", *line, *col); ind = config_parse_indent(s, col); diff --git a/example.cfg b/example.cfg index a68d73b..ec21c60 100644 --- a/example.cfg +++ b/example.cfg @@ -3,3 +3,9 @@ x 1.0 hi name 1 val "hi" + list + - "value" + - -1 + - + - "this" + - "obj"