-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdatetime_scanner.lex
87 lines (74 loc) · 3.43 KB
/
datetime_scanner.lex
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
%option c++
%option yyclass="DatetimeScanner"
%option noyywrap
%option never-interactive
%option yylineno
%option case-sensitive
%option prefix="datetime"
%{
#include "common/time/parser/DatetimeScanner.h"
#include "DatetimeParser.hpp"
#include <stdlib.h>
#include <string>
#define YY_USER_ACTION \
yylloc->step(); \
yylloc->columns(yyleng);
%}
DEC ([0-9])
L_BRACKET "["
R_BRACKET "]"
%%
/* date time shape type prefix */
"T" { return TokenType::KW_TIME_ID; }
":" { return TokenType::TIME_DELIMITER; }
" " { return TokenType::SPACE; }
"+" { return TokenType::POSITIVE; }
"-" { return TokenType::NEGATIVE; }
{DEC}+ {
try {
folly::StringPiece text(yytext, yyleng);
uint64_t val = folly::to<uint64_t>(text);
yylval->intVal = val;
} catch (...) {
throw DatetimeParser::syntax_error(*yylloc, "Invalid integer:");
}
return TokenType::INTEGER;
}
{DEC}+\.{DEC}+ {
try {
folly::StringPiece text(yytext, yyleng);
yylval->doubleVal = folly::to<double>(text);
} catch (...) {
throw DatetimeParser::syntax_error(*yylloc, "Invalid double value:");
}
return TokenType::DOUBLE;
}
\[[^\]]+\] {
std::string *str = new std::string(yytext + 1, yyleng - 2);
yylval->strVal = str;
return TokenType::TIME_ZONE_NAME;
}
. {
/**
* Any other unmatched byte sequences will get us here,
* including the non-ascii ones, which are negative
* in terms of type of `signed char'. At the same time, because
* Bison translates all negative tokens to EOF(i.e. YY_NULL),
* so we have to cast illegal characters to type of `unsinged char'
* This will make Bison receive an unknown token, which leads to
* a syntax error.
*
* Please note that it is not Flex but Bison to regard illegal
* characters as errors, in such case.
*/
return static_cast<unsigned char>(yytext[0]);
/**
* Alternatively, we could report illegal characters by
* throwing a `syntax_error' exception.
* In such a way, we could distinguish illegal characters
* from normal syntax errors, but at cost of poor performance
* incurred by the expensive exception handling.
*/
// throw DatetimeParser::syntax_error(*yylloc, "char illegal");
}
%%