File: date_parser2.l

package info (click to toggle)
gtkpod 2.1.5-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,428 kB
  • ctags: 6,307
  • sloc: ansic: 51,604; xml: 16,135; sh: 11,916; cpp: 7,545; perl: 1,449; makefile: 1,376; lex: 638; awk: 73; python: 35
file content (235 lines) | stat: -rw-r--r-- 6,366 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- mode: c -*-
|
|  Copyright (C) 2002-2007 Jorg Schuler <jcsjcs at users.sourceforge.net>
|  Part of the gtkpod project.
| 
|  URL: http://gtkpod.sourceforge.net/
| 
|  This program is free software; you can redistribute it and/or modify
|  it under the terms of the GNU General Public License as published by
|  the Free Software Foundation; either version 2 of the License, or
|  (at your option) any later version.
| 
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
| 
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
| 
|  iTunes and iPod are trademarks of Apple
| 
|  This product is not supported/written/published by Apple!
*/

%{
#include <stdlib.h>
#include <string.h>
#include "date_parser.h"
#include "libgtkpod/misc.h"
#include "libgtkpod/itdb.h"
/* Will be set to TRUE if error occurred */
    static gboolean dp_error;
/* Will be set to TRUE if construct (< date, = date...) was not
   recognized */
    static gboolean dp_construct_error;
/* Variable to hold lower and upper limit */
    static time_t lower_stamp, upper_stamp;
/* Pointer to input string to be parsed */
    static gchar *dp_strp = NULL;
/* type of interval */
    typedef enum {
	INT_NORMAL=0,    /* normal interval */
	INT_NO_LOWER=2,  /* interval with no lower limit */
	INT_NO_UPPER=4,  /* interval with no upper limit */
    } IntervalType;
    static IntervalType dp_int_type;
/* We don't read from a stream but from a string buffer. This macro
   will copy a maximum of @max_size chars from dp_strp to @buf,
   writing the number of chars copied into @result. If no characters
   are copied, YY_NULL is written into @result. */
#define YY_INPUT(buf,result,max_size) \
    { \
    if (!dp_strp || !dp_strp[0]) result = YY_NULL; \
    else \
      { \
        gint i; \
        for (i=0; (i<max_size && *dp_strp); ++i)  buf[i] = *dp_strp++; \
        result = i; \
      } \
    }
    static void dp_ll (gchar *str);
    static void dp_LL (gchar *str);
%}

/* stop parsing after end of string is reached */
%option noyywrap
/* avoid compiler warning: `yyunput' defined but not used */
%option nounput
/* We have several parsers in one executable, so we must use the
   prefix option to avoid name space clashes. Note that this only
   works with flex, but not with lex. */
%option prefix="lexdp2"
/* Unfortunately, the prefix option also changes the output filename
   and thereby breaks the automake script. Therefore I define the
   output filename here. However, this is not portable -- on other
   systems the default output filename expected by the automake
   scripts may be lexyy.c or similar. To make the code portable again
   (hopefully), I also define "LEX_OUTPUT_ROOT = lex.yy" in
   Makefile.am */
%option outfile="lex.yy.c"

DATECHARS [^<>=]+


%%

^"<"{DATECHARS}$ {
    gchar *strp;
#if DP_DEBUG
    printf ("<: '%s'\n", yytext);
#endif
    dp_int_type |= INT_NO_LOWER;
    strp = strchr (yytext, '<');
    if (strp)  dp_error = !dp_parse (strp+1, &upper_stamp, FALSE, TRUE);
    else       dp_error = TRUE;
}

^">"{DATECHARS}$ {
    gchar *strp;
#if DP_DEBUG
    printf (">: '%s'\n", yytext);
#endif
    dp_int_type |= INT_NO_UPPER;
    strp = strchr (yytext, '>');
    if (strp) dp_error = !dp_parse (strp+1, &lower_stamp, TRUE, TRUE);
    else      dp_error = TRUE;
}

^"="{DATECHARS}$ {
    gchar *strp;
#if DP_DEBUG
    printf ("=: '%s'\n", yytext);
#endif
    strp = strchr (yytext, '=');
    if (strp)
    {
	dp_error = !dp_parse (strp+1, &lower_stamp, TRUE, FALSE);
	if (!dp_error)
	    dp_error = !dp_parse (strp+1, &upper_stamp, FALSE, FALSE);
    }
    else      dp_error = TRUE;
    
}

^{DATECHARS}"<"[ \t]*"d"[ \t]*"<"{DATECHARS}$ {
    dp_ll (yytext);
}

^{DATECHARS}"<"[ \t]*"<"{DATECHARS}$ {
    dp_ll (yytext);
}

^{DATECHARS}">"[ \t]*"d"[ \t]*">"{DATECHARS}$ {
    dp_LL (yytext);
}

^{DATECHARS}">"[ \t]*">"{DATECHARS}$ {
    dp_LL (yytext);
}

[\n]        /* ignore */

.           {
    dp_error = TRUE;
    dp_construct_error = TRUE;
}

%%


/* pattern " < < " */
static void dp_ll (gchar *str)
{
    gchar *strp;
#if DP_DEBUG
    printf ("<<: '%s'\n", yytext);
#endif
    strp = strchr (str, '<');
    if (strp)
    {
	*strp = 0;
	dp_error = !dp_parse (str, &lower_stamp, TRUE, TRUE);
	strp = strchr (strp+1, '<');
	if (!dp_error && strp)
	      dp_error = !dp_parse (strp+1, &upper_stamp, FALSE, TRUE);
	else  dp_error = TRUE;
    }
    else  dp_error = TRUE;
}


/* pattern " > > " */
static void dp_LL (gchar *str)
{
    gchar *strp;
#if DP_DEBUG
    printf (">>: '%s'\n", yytext);
#endif
    strp = strchr (str, '>');
    if (strp)
    {
	*strp = 0;
	dp_error = !dp_parse (str, &upper_stamp, FALSE, TRUE);
	strp = strchr (strp+1, '>');
	if (!dp_error && strp)
	      dp_error = !dp_parse (strp+1, &lower_stamp, TRUE, TRUE);
	else  dp_error = TRUE;
    }
    else  dp_error = TRUE;
}



void dp2_parse (TimeInfo *ti)
{
    /* for the 'end of line ("$") rule to work, we need a "\n" at the
       end of the string... */
    gchar *str = g_strdup_printf ("%s\n", ti->int_str);
    /* set string to parse */
    dp_strp = str;
    /* no error occurred (yet) */
    dp_error = FALSE;
    dp_construct_error = FALSE;
    /* set interval type to normal */
    dp_int_type = INT_NORMAL;
    /* parse the string */
    yylex ();
    /* free memory */
    g_free (str);
    str = NULL;
    /* print error message */
    if (dp_construct_error)
	gtkpod_warning ("Date parser: did not recognize construct:\n   '%s'\n",
			ti->int_str);
    if (dp_error)
    {   /* error occurred -> invalidate TimeInfo */
	ti->valid = FALSE;
	ti->lower = 0;
	ti->upper = 0;
    }
    else
    {   /* no error occurred -> set the information accordingly */
	ti->valid = TRUE;
	if (dp_int_type & INT_NO_LOWER) ti->lower = 0;
	else   	ti->lower = lower_stamp;
	if (dp_int_type & INT_NO_UPPER) ti->upper = -1; /* -1 = 2^32-1 */
	else    ti->upper = upper_stamp;
    }
#if DP_DEBUG
    printf ("valid: %d, int_type: %d, lower: %u, upper: %u\n",
	    ti->valid, dp_int_type, ti->lower, ti->upper);
#endif
}