[smartpl] Added rule for enum tags like data_kind and media_kind to

avoid having magic numbers like "media_kind = 1" in smartplaylists. Enum
rules are defined like "media_kind is music" with a fixed set of valid
values after the "is" keyword.
This commit is contained in:
chme 2015-04-23 11:36:36 +02:00
parent 659f9c09bb
commit 8376180fd6
2 changed files with 83 additions and 12 deletions

View File

@ -41,6 +41,7 @@ crit : LPAR expression RPAR -> expression
| STRTAG (INCLUDES|IS) STR
| INTTAG INTBOOL INT
| DATETAG (AFTER|BEFORE) dateval
| ENUMTAG IS ENUMVAL
;
dateval : DATE
@ -62,20 +63,26 @@ STRTAG : 'artist'
| 'type'
;
INTTAG : 'play_count'
| 'rating'
| 'year'
| 'compilation'
;
DATETAG : 'time_added'
| 'time_played'
;
ENUMTAG : 'data_kind'
| 'media_kind'
;
INCLUDES : 'includes'
;
IS : 'is'
;
INTTAG : 'data_kind'
| 'media_kind'
| 'play_count'
| 'rating'
| 'year'
| 'compilation'
;
INTBOOL : (GREATER|GREATEREQUAL|LESS|LESSEQUAL|EQUAL)
;
@ -99,10 +106,6 @@ fragment
EQUAL : '='
;
DATETAG : 'time_added'
| 'time_played'
;
AFTER : 'after'
;
@ -144,6 +147,17 @@ DATINTERVAL : 'days'
| 'years'
;
ENUMVAL : 'music'
| 'movie'
| 'podcast'
| 'audiobook'
| 'tvshow'
| 'file'
| 'url'
| 'spotify'
| 'pipe'
;
STR : '"' ~('"')+ '"'
;

View File

@ -34,6 +34,7 @@ options {
#include <sqlite3.h>
#include "logger.h"
#include "db.h"
}
@members {
@ -145,6 +146,62 @@ expression returns [ pANTLR3_STRING result ]
$result->append8($result, " > ");
$result->append8($result, str);
}
| ENUMTAG IS ENUMVAL
{
pANTLR3_UINT8 tag;
pANTLR3_UINT8 val;
char str[20];
sprintf(str, "1=1");
tag = $ENUMTAG.text->chars;
val = $ENUMVAL.text->chars;
if (strcmp((char *)tag, "media_kind") == 0)
{
if (strcmp((char *)val, "music") == 0)
{
sprintf(str, "f.media_kind = \%d", MEDIA_KIND_MUSIC);
}
else if (strcmp((char *)val, "movie") == 0)
{
sprintf(str, "f.media_kind = \%d", MEDIA_KIND_MOVIE);
}
else if (strcmp((char *)val, "podcast") == 0)
{
sprintf(str, "f.media_kind = \%d", MEDIA_KIND_PODCAST);
}
else if (strcmp((char *)val, "audiobook") == 0)
{
sprintf(str, "f.media_kind = \%d", MEDIA_KIND_AUDIOBOOK);
}
else if (strcmp((char *)val, "tvshow") == 0)
{
sprintf(str, "f.media_kind = \%d", MEDIA_KIND_TVSHOW);
}
}
else if (strcmp((char *)tag, "data_kind") == 0)
{
if (strcmp((char *)val, "file") == 0)
{
sprintf(str, "f.data_kind = \%d", DATA_KIND_FILE);
}
else if (strcmp((char *)val, "url") == 0)
{
sprintf(str, "f.data_kind = \%d", DATA_KIND_URL);
}
else if (strcmp((char *)val, "spotify") == 0)
{
sprintf(str, "f.data_kind = \%d", DATA_KIND_SPOTIFY);
}
else if (strcmp((char *)val, "pipe") == 0)
{
sprintf(str, "f.data_kind = \%d", DATA_KIND_PIPE);
}
}
$result = $ENUMTAG.text->factory->newRaw($ENUMTAG.text->factory);
$result->append8($result, str);
}
;
dateval returns [ int result ]