[Grammar] Add support for env();

Add support for `env(ENV,default)`.

Fixes: #1411
This commit is contained in:
Dave Davenport 2021-09-01 21:27:25 +02:00
parent 6715c59682
commit 68e79b66a9
4 changed files with 88 additions and 14 deletions

View File

@ -470,6 +470,23 @@ window {
}
```
* Format: `var(PROPERTY NAME, DEFAULT)`
A reference can point to another reference. Currently, the maximum number of redirects is 20.
A property always refers to another property. It cannot be used for a subpart of the property.
Example:
```css
window {
width: var( width, 30%);
}
```
If the property `width` is set globally (`*{}`) that value is used, if the property
`width` is not set, the default value is used.
## Orientation
* Format: `(horizontal|vertical)`
@ -502,6 +519,20 @@ The environment variable should be an alphanumeric string without white-space.
}
```
* Format: `env(ENVIRONMENT, default)`
This will parse the environment variable as the property value. (that then can be any of the above types).
The environment variable should be an alphanumeric string without white-space.
If the environment value is not found, the default value is used.
```css
window {
width: env(WIDTH, 40%);
}
```
If environment WIDTH is set, then that value is parsed, otherwise the default value (`40%`).
## Inherit
* Format: `inherit`

View File

@ -273,6 +273,7 @@ LIST_OPEN \[
LIST_CLOSE \]
VAR_START "var"
ENV_START "env"
CPP_COMMENT "//"
C_COMMENT_OPEN "/*"
@ -290,6 +291,7 @@ CONFIGURATION (?i:configuration)
%x PROPERTIES
%x PROPERTIES_ENV
%x PROPERTIES_VAR
%x PROPERTIES_ENV_VAR
%x PROPERTIES_VAR_DEFAULT
%x PROPERTIES_LIST
%x NAMESTR
@ -491,7 +493,7 @@ if ( queue == NULL ) {
/* After Namestr/Classstr we want to go to state str, then to { */
<INITIAL,SECTION>{WHITESPACE}+ ; // ignore all whitespace
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,PROPERTIES_LIST,PROPERTIES_VAR,MEDIA_CONTENT>{WHITESPACE}+ ; // ignore all whitespace
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,PROPERTIES_LIST,PROPERTIES_ENV_VAR,PROPERTIES_VAR,MEDIA_CONTENT>{WHITESPACE}+ ; // ignore all whitespace
<SECTION>":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return T_PSEP; }
<PROPERTIES>";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return T_PCLOSE;}
@ -550,6 +552,26 @@ if ( queue == NULL ) {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES_ENV);
}
}
<PROPERTIES_ENV_VAR>{WORD} {
const char *val = g_getenv(yytext);
if ( val ) {
ParseObject *top = g_queue_peek_head ( file_queue );
top->location = *yylloc;
ParseObject *po = g_malloc0(sizeof(ParseObject));
po->type = PT_ENV;
po->input_str = val;
po->str_len = strlen(val);
current = po;
g_queue_push_head ( file_queue, po );
yypush_buffer_state (yy_create_buffer ( 0, YY_BUF_SIZE ));
yylloc->first_line = yylloc->last_line = 1;
yylloc->first_column = yylloc->last_column = 1;
yylloc->filename = current->filename;
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES_ENV);
}
}
/**
* Color parsing. It is easier to do this at lexer level.
@ -610,17 +632,26 @@ if ( queue == NULL ) {
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{HSL} { return T_COL_HSL; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{HWB} { return T_COL_HWB; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{CMYK} { return T_COL_CMYK; }
/* Fluff */
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{VAR_START}{S_T_PARENT_LEFT} {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(PROPERTIES_VAR);
<PROPERTIES_ENV_VAR,PROPERTIES_VAR>{S_T_PARENT_LEFT} {
return T_PARENT_LEFT;
}
<PROPERTIES_VAR>{S_T_PARENT_RIGHT} {
/* Fluff */
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{VAR_START} {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(PROPERTIES_VAR);
return T_VAR_START;
}
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{ENV_START} {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(PROPERTIES_ENV_VAR);
return T_ENV_START;
}
<PROPERTIES_VAR,PROPERTIES_ENV_VAR>{S_T_PARENT_RIGHT} {
BEGIN(GPOINTER_TO_INT(g_queue_pop_head ( queue )));
return T_PARENT_RIGHT;
}
<PROPERTIES_VAR>{COMMA} {
<PROPERTIES_VAR,PROPERTIES_ENV_VAR>{COMMA} {
g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) );
BEGIN(PROPERTIES_VAR_DEFAULT);
return T_COMMA;
@ -794,7 +825,7 @@ if ( queue == NULL ) {
return T_ELEMENT;
}
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,PROPERTIES_LIST>. {
<PROPERTIES_ENV_VAR,PROPERTIES_VAR,PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,PROPERTIES_LIST>. {
yytext[yyleng-1] = '\0';
return T_ERROR_PROPERTY;
}

View File

@ -265,6 +265,10 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%token T_MEDIA_MAX "Max"
%token T_MEDIA_SEP "-"
%token T_VAR_START "var"
%token T_ENV_START "env"
%type <theme> t_entry_list
%type <theme> t_entry_list_included
%type <list> t_entry_name_path
@ -481,17 +485,25 @@ t_property
$$ = $3;
$$->name = $1;
}
| t_property_name T_PSEP T_PARENT_LEFT T_ELEMENT T_PARENT_RIGHT T_PCLOSE{
| t_property_name T_PSEP T_VAR_START T_PARENT_LEFT T_ELEMENT T_PARENT_RIGHT T_PCLOSE{
$$ = rofi_theme_property_create ( P_LINK );
$$->name = $1;
$$->value.link.name = $4;
$$->value.link.name = $5;
}
| t_property_name T_PSEP T_PARENT_LEFT T_ELEMENT T_COMMA t_property_element T_PARENT_RIGHT T_PCLOSE{
| t_property_name T_PSEP T_VAR_START T_PARENT_LEFT T_ELEMENT T_COMMA t_property_element T_PARENT_RIGHT T_PCLOSE{
$$ = rofi_theme_property_create ( P_LINK );
$$->name = $1;
$$->value.link.name = $4;
$$->value.link.def_value = $6;
$$->value.link.name = $5;
$$->value.link.def_value = $7;
}
| t_property_name T_PSEP T_ENV_START T_PARENT_LEFT T_COMMA t_property_element T_PARENT_RIGHT T_PCLOSE {
$$ = $6;
$$->name = $1;
}
| t_property_name T_PSEP T_ENV_START T_PARENT_LEFT t_property_element T_COMMA t_property_element T_PARENT_RIGHT T_PCLOSE {
$$ = $5;
$$->name = $1;
}
t_property_element
: T_INHERIT {

View File

@ -134,7 +134,7 @@ flex = generator(find_program('flex'),
)
bison = generator(find_program('bison'),
output: [ '@BASENAME@.c', '@BASENAME@.h' ],
arguments: [ '-d', '@INPUT@', '--defines=@OUTPUT1@', '--output=@OUTPUT0@' ]
arguments: [ '--verbose', '-d', '@INPUT@', '--defines=@OUTPUT1@', '--output=@OUTPUT0@' ]
)
rofi_sources = files(