From 63da417be1dfe1366a4a8984770c06cbbadcda39 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Mon, 23 May 2011 11:04:36 +0300 Subject: git: fix parser Accept ; as a comment starter. Accept partially or fully quoted values. Support the backslash escapes allowed by Git. * src/git-parser.c (input): semicolon starts a comment, too. (collect_substring): New function. (collect_string): Rewrite via collect_substring. (collect_value): New function. (getvalue): Use collect_value. --- src/git-parser.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/git-parser.c b/src/git-parser.c index 117a8fc..37eeab7 100644 --- a/src/git-parser.c +++ b/src/git-parser.c @@ -61,7 +61,7 @@ static int input() { rawinput(); - if (input_char == '#') { + if (input_char == '#' || input_char == ';') { while (rawinput() && input_char != '\n') ; } @@ -96,7 +96,7 @@ collect_tag() } static void -collect_string() +collect_substring() { while (rawinput()) { if (input_char == '\\') { @@ -105,10 +105,26 @@ collect_string() "unexpected EOF in string"); break; } + switch (input_char) { + case 'n': + input_char = '\n'; + break; + case 't': + input_char = '\t'; + break; + case 'b': + input_char = '\b'; + } } else if (input_char == '"') break; grecs_txtacc_grow_char(acc, input_char); } +} + +static void +collect_string() +{ + collect_substring(); grecs_txtacc_grow_char(acc, 0); tok.tag = grecs_txtacc_finish(acc, 0); } @@ -184,6 +200,35 @@ gettoken(void) tok.type = input_char; } +static void +collect_value() +{ + do { + if (input_char == '"') { + collect_substring(); + if (input_char == '"') + continue; + else + break; + } + if (input_char == '\\') { + if (!rawinput()) + break; + switch (input_char) { + case 'n': + input_char = '\n'; + break; + case 't': + input_char = '\t'; + break; + case 'b': + input_char = '\b'; + } + } + grecs_txtacc_grow_char(acc, input_char); + } while (input() && input_char != '\n'); +} + static struct grecs_value * getvalue() { @@ -192,9 +237,7 @@ getvalue() while (input() && ISSPACE(input_char) && input_char != '\n') ; if (input_char != '\n') - do - grecs_txtacc_grow_char(acc, input_char); - while (input() && input_char != '\n'); + collect_value(); grecs_txtacc_grow_char(acc, 0); tok.type = TOK_VALUE; tok.buf = grecs_txtacc_finish(acc, 1); -- cgit v1.2.1