4 * Copyright (C) 2014-2016 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 ttfautohint control instructions
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="tacontrol-flex.c"
26 %option header-file="tacontrol-flex.h"
30 %option bison-locations
31 %option never-interactive
47 #include "tacontrol-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 + (int)yyleng - 1; \
60 #define YY_EXTRA_TYPE Control_Context*
62 #define YY_FATAL_ERROR(msg) TA_control_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_control_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)
88 yylval->name = strdup(yytext); \
90 yyextra->error = TA_Err_Control_Allocation_Error; \
94 TA_control_scanner_fatal_error(const char* msg,
104 /* skip whitespace */
116 /* we want an EOE token at end of file, too */
129 /* skip escaped newline */
134 /* skip line comment */
140 /* values `0' and `-' need special treatment for number ranges */
145 | "0" [xX] [0-9a-fA-F]+
148 /* we don't support localized formats like a thousands separator */
150 yylval->integer = strtol(yytext, NULL, 0);
153 /* overflow or underflow */
154 yyextra->error = TA_Err_Control_Overflow;
159 (?x: [0-9]* "." [0-9]+
162 /* we don't support exponents, */
163 /* and we don't support localized formats like using `,' instead of `.' */
165 yylval->real = strtod(yytext, NULL);
166 if (yylval->real != 0 && errno == ERANGE)
169 yyextra->error = TA_Err_Control_Overflow;
230 (?x: [A-Za-z._] [A-Za-z0-9._]*
240 yylval->character = yytext[0];
241 return INVALID_CHARACTER;
248 /* the following two routines set `context->error' */
251 yyalloc(yy_size_t size,
254 YY_EXTRA_TYPE context;
257 void* p = malloc(size);
260 context = yyget_extra(yyscanner);
261 context->error = TA_Err_Control_Allocation_Error;
272 YY_EXTRA_TYPE context;
275 void* p = realloc(ptr, size);
278 context = yyget_extra(yyscanner);
279 context->error = TA_Err_Control_Allocation_Error;
285 /* we reimplement this routine also to avoid a compiler warning, */
286 /* cf. https://sourceforge.net/p/flex/bugs/115 */
297 TA_control_scanner_fatal_error(const char* msg,
300 YY_EXTRA_TYPE context = yyget_extra(yyscanner);
303 /* allocation routines set a different error value */
305 context->error = TA_Err_Control_Flex_Error;
306 strncpy(context->errmsg, msg, sizeof (context->errmsg));
308 longjmp(context->jump_buffer, 1);
310 /* the next line, which we never reach, suppresses a warning about */
311 /* `yy_fatal_error' defined but not used */
312 yy_fatal_error(msg, yyscanner);
317 TA_control_scanner_init(Control_Context* context,
326 /* this function sets `errno' in case of error */
327 flex_error = yylex_init(&scanner);
328 if (flex_error && errno == ENOMEM)
330 context->error = FT_Err_Out_Of_Memory;
331 context->errmsg[0] = '\0';
335 /* initialize some context fields */
336 context->font = font;
337 context->error = TA_Err_Ok;
338 context->result = NULL;
339 context->scanner = scanner;
342 yyset_extra(context, scanner);
344 /* by default, `yy_scan_bytes' simply calls `exit' */
345 /* (via YY_FATAL_ERROR) in case of a (more or less) fatal error -- */
346 /* this is bad for a library, thus we use `longjmp' to catch this, */
347 /* together with a proper handler for YY_FATAL_ERROR */
348 if (setjmp(context->jump_buffer) != 0)
350 /* error and error message in `context' already stored by */
351 /* `TA_control_scanner_fatal_error' */
355 b = yy_scan_bytes(font->control_buf, font->control_len, scanner);
357 /* flex bug: these two fields are not initialized, */
358 /* causing zillions of valgrind errors; see */
359 /* https://sourceforge.net/p/flex/bugs/180 */
366 TA_control_scanner_done(Control_Context* context)
368 yylex_destroy(context->scanner);
377 "# 0 a p 1-3 x 0.3 y -0.2 @ 20-30; \\\n"
378 "0 exclam p 2-4 x 0.7 y -0.4 @ 6,7,9,10,11; \\\n"
379 "0 a p 2 x 0.5 @ 23-25";
389 Control_Context context;
391 YYSTYPE yylval_param;
392 YYLTYPE yylloc_param;
395 /* we only need the control instructions buffer */
396 font.control_buf = (char*)input;
397 font.control_len = strlen(input);
399 TA_control_scanner_init(&context, &font);
403 yyset_debug(1, context.scanner);
404 while (yylex(&yylval_param, &yylloc_param, context.scanner))
413 TA_control_scanner_done(&context);
420 /* end of tacontrol.flex */