From 9080ec820e692c718824464e206e1f8ec2752a51 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 11 Feb 2023 00:40:05 +0100 Subject: [Theme] Fix unary - and tighten the grammar parser. --- lexer/theme-lexer.l | 11 +++--- lexer/theme-parser.y | 64 ++++++++-------------------------- test/theme-parser-test.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 56 deletions(-) diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l index 7ae18da0..bf704546 100644 --- a/lexer/theme-lexer.l +++ b/lexer/theme-lexer.l @@ -184,8 +184,8 @@ STRING \"{UANYN}*\" STRING_LIST \"{UANYNP}*\" CHAR \'({ASCN}|\\\\|\\\'|\\0)\' HEX [[:xdigit:]] -NUMBER [[:digit:]] -PNNUMBER [-+]?[[:digit:]]+ +NUMBER [[:digit:]]+ +UNARYMIN - PX (px) MM (mm) EM (em) @@ -199,7 +199,6 @@ ASTERIX \* ENV $\{[[:alpha:]_][[:alnum:]_]*\} MODIFIER_ADD \+ -MODIFIER_SUBTRACT - MODIFIER_MULTIPLY \* MODIFIER_MIN (min) MODIFIER_MAX (max) @@ -510,8 +509,9 @@ if ( queue == NULL ) {
":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return T_PSEP; } ";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return T_PCLOSE;} (true|false) { yylval->bval= g_strcmp0(yytext, "true") == 0; return T_BOOLEAN;} -{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;} +{NUMBER}\.{NUMBER} { yylval->fval = g_ascii_strtod(yytext, NULL); return T_DOUBLE;} +{NUMBER} { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;} +{UNARYMIN} { return T_MIN; } {STRING} { yytext[yyleng-1] = '\0'; yylval->sval = g_strcompress(&yytext[1]); return T_STRING;} {STRING_LIST} { 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;} @@ -539,7 +539,6 @@ if ( queue == NULL ) { {INHERIT} { return T_INHERIT; } {MODIFIER_ADD} { return T_MODIFIER_ADD; } -{MODIFIER_SUBTRACT} { return T_MODIFIER_SUBTRACT; } {MODIFIER_MULTIPLY} { return T_MODIFIER_MULTIPLY; } {MODIFIER_MIN} { return T_MODIFIER_MIN; } {MODIFIER_MAX} { return T_MODIFIER_MAX; } diff --git a/lexer/theme-parser.y b/lexer/theme-parser.y index 08ef0199..dd317ea9 100644 --- a/lexer/theme-parser.y +++ b/lexer/theme-parser.y @@ -195,7 +195,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b ) %token T_UNIT_MM "mm" %token T_UNIT_EM "em" %token T_UNIT_CH "ch" -%token T_UNIT_PERCENT "%" %token T_ANGLE_DEG "Degrees" %token T_ANGLE_GRAD "Gradians" @@ -231,7 +230,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b ) %token T_PARENT_LEFT "Parent left ('(')" %token T_PARENT_RIGHT "Parent right (')')" %token T_COMMA "comma separator (',')" -%token T_OPTIONAL_COMMA "Optional comma separator (',')" %token T_FORWARD_SLASH "forward slash ('/')" %token T_PERCENT "Percent sign ('%')" @@ -239,7 +237,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b ) %token T_LIST_CLOSE "List close (']')" %token T_MODIFIER_ADD "Add ('+')" -%token T_MODIFIER_SUBTRACT "Subtract ('-')" %token T_MODIFIER_MULTIPLY "Multiply ('*')" %token T_MODIFIER_MAX "Max ('max')" @@ -257,7 +254,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b ) %token T_NSEP "Name separator (' ' or '.')" %token T_SSEP "Selector separator (',')" %token T_NAME_PREFIX "Element section ('# {name} { ... }')" -%token T_WHITESPACE "White space" %token T_PDEFAULTS "Default settings section ( '* { ... }')" %token T_CONFIGURATION "Configuration block" %token T_RESET_THEME "Reset Theme" @@ -266,13 +262,7 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b ) %token T_INHERIT "Inherit" -%token T_MEDIA_WIDTH "Width" -%token T_MEDIA_HEIGHT "Height" - -%token T_MEDIA_MIN "Min" -%token T_MEDIA_MONITOR_ID "Monitor-ID" -%token T_MEDIA_MAX "Max" -%token T_MEDIA_SEP "-" +%token T_MIN "-" %token T_VAR_START "var" @@ -313,6 +303,7 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b ) %type t_property_orientation %type t_property_cursor %type t_name_prefix_optional +%type t_property_number %start t_main %% @@ -771,15 +762,7 @@ t_property_highlight_style t_property_distance_zero -: T_INT t_property_line_style { - $$.base.distance = (double) $1; - $$.base.type = ROFI_PU_PX; - $$.base.left = NULL; - $$.base.right = NULL; - $$.base.modtype = ROFI_DISTANCE_MODIFIER_NONE; - $$.style = $2; -} -| T_DOUBLE t_property_line_style { +: t_property_number t_property_line_style { $$.base.distance = $1; $$.base.type = ROFI_PU_PX; $$.base.left = NULL; @@ -792,7 +775,7 @@ t_property_distance_zero /** Distance. */ t_property_distance_unit -: T_INT t_property_unit { +: t_property_number t_property_unit { $$ = g_slice_new0(RofiDistanceUnit); $$->distance = (double)$1; $$->type = $2; @@ -800,7 +783,7 @@ t_property_distance_unit $$->right = NULL; $$->modtype = ROFI_DISTANCE_MODIFIER_NONE; } -| T_INT { +| t_property_number { $$ = g_slice_new0(RofiDistanceUnit); $$->distance = (double)$1; $$->type = ROFI_PU_PX; @@ -808,22 +791,6 @@ t_property_distance_unit $$->right = NULL; $$->modtype = ROFI_DISTANCE_MODIFIER_NONE; } -| T_DOUBLE { - $$ = g_slice_new0(RofiDistanceUnit); - $$->distance = (double)$1; - $$->type = ROFI_PU_PX; - $$->left = NULL; - $$->right = NULL; - $$->modtype = ROFI_DISTANCE_MODIFIER_NONE; -} -| T_DOUBLE t_property_unit { - $$ = g_slice_new0(RofiDistanceUnit); - $$->distance = (double)$1; - $$->type = $2; - $$->left = NULL; - $$->right = NULL; - $$->modtype = ROFI_DISTANCE_MODIFIER_NONE; -} | T_PARENT_LEFT t_property_distance_unit_math3 T_PARENT_RIGHT { $$ = g_slice_new0(RofiDistanceUnit); $$->distance = 0; @@ -869,7 +836,7 @@ t_property_distance_unit_math2 $$->right = $3; $$->modtype = ROFI_DISTANCE_MODIFIER_ADD; } -| t_property_distance_unit_math2 T_MODIFIER_SUBTRACT t_property_distance_unit_math { +| t_property_distance_unit_math2 T_MIN t_property_distance_unit_math { $$ = g_slice_new0(RofiDistanceUnit); $$->left = $1; $$->right = $3; @@ -917,23 +884,14 @@ t_property_distance_unit_math3 t_property_distance /** Integer unit and line style */ -: T_INT t_property_unit t_property_line_style { - $$.base.distance = (double)$1; +: t_property_number t_property_unit t_property_line_style { + $$.base.distance = $1; $$.base.type = $2; $$.base.left = NULL; $$.base.right = NULL; $$.base.modtype = ROFI_DISTANCE_MODIFIER_NONE; $$.style = $3; } -/** Double unit and line style */ -| T_DOUBLE t_property_unit t_property_line_style { - $$.base.distance = (double)$1; - $$.base.type = $2; - $$.base.modtype = ROFI_DISTANCE_MODIFIER_NONE; - $$.base.left = NULL; - $$.base.right = NULL; - $$.style = $3; -} | T_CALC T_PARENT_LEFT t_property_distance_unit_math3 T_PARENT_RIGHT t_property_line_style { $$.base.distance = 0; $$.base.type = ROFI_PU_PX; @@ -943,6 +901,12 @@ t_property_distance $$.style = $5; }; +t_property_number +: T_INT { $$ = (double) $1; } +| T_DOUBLE { $$ = $1; } +| T_MIN T_INT { $$ = -(double)$2; } +| T_MIN T_DOUBLE { $$ = -$2; } + /** distance unit. px, em, % */ t_property_unit : T_UNIT_PX { $$ = ROFI_PU_PX; } diff --git a/test/theme-parser-test.c b/test/theme-parser-test.c index 036a4947..c413258f 100644 --- a/test/theme-parser-test.c +++ b/test/theme-parser-test.c @@ -1527,6 +1527,90 @@ START_TEST(test_prepare_math_failure2) { int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); ck_assert_int_eq(dist, -12); } +START_TEST(test_prepare_math_failure3) { + widget wid; + wid.name = "window"; + wid.state = ""; + rofi_theme_parse_string("window { width: calc(10+3);}"); + ck_assert_ptr_nonnull(rofi_theme); + // ck_assert_ptr_null ( rofi_theme->widgets ); + ck_assert_ptr_null(rofi_theme->properties); + ck_assert_ptr_null(rofi_theme->parent); + ck_assert_str_eq(rofi_theme->name, "Root"); + RofiDistance l = rofi_theme_get_distance(&wid, "width", 0); + int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); + ck_assert_int_eq(dist, 13); +} +START_TEST(test_prepare_math_failure4) { + widget wid; + wid.name = "window"; + wid.state = ""; + rofi_theme_parse_string("window { width: calc(10.0+3.2);}"); + ck_assert_ptr_nonnull(rofi_theme); + // ck_assert_ptr_null ( rofi_theme->widgets ); + ck_assert_ptr_null(rofi_theme->properties); + ck_assert_ptr_null(rofi_theme->parent); + ck_assert_str_eq(rofi_theme->name, "Root"); + RofiDistance l = rofi_theme_get_distance(&wid, "width", 0); + int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); + ck_assert_int_eq(dist, 13); +} +START_TEST(test_prepare_math_failure5) { + widget wid; + wid.name = "window"; + wid.state = ""; + rofi_theme_parse_string("window { width: calc(10-3);}"); + ck_assert_ptr_nonnull(rofi_theme); + // ck_assert_ptr_null ( rofi_theme->widgets ); + ck_assert_ptr_null(rofi_theme->properties); + ck_assert_ptr_null(rofi_theme->parent); + ck_assert_str_eq(rofi_theme->name, "Root"); + RofiDistance l = rofi_theme_get_distance(&wid, "width", 0); + int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); + ck_assert_int_eq(dist, 7); +} +START_TEST(test_prepare_math_failure6) { + widget wid; + wid.name = "window"; + wid.state = ""; + rofi_theme_parse_string("window { width: calc(10.0-3.2);}"); + ck_assert_ptr_nonnull(rofi_theme); + // ck_assert_ptr_null ( rofi_theme->widgets ); + ck_assert_ptr_null(rofi_theme->properties); + ck_assert_ptr_null(rofi_theme->parent); + ck_assert_str_eq(rofi_theme->name, "Root"); + RofiDistance l = rofi_theme_get_distance(&wid, "width", 0); + int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); + ck_assert_int_eq(dist, 6); +} +START_TEST(test_prepare_math_failure7) { + widget wid; + wid.name = "window"; + wid.state = ""; + rofi_theme_parse_string("window { width: calc(-10--3);}"); + ck_assert_ptr_nonnull(rofi_theme); + // ck_assert_ptr_null ( rofi_theme->widgets ); + ck_assert_ptr_null(rofi_theme->properties); + ck_assert_ptr_null(rofi_theme->parent); + ck_assert_str_eq(rofi_theme->name, "Root"); + RofiDistance l = rofi_theme_get_distance(&wid, "width", 0); + int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); + ck_assert_int_eq(dist, -7); +} +START_TEST(test_prepare_math_failure8) { + widget wid; + wid.name = "window"; + wid.state = ""; + rofi_theme_parse_string("window { width: calc(-10.0--3.2);}"); + ck_assert_ptr_nonnull(rofi_theme); + // ck_assert_ptr_null ( rofi_theme->widgets ); + ck_assert_ptr_null(rofi_theme->properties); + ck_assert_ptr_null(rofi_theme->parent); + ck_assert_str_eq(rofi_theme->name, "Root"); + RofiDistance l = rofi_theme_get_distance(&wid, "width", 0); + int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL); + ck_assert_int_eq(dist, -6); +} END_TEST START_TEST(test_prepare_default) { rofi_theme_parse_string("@import \"default\""); @@ -1770,6 +1854,12 @@ static Suite *theme_parser_suite(void) { tcase_add_test(tc_prepare_math, test_prepare_math_max); tcase_add_test(tc_prepare_math, test_prepare_math_failure); tcase_add_test(tc_prepare_math, test_prepare_math_failure2); + tcase_add_test(tc_prepare_math, test_prepare_math_failure3); + tcase_add_test(tc_prepare_math, test_prepare_math_failure4); + tcase_add_test(tc_prepare_math, test_prepare_math_failure5); + tcase_add_test(tc_prepare_math, test_prepare_math_failure6); + tcase_add_test(tc_prepare_math, test_prepare_math_failure7); + tcase_add_test(tc_prepare_math, test_prepare_math_failure8); suite_add_tcase(s, tc_prepare_math); } return s; -- cgit v1.2.3