1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
|
#include "scanner.hpp"
#include <cassert>
#include <sstream>
#include "errorhandler.hpp"
#include "exception.hpp"
#include "extensions.hpp"
#include "parser.hpp"
#include <components/misc/strings/conversion.hpp>
#include <components/misc/strings/lower.hpp>
namespace
{
bool startsComparisonOperator(const Compiler::MultiChar& c)
{
return c == '=' || c == '<' || c == '>' || c == '!';
}
bool validComparisonOperatorCharacter(const Compiler::MultiChar& c)
{
return startsComparisonOperator(c) || c == ' ' || c == '\t';
}
}
namespace Compiler
{
bool Scanner::get(MultiChar& c)
{
if (!c.getFrom(mStream))
return false;
mPrevLoc = mLoc;
if (c == '\n')
{
mStrictKeywords = false;
mTolerantNames = false;
mExpectName = false;
mLoc.mColumn = 0;
++mLoc.mLine;
mLoc.mLiteral.clear();
mIgnoreSpecial = true;
}
else
{
++mLoc.mColumn;
c.appendTo(mLoc.mLiteral);
}
return true;
}
void Scanner::putback(MultiChar& c)
{
c.putback(mStream);
mLoc = mPrevLoc;
}
bool Scanner::scanToken(Parser& parser)
{
switch (mPutback)
{
case Putback_Special:
{
mPutback = Putback_None;
// Replicate behaviour from scanSpecial so putting something back doesn't change the way it's handled
if (mExpectName && (mPutbackCode == S_member || mPutbackCode == S_minus))
{
mExpectName = false;
bool cont = false;
bool tolerant = mTolerantNames;
mTolerantNames = true;
MultiChar c{ mPutbackCode == S_member ? '.' : '-' };
scanName(c, parser, cont);
mTolerantNames = tolerant;
return cont;
}
return parser.parseSpecial(mPutbackCode, mPutbackLoc, *this);
}
case Putback_Integer:
mPutback = Putback_None;
return parser.parseInt(mPutbackInteger, mPutbackLoc, *this);
case Putback_Float:
mPutback = Putback_None;
return parser.parseFloat(mPutbackFloat, mPutbackLoc, *this);
case Putback_Name:
mPutback = Putback_None;
return parser.parseName(mPutbackName, mPutbackLoc, *this);
case Putback_Keyword:
mPutback = Putback_None;
return parser.parseKeyword(mPutbackCode, mPutbackLoc, *this);
case Putback_None:
break;
}
MultiChar c;
if (!get(c))
{
parser.parseEOF(*this);
return false;
}
else if (c == ';')
{
std::string comment;
c.appendTo(comment);
while (get(c))
{
if (c == '\n')
{
putback(c);
break;
}
else
c.appendTo(comment);
}
TokenLoc loc(mLoc);
mLoc.mLiteral.clear();
return parser.parseComment(comment, loc, *this);
}
else if (c.isWhitespace())
{
mLoc.mLiteral.clear();
return true;
}
else if (c == ':')
{
// treat : as a whitespace :(
mLoc.mLiteral.clear();
return true;
}
else if (c.isAlpha() || c == '_' || c == '"')
{
mIgnoreSpecial = false;
bool cont = false;
if (scanName(c, parser, cont))
{
mLoc.mLiteral.clear();
return cont;
}
}
else if (c.isDigit())
{
mIgnoreSpecial = false;
bool cont = false;
bool scanned = mExpectName ? scanName(c, parser, cont) : scanInt(c, parser, cont);
if (scanned)
{
mLoc.mLiteral.clear();
return cont;
}
}
else if (c == 13) // linux compatibility hack
{
return true;
}
else
{
bool cont = false;
if (scanSpecial(c, parser, cont))
{
mLoc.mLiteral.clear();
return cont;
}
}
TokenLoc loc(mLoc);
mLoc.mLiteral.clear();
mErrorHandler.error("Syntax error", loc);
throw SourceException();
}
bool Scanner::scanInt(MultiChar& c, Parser& parser, bool& cont)
{
assert(c != '\0');
std::string value;
c.appendTo(value);
while (get(c))
{
if (c.isDigit())
{
c.appendTo(value);
}
else if (!c.isMinusSign() && isStringCharacter(c))
{
/// workaround that allows names to begin with digits
return scanName(c, parser, cont, value);
}
else if (c == '.')
{
return scanFloat(value, parser, cont);
}
else
{
putback(c);
break;
}
}
TokenLoc loc(mLoc);
mLoc.mLiteral.clear();
cont = parser.parseInt(Misc::StringUtils::toNumeric<int>(value, 0), loc, *this);
return true;
}
bool Scanner::scanFloat(const std::string& intValue, Parser& parser, bool& cont)
{
std::string value = intValue + ".";
MultiChar c;
bool empty = intValue.empty() || intValue == "-";
bool error = false;
while (get(c))
{
if (c.isDigit())
{
c.appendTo(value);
empty = false;
}
else if (c.isAlpha() || c == '_')
error = true;
else
{
putback(c);
break;
}
}
if (empty || error)
return false;
TokenLoc loc(mLoc);
mLoc.mLiteral.clear();
cont = parser.parseFloat(Misc::StringUtils::toNumeric<float>(value, 0.0f), loc, *this);
return true;
}
static const char* sKeywords[] = {
"begin",
"end",
"short",
"long",
"float",
"if",
"endif",
"else",
"elseif",
"while",
"endwhile",
"return",
"messagebox",
"set",
"to",
nullptr,
};
bool Scanner::scanName(MultiChar& c, Parser& parser, bool& cont, std::string name)
{
c.appendTo(name);
if (!scanName(name))
return false;
else if (name.empty())
return true;
TokenLoc loc(mLoc);
mLoc.mLiteral.clear();
if (name.size() >= 2 && name[0] == '"' && name[name.size() - 1] == '"')
{
name = name.substr(1, name.size() - 2);
// allow keywords enclosed in ""
/// \todo optionally disable
if (mStrictKeywords)
{
cont = parser.parseName(name, loc, *this);
return true;
}
}
int i = 0;
std::string lowerCase = Misc::StringUtils::lowerCase(name);
bool isKeyword = false;
for (; sKeywords[i]; ++i)
if (lowerCase == sKeywords[i])
{
isKeyword = true;
break;
}
// Russian localization and some mods use a quirk - add newline character directly
// to compiled bytecode via HEX-editor to implement multiline messageboxes.
// Of course, original editor can not compile such script.
// Allow messageboxes to bypass the "incomplete string or name" error.
if (lowerCase == "messagebox")
enableIgnoreNewlines();
else if (isKeyword)
mIgnoreNewline = false;
if (sKeywords[i])
{
cont = parser.parseKeyword(i, loc, *this);
return true;
}
if (mExtensions)
{
if (int keyword = mExtensions->searchKeyword(lowerCase))
{
cont = parser.parseKeyword(keyword, loc, *this);
return true;
}
}
cont = parser.parseName(name, loc, *this);
return true;
}
bool Scanner::scanName(std::string& name)
{
MultiChar c;
bool error = false;
while (get(c))
{
if (!name.empty() && name[0] == '"')
{
if (c == '"')
{
c.appendTo(name);
break;
}
// ignoring escape sequences for now, because they are messing up stupid Windows path names.
// else if (c=='\\')
// {
// if (!get (c))
// {
// error = true;
// mErrorHandler.error ("incomplete escape sequence", mLoc);
// break;
// }
// }
else if (c == '\n')
{
if (mIgnoreNewline)
mErrorHandler.warning("string contains newline character, make sure that it is intended", mLoc);
else
{
bool allWhitespace = true;
for (size_t i = 1; i < name.size(); i++)
{
// ignore comments
if (name[i] == ';')
break;
else if (name[i] != '\t' && name[i] != ' ' && name[i] != '\r')
{
allWhitespace = false;
break;
}
}
if (allWhitespace)
{
name.clear();
mLoc.mLiteral.clear();
mErrorHandler.warning("unterminated empty string", mLoc);
return true;
}
error = true;
mErrorHandler.error("incomplete string or name", mLoc);
break;
}
}
}
else if (!(c == '"' && name.empty()))
{
if (!isStringCharacter(c) && !(mTolerantNames && (c == '.' || c == '-')))
{
putback(c);
break;
}
}
c.appendTo(name);
}
return !error;
}
bool Scanner::scanSpecial(MultiChar& c, Parser& parser, bool& cont)
{
bool expectName = mExpectName;
mExpectName = false;
int special = -1;
if (c == '\n')
special = S_newline;
else if (mIgnoreSpecial)
{
// Ignore junk special characters
TokenLoc loc = mLoc;
while (get(c))
{
if (c.isAlpha() || c == '_' || c == '"' || c.isDigit() || c == ';' || c == '\n')
{
putback(c);
break;
}
c.appendTo(loc.mLiteral);
}
mErrorHandler.warning("Stray special character at start of line", loc);
cont = true;
return true;
}
else if (c == '(' || c == '[') /// \todo option to disable the use of [ as alias for (
special = S_open;
else if (c == ')' || c == ']') /// \todo option to disable the use of ] as alias for )
special = S_close;
else if (c == '.')
{
MultiChar next;
// check, if this starts a float literal
if (get(next))
{
putback(next);
if (next.isDigit())
return scanFloat("", parser, cont);
}
special = S_member;
}
else if (startsComparisonOperator(c))
{
TokenLoc loc = mLoc;
std::string op;
c.appendTo(op);
while (get(c))
{
if (validComparisonOperatorCharacter(c))
c.appendTo(op);
else
{
putback(c);
break;
}
}
size_t end = op.size();
while (end > 0 && (op[end - 1] == '\t' || op[end - 1] == ' '))
--end;
switch (op[0])
{
case '=':
special = S_cmpEQ;
if (end != 2 || op[1] != '=')
mErrorHandler.warning("invalid operator " + op.substr(0, end) + ", treating it as ==", loc);
break;
case '<':
special = S_cmpLT;
if (op[1] == '=')
{
special = S_cmpLE;
if (end != 2)
mErrorHandler.warning("invalid operator " + op.substr(0, end) + ", treating it as <=", loc);
}
else if (end != 1)
mErrorHandler.warning("invalid operator " + op.substr(0, end) + ", treating it as <", loc);
break;
case '>':
special = S_cmpGT;
if (op[1] == '=')
{
special = S_cmpGE;
if (end != 2)
mErrorHandler.warning("invalid operator " + op.substr(0, end) + ", treating it as >=", loc);
}
else if (end != 1)
mErrorHandler.warning("invalid operator " + op.substr(0, end) + ", treating it as >", loc);
break;
case '!':
special = S_cmpNE;
if (end != 2 || op[1] != '=')
mErrorHandler.warning("invalid operator " + op.substr(0, end) + ", treating it as !=", loc);
break;
default:
return false;
}
}
else if (c.isMinusSign())
{
MultiChar next;
if (get(next))
{
if (next == '>')
special = S_ref;
else
{
putback(next);
special = S_minus;
}
}
else
special = S_minus;
}
else if (c == '+')
special = S_plus;
else if (c == '*')
special = S_mult;
else if (c == '/')
special = S_div;
else
return false;
if (special == S_newline)
mLoc.mLiteral = "<newline>";
else if (expectName && (special == S_member || special == S_minus))
{
bool tolerant = mTolerantNames;
mTolerantNames = true;
bool out = scanName(c, parser, cont);
mTolerantNames = tolerant;
return out;
}
TokenLoc loc(mLoc);
mLoc.mLiteral.clear();
cont = parser.parseSpecial(special, loc, *this);
return true;
}
bool Scanner::isStringCharacter(MultiChar& c, bool lookAhead)
{
if (lookAhead && c.isMinusSign())
{
/// \todo disable this when doing more stricter compiling. Also, find out who is
/// responsible for allowing it in the first place and meet up with that person in
/// a dark alley.
MultiChar next;
if (next.peek(mStream) && isStringCharacter(next, false))
return true;
}
return c.isAlpha() || c.isDigit() || c == '_' ||
/// \todo disable this when doing more stricter compiling
c == '`' || c == '\'';
}
// constructor
Scanner::Scanner(ErrorHandler& errorHandler, std::istream& inputStream, const Extensions* extensions)
: mErrorHandler(errorHandler)
, mStream(inputStream)
, mExtensions(extensions)
, mPutback(Putback_None)
, mPutbackCode(0)
, mPutbackInteger(0)
, mPutbackFloat(0)
, mStrictKeywords(false)
, mTolerantNames(false)
, mIgnoreNewline(false)
, mExpectName(false)
, mIgnoreSpecial(true)
{
}
void Scanner::scan(Parser& parser)
{
while (scanToken(parser))
;
mExpectName = false;
}
void Scanner::putbackSpecial(int code, const TokenLoc& loc)
{
mPutback = Putback_Special;
mPutbackCode = code;
mPutbackLoc = loc;
}
void Scanner::putbackInt(int value, const TokenLoc& loc)
{
mPutback = Putback_Integer;
mPutbackInteger = value;
mPutbackLoc = loc;
}
void Scanner::putbackFloat(float value, const TokenLoc& loc)
{
mPutback = Putback_Float;
mPutbackFloat = value;
mPutbackLoc = loc;
}
void Scanner::putbackName(const std::string& name, const TokenLoc& loc)
{
mPutback = Putback_Name;
mPutbackName = name;
mPutbackLoc = loc;
}
void Scanner::putbackKeyword(int keyword, const TokenLoc& loc)
{
mPutback = Putback_Keyword;
mPutbackCode = keyword;
mPutbackLoc = loc;
}
void Scanner::listKeywords(std::vector<std::string>& keywords)
{
for (int i = 0; Compiler::sKeywords[i]; ++i)
keywords.emplace_back(Compiler::sKeywords[i]);
if (mExtensions)
mExtensions->listKeywords(keywords);
}
void Scanner::enableIgnoreNewlines()
{
mIgnoreNewline = true;
}
void Scanner::enableStrictKeywords()
{
mStrictKeywords = true;
}
void Scanner::enableTolerantNames()
{
mTolerantNames = true;
}
void Scanner::enableExpectName()
{
mExpectName = true;
}
}
|