2 /*=========================================================================
4 Program: CMake - Cross-Platform Makefile Generator
5 Module: $RCSfile: cmListFileLexer.in.l,v $
7 Date: $Date: 2006-08-09 13:45:04 $
8 Version: $Revision: 1.15 $
10 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
11 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 This file must be translated to C and modified to build everywhere.
24 flex --prefix=cmListFileLexer_yy -ocmListFileLexer.c cmListFileLexer.in.l
26 Modify cmListFileLexer.c:
28 - remove the yyunput function
29 - add a statement "(void)yyscanner;" to the top of these methods:
30 yy_fatal_error, cmListFileLexer_yyalloc, cmListFileLexer_yyrealloc, cmListFileLexer_yyfree
31 - remove all YY_BREAK lines occurring right after return statements
32 - remove the isatty forward declaration
36 #include "cmStandardLexer.h"
38 /* Setup the proper cmListFileLexer_yylex declaration. */
39 #define YY_EXTRA_TYPE cmListFileLexer*
40 #define YY_DECL int cmListFileLexer_yylex (yyscan_t yyscanner, cmListFileLexer* lexer)
42 #include "cmListFileLexer.h"
44 /*--------------------------------------------------------------------------*/
45 struct cmListFileLexer_s
47 cmListFileLexer_Token token;
53 char* string_position;
58 static void cmListFileLexerSetToken(cmListFileLexer* lexer, const char* text,
60 static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
62 static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer,
64 static void cmListFileLexerInit(cmListFileLexer* lexer);
65 static void cmListFileLexerDestroy(cmListFileLexer* lexer);
67 /* Replace the lexer input function. */
69 #define YY_INPUT(buf, result, max_size) \
70 { result = cmListFileLexerInput(cmListFileLexer_yyget_extra(yyscanner), buf, max_size); }
72 /*--------------------------------------------------------------------------*/
81 MAKEVAR \$\([A-Za-z0-9_]*\)
86 lexer->token.type = cmListFileLexer_Token_Newline;
87 cmListFileLexerSetToken(lexer, yytext, yyleng);
94 lexer->column += yyleng;
98 lexer->token.type = cmListFileLexer_Token_ParenLeft;
99 cmListFileLexerSetToken(lexer, yytext, yyleng);
100 lexer->column += yyleng;
105 lexer->token.type = cmListFileLexer_Token_ParenRight;
106 cmListFileLexerSetToken(lexer, yytext, yyleng);
107 lexer->column += yyleng;
111 [A-Za-z_][A-Za-z0-9_]+ {
112 lexer->token.type = cmListFileLexer_Token_Identifier;
113 cmListFileLexerSetToken(lexer, yytext, yyleng);
114 lexer->column += yyleng;
118 ({MAKEVAR}|[^ \t\r\n\(\)#\\\"]|\\.)({MAKEVAR}|[^ \t\r\n\(\)#\\\"]|\\.|\"({MAKEVAR}|[^\r\n\(\)#\\\"]|\\.)*\")* {
119 lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
120 cmListFileLexerSetToken(lexer, yytext, yyleng);
121 lexer->column += yyleng;
126 lexer->token.type = cmListFileLexer_Token_ArgumentQuoted;
127 cmListFileLexerSetToken(lexer, "", 0);
128 lexer->column += yyleng;
132 <STRING>([^\\\n\"]|\\(.|\n))+ {
133 cmListFileLexerAppend(lexer, yytext, yyleng);
134 lexer->column += yyleng;
138 cmListFileLexerAppend(lexer, yytext, yyleng);
144 lexer->column += yyleng;
150 cmListFileLexerAppend(lexer, yytext, yyleng);
151 lexer->column += yyleng;
155 lexer->token.type = cmListFileLexer_Token_BadString;
161 lexer->column += yyleng;
165 lexer->token.type = cmListFileLexer_Token_BadCharacter;
166 cmListFileLexerSetToken(lexer, yytext, yyleng);
167 lexer->column += yyleng;
172 lexer->token.type = cmListFileLexer_Token_None;
173 cmListFileLexerSetToken(lexer, 0, 0);
179 /*--------------------------------------------------------------------------*/
180 static void cmListFileLexerSetToken(cmListFileLexer* lexer, const char* text,
183 /* Set the token line and column number. */
184 lexer->token.line = lexer->line;
185 lexer->token.column = lexer->column;
187 /* Use the same buffer if possible. */
188 if(lexer->token.text)
190 if(text && length < lexer->size)
192 strcpy(lexer->token.text, text);
193 lexer->token.length = length;
196 free(lexer->token.text);
197 lexer->token.text = 0;
201 /* Need to extend the buffer. */
204 lexer->token.text = strdup(text);
205 lexer->token.length = length;
206 lexer->size = length+1;
210 lexer->token.length = 0;
214 /*--------------------------------------------------------------------------*/
215 static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
221 /* If the appended text will fit in the buffer, do not reallocate. */
222 newSize = lexer->token.length + length + 1;
223 if(lexer->token.text && newSize <= lexer->size)
225 strcpy(lexer->token.text+lexer->token.length, text);
226 lexer->token.length += length;
230 /* We need to extend the buffer. */
231 temp = malloc(newSize);
232 if(lexer->token.text)
234 memcpy(temp, lexer->token.text, lexer->token.length);
235 free(lexer->token.text);
237 memcpy(temp+lexer->token.length, text, length);
238 temp[lexer->token.length+length] = 0;
239 lexer->token.text = temp;
240 lexer->token.length += length;
241 lexer->size = newSize;
244 /*--------------------------------------------------------------------------*/
245 static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer,
252 return (int)fread(buffer, 1, bufferSize, lexer->file);
254 else if(lexer->string_left)
256 int length = lexer->string_left;
257 if((int)bufferSize < length) { length = (int)bufferSize; }
258 memcpy(buffer, lexer->string_position, length);
259 lexer->string_position += length;
260 lexer->string_left -= length;
267 /*--------------------------------------------------------------------------*/
268 static void cmListFileLexerInit(cmListFileLexer* lexer)
270 if(lexer->file || lexer->string_buffer)
272 cmListFileLexer_yylex_init(&lexer->scanner);
273 cmListFileLexer_yyset_extra(lexer, lexer->scanner);
277 /*--------------------------------------------------------------------------*/
278 static void cmListFileLexerDestroy(cmListFileLexer* lexer)
280 if(lexer->file || lexer->string_buffer)
282 cmListFileLexer_yylex_destroy(lexer->scanner);
288 if(lexer->string_buffer)
290 free(lexer->string_buffer);
291 lexer->string_buffer = 0;
292 lexer->string_left = 0;
293 lexer->string_position = 0;
298 /*--------------------------------------------------------------------------*/
299 cmListFileLexer* cmListFileLexer_New()
301 cmListFileLexer* lexer = (cmListFileLexer*)malloc(sizeof(cmListFileLexer));
306 memset(lexer, 0, sizeof(*lexer));
312 /*--------------------------------------------------------------------------*/
313 void cmListFileLexer_Delete(cmListFileLexer* lexer)
315 cmListFileLexer_SetFileName(lexer, 0);
319 /*--------------------------------------------------------------------------*/
320 int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name)
323 cmListFileLexerDestroy(lexer);
326 lexer->file = fopen(name, "r");
332 cmListFileLexerInit(lexer);
336 /*--------------------------------------------------------------------------*/
337 int cmListFileLexer_SetString(cmListFileLexer* lexer, const char* text)
340 cmListFileLexerDestroy(lexer);
343 int length = (int)strlen(text);
344 lexer->string_buffer = (char*)malloc(length+1);
345 if(lexer->string_buffer)
347 strcpy(lexer->string_buffer, text);
348 lexer->string_position = lexer->string_buffer;
349 lexer->string_left = length;
356 cmListFileLexerInit(lexer);
360 /*--------------------------------------------------------------------------*/
361 cmListFileLexer_Token* cmListFileLexer_Scan(cmListFileLexer* lexer)
367 if(cmListFileLexer_yylex(lexer->scanner, lexer))
369 return &lexer->token;
373 cmListFileLexer_SetFileName(lexer, 0);
378 /*--------------------------------------------------------------------------*/
379 long cmListFileLexer_GetCurrentLine(cmListFileLexer* lexer)
391 /*--------------------------------------------------------------------------*/
392 long cmListFileLexer_GetCurrentColumn(cmListFileLexer* lexer)
396 return lexer->column;
404 /*--------------------------------------------------------------------------*/
405 const char* cmListFileLexer_GetTypeAsString(cmListFileLexer* lexer,
406 cmListFileLexer_Type type)
411 case cmListFileLexer_Token_None: return "nothing";
412 case cmListFileLexer_Token_Newline: return "newline";
413 case cmListFileLexer_Token_Identifier: return "identifier";
414 case cmListFileLexer_Token_ParenLeft: return "left paren";
415 case cmListFileLexer_Token_ParenRight: return "right paren";
416 case cmListFileLexer_Token_ArgumentUnquoted: return "unquoted argument";
417 case cmListFileLexer_Token_ArgumentQuoted: return "quoted argument";
418 case cmListFileLexer_Token_BadCharacter: return "bad character";
419 case cmListFileLexer_Token_BadString: return "unterminated string";
421 return "unknown token";