Cleanup lexer a bit

This commit is contained in:
Dave Davenport 2017-03-13 10:49:33 +01:00
parent be036f086d
commit 1ca69704db
2 changed files with 42 additions and 22 deletions

View File

@ -8,6 +8,8 @@
#include "rofi.h" #include "rofi.h"
#include "lexer/theme-parser.h" #include "lexer/theme-parser.h"
#define LOG_DOMAIN "Parser"
int last_state = 0; int last_state = 0;
/** /**
@ -32,13 +34,15 @@ typedef struct _ParseObject {
char *filename; char *filename;
/** Length of string */ /** Length of string */
int str_len; size_t str_len;
/** String */ /** String */
const char *input_str; const char *input_str;
/** Position in file */ /** Position in file */
YYLTYPE location; YYLTYPE location;
} ParseObject; } ParseObject;
GList *prev_imported_files = NULL;
GQueue *file_queue = NULL; GQueue *file_queue = NULL;
GQueue *queue = NULL; GQueue *queue = NULL;
@ -125,7 +129,7 @@ INCLUDE "@import"
%x INCLUDE %x INCLUDE
%x PROPERTIES %x PROPERTIES
%x NAMESTR %x NAMESTR
%x ENTRY %x SECTION
%x DEFAULTS %x DEFAULTS
%% %%
@ -138,6 +142,11 @@ if ( queue == NULL ){
} }
%} %}
/**
* General code for handling comments.
* Both C and C++ style comments, including nexting.
*/
<*>"//" { <*>"//" {
int c; int c;
while ((c = input()) != 0){ while ((c = input()) != 0){
@ -180,8 +189,10 @@ if ( queue == NULL ){
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(INCLUDE); BEGIN(INCLUDE);
} }
/** Skip all whitespace */
<INCLUDE>{WHITESPACE} {} <INCLUDE>{WHITESPACE} {}
/** Parse path. Last element in this INCLUDE */
<INCLUDE>\"{STRING}\" { <INCLUDE>\"{STRING}\" {
yytext[yyleng-1] = '\0'; yytext[yyleng-1] = '\0';
ParseObject *top = g_queue_peek_head ( file_queue ); ParseObject *top = g_queue_peek_head ( file_queue );
@ -195,6 +206,7 @@ if ( queue == NULL ){
filename = path; filename = path;
g_free ( basedir ); g_free ( basedir );
} }
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Parsing file: '%s'", filename );
FILE *f = fopen ( filename, "rb" ); FILE *f = fopen ( filename, "rb" );
if ( f ) { if ( f ) {
top->location = *yylloc; top->location = *yylloc;
@ -218,47 +230,59 @@ if ( queue == NULL ){
// Pop out of include. */ // Pop out of include. */
BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue ))); BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue )));
} }
/** Everythin not yet parsed is an error. */
<INCLUDE>. {
return T_ERROR_INCLUDE;
}
/** /**
* END INCLUDES * END INCLUDES
*/ */
/**
* Handle defaults: * { ... }
*/
<INITIAL>{ASTERIX} { <INITIAL>{ASTERIX} {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(DEFAULTS); BEGIN(DEFAULTS);
return PDEFAULTS; return PDEFAULTS;
} }
/** Skip all whitespace */
<DEFAULTS>{WHITESPACE} {} <DEFAULTS>{WHITESPACE} {}
<DEFAULTS>"\{" { <DEFAULTS>"\{" {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(ENTRY); BEGIN(SECTION);
return BOPEN; return BOPEN;
} }
/** Everythin not yet parsed is an error. */
<DEFAULTS>. {
return T_ERROR_DEFAULTS;
}
/* Go into parsing an entry */ <INITIAL>"#" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(NAMESTR);return NAME_PREFIX;}
/* Go into parsing an section*/
<NAMESTR>"\{" { <NAMESTR>"\{" {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(ENTRY); BEGIN(SECTION);
return BOPEN; return BOPEN;
} }
/* Pop out of parsing an entry. */ /* Pop out of parsing an section. */
<ENTRY>"\}" { <SECTION>"\}" {
g_queue_pop_head ( queue ); g_queue_pop_head ( queue );
BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue ))); BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue )));
return BCLOSE; return BCLOSE;
} }
<INITIAL>"#" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(NAMESTR);return NAME_PREFIX;}
<NAMESTR>\.|{WHITESPACE} { return NSEP; } <NAMESTR>\.|{WHITESPACE} { return NSEP; }
<ENTRY>{WORD} { yylval->sval = g_strdup(yytext); return N_STRING;} <SECTION>{WORD} { yylval->sval = g_strdup(yytext); return N_STRING;}
<NAMESTR>{WORD} { yylval->sval = g_strdup(yytext); return NAME_ELEMENT;} <NAMESTR>{WORD} { yylval->sval = g_strdup(yytext); return NAME_ELEMENT;}
/* After Namestr/Classstr we want to go to state str, then to { */ /* After Namestr/Classstr we want to go to state str, then to { */
/*<NAMESTR>{WHITESPACE} { BEGIN(GPOINTER_TO_INT (g_queue_pop_head ( queue )));}*/ <INITIAL,SECTION>{WHITESPACE}+ ; // ignore all whitespace
<INITIAL,ENTRY>{WHITESPACE}+ ; // ignore all whitespace
<PROPERTIES>{WHITESPACE}+ ; // ignore all whitespace <PROPERTIES>{WHITESPACE}+ ; // ignore all whitespace
<INITIAL,ENTRY>":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return PSEP; } <SECTION>":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return PSEP; }
<PROPERTIES>";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return PCLOSE;} <PROPERTIES>";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return PCLOSE;}
<PROPERTIES>(true|false) { yylval->bval= g_strcmp0(yytext, "true") == 0; return T_BOOLEAN;} <PROPERTIES>(true|false) { yylval->bval= g_strcmp0(yytext, "true") == 0; return T_BOOLEAN;}
<PROPERTIES>{PNNUMBER}+ { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;} <PROPERTIES>{PNNUMBER}+ { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;}
@ -453,8 +477,8 @@ if ( queue == NULL ){
<INITIAL>. { <INITIAL>. {
return T_ERROR; return T_ERROR;
} }
<ENTRY>. { <SECTION>. {
return T_ERROR_ENTRY; return T_ERROR_SECTION;
} }
<PROPERTIES>. { <PROPERTIES>. {
return T_ERROR_PROPERTY; return T_ERROR_PROPERTY;
@ -462,12 +486,6 @@ if ( queue == NULL ){
<NAMESTR>. { <NAMESTR>. {
return T_ERROR_NAMESTRING; return T_ERROR_NAMESTRING;
} }
<DEFAULTS>. {
return T_ERROR_DEFAULTS;
}
<INCLUDE>. {
return T_ERROR_INCLUDE;
}
%% %%
gboolean rofi_theme_parse_file ( const char *file ) gboolean rofi_theme_parse_file ( const char *file )
@ -491,6 +509,7 @@ gboolean rofi_theme_parse_file ( const char *file )
po->filein = yyin; po->filein = yyin;
current = po; current = po;
g_queue_push_head ( file_queue, po ); g_queue_push_head ( file_queue, po );
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Parsing top file: '%s'", filename );
int parser_retv = yyparse ( file ); int parser_retv = yyparse ( file );
yylex_destroy (); yylex_destroy ();
@ -516,6 +535,7 @@ gboolean rofi_theme_parse_string ( const char *string )
po->str_len = strlen(string); po->str_len = strlen(string);
current = po; current = po;
g_queue_push_head ( file_queue, po ); g_queue_push_head ( file_queue, po );
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Parsing string: '%s'", string );
int parser_retv = yyparse ( string ); int parser_retv = yyparse ( string );
yylex_destroy (); yylex_destroy ();

View File

@ -35,7 +35,7 @@ int yylex (YYSTYPE *, YYLTYPE *);
%token <ival> T_END 0 "end of file" %token <ival> T_END 0 "end of file"
%token <ival> T_ERROR 1 "error from file parser" %token <ival> T_ERROR 1 "error from file parser"
%token <ival> T_ERROR_PROPERTY 2 "invalid property value" %token <ival> T_ERROR_PROPERTY 2 "invalid property value"
%token <ival> T_ERROR_ENTRY 3 "invalid property name" %token <ival> T_ERROR_SECTION 3 "invalid property name"
%token <ival> T_ERROR_NAMESTRING 4 "invalid element name" %token <ival> T_ERROR_NAMESTRING 4 "invalid element name"
%token <ival> T_ERROR_DEFAULTS 5 "invalid defaults name" %token <ival> T_ERROR_DEFAULTS 5 "invalid defaults name"
%token <ival> T_ERROR_INCLUDE 6 "invalid import value" %token <ival> T_ERROR_INCLUDE 6 "invalid import value"