File: utscanner.l

package info (click to toggle)
harp 1.5%2Bdata-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 54,032 kB
  • sloc: xml: 286,510; ansic: 143,710; yacc: 1,910; python: 913; makefile: 600; lex: 574; sh: 69
file content (360 lines) | stat: -rw-r--r-- 8,384 bytes parent folder | download | duplicates (2)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright 2014 University Corporation for Atmospheric Research
 *
 * This file is part of the UDUNITS-2 package.  See the file COPYRIGHT
 * in the top-level source-directory of the package for copying and
 * redistribution conditions.
 */
/*
 * lex(1) specification for tokens for the Unidata units package, UDUNITS2.
 */

%option prefix="harp_ut"
%option noyywrap
%option noinput
%option nounput

%{

/* This define is to allow more than one yacc file in the program */
#define yylval utlval

#include <ctype.h>
#include <errno.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "udunits2.h"
#include "utparser.h"

int utrestartScanner = 0;
int utisTime = 0;

int ut_get_bufferpos(YY_BUFFER_STATE buf)
{
    return yy_c_buf_p - buf->yy_ch_buf;
}

/**
 * Decodes a date.
 *
 * @param[in]  text     The text specifying the date to be decoded.
 * @param[in]  format   The format to use for decoding. The order is year (int),
 *                      month (int), and day (int).
 * @param[out] date     The date corresponding to the input.
 * @retval     DATE     Success
 * @retval     ERR      Error
 */
static double decodeDate(
    const char* const   text,
    const char* const   format,
    double* const       date)
{
    int		year;
    int		month = 1;
    int		day = 1;

    (void)sscanf(text, format, &year, &month, &day);
    *date = ut_encode_date(year, month, day);
    return DATE;
}

/**
 * Decodes a clock-time.
 *
 * @param[in] text      The text specifying the clock-time to be decoded.
 * @param[in] format    The format to use for decoding. The order is hour (int),
 *                      minute (int), and second (double).
 * @return              The clock-time corresponding to the input.
 */
static double decodeClock(
    const char* const   text,
    const char* const   format)
{
    int         hour = 0;
    int         minute = 0;
    double      second = 0;

    (void)sscanf(text, format, &hour, &minute, &second);
    if (hour < 0) {
        minute = -minute;
        second = -second;
    }
    return ut_encode_clock(hour, minute, second);
}

/**
 * Decodes a real value.
 *
 * @param[in]  text     Text to be decoded.
 * @param[out] value    Decoded value.
 * @retval     REAL     Success.
 * @retval     ERR      Failure.
 */
static int decodeReal(
    const char* const text,
    double* const     value)
{
    errno = 0;
    *value = strtod(text, NULL);

    if (errno == 0)
        return REAL;

    ut_handle_error_message("Invalid real: \"%s\"", text);
    return ERR;
}

%}

space			[ \t\r\f\v]
sign                    [+-]
int			[0-9]+
int_period		{int}\.
period_int		\.{int}
int_period_int		{int}\.{int}
mantissa		{int_period}|{period_int}|{int_period_int}
real_exp		[eE][+-]?[0-9]+
real			[+-]?({int}{real_exp}|{mantissa}{real_exp}?)
year			[+-]?[0-9]{1,4}
month			0?[1-9]|1[0-2]
day			0?[1-9]|[1-2][0-9]|30|31
hour			[+-]?[0-1]?[0-9]|2[0-3]
minute			[0-5]?[0-9]
second			({minute}|60)(\.[0-9]*)?
middot                  \xc2\xb7
utf8_exp_digit	        \xc2(\xb9|\xb2|\xb3)|\xe2\x81(\xb0|[\xb4-\xb9])
utf8_exp_sign		\xe2\x81\xba|\xe2\x81\xbb
utf8_exponent		{utf8_exp_sign}?{utf8_exp_digit}+
nbsp                    \xc2\xa0
shy                     \xc2\xad
degree                  \xc2\xb0
mu                      \xc2\xb5
blk1                    \xc3([\x80-\x96])
blk2                    \xc3([\x98-\xB6])
blk3                    \xc3([\xB8-\xBF])
latin1		        {nbsp}|{shy}|{degree}|{mu}|{blk1}|{blk2}|{blk3}
utf8_cont               [\x80-\xbf]
utf8_2bytes             [\xc8-\xdf]{utf8_cont}
utf8_3bytes             [\xe0-\xef]{utf8_cont}{utf8_cont}
letter  		[_a-zA-Z]|{latin1}|{utf8_2bytes}|{utf8_3bytes}
alphanum		{letter}|[0-9]
id			%|'|\"|{letter}({alphanum}*{letter})?
broken_date		{year}-{month}(-{day})?
packed_date		{year}({month}{day}?)?
broken_clock		{hour}:{minute}(:{second})?
packed_clock		{hour}({minute}{second}?)?
broken_timestamp	{broken_date}({space}+{broken_clock})?
packed_timestamp	{packed_date}T{packed_clock}?
logref			\({space}*[Rr][Ee](:{space})?{space}*
after                   [Aa][Ff][Tt][Ee][Rr]
from                    [Ff][Rr][Oo][Mm]
since                   [Ss][Ii][Nn][Cc][Ee]
ref                     [Rr][Ee][Ff]
per                     [Pp][Ee][Rr]

%Start		ID_SEEN SHIFT_SEEN DATE_SEEN CLOCK_SEEN

%%
    if (utrestartScanner) {
	BEGIN INITIAL;
	utrestartScanner = 0;
    }

<INITIAL,ID_SEEN>{space}*(@|{after}|{from}|{ref}|{since}){space}* {
    BEGIN SHIFT_SEEN;
    return SHIFT;
}

<INITIAL,ID_SEEN>{space}*({per}|"/"){space}* {
    BEGIN INITIAL;
    return DIVIDE;
}

<INITIAL,ID_SEEN>"-"|"."|"*"|{middot}|{space}+ {
    BEGIN INITIAL;
    return MULTIPLY;
}

<INITIAL,ID_SEEN>("^"|"**")[+-]?{int} {
    int		status;

    if (sscanf(yytext, "%*[*^]%ld", &yylval.ival) != 1) {
        ut_handle_error_message("Invalid integer\n", stderr);

	status	= ERR;
    }
    else {
	status	= EXPONENT;
    }

    return status;
}

<INITIAL,ID_SEEN>{utf8_exponent} {
    int		status = EXPONENT;
    int		exponent = 0;
    int		sign = 1;
    char*	cp = yytext;

    if (strncmp(cp, "\xe2\x81\xba", 3) == 0) {
	cp += 3;
    }
    else if (strncmp(cp, "\xe2\x81\xbb", 3) == 0) {
	sign = -1;
	cp += 3;
    }

    while (cp < yytext + yyleng) {
	int	j;
	static struct {
	    const char*	string;
	    const int	len;
	} utf8_exponents[] = {
	    {"\xe2\x81\xb0", 3},        /* 0 */
	    {"\xc2\xb9",     2},        /* 1 */
	    {"\xc2\xb2",     2},        /* 2 */
	    {"\xc2\xb3",     2},        /* 3 */
	    {"\xe2\x81\xb4", 3},        /* 4 */
	    {"\xe2\x81\xb5", 3},        /* 5 */
	    {"\xe2\x81\xb6", 3},        /* 6 */
	    {"\xe2\x81\xb7", 3},        /* 7 */
	    {"\xe2\x81\xb8", 3},        /* 8 */
	    {"\xe2\x81\xb9", 3},        /* 9 */
	};

	exponent *= 10;

	for (j = 0; j < 10; j++) {
	    int	len = utf8_exponents[j].len;

	    if (strncmp(cp, utf8_exponents[j].string, len) == 0) {
		exponent += j;
		cp += len;
		break;
	    }
	}

	if (j >= 10) {
	    status = ERR;
	    break;
	}
    }

    if (status == EXPONENT)
	yylval.ival = sign * exponent;

    BEGIN INITIAL;
    return status;
}

<SHIFT_SEEN>{broken_date}(T|{space}*) {
    BEGIN DATE_SEEN;
    return decodeDate((char*)yytext, "%d-%d-%d", &yylval.rval);
}

<SHIFT_SEEN>{packed_date}(T|{space}*) {
    if (utisTime) {
        BEGIN DATE_SEEN;
        return decodeDate((char*)yytext, "%4d%2d%2d", &yylval.rval);
    }
    else {
        BEGIN INITIAL;
        return decodeReal((char*)yytext, &yylval.rval);
    }
}

<DATE_SEEN>{broken_clock}{space}* {
    yylval.rval = decodeClock((char*)yytext, "%d:%d:%lf");
    BEGIN CLOCK_SEEN;
    return CLOCK;
}

<DATE_SEEN>{packed_clock}{space}* {
    yylval.rval = decodeClock((char*)yytext, "%2d%2d%lf");
    BEGIN CLOCK_SEEN;
    return CLOCK;
}

<CLOCK_SEEN>{sign}?{int}:{int} {
    yylval.rval	= decodeClock((char*)yytext, "%d:%d");
    BEGIN INITIAL;
    return CLOCK;
}

<CLOCK_SEEN>{sign}{int} {
    yylval.rval	= (yyleng <= 3)
                        ? decodeClock((char*)yytext, "%d")
                        : (yyleng == 4)
                            ? decodeClock((char*)yytext, "%2d%d")
                            : decodeClock((char*)yytext, "%3d%d");
    BEGIN INITIAL;
    return CLOCK;
}

<CLOCK_SEEN>{int} {
    yylval.rval	= (yyleng <= 2)
                        ? decodeClock((char*)yytext, "%d")
                        : (yyleng == 3)
                            ? decodeClock((char*)yytext, "%1d%d")
                            : decodeClock((char*)yytext, "%2d%d");
    BEGIN INITIAL;
    return CLOCK;
}

<INITIAL,SHIFT_SEEN>{real} {
    BEGIN INITIAL;
    return decodeReal((char*)yytext, &yylval.rval);
}

<INITIAL,ID_SEEN,SHIFT_SEEN>[+-]?{int} {
    int		status;

    errno	= 0;
    yylval.ival = atol((char*)yytext);

    if (errno == 0) {
	status	= INT;
    } else {
        ut_handle_error_message("Invalid integer\n", stderr);

	status	= ERR;
    }

    BEGIN INITIAL;
    return status;
}

(log|lg){space}*{logref} {
    yylval.rval = 10;
    return LOGREF;
}

ln{space}*{logref} {
    yylval.rval = M_E;
    return LOGREF;
}

lb{space}*{logref} {
    yylval.rval = 2;
    return LOGREF;
}

<INITIAL,CLOCK_SEEN>{id} {
    yylval.id = strdup((char*)yytext);

    BEGIN ID_SEEN;
    return ID;
}

. {
    BEGIN INITIAL;
    return yytext[0];
}

%%