From 6396bc5da9d7d3d7dd3bff8b315bbe8db7987ef8 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 22 Aug 2014 21:39:22 -0700 Subject: [PATCH] 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. --- src/frontend/scanner.l | 15 ++++++++++++++- t/unit/frontend/parser_test.c | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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 = " -- 2.30.2