Fix to_string() on eof marker token (#9463)

* Rewrite to_string() to use switch{} and handle Token_NONE and identifier without as_string()

* Add regression tests for to_string() on tokens without a string form

Cover both halves of the recursion that made a whitespace-only file
segfault: token::to_string() on the EOF marker and on an instance name,
and a parse of input that lexes to zero tokens, which is how the header
parser reaches token::as_string() on the EOF marker.

Generated with the assistance of an AI coding tool.

---------

Co-authored-by: Bruno Postle <bruno@postle.net>
This commit is contained in:
Thomas Krijnen
2026-09-10 11:02:00 +02:00
committed by GitHub
parent e1be433207
commit 148ff02859
2 changed files with 59 additions and 10 deletions
+25 -10
View File
@@ -536,19 +536,34 @@ boost::dynamic_bitset<> token::as_binary() {
}
std::string token::to_string() {
std::string result;
if (type == Token_OPERATOR || type == Token_BOOL) {
result.push_back(value_char);
} else if (type == Token_INT) {
result = std::to_string(value_int);
} else if (type == Token_FLOAT) {
switch (type) {
case Token_OPERATOR:
case Token_BOOL:
return std::string(1, value_char);
case Token_INT:
return std::to_string(value_int);
case Token_IDENTIFIER:
return "#" + std::to_string(value_int);
case Token_FLOAT: {
std::ostringstream oss;
oss << std::setprecision(15) << value_double;
result = oss.str();
} else {
return as_string();
return oss.str();
}
return result;
case Token_STRING:
case Token_ENUMERATION:
case Token_BINARY:
case Token_KEYWORD:
return as_string();
case Token_NONE:
throw invalid_token_exception(start_pos, "", "");
}
throw exception("Unknown token type");
}
std::string ifcopenshell::encode_spf_string(const std::string& value) {