libamxp  1.4.0
Patterns C Implementation
amxp_expression.l
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2020 SoftAtHome
4 **
5 ** Redistribution and use in source and binary forms, with or
6 ** without modification, are permitted provided that the following
7 ** conditions are met:
8 **
9 ** 1. Redistributions of source code must retain the above copyright
10 ** notice, this list of conditions and the following disclaimer.
11 **
12 ** 2. Redistributions in binary form must reproduce the above
13 ** copyright notice, this list of conditions and the following
14 ** disclaimer in the documentation and/or other materials provided
15 ** with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each
18 ** copyright holder and contributor hereby grants to those receiving
19 ** rights under this license a perpetual, worldwide, non-exclusive,
20 ** no-charge, royalty-free, irrevocable (except for failure to
21 ** satisfy the conditions of this license) patent license to make,
22 ** have made, use, offer to sell, sell, import, and otherwise
23 ** transfer this software, where such license applies only to those
24 ** patent claims, already acquired or hereafter acquired, licensable
25 ** by such copyright holder or contributor that are necessarily
26 ** infringed by:
27 **
28 ** (a) their Contribution(s) (the licensed copyrights of copyright
29 ** holders and non-copyrightable additions of contributors, in
30 ** source or binary form) alone; or
31 **
32 ** (b) combination of their Contribution(s) with the work of
33 ** authorship to which such Contribution(s) was added by such
34 ** copyright holder or contributor, if, at the time the Contribution
35 ** is added, such addition causes such combination to be necessarily
36 ** infringed. The patent license shall not apply to any other
37 ** combinations which include the Contribution.
38 **
39 ** Except as expressly stated above, no rights or licenses from any
40 ** copyright holder or contributor is granted under this license,
41 ** whether expressly, by implication, estoppel or otherwise.
42 **
43 ** DISCLAIMER
44 **
45 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
46 ** CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
47 ** INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 ** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
50 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
53 ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
54 ** AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 ** POSSIBILITY OF SUCH DAMAGE.
58 **
59 ****************************************************************************/
60 %option reentrant bison-bridge
61 %option never-interactive
62 %option nounput
63 %option noinput
64 %option noyywrap
65 %option bison-locations
66 
67 %{
68  #ifndef _GNU_SOURCE
69  #define _GNU_SOURCE
70  #endif
71  #include <string.h>
72  #include <sys/types.h>
73  #include <dlfcn.h>
74  #include <stdint.h>
75  #include <amxc/amxc.h>
76 
77  #include "amxp_expr_priv.h"
78  #include "amxp_expr.tab.h"
79 
80  #if __SIZE_WIDTH__ == 64
81  #define ssize_t_abs(x) labs(x)
82  #else
83  #define ssize_t_abs(x) abs(x)
84  #endif
85 
86  #define YY_EXTRA_TYPE amxp_expr_t*
87  #define YY_INPUT(buf,result,max_size) { \
88  ssize_t read_result = 0; \
89  read_result = yyextra->reader(yyextra, buf, max_size); \
90  if (read_result == -1) { \
91  YY_FATAL_ERROR( "input in flex scanner failed" ); \
92  } \
93  result = read_result > 0? ssize_t_abs(read_result):0; \
94  }
95 
96  extern int yylex \
97  (YYSTYPE * yylval_param , YYLTYPE* yylloc_param, yyscan_t yyscanner);
98 
99  #define UNUSED __attribute__((unused))
100  #define YY_DECL int yylex \
101  (YYSTYPE * yylval_param , UNUSED YYLTYPE* yylloc_param, yyscan_t yyscanner)
102 %}
103 
104 %x MULTI_LINE_COMMENT SINGLE_LINE_COMMENT LONG_TEXT LONG_TEXT_SQ FIELD_PATH
105 
106 NEWLINE \r?\n
107 
108 %%
109 
110 "/*" { BEGIN( MULTI_LINE_COMMENT ); }
111 <MULTI_LINE_COMMENT>{
112  "*" { yymore(); }
113  [^*\n]+ { yymore(); }
114  [^*\n]*{NEWLINE} { yymore(); }
115  "*/" { yylval->cptr.txt = yytext;
116  yylval->cptr.length = yyleng - 2;
117  BEGIN(INITIAL);
118  }
119 }
120 
121 "//" { BEGIN( SINGLE_LINE_COMMENT ); }
122 <SINGLE_LINE_COMMENT>{NEWLINE} { BEGIN(INITIAL); }
123 <SINGLE_LINE_COMMENT>"*" { }
124 <SINGLE_LINE_COMMENT>[^*\n]+ { }
125 
126 "'" { BEGIN(LONG_TEXT_SQ); }
127 <LONG_TEXT_SQ>{
128  "*" { yymore(); }
129  {NEWLINE} { yymore(); }
130  [^\\']+ { yymore(); }
131  [\\]. { yymore(); }
132  "'" { yylval->cptr.txt = yytext;
133  yylval->cptr.length = yyleng - 1;
134  BEGIN(INITIAL);
135  return TEXT;
136  }
137  . { yymore(); }
138 }
139 
140 
141 "\"" { BEGIN(LONG_TEXT); }
142 <LONG_TEXT>{
143  "*" { yymore(); }
144  {NEWLINE} { yymore(); }
145  [^\\"]+ { yymore(); }
146  [\\]. { yymore(); }
147  "\"" { yylval->cptr.txt = yytext;
148  yylval->cptr.length = yyleng - 1;
149  BEGIN(INITIAL);
150  return TEXT;
151  }
152  . { yymore(); }
153 }
154 
155 "{" { BEGIN(FIELD_PATH); }
156 <FIELD_PATH>{
157  "*" { yymore(); }
158  "}" { yylval->cptr.txt = yytext;
159  yylval->cptr.length = yyleng - 1;
160  BEGIN(INITIAL);
161  return FIELD;
162  }
163  . { yymore(); }
164 }
165 
166 [ \t] { }
167 [\n] { }
168 
169 <<EOF>> { yylval->integer = token_eof;
170  yyterminate();
171  return EOF_TOKEN;
172  }
173 
174 "!" { return LNOT; }
175 not { return LNOT; }
176 "&&" { return LAND; }
177 and { return LAND; }
178 "||" { return LOR; }
179 or { return LOR; }
180 
181 "==" { yylval->comp = amxp_expr_comp_equal;
182  return COMPERATOR;
183  }
184 "!=" { yylval->comp = amxp_expr_comp_not_equal;
185  return COMPERATOR;
186  }
187 "<" { yylval->comp = amxp_expr_comp_lesser;
188  return COMPERATOR;
189  }
190 ">" { yylval->comp = amxp_expr_comp_bigger;
191  return COMPERATOR;
192  }
193 "<=" { yylval->comp = amxp_expr_comp_lesser_equal;
194  return COMPERATOR;
195  }
196 ">=" { yylval->comp = amxp_expr_comp_bigger_equal;
197  return COMPERATOR;
198  }
199 matches { yylval->comp = amxp_expr_comp_matches;
200  return COMPERATOR;
201  }
202 starts[ \t]{1,}with { yylval->comp = amxp_expr_comp_starts_with;
203  return COMPERATOR;
204  }
205 ends[ \t]{1,}with { yylval->comp = amxp_expr_comp_ends_with;
206  return COMPERATOR;
207  }
208 in { yylval->comp = amxp_expr_comp_in;
209  return COMPERATOR;
210  }
211 "~=" { yylval->comp = amxp_expr_comp_contains;
212  return COMPERATOR;
213  }
214 "^=" { yylval->comp = amxp_expr_comp_equals_ignorecase;
215  return COMPERATOR;
216  }
217 (?i:true) { yylval->boolean = true;
218  return BOOL;
219  }
220 (?i:false) { yylval->boolean = false;
221  return BOOL;
222  }
223 
224 [[:digit:]]+ { char *endptr = NULL;
225  yylval->integer = strtoll(yytext, &endptr, 0);
226  return DIGIT;
227  }
228 -[[:digit:]]+ { char *endptr = NULL;
229  yylval->integer = strtoll(yytext, &endptr, 0);
230  return DIGIT;
231  }
232 
233 [a-zA-Z0-9\-_@]+ { yylval->cptr.txt = yytext;
234  yylval->cptr.length = yyleng;
235  return STRING;
236  }
237 
238 \n|. { return yytext[0]; }
239 
240 %%
241 
242 void amxp_expr_create_lex(amxp_expr_t *expr) {
243  yylex_init(&expr->scanner);
244  yyset_extra(expr, expr->scanner);
245 }
246 
247 void amxp_expr_destroy_lex(amxp_expr_t *expr) {
248  yylex_destroy(expr->scanner);
249 }