From 3be0a0b78a3a59d10a53ed08bbff04aa9cafbdad Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 23 Nov 2009 00:45:10 -0500 Subject: Support nested /* */ comments. --- dimension/tokenize.l | 46 ++++++++++++++++++++++------------------- tests/dimension/punctuation.pov | 4 +++- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/dimension/tokenize.l b/dimension/tokenize.l index 5ebd4ed..84b0694 100644 --- a/dimension/tokenize.l +++ b/dimension/tokenize.l @@ -17,7 +17,7 @@ * along with this program. If not, see . * *************************************************************************/ -%option reentrant yylineno noyywrap +%option reentrant stack yylineno noyywrap %{ #define YY_DECL static int yylex(const char *filename, dmnsn_array *tokens, \ @@ -81,15 +81,19 @@ size_t string_length; unsigned long wchar; %} -"/*" BEGIN(DMNSN_BLOCK_COMMENT); -"//" BEGIN(DMNSN_LINE_COMMENT); +(?# Comments) -"*/" BEGIN(INITIAL); -[^*\n]* ; +"/*" { + yy_push_state(DMNSN_BLOCK_COMMENT, yyscanner); +} +"*/" yy_pop_state(yyscanner); +[^*/\n]* ; +"/" ; "*" ; \n ; -\n BEGIN(INITIAL); +"//" yy_push_state(DMNSN_LINE_COMMENT, yyscanner); +\n yy_pop_state(yyscanner); [^\n]+ ; (?# Punctuation) @@ -143,36 +147,36 @@ unsigned long wchar; (?# Strings) -"\"" STRING_TOKEN(); BEGIN(DMNSN_STRING); +"\"" STRING_TOKEN(); yy_push_state(DMNSN_STRING, yyscanner); [^\\\"\n]* STRCAT(yytext, yyleng); -"\"" PUSH(); BEGIN(INITIAL); +"\"" PUSH(); yy_pop_state(yyscanner); (?# String escape sequences) -"\\" BEGIN(DMNSN_STRING_ESCAPE); -"a" STRCAT("\a", 1); BEGIN(DMNSN_STRING); -"b" STRCAT("\b", 1); BEGIN(DMNSN_STRING); -"f" STRCAT("\f", 1); BEGIN(DMNSN_STRING); -"n" STRCAT("\n", 1); BEGIN(DMNSN_STRING); -"r" STRCAT("\r", 1); BEGIN(DMNSN_STRING); -"t" STRCAT("\t", 1); BEGIN(DMNSN_STRING); -"v" STRCAT("\v", 1); BEGIN(DMNSN_STRING); -"\\" STRCAT("\\", 1); BEGIN(DMNSN_STRING); -"'" STRCAT("'", 1); BEGIN(DMNSN_STRING); -"\"" STRCAT("\"", 1); BEGIN(DMNSN_STRING); +"\\" yy_push_state(DMNSN_STRING_ESCAPE, yyscanner); +"a" STRCAT("\a", 1); yy_pop_state(yyscanner); +"b" STRCAT("\b", 1); yy_pop_state(yyscanner); +"f" STRCAT("\f", 1); yy_pop_state(yyscanner); +"n" STRCAT("\n", 1); yy_pop_state(yyscanner); +"r" STRCAT("\r", 1); yy_pop_state(yyscanner); +"t" STRCAT("\t", 1); yy_pop_state(yyscanner); +"v" STRCAT("\v", 1); yy_pop_state(yyscanner); +"\\" STRCAT("\\", 1); yy_pop_state(yyscanner); +"'" STRCAT("'", 1); yy_pop_state(yyscanner); +"\"" STRCAT("\"", 1); yy_pop_state(yyscanner); "u"[[:digit:]aAbBcCdDeEfF]{4} { wchar = strtoul(yytext + 1, NULL, 16); STRCAT("", 2); token.value[string_length - 2] = wchar/256; token.value[string_length - 1] = wchar%256; - BEGIN(DMNSN_STRING); + yy_pop_state(yyscanner); } . { dmnsn_diagnostic(filename, yylineno, yycolumn, "WARNING: unrecognised escape sequence '\\%c'", (int)*yytext); STRCAT(yytext, yyleng); - BEGIN(DMNSN_STRING); + yy_pop_state(yyscanner); } (?# Ignore whitespace) diff --git a/tests/dimension/punctuation.pov b/tests/dimension/punctuation.pov index 9e8a906..e68646b 100644 --- a/tests/dimension/punctuation.pov +++ b/tests/dimension/punctuation.pov @@ -17,6 +17,8 @@ * along with this program. If not, see . * *************************************************************************/ -// Test that we correctly tokenize all simple punctuation marks +/* Test that we correctly tokenize all simple punctuation marks + * // Also make sure we handle nested /* comments */ properly + */ {}()[]+-*/,;?:&.|=<>!<= >= != -- cgit v1.2.3