]> begriffs open source - sa-parse/blob - src/csv.l
Better error handling in csv_parser_parse_file()
[sa-parse] / src / csv.l
1 /* csv.l */
2
3 /* disable unused functions so we don't
4    get compiler warnings about them */
5
6 %option noyywrap nounput noinput
7
8 /* change our prefix from yy to csv */
9
10 %option prefix="csv"
11
12 /* use the pure parser calling convention */
13
14 %option reentrant bison-bridge
15
16 %{
17 #include "csv.tab.h"
18
19 #define YY_EXIT_FAILURE ((void)yyscanner, EXIT_FAILURE)
20
21 /* XOPEN for strdup */
22 #define _XOPEN_SOURCE 600
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* seems like a bug that I have to do this, since flex
27    should know prefix=csv and match bison's CSVSTYPE */
28 #define YYSTYPE CSVSTYPE
29
30 %}
31
32 %%
33
34 \"([^"]|\"\")*\" {
35         /* yyleng is precomputed strlen(yytext) */
36     size_t i, n = yyleng;
37     char *s;
38
39     s = yylval->str = calloc(n, 1);
40     if (!s)
41         return FIELD;
42
43         /* copy yytext, changing "" to " */
44     for (i = 1 /*skip 0="*/; i < n-1; i++)
45     {
46         *s++ = yytext[i];
47         if (yytext[i] == '"')
48             i++; /* skip second one */
49     }
50     return FIELD;
51 }
52
53 [^",\r\n]+ { yylval->str = strdup(yytext); return FIELD; }
54 \n|\r\n    { return CRLF; }
55 .          { return *yytext; }
56
57 %%