2003-12-08 21:36:59 +03:00
|
|
|
%{
|
2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2003-12-08 21:36:59 +03:00
|
|
|
/*
|
|
|
|
|
|
|
|
This file must be translated to C and modified to build everywhere.
|
|
|
|
|
2016-05-17 17:31:41 +03:00
|
|
|
Run flex >= 2.6 like this:
|
2003-12-08 21:36:59 +03:00
|
|
|
|
2005-01-19 20:06:24 +03:00
|
|
|
flex --prefix=cmListFileLexer_yy -ocmListFileLexer.c cmListFileLexer.in.l
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
Modify cmListFileLexer.c:
|
2016-05-17 17:31:41 +03:00
|
|
|
- remove trailing whitespace: sed -i 's/\s*$//' cmListFileLexer.c
|
|
|
|
- remove blank lines at end of file
|
2013-08-07 16:07:39 +04:00
|
|
|
- remove statement "yyscanner = NULL;" from cmListFileLexer_yylex_destroy
|
2003-12-09 17:16:40 +03:00
|
|
|
- remove all YY_BREAK lines occurring right after return statements
|
2016-05-17 17:31:41 +03:00
|
|
|
- remove unnecessary cast to (int) in yy_get_next_buffer
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2006-08-08 22:00:28 +04:00
|
|
|
#include "cmStandardLexer.h"
|
2014-01-04 09:47:13 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <cmsys/Encoding.h>
|
|
|
|
#endif
|
2006-08-08 22:00:28 +04:00
|
|
|
|
|
|
|
/* Setup the proper cmListFileLexer_yylex declaration. */
|
2004-04-22 00:23:58 +04:00
|
|
|
#define YY_EXTRA_TYPE cmListFileLexer*
|
2005-01-19 20:06:24 +03:00
|
|
|
#define YY_DECL int cmListFileLexer_yylex (yyscan_t yyscanner, cmListFileLexer* lexer)
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
#include "cmListFileLexer.h"
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
struct cmListFileLexer_s
|
|
|
|
{
|
|
|
|
cmListFileLexer_Token token;
|
2016-05-17 17:31:41 +03:00
|
|
|
yy_size_t bracket;
|
2013-08-06 23:58:22 +04:00
|
|
|
int comment;
|
2003-12-08 21:36:59 +03:00
|
|
|
int line;
|
|
|
|
int column;
|
|
|
|
int size;
|
|
|
|
FILE* file;
|
2013-10-14 21:54:19 +04:00
|
|
|
size_t cr;
|
2004-04-22 00:23:58 +04:00
|
|
|
char* string_buffer;
|
|
|
|
char* string_position;
|
|
|
|
int string_left;
|
2003-12-08 21:36:59 +03:00
|
|
|
yyscan_t scanner;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void cmListFileLexerSetToken(cmListFileLexer* lexer, const char* text,
|
|
|
|
int length);
|
|
|
|
static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
|
|
|
|
int length);
|
2004-04-22 00:23:58 +04:00
|
|
|
static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer,
|
|
|
|
size_t bufferSize);
|
|
|
|
static void cmListFileLexerInit(cmListFileLexer* lexer);
|
|
|
|
static void cmListFileLexerDestroy(cmListFileLexer* lexer);
|
|
|
|
|
|
|
|
/* Replace the lexer input function. */
|
|
|
|
#undef YY_INPUT
|
|
|
|
#define YY_INPUT(buf, result, max_size) \
|
2005-01-19 20:06:24 +03:00
|
|
|
{ result = cmListFileLexerInput(cmListFileLexer_yyget_extra(yyscanner), buf, max_size); }
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
%}
|
|
|
|
|
|
|
|
%option reentrant
|
|
|
|
%option yylineno
|
|
|
|
%option noyywrap
|
|
|
|
%pointer
|
|
|
|
%x STRING
|
2013-08-06 23:58:22 +04:00
|
|
|
%x BRACKET
|
|
|
|
%x BRACKETEND
|
|
|
|
%x COMMENT
|
2003-12-08 21:36:59 +03:00
|
|
|
|
2004-09-20 16:51:12 +04:00
|
|
|
MAKEVAR \$\([A-Za-z0-9_]*\)
|
2013-08-06 23:58:22 +04:00
|
|
|
UNQUOTED ([^ \t\r\n\(\)#\\\"[=]|\\.)
|
|
|
|
LEGACY {MAKEVAR}|{UNQUOTED}|\"({MAKEVAR}|{UNQUOTED}|[ \t[=])*\"
|
2004-09-20 16:51:12 +04:00
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
%%
|
|
|
|
|
2013-08-06 23:58:22 +04:00
|
|
|
<INITIAL,COMMENT>\n {
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.type = cmListFileLexer_Token_Newline;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
++lexer->line;
|
|
|
|
lexer->column = 1;
|
2013-08-06 23:58:22 +04:00
|
|
|
BEGIN(INITIAL);
|
2003-12-08 21:36:59 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:58:22 +04:00
|
|
|
#?\[=*\[\n? {
|
|
|
|
const char* bracket = yytext;
|
|
|
|
lexer->comment = yytext[0] == '#';
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->comment) {
|
2013-08-06 23:58:22 +04:00
|
|
|
lexer->token.type = cmListFileLexer_Token_CommentBracket;
|
|
|
|
bracket += 1;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2013-08-06 23:58:22 +04:00
|
|
|
lexer->token.type = cmListFileLexer_Token_ArgumentBracket;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2013-08-06 23:58:22 +04:00
|
|
|
cmListFileLexerSetToken(lexer, "", 0);
|
2016-05-17 17:31:41 +03:00
|
|
|
lexer->bracket = strchr(bracket+1, '[') - bracket;
|
2016-05-17 20:23:06 +03:00
|
|
|
if (yytext[yyleng-1] == '\n') {
|
2013-08-06 23:58:22 +04:00
|
|
|
++lexer->line;
|
|
|
|
lexer->column = 1;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2013-08-06 23:58:22 +04:00
|
|
|
lexer->column += yyleng;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2013-08-06 23:58:22 +04:00
|
|
|
BEGIN(BRACKET);
|
|
|
|
}
|
|
|
|
|
|
|
|
# {
|
|
|
|
lexer->column += yyleng;
|
|
|
|
BEGIN(COMMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
<COMMENT>.* {
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->column += yyleng;
|
|
|
|
}
|
|
|
|
|
|
|
|
\( {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_ParenLeft;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
\) {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_ParenRight;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-14 23:24:06 +04:00
|
|
|
[A-Za-z_][A-Za-z0-9_]* {
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.type = cmListFileLexer_Token_Identifier;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:58:22 +04:00
|
|
|
<BRACKET>\]=* {
|
|
|
|
/* Handle ]]====]=======]*/
|
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
2016-05-17 20:23:06 +03:00
|
|
|
if (yyleng == lexer->bracket) {
|
2013-08-06 23:58:22 +04:00
|
|
|
BEGIN(BRACKETEND);
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2013-08-06 23:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
<BRACKETEND>\] {
|
|
|
|
lexer->column += yyleng;
|
|
|
|
/* Erase the partial bracket from the token. */
|
|
|
|
lexer->token.length -= lexer->bracket;
|
|
|
|
lexer->token.text[lexer->token.length] = 0;
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
<BRACKET>([^]\n])+ {
|
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
}
|
|
|
|
|
|
|
|
<BRACKET,BRACKETEND>\n {
|
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
++lexer->line;
|
|
|
|
lexer->column = 1;
|
|
|
|
BEGIN(BRACKET);
|
|
|
|
}
|
|
|
|
|
|
|
|
<BRACKET,BRACKETEND>. {
|
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
BEGIN(BRACKET);
|
|
|
|
}
|
|
|
|
|
|
|
|
<BRACKET,BRACKETEND><<EOF>> {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_BadBracket;
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
({UNQUOTED}|=|\[=*{UNQUOTED})({UNQUOTED}|[[=])* {
|
2012-08-06 17:53:03 +04:00
|
|
|
lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:58:22 +04:00
|
|
|
({MAKEVAR}|{UNQUOTED}|=|\[=*{LEGACY})({LEGACY}|[[=])* {
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:18:20 +04:00
|
|
|
\[ {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
\" {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_ArgumentQuoted;
|
|
|
|
cmListFileLexerSetToken(lexer, "", 0);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
BEGIN(STRING);
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:41:10 +04:00
|
|
|
<STRING>([^\\\n\"]|\\.)+ {
|
2003-12-08 21:36:59 +03:00
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
}
|
|
|
|
|
2012-08-06 17:41:10 +04:00
|
|
|
<STRING>\\\n {
|
2013-10-23 02:11:22 +04:00
|
|
|
/* Continuation: text is not part of string */
|
2012-08-06 17:41:10 +04:00
|
|
|
++lexer->line;
|
|
|
|
lexer->column = 1;
|
|
|
|
}
|
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
<STRING>\n {
|
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
++lexer->line;
|
|
|
|
lexer->column = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
<STRING>\" {
|
|
|
|
lexer->column += yyleng;
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
<STRING>. {
|
|
|
|
cmListFileLexerAppend(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
}
|
|
|
|
|
2004-09-01 02:39:42 +04:00
|
|
|
<STRING><<EOF>> {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_BadString;
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-02-17 05:41:47 +04:00
|
|
|
[ \t\r]+ {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_Space;
|
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->column += yyleng;
|
2013-02-17 05:41:47 +04:00
|
|
|
return 1;
|
2003-12-08 21:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
. {
|
2004-09-01 02:39:42 +04:00
|
|
|
lexer->token.type = cmListFileLexer_Token_BadCharacter;
|
2003-12-08 21:36:59 +03:00
|
|
|
cmListFileLexerSetToken(lexer, yytext, yyleng);
|
|
|
|
lexer->column += yyleng;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
<<EOF>> {
|
|
|
|
lexer->token.type = cmListFileLexer_Token_None;
|
|
|
|
cmListFileLexerSetToken(lexer, 0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-09 17:11:35 +03:00
|
|
|
static void cmListFileLexerSetToken(cmListFileLexer* lexer, const char* text,
|
|
|
|
int length)
|
2003-12-08 21:36:59 +03:00
|
|
|
{
|
|
|
|
/* Set the token line and column number. */
|
|
|
|
lexer->token.line = lexer->line;
|
|
|
|
lexer->token.column = lexer->column;
|
|
|
|
|
|
|
|
/* Use the same buffer if possible. */
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->token.text) {
|
|
|
|
if (text && length < lexer->size) {
|
2003-12-08 21:36:59 +03:00
|
|
|
strcpy(lexer->token.text, text);
|
|
|
|
lexer->token.length = length;
|
|
|
|
return;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
free(lexer->token.text);
|
|
|
|
lexer->token.text = 0;
|
|
|
|
lexer->size = 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
/* Need to extend the buffer. */
|
2016-05-17 20:23:06 +03:00
|
|
|
if (text) {
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.text = strdup(text);
|
|
|
|
lexer->token.length = length;
|
2016-05-17 20:23:06 +03:00
|
|
|
lexer->size = length + 1;
|
|
|
|
} else {
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.length = 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-09 17:11:35 +03:00
|
|
|
static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
|
|
|
|
int length)
|
2003-12-08 21:36:59 +03:00
|
|
|
{
|
|
|
|
char* temp;
|
|
|
|
int newSize;
|
|
|
|
|
|
|
|
/* If the appended text will fit in the buffer, do not reallocate. */
|
|
|
|
newSize = lexer->token.length + length + 1;
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->token.text && newSize <= lexer->size) {
|
|
|
|
strcpy(lexer->token.text + lexer->token.length, text);
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.length += length;
|
|
|
|
return;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
|
|
|
|
/* We need to extend the buffer. */
|
|
|
|
temp = malloc(newSize);
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->token.text) {
|
2003-12-08 21:36:59 +03:00
|
|
|
memcpy(temp, lexer->token.text, lexer->token.length);
|
|
|
|
free(lexer->token.text);
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
|
|
|
memcpy(temp + lexer->token.length, text, length);
|
|
|
|
temp[lexer->token.length + length] = 0;
|
2003-12-08 21:36:59 +03:00
|
|
|
lexer->token.text = temp;
|
|
|
|
lexer->token.length += length;
|
|
|
|
lexer->size = newSize;
|
|
|
|
}
|
|
|
|
|
2004-04-22 00:23:58 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer,
|
|
|
|
size_t bufferSize)
|
|
|
|
{
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer) {
|
|
|
|
if (lexer->file) {
|
2013-10-14 21:54:19 +04:00
|
|
|
/* Convert CRLF -> LF explicitly. The C FILE "t"ext mode
|
|
|
|
does not convert newlines on all platforms. Move any
|
|
|
|
trailing CR to the start of the buffer for the next read. */
|
|
|
|
size_t cr = lexer->cr;
|
|
|
|
size_t n;
|
|
|
|
buffer[0] = '\r';
|
2016-05-17 20:23:06 +03:00
|
|
|
n = fread(buffer + cr, 1, bufferSize - cr, lexer->file);
|
|
|
|
if (n) {
|
2013-10-14 21:54:19 +04:00
|
|
|
char* o = buffer;
|
|
|
|
const char* i = buffer;
|
|
|
|
const char* e;
|
|
|
|
n += cr;
|
2016-05-17 20:23:06 +03:00
|
|
|
cr = (buffer[n - 1] == '\r') ? 1 : 0;
|
2013-10-14 21:54:19 +04:00
|
|
|
e = buffer + n - cr;
|
2016-05-17 20:23:06 +03:00
|
|
|
while (i != e) {
|
|
|
|
if (i[0] == '\r' && i[1] == '\n') {
|
2013-10-14 21:54:19 +04:00
|
|
|
++i;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
*o++ = *i++;
|
2013-10-14 21:54:19 +04:00
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
n = o - buffer;
|
|
|
|
} else {
|
2013-10-14 21:54:19 +04:00
|
|
|
n = cr;
|
|
|
|
cr = 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2013-10-14 21:54:19 +04:00
|
|
|
lexer->cr = cr;
|
|
|
|
return n;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else if (lexer->string_left) {
|
2004-04-22 00:23:58 +04:00
|
|
|
int length = lexer->string_left;
|
2016-05-17 20:23:06 +03:00
|
|
|
if ((int)bufferSize < length) {
|
|
|
|
length = (int)bufferSize;
|
|
|
|
}
|
2004-04-22 00:23:58 +04:00
|
|
|
memcpy(buffer, lexer->string_position, length);
|
|
|
|
lexer->string_position += length;
|
|
|
|
lexer->string_left -= length;
|
|
|
|
return length;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2004-04-22 00:23:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void cmListFileLexerInit(cmListFileLexer* lexer)
|
|
|
|
{
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->file || lexer->string_buffer) {
|
2005-01-19 20:06:24 +03:00
|
|
|
cmListFileLexer_yylex_init(&lexer->scanner);
|
|
|
|
cmListFileLexer_yyset_extra(lexer, lexer->scanner);
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2004-04-22 00:23:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void cmListFileLexerDestroy(cmListFileLexer* lexer)
|
|
|
|
{
|
2013-09-17 18:24:17 +04:00
|
|
|
cmListFileLexerSetToken(lexer, 0, 0);
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->file || lexer->string_buffer) {
|
2005-01-19 20:06:24 +03:00
|
|
|
cmListFileLexer_yylex_destroy(lexer->scanner);
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->file) {
|
2004-04-22 00:23:58 +04:00
|
|
|
fclose(lexer->file);
|
|
|
|
lexer->file = 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
|
|
|
if (lexer->string_buffer) {
|
2004-04-22 00:23:58 +04:00
|
|
|
free(lexer->string_buffer);
|
|
|
|
lexer->string_buffer = 0;
|
|
|
|
lexer->string_left = 0;
|
|
|
|
lexer->string_position = 0;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2004-04-22 00:23:58 +04:00
|
|
|
}
|
|
|
|
|
2003-12-08 21:36:59 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
cmListFileLexer* cmListFileLexer_New()
|
|
|
|
{
|
|
|
|
cmListFileLexer* lexer = (cmListFileLexer*)malloc(sizeof(cmListFileLexer));
|
2016-05-17 20:23:06 +03:00
|
|
|
if (!lexer) {
|
2003-12-08 21:36:59 +03:00
|
|
|
return 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
memset(lexer, 0, sizeof(*lexer));
|
|
|
|
lexer->line = 1;
|
|
|
|
lexer->column = 1;
|
|
|
|
return lexer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void cmListFileLexer_Delete(cmListFileLexer* lexer)
|
|
|
|
{
|
2013-10-14 23:13:11 +04:00
|
|
|
cmListFileLexer_SetFileName(lexer, 0, 0);
|
2003-12-08 21:36:59 +03:00
|
|
|
free(lexer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2013-10-14 23:13:11 +04:00
|
|
|
static cmListFileLexer_BOM cmListFileLexer_ReadBOM(FILE* f)
|
|
|
|
{
|
|
|
|
unsigned char b[2];
|
2016-05-17 20:23:06 +03:00
|
|
|
if (fread(b, 1, 2, f) == 2) {
|
|
|
|
if (b[0] == 0xEF && b[1] == 0xBB) {
|
|
|
|
if (fread(b, 1, 1, f) == 1 && b[0] == 0xBF) {
|
2013-10-14 23:13:11 +04:00
|
|
|
return cmListFileLexer_BOM_UTF8;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
} else if (b[0] == 0xFE && b[1] == 0xFF) {
|
2013-10-14 23:13:11 +04:00
|
|
|
/* UTF-16 BE */
|
|
|
|
return cmListFileLexer_BOM_UTF16BE;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else if (b[0] == 0 && b[1] == 0) {
|
|
|
|
if (fread(b, 1, 2, f) == 2 && b[0] == 0xFE && b[1] == 0xFF) {
|
2013-10-14 23:13:11 +04:00
|
|
|
return cmListFileLexer_BOM_UTF32BE;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
} else if (b[0] == 0xFF && b[1] == 0xFE) {
|
2013-10-14 23:13:11 +04:00
|
|
|
fpos_t p;
|
|
|
|
fgetpos(f, &p);
|
2016-05-17 20:23:06 +03:00
|
|
|
if (fread(b, 1, 2, f) == 2 && b[0] == 0 && b[1] == 0) {
|
2013-10-14 23:13:11 +04:00
|
|
|
return cmListFileLexer_BOM_UTF32LE;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2013-10-14 23:13:11 +04:00
|
|
|
fsetpos(f, &p);
|
|
|
|
return cmListFileLexer_BOM_UTF16LE;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2013-10-14 23:13:11 +04:00
|
|
|
rewind(f);
|
|
|
|
return cmListFileLexer_BOM_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name,
|
|
|
|
cmListFileLexer_BOM* bom)
|
2003-12-08 21:36:59 +03:00
|
|
|
{
|
|
|
|
int result = 1;
|
2004-04-22 00:23:58 +04:00
|
|
|
cmListFileLexerDestroy(lexer);
|
2016-05-17 20:23:06 +03:00
|
|
|
if (name) {
|
2014-01-04 09:47:13 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
wchar_t* wname = cmsysEncoding_DupToWide(name);
|
|
|
|
lexer->file = _wfopen(wname, L"rb");
|
|
|
|
free(wname);
|
|
|
|
#else
|
2013-10-14 21:54:19 +04:00
|
|
|
lexer->file = fopen(name, "rb");
|
2014-01-04 09:47:13 +04:00
|
|
|
#endif
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->file) {
|
|
|
|
if (bom) {
|
2013-10-14 23:13:11 +04:00
|
|
|
*bom = cmListFileLexer_ReadBOM(lexer->file);
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2003-12-08 21:36:59 +03:00
|
|
|
result = 0;
|
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2004-04-22 00:23:58 +04:00
|
|
|
cmListFileLexerInit(lexer);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int cmListFileLexer_SetString(cmListFileLexer* lexer, const char* text)
|
|
|
|
{
|
|
|
|
int result = 1;
|
|
|
|
cmListFileLexerDestroy(lexer);
|
2016-05-17 20:23:06 +03:00
|
|
|
if (text) {
|
2004-04-22 00:23:58 +04:00
|
|
|
int length = (int)strlen(text);
|
2016-05-17 20:23:06 +03:00
|
|
|
lexer->string_buffer = (char*)malloc(length + 1);
|
|
|
|
if (lexer->string_buffer) {
|
2004-04-22 00:23:58 +04:00
|
|
|
strcpy(lexer->string_buffer, text);
|
|
|
|
lexer->string_position = lexer->string_buffer;
|
|
|
|
lexer->string_left = length;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2004-04-22 00:23:58 +04:00
|
|
|
result = 0;
|
2003-12-08 21:36:59 +03:00
|
|
|
}
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2004-04-22 00:23:58 +04:00
|
|
|
cmListFileLexerInit(lexer);
|
2003-12-08 21:36:59 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
cmListFileLexer_Token* cmListFileLexer_Scan(cmListFileLexer* lexer)
|
|
|
|
{
|
2016-05-17 20:23:06 +03:00
|
|
|
if (!lexer->file) {
|
2003-12-08 21:36:59 +03:00
|
|
|
return 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
|
|
|
if (cmListFileLexer_yylex(lexer->scanner, lexer)) {
|
2003-12-08 21:36:59 +03:00
|
|
|
return &lexer->token;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2013-10-14 23:13:11 +04:00
|
|
|
cmListFileLexer_SetFileName(lexer, 0, 0);
|
2003-12-08 21:36:59 +03:00
|
|
|
return 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
long cmListFileLexer_GetCurrentLine(cmListFileLexer* lexer)
|
|
|
|
{
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->file) {
|
2003-12-08 21:36:59 +03:00
|
|
|
return lexer->line;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2003-12-08 21:36:59 +03:00
|
|
|
return 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
long cmListFileLexer_GetCurrentColumn(cmListFileLexer* lexer)
|
|
|
|
{
|
2016-05-17 20:23:06 +03:00
|
|
|
if (lexer->file) {
|
2003-12-08 21:36:59 +03:00
|
|
|
return lexer->column;
|
2016-05-17 20:23:06 +03:00
|
|
|
} else {
|
2003-12-08 21:36:59 +03:00
|
|
|
return 0;
|
2016-05-17 20:23:06 +03:00
|
|
|
}
|
2003-12-08 21:36:59 +03:00
|
|
|
}
|
2004-09-01 02:39:42 +04:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
const char* cmListFileLexer_GetTypeAsString(cmListFileLexer* lexer,
|
|
|
|
cmListFileLexer_Type type)
|
|
|
|
{
|
|
|
|
(void)lexer;
|
2016-05-17 20:23:06 +03:00
|
|
|
switch (type) {
|
|
|
|
case cmListFileLexer_Token_None:
|
|
|
|
return "nothing";
|
|
|
|
case cmListFileLexer_Token_Space:
|
|
|
|
return "space";
|
|
|
|
case cmListFileLexer_Token_Newline:
|
|
|
|
return "newline";
|
|
|
|
case cmListFileLexer_Token_Identifier:
|
|
|
|
return "identifier";
|
|
|
|
case cmListFileLexer_Token_ParenLeft:
|
|
|
|
return "left paren";
|
|
|
|
case cmListFileLexer_Token_ParenRight:
|
|
|
|
return "right paren";
|
|
|
|
case cmListFileLexer_Token_ArgumentUnquoted:
|
|
|
|
return "unquoted argument";
|
|
|
|
case cmListFileLexer_Token_ArgumentQuoted:
|
|
|
|
return "quoted argument";
|
|
|
|
case cmListFileLexer_Token_ArgumentBracket:
|
|
|
|
return "bracket argument";
|
|
|
|
case cmListFileLexer_Token_CommentBracket:
|
|
|
|
return "bracket comment";
|
|
|
|
case cmListFileLexer_Token_BadCharacter:
|
|
|
|
return "bad character";
|
|
|
|
case cmListFileLexer_Token_BadBracket:
|
|
|
|
return "unterminated bracket";
|
|
|
|
case cmListFileLexer_Token_BadString:
|
|
|
|
return "unterminated string";
|
|
|
|
}
|
2004-09-01 02:39:42 +04:00
|
|
|
return "unknown token";
|
|
|
|
}
|