1/*
2	This file is part of Warzone 2100.
3	Copyright (C) 1999-2004  Eidos Interactive
4	Copyright (C) 2005-2020  Warzone 2100 Project
5
6	Warzone 2100 is free software; you can redistribute it and/or modify
7	it under the terms of the GNU General Public License as published by
8	the Free Software Foundation; either version 2 of the License, or
9	(at your option) any later version.
10
11	Warzone 2100 is distributed in the hope that it will be useful,
12	but WITHOUT ANY WARRANTY; without even the implied warranty of
13	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14	GNU General Public License for more details.
15
16	You should have received a copy of the GNU General Public License
17	along with Warzone 2100; if not, write to the Free Software
18	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20%{
21/** @file
22 *
23 *  Lexical analyser resource files
24 */
25
26#include "lib/framework/frame.h"
27
28/* Allow frame header files to be singly included */
29#define FRAME_LIB_INCLUDE
30
31#include "lib/framework/strres.h"
32#include "lib/framework/strresly.h"
33
34#include <physfs.h>
35
36/* Get the Yacc definitions */
37#if defined (WZ_CC_MSVC)
38#include "strres_parser.hpp"
39#else
40#include "strres_parser.h"
41#endif
42#include "lib/framework/lexer_input.h"
43
44// fwrite declared with warn_unused_result, resulting in mysterious errors in "%%" on some distros.
45static inline bool no_warn_unused_result(int ignore) { if (ignore) {} return true; }
46#define fwrite(a, b, c, d) no_warn_unused_result(fwrite(a, b, c, d))
47
48#ifndef yyextra
49# define yyextra yyget_extra()
50#endif
51
52/* Older GNU Flex versions don't define yyget_extra(), yyset_extra(),
53 * yyget_text() and yyget_lineno().
54 * (and neither define a subminor version)
55 */
56#if !defined(YY_FLEX_SUBMINOR_VERSION) || (YY_FLEX_SUBMINOR_VERSION < 9)
57# define yyget_extra  strres_get_extra
58# define yyset_extra  strres_set_extra
59# define yyget_lineno strres_get_lineno
60# define yyget_text   strres_get_text
61extern void yyset_extra(YY_EXTRA_TYPE user_defined);
62extern YY_EXTRA_TYPE yyget_extra(void);
63extern int yyget_lineno(void);
64int yyget_lineno()
65{
66	return yylineno;
67}
68
69extern char* yyget_text(void);
70char* yyget_text()
71{
72	return yytext;
73}
74#elif defined(YY_FLEX_SUBMINOR_VERSION) && YY_FLEX_SUBMINOR_VERSION == 33
75extern YY_EXTRA_TYPE yyget_extra(void);
76extern int strres_get_lineno(void);
77extern FILE *strres_get_in(void);
78extern FILE *strres_get_out(void);
79extern int strres_get_leng(void);
80extern char *strres_get_text(void);
81extern void strres_set_lineno(int line_number);
82extern void strres_set_in(FILE* in_str);
83extern void strres_set_out(FILE* out_str);
84extern int strres_get_debug(void);
85extern void strres_set_debug(int bdebug);
86#endif
87
88%}
89
90%option yylineno noyywrap nounput never-interactive
91%option prefix="strres_"
92
93%x COMMENT
94%x QUOTE
95%x SLCOMMENT
96
97%%
98
99	/* Match text values */
100[a-zA-Z][-0-9_a-zA-Z]*		{
101								strres_lval.sval = strdup(yytext);
102								return TEXT_T;
103							}
104
105	/* Match quoted text */
106\"							{ BEGIN QUOTE; }
107<QUOTE>\"					{ BEGIN 0; }
108<QUOTE>\n					{ strres_error("Unexpected end of line in string"); }
109<QUOTE>[^\"\n]*				{
110								strres_lval.sval = strdup(yytext);
111								return QTEXT_T;
112							}
113
114	/* Skip white space */
115[ \t\n\x0d\x0a]						;
116
117	/* Strip comments */
118"/*"						{ BEGIN COMMENT; }
119<COMMENT>"*/" |
120<COMMENT>"*/"\n				{ BEGIN 0; }
121<COMMENT>. |
122<COMMENT>\n					;
123
124	/* Strip single line comments */
125"//"						{ BEGIN SLCOMMENT; }
126<SLCOMMENT>\n				{ BEGIN 0; }
127<SLCOMMENT>[^\n]*			;
128
129	/* Match anything that's been missed and pass it as a char */
130.							return yytext[0];
131
132%%
133
134static YY_EXTRA_TYPE pBuffer = NULL;
135
136void yyset_extra(YY_EXTRA_TYPE user_defined)
137{
138	pBuffer = user_defined;
139}
140
141YY_EXTRA_TYPE yyget_extra()
142{
143	return pBuffer;
144}
145
146/* Older GNU Flex versions don't define yylex_destroy()
147 * (and neither define a subminor version)
148 */
149#if !defined(YY_FLEX_SUBMINOR_VERSION) || (YY_FLEX_SUBMINOR_VERSION < 9)
150int strres_lex_destroy(void)
151{
152	/* For non-reentrant C scanner only. */
153	yy_delete_buffer(YY_CURRENT_BUFFER);
154	yy_init = 1;
155	return 0;
156}
157#endif
158