Last active
February 26, 2020 17:11
-
-
Save awave1/054243d6242ebf287ad9ec76fb323f70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %{ | |
| #include ... // your includes | |
| // ensure that string_builder is initialized within the scanner, otherwise reading string tokens will fail | |
| static string_builder_t *sb = sb_init(); | |
| %} | |
| ...the rest of your lexer stuff | |
| %% | |
| <STRING>{ | |
| \" { | |
| BEGIN(INITIAL); | |
| if (!sb_is_empty(sb)) { | |
| yytext = sb_build_str(sb); | |
| sb_reset(sb); | |
| } | |
| return yy::Parser::token::T_STR; | |
| } | |
| {string_char} { | |
| if (YYText()[0] != '\0') { | |
| sb_append(sb, YYText()); | |
| } | |
| } | |
| \\t { sb_append(sb, yytext); } | |
| \\n { sb_append(sb, yytext); } | |
| \\r { sb_append(sb, yytext); } | |
| \\b { sb_append(sb, yytext); } | |
| \\f { sb_append(sb, yytext); } | |
| \\' { sb_append(sb, yytext); } | |
| \\\" { sb_append(sb, yytext); } | |
| \\ { sb_append(sb, yytext); } | |
| <<EOF>> { | |
| // in case if string is unterminated and there's EOF | |
| fprintf(stderr, "unterminated string on line %d\n", yylineno); | |
| BEGIN(INITIAL); | |
| warning_count++; | |
| } | |
| {newline} { | |
| fprintf(stderr, "unterminated string on line %d\n", yylineno); | |
| BEGIN(INITIAL); | |
| warning_count++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment