summaryrefslogtreecommitdiff
path: root/yy/kalmia.l
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-01-18 13:03:34 -0600
committersanine <sanine.not@pm.me>2023-01-18 13:03:34 -0600
commit82f47550fe3327cce6f2e0e1bf62e81d9ebcf90c (patch)
tree18c15c14a3f1623a4343d36a2f518ee6f51f204a /yy/kalmia.l
parent8d5389d66ef79b58a0fff32fa2b01b4206bfb311 (diff)
begin reentrant refactor
Diffstat (limited to 'yy/kalmia.l')
-rw-r--r--yy/kalmia.l92
1 files changed, 65 insertions, 27 deletions
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 <stdio.h>
-#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 <string.h>
+#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
-
%%
-<INITIAL>"<?xml".*"?>" { return PROLOG; }
-<INITIAL>[\x20\x0a\x0d\x09] { if (*yytext == '\n') { line_num += 1; } }
+<INITIAL>"<?xml".*"?>" {
+ /* xml prologue */
+ return PROLOG;
+}
+<INITIAL>[\x20\x0a\x0d\x09] {
+ /* ignore whitespace */
+}
<INITIAL>"<" { 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]*
<INITIAL>"/>" { return EMPTY_TAG_CLOSE; }
-<INITIAL>[a-zA-Z_:][a-zA-Z0-9\.\-_:]* { yylval.sval = copy_str(yytext); return NAME; }
-
-<INITIAL>"=" { return *yytext; }
+<INITIAL>[a-zA-Z_:][a-zA-Z0-9\.\-_:]* {
+ /* generic tag name */
+ yylval->sval = strdup(yytext); return NAME;
+}
+<INITIAL>[a-zA-Z_:][a-zA-Z0-9\.\-_:]*/([\x20\x0a\x0d\x09])*"=" {
+ /* generic attribute key */
+ yylval->sval = strdup(yytext);
+ return ATTR;
+}
+<INITIAL>"=" {
+ /* attribute "=" */
+ return *yytext;
+}
-<INITIAL>"\"" { BEGIN(STRING); return *yytext; }
-<STRING>"\"" { BEGIN(INITIAL); return *yytext; }
-<STRING>[^"] { yylval.cval = *yytext; return CHAR; }
-<INITIAL>-?[0-9]+ { yylval.lval = strtol(yytext, NULL, 10); return INTEGER; }
-<INITIAL>-?[0-9]+\.?[0-9]* { yylval.dval = strtod(yytext, NULL); return DOUBLE; }
-<INITIAL>{DATE} { return DATE; }
+<INITIAL>"\"" {
+ /* begin a string */
+ BEGIN(STRING);
+ return *yytext;
+}
+<STRING>"\"" {
+ /* end a string */
+ BEGIN(INITIAL);
+ return *yytext;
+}
+<STRING>[^"]+ {
+ /* 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;
+<INITIAL>-?[0-9]+ {
+ /* integers */
+ yylval->lval = strtol(yytext, NULL, 10);
+ return INTEGER;
+}
+<INITIAL>-?[0-9]+\.?[0-9]* {
+ /* doubles */
+ yylval->dval = strtod(yytext, NULL);
+ return DOUBLE;
+}
+<INITIAL>{DATE} {
+ /* dates */
+ return DATE;
}