summaryrefslogtreecommitdiffstats
path: root/src/config.l
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-03-29 03:19:50 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-03-29 03:19:50 +0200
commiteb0c48b789bbf41745287adb6b2949f84e7e9171 (patch)
tree212f1156670e2f600d958116738425caa35d815e /src/config.l
parentaadf0a94b436990202cd2f13f1fe8528a9fd183c (diff)
downloadfastd-eb0c48b789bbf41745287adb6b2949f84e7e9171.tar
fastd-eb0c48b789bbf41745287adb6b2949f84e7e9171.zip
Config parse: add location tracking for nice error messages
Diffstat (limited to 'src/config.l')
-rw-r--r--src/config.l83
1 files changed, 50 insertions, 33 deletions
diff --git a/src/config.l b/src/config.l
index eeb20ae..f232aee 100644
--- a/src/config.l
+++ b/src/config.l
@@ -27,6 +27,7 @@
%option prefix="fastd_config_yy"
%option noyywrap
%option bison-bridge
+%option bison-locations
%option reentrant
%top {
@@ -38,27 +39,37 @@
%s COMMENT
%%
+%{
+ #define UPDATE_LOCATION do { \
+ yylloc->first_line = yylloc->last_line; \
+ yylloc->first_column = yylloc->last_column+1; \
+ yylloc->last_column += yyleng; \
+ } while (0)
+%}
+
<INITIAL>{
-[0-9]+ { yylval->num = atoi(yytext); return TOK_INTEGER; }
-
-interface { return TOK_INTERFACE; }
-bind { return TOK_BIND; }
-mtu { return TOK_MTU; }
-mode { return TOK_MODE; }
-protocol { return TOK_PROTOCOL; }
-peer { return TOK_PEER; }
-address { return TOK_ADDRESS; }
-secret { return TOK_SECRET; }
-key { return TOK_KEY; }
-include { return TOK_INCLUDE; }
-as { return TOK_AS; }
-any { return TOK_ANY; }
-tap { return TOK_TAP; }
-tun { return TOK_TUN; }
-on { return TOK_ON; }
-up { return TOK_UP; }
+[0-9]+ { UPDATE_LOCATION; yylval->num = atoi(yytext); return TOK_INTEGER; }
+
+interface { UPDATE_LOCATION; return TOK_INTERFACE; }
+bind { UPDATE_LOCATION; return TOK_BIND; }
+mtu { UPDATE_LOCATION; return TOK_MTU; }
+mode { UPDATE_LOCATION; return TOK_MODE; }
+protocol { UPDATE_LOCATION; return TOK_PROTOCOL; }
+peer { UPDATE_LOCATION; return TOK_PEER; }
+address { UPDATE_LOCATION; return TOK_ADDRESS; }
+secret { UPDATE_LOCATION; return TOK_SECRET; }
+key { UPDATE_LOCATION; return TOK_KEY; }
+include { UPDATE_LOCATION; return TOK_INCLUDE; }
+as { UPDATE_LOCATION; return TOK_AS; }
+any { UPDATE_LOCATION; return TOK_ANY; }
+tap { UPDATE_LOCATION; return TOK_TAP; }
+tun { UPDATE_LOCATION; return TOK_TUN; }
+on { UPDATE_LOCATION; return TOK_ON; }
+up { UPDATE_LOCATION; return TOK_UP; }
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} {
+ UPDATE_LOCATION;
+
if (!inet_pton(AF_INET, yytext, &yylval->addr)) {
yylval->str = "invalid address";
return -1;
@@ -67,14 +78,17 @@ up { return TOK_UP; }
return TOK_ADDR;
}
-[;:\{\}] { return yytext[0]; }
+[;:\{\}] { UPDATE_LOCATION; return yytext[0]; }
-[ \t\n] ;
+[ \t] { yylloc->last_column++; }
+\n { yylloc->last_column = 0; yylloc->last_line++; }
}
-<INITIAL>\" BEGIN(STRING);
-<STRING>[^"\\]* yymore();
-<STRING>\\(.|\n) yymore();
+<INITIAL>\" { UPDATE_LOCATION; BEGIN(STRING); }
+<STRING>[^"\\\n] { yylloc->last_column++; yymore(); }
+<STRING>\n { yylloc->last_line++; yylloc->last_column = 0; yymore(); }
+<STRING>\\. { yylloc->last_column+=2; yymore(); }
+<STRING>\\\n { yylloc->last_line++; yylloc->last_column = 0; yymore(); }
<STRING>\" {
int i, esc = 0;
@@ -87,29 +101,32 @@ up { return TOK_UP; }
yytext[yyleng-esc-1] = 0;
yylval->str = strdup(yytext);
BEGIN(INITIAL);
+ yylloc->last_column++;
return TOK_STRING;
}
-<INITIAL>\[ BEGIN(ADDR6);
-<ADDR6>[^\]]+ {
+<INITIAL>\[ { UPDATE_LOCATION; BEGIN(ADDR6); }
+<ADDR6>[0-9a-fA-F:]+ {
+ yylloc->last_column += yyleng;
if (!inet_pton(AF_INET6, yytext, &yylval->addr6)) {
yylval->str = "invalid address";
return -1;
}
-
- return TOK_ADDR6;
}
-<ADDR6>\] BEGIN(INITIAL);
+<ADDR6>\] { yylloc->last_column++; BEGIN(INITIAL); return TOK_ADDR6; }
-<INITIAL>#.* /* ignore */
-<INITIAL>\/\/.* /* ignore */
+<INITIAL>#.* { yylloc->last_column += yyleng; }
+<INITIAL>\/\/.* { yylloc->last_column += yyleng; }
-<INITIAL>\/\* BEGIN(COMMENT);
-<COMMENT>\*\/ BEGIN(INITIAL);
-<COMMENT>.|\n /* ignore everything */
+<INITIAL>\/\* { UPDATE_LOCATION; BEGIN(COMMENT); }
+<COMMENT>\*\/ { yylloc->last_column += yyleng; BEGIN(INITIAL); }
+<COMMENT>. { yylloc->last_column++; }
+<COMMENT>\n { yylloc->last_line++; yylloc->last_column = 0; }
. {
+ yylloc->first_line = yylloc->last_line;
+ yylloc->first_column = yylloc->last_column+1;
yylval->str = "invalid character";
return -1;
}