From: Sebastian Harl Date: Sat, 23 Aug 2014 04:39:22 +0000 (-0700) Subject: frontend: Added support for single quotes in string constants. X-Git-Tag: sysdb-0.4.0~9 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=6396bc5da9d7d3d7dd3bff8b315bbe8db7987ef8;p=sysdb.git frontend: Added support for single quotes in string constants. Just like in SQL, single quotes may be included in a string by specifying two adjacent single quotes. --- diff --git a/src/frontend/scanner.l b/src/frontend/scanner.l index 5af85e0..9d8e73d 100644 --- a/src/frontend/scanner.l +++ b/src/frontend/scanner.l @@ -102,7 +102,7 @@ csc_end \*\/ */ identifier ([A-Za-z_][A-Za-z_0-9$]*) /* TODO: fully support SQL strings */ -string ('[^']*') +string ('([^']|'')*') /* * Numeric constants. @@ -146,8 +146,21 @@ time ([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2}(\.[0-9]{1,9})?)?) return IDENTIFIER; } {string} { + char *quot; + size_t len; + + /* remove the leading and trailing quote */ yytext[yyleng - 1] = '\0'; yylval->str = strdup(yytext + 1); + + quot = yylval->str; + len = yyleng - 2; + while ((quot = strstr(quot, "''")) != NULL) { + memmove(quot, quot + 1, len - (quot - yylval->str) - 1); + yylval->str[len - 1] = '\0'; + --len; + ++quot; + } return STRING; } {integer} { diff --git a/t/unit/frontend/parser_test.c b/t/unit/frontend/parser_test.c index 9005811..bcd46af 100644 --- a/t/unit/frontend/parser_test.c +++ b/t/unit/frontend/parser_test.c @@ -109,6 +109,18 @@ START_TEST(test_parse) { "TIMESERIES " "'host'.'metric'", -1, 1, CONNECTION_TIMESERIES }, + /* string constants */ + { "LOOKUP hosts MATCHING " + "host = ''''", -1, 1, CONNECTION_LOOKUP }, + { "LOOKUP hosts MATCHING " + "host = '''foo'", -1, 1, CONNECTION_LOOKUP }, + { "LOOKUP hosts MATCHING " + "host = 'f''oo'", -1, 1, CONNECTION_LOOKUP }, + { "LOOKUP hosts MATCHING " + "host = 'foo'''", -1, 1, CONNECTION_LOOKUP }, + { "LOOKUP hosts MATCHING " + "host = '''", -1, -1, 0 }, + /* numeric constants */ { "LOOKUP hosts MATCHING " "attribute.foo = "