From a8a8906adc83cf6d0c32cb158fe4d6880717762a Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 17 Dec 2016 16:16:28 +0100 Subject: [PATCH] Improve theme parser on error. --- lexer/theme-lexer.l | 56 +++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l index 62f6dd9b..beebf1f3 100644 --- a/lexer/theme-lexer.l +++ b/lexer/theme-lexer.l @@ -6,6 +6,8 @@ #include "lexer/theme-parser.h" +int last_state = 0; +GQueue *queue = NULL; %} %{ @@ -25,13 +27,19 @@ NUMBER [0-9] %x PROPERTIES %x NAMESTR +%x ENTRY %% %{ YY_LLOC_START %} - -"//" { +%{ +if ( queue == NULL ){ +printf("queue create\n"); + queue = g_queue_new ( ); +} +%} +<*>"//" { int c; while ((c = input()) != EOF){ if (c == '\n') { @@ -43,7 +51,7 @@ YY_LLOC_START } YY_LLOC_START } -"/*" { +<*>"/*" { int c = 0, p; int nesting_depth = 1; while (nesting_depth) { @@ -66,20 +74,31 @@ YY_LLOC_START YY_LLOC_START } -"\{" { return BOPEN;} -"@" { BEGIN(NAMESTR);return CLASS_PREFIX;} -"#" { BEGIN(NAMESTR);return NAME_PREFIX;} -"\}" { return BCLOSE;} -"." { return NSEP; } -{WORD} { yylval->sval = g_strdup(yytext); return N_STRING;} -{WORD} { yylval->sval = g_strdup(yytext); return NAME_ELEMENT;} + /* Go into parsing an entry */ +"\{" { + g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); + BEGIN(ENTRY); + return BOPEN; +} + /* Pop out of parsing an entry. */ +"\}" { + BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue ))); + return BCLOSE; +} -{WHITESPACE} { BEGIN(INITIAL);} -{WHITESPACE}+ ; // ignore all whitespace +"@" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(NAMESTR);return CLASS_PREFIX;} +"#" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(NAMESTR);return NAME_PREFIX;} +"." { return NSEP; } +{WORD} { yylval->sval = g_strdup(yytext); return N_STRING;} +{WORD} { yylval->sval = g_strdup(yytext); return NAME_ELEMENT;} + + /* After Namestr/Classstr we want to go to state str, then to { */ +{WHITESPACE} { BEGIN(GPOINTER_TO_INT (g_queue_pop_head ( queue )));} +{WHITESPACE}+ ; // ignore all whitespace {WHITESPACE}+ ; // ignore all whitespace -":" { BEGIN(PROPERTIES); return PSEP; } -";" { BEGIN(0); return PCLOSE;} +":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return PSEP; } +";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return PCLOSE;} (true|false) { yylval->bval= g_strcmp0(yytext, "true") == 0; return T_BOOLEAN;} {NUMBER}+ { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;} {NUMBER}+\.{NUMBER}+ { yylval->fval = g_ascii_strtod(yytext, NULL); return T_DOUBLE;} @@ -133,7 +152,14 @@ YY_LLOC_START yylloc->last_line ++; }; -<*><> { +<> { + yyterminate(); + printf("Queue free: %d\n", g_queue_get_length(queue));; + g_queue_free ( queue ); +} +<*>. { + fprintf(stderr, "Invalid character: '%c'\n", *yytext); yyterminate(); } + %%