]> begriffs open source - sa-parse/blob - src/csv.l
WIP: implement API on top of CSV parser
[sa-parse] / src / csv.l
1 /* csv.l */
2
3 %{
4 #define _XOPEN_SOURCE 600
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "csv.tab.h"
9 %}
10
11 %%
12
13 \"([^"]|\"\")*\" {
14         /* yyleng is precomputed strlen(yytext) */
15     size_t i, n = yyleng;
16     char *s;
17
18     s = yylval.str = calloc(n, 1);
19     if (!s)
20         return FIELD;
21
22         /* copy yytext, changing "" to " */
23     for (i = 1 /*skip 0="*/; i < n-1; i++)
24     {
25         *s++ = yytext[i];
26         if (yytext[i] == '"')
27             i++; /* skip second one */
28     }
29     return FIELD;
30 }
31
32 [^",\r\n]+ { yylval.str = strdup(yytext); return FIELD; }
33 \n|\r\n    { return CRLF; }
34 .          { return *yytext; }
35
36 %%