Code

frontend: Added support for single quotes in string constants.
authorSebastian Harl <sh@tokkee.org>
Sat, 23 Aug 2014 04:39:22 +0000 (21:39 -0700)
committerSebastian Harl <sh@tokkee.org>
Sat, 23 Aug 2014 04:39:22 +0000 (21:39 -0700)
Just like in SQL, single quotes may be included in a string by specifying two
adjacent single quotes.

src/frontend/scanner.l
t/unit/frontend/parser_test.c

index 5af85e047ec2913747f010638d884f9256cb1943..9d8e73d7d73cf01e8e80f552e8e90742cd345991 100644 (file)
@@ -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} {
index 9005811d500b68b6ce594fb7fcc861af2d85c449..bcd46af274c6bddd8a1d2bc6bbf8db27fcf9b2a0 100644 (file)
@@ -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 = "