]> git.tuebingen.mpg.de Git - paraslash.git/blob - yy/mp.lex
1e06b8dcc1072cfb23e3cfbcc198d227706f1b22
[paraslash.git] / yy / mp.lex
1 /*
2  * Copyright (C) 2017 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7  /*
8   * Since we do not supply yywrap(), we use noyywrap to instruct the scanner to
9   * behave as though yywrap() returned 1.
10   */
11 %option noyywrap
12
13  /*
14   * We don't want symbols to clash with those of other flex users, particularly
15   * lopsub.
16   */
17 %option prefix="mp_yy"
18
19  /*
20   * Generate a scanner that maintains the number of the current line read from
21   * its input in the yylineno variable.
22   */
23 %option yylineno
24
25  /* Generate a bison-compatible scanner. */
26 %option bison-bridge bison-locations
27
28  /*
29   * Warn (in particular) if the default rule can be matched but no default rule
30   * has been given.
31   */
32 %option warn
33
34  /*
35   * Generate a scanner which is portable and safe to use in one or more threads
36   * of control.
37   */
38 %option reentrant
39
40  /*
41   * Generate a scanner which always looks one extra character ahead. This is a
42   * bit faster than an interactive scanner for which look ahead happens only
43   * when necessary.
44   */
45 %option never-interactive
46
47 %{
48 #include <regex.h>
49 #include "para.h"
50 #include "string.h"
51 #include "mp.h"
52 #include "error.h"
53
54 #define YYSTYPE MP_YYSTYPE
55 #define YYLTYPE MP_YYLTYPE
56 #define YY_DECL int mp_yylex(MP_YYSTYPE *yylval_param, MP_YYLTYPE *yylloc_param, \
57         struct mp_context *ctx, struct mp_ast_node **ast, mp_yyscan_t yyscanner)
58 #include "mp.bison.h"
59 #define MP_YY_USER_ACTION do {mp_yylloc->first_line = mp_yylineno;} while (0);
60 %}
61 DECIMAL_CONSTANT  (0|([[:digit:]]{-}[0])[[:digit:]]*)
62 STRING_LITERAL    \"([^\"\\\n]|(\\[\"\\abfnrtv]))*\"
63 REGEX_PATTERN     \/([^\/\\\n]|(\\[\/\\abfnrtv]))*\/([in])*
64 WILDCARD_PATTERN  \|([^\|\\\n]|(\\[\|\\abfnrtv]))*\|([npPlie])*
65 %%
66
67 is_set {return IS_SET;}
68 num_attributes_set {return NUM_ATTRIBUTES_SET;}
69 path {return PATH;}
70 artist {return ARTIST;}
71 title {return TITLE;}
72 album {return ALBUM;}
73 comment {return COMMENT;}
74 year {return YEAR;}
75 num_played {return NUM_PLAYED;}
76 image_id {return IMAGE_ID;}
77 lyrics_id {return LYRICS_ID;}
78 bitrate {return BITRATE;}
79 frequency {return FREQUENCY;}
80 channels {return CHANNELS;}
81 true {return TRUE;}
82 false {return FALSE;}
83
84 [[:space:]]+|#.*\n /* skip comments and whitespace */
85
86 "("|")"|","|"+"|"-"|"*"|"/"|"<"|">" {return yytext[0];}
87
88 "||" {return OR;}
89 "&&" {return AND;}
90 "!" {return NOT;}
91 "==" {return EQUAL;}
92 "!=" {return NOT_EQUAL;}
93 "<=" {return LESS_OR_EQUAL;}
94 ">=" {return GREATER_OR_EQUAL;}
95 "=~" {return REGEX_MATCH;}
96 "=|" {return FILENAME_MATCH;}
97
98 {DECIMAL_CONSTANT} {
99         int ret;
100         yylval->node = mp_new_ast_leaf_node(NUM);
101         ret = para_atoi64(yytext, &yylval->node->sv.intval);
102         if (ret < 0) {
103                 free(yylval->node);
104                 mp_parse_error(yylloc->first_line, ctx, "%s: %s", yytext,
105                         para_strerror(-ret));
106                 return -E_MOOD_PARSE;
107         }
108         return NUM;
109 }
110
111 {STRING_LITERAL} {
112         yylval->node = mp_new_ast_leaf_node(STRING_LITERAL);
113         parse_quoted_string(yytext, "\"\"", &yylval->node->sv.strval);
114         //PARA_CRIT_LOG("strval: %s\n", yylval->node->sv.strval);
115         //PARA_CRIT_LOG("node: %p\n", yylval->node);
116         return STRING_LITERAL;
117 }
118
119 {REGEX_PATTERN} {
120         int ret;
121         yylval->node = mp_new_ast_leaf_node(REGEX_PATTERN);
122         ret = mp_parse_regex_pattern(yytext, &yylval->node->sv.re_pattern);
123         if (ret < 0) {
124                 mp_parse_error(yylloc->first_line, ctx, "%s: %s", yytext,
125                         para_strerror(-ret));
126                 return -E_MOOD_PARSE;
127         }
128         return REGEX_PATTERN;
129 }
130
131 {WILDCARD_PATTERN} {
132         yylval->node = mp_new_ast_leaf_node(WILDCARD_PATTERN);
133         mp_parse_wildcard_pattern(yytext, &yylval->node->sv.wc_pattern);
134         return WILDCARD_PATTERN;
135 }
136
137 . {
138         mp_parse_error(yylloc->first_line, ctx, "unrecognized text: %s",
139                 yytext);
140         return -E_MOOD_PARSE;
141 }