4 * Date: Sat Jan 9 16:35:18 2010
6 * GNU recutils - Selection Expressions lexer
10 /* Copyright (C) 2010-2019 Jose E. Marchesi */
12 /* This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /* Automake needs this. */
27 %option outfile="lex.yy.c"
33 %option extra-type="void *"
35 %option header-file="rec-sex-lex.h"
39 /* This code goes at the "top" of the generated file. */
44 #include <rec-utils.h>
46 #include <rec-sex-ast.h>
47 #include <rec-sex-parser.h>
48 #include <rec-sex-tab.h>
50 #define YY_INPUT(buf,result,max_size) \
53 if ((ci = rec_sex_parser_getc ((rec_sex_parser_t) yyextra)) == -1) \
64 /* Forward prototypes for functions defined below. */
65 char *rec_sex_lex_extract_name (char *str);
66 char *rec_sex_lex_extract_subname (char *str);
67 bool rec_sex_lex_extract_index (char *str, int *num);
75 FIELD_NAME {LETTER}[a-zA-Z0-9_]*
76 STRING '([^']|\\(.|\n))*'|\"([^"]|\\(.|\n))*\"
78 /* Please do not touch this comment' */
82 {BLANK} { /* Ignore whitespace */ }
84 "-"?{DIGIT}*\.{DIGIT}+ {
90 yylval->node = rec_sex_ast_node_new ();
91 rec_atod (yytext, &real_value);
92 rec_sex_ast_node_set_real (yylval->node, real_value);
94 return REC_SEX_TOK_REAL;
97 "-"?((0x[0-9a-fA-F]+)|{DIGIT}+) {
99 * Create an integer node.
103 yylval->node = rec_sex_ast_node_new ();
104 rec_atoi (yytext, &integer_value);
105 rec_sex_ast_node_set_int (yylval->node, integer_value);
107 return REC_SEX_TOK_INT;
110 "+" { return REC_SEX_TOK_ADD; }
111 "-" { return REC_SEX_TOK_SUB; }
112 "/" { return REC_SEX_TOK_DIV; }
113 "%" { return REC_SEX_TOK_MOD; }
114 ">>" { return REC_SEX_TOK_AFTER; }
115 "<<" { return REC_SEX_TOK_BEFORE; }
116 "<=" { return REC_SEX_TOK_LTE; }
117 ">=" { return REC_SEX_TOK_GTE; }
118 "==" { return REC_SEX_TOK_SAMETIME; }
119 "=>" { return REC_SEX_TOK_IMPLIES; }
120 "*" { return REC_SEX_TOK_MUL; }
121 "=" { return REC_SEX_TOK_EQL; }
122 "!=" { return REC_SEX_TOK_NEQ; }
123 "!" { return REC_SEX_TOK_NOT; }
124 "~" { return REC_SEX_TOK_MAT; }
125 ">" { return REC_SEX_TOK_GT; }
126 "<" { return REC_SEX_TOK_LT; }
127 "(" { return REC_SEX_TOK_BP; }
128 ")" { return REC_SEX_TOK_EP; }
129 "&&" { return REC_SEX_TOK_AND; }
130 "||" { return REC_SEX_TOK_OR; }
131 "#" { return REC_SEX_TOK_SHARP; }
132 "?" { return REC_SEX_TOK_QM; }
133 ":" { return REC_SEX_TOK_COLON; }
134 "&" { return REC_SEX_TOK_AMP; }
136 {FIELD_NAME}(\.{FIELD_NAME})?(\[[0-9]+\])? {
139 char *name, *subname;
142 match = strdup (yytext);
143 rec_sex_lex_extract_index (match, &index);
144 name = rec_sex_lex_extract_name (match);
145 subname = rec_sex_lex_extract_subname (match);
147 /* Create a name node. */
148 yylval->node = rec_sex_ast_node_new ();
149 rec_sex_ast_node_set_name (yylval->node, name, subname);
150 rec_sex_ast_node_set_index (yylval->node, index);
151 res = REC_SEX_TOK_NAM;
161 * Create a string node.
164 /* Strip the quoting characters */
165 yytext[strlen(yytext) - 1] = 0;
167 yylval->node = rec_sex_ast_node_new ();
168 rec_sex_ast_node_set_str (yylval->node, yytext + 1);
170 return REC_SEX_TOK_STR;
173 . { return REC_SEX_TOK_ERR; }
178 rec_sex_lex_extract_name (char *str)
185 while ((*p != '[') && (*p != '.') && (*p != 0))
191 res = malloc (size + 1);
192 memcpy (res, str, size);
199 rec_sex_lex_extract_subname (char *str)
203 /* If there is not a subname denoted in STR then this function
207 while ((*p != '.') && (*p != '\0'))
214 /* There is not a second name. */
218 p++; /* Skip the dot separator. */
220 return rec_sex_lex_extract_name (p);
224 rec_sex_lex_extract_index (char *str,
232 /* Note that this function assumes syntax correctness in STR if a
237 while ((*p != 0) && (*p != '['))
248 p++; /* Pass the [. */
251 aux[aux_size++] = *p;
256 if (!rec_atoi (aux, num))
267 /* End of rec-sex.l */