aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Sandulenko2019-11-26 01:10:44 +0100
committerEugene Sandulenko2019-11-26 01:12:53 +0100
commitdf41cb125fe5f66919030ebc60304df01356cded (patch)
tree0279a0add7a43973bf5d4045bf1d85cf1a1cccd1
parentff2367d3dab4b4058b6555135eb4bb9ac23264db (diff)
downloadscummvm-rg350-df41cb125fe5f66919030ebc60304df01356cded.tar.gz
scummvm-rg350-df41cb125fe5f66919030ebc60304df01356cded.tar.bz2
scummvm-rg350-df41cb125fe5f66919030ebc60304df01356cded.zip
DIRECTOR: LINGO: Strip comments and trailing whitespaces from the scripts
-rw-r--r--engines/director/lingo/lingo.cpp51
-rw-r--r--engines/director/lingo/lingo.h1
2 files changed, 52 insertions, 0 deletions
diff --git a/engines/director/lingo/lingo.cpp b/engines/director/lingo/lingo.cpp
index 761cf74137..161fd275dc 100644
--- a/engines/director/lingo/lingo.cpp
+++ b/engines/director/lingo/lingo.cpp
@@ -151,6 +151,10 @@ void Lingo::addCode(const char *code, ScriptType type, uint16 id) {
return;
}
+ // Strip comments for ease of the parser
+ Common::String codeNorm = stripComments(code);
+ code = codeNorm.c_str();
+
// macros and factories have conflicting grammar. Thus we ease life for the parser.
if ((begin = findNextDefinition(code))) {
bool first = true;
@@ -211,6 +215,53 @@ void Lingo::addCode(const char *code, ScriptType type, uint16 id) {
}
}
+Common::String Lingo::stripComments(const char *s) {
+ Common::String noComments;
+
+ // Strip comments
+ while (*s) {
+ if (*s == '-' && *(s + 1) == '-') { // At the end of the line we will have \0
+ while (*s && *s != '\n')
+ s++;
+ }
+
+ if (*s)
+ noComments += *s;
+
+ s++;
+ }
+
+ // Strip trailing whitespaces
+ Common::String res;
+ s = noComments.c_str();
+ while (*s) {
+ if (*s == ' ' || *s == '\t') { // If we see a whitespace
+ const char *ps = s; // Remember where we saw it
+
+ while (*ps == ' ' || *ps == '\t') // Scan until end of whitespaces
+ ps++;
+
+ if (*ps) { // Not end of the string
+ if (*ps == '\n') { // If it is newline, then we continue from it
+ s = ps;
+ } else { // It is not a newline
+ while (s != ps) { // Add all whitespaces
+ res += *s;
+ s++;
+ }
+ }
+ }
+ }
+
+ if (*s)
+ res += *s;
+
+ s++;
+ }
+
+ return res;
+}
+
void Lingo::executeScript(ScriptType type, uint16 id, uint16 function) {
if (!_scriptContexts[type].contains(id)) {
debugC(3, kDebugLingoExec, "Request to execute non-existant script type %d id %d", type, id);
diff --git a/engines/director/lingo/lingo.h b/engines/director/lingo/lingo.h
index 7556e90bc2..b2acdd82f0 100644
--- a/engines/director/lingo/lingo.h
+++ b/engines/director/lingo/lingo.h
@@ -189,6 +189,7 @@ public:
void runTests();
private:
+ Common::String stripComments(const char *s);
const char *findNextDefinition(const char *s);
// lingo-events.cpp