Skip to content

Commit 9262ea1

Browse files
author
aothms
committed
Fixes for parsing files with comments
1 parent 81537be commit 9262ea1

2 files changed

Lines changed: 47 additions & 29 deletions

File tree

src/ifcparse/IfcParse.cpp

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,54 @@ Tokens::~Tokens() {
221221
delete decoder;
222222
}
223223

224+
unsigned int Tokens::skipWhitespace() {
225+
unsigned int n = 0;
226+
while ( !stream->eof ) {
227+
char c = stream->Peek();
228+
if ( (c == ' ' || c == '\r' || c == '\n' || c == '\t' ) ) {
229+
stream->Inc();
230+
++n;
231+
}
232+
else break;
233+
}
234+
return n;
235+
}
236+
237+
unsigned int Tokens::skipComment() {
238+
char c = stream->Peek();
239+
if (c != '/') return 0;
240+
stream->Inc();
241+
c = stream->Peek();
242+
if (c != '*') {
243+
stream->Seek(stream->Tell() - 1);
244+
return 0;
245+
}
246+
unsigned int n = 2;
247+
char p = 0;
248+
while ( !stream->eof ) {
249+
c = stream->Peek();
250+
stream->Inc();
251+
++ n;
252+
if (c == '/' && p == '*') break;
253+
p = c;
254+
}
255+
return n;
256+
}
257+
224258
//
225259
// Returns the offset of the current Token and moves cursor to next
226260
//
227261
Token Tokens::Next() {
228262

229263
if ( stream->eof ) return TokenPtr();
230264

231-
char c;
232-
233-
// Trim whitespace
234-
while ( !stream->eof ) {
235-
c = stream->Peek();
236-
if ( (c == ' ' || c == '\r' || c == '\n' || c == '\t' ) ) stream->Inc();
237-
else break;
238-
}
239-
265+
while (skipWhitespace() || skipComment()) {}
266+
240267
if ( stream->eof ) return TokenPtr();
241268
unsigned int pos = stream->Tell();
242269

243-
bool inString = false;
244-
bool inComment = false;
245-
270+
char c = stream->Peek();
271+
246272
// If the cursor is at [()=,;$*] we know token consists of single char
247273
if ( c == '(' || c == ')' || c == '=' || c == ',' || c == ';' || c == '$' || c == '*' ) {
248274
stream->Inc();
@@ -256,18 +282,12 @@ Token Tokens::Next() {
256282

257283
// Read character and increment pointer if not starting a new token
258284
char c = stream->Peek();
259-
if ( len && (!inString || inComment) && (c == '(' || c == ')' || c == '=' || c == ',' || c == ';' ) ) break;
285+
if ( len && (c == '(' || c == ')' || c == '=' || c == ',' || c == ';' ) ) break;
260286
stream->Inc();
261-
262-
// Skip whitespace if not in comment or string
263-
if ( !inComment && !inString && (c == ' ' || c == '\r' || c == '\n' || c == '\t' ) ) continue;
264-
265287
len ++;
266288

267-
// Keep track of whether cursor is inside a string or comment
268-
if ( inComment && p == '*' && c == '/' ) inComment = false;
269-
else if ( !inString && !inComment && p == '/' && c == '*' ) inComment = true;
270-
else if ( !inComment && c == '\'' ) decoder->dryRun();
289+
// If a string is encountered defer processing to the IfcCharacterDecoder
290+
if ( c == '\'' ) decoder->dryRun();
271291

272292
p = c;
273293
}
@@ -283,20 +303,16 @@ std::string Tokens::TokenString(unsigned int offset) {
283303
const bool was_eof = stream->eof;
284304
unsigned int old_offset = stream->Tell();
285305
stream->Seek(offset);
286-
bool inString = false;
287-
bool inComment = false;
288306
std::string buffer;
289307
buffer.reserve(128);
290308
char p = 0;
291309
while ( ! stream->eof ) {
292310
char c = stream->Peek();
293-
if ( buffer.size() && (!inString || inComment) && (c == '(' || c == ')' || c == '=' || c == ',' || c == ';' ) ) break;
311+
if ( buffer.size() && (c == '(' || c == ')' || c == '=' || c == ',' || c == ';' ) ) break;
294312
stream->Inc();
295-
if ( !inComment && !inString && (c == ' ' || c == '\r' || c == '\n' || c == '\t' ) ) continue;
296-
if ( !inComment ) buffer.push_back(c);
297-
if ( inComment && p == '*' && c == '/' ) inComment = false;
298-
else if ( !inString && !inComment && p == '/' && c == '*' ) inComment = true;
299-
else if ( !inComment && c == '\'' ) return *decoder;
313+
if ( c == ' ' || c == '\r' || c == '\n' || c == '\t' ) continue;
314+
else if ( c == '\'' ) return *decoder;
315+
else buffer.push_back(c);
300316
p = c;
301317
}
302318
if ( was_eof ) stream->eof = true;

src/ifcparse/IfcParse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ namespace IfcParse {
103103
class Tokens {
104104
private:
105105
IfcCharacterDecoder* decoder;
106+
unsigned int skipWhitespace();
107+
unsigned int skipComment();
106108
public:
107109
IfcSpfStream* stream;
108110
IfcFile* file;

0 commit comments

Comments
 (0)