From a4c5a92199249369f1f312f83f6ac3995c6c64f9 Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Sun, 17 May 2020 12:50:38 +0000 Subject: [PATCH] Support rasi config character type options (#1131) * style: remove extra space * feat: handle xrm_Char in config parser Handle the `xrm_Char` case in the (rasi theme) config file parser. This should properly handle configuration like ``` matching-negate-char: "\0"; ``` and ``` matching-negate-char: "-"; ``` * refactor: don't handle mem in xrm_Char case `mem` shouldn't ever be set when `XrmOption` is `type` `xrm_Char`. Therefore, there is no need to check it and free it. Remove that logic. * refactor: further condense logic * style: s/Everythin/Everything/ * style: s/parsing an section/parsing a section/ ...and missing period. * feat(lexer): add CHAR token Add a `CHAR` token that takes things of the form `''` or some specific backslash escape sequences like `'\''` and `'\0'`. For now, save it as a `T_STRING`. * refactor: define char property type * feat(parser): add cval and T_CHAR * refactor: use char property for xrm_Char Instead of using strings for property elements of type char, use characters, which were recently added to the grammar. --- include/rofi-types.h | 4 ++++ lexer/theme-lexer.l | 8 +++++--- lexer/theme-parser.y | 6 ++++++ source/rofi-types.c | 2 ++ source/xrmoptions.c | 10 +++++++++- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/rofi-types.h b/include/rofi-types.h index ced7e321..29fdca93 100644 --- a/include/rofi-types.h +++ b/include/rofi-types.h @@ -15,6 +15,8 @@ typedef enum P_DOUBLE, /** String */ P_STRING, + /** Character */ + P_CHAR, /** Boolean */ P_BOOLEAN, /** Color */ @@ -211,6 +213,8 @@ typedef union _PropertyValue double f; /** String */ char *s; + /** Character */ + char c; /** boolean */ gboolean b; /** Color */ diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l index 61b099f2..e8ca45ba 100644 --- a/lexer/theme-lexer.l +++ b/lexer/theme-lexer.l @@ -169,6 +169,7 @@ WSO [[:blank:]]* WORD [[:alnum:]-]+ COLOR_NAME [[:alpha:]]+ STRING \"{UANYN}*\" +CHAR \'({ASCN}|\\\\|\\\'|\\0)\' HEX [[:xdigit:]] NUMBER [[:digit:]] PNNUMBER [-+]?[[:digit:]]+ @@ -401,7 +402,7 @@ if ( queue == NULL ){ BEGIN(SECTION); return T_BOPEN; } - /** Everythin not yet parsed is an error. */ + /** Everything not yet parsed is an error. */ . { return T_ERROR_DEFAULTS; } @@ -411,13 +412,13 @@ if ( queue == NULL ){ BEGIN(NAMESTR); return T_NAME_PREFIX; } - /* Go into parsing an section*/ + /* Go into parsing a section. */ "\{" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(SECTION); return T_BOPEN; } - /* Pop out of parsing an section. */ + /* Pop out of parsing a section. */
"\}" { g_queue_pop_head ( queue ); BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue ))); @@ -441,6 +442,7 @@ if ( queue == NULL ){ {PNNUMBER}\.{NUMBER}+ { yylval->fval = g_ascii_strtod(yytext, NULL); return T_DOUBLE;} {PNNUMBER} { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;} {STRING} { yytext[yyleng-1] = '\0'; yylval->sval = g_strcompress(&yytext[1]); return T_STRING;} +{CHAR} { yytext[yyleng-1] = '\0'; yylval->cval = g_strcompress(&yytext[1])[0]; return T_CHAR;} @{WORD} { yylval->sval = g_strdup(yytext+1); diff --git a/lexer/theme-parser.y b/lexer/theme-parser.y index 8bc0b5fa..ed3fdb1c 100644 --- a/lexer/theme-parser.y +++ b/lexer/theme-parser.y @@ -138,6 +138,7 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b) int ival; double fval; char *sval; + char cval; int bval; WindowLocation wloc; ThemeColor colorval; @@ -160,6 +161,7 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b) %token T_INT "Integer number" %token T_DOUBLE "Floating-point number" %token T_STRING "UTF-8 encoded string" +%token T_CHAR "Character" %token T_PROP_NAME "property name" %token T_COLOR_NAME "Color value by name" %token T_NAME_ELEMENT "Element name" @@ -503,6 +505,10 @@ t_property_element $$ = rofi_theme_property_create ( P_STRING ); $$->value.s = $1; } +| T_CHAR { + $$ = rofi_theme_property_create ( P_CHAR ); + $$->value.c = $1; + } | T_LINK { $$ = rofi_theme_property_create ( P_LINK ); $$->value.link.name = $1; diff --git a/source/rofi-types.c b/source/rofi-types.c index 507c659a..690a18c4 100644 --- a/source/rofi-types.c +++ b/source/rofi-types.c @@ -10,6 +10,8 @@ const char * const PropertyTypeName[P_NUM_TYPES] = { "Double", /** String */ "String", + /** Character */ + "Character", /** Boolean */ "Boolean", /** Color */ diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 13058262..18b925f4 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -422,7 +422,7 @@ void config_parse_cmd_options ( void ) static gboolean __config_parser_set_property ( XrmOption *option, const Property *p, char **error ) { - if ( option->type == xrm_String ) { + if ( option->type == xrm_String ) { if ( p->type != P_STRING && p->type != P_LIST ) { *error = g_strdup_printf ( "Option: %s needs to be set with a string not a %s.", option->name, PropertyTypeName[p->type] ); return TRUE; @@ -477,6 +477,14 @@ static gboolean __config_parser_set_property ( XrmOption *option, const Property *( option->value.num ) = ( p->value.b ); option->source = CONFIG_FILE_THEME; } + else if ( option->type == xrm_Char ) { + if ( p->type != P_CHAR ) { + *error = g_strdup_printf ( "Option: %s needs to be set with a character not a %s.", option->name, PropertyTypeName[p->type] ); + return TRUE; + } + *( option->value.charc ) = ( p->value.c ); + option->source = CONFIG_FILE_THEME; + } else { // TODO add type *error = g_strdup_printf ( "Option: %s is not of a supported type: %s.", option->name, PropertyTypeName[p->type] );