From fd4086aa8d7b22eed9c9e6535007f8c66e9bf0e7 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Tue, 19 Jul 2016 16:41:36 +0200 Subject: [PATCH] Report errors in string escape sequences more verbosely --- src/ifcparse/IfcCharacterDecoder.cpp | 4 ++-- src/ifcparse/IfcException.h | 10 ++++++++++ src/ifcparse/IfcParse.cpp | 11 +++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ifcparse/IfcCharacterDecoder.cpp b/src/ifcparse/IfcCharacterDecoder.cpp index 1a03570cbd..7061ad80ca 100644 --- a/src/ifcparse/IfcCharacterDecoder.cpp +++ b/src/ifcparse/IfcCharacterDecoder.cpp @@ -218,7 +218,7 @@ IfcCharacterDecoder::operator std::string() { (current_char == '\'' && parse_state == APOSTROPHE) ) ) { if ( parse_state == APOSTROPHE && current_char != '\'' ) break; - throw IfcException("Invalid character encountered"); + throw IfcInvalidTokenException(file->Tell(), current_char); } else { parse_state = hex = hex_count = 0; // NOTE: this is in fact wrong, this ought to be the representation of the character. @@ -286,7 +286,7 @@ void IfcCharacterDecoder::dryRun() { (current_char == '\'' && parse_state == APOSTROPHE) ) ) { if ( parse_state == APOSTROPHE && current_char != '\'' ) break; - throw IfcException("Invalid character encountered"); + throw IfcInvalidTokenException(file->Tell(), current_char); } else { parse_state = hex_count = 0; } diff --git a/src/ifcparse/IfcException.h b/src/ifcparse/IfcException.h index 0aae1d7e2a..f458d5556b 100644 --- a/src/ifcparse/IfcException.h +++ b/src/ifcparse/IfcException.h @@ -67,8 +67,18 @@ namespace IfcParse { " invalid " + expected_type ) {} + IfcInvalidTokenException( + int token_start, + char c + ) + : IfcException( + std::string("Unexpected '") + std::string(1, c) + "' at " + + boost::lexical_cast(token_start) + ) + {} ~IfcInvalidTokenException() throw () {} }; + } #ifdef _MSC_VER diff --git a/src/ifcparse/IfcParse.cpp b/src/ifcparse/IfcParse.cpp index 8e88709353..3e3c7377b7 100644 --- a/src/ifcparse/IfcParse.cpp +++ b/src/ifcparse/IfcParse.cpp @@ -1098,8 +1098,15 @@ bool IfcFile::Init(IfcParse::IfcSpfStream* s) { MaxId = (std::max)(MaxId,currentId); currentId = 0; } else { - try { token = tokens->Next(); } - catch (... ) { token = NoneTokenPtr(); } + try { + token = tokens->Next(); + } catch (const IfcException& e) { + Logger::Message(Logger::LOG_ERROR, std::string(e.what()) + ". Parsing terminated"); + token = NoneTokenPtr(); + } catch (...) { + Logger::Message(Logger::LOG_ERROR, "Parsing terminated"); + token = NoneTokenPtr(); + } } if ( ! (token.startPos || token.lexer) ) break;