/* $Id: u.l,v 1.6 2003/11/13 20:16:37 hal Exp $ */ %option noyywrap %{ #include #include #include #include #include /* expect bison -d to create y.tab.h */ #include "ubf_a.h" #include "u.tab.h" #define YY_DECL int yylex(YYSTYPE *lvalp) #define LVALP ((ubfa_term_t *)lvalp) %} BLANK [[:blank:]] DIGIT [[:digit:]] MINUS - UBF_SPECIAL [&$~#{}\[\]] %x IN_STRING IN_CONST IN_SEM_TAG IN_BIN_DATA %% %{ #define UBF_BUF_LEN 8192 unsigned char ubf_buf[UBF_BUF_LEN]; unsigned char * ubf_buf_ptr; long ival; char * cp; int len; ubfprintf(("enter yylex()\n")); %} {MINUS}?{DIGIT}+ { /* collect optional minus and digits for UBF(A) integer */ ival = strtol(yytext, &cp, 10); if(cp != yytext) { /* ival is result */ LVALP->t = ubfa_integer; LVALP->g = NULL; LVALP->d.i = ival; return INTEGER; } else { ubfprintf(("got %s\n", yytext)); } } \" { BEGIN(IN_STRING); ubf_buf_ptr = ubf_buf; len = 0; } \" { BEGIN(INITIAL); *ubf_buf_ptr = '\0'; if(len >= UBF_BUF_LEN) { ubfprintf(("warning: truncating string from %d to $d chars\n",len,UBF_BUF_LEN-1)); len = UBF_BUF_LEN-1; ubf_buf[len] = '\0'; } LVALP->t = ubfa_string; LVALP->g = NULL; LVALP->d.buf = (unsigned char *)malloc(len + 1); LVALP->len = len; strlcpy(LVALP->d.buf, ubf_buf, len + 1); ubfprintf(("ubf string: <%s> len %d\n", LVALP->d.buf, len)); return STRING; } \\(\\|\") *ubf_buf_ptr++ = yytext[1]; len++; (.|\n) *ubf_buf_ptr++ = yytext[0]; len++; \' { BEGIN(IN_CONST); ubf_buf_ptr = ubf_buf; } \' { BEGIN(INITIAL); *ubf_buf_ptr = '\0'; if(len >= UBF_BUF_LEN) { ubfprintf(("warning: truncating constant from %d to $d chars\n",len,UBF_BUF_LEN-1)); len = UBF_BUF_LEN-1; ubf_buf[len] = '\0'; } LVALP->t = ubfa_const; LVALP->g = NULL; LVALP->d.buf = (unsigned char *)malloc(len + 1); LVALP->len = len; strlcpy(LVALP->d.buf, ubf_buf, len + 1); ubfprintf(("ubf const: <%s> len %d\n", LVALP->d.buf, len)); return CONSTANT; } \\(\\|\') *ubf_buf_ptr++ = yytext[1]; len++; (.|\n) *ubf_buf_ptr++ = yytext[0]; len++; \` { BEGIN(IN_SEM_TAG); ubf_buf_ptr = ubf_buf; } \` { BEGIN(INITIAL); *ubf_buf_ptr = '\0'; if(len >= UBF_BUF_LEN) { ubfprintf(("warning: truncating semantic tag from %d to $d chars\n",len,UBF_BUF_LEN-1)); len = UBF_BUF_LEN-1; ubf_buf[len] = '\0'; } LVALP->t = ubfa_tag; LVALP->g = NULL; LVALP->d.buf = (unsigned char *)malloc(len + 1); LVALP->len = len; strlcpy(LVALP->d.buf, ubf_buf, len + 1); ubfprintf(("ubf semantic tag: <%s> len %d\n", LVALP->d.buf, len)); return SEM_TAG; } \\(\\|\`) *ubf_buf_ptr++ = yytext[1]; len++; (.|\n) *ubf_buf_ptr++ = yytext[0]; len++; ~ { BEGIN(IN_BIN_DATA); ubf_buf_ptr = ubf_buf; } ~ { BEGIN(INITIAL); *ubf_buf_ptr = '\0'; /* not strictly needed */ if(len >= UBF_BUF_LEN) { ubfprintf(("warning: truncating binary data from %d to $d chars\n",len,UBF_BUF_LEN-1)); len = UBF_BUF_LEN-1; ubf_buf[len] = '\0'; } LVALP->t = ubfa_bin_data; LVALP->g = NULL; LVALP->d.buf = (unsigned char *)malloc(len + 1); LVALP->len = len; memcpy(LVALP->d.buf, ubf_buf, len + 1); ubfprintf(("ubf binary data: <%s> len %d\n", LVALP->d.buf, len)); return BIN_DATA; } (.|\n) *ubf_buf_ptr++ = yytext[0]; len++; {UBF_SPECIAL} { /* single characters passed directly to parser */ ubfprintf(("ubf special: %c\n", yytext[0])); return yytext[0]; } \n /* ignore newlines */ . /* { ubfprintf( ("skip %c\n", yytext[0])); } */ <> { return DONE; } %%