X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-lex.l;h=9720efb6188886b46100b8aa63a507438d660970;hp=2a9d8e247908e33e5754a850492d9f52c3243ded;hb=c25df0131987fe52be7430c5be03ec83fbaea9b3;hpb=c6398c6a1b59f6eddefc9f9ce577017b15677251 diff --git a/src/ttt-lex.l b/src/ttt-lex.l index 2a9d8e2..9720efb 100644 --- a/src/ttt-lex.l +++ b/src/ttt-lex.l @@ -1,3 +1,24 @@ + /* ttt-lex.l - flex-based lexical analyzer + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Carl Worth + */ + /* Definitions */ %option reentrant @@ -5,12 +26,70 @@ %{ #include "ttt-token.h" + +#define YY_DECL int yylex (yyscan_t yyscanner, ttt_token_t *token) +#define STRING_BUF_SIZE 8000 + + /* Shut off some compiler warnings due to annoying flex bugs. */ +YY_DECL; +int yyget_column (yyscan_t yyscanner); +void yyset_column (int column_no , yyscan_t yyscanner); + %} +%x STRING + %% + char string_buf[STRING_BUF_SIZE]; + char *string_buf_ptr; + /* Rules */ -\r\n return TTT_TOKEN_NEWLINE; -[^ \t\r\n][^ \t\r\n]* return TTT_TOKEN_STRING; -[ \t\r\n] ; +\r\n { + token->type = TTT_TOKEN_TYPE_NEWLINE; + return token->type; + + /* This dead code is just to shut off a + * compiler warning due to an annoying flex + * bug. */ + unput('?'); + } + +\" { + string_buf_ptr = string_buf; + BEGIN (STRING); + } + +{ + \\\" { + *string_buf_ptr++ = '"'; + } + + \\ { + *string_buf_ptr++ = '\\'; + } + + [^\\\"\\]+ { + char *s = yytext; + + while (*s) + *string_buf_ptr++ = *s++; + } + + \" { + BEGIN (INITIAL); + *string_buf_ptr = '\0'; + token->type = TTT_TOKEN_TYPE_STRING; + token->u.string = xstrdup (string_buf); + return token->type; + } +} + +[^ \t\r\n\"][^ \t\r\n\"]* { + token->type = TTT_TOKEN_TYPE_STRING; + token->u.string = xstrdup (yytext); + return token->type; + } + +[ \t\r\n] ;