doubles parsed
This commit is contained in:
parent
9fc1514678
commit
27bc848eed
@ -23,26 +23,37 @@ static int parse_uint(const char *s, const char *e, unsigned *u) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int parse_double(const char *s0, const char *s1, double *d) {
|
||||
char *finish;
|
||||
assert(d && s0 && s0 < s1);
|
||||
*d = strtod(s0, &finish);
|
||||
return !errno && ((finish == s1) || (errno = EDOM, 0));
|
||||
}
|
||||
|
||||
static int lex_line(const char *YYCURSOR, unsigned *const u) {
|
||||
const char *YYMARKER, *o1, *o2, *o3, *o4;
|
||||
double d;
|
||||
/*!stags:re2c format = 'const char *@@;\n'; */
|
||||
|
||||
/*!re2c
|
||||
re2c:yyfill:enable = 0;
|
||||
re2c:flags:tags = 1;
|
||||
re2c:define:YYCTYPE = char;
|
||||
|
||||
keyword = [a-z][a-z0-9_-]{0,64}; // uppercase is lame
|
||||
octet = [0-9] | [1-9][0-9] | [1][0-9][0-9] | [2][0-4][0-9] | [2][5][0-5];
|
||||
dot = [.];
|
||||
end = [\x00];
|
||||
|
||||
eol = [\n];
|
||||
w = [ \t]*;
|
||||
|
||||
glider = "{" "glider" ":";
|
||||
flight = "{" "}";
|
||||
|
||||
|
||||
double = [0-9]* "." [0-9]+ | [0-9]+;
|
||||
*/
|
||||
errno = 0; /* For accuracy detecting errors. */
|
||||
text:
|
||||
/*!re2c
|
||||
"\\[" | "\\" {
|
||||
printf("Got a map.\n");
|
||||
if(!parse_double(o1, o2, &d)) return 0;
|
||||
printf("Latitude %f.\n", d);
|
||||
if(!parse_double(o3, o4, &d)) return 0;
|
||||
printf("Longitude %f.\n", d);
|
||||
goto text;
|
||||
}
|
||||
* { return errno = EILSEQ, 0; }
|
||||
*/
|
||||
custom:
|
||||
/*!re2c
|
||||
glider = "glider" ":" ;
|
||||
flight = "flight" ":" ;
|
||||
* { return errno = EILSEQ, 0; }
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
unsigned u;
|
||||
int success;
|
||||
success = lex_line("1.2.3.4", &u);
|
||||
assert(success);
|
||||
printf("%x\n", u);
|
||||
if(!lex_line("1.2.3.4", &u)) goto catch;
|
||||
printf("0x%x\n", u);
|
||||
if(!lex_line("1.2.3.49999999999999999", &u)) goto catch;
|
||||
return EXIT_SUCCESS;
|
||||
catch:
|
||||
perror("interpret");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user