mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-19 19:25:41 +02:00
Implement '&&' and '||' operators on map
'-' is a valid character for tags.
Commit 03e1c7d
(module/network: Add link stats, 2022-04-30) introduced
two new tags for the network module: `ul-speed` and `dl-speed`. These
use the `-` character, that was previously never used in any tag.
We had two options: either change those tags to use `_` instead, or just
accept `-`s as a valid character. Going forward, I can see many people
deciding to name their tags with `-` instead of `_`, so I believe it is
better to just accept it once and for all.
Note that `-` cannot be used as the first character of a tag (e.g.
`-tag1`) since the `-` has a special meaning in `.yml` files. I don't
believe this will happen often, however, and should be easy to both
detect and correct if it does.
This commit is contained in:
parent
463b39b56d
commit
4a41d4296a
10 changed files with 330 additions and 226 deletions
|
@ -40,6 +40,7 @@ pipeline:
|
|||
- apk add json-c-dev libmpdclient-dev alsa-lib-dev
|
||||
- apk add ttf-dejavu
|
||||
- apk add git
|
||||
- apk add flex bison
|
||||
|
||||
# Debug
|
||||
- apk add gcovr
|
||||
|
@ -97,6 +98,7 @@ pipeline:
|
|||
- apk add json-c-dev libmpdclient-dev alsa-lib-dev
|
||||
- apk add ttf-dejavu
|
||||
- apk add git
|
||||
- apk add flex bison
|
||||
|
||||
# Debug
|
||||
- mkdir -p bld/debug-x86
|
||||
|
|
14
CHANGELOG.md
14
CHANGELOG.md
|
@ -87,6 +87,20 @@
|
|||
...
|
||||
```
|
||||
|
||||
Note that if `<value>` contains any non-alphanumerical characters,
|
||||
it **must** be surrounded by `""`:
|
||||
|
||||
`State == "very confused!!!"`
|
||||
|
||||
Finally, you can mix and match conditions using the boolean
|
||||
operators `&&` and `||`:
|
||||
|
||||
```
|
||||
<condition1> && <condition2>
|
||||
<condition1> && (<condition2> || <condition3>) # parenthesis work
|
||||
~(<condition1> && <condition2>) # '~' can be applied to any condition
|
||||
```
|
||||
|
||||
For a more thorough explanation, see the updated map section in the
|
||||
man page for yambar-particles([#137][137] and [#175][175]).
|
||||
|
||||
|
|
|
@ -218,11 +218,15 @@ content:
|
|||
This particle maps the values of a specific tag to different
|
||||
particles based on conditions. A condition takes either the form of:
|
||||
|
||||
```
|
||||
<tag> <operation> <value>
|
||||
```
|
||||
|
||||
Or, for boolean tags:
|
||||
|
||||
```
|
||||
<tag>
|
||||
```
|
||||
|
||||
Where <tag> is the tag you would like to map, <operation> is one of:
|
||||
|
||||
|
@ -233,15 +237,46 @@ Where <tag> is the tag you would like to map, <operation> is one of:
|
|||
:- <=
|
||||
:- <
|
||||
|
||||
and <value> is the value you would like to compare it to.
|
||||
and <value> is the value you would like to compare it to. *If the
|
||||
value contains any non-alphanumerical characters, you must
|
||||
surround it with ' " ' *:
|
||||
|
||||
For boolean tags, negation is done with a preceding '~':
|
||||
```
|
||||
"hello world"
|
||||
"@#$%"
|
||||
```
|
||||
|
||||
Negation is done with a preceding '~':
|
||||
|
||||
```
|
||||
~<tag>
|
||||
~<condition>
|
||||
```
|
||||
|
||||
To match for empty strings, use ' "" ':
|
||||
|
||||
```
|
||||
<tag> == ""
|
||||
```
|
||||
|
||||
Furthermore, you may use the boolean operators:
|
||||
|
||||
[- &&
|
||||
:- ||
|
||||
|
||||
in order to create more complex conditions:
|
||||
|
||||
```
|
||||
<condition1> && <condition2>
|
||||
```
|
||||
|
||||
You may surround <condition> with parenthesis for clarity or
|
||||
specifying precedence:
|
||||
|
||||
```
|
||||
(<condition>)
|
||||
<condition1> && (<condition2> || <condition3>)
|
||||
```
|
||||
|
||||
In addition to explicit tag values, you can also specify a
|
||||
default/fallback particle.
|
||||
|
|
|
@ -178,11 +178,7 @@ bar:
|
|||
map:
|
||||
default: {string: {text: , font: *awesome, foreground: ffffff66}}
|
||||
conditions:
|
||||
state == up:
|
||||
map:
|
||||
default: {string: {text: , font: *awesome}}
|
||||
conditions:
|
||||
ipv4 == "": {string: {text: , font: *awesome, foreground: ffffff66}}
|
||||
state == up && ipv4 != "": {string: {text: , font: *awesome}}
|
||||
- network:
|
||||
name: wlp2s0
|
||||
poll-interval: 1
|
||||
|
@ -257,7 +253,7 @@ bar:
|
|||
state == full:
|
||||
- string: {text: , foreground: 00ff00ff, font: *awesome}
|
||||
- string: {text: "{capacity}% full"}
|
||||
state == not charging:
|
||||
state == "not charging":
|
||||
- ramp:
|
||||
tag: capacity
|
||||
items:
|
||||
|
|
|
@ -41,16 +41,18 @@ bar:
|
|||
map:
|
||||
<<: *river_base
|
||||
deco: *bg_default
|
||||
state == visible:
|
||||
state == visible && ~occupied:
|
||||
map:
|
||||
conditions:
|
||||
~occupied: {map: {<<: *river_base}}
|
||||
occupied: {map: {<<: *river_base, deco: *bg_default}}
|
||||
<<: *river_base
|
||||
state == visible && occupied:
|
||||
map:
|
||||
<<: *river_base
|
||||
deco: *bg_default
|
||||
state == unfocused:
|
||||
map:
|
||||
<<: *river_base
|
||||
state == invisible:
|
||||
state == invisible && ~occupied: {empty: {}}
|
||||
state == invisible && occupied:
|
||||
map:
|
||||
conditions:
|
||||
~occupied: {empty: {}}
|
||||
occupied: {map: {<<: *river_base, deco: {underline: {size: 3, color: ea6962ff}}}}
|
||||
<<: *river_base
|
||||
deco: {underline: {size: 3, color: ea6962ff}}
|
||||
|
|
264
particles/map.c
264
particles/map.c
|
@ -11,38 +11,7 @@
|
|||
#include "../plugin.h"
|
||||
#include "dynlist.h"
|
||||
|
||||
enum map_op{
|
||||
MAP_OP_EQ,
|
||||
MAP_OP_NE,
|
||||
MAP_OP_LE,
|
||||
MAP_OP_LT,
|
||||
MAP_OP_GE,
|
||||
MAP_OP_GT,
|
||||
/* The next two are for bool types */
|
||||
MAP_OP_SELF,
|
||||
MAP_OP_NOT,
|
||||
};
|
||||
|
||||
struct map_condition {
|
||||
char *tag;
|
||||
enum map_op op;
|
||||
char *value;
|
||||
};
|
||||
|
||||
static char *
|
||||
trim(char *s)
|
||||
{
|
||||
while (*s == ' ')
|
||||
s++;
|
||||
|
||||
char *end = s + strlen(s) - 1;
|
||||
while (*end == ' ') {
|
||||
*end = '\0';
|
||||
end--;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
#include "map.h"
|
||||
|
||||
bool
|
||||
int_condition(const long tag_value, const long cond_value, enum map_op op)
|
||||
|
@ -54,6 +23,7 @@ int_condition(const long tag_value, const long cond_value, enum map_op op)
|
|||
case MAP_OP_LT: return tag_value < cond_value;
|
||||
case MAP_OP_GE: return tag_value >= cond_value;
|
||||
case MAP_OP_GT: return tag_value > cond_value;
|
||||
case MAP_OP_SELF: LOG_WARN("using int tag as bool");
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +38,7 @@ float_condition(const double tag_value, const double cond_value, enum map_op op)
|
|||
case MAP_OP_LT: return tag_value < cond_value;
|
||||
case MAP_OP_GE: return tag_value >= cond_value;
|
||||
case MAP_OP_GT: return tag_value > cond_value;
|
||||
case MAP_OP_SELF: LOG_WARN("using float tag as bool");
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
@ -82,12 +53,13 @@ str_condition(const char* tag_value, const char* cond_value, enum map_op op)
|
|||
case MAP_OP_LT: return strcmp(tag_value, cond_value) < 0;
|
||||
case MAP_OP_GE: return strcmp(tag_value, cond_value) >= 0;
|
||||
case MAP_OP_GT: return strcmp(tag_value, cond_value) > 0;
|
||||
case MAP_OP_SELF: LOG_WARN("using String tag as bool");
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
eval_map_condition(const struct map_condition* map_cond, const struct tag_set *tags)
|
||||
eval_comparison(const struct map_condition* map_cond, const struct tag_set *tags)
|
||||
{
|
||||
const struct tag *tag = tag_for_name(tags, map_cond->tag);
|
||||
if (tag == NULL) {
|
||||
|
@ -131,8 +103,6 @@ eval_map_condition(const struct map_condition* map_cond, const struct tag_set *t
|
|||
case TAG_TYPE_BOOL:
|
||||
if (map_cond->op == MAP_OP_SELF)
|
||||
return tag->as_bool(tag);
|
||||
else if (map_cond->op == MAP_OP_NOT)
|
||||
return !tag->as_bool(tag);
|
||||
else {
|
||||
LOG_WARN("boolean tag '%s' should be used directly", map_cond->tag);
|
||||
return false;
|
||||
|
@ -145,86 +115,43 @@ eval_map_condition(const struct map_condition* map_cond, const struct tag_set *t
|
|||
return false;
|
||||
}
|
||||
|
||||
struct map_condition*
|
||||
map_condition_from_str(const char *str)
|
||||
bool
|
||||
eval_map_condition(const struct map_condition* map_cond, const struct tag_set *tags)
|
||||
{
|
||||
struct map_condition *cond = malloc(sizeof(*cond));
|
||||
|
||||
/* This strdup is just to discard the 'const' qualifier */
|
||||
char *str_cpy = strdup(str);
|
||||
char *op_str = strpbrk(str_cpy, "=!<>~");
|
||||
|
||||
/* we've already checked things in the verify functions, so these should all work */
|
||||
char *tag = str_cpy;
|
||||
char *value = NULL;
|
||||
|
||||
if (op_str == NULL) {
|
||||
cond->tag = strdup(trim(tag));
|
||||
cond->op = MAP_OP_SELF;
|
||||
cond->value = NULL;
|
||||
free(str_cpy);
|
||||
return cond;
|
||||
switch(map_cond->op)
|
||||
{
|
||||
case MAP_OP_NOT: return !eval_map_condition(map_cond->cond1, tags);
|
||||
case MAP_OP_AND: return eval_map_condition(map_cond->cond1, tags) && eval_map_condition(map_cond->cond2, tags);
|
||||
case MAP_OP_OR: return eval_map_condition(map_cond->cond1, tags) || eval_map_condition(map_cond->cond2, tags);
|
||||
default: return eval_comparison(map_cond, tags);
|
||||
}
|
||||
|
||||
switch (op_str[0]) {
|
||||
case '=':
|
||||
cond->op = MAP_OP_EQ;
|
||||
value = op_str + 2;
|
||||
break;
|
||||
case '!':
|
||||
cond->op = MAP_OP_NE;
|
||||
value = op_str + 2;
|
||||
break;
|
||||
case '<':
|
||||
if (op_str[1] == '=') {
|
||||
cond->op = MAP_OP_LE;
|
||||
value = op_str + 2;
|
||||
} else {
|
||||
cond->op = MAP_OP_LT;
|
||||
value = op_str + 1;
|
||||
}
|
||||
break;
|
||||
case '>':
|
||||
if (op_str[1] == '=') {
|
||||
cond->op = MAP_OP_GE;
|
||||
value = op_str + 2;
|
||||
} else {
|
||||
cond->op = MAP_OP_GT;
|
||||
value = op_str + 1;
|
||||
}
|
||||
break;
|
||||
case '~':
|
||||
tag = op_str + 1;
|
||||
cond->op = MAP_OP_NOT;
|
||||
break;
|
||||
}
|
||||
|
||||
/* NULL terminate the tag value */
|
||||
op_str[0] = '\0';
|
||||
|
||||
cond->tag = strdup(trim(tag));
|
||||
|
||||
cond->value = NULL;
|
||||
if (value != NULL) {
|
||||
value = trim(value);
|
||||
const size_t value_len = strlen(value);
|
||||
if (value[0] == '"' && value[value_len - 1] == '"') {
|
||||
value[value_len - 1] = '\0';
|
||||
++value;
|
||||
}
|
||||
cond->value = strdup(value);
|
||||
}
|
||||
|
||||
free(str_cpy);
|
||||
return cond;
|
||||
}
|
||||
|
||||
void
|
||||
free_map_condition(struct map_condition* mc)
|
||||
free_map_condition(struct map_condition* c)
|
||||
{
|
||||
free(mc->tag);
|
||||
free(mc->value);
|
||||
free(mc);
|
||||
switch (c->op)
|
||||
{
|
||||
case MAP_OP_EQ:
|
||||
case MAP_OP_NE:
|
||||
case MAP_OP_LE:
|
||||
case MAP_OP_LT:
|
||||
case MAP_OP_GE:
|
||||
case MAP_OP_GT:
|
||||
free(c->value);
|
||||
/* FALLTHROUGH */
|
||||
case MAP_OP_SELF:
|
||||
free(c->tag);
|
||||
break;
|
||||
case MAP_OP_AND:
|
||||
case MAP_OP_OR:
|
||||
free_map_condition(c->cond2);
|
||||
/* FALLTHROUGH */
|
||||
case MAP_OP_NOT:
|
||||
free_map_condition(c->cond1);
|
||||
break;
|
||||
}
|
||||
free(c);
|
||||
}
|
||||
|
||||
struct particle_map {
|
||||
|
@ -379,101 +306,6 @@ map_new(struct particle *common, const struct particle_map particle_map[],
|
|||
return common;
|
||||
}
|
||||
|
||||
static bool
|
||||
verify_map_condition_syntax(keychain_t *chain, const struct yml_node *node,
|
||||
const char *line)
|
||||
{
|
||||
/* We need this strdup to discard the 'const' qualifier */
|
||||
char *cond = strdup(line);
|
||||
char *op_str = strpbrk(cond, " =!<>~");
|
||||
if (op_str == NULL) {
|
||||
char *tag = trim(cond);
|
||||
if (tag[0] == '\0')
|
||||
goto syntax_fail_missing_tag;
|
||||
|
||||
free(cond);
|
||||
return true;
|
||||
}
|
||||
op_str = trim(op_str);
|
||||
|
||||
char *tag = cond;
|
||||
char *value = NULL;
|
||||
|
||||
switch (op_str[0]) {
|
||||
case '=':
|
||||
if (op_str[1] != '=')
|
||||
goto syntax_fail_invalid_op;
|
||||
|
||||
value = op_str + 2;
|
||||
break;
|
||||
|
||||
case '!':
|
||||
if (op_str[1] != '=')
|
||||
goto syntax_fail_invalid_op;
|
||||
|
||||
value = op_str + 2;
|
||||
break;
|
||||
|
||||
case '<':
|
||||
if (op_str[1] == '=')
|
||||
value = op_str + 2;
|
||||
else
|
||||
value = op_str + 1;
|
||||
break;
|
||||
|
||||
case '>':
|
||||
if (op_str[1] == '=')
|
||||
value = op_str + 2;
|
||||
else
|
||||
value = op_str + 1;
|
||||
break;
|
||||
|
||||
case '~':
|
||||
tag = op_str + 1;
|
||||
if (strpbrk(tag, " =!<>~") != NULL)
|
||||
goto syntax_fail_bad_not;
|
||||
value = NULL;
|
||||
break;
|
||||
default:
|
||||
goto syntax_fail_invalid_op;
|
||||
}
|
||||
|
||||
/* NULL terminate the tag value */
|
||||
op_str[0] = '\0';
|
||||
|
||||
tag = trim(tag);
|
||||
if (tag[0] == '\0')
|
||||
goto syntax_fail_missing_tag;
|
||||
|
||||
value = value != NULL ? trim(value) : NULL;
|
||||
|
||||
if (value != NULL && value[0] == '\0')
|
||||
goto syntax_fail_missing_value;
|
||||
|
||||
free(cond);
|
||||
return true;
|
||||
|
||||
syntax_fail_invalid_op:
|
||||
LOG_ERR("%s: \"%s\" invalid operator", conf_err_prefix(chain, node), line);
|
||||
goto err;
|
||||
|
||||
syntax_fail_missing_tag:
|
||||
LOG_ERR("%s: \"%s\" missing tag", conf_err_prefix(chain, node), line);
|
||||
goto err;
|
||||
|
||||
syntax_fail_missing_value:
|
||||
LOG_ERR("%s: \"%s\" missing value", conf_err_prefix(chain, node), line);
|
||||
goto err;
|
||||
|
||||
syntax_fail_bad_not:
|
||||
LOG_ERR("%s: \"%s\" '~' cannot be used with other operators",
|
||||
conf_err_prefix(chain, node), line);
|
||||
goto err;
|
||||
|
||||
err:
|
||||
free(cond);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
verify_map_conditions(keychain_t *chain, const struct yml_node *node)
|
||||
|
@ -485,6 +317,7 @@ verify_map_conditions(keychain_t *chain, const struct yml_node *node)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool result = true;
|
||||
for (struct yml_dict_iter it = yml_dict_iter(node);
|
||||
it.key != NULL;
|
||||
yml_dict_next(&it))
|
||||
|
@ -494,9 +327,16 @@ verify_map_conditions(keychain_t *chain, const struct yml_node *node)
|
|||
LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!verify_map_condition_syntax(chain, it.key, key))
|
||||
return false;
|
||||
char *key_clone = strdup(key);
|
||||
YY_BUFFER_STATE buffer = yy_scan_string(key_clone);
|
||||
if (yyparse() != 0) {
|
||||
LOG_ERR("%s: %s", conf_err_prefix(chain, it.key), MAP_PARSER_ERROR_MSG);
|
||||
free(MAP_PARSER_ERROR_MSG);
|
||||
result = false;
|
||||
} else
|
||||
free_map_condition(MAP_CONDITION_PARSE_RESULT);
|
||||
yy_delete_buffer(buffer);
|
||||
free(key_clone);
|
||||
|
||||
if (!conf_verify_particle(chain_push(chain, key), it.value))
|
||||
return false;
|
||||
|
@ -504,7 +344,7 @@ verify_map_conditions(keychain_t *chain, const struct yml_node *node)
|
|||
chain_pop(chain);
|
||||
}
|
||||
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
static struct particle *
|
||||
|
@ -526,7 +366,13 @@ from_conf(const struct yml_node *node, struct particle *common)
|
|||
it.key != NULL;
|
||||
yml_dict_next(&it), idx++)
|
||||
{
|
||||
particle_map[idx].condition = map_condition_from_str(yml_value_as_string(it.key));
|
||||
/* Note we can skip the error checking here */
|
||||
char *key_clone = strdup(yml_value_as_string(it.key));
|
||||
YY_BUFFER_STATE buffer = yy_scan_string(key_clone);
|
||||
yyparse();
|
||||
particle_map[idx].condition = MAP_CONDITION_PARSE_RESULT;
|
||||
yy_delete_buffer(buffer);
|
||||
free(key_clone);
|
||||
particle_map[idx].particle = conf_to_particle(it.value, inherited);
|
||||
}
|
||||
|
||||
|
|
37
particles/map.h
Normal file
37
particles/map.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
enum map_op {
|
||||
MAP_OP_EQ,
|
||||
MAP_OP_NE,
|
||||
MAP_OP_LE,
|
||||
MAP_OP_LT,
|
||||
MAP_OP_GE,
|
||||
MAP_OP_GT,
|
||||
MAP_OP_SELF,
|
||||
MAP_OP_NOT,
|
||||
|
||||
MAP_OP_AND,
|
||||
MAP_OP_OR,
|
||||
};
|
||||
|
||||
struct map_condition {
|
||||
union {
|
||||
char *tag;
|
||||
struct map_condition *cond1;
|
||||
};
|
||||
enum map_op op;
|
||||
union {
|
||||
char *value;
|
||||
struct map_condition *cond2;
|
||||
};
|
||||
};
|
||||
|
||||
void free_map_condition(struct map_condition *c);
|
||||
|
||||
typedef struct yy_buffer_state* YY_BUFFER_STATE;
|
||||
YY_BUFFER_STATE yy_scan_string(const char *str);
|
||||
int yyparse();
|
||||
void yy_delete_buffer(YY_BUFFER_STATE buffer);
|
||||
|
||||
extern struct map_condition *MAP_CONDITION_PARSE_RESULT;
|
||||
extern char *MAP_PARSER_ERROR_MSG;
|
25
particles/map.l
Normal file
25
particles/map.l
Normal file
|
@ -0,0 +1,25 @@
|
|||
%{
|
||||
#include <string.h>
|
||||
#include "map.h"
|
||||
#include "map.tab.h"
|
||||
%}
|
||||
|
||||
%option warn nodefault nounput noinput noyywrap
|
||||
|
||||
%%
|
||||
[[:alnum:]_-]+ yylval.str = strdup(yytext); return WORD;
|
||||
\".*\" yylval.str = strndup(yytext + 1, strlen(yytext) - 2); return STRING;
|
||||
== yylval.op = MAP_OP_EQ; return CMP_OP;
|
||||
!= yylval.op = MAP_OP_NE; return CMP_OP;
|
||||
\<= yylval.op = MAP_OP_LE; return CMP_OP;
|
||||
\< yylval.op = MAP_OP_LT; return CMP_OP;
|
||||
>= yylval.op = MAP_OP_GE; return CMP_OP;
|
||||
> yylval.op = MAP_OP_GT; return CMP_OP;
|
||||
&& yylval.op = MAP_OP_AND; return BOOL_OP;
|
||||
\|\| yylval.op = MAP_OP_OR; return BOOL_OP;
|
||||
~ return NOT;
|
||||
\( return L_PAR;
|
||||
\) return R_PAR;
|
||||
[ \t\n] ;
|
||||
. yylval.str = strdup(yytext); return STRING;
|
||||
%%
|
125
particles/map.y
Normal file
125
particles/map.y
Normal file
|
@ -0,0 +1,125 @@
|
|||
%{
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "map.h"
|
||||
|
||||
struct map_condition *MAP_CONDITION_PARSE_RESULT;
|
||||
char *MAP_PARSER_ERROR_MSG;
|
||||
|
||||
static const int NUM_TOKENS = 7;
|
||||
int yylex();
|
||||
void yyerror(const char *str);
|
||||
%}
|
||||
|
||||
%define parse.lac full
|
||||
%define parse.error custom
|
||||
|
||||
%union {
|
||||
char *str;
|
||||
struct map_condition *condition;
|
||||
enum map_op op;
|
||||
}
|
||||
|
||||
%token WORD STRING CMP_OP L_PAR R_PAR
|
||||
%left BOOL_OP
|
||||
%precedence NOT
|
||||
|
||||
%destructor { free_map_condition($<condition>$); } condition
|
||||
%destructor { free($<str>$); } WORD
|
||||
%destructor { free($<str>$); } STRING
|
||||
|
||||
%%
|
||||
result: condition { MAP_CONDITION_PARSE_RESULT = $<condition>1; };
|
||||
|
||||
condition:
|
||||
WORD {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->op = MAP_OP_SELF;
|
||||
}
|
||||
|
|
||||
WORD CMP_OP WORD {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->op = $<op>2;
|
||||
$<condition>$->value = $<str>3;
|
||||
}
|
||||
|
|
||||
WORD CMP_OP STRING {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->op = $<op>2;
|
||||
$<condition>$->value = $<str>3;
|
||||
}
|
||||
|
|
||||
L_PAR condition R_PAR { $<condition>$ = $<condition>2; }
|
||||
|
|
||||
NOT condition {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->cond1 = $<condition>2;
|
||||
$<condition>$->op = MAP_OP_NOT;
|
||||
}
|
||||
|
|
||||
condition BOOL_OP condition {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->cond1 = $<condition>1;
|
||||
$<condition>$->op = $<op>2;
|
||||
$<condition>$->cond2 = $<condition>3;
|
||||
}
|
||||
;
|
||||
%%
|
||||
|
||||
void yyerror(const char *str)
|
||||
{
|
||||
fprintf(stderr, "error: %s\n", str);
|
||||
}
|
||||
|
||||
static char const*
|
||||
token_to_str(yysymbol_kind_t tkn)
|
||||
{
|
||||
switch (tkn) {
|
||||
case YYSYMBOL_CMP_OP: return "==, !=, <=, <, >=, >";
|
||||
case YYSYMBOL_BOOL_OP: return "||, &&";
|
||||
case YYSYMBOL_L_PAR: return "(";
|
||||
case YYSYMBOL_R_PAR: return ")";
|
||||
case YYSYMBOL_NOT: return "~";
|
||||
default: return yysymbol_name(tkn);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
yyreport_syntax_error (const yypcontext_t *ctx)
|
||||
{
|
||||
int res = 0;
|
||||
char *errmsg = malloc(1024);
|
||||
errmsg[0] = '\0';
|
||||
|
||||
// Report the tokens expected at this point.
|
||||
yysymbol_kind_t expected[NUM_TOKENS];
|
||||
int n = yypcontext_expected_tokens(ctx, expected, NUM_TOKENS);
|
||||
if (n < 0)
|
||||
res = n; // Forward errors to yyparse.
|
||||
else {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
strcat(errmsg, i == 0 ? "expected [" : ", ");
|
||||
strcat(errmsg, token_to_str(expected[i]));
|
||||
}
|
||||
strcat(errmsg, "]");
|
||||
}
|
||||
|
||||
// Report the unexpected token.
|
||||
yysymbol_kind_t lookahead = yypcontext_token(ctx);
|
||||
if (lookahead != YYSYMBOL_YYEMPTY) {
|
||||
strcat(errmsg, ", found ");
|
||||
if (!(lookahead == YYSYMBOL_STRING || lookahead == YYSYMBOL_WORD))
|
||||
strcat(errmsg, yysymbol_name(lookahead));
|
||||
else if (yylval.str != NULL)
|
||||
strcat(errmsg, yylval.str);
|
||||
else
|
||||
strcat(errmsg, "nothing");
|
||||
}
|
||||
|
||||
MAP_PARSER_ERROR_MSG = errmsg;
|
||||
return res;
|
||||
}
|
|
@ -1,3 +1,25 @@
|
|||
flex = find_program('flex', required: true)
|
||||
bison = find_program('bison', required: true)
|
||||
|
||||
lgen = generator(
|
||||
flex,
|
||||
output : '@BASENAME@.yy.c',
|
||||
arguments : ['-o', '@OUTPUT@', '@INPUT@']
|
||||
)
|
||||
lfiles = lgen.process('map.l')
|
||||
|
||||
pgen = generator(
|
||||
bison,
|
||||
output : ['@BASENAME@.tab.c', '@BASENAME@.tab.h'],
|
||||
arguments : ['-Wall',
|
||||
'-Wcounterexamples',
|
||||
'--defines=@OUTPUT1@',
|
||||
'--output=@OUTPUT0@', '@INPUT@']
|
||||
)
|
||||
pfiles = pgen.process('map.y')
|
||||
|
||||
map_parser = declare_dependency(sources: [pfiles, lfiles], include_directories: '.')
|
||||
|
||||
particle_sdk = declare_dependency(dependencies: [pixman, tllist, fcft])
|
||||
|
||||
dynlist_lib = build_target(
|
||||
|
@ -14,7 +36,7 @@ dynlist = declare_dependency(link_with: dynlist_lib)
|
|||
deps = {
|
||||
'empty': [],
|
||||
'list': [],
|
||||
'map': [dynlist],
|
||||
'map': [dynlist, map_parser],
|
||||
'progress-bar': [],
|
||||
'ramp': [],
|
||||
'string': [],
|
||||
|
|
Loading…
Add table
Reference in a new issue