4 * Copyright (C) 2014 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 * lexical analyzer for parsing delta exceptions
18 * Lexing errors are indicated by setting `context->error' to an appropriate
19 * value; fatal lexer errors return token `INTERNAL_FLEX_ERROR'.
22 /* you should use flex version >= 2.5.39 to avoid various buglets */
23 /* that don't have work-arounds */
25 %option outfile="tadeltas-flex.c"
26 %option header-file="tadeltas-flex.h"
30 %option bison-locations
31 %option never-interactive
47 #include "tadeltas-bison.h"
49 /* option `yylineno' resets `yycolumn' to 0 after a newline */
50 /* (since version 2.5.30, March 2003) before the next token gets read; */
51 /* note we want both line number and column start with value 1 */
53 #define YY_USER_ACTION \
54 yylloc->first_line = yylineno + 1; \
55 yylloc->last_line = yylineno + 1; \
56 yylloc->first_column = yycolumn; \
57 yylloc->last_column = yycolumn + yyleng - 1; \
60 #define YY_EXTRA_TYPE Deltas_Context*
62 #define YY_FATAL_ERROR(msg) TA_deltas_scanner_fatal_error(msg, yyscanner)
64 /* by default, `yylex' simply calls `exit' (via YY_FATAL_ERROR) */
65 /* in case of a (more or less) fatal error -- */
66 /* this is bad for a library, thus we use `longjmp' to catch this, */
67 /* together with a proper handler for YY_FATAL_ERROR */
69 #define YY_USER_INIT \
72 if (setjmp(yyextra->jump_buffer) != 0) \
74 /* error and error message in `context' already stored by */ \
75 /* `TA_deltas_scanner_fatal_error' */ \
76 return INTERNAL_FLEX_ERROR; \
80 /* this macro works around flex bug to suppress a compilation warning, */
81 /* cf. https://sourceforge.net/p/flex/bugs/115 */
83 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
86 TA_deltas_scanner_fatal_error(const char* msg,
108 /* we want an EOE token at end of file, too */
121 /* skip escaped newline */
126 /* skip line comment */
132 /* values `0' and `-' need special treatment for number ranges */
137 | "0" [xX] [0-9a-fA-F]+
140 /* we don't support localized formats like a thousands separator */
142 yylval->integer = strtol(yytext, NULL, 0);
145 /* overflow or underflow */
146 yyextra->error = TA_Err_Deltas_Overflow;
151 (?x: [0-9]* "." [0-9]+
154 /* we don't support exponents, */
155 /* and we don't support localized formats like using `,' instead of `.' */
157 yylval->real = strtod(yytext, NULL);
158 if (yylval->real && errno == ERANGE)
161 yyextra->error = TA_Err_Deltas_Overflow;
173 (?x: [A-Za-z._] [A-Za-z0-9._]*
175 yylval->name = strdup(yytext);
178 /* allocation error */
179 yyextra->error = TA_Err_Deltas_Allocation_Error;
188 yylval->character = yytext[0];
189 return INVALID_CHARACTER;
196 /* the following two routines set `context->error' */
199 yyalloc(yy_size_t size,
202 YY_EXTRA_TYPE context;
205 void* p = malloc(size);
208 context = yyget_extra(yyscanner);
209 context->error = TA_Err_Deltas_Allocation_Error;
220 YY_EXTRA_TYPE context;
223 void* p = realloc(ptr, size);
226 context = yyget_extra(yyscanner);
227 context->error = TA_Err_Deltas_Allocation_Error;
233 /* we reimplement this routine also to avoid a compiler warning, */
234 /* cf. https://sourceforge.net/p/flex/bugs/115 */
245 TA_deltas_scanner_fatal_error(const char* msg,
248 YY_EXTRA_TYPE context = yyget_extra(yyscanner);
251 /* allocation routines set a different error value */
253 context->error = TA_Err_Deltas_Flex_Error;
254 strncpy(context->errmsg, msg, sizeof (context->errmsg));
256 longjmp(context->jump_buffer, 1);
258 /* the next line, which we never reach, suppresses a warning about */
259 /* `yy_fatal_error' defined but not used */
260 yy_fatal_error(msg, yyscanner);
265 TA_deltas_scanner_init(Deltas_Context* context,
274 /* this function sets `errno' in case of error */
275 flex_error = yylex_init(&scanner);
276 if (flex_error && errno == ENOMEM)
278 context->error = FT_Err_Out_Of_Memory;
279 context->errmsg[0] = '\0';
283 /* initialize some context fields */
284 context->font = font;
285 context->error = TA_Err_Ok;
286 context->result = NULL;
287 context->scanner = scanner;
290 yyset_extra(context, scanner);
292 /* by default, `yy_scan_bytes' simply calls `exit' */
293 /* (via YY_FATAL_ERROR) in case of a (more or less) fatal error -- */
294 /* this is bad for a library, thus we use `longjmp' to catch this, */
295 /* together with a proper handler for YY_FATAL_ERROR */
296 if (setjmp(context->jump_buffer) != 0)
298 /* error and error message in `context' already stored by */
299 /* `TA_deltas_scanner_fatal_error' */
303 b = yy_scan_bytes(font->deltas_buf, font->deltas_len, scanner);
305 /* flex bug: these two fields are not initialized, */
306 /* causing zillions of valgrind errors; see */
307 /* https://sourceforge.net/p/flex/bugs/180 */
314 TA_deltas_scanner_done(Deltas_Context* context)
316 yylex_destroy(context->scanner);
325 "# 0 a p 1-3 x 0.3 y -0.2 @ 20-30; \\\n"
326 "0 exclam p 2-4 x 0.7 y -0.4 @ 6,7,9,10,11; \\\n"
327 "0 a p 2 x 0.5 @ 23-25";
337 Deltas_Context context;
339 YYSTYPE yylval_param;
340 YYLTYPE yylloc_param;
343 /* we only need the delta exceptions buffer */
344 font.deltas_buf = (char*)input;
345 font.deltas_len = strlen(input);
347 TA_deltas_scanner_init(&context, &font);
351 yyset_debug(1, context.scanner);
352 while (yylex(&yylval_param, &yylloc_param, context.scanner))
361 TA_deltas_scanner_done(&context);
368 /* end of tadeltas.flex */