cmListFileLexer: Revise C++ coding style using clang-format

Manually extract the C++ portion of `cmListFileLexer.in.l` into a
temporary file, format it, and then move it back into the original file.
Manually format C++ code inside the lexer actions to match our style.
Then re-generate the lexer.
This commit is contained in:
Brad King 2016-05-17 13:23:06 -04:00
parent e4a92dab3f
commit 030556b72b
2 changed files with 241 additions and 329 deletions

View File

@ -980,32 +980,26 @@ YY_RULE_SETUP
{ {
const char* bracket = yytext; const char* bracket = yytext;
lexer->comment = yytext[0] == '#'; lexer->comment = yytext[0] == '#';
if(lexer->comment) if (lexer->comment) {
{
lexer->token.type = cmListFileLexer_Token_CommentBracket; lexer->token.type = cmListFileLexer_Token_CommentBracket;
bracket += 1; bracket += 1;
} } else {
else
{
lexer->token.type = cmListFileLexer_Token_ArgumentBracket; lexer->token.type = cmListFileLexer_Token_ArgumentBracket;
} }
cmListFileLexerSetToken(lexer, "", 0); cmListFileLexerSetToken(lexer, "", 0);
lexer->bracket = strchr(bracket+1, '[') - bracket; lexer->bracket = strchr(bracket+1, '[') - bracket;
if(yytext[yyleng-1] == '\n') if (yytext[yyleng-1] == '\n') {
{
++lexer->line; ++lexer->line;
lexer->column = 1; lexer->column = 1;
} } else {
else
{
lexer->column += yyleng; lexer->column += yyleng;
} }
BEGIN(BRACKET); BEGIN(BRACKET);
} }
YY_BREAK YY_BREAK
case 3: case 3:
YY_RULE_SETUP YY_RULE_SETUP
#line 125 "cmListFileLexer.in.l" #line 119 "cmListFileLexer.in.l"
{ {
lexer->column += yyleng; lexer->column += yyleng;
BEGIN(COMMENT); BEGIN(COMMENT);
@ -1013,14 +1007,14 @@ YY_RULE_SETUP
YY_BREAK YY_BREAK
case 4: case 4:
YY_RULE_SETUP YY_RULE_SETUP
#line 130 "cmListFileLexer.in.l" #line 124 "cmListFileLexer.in.l"
{ {
lexer->column += yyleng; lexer->column += yyleng;
} }
YY_BREAK YY_BREAK
case 5: case 5:
YY_RULE_SETUP YY_RULE_SETUP
#line 134 "cmListFileLexer.in.l" #line 128 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_ParenLeft; lexer->token.type = cmListFileLexer_Token_ParenLeft;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1029,7 +1023,7 @@ YY_RULE_SETUP
} }
case 6: case 6:
YY_RULE_SETUP YY_RULE_SETUP
#line 141 "cmListFileLexer.in.l" #line 135 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_ParenRight; lexer->token.type = cmListFileLexer_Token_ParenRight;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1038,7 +1032,7 @@ YY_RULE_SETUP
} }
case 7: case 7:
YY_RULE_SETUP YY_RULE_SETUP
#line 148 "cmListFileLexer.in.l" #line 142 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_Identifier; lexer->token.type = cmListFileLexer_Token_Identifier;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1047,20 +1041,19 @@ YY_RULE_SETUP
} }
case 8: case 8:
YY_RULE_SETUP YY_RULE_SETUP
#line 155 "cmListFileLexer.in.l" #line 149 "cmListFileLexer.in.l"
{ {
/* Handle ]]====]=======]*/ /* Handle ]]====]=======]*/
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
lexer->column += yyleng; lexer->column += yyleng;
if(yyleng == lexer->bracket) if (yyleng == lexer->bracket) {
{
BEGIN(BRACKETEND); BEGIN(BRACKETEND);
} }
} }
YY_BREAK YY_BREAK
case 9: case 9:
YY_RULE_SETUP YY_RULE_SETUP
#line 165 "cmListFileLexer.in.l" #line 158 "cmListFileLexer.in.l"
{ {
lexer->column += yyleng; lexer->column += yyleng;
/* Erase the partial bracket from the token. */ /* Erase the partial bracket from the token. */
@ -1071,7 +1064,7 @@ YY_RULE_SETUP
} }
case 10: case 10:
YY_RULE_SETUP YY_RULE_SETUP
#line 174 "cmListFileLexer.in.l" #line 167 "cmListFileLexer.in.l"
{ {
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
lexer->column += yyleng; lexer->column += yyleng;
@ -1080,7 +1073,7 @@ YY_RULE_SETUP
case 11: case 11:
/* rule 11 can match eol */ /* rule 11 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 179 "cmListFileLexer.in.l" #line 172 "cmListFileLexer.in.l"
{ {
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
++lexer->line; ++lexer->line;
@ -1090,7 +1083,7 @@ YY_RULE_SETUP
YY_BREAK YY_BREAK
case 12: case 12:
YY_RULE_SETUP YY_RULE_SETUP
#line 186 "cmListFileLexer.in.l" #line 179 "cmListFileLexer.in.l"
{ {
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
lexer->column += yyleng; lexer->column += yyleng;
@ -1099,7 +1092,7 @@ YY_RULE_SETUP
YY_BREAK YY_BREAK
case YY_STATE_EOF(BRACKET): case YY_STATE_EOF(BRACKET):
case YY_STATE_EOF(BRACKETEND): case YY_STATE_EOF(BRACKETEND):
#line 192 "cmListFileLexer.in.l" #line 185 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_BadBracket; lexer->token.type = cmListFileLexer_Token_BadBracket;
BEGIN(INITIAL); BEGIN(INITIAL);
@ -1107,7 +1100,7 @@ case YY_STATE_EOF(BRACKETEND):
} }
case 13: case 13:
YY_RULE_SETUP YY_RULE_SETUP
#line 198 "cmListFileLexer.in.l" #line 191 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted; lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1116,7 +1109,7 @@ YY_RULE_SETUP
} }
case 14: case 14:
YY_RULE_SETUP YY_RULE_SETUP
#line 205 "cmListFileLexer.in.l" #line 198 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted; lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1125,7 +1118,7 @@ YY_RULE_SETUP
} }
case 15: case 15:
YY_RULE_SETUP YY_RULE_SETUP
#line 212 "cmListFileLexer.in.l" #line 205 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted; lexer->token.type = cmListFileLexer_Token_ArgumentUnquoted;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1134,7 +1127,7 @@ YY_RULE_SETUP
} }
case 16: case 16:
YY_RULE_SETUP YY_RULE_SETUP
#line 219 "cmListFileLexer.in.l" #line 212 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_ArgumentQuoted; lexer->token.type = cmListFileLexer_Token_ArgumentQuoted;
cmListFileLexerSetToken(lexer, "", 0); cmListFileLexerSetToken(lexer, "", 0);
@ -1144,7 +1137,7 @@ YY_RULE_SETUP
YY_BREAK YY_BREAK
case 17: case 17:
YY_RULE_SETUP YY_RULE_SETUP
#line 226 "cmListFileLexer.in.l" #line 219 "cmListFileLexer.in.l"
{ {
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
lexer->column += yyleng; lexer->column += yyleng;
@ -1153,7 +1146,7 @@ YY_RULE_SETUP
case 18: case 18:
/* rule 18 can match eol */ /* rule 18 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 231 "cmListFileLexer.in.l" #line 224 "cmListFileLexer.in.l"
{ {
/* Continuation: text is not part of string */ /* Continuation: text is not part of string */
++lexer->line; ++lexer->line;
@ -1163,7 +1156,7 @@ YY_RULE_SETUP
case 19: case 19:
/* rule 19 can match eol */ /* rule 19 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 237 "cmListFileLexer.in.l" #line 230 "cmListFileLexer.in.l"
{ {
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
++lexer->line; ++lexer->line;
@ -1172,7 +1165,7 @@ YY_RULE_SETUP
YY_BREAK YY_BREAK
case 20: case 20:
YY_RULE_SETUP YY_RULE_SETUP
#line 243 "cmListFileLexer.in.l" #line 236 "cmListFileLexer.in.l"
{ {
lexer->column += yyleng; lexer->column += yyleng;
BEGIN(INITIAL); BEGIN(INITIAL);
@ -1180,14 +1173,14 @@ YY_RULE_SETUP
} }
case 21: case 21:
YY_RULE_SETUP YY_RULE_SETUP
#line 249 "cmListFileLexer.in.l" #line 242 "cmListFileLexer.in.l"
{ {
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
lexer->column += yyleng; lexer->column += yyleng;
} }
YY_BREAK YY_BREAK
case YY_STATE_EOF(STRING): case YY_STATE_EOF(STRING):
#line 254 "cmListFileLexer.in.l" #line 247 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_BadString; lexer->token.type = cmListFileLexer_Token_BadString;
BEGIN(INITIAL); BEGIN(INITIAL);
@ -1195,7 +1188,7 @@ case YY_STATE_EOF(STRING):
} }
case 22: case 22:
YY_RULE_SETUP YY_RULE_SETUP
#line 260 "cmListFileLexer.in.l" #line 253 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_Space; lexer->token.type = cmListFileLexer_Token_Space;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1204,7 +1197,7 @@ YY_RULE_SETUP
} }
case 23: case 23:
YY_RULE_SETUP YY_RULE_SETUP
#line 267 "cmListFileLexer.in.l" #line 260 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_BadCharacter; lexer->token.type = cmListFileLexer_Token_BadCharacter;
cmListFileLexerSetToken(lexer, yytext, yyleng); cmListFileLexerSetToken(lexer, yytext, yyleng);
@ -1213,7 +1206,7 @@ YY_RULE_SETUP
} }
case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(COMMENT): case YY_STATE_EOF(COMMENT):
#line 274 "cmListFileLexer.in.l" #line 267 "cmListFileLexer.in.l"
{ {
lexer->token.type = cmListFileLexer_Token_None; lexer->token.type = cmListFileLexer_Token_None;
cmListFileLexerSetToken(lexer, 0, 0); cmListFileLexerSetToken(lexer, 0, 0);
@ -1221,10 +1214,10 @@ case YY_STATE_EOF(COMMENT):
} }
case 24: case 24:
YY_RULE_SETUP YY_RULE_SETUP
#line 280 "cmListFileLexer.in.l" #line 273 "cmListFileLexer.in.l"
ECHO; ECHO;
YY_BREAK YY_BREAK
#line 1242 "cmListFileLexer.c" #line 1235 "cmListFileLexer.c"
case YY_END_OF_BUFFER: case YY_END_OF_BUFFER:
{ {
@ -2401,7 +2394,7 @@ void cmListFileLexer_yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables" #define YYTABLES_NAME "yytables"
#line 280 "cmListFileLexer.in.l" #line 273 "cmListFileLexer.in.l"
@ -2414,30 +2407,25 @@ static void cmListFileLexerSetToken(cmListFileLexer* lexer, const char* text,
lexer->token.column = lexer->column; lexer->token.column = lexer->column;
/* Use the same buffer if possible. */ /* Use the same buffer if possible. */
if(lexer->token.text) if (lexer->token.text) {
{ if (text && length < lexer->size) {
if(text && length < lexer->size)
{
strcpy(lexer->token.text, text); strcpy(lexer->token.text, text);
lexer->token.length = length; lexer->token.length = length;
return; return;
} }
free(lexer->token.text); free(lexer->token.text);
lexer->token.text = 0; lexer->token.text = 0;
lexer->size = 0; lexer->size = 0;
} }
/* Need to extend the buffer. */ /* Need to extend the buffer. */
if(text) if (text) {
{
lexer->token.text = strdup(text); lexer->token.text = strdup(text);
lexer->token.length = length; lexer->token.length = length;
lexer->size = length+1; lexer->size = length + 1;
} } else {
else
{
lexer->token.length = 0; lexer->token.length = 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@ -2449,22 +2437,20 @@ static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
/* If the appended text will fit in the buffer, do not reallocate. */ /* If the appended text will fit in the buffer, do not reallocate. */
newSize = lexer->token.length + length + 1; newSize = lexer->token.length + length + 1;
if(lexer->token.text && newSize <= lexer->size) if (lexer->token.text && newSize <= lexer->size) {
{ strcpy(lexer->token.text + lexer->token.length, text);
strcpy(lexer->token.text+lexer->token.length, text);
lexer->token.length += length; lexer->token.length += length;
return; return;
} }
/* We need to extend the buffer. */ /* We need to extend the buffer. */
temp = malloc(newSize); temp = malloc(newSize);
if(lexer->token.text) if (lexer->token.text) {
{
memcpy(temp, lexer->token.text, lexer->token.length); memcpy(temp, lexer->token.text, lexer->token.length);
free(lexer->token.text); free(lexer->token.text);
} }
memcpy(temp+lexer->token.length, text, length); memcpy(temp + lexer->token.length, text, length);
temp[lexer->token.length+length] = 0; temp[lexer->token.length + length] = 0;
lexer->token.text = temp; lexer->token.text = temp;
lexer->token.length += length; lexer->token.length += length;
lexer->size = newSize; lexer->size = newSize;
@ -2474,96 +2460,84 @@ static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer, static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer,
size_t bufferSize) size_t bufferSize)
{ {
if(lexer) if (lexer) {
{ if (lexer->file) {
if(lexer->file)
{
/* Convert CRLF -> LF explicitly. The C FILE "t"ext mode /* Convert CRLF -> LF explicitly. The C FILE "t"ext mode
does not convert newlines on all platforms. Move any does not convert newlines on all platforms. Move any
trailing CR to the start of the buffer for the next read. */ trailing CR to the start of the buffer for the next read. */
size_t cr = lexer->cr; size_t cr = lexer->cr;
size_t n; size_t n;
buffer[0] = '\r'; buffer[0] = '\r';
n = fread(buffer+cr, 1, bufferSize-cr, lexer->file); n = fread(buffer + cr, 1, bufferSize - cr, lexer->file);
if(n) if (n) {
{
char* o = buffer; char* o = buffer;
const char* i = buffer; const char* i = buffer;
const char* e; const char* e;
n += cr; n += cr;
cr = (buffer[n-1] == '\r')? 1:0; cr = (buffer[n - 1] == '\r') ? 1 : 0;
e = buffer + n - cr; e = buffer + n - cr;
while(i != e) while (i != e) {
{ if (i[0] == '\r' && i[1] == '\n') {
if(i[0] == '\r' && i[1] == '\n')
{
++i; ++i;
}
*o++ = *i++;
} }
n = o - buffer; *o++ = *i++;
} }
else n = o - buffer;
{ } else {
n = cr; n = cr;
cr = 0; cr = 0;
} }
lexer->cr = cr; lexer->cr = cr;
return n; return n;
} } else if (lexer->string_left) {
else if(lexer->string_left)
{
int length = lexer->string_left; int length = lexer->string_left;
if((int)bufferSize < length) { length = (int)bufferSize; } if ((int)bufferSize < length) {
length = (int)bufferSize;
}
memcpy(buffer, lexer->string_position, length); memcpy(buffer, lexer->string_position, length);
lexer->string_position += length; lexer->string_position += length;
lexer->string_left -= length; lexer->string_left -= length;
return length; return length;
}
} }
}
return 0; return 0;
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
static void cmListFileLexerInit(cmListFileLexer* lexer) static void cmListFileLexerInit(cmListFileLexer* lexer)
{ {
if(lexer->file || lexer->string_buffer) if (lexer->file || lexer->string_buffer) {
{
cmListFileLexer_yylex_init(&lexer->scanner); cmListFileLexer_yylex_init(&lexer->scanner);
cmListFileLexer_yyset_extra(lexer, lexer->scanner); cmListFileLexer_yyset_extra(lexer, lexer->scanner);
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
static void cmListFileLexerDestroy(cmListFileLexer* lexer) static void cmListFileLexerDestroy(cmListFileLexer* lexer)
{ {
cmListFileLexerSetToken(lexer, 0, 0); cmListFileLexerSetToken(lexer, 0, 0);
if(lexer->file || lexer->string_buffer) if (lexer->file || lexer->string_buffer) {
{
cmListFileLexer_yylex_destroy(lexer->scanner); cmListFileLexer_yylex_destroy(lexer->scanner);
if(lexer->file) if (lexer->file) {
{
fclose(lexer->file); fclose(lexer->file);
lexer->file = 0; lexer->file = 0;
} }
if(lexer->string_buffer) if (lexer->string_buffer) {
{
free(lexer->string_buffer); free(lexer->string_buffer);
lexer->string_buffer = 0; lexer->string_buffer = 0;
lexer->string_left = 0; lexer->string_left = 0;
lexer->string_position = 0; lexer->string_position = 0;
}
} }
}
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
cmListFileLexer* cmListFileLexer_New() cmListFileLexer* cmListFileLexer_New()
{ {
cmListFileLexer* lexer = (cmListFileLexer*)malloc(sizeof(cmListFileLexer)); cmListFileLexer* lexer = (cmListFileLexer*)malloc(sizeof(cmListFileLexer));
if(!lexer) if (!lexer) {
{
return 0; return 0;
} }
memset(lexer, 0, sizeof(*lexer)); memset(lexer, 0, sizeof(*lexer));
lexer->line = 1; lexer->line = 1;
lexer->column = 1; lexer->column = 1;
@ -2581,39 +2555,28 @@ void cmListFileLexer_Delete(cmListFileLexer* lexer)
static cmListFileLexer_BOM cmListFileLexer_ReadBOM(FILE* f) static cmListFileLexer_BOM cmListFileLexer_ReadBOM(FILE* f)
{ {
unsigned char b[2]; unsigned char b[2];
if(fread(b, 1, 2, f) == 2) if (fread(b, 1, 2, f) == 2) {
{ if (b[0] == 0xEF && b[1] == 0xBB) {
if(b[0] == 0xEF && b[1] == 0xBB) if (fread(b, 1, 1, f) == 1 && b[0] == 0xBF) {
{
if(fread(b, 1, 1, f) == 1 && b[0] == 0xBF)
{
return cmListFileLexer_BOM_UTF8; return cmListFileLexer_BOM_UTF8;
}
} }
else if(b[0] == 0xFE && b[1] == 0xFF) } else if (b[0] == 0xFE && b[1] == 0xFF) {
{
/* UTF-16 BE */ /* UTF-16 BE */
return cmListFileLexer_BOM_UTF16BE; return cmListFileLexer_BOM_UTF16BE;
} } else if (b[0] == 0 && b[1] == 0) {
else if(b[0] == 0 && b[1] == 0) if (fread(b, 1, 2, f) == 2 && b[0] == 0xFE && b[1] == 0xFF) {
{
if(fread(b, 1, 2, f) == 2 && b[0] == 0xFE && b[1] == 0xFF)
{
return cmListFileLexer_BOM_UTF32BE; return cmListFileLexer_BOM_UTF32BE;
}
} }
else if(b[0] == 0xFF && b[1] == 0xFE) } else if (b[0] == 0xFF && b[1] == 0xFE) {
{
fpos_t p; fpos_t p;
fgetpos(f, &p); fgetpos(f, &p);
if(fread(b, 1, 2, f) == 2 && b[0] == 0 && b[1] == 0) if (fread(b, 1, 2, f) == 2 && b[0] == 0 && b[1] == 0) {
{
return cmListFileLexer_BOM_UTF32LE; return cmListFileLexer_BOM_UTF32LE;
} }
fsetpos(f, &p); fsetpos(f, &p);
return cmListFileLexer_BOM_UTF16LE; return cmListFileLexer_BOM_UTF16LE;
}
} }
}
rewind(f); rewind(f);
return cmListFileLexer_BOM_None; return cmListFileLexer_BOM_None;
} }
@ -2624,8 +2587,7 @@ int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name,
{ {
int result = 1; int result = 1;
cmListFileLexerDestroy(lexer); cmListFileLexerDestroy(lexer);
if(name) if (name) {
{
#ifdef _WIN32 #ifdef _WIN32
wchar_t* wname = cmsysEncoding_DupToWide(name); wchar_t* wname = cmsysEncoding_DupToWide(name);
lexer->file = _wfopen(wname, L"rb"); lexer->file = _wfopen(wname, L"rb");
@ -2633,18 +2595,14 @@ int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name,
#else #else
lexer->file = fopen(name, "rb"); lexer->file = fopen(name, "rb");
#endif #endif
if(lexer->file) if (lexer->file) {
{ if (bom) {
if(bom)
{
*bom = cmListFileLexer_ReadBOM(lexer->file); *bom = cmListFileLexer_ReadBOM(lexer->file);
}
} }
else } else {
{
result = 0; result = 0;
}
} }
}
cmListFileLexerInit(lexer); cmListFileLexerInit(lexer);
return result; return result;
} }
@ -2654,21 +2612,17 @@ int cmListFileLexer_SetString(cmListFileLexer* lexer, const char* text)
{ {
int result = 1; int result = 1;
cmListFileLexerDestroy(lexer); cmListFileLexerDestroy(lexer);
if(text) if (text) {
{
int length = (int)strlen(text); int length = (int)strlen(text);
lexer->string_buffer = (char*)malloc(length+1); lexer->string_buffer = (char*)malloc(length + 1);
if(lexer->string_buffer) if (lexer->string_buffer) {
{
strcpy(lexer->string_buffer, text); strcpy(lexer->string_buffer, text);
lexer->string_position = lexer->string_buffer; lexer->string_position = lexer->string_buffer;
lexer->string_left = length; lexer->string_left = length;
} } else {
else
{
result = 0; result = 0;
}
} }
}
cmListFileLexerInit(lexer); cmListFileLexerInit(lexer);
return result; return result;
} }
@ -2676,45 +2630,35 @@ int cmListFileLexer_SetString(cmListFileLexer* lexer, const char* text)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
cmListFileLexer_Token* cmListFileLexer_Scan(cmListFileLexer* lexer) cmListFileLexer_Token* cmListFileLexer_Scan(cmListFileLexer* lexer)
{ {
if(!lexer->file) if (!lexer->file) {
{
return 0; return 0;
} }
if(cmListFileLexer_yylex(lexer->scanner, lexer)) if (cmListFileLexer_yylex(lexer->scanner, lexer)) {
{
return &lexer->token; return &lexer->token;
} } else {
else
{
cmListFileLexer_SetFileName(lexer, 0, 0); cmListFileLexer_SetFileName(lexer, 0, 0);
return 0; return 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
long cmListFileLexer_GetCurrentLine(cmListFileLexer* lexer) long cmListFileLexer_GetCurrentLine(cmListFileLexer* lexer)
{ {
if(lexer->file) if (lexer->file) {
{
return lexer->line; return lexer->line;
} } else {
else
{
return 0; return 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
long cmListFileLexer_GetCurrentColumn(cmListFileLexer* lexer) long cmListFileLexer_GetCurrentColumn(cmListFileLexer* lexer)
{ {
if(lexer->file) if (lexer->file) {
{
return lexer->column; return lexer->column;
} } else {
else
{
return 0; return 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@ -2722,21 +2666,33 @@ const char* cmListFileLexer_GetTypeAsString(cmListFileLexer* lexer,
cmListFileLexer_Type type) cmListFileLexer_Type type)
{ {
(void)lexer; (void)lexer;
switch(type) switch (type) {
{ case cmListFileLexer_Token_None:
case cmListFileLexer_Token_None: return "nothing"; return "nothing";
case cmListFileLexer_Token_Space: return "space"; case cmListFileLexer_Token_Space:
case cmListFileLexer_Token_Newline: return "newline"; return "space";
case cmListFileLexer_Token_Identifier: return "identifier"; case cmListFileLexer_Token_Newline:
case cmListFileLexer_Token_ParenLeft: return "left paren"; return "newline";
case cmListFileLexer_Token_ParenRight: return "right paren"; case cmListFileLexer_Token_Identifier:
case cmListFileLexer_Token_ArgumentUnquoted: return "unquoted argument"; return "identifier";
case cmListFileLexer_Token_ArgumentQuoted: return "quoted argument"; case cmListFileLexer_Token_ParenLeft:
case cmListFileLexer_Token_ArgumentBracket: return "bracket argument"; return "left paren";
case cmListFileLexer_Token_CommentBracket: return "bracket comment"; case cmListFileLexer_Token_ParenRight:
case cmListFileLexer_Token_BadCharacter: return "bad character"; return "right paren";
case cmListFileLexer_Token_BadBracket: return "unterminated bracket"; case cmListFileLexer_Token_ArgumentUnquoted:
case cmListFileLexer_Token_BadString: return "unterminated string"; 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";
}
return "unknown token"; return "unknown token";
} }

View File

@ -99,26 +99,20 @@ LEGACY {MAKEVAR}|{UNQUOTED}|\"({MAKEVAR}|{UNQUOTED}|[ \t[=])*\"
#?\[=*\[\n? { #?\[=*\[\n? {
const char* bracket = yytext; const char* bracket = yytext;
lexer->comment = yytext[0] == '#'; lexer->comment = yytext[0] == '#';
if(lexer->comment) if (lexer->comment) {
{
lexer->token.type = cmListFileLexer_Token_CommentBracket; lexer->token.type = cmListFileLexer_Token_CommentBracket;
bracket += 1; bracket += 1;
} } else {
else
{
lexer->token.type = cmListFileLexer_Token_ArgumentBracket; lexer->token.type = cmListFileLexer_Token_ArgumentBracket;
} }
cmListFileLexerSetToken(lexer, "", 0); cmListFileLexerSetToken(lexer, "", 0);
lexer->bracket = strchr(bracket+1, '[') - bracket; lexer->bracket = strchr(bracket+1, '[') - bracket;
if(yytext[yyleng-1] == '\n') if (yytext[yyleng-1] == '\n') {
{
++lexer->line; ++lexer->line;
lexer->column = 1; lexer->column = 1;
} } else {
else
{
lexer->column += yyleng; lexer->column += yyleng;
} }
BEGIN(BRACKET); BEGIN(BRACKET);
} }
@ -156,10 +150,9 @@ LEGACY {MAKEVAR}|{UNQUOTED}|\"({MAKEVAR}|{UNQUOTED}|[ \t[=])*\"
/* Handle ]]====]=======]*/ /* Handle ]]====]=======]*/
cmListFileLexerAppend(lexer, yytext, yyleng); cmListFileLexerAppend(lexer, yytext, yyleng);
lexer->column += yyleng; lexer->column += yyleng;
if(yyleng == lexer->bracket) if (yyleng == lexer->bracket) {
{
BEGIN(BRACKETEND); BEGIN(BRACKETEND);
} }
} }
<BRACKETEND>\] { <BRACKETEND>\] {
@ -288,30 +281,25 @@ static void cmListFileLexerSetToken(cmListFileLexer* lexer, const char* text,
lexer->token.column = lexer->column; lexer->token.column = lexer->column;
/* Use the same buffer if possible. */ /* Use the same buffer if possible. */
if(lexer->token.text) if (lexer->token.text) {
{ if (text && length < lexer->size) {
if(text && length < lexer->size)
{
strcpy(lexer->token.text, text); strcpy(lexer->token.text, text);
lexer->token.length = length; lexer->token.length = length;
return; return;
} }
free(lexer->token.text); free(lexer->token.text);
lexer->token.text = 0; lexer->token.text = 0;
lexer->size = 0; lexer->size = 0;
} }
/* Need to extend the buffer. */ /* Need to extend the buffer. */
if(text) if (text) {
{
lexer->token.text = strdup(text); lexer->token.text = strdup(text);
lexer->token.length = length; lexer->token.length = length;
lexer->size = length+1; lexer->size = length + 1;
} } else {
else
{
lexer->token.length = 0; lexer->token.length = 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@ -323,22 +311,20 @@ static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
/* If the appended text will fit in the buffer, do not reallocate. */ /* If the appended text will fit in the buffer, do not reallocate. */
newSize = lexer->token.length + length + 1; newSize = lexer->token.length + length + 1;
if(lexer->token.text && newSize <= lexer->size) if (lexer->token.text && newSize <= lexer->size) {
{ strcpy(lexer->token.text + lexer->token.length, text);
strcpy(lexer->token.text+lexer->token.length, text);
lexer->token.length += length; lexer->token.length += length;
return; return;
} }
/* We need to extend the buffer. */ /* We need to extend the buffer. */
temp = malloc(newSize); temp = malloc(newSize);
if(lexer->token.text) if (lexer->token.text) {
{
memcpy(temp, lexer->token.text, lexer->token.length); memcpy(temp, lexer->token.text, lexer->token.length);
free(lexer->token.text); free(lexer->token.text);
} }
memcpy(temp+lexer->token.length, text, length); memcpy(temp + lexer->token.length, text, length);
temp[lexer->token.length+length] = 0; temp[lexer->token.length + length] = 0;
lexer->token.text = temp; lexer->token.text = temp;
lexer->token.length += length; lexer->token.length += length;
lexer->size = newSize; lexer->size = newSize;
@ -348,96 +334,84 @@ static void cmListFileLexerAppend(cmListFileLexer* lexer, const char* text,
static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer, static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer,
size_t bufferSize) size_t bufferSize)
{ {
if(lexer) if (lexer) {
{ if (lexer->file) {
if(lexer->file)
{
/* Convert CRLF -> LF explicitly. The C FILE "t"ext mode /* Convert CRLF -> LF explicitly. The C FILE "t"ext mode
does not convert newlines on all platforms. Move any does not convert newlines on all platforms. Move any
trailing CR to the start of the buffer for the next read. */ trailing CR to the start of the buffer for the next read. */
size_t cr = lexer->cr; size_t cr = lexer->cr;
size_t n; size_t n;
buffer[0] = '\r'; buffer[0] = '\r';
n = fread(buffer+cr, 1, bufferSize-cr, lexer->file); n = fread(buffer + cr, 1, bufferSize - cr, lexer->file);
if(n) if (n) {
{
char* o = buffer; char* o = buffer;
const char* i = buffer; const char* i = buffer;
const char* e; const char* e;
n += cr; n += cr;
cr = (buffer[n-1] == '\r')? 1:0; cr = (buffer[n - 1] == '\r') ? 1 : 0;
e = buffer + n - cr; e = buffer + n - cr;
while(i != e) while (i != e) {
{ if (i[0] == '\r' && i[1] == '\n') {
if(i[0] == '\r' && i[1] == '\n')
{
++i; ++i;
}
*o++ = *i++;
} }
n = o - buffer; *o++ = *i++;
} }
else n = o - buffer;
{ } else {
n = cr; n = cr;
cr = 0; cr = 0;
} }
lexer->cr = cr; lexer->cr = cr;
return n; return n;
} } else if (lexer->string_left) {
else if(lexer->string_left)
{
int length = lexer->string_left; int length = lexer->string_left;
if((int)bufferSize < length) { length = (int)bufferSize; } if ((int)bufferSize < length) {
length = (int)bufferSize;
}
memcpy(buffer, lexer->string_position, length); memcpy(buffer, lexer->string_position, length);
lexer->string_position += length; lexer->string_position += length;
lexer->string_left -= length; lexer->string_left -= length;
return length; return length;
}
} }
}
return 0; return 0;
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
static void cmListFileLexerInit(cmListFileLexer* lexer) static void cmListFileLexerInit(cmListFileLexer* lexer)
{ {
if(lexer->file || lexer->string_buffer) if (lexer->file || lexer->string_buffer) {
{
cmListFileLexer_yylex_init(&lexer->scanner); cmListFileLexer_yylex_init(&lexer->scanner);
cmListFileLexer_yyset_extra(lexer, lexer->scanner); cmListFileLexer_yyset_extra(lexer, lexer->scanner);
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
static void cmListFileLexerDestroy(cmListFileLexer* lexer) static void cmListFileLexerDestroy(cmListFileLexer* lexer)
{ {
cmListFileLexerSetToken(lexer, 0, 0); cmListFileLexerSetToken(lexer, 0, 0);
if(lexer->file || lexer->string_buffer) if (lexer->file || lexer->string_buffer) {
{
cmListFileLexer_yylex_destroy(lexer->scanner); cmListFileLexer_yylex_destroy(lexer->scanner);
if(lexer->file) if (lexer->file) {
{
fclose(lexer->file); fclose(lexer->file);
lexer->file = 0; lexer->file = 0;
} }
if(lexer->string_buffer) if (lexer->string_buffer) {
{
free(lexer->string_buffer); free(lexer->string_buffer);
lexer->string_buffer = 0; lexer->string_buffer = 0;
lexer->string_left = 0; lexer->string_left = 0;
lexer->string_position = 0; lexer->string_position = 0;
}
} }
}
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
cmListFileLexer* cmListFileLexer_New() cmListFileLexer* cmListFileLexer_New()
{ {
cmListFileLexer* lexer = (cmListFileLexer*)malloc(sizeof(cmListFileLexer)); cmListFileLexer* lexer = (cmListFileLexer*)malloc(sizeof(cmListFileLexer));
if(!lexer) if (!lexer) {
{
return 0; return 0;
} }
memset(lexer, 0, sizeof(*lexer)); memset(lexer, 0, sizeof(*lexer));
lexer->line = 1; lexer->line = 1;
lexer->column = 1; lexer->column = 1;
@ -455,39 +429,28 @@ void cmListFileLexer_Delete(cmListFileLexer* lexer)
static cmListFileLexer_BOM cmListFileLexer_ReadBOM(FILE* f) static cmListFileLexer_BOM cmListFileLexer_ReadBOM(FILE* f)
{ {
unsigned char b[2]; unsigned char b[2];
if(fread(b, 1, 2, f) == 2) if (fread(b, 1, 2, f) == 2) {
{ if (b[0] == 0xEF && b[1] == 0xBB) {
if(b[0] == 0xEF && b[1] == 0xBB) if (fread(b, 1, 1, f) == 1 && b[0] == 0xBF) {
{
if(fread(b, 1, 1, f) == 1 && b[0] == 0xBF)
{
return cmListFileLexer_BOM_UTF8; return cmListFileLexer_BOM_UTF8;
}
} }
else if(b[0] == 0xFE && b[1] == 0xFF) } else if (b[0] == 0xFE && b[1] == 0xFF) {
{
/* UTF-16 BE */ /* UTF-16 BE */
return cmListFileLexer_BOM_UTF16BE; return cmListFileLexer_BOM_UTF16BE;
} } else if (b[0] == 0 && b[1] == 0) {
else if(b[0] == 0 && b[1] == 0) if (fread(b, 1, 2, f) == 2 && b[0] == 0xFE && b[1] == 0xFF) {
{
if(fread(b, 1, 2, f) == 2 && b[0] == 0xFE && b[1] == 0xFF)
{
return cmListFileLexer_BOM_UTF32BE; return cmListFileLexer_BOM_UTF32BE;
}
} }
else if(b[0] == 0xFF && b[1] == 0xFE) } else if (b[0] == 0xFF && b[1] == 0xFE) {
{
fpos_t p; fpos_t p;
fgetpos(f, &p); fgetpos(f, &p);
if(fread(b, 1, 2, f) == 2 && b[0] == 0 && b[1] == 0) if (fread(b, 1, 2, f) == 2 && b[0] == 0 && b[1] == 0) {
{
return cmListFileLexer_BOM_UTF32LE; return cmListFileLexer_BOM_UTF32LE;
} }
fsetpos(f, &p); fsetpos(f, &p);
return cmListFileLexer_BOM_UTF16LE; return cmListFileLexer_BOM_UTF16LE;
}
} }
}
rewind(f); rewind(f);
return cmListFileLexer_BOM_None; return cmListFileLexer_BOM_None;
} }
@ -498,8 +461,7 @@ int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name,
{ {
int result = 1; int result = 1;
cmListFileLexerDestroy(lexer); cmListFileLexerDestroy(lexer);
if(name) if (name) {
{
#ifdef _WIN32 #ifdef _WIN32
wchar_t* wname = cmsysEncoding_DupToWide(name); wchar_t* wname = cmsysEncoding_DupToWide(name);
lexer->file = _wfopen(wname, L"rb"); lexer->file = _wfopen(wname, L"rb");
@ -507,18 +469,14 @@ int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name,
#else #else
lexer->file = fopen(name, "rb"); lexer->file = fopen(name, "rb");
#endif #endif
if(lexer->file) if (lexer->file) {
{ if (bom) {
if(bom)
{
*bom = cmListFileLexer_ReadBOM(lexer->file); *bom = cmListFileLexer_ReadBOM(lexer->file);
}
} }
else } else {
{
result = 0; result = 0;
}
} }
}
cmListFileLexerInit(lexer); cmListFileLexerInit(lexer);
return result; return result;
} }
@ -528,21 +486,17 @@ int cmListFileLexer_SetString(cmListFileLexer* lexer, const char* text)
{ {
int result = 1; int result = 1;
cmListFileLexerDestroy(lexer); cmListFileLexerDestroy(lexer);
if(text) if (text) {
{
int length = (int)strlen(text); int length = (int)strlen(text);
lexer->string_buffer = (char*)malloc(length+1); lexer->string_buffer = (char*)malloc(length + 1);
if(lexer->string_buffer) if (lexer->string_buffer) {
{
strcpy(lexer->string_buffer, text); strcpy(lexer->string_buffer, text);
lexer->string_position = lexer->string_buffer; lexer->string_position = lexer->string_buffer;
lexer->string_left = length; lexer->string_left = length;
} } else {
else
{
result = 0; result = 0;
}
} }
}
cmListFileLexerInit(lexer); cmListFileLexerInit(lexer);
return result; return result;
} }
@ -550,45 +504,35 @@ int cmListFileLexer_SetString(cmListFileLexer* lexer, const char* text)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
cmListFileLexer_Token* cmListFileLexer_Scan(cmListFileLexer* lexer) cmListFileLexer_Token* cmListFileLexer_Scan(cmListFileLexer* lexer)
{ {
if(!lexer->file) if (!lexer->file) {
{
return 0; return 0;
} }
if(cmListFileLexer_yylex(lexer->scanner, lexer)) if (cmListFileLexer_yylex(lexer->scanner, lexer)) {
{
return &lexer->token; return &lexer->token;
} } else {
else
{
cmListFileLexer_SetFileName(lexer, 0, 0); cmListFileLexer_SetFileName(lexer, 0, 0);
return 0; return 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
long cmListFileLexer_GetCurrentLine(cmListFileLexer* lexer) long cmListFileLexer_GetCurrentLine(cmListFileLexer* lexer)
{ {
if(lexer->file) if (lexer->file) {
{
return lexer->line; return lexer->line;
} } else {
else
{
return 0; return 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
long cmListFileLexer_GetCurrentColumn(cmListFileLexer* lexer) long cmListFileLexer_GetCurrentColumn(cmListFileLexer* lexer)
{ {
if(lexer->file) if (lexer->file) {
{
return lexer->column; return lexer->column;
} } else {
else
{
return 0; return 0;
} }
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@ -596,21 +540,33 @@ const char* cmListFileLexer_GetTypeAsString(cmListFileLexer* lexer,
cmListFileLexer_Type type) cmListFileLexer_Type type)
{ {
(void)lexer; (void)lexer;
switch(type) switch (type) {
{ case cmListFileLexer_Token_None:
case cmListFileLexer_Token_None: return "nothing"; return "nothing";
case cmListFileLexer_Token_Space: return "space"; case cmListFileLexer_Token_Space:
case cmListFileLexer_Token_Newline: return "newline"; return "space";
case cmListFileLexer_Token_Identifier: return "identifier"; case cmListFileLexer_Token_Newline:
case cmListFileLexer_Token_ParenLeft: return "left paren"; return "newline";
case cmListFileLexer_Token_ParenRight: return "right paren"; case cmListFileLexer_Token_Identifier:
case cmListFileLexer_Token_ArgumentUnquoted: return "unquoted argument"; return "identifier";
case cmListFileLexer_Token_ArgumentQuoted: return "quoted argument"; case cmListFileLexer_Token_ParenLeft:
case cmListFileLexer_Token_ArgumentBracket: return "bracket argument"; return "left paren";
case cmListFileLexer_Token_CommentBracket: return "bracket comment"; case cmListFileLexer_Token_ParenRight:
case cmListFileLexer_Token_BadCharacter: return "bad character"; return "right paren";
case cmListFileLexer_Token_BadBracket: return "unterminated bracket"; case cmListFileLexer_Token_ArgumentUnquoted:
case cmListFileLexer_Token_BadString: return "unterminated string"; 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";
}
return "unknown token"; return "unknown token";
} }