From 31c7fc8565dd8297663dd409ef63b9292df25c10 Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Sun, 6 Jun 2021 12:44:03 +0100 Subject: [PATCH] src/Scripting/NasalSys.cxx: show source line if we get error while parsing Nasal. Helps track down errors in Nalsa embedded inside XML - line numbers are from start of Nasal, not start of XML file. --- src/Scripting/NasalSys.cxx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 850a05e19..d384caed4 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -1580,6 +1580,28 @@ naRef FGNasalSys::parse(naContext ctx, const char* filename, " in "<< filename <<", line " << errLine; errors = errorMessageStream.str(); SG_LOG(SG_NASAL, SG_ALERT, errors); + + // Show the line, in case isn't a real file, e.g. nasal code + // is in an .xml file. + const char* line_begin = buf; + const char* line_end = nullptr; + int line_num = 1; + for(;;) { + line_end = strchr(line_begin, '\n'); + if (!line_end) { + line_end = line_begin + strlen(line_begin); + break; + } + if (line_num == errLine) break; + line_begin = line_end + 1; + line_num += 1; + } + if (line_num == errLine) { + SG_LOG(SG_NASAL, SG_ALERT, std::string(line_begin, line_end) << "\n"); + } + else { + SG_LOG(SG_NASAL, SG_ALERT, "[Could not find line " << errLine << " - only " << line_num << " lines."); + } return naNil(); }