| 1 | // re2c example
 | 
| 2 | //
 | 
| 3 | // Similar to https://re2c.org/manual/manual_c.html
 | 
| 4 | 
 | 
| 5 | // Demos:
 | 
| 6 | //
 | 
| 7 | // - Unreachable code - add unreachable
 | 
| 8 | // - Unhandled input - comment out * clause (more convincing in our huge lexer)
 | 
| 9 | 
 | 
| 10 | #include <assert.h>  // assert
 | 
| 11 | #include <stdio.h>  // printf
 | 
| 12 | 
 | 
| 13 | 
 | 
| 14 | /*!re2c
 | 
| 15 |   re2c:yyfill:enable = 0;
 | 
| 16 |   re2c:define:YYCTYPE = char;
 | 
| 17 | 
 | 
| 18 |   // Define rule for C-style strings with backslash escapes.
 | 
| 19 |   //
 | 
| 20 |   // \x00 is for the sentinel.  re2c warns you that the sentinel should not be
 | 
| 21 |   // matched in a pattern.
 | 
| 22 | 
 | 
| 23 |   favorite = ["] ( [^\x00"\\] | "\\" [^\x00] )* ["];
 | 
| 24 | 
 | 
| 25 |   unreachable = "\"\"";
 | 
| 26 | 
 | 
| 27 |   favorite2 = ["] ( [^\x00"\\] | "\\z" )* ["];
 | 
| 28 | */
 | 
| 29 | 
 | 
| 30 | bool MatchDoubleQuotedString(const char *s) {
 | 
| 31 |   const char *YYCURSOR = s;
 | 
| 32 |   const char *YYMARKER;   // depending on pattern, generated code may use this
 | 
| 33 | 
 | 
| 34 |   /*!re2c
 | 
| 35 |     favorite  { return true; }
 | 
| 36 |     *         { return false; }
 | 
| 37 |   */
 | 
| 38 | 
 | 
| 39 |   assert(0);
 | 
| 40 | }
 | 
| 41 | 
 | 
| 42 | int main(int argc, char **argv) {
 | 
| 43 |   char *s = argv[1];
 | 
| 44 |   bool matched = MatchDoubleQuotedString(s);
 | 
| 45 | 
 | 
| 46 |   if (matched) {
 | 
| 47 |     printf("YES   %s\n", s);
 | 
| 48 |   } else {
 | 
| 49 |     printf("NO    %s\n", s);
 | 
| 50 |   }
 | 
| 51 | 
 | 
| 52 |   return 0;
 | 
| 53 | }
 |