2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2007-01-14 21:29:12 +00:00
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-02-18 01:34:23 +00:00
|
|
|
*
|
2007-01-14 21:29:12 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 01:34:23 +00:00
|
|
|
*
|
2007-01-14 21:29:12 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-01-14 21:29:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
|
|
|
|
2007-02-19 22:34:00 +00:00
|
|
|
#include "parallaction/parallaction.h"
|
2009-03-23 17:42:08 +00:00
|
|
|
#include "parallaction/parser.h"
|
2007-05-13 14:38:05 +00:00
|
|
|
|
2007-01-14 21:29:12 +00:00
|
|
|
|
|
|
|
namespace Parallaction {
|
|
|
|
|
2008-08-02 03:19:45 +00:00
|
|
|
#define MAX_TOKENS 50
|
|
|
|
|
2008-08-02 02:24:36 +00:00
|
|
|
int _numTokens;
|
2008-08-02 03:19:45 +00:00
|
|
|
char _tokens[MAX_TOKENS][MAX_TOKEN_LEN];
|
2007-01-14 21:29:12 +00:00
|
|
|
|
2008-06-03 18:24:54 +00:00
|
|
|
Script::Script(Common::ReadStream *input, bool disposeSource) : _input(input), _disposeSource(disposeSource), _line(0) {}
|
2007-01-14 21:29:12 +00:00
|
|
|
|
2007-03-04 15:00:50 +00:00
|
|
|
Script::~Script() {
|
|
|
|
if (_disposeSource)
|
2007-04-03 22:03:21 +00:00
|
|
|
delete _input;
|
2007-01-14 21:29:12 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 09:33:37 +00:00
|
|
|
/*
|
|
|
|
* readLineIntern read a text line and prepares it for
|
|
|
|
* parsing, by stripping the leading whitespace and
|
2010-05-21 11:05:18 +00:00
|
|
|
* changing tabs to spaces. It will stop on a CR, LF, or
|
|
|
|
* SUB (0x1A), which may all occur at the end of a script
|
|
|
|
* line.
|
|
|
|
* Returns an empty string (length = 0) when a line
|
2008-12-04 09:33:37 +00:00
|
|
|
* has no printable text in it.
|
|
|
|
*/
|
2008-11-13 15:15:54 +00:00
|
|
|
char *Script::readLineIntern(char *buf, size_t bufSize) {
|
2008-12-04 09:33:37 +00:00
|
|
|
uint i = 0;
|
|
|
|
for ( ; i < bufSize; ) {
|
2008-11-09 09:39:36 +00:00
|
|
|
char c = _input->readSByte();
|
|
|
|
if (_input->eos())
|
|
|
|
break;
|
2010-05-21 11:05:18 +00:00
|
|
|
// break if EOL
|
|
|
|
if (c == '\n' || c == '\r' || c == (char)0x1A)
|
2008-11-09 09:39:36 +00:00
|
|
|
break;
|
2008-11-13 15:15:54 +00:00
|
|
|
if (c == '\t')
|
|
|
|
c = ' ';
|
2008-11-09 09:39:36 +00:00
|
|
|
|
2008-12-04 09:33:37 +00:00
|
|
|
if ((c != ' ') || (i > 0)) {
|
|
|
|
buf[i] = c;
|
|
|
|
i++;
|
|
|
|
}
|
2007-02-19 22:34:00 +00:00
|
|
|
}
|
2007-10-14 21:32:43 +00:00
|
|
|
_line++;
|
2008-11-13 15:15:54 +00:00
|
|
|
if (i == bufSize) {
|
|
|
|
warning("overflow in readLineIntern (line %i)", _line);
|
|
|
|
}
|
|
|
|
if (i == 0 && _input->eos()) {
|
2021-11-13 21:40:35 +00:00
|
|
|
return nullptr;
|
2008-11-13 15:15:54 +00:00
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
2007-02-19 22:34:00 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
bool isCommentLine(char *text) {
|
|
|
|
return text[0] == '#';
|
|
|
|
}
|
2007-02-19 22:34:00 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
bool isStartOfCommentBlock(char *text) {
|
|
|
|
return (text[0] == '[');
|
|
|
|
}
|
2007-02-19 22:34:00 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
bool isEndOfCommentBlock(char *text) {
|
|
|
|
return (text[0] == ']');
|
2007-02-19 22:34:00 +00:00
|
|
|
}
|
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
char *Script::readLine(char *buf, size_t bufSize) {
|
|
|
|
bool inBlockComment = false;
|
|
|
|
bool ignoreLine = true;
|
2007-02-19 22:34:00 +00:00
|
|
|
|
2021-11-13 21:40:35 +00:00
|
|
|
char *line = nullptr;
|
2008-11-13 15:15:54 +00:00
|
|
|
do {
|
|
|
|
line = readLineIntern(buf, bufSize);
|
2021-11-13 21:40:35 +00:00
|
|
|
if (line == nullptr) {
|
|
|
|
return nullptr;
|
2008-11-13 15:15:54 +00:00
|
|
|
}
|
2007-02-19 22:34:00 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
if (line[0] == '\0')
|
|
|
|
continue;
|
2007-02-19 14:11:56 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
ignoreLine = false;
|
2007-02-19 14:11:56 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
line = Common::ltrim(line);
|
|
|
|
if (isCommentLine(line)) {
|
|
|
|
// ignore this line
|
|
|
|
ignoreLine = true;
|
|
|
|
} else
|
|
|
|
if (isStartOfCommentBlock(line)) {
|
|
|
|
// mark this and the following lines as comment
|
|
|
|
inBlockComment = true;
|
|
|
|
} else
|
|
|
|
if (isEndOfCommentBlock(line)) {
|
|
|
|
// comment is finished, so stop ignoring
|
|
|
|
inBlockComment = false;
|
|
|
|
// the current line must be skipped, though,
|
|
|
|
// as it contains the end-of-comment marker
|
|
|
|
ignoreLine = true;
|
|
|
|
}
|
2008-08-02 02:24:36 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
} while (inBlockComment || ignoreLine);
|
2007-02-19 14:11:56 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Script::clearTokens() {
|
|
|
|
memset(_tokens, 0, sizeof(_tokens));
|
|
|
|
_numTokens = 0;
|
2007-02-19 14:11:56 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 21:32:43 +00:00
|
|
|
void Script::skip(const char* endToken) {
|
2007-08-24 20:14:51 +00:00
|
|
|
|
|
|
|
while (scumm_stricmp(_tokens[0], endToken)) {
|
2007-10-14 21:32:43 +00:00
|
|
|
readLineToken(true);
|
2007-08-24 20:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-20 09:52:07 +00:00
|
|
|
//
|
2007-07-08 12:39:39 +00:00
|
|
|
// Scans 's' until one of the stop-chars in 'brk' is found, building a token.
|
|
|
|
// If the routine encounters quotes, it will extract the contained text and
|
|
|
|
// make a proper token. When scanning inside quotes, 'brk' is ignored and
|
|
|
|
// only newlines are considered stop-chars.
|
|
|
|
//
|
|
|
|
// The routine returns the unparsed portion of the input string 's'.
|
|
|
|
//
|
2008-11-14 12:13:59 +00:00
|
|
|
char *Script::parseNextToken(char *s, char *tok, uint16 count, const char *brk) {
|
2007-02-20 09:52:07 +00:00
|
|
|
|
2007-07-08 09:22:08 +00:00
|
|
|
enum STATES { NORMAL, QUOTED };
|
|
|
|
|
|
|
|
STATES state = NORMAL;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case NORMAL:
|
|
|
|
if (*s == '\0') {
|
|
|
|
*tok = '\0';
|
|
|
|
return s;
|
2008-11-13 15:15:54 +00:00
|
|
|
} else
|
2007-07-08 09:22:08 +00:00
|
|
|
if (strchr(brk, *s)) {
|
|
|
|
*tok = '\0';
|
|
|
|
return ++s;
|
2008-11-13 15:15:54 +00:00
|
|
|
} else
|
2007-07-08 09:22:08 +00:00
|
|
|
if (*s == '"') {
|
2008-11-13 15:15:54 +00:00
|
|
|
state = QUOTED;
|
|
|
|
s++;
|
2007-07-08 09:22:08 +00:00
|
|
|
} else {
|
|
|
|
*tok++ = *s++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QUOTED:
|
|
|
|
if (*s == '\0') {
|
|
|
|
*tok = '\0';
|
|
|
|
return s;
|
2008-11-13 15:15:54 +00:00
|
|
|
} else
|
|
|
|
if (*s == '"') {
|
2007-07-08 09:22:08 +00:00
|
|
|
*tok = '\0';
|
|
|
|
return ++s;
|
2008-11-13 15:15:54 +00:00
|
|
|
} else {
|
|
|
|
*tok++ = *s++;
|
|
|
|
count--;
|
2007-07-08 09:22:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-10-20 20:15:43 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2007-07-08 09:22:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 09:52:07 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 12:39:39 +00:00
|
|
|
*tok = '\0';
|
2007-12-16 16:03:04 +00:00
|
|
|
// TODO: if execution flows here, make *REALLY* sure everything has been parsed
|
|
|
|
// out of the input string. This is what is supposed to happen, but never ever
|
|
|
|
// allocated time to properly check.
|
2007-07-08 12:39:39 +00:00
|
|
|
|
|
|
|
return tok;
|
2007-07-08 09:22:08 +00:00
|
|
|
|
2007-02-20 09:52:07 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 21:32:43 +00:00
|
|
|
uint16 Script::readLineToken(bool errorOnEOF) {
|
2007-02-19 14:09:13 +00:00
|
|
|
char buf[200];
|
2008-11-13 15:15:54 +00:00
|
|
|
char *line = readLine(buf, 200);
|
|
|
|
if (!line) {
|
|
|
|
if (errorOnEOF)
|
|
|
|
error("unexpected end of file while parsing");
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2007-02-19 14:09:13 +00:00
|
|
|
|
2008-11-13 15:15:54 +00:00
|
|
|
clearTokens();
|
2008-11-30 10:11:38 +00:00
|
|
|
while (*line && _numTokens < MAX_TOKENS) {
|
2008-11-13 15:15:54 +00:00
|
|
|
line = parseNextToken(line, _tokens[_numTokens], MAX_TOKEN_LEN, " ");
|
|
|
|
line = Common::ltrim(line);
|
|
|
|
_numTokens++;
|
|
|
|
}
|
|
|
|
return _numTokens;
|
2007-02-19 14:09:13 +00:00
|
|
|
}
|
2007-02-19 12:53:59 +00:00
|
|
|
|
2008-05-04 15:09:23 +00:00
|
|
|
|
|
|
|
void Parser::reset() {
|
2021-11-13 21:40:35 +00:00
|
|
|
_currentOpcodes = nullptr;
|
|
|
|
_currentStatements = nullptr;
|
2016-05-28 13:21:47 +00:00
|
|
|
_lookup = 0;
|
2008-05-11 08:55:16 +00:00
|
|
|
|
|
|
|
_statements.clear();
|
|
|
|
_opcodes.clear();
|
2008-05-04 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::pushTables(OpcodeSet *opcodes, Table *statements) {
|
|
|
|
_opcodes.push(_currentOpcodes);
|
|
|
|
_statements.push(_currentStatements);
|
|
|
|
|
|
|
|
_currentOpcodes = opcodes;
|
|
|
|
_currentStatements = statements;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::popTables() {
|
|
|
|
assert(_opcodes.size() > 0);
|
|
|
|
|
|
|
|
_currentOpcodes = _opcodes.pop();
|
|
|
|
_currentStatements = _statements.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::parseStatement() {
|
2021-11-13 21:40:35 +00:00
|
|
|
assert(_currentOpcodes != nullptr);
|
2008-05-04 15:09:23 +00:00
|
|
|
|
|
|
|
_lookup = _currentStatements->lookup(_tokens[0]);
|
|
|
|
|
|
|
|
debugC(9, kDebugParser, "parseStatement: %s (lookup = %i)", _tokens[0], _lookup);
|
|
|
|
|
|
|
|
(*(*_currentOpcodes)[_lookup])();
|
|
|
|
}
|
|
|
|
|
2010-11-19 17:03:07 +00:00
|
|
|
} // End of namespace Parallaction
|