From 82f47550fe3327cce6f2e0e1bf62e81d9ebcf90c Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 18 Jan 2023 13:03:34 -0600 Subject: begin reentrant refactor --- yy/kalmia.l | 92 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 27 deletions(-) (limited to 'yy/kalmia.l') diff --git a/yy/kalmia.l b/yy/kalmia.l index 2472bdc..916f576 100644 --- a/yy/kalmia.l +++ b/yy/kalmia.l @@ -1,26 +1,39 @@ -%{ -#include -#include "y.tab.h" - -char *copy_str(char *); -int line_num; +%option noinput nounput noyywrap 8bit nodefault +%option reentrant bison-bridge bison-locations +%start STRING +%{ +#include +#include "kalmia.tab.h" +#define YY_USER_ACTION \ + yylloc->first_line = yylloc->last_line; \ + yylloc->first_column = yylloc->last_column; \ + if (*yytext == '\n') { \ + yylloc->last_line += 1; \ + yylloc->last_column = 0; \ + } \ + else { \ + yylloc->last_column += yyleng; \ + } %} S \x20\x0a\x0d\x09 DATE [0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[A-Z]* +ATTR ([\x20\x0a\x0d\x09]*)?"=" - -%start STRING - %% -"" { return PROLOG; } -[\x20\x0a\x0d\x09] { if (*yytext == '\n') { line_num += 1; } } +"" { + /* xml prologue */ + return PROLOG; +} +[\x20\x0a\x0d\x09] { + /* ignore whitespace */ +} "<" { return S_TAG_OPEN; } @@ -29,24 +42,49 @@ DATE [0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[A-Z]* "/>" { return EMPTY_TAG_CLOSE; } -[a-zA-Z_:][a-zA-Z0-9\.\-_:]* { yylval.sval = copy_str(yytext); return NAME; } - -"=" { return *yytext; } +[a-zA-Z_:][a-zA-Z0-9\.\-_:]* { + /* generic tag name */ + yylval->sval = strdup(yytext); return NAME; +} +[a-zA-Z_:][a-zA-Z0-9\.\-_:]*/([\x20\x0a\x0d\x09])*"=" { + /* generic attribute key */ + yylval->sval = strdup(yytext); + return ATTR; +} +"=" { + /* attribute "=" */ + return *yytext; +} -"\"" { BEGIN(STRING); return *yytext; } -"\"" { BEGIN(INITIAL); return *yytext; } -[^"] { yylval.cval = *yytext; return CHAR; } --?[0-9]+ { yylval.lval = strtol(yytext, NULL, 10); return INTEGER; } --?[0-9]+\.?[0-9]* { yylval.dval = strtod(yytext, NULL); return DOUBLE; } -{DATE} { return DATE; } +"\"" { + /* begin a string */ + BEGIN(STRING); + return *yytext; +} +"\"" { + /* end a string */ + BEGIN(INITIAL); + return *yytext; +} +[^"]+ { + /* within a string */ + yylval->sval = strdup(yytext); + return TEXT; +} -%% -char * copy_str(char *str) -{ - size_t len = strlen(str) + 1; - char *copy = malloc(sizeof(char) * len); - strncpy(copy, str, len); - return copy; +-?[0-9]+ { + /* integers */ + yylval->lval = strtol(yytext, NULL, 10); + return INTEGER; +} +-?[0-9]+\.?[0-9]* { + /* doubles */ + yylval->dval = strtod(yytext, NULL); + return DOUBLE; +} +{DATE} { + /* dates */ + return DATE; } -- cgit v1.2.1