lexer.cpp 45.8 KB
Newer Older
victor's avatar
 
victor committed
1 2 3
#include "lexer.h"
#include "lexer_p.h"
#include "interfaces/lexemtype.h"
victor's avatar
 
victor committed
4 5
#include "errormessages/errormessages.h"

victor's avatar
 
victor committed
6 7 8 9 10 11 12 13 14 15 16 17

using namespace Shared;

namespace KumirAnalizer {

Lexer::Lexer(QObject *parent) :
    QObject(parent)
{
    d = new LexerPrivate;
    d->q = this;
}

18 19 20 21 22
Lexer::~Lexer()
{
    delete d;
}

victor's avatar
 
victor committed
23
int Lexer::splitIntoStatements(const QStringList &lines
victor's avatar
 
victor committed
24
                                , int baseLineNo
victor's avatar
 
victor committed
25
                                , QList<Statement*> &statements
26
                                , const QStringList & extraTypeNames
victor's avatar
 
victor committed
27 28 29 30 31
                                ) const
{
    int errorsCount = 0;
    for (int i=0; i<lines.size(); i++) {
        const QString line = lines[i];
victor's avatar
 
victor committed
32
        QList<Lexem*> lexems;
33
        d->splitLineIntoLexems(line, lexems, extraTypeNames);
victor's avatar
 
victor committed
34
        QList<Statement*> sts;
victor's avatar
 
victor committed
35
        d->groupLexemsByStatements(lexems, sts);        
victor's avatar
 
victor committed
36
        for (int j=0; j<sts.size(); j++) {
victor's avatar
 
victor committed
37 38 39
            for (int k=0; k<sts[j]->data.size(); k++) {
                sts[j]->data[k]->lineNo = baseLineNo==-1? -1 : baseLineNo + i;
            }
victor's avatar
 
victor committed
40 41 42 43 44 45
        }
        statements << sts;
    }
    return errorsCount;
}

46
void Lexer::splitIntoLexems(const QString &text, QList<Lexem *> &lexems, const QStringList & extraTypeNames)
victor's avatar
 
victor committed
47
{
48
    d->splitLineIntoLexems(text, lexems, extraTypeNames);
victor's avatar
 
victor committed
49 50
}

victor's avatar
 
victor committed
51
QStringList allVariants(const QString & value) {
victor's avatar
 
victor committed
52
    QStringList variants = value.split("|");
victor's avatar
 
victor committed
53
    QStringList result;
victor's avatar
 
victor committed
54 55 56 57 58
    foreach (QString variant, variants) {
        variant.remove("\\s+");
        variant.remove("\\s*");
        variant.remove("\\s");
        variant.remove("_");
victor's avatar
 
victor committed
59 60 61 62 63 64 65 66 67 68
        result << variant;
    }
    return result;
}

void addToMap(QHash<QString,LexemType> & map,
              const QString &value, const LexemType type)
{
    QStringList variants = allVariants(value);
    foreach (QString variant, variants) {
victor's avatar
 
victor committed
69 70 71 72
        map[variant] = type;
    }
}

victor's avatar
 
victor committed
73 74 75
QString Lexer::classNameByBaseType(const AST::VariableBaseType &type) const
{
    QString result;
victor's avatar
 
victor committed
76 77 78
    for (int i=0; i<d->baseTypes0.keys().size(); i++) {
        if (d->baseTypes0[d->baseTypes0.keys()[i]]==type) {
            result = d->baseTypes0.keys()[i];
victor's avatar
 
victor committed
79 80 81 82 83 84
            break;
        }
    }
    return result;
}

victor's avatar
 
victor committed
85 86 87
void Lexer::setLanguage(const QLocale::Language &language)
{
    const QString langName = QLocale::languageToString(language);
victor's avatar
 
victor committed
88 89
    const QString resourcesRoot = qApp->property("sharePath").toString()+"/kumiranalizer/";
    const QString fileName = resourcesRoot+langName.toLower()+".keywords";
victor's avatar
 
victor committed
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
    LexerPrivate::initNormalizator(fileName);
}

void LexerPrivate::initNormalizator(const QString &fileName)
{
    QFile kwdFile(fileName);
    if (kwdFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
        QTextStream ts(&kwdFile);
        ts.setCodec("UTF-8");
        const QStringList lines = ts.readAll().split("\n",QString::SkipEmptyParts);
        kwdFile.close();
        foreach (const QString &line, lines) {
            if (line.startsWith("#"))
                continue;
            const QStringList pair = line.split("=");
            if (pair.size()>=2) {
                const QString context = pair.at(0).simplified();
                const QString value = pair.at(1).simplified();
                if (context=="begin module") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriModule);
                }
                else if (context=="end module") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriEndModule);
                }
victor's avatar
 
victor committed
116 117 118 119 120 121 122 123
                else if (context=="pause running") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriPause);
                }
                else if (context=="terminate running") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriHalt);
                }
victor's avatar
 
victor committed
124 125 126 127 128 129 130 131 132
                else if (context=="include text") {
//                    keyWords << value;
//                    addToMap(kwdMap, value, KS_INCLUDE);
                }
                else if (context=="use module") {
                    keyWords << value;
//                    useKwd = new QString(value);
                    addToMap(kwdMap, value, LxPriImport);
                }
victor's avatar
 
victor committed
133 134 135
                else if (context=="function return value") {
                    retvalKeyword = value;
                }
victor's avatar
 
victor committed
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
                else if (context=="algorhitm header") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriAlgHeader);
                }
                else if (context=="begin algorhitm implementation") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriAlgBegin);
                }
                else if (context=="end algorhitm implementation") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriAlgEnd);
                }
                else if (context=="algorhitm pre-condition") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriPre);
                }
                else if (context=="algorhitm post-condition") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriPost);
                }
                else if (context=="assertion") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriAssert);
                }
                else if (context=="begin loop") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriLoop);
                }
                else if (context=="end loop") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriEndLoop);
                }
                else if (context=="conditional end loop") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriEndLoop);
                }
                else if (context=="'for' loop type") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecFor);
                }
                else if (context=="'while' loop type") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecWhile);
                }
                else if (context=="'times' loop type") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecTimes);
                }
                else if (context=="for loop 'from'") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecFrom);
                }
                else if (context=="for loop 'to'") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecTo);
                }
                else if (context=="for loop 'step'") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecStep);
                }
                else if (context=="loop break and algorhitm return") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriExit);
                }
                else if (context=="if") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriIf);
                }
                else if (context=="then") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriThen);
                }
                else if (context=="else") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriElse);
                }
                else if (context=="end of 'if' or 'switch' statement") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriFi);
                }
                else if (context=="switch") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriSwitch);
                }
                else if (context=="case") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriCase);
                }
                else if (context=="terminal input") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriInput);
                }
                else if (context=="terminal output") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxPriOutput);
                }
                else if (context=="new line symbol") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecNewline);
                }
Victor Yacovlev's avatar
Victor Yacovlev committed
236 237 238 239
                else if (context=="string length") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecCurrentStringLength);
                }
victor's avatar
 
victor committed
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
                else if (context=="logical 'not'") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecNot);
                }
                else if (context=="logical 'and'") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecAnd);
                }
                else if (context=="logical 'or'") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecOr);
                }
                else if (context=="'write only' parameter modifier") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecOut);
                }
                else if (context=="'read only' parameter modifier") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecIn);
                }
                else if (context=="'read and write' parameter modifier") {
                    keyWords << value;
                    addToMap(kwdMap, value, LxSecInout);
                }
                else if (context=="'true' constant value") {
                    constNames << value;
victor's avatar
 
victor committed
266
                    boolConstantValues.insert(value, true);
victor's avatar
 
victor committed
267 268 269 270
                    addToMap(kwdMap, value, LxConstBoolTrue);
                }
                else if (context=="'false' constant value") {
                    constNames << value;
victor's avatar
 
victor committed
271
                    boolConstantValues.insert(value, false);
victor's avatar
 
victor committed
272 273 274 275
                    addToMap(kwdMap, value, LxConstBoolFalse);
                }
                else if (context=="integer type name") {
                    typeNames << value;
victor's avatar
 
victor committed
276
                    baseTypes.insert(value, AST::TypeInteger);
victor's avatar
 
victor committed
277
                    baseTypes0.insert(value, AST::TypeInteger);
victor's avatar
 
victor committed
278 279 280 281
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="floating point type name") {
                    typeNames << value;
victor's avatar
 
victor committed
282
                    baseTypes.insert(value, AST::TypeReal);
victor's avatar
 
victor committed
283
                    baseTypes0.insert(value, AST::TypeReal);
victor's avatar
 
victor committed
284 285 286 287
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="character type name") {
                    typeNames << value;
victor's avatar
 
victor committed
288
                    baseTypes.insert(value, AST::TypeCharect);
victor's avatar
 
victor committed
289
                    baseTypes0.insert(value, AST::TypeCharect);
victor's avatar
 
victor committed
290 291 292 293
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="string type name") {
                    typeNames << value;
victor's avatar
 
victor committed
294
                    baseTypes.insert(value, AST::TypeString);
victor's avatar
 
victor committed
295
                    baseTypes0.insert(value, AST::TypeString);
victor's avatar
 
victor committed
296 297 298 299
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="boolean type name") {
                    typeNames << value;
victor's avatar
 
victor committed
300
                    baseTypes.insert(value, AST::TypeBoolean);
victor's avatar
 
victor committed
301
                    baseTypes0.insert(value, AST::TypeBoolean);
victor's avatar
 
victor committed
302 303 304
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="integer array type name") {
victor's avatar
 
victor committed
305 306 307 308 309
                    QStringList variants = allVariants(value);
                    foreach (const QString & variant, variants) {
                        baseTypes.insert(variant, AST::TypeInteger);
                        arrayTypes.insert(variant);
                    }
victor's avatar
 
victor committed
310 311 312 313
                    variants = value.split("|");
                    foreach (const QString & variant, variants) {
                        typeNames << variant;
                    }
victor's avatar
 
victor committed
314 315 316
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="floating point array type name") {
victor's avatar
 
victor committed
317 318 319 320 321
                    QStringList variants = allVariants(value);
                    foreach (const QString & variant, variants) {
                        baseTypes.insert(variant, AST::TypeReal);
                        arrayTypes.insert(variant);
                    }
victor's avatar
 
victor committed
322 323 324 325
                    variants = value.split("|");
                    foreach (const QString & variant, variants) {
                        typeNames << variant;
                    }
victor's avatar
 
victor committed
326 327 328
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="character array type name") {
victor's avatar
 
victor committed
329 330 331 332 333
                    QStringList variants = allVariants(value);
                    foreach (const QString & variant, variants) {
                        baseTypes.insert(variant, AST::TypeCharect);
                        arrayTypes.insert(variant);
                    }
victor's avatar
 
victor committed
334 335 336 337
                    variants = value.split("|");
                    foreach (const QString & variant, variants) {
                        typeNames << variant;
                    }
victor's avatar
 
victor committed
338 339 340
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="string array type name") {
victor's avatar
 
victor committed
341 342 343 344 345
                    QStringList variants = allVariants(value);
                    foreach (const QString & variant, variants) {
                        baseTypes.insert(variant, AST::TypeString);
                        arrayTypes.insert(variant);
                    }
victor's avatar
 
victor committed
346 347 348 349
                    variants = value.split("|");
                    foreach (const QString & variant, variants) {
                        typeNames << variant;
                    }
victor's avatar
 
victor committed
350 351 352
                    addToMap(kwdMap, value, LxNameClass);
                }
                else if (context=="boolean array type name") {
victor's avatar
 
victor committed
353 354 355 356 357
                    QStringList variants = allVariants(value);
                    foreach (const QString & variant, variants) {
                        baseTypes.insert(variant, AST::TypeBoolean);
                        arrayTypes.insert(variant);
                    }
victor's avatar
 
victor committed
358 359 360 361
                    variants = value.split("|");
                    foreach (const QString & variant, variants) {
                        typeNames << variant;
                    }
victor's avatar
 
victor committed
362 363 364 365 366 367 368
                    addToMap(kwdMap, value, LxNameClass);
                }

            }
        }
    }

victor's avatar
 
victor committed
369 370 371 372 373 374
    operators << "\\+";
    kwdMap["+"] = LxOperPlus;

    operators << "-";
    kwdMap["-"] = LxOperMinus;

victor's avatar
 
victor committed
375 376 377
    operators << "\\*\\*";
    kwdMap["**"] = LxOperPower;

victor's avatar
 
victor committed
378 379 380 381 382 383 384 385 386 387 388 389
    operators << "\\(";
    kwdMap["("] = LxOperLeftBr;

    operators << "\\)";
    kwdMap[")"] = LxOperRightBr;

    operators << "\\[";
    kwdMap["["] = LxOperLeftSqBr;

    operators << "\\]";
    kwdMap["]"] = LxOperRightSqBr;

390 391 392 393 394 395
    operators << "\\{";
    kwdMap["{"] = LxOperLeftBrace;

    operators << "\\}";
    kwdMap["}"] = LxOperRightBrace;

victor's avatar
 
victor committed
396 397 398 399 400 401 402 403 404
    operators << ",";
    kwdMap[","] = LxOperComa;

    operators << ":";
    kwdMap[":"] = LxOperColon;

    operators << "\\*";
    kwdMap["*"] = LxOperAsterisk;

victor's avatar
 
victor committed
405 406 407
    operators << ">=";
    kwdMap[">="] = LxOperGreaterOrEqual;

victor's avatar
 
victor committed
408 409 410
    operators << QString::fromUtf8("≥");
    kwdMap[QString::fromUtf8("≥")] = LxOperGreaterOrEqual;

victor's avatar
 
victor committed
411 412 413
    operators << "<=";
    kwdMap["<="] = LxOperLessOrEqual;

victor's avatar
 
victor committed
414 415 416
    operators << QString::fromUtf8("≤");
    kwdMap[QString::fromUtf8("≤")] = LxOperLessOrEqual;

victor's avatar
 
victor committed
417 418 419
    operators << "<>";
    kwdMap["<>"] = LxOperNotEqual;

victor's avatar
 
victor committed
420
    operators << QString::fromUtf8("≠");
victor's avatar
 
victor committed
421
    kwdMap[QString::fromUtf8("≠")] = LxOperNotEqual;
victor's avatar
 
victor committed
422

victor's avatar
 
victor committed
423 424 425
    operators << "=";
    kwdMap["="] = LxOperEqual;

victor's avatar
 
victor committed
426 427 428 429 430 431
    operators << "<";
    kwdMap["<"] = LxOperLess;

    operators << ">";
    kwdMap[">"] = LxOperGreater;

victor's avatar
 
victor committed
432 433 434
    operators << "/";
    kwdMap["/"] = LxOperSlash;

victor's avatar
 
victor committed
435 436 437
    operators << ";";
    kwdMap[";"] = LxOperSemicolon;

victor's avatar
 
victor committed
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
    operators << "#";


    operators << ":=";
    kwdMap[":="] = LxPriAssign;

//    operators << "\\~";
//    kwdMap["~"] = QString(QChar(KS_TILDA));

//    operators << "&";
//    kwdMap["&"] = QString(QChar(KS_PAR));

//    operators << "%";
//    kwdMap["%"] = QString(QChar(KS_PERCENT));

    operators << "\"";
    operators << "'";
//    operators << QString::fromUtf8("”");
    operators << "\\|";

    compounds += keyWords;
    compounds += typeNames;
    compounds += constNames;

//    keyWords << tr("value");
    for (int i=0; i<compounds.size(); i++) {
        compounds[i] = "\\b("+compounds[i]+")\\b";
    }

//    QString operatorsPattern = operators.join("|");

    QString compoundsPattern = compounds.join("|");

    QString keywordsPattern = "\\b"+keyWords.join("|")+"\\b";
    keywordsPattern.replace("|","\\b|\\b");

    QString typesPattern = "\\b"+typeNames.join("|")+"\\b";
    typesPattern.replace("|","\\b|\\b");

    QString constsPattern = "\\b"+constNames.join("|")+"\\b";
    constsPattern.replace("|","\\b|\\b");

    rxCompound = QRegExp(operators.join("|")+ "|"+
                             compoundsPattern);
    rxTypes =  QRegExp(typesPattern);
    rxConst = QRegExp(constsPattern);
    rxKeyWords = QRegExp(keywordsPattern);

    rxKeyWords.setMinimal(false);
    rxCompound.setMinimal(false);
}

bool isDecimalIntegerConstant(const QString &s) {
    bool result = s.length()>0;
    for (int i=0; i<s.length(); i++) {
        result = result && s[i].isDigit();
        if (!result)
            break;
    }
    return result;
}

bool isDecimalRealConstant(const QString &s) {
    bool result = s.length()>0;
    bool dotFound = false;
    for (int i=0; i<s.length(); i++) {
        if (s[i]=='.') {
            if (!dotFound) {
                dotFound = true;
            }
            else {
                result = false;
                break;
            }
        }
        else if (!s[i].isDigit()) {
            result = false;
            break;
        }
    }
victor's avatar
 
victor committed
518
    return result && s!=".";
victor's avatar
 
victor committed
519 520 521 522 523 524
}

static const QString expFormSymbols = QString::fromUtf8("eEеЕ01234567890");
static const QString hexFormSymbols = QString::fromAscii("0123456789ABCDEFabcdef");

bool isExpRealConstant(const QString &s) {
victor's avatar
 
victor committed
525
    bool result = s.length()>0 && (s[0].isDigit() || s[0]=='.');
victor's avatar
 
victor committed
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
    bool dotFound = false;
    for (int i=0; i<s.length(); i++) {
        if (s[i]=='.') {
            if (!dotFound) {
                dotFound = true;
            }
            else {
                result = false;
                break;
            }
        }
        else if (!expFormSymbols.contains(s[i])) {
            result = false;
            break;
        }
    }
    return result;
}

bool isHexIntegerConstant(const QString &s)  {
    bool result = s.length()>1 && s[0]=='$';
    for (int i=1; i<s.length(); i++) {
        result = result && hexFormSymbols.contains(s[i]);
        if (!result)
            break;
    }
    return result;
}


victor's avatar
 
victor committed
556 557 558
void searchNumericConstants(QList<Lexem*> & lexems) {
    QList<Lexem*>::iterator it = lexems.begin();
    QList<Lexem*>::iterator itt;
victor's avatar
 
victor committed
559
    while (it!=lexems.end()) {
victor's avatar
 
victor committed
560 561
        Lexem * lx = (*it);
        if ( lx->type==LxTypeName ) {
victor's avatar
 
victor committed
562
            itt = it + 1;
victor's avatar
 
victor committed
563
            const QString s = lx->data;
victor's avatar
 
victor committed
564
            if (isDecimalIntegerConstant(s) || isHexIntegerConstant(s)) {
victor's avatar
 
victor committed
565
                lx->type=LxConstInteger;
victor's avatar
 
victor committed
566 567
            }
            else if (isDecimalRealConstant(s)) {
victor's avatar
 
victor committed
568
                lx->type=LxConstReal;
victor's avatar
 
victor committed
569 570
            }
            else if (isExpRealConstant(s)) {
victor's avatar
 
victor committed
571 572 573 574 575 576
                lx->type=LxConstReal;
                lx->data.replace(QString::fromUtf8("е"), "e");
                lx->data.replace(QString::fromUtf8("Е"), "e");
                lx->data.replace("E", "e");
                const QString d = lx->data;
                if ( d.endsWith('e') ) {
victor's avatar
 
victor committed
577
                    if (itt!=lexems.end()) {
victor's avatar
 
victor committed
578 579 580 581 582 583
                        Lexem * lxx = (*itt);
                        if ( lxx->type==LxOperPlus || lxx->type==LxOperMinus ) {
                            lx->data += lxx->data;
                            lx->length += lxx->length;
                            delete lxx;
                            lxx = 0;
victor's avatar
 
victor committed
584 585
                            itt = lexems.erase(itt);
                            if (itt!=lexems.end()) {
victor's avatar
 
victor committed
586 587
                                lxx = (*itt);
                                const QString ss = lxx->data;
victor's avatar
 
victor committed
588
//                                if (isDecimalRealConstant(ss)) {
victor's avatar
 
victor committed
589 590 591 592
                                    lx->data += lxx->data;
                                    lx->length += lxx->length;
                                    delete lxx;
                                    lxx = 0;
victor's avatar
 
victor committed
593
                                    lexems.erase(itt);
victor's avatar
 
victor committed
594
//                                }
victor's avatar
 
victor committed
595 596 597 598 599
                            }
                        }
                    }
                }
                else {
victor's avatar
 
victor committed
600 601
                    if ( !lx->data.contains("+") && !lx->data.contains("-") ) {
                        lx->data.replace("e","e+");
victor's avatar
 
victor committed
602 603 604 605 606 607 608 609
                    }
                }
            }
        }
        it ++;
    }
}

victor's avatar
 
victor committed
610 611 612 613 614 615
void searchEndLoopIf(QList<Lexem*> & lexems) {
    QList<Lexem*>::iterator it;
    for (it=lexems.begin(); it!=lexems.end(); ) {
        Lexem * lx = (*it);
        Lexem * lxx = 0;
        if (lx->type==LxPriEndLoop) {
616
            if (lx->data.contains("_") || lx->data.contains(" ")) {
victor's avatar
 
victor committed
617
                int underscorePos = lx->data.indexOf("_");
618 619
                if (underscorePos==-1)
                    underscorePos = lx->data.indexOf(" ");
victor's avatar
 
victor committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
                QString caseText = lx->data.mid(underscorePos);
                lx->data = lx->data.left(underscorePos);
                lx->length -= caseText.length();
                lxx = new Lexem;
                lxx->data = caseText;
                lxx->lineNo = lx->lineNo;
                lxx->linePos = lx->linePos + lx->length;
                lxx->length = caseText.length();
                lxx->type = LxSecIf;
            }
        }
        it++;
        if (lxx) {
            it = lexems.insert(it, lxx);
        }
    }
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
void searchUserTypeNames(QList<Lexem*> & lexems, const QStringList & names) {
    QList<Lexem*>::iterator it;
    for (it=lexems.begin(); it!=lexems.end(); ) {
        Lexem * lx = (*it);
        if (lx->type==LxTypeName) {
            for (int i=0; i<names.size(); i++) {
                const QString typeName = names[i];
                const QRegExp rxTypeName("\\b"+typeName+"\\b");
                int p = lx->data.indexOf(rxTypeName);
                if (p!=-1) {
                    QString left, right;
                    if (p>0) {
                        left = lx->data.left(p);
                        Lexem * lxx = new Lexem;
                        lxx->data = left;
                        lxx->type = LxTypeName;
                        lxx->lineNo = lx->lineNo;
                        lxx->linePos = lx->linePos;
                        lxx->length = left.length();
                        it = lexems.insert(it, lxx);
658
                        it++;
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                    }
                    Lexem *tl = new Lexem;
                    tl->data = typeName;
                    tl->type = LxNameClass;
                    tl->lineNo = lx->lineNo;
                    tl->linePos = lx->linePos + p;
                    tl->length = typeName.length();
                    it = lexems.insert(it, tl);
                    right = lx->data.mid(p+typeName.length());
                    lx->data = right;
                    lx->length = right.length();
                    lx->linePos = lx->linePos + p + typeName.length();
                }
            }
        }
        it++;
    }
}

victor's avatar
 
victor committed
678
void LexerPrivate::splitLineIntoLexems(const QString &text
victor's avatar
 
victor committed
679
                                       , QList<Lexem*> &lexems
680
                                       , const QStringList & extraTypeNames
victor's avatar
 
victor committed
681 682 683 684 685 686 687 688
                                       ) const
{
    lexems.clear();
    Q_ASSERT(rxCompound.isValid());
    bool inLit = false;
    QChar litSimb;
    int cur = 0;
    int prev = -1;
victor's avatar
 
victor committed
689
    if (text.trimmed().isEmpty()) {
victor's avatar
 
victor committed
690 691 692
        return;
    }
    forever {
victor's avatar
 
victor committed
693
        cur = rxCompound.indexIn(text, qMax(0,prev));
victor's avatar
 
victor committed
694
        if (cur!=-1) {
victor's avatar
 
victor committed
695
            if ( (cur-prev>1&&prev==-1) || (cur-prev>0&&prev>=0) ) {
victor's avatar
 
victor committed
696
                if (inLit) {
victor's avatar
 
victor committed
697 698
                    const QString app = text.mid(prev, cur-prev);
                    lexems.last()->data += app;
victor's avatar
 
victor committed
699 700
                }
                else {
victor's avatar
 
victor committed
701
                    Lexem * lx = new Lexem;
victor's avatar
 
victor committed
702
                    lx->linePos = qMax(prev, 0);
victor's avatar
 
victor committed
703 704
                    lx->type = LxTypeName;
                    lx->data = text.mid(qMax(prev,0), prev>=0? cur-prev : cur-prev-1);
victor's avatar
 
victor committed
705 706 707 708 709 710 711 712 713
                    lexems << lx;
                }
            }
            QString symb = rxCompound.cap(0);
            if (inLit) {
                if ( (symb=="\"" || symb=="'") && symb[0]==litSimb) {
                    inLit = false;
                }
                else {
victor's avatar
 
victor committed
714
                    lexems.last()->data += symb;
victor's avatar
 
victor committed
715 716 717 718 719
                }

            }
            else {
                if (symb=="|") {
victor's avatar
 
victor committed
720 721
                    Lexem * lx = new Lexem;
                    lx->type = LxTypeComment;
victor's avatar
 
victor committed
722 723
                    lx->data = text.mid(cur);
                    lx->linePos = cur;
victor's avatar
 
victor committed
724 725 726 727
                    lexems << lx;
                    break;
                }
                else if (symb=="#") {
victor's avatar
 
victor committed
728 729
                    Lexem * lx = new Lexem;
                    lx->type = LxTypeDoc;
victor's avatar
 
victor committed
730 731
                    lx->data = text.mid(cur);
                    lx->linePos = cur;
victor's avatar
 
victor committed
732 733 734 735 736
                    lexems << lx;
                    break;
                }
                else if (symb=="\"" || symb=="'") {
                    litSimb = symb[0];
victor's avatar
 
victor committed
737 738
                    Lexem * lx = new Lexem;
                    lx->type = LxConstLiteral;
victor's avatar
 
victor committed
739
                    lx->linePos = cur;
victor's avatar
 
victor committed
740
                    inLit = true;
victor's avatar
 
victor committed
741
                    lexems << lx;
victor's avatar
 
victor committed
742 743 744 745 746
                }
                else {
                    QString simplifiedSymb = symb;
                    simplifiedSymb.remove(' ');
                    simplifiedSymb.remove('_');
victor's avatar
 
victor committed
747 748 749
                    Lexem * lx = new Lexem;
                    lx->type = kwdMap[simplifiedSymb];
                    lx->data = symb;
victor's avatar
 
victor committed
750
                    lx->linePos = cur;
victor's avatar
 
victor committed
751 752 753
                    lexems << lx;
                }
            }
victor's avatar
 
victor committed
754
            prev = cur + rxCompound.matchedLength();
victor's avatar
 
victor committed
755 756 757
        } // end if cur!=-1
        else {
            if (inLit) {
victor's avatar
 
victor committed
758 759
                lexems.last()->data += text.mid(prev+1);
                lexems.last()->error = _("Unpaired quote");
victor's avatar
 
victor committed
760 761
                lexems.last()->errorStage = AST::Lexem::Lexer;

victor's avatar
 
victor committed
762 763
            }
            else {
victor's avatar
 
victor committed
764 765 766 767 768 769 770 771 772 773 774 775
                if (!lexems.isEmpty()) {
                    Lexem * lx = lexems.last();
                    if (lx->type==LxTypeComment || lx->type==LxTypeDoc)
                        lexems.last()->data += text.mid(prev+1);
                    else {
                        if (prev<text.length()) {
                            Lexem * llx = new Lexem;
                            llx->type = LxTypeName;
                            llx->linePos = qMax(prev, 0);
                            llx->data = text.mid(prev);
                            lexems << llx;
                        }
victor's avatar
 
victor committed
776
                    }
victor's avatar
 
victor committed
777
                }
victor's avatar
 
victor committed
778 779 780 781 782 783 784
                else {
                    Lexem * lx = new Lexem;
                    lx->type = LxTypeName;
                    lx->linePos = 0;
                    lx->data = text;
                    lexems << lx;
                }
victor's avatar
 
victor committed
785 786 787 788
            }
            break;
        }
    }
789
    searchUserTypeNames(lexems, extraTypeNames);
victor's avatar
 
victor committed
790
    for (int i=0; i<lexems.size(); i++) {
victor's avatar
 
victor committed
791
        lexems[i]->length = lexems[i]->data.size();
victor's avatar
 
victor committed
792
        if (lexems[i]->type!=LxConstLiteral) {
victor's avatar
 
victor committed
793 794 795 796 797 798 799 800 801
            while (lexems[i]->data.startsWith(' ')) {
                lexems[i]->data = lexems[i]->data.mid(1);
                lexems[i]->length--;
                lexems[i]->linePos++;
            }
            while (lexems[i]->data.endsWith(' ')) {
                lexems[i]->data = lexems[i]->data.mid(0, lexems[i]->data.length()-1);
                lexems[i]->length--;
            }
victor's avatar
 
victor committed
802
            lexems[i]->data = lexems[i]->data.simplified();
victor's avatar
 
victor committed
803 804
        }
        else {
victor's avatar
 
victor committed
805
            lexems[i]->length += 2;
victor's avatar
 
victor committed
806 807
        }
    }
victor's avatar
 
victor committed
808
    QList<Lexem*>::iterator it = lexems.begin();
victor's avatar
 
victor committed
809
    while (it!=lexems.end()) {
victor's avatar
 
victor committed
810
        Lexem * lx = (*it);
victor's avatar
 
victor committed
811 812 813 814 815
        if (lx->data.isEmpty() &&
                lx->errorStage!=AST::Lexem::Lexer &&
                lx->type!=LxConstLiteral &&
                lx->type!=LxTypeDoc
                ) {
victor's avatar
 
victor committed
816
            delete lx;
victor's avatar
 
victor committed
817 818 819 820 821 822
            it = lexems.erase(it);
        }
        else {
            it ++;
        }
    }
victor's avatar
 
victor committed
823
    searchNumericConstants(lexems);
824

victor's avatar
 
victor committed
825
    searchEndLoopIf(lexems);
826

victor's avatar
 
victor committed
827 828
}

victor's avatar
 
victor committed
829 830
void popFirstStatement(QList<Lexem*> & lexems, Statement & result );
void popFirstStatementByKeyword(QList<Lexem*> & lexems, Statement & result );
victor's avatar
 
victor committed
831 832

void LexerPrivate::groupLexemsByStatements(
victor's avatar
 
victor committed
833
    const QList<Lexem*> &lexems
victor's avatar
 
victor committed
834
    , QList<Statement*> &statements
victor's avatar
 
victor committed
835 836
    ) const
{
victor's avatar
 
victor committed
837
    QList<Lexem*> lexemsCopy = lexems;
victor's avatar
 
victor committed
838 839 840 841
    while (lexemsCopy.size()>0) {
        Statement statement;
        popFirstStatement(lexemsCopy, statement);
        if (statement.data.size()>0)
victor's avatar
 
victor committed
842
            statements << new Statement(statement);
victor's avatar
 
victor committed
843 844 845
    }
}

victor's avatar
 
victor committed
846 847


victor's avatar
 
victor committed
848
void popFirstStatement(QList<Lexem*> & lexems, Statement & result )
victor's avatar
 
victor committed
849 850 851
{
    if (lexems.isEmpty())
        return;
victor's avatar
 
victor committed
852
    if (lexems[0]->type==LxOperSemicolon) {
victor's avatar
 
victor committed
853 854 855
        // Semicolons are delemiters. Just skip them.
        lexems.pop_front();
    }
victor's avatar
 
victor committed
856
    else if (lexems[0]->type==LxTypeComment || lexems[0]->type==LxTypeDoc) {
victor's avatar
 
victor committed
857 858
        // Comments and docstrings are newline-endian lexems
        result.data << lexems[0];
victor's avatar
 
victor committed
859
        result.type = lexems[0]->type;
victor's avatar
 
victor committed
860 861
        lexems.pop_front();
    }
victor's avatar
 
victor committed
862
    else if (lexems[0]->type & LxTypePrimaryKwd || lexems[0]->type==LxNameClass) {
victor's avatar
 
victor committed
863 864 865 866 867 868 869
        // Statement type can be determined by primary keyword
        popFirstStatementByKeyword(lexems, result);
    }
    else {
        // Generic assignment (or algothitm call) statement
        result.type = LxPriAssign;
        while (lexems.size()>0) {
victor's avatar
 
victor committed
870 871
            Lexem * lx = lexems[0];
            if (lx->type==LxOperSemicolon
victor's avatar
 
victor committed
872 873 874 875 876
                    || lx->type==LxPriElse
                    || lx->type==LxPriFi
                    || lx->type==LxPriEndLoop
                    || lx->type==LxPriEndModule
                    || lx->type==LxPriAlgEnd
victor's avatar
 
victor committed
877
//                    || ( lx->type & LxTypePrimaryKwd && lx->type!=LxPriAssign)
victor's avatar
 
victor committed
878 879
//                    || lx->type==LxNameClass
                    ) {
victor's avatar
 
victor committed
880 881 882 883 884 885 886 887 888
                // end of statement
                break;
            }
            lexems.pop_front();
            result.data << lx;
        }
    }
}

victor's avatar
 
victor committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
void popModuleStatement(QList<Lexem*> & lexems, Statement &result);
void popEndModuleStatement(QList<Lexem*> & lexems, Statement &result);
void popAlgHeaderStatement(QList<Lexem*> & lexems, Statement &result);
void popAlgBeginStatement(QList<Lexem*> & lexems, Statement &result);
void popAlgEndStatement(QList<Lexem*> & lexems, Statement &result);
void popPreStatement(QList<Lexem*> & lexems, Statement &result);
void popPostStatement(QList<Lexem*> & lexems, Statement &result);
void popIfStatement(QList<Lexem*> & lexems, Statement &result);
void popThenStatement(QList<Lexem*> & lexems, Statement &result);
void popElseStatement(QList<Lexem*> & lexems, Statement &result);
void popFiStatement(QList<Lexem*> & lexems, Statement &result);
void popSwitchStatement(QList<Lexem*> & lexems, Statement &result);
void popCaseStatement(QList<Lexem*> & lexems, Statement &result);
void popLoopStatement(QList<Lexem*> & lexems, Statement &result);
void popEndLoopStatement(QList<Lexem*> & lexems, Statement &result);
void popInputStatement(QList<Lexem*> & lexems, Statement &result);
void popOutputStatement(QList<Lexem*> & lexems, Statement &result);
void popFinputStatement(QList<Lexem*> & lexems, Statement &result);
void popFoutputStatement(QList<Lexem*> & lexems, Statement &result);
void popAssertStatement(QList<Lexem*> & lexems, Statement &result);
void popImportStatement(QList<Lexem*> & lexems, Statement &result);
void popExitStatement(QList<Lexem*> & lexems, Statement &result);
victor's avatar
 
victor committed
911 912
void popPauseStatement(QList<Lexem*> & lexems, Statement &result);
void popHaltStatement(QList<Lexem*> & lexems, Statement &result);
victor's avatar
 
victor committed
913
void popVarDeclStatement(QList<Lexem*> & lexems, Statement &result);
victor's avatar
 
victor committed
914
void popAssignFileStatement(QList<Lexem*> & lexems, Statement & result);
victor's avatar
 
victor committed
915

victor's avatar
 
victor committed
916 917 918 919
void popLexemsUntilPrimaryKeyword(QList<Lexem*> & lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
920
        if (lx->type==LxOperSemicolon || (lx->type & LxTypePrimaryKwd && lx->type!=LxPriAssign))
victor's avatar
 
victor committed
921 922 923 924 925 926
            break;
        lexems.pop_front();
        result.data << lx;
    }
}

victor's avatar
 
victor committed
927 928 929 930 931 932 933 934 935 936 937
void popLexemsUntilSemicolonOrBegin(QList<Lexem*> & lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
        if (lx->type==LxOperSemicolon || lx->type==LxPriAlgBegin)
            break;
        lexems.pop_front();
        result.data << lx;
    }
}

938 939 940 941 942 943 944 945 946 947 948 949 950 951

void popLexemsUntilSemicolonOrBlockClose(QList<Lexem*> & lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
        if (lx->type==LxOperSemicolon
                || lx->type==LxPriElse
                || lx->type==LxPriEndLoop
                || lx->type==LxPriFi)
            break;
        lexems.pop_front();
        result.data << lx;
    }
}
victor's avatar
 
victor committed
952 953 954 955 956 957 958 959 960 961 962 963 964
void popLexemsUntilPrimaryKeywordExclIO(QList<Lexem*> & lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
        if (lx->type==LxOperSemicolon || lx->type & LxTypePrimaryKwd) {
            if (lx->type!=LxPriInput && lx->type!=LxPriOutput)
                break;
        }
        lexems.pop_front();
        result.data << lx;
    }
}

victor's avatar
 
victor committed
965 966 967 968
void popLexemsUntilPrimaryKeywordOrVarDecl(QList<Lexem*> &lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
victor's avatar
 
victor committed
969 970 971
        if (lx->type==LxOperSemicolon ||
                (lx->type & LxTypePrimaryKwd && lx->type!=LxPriAssign) ||
                lx->type==LxNameClass)
victor's avatar
 
victor committed
972 973 974 975 976 977
            break;
        lexems.pop_front();
        result.data << lx;
    }
}

victor's avatar
 
victor committed
978 979 980 981 982 983 984 985 986 987 988 989 990 991
void popLexemsUntilPrimaryKeywordOrColon(QList<Lexem*> &lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
        if (lx->type==LxOperSemicolon ||
                (lx->type & LxTypePrimaryKwd && lx->type!=LxPriAssign))
            break;
        lexems.pop_front();
        result.data << lx;
        if (lx->type==LxOperColon)
            break;
    }
}

victor's avatar
 
victor committed
992 993 994 995 996 997 998 999 1000 1001 1002
void popLexemsUntilSemicolon(QList<Lexem*> &lexems, Statement &result)
{
    while (lexems.size()>0) {
        Lexem * lx = lexems[0];
        if (lx->type==LxOperSemicolon)
            break;
        lexems.pop_front();
        result.data << lx;
    }
}

victor's avatar
 
victor committed
1003
void popFirstStatementByKeyword(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1004 1005
{
    Q_ASSERT(!lexems.isEmpty());
victor's avatar
 
victor committed
1006
    if (lexems[0]->type==LxPriModule) {
victor's avatar
 
victor committed
1007 1008
        popModuleStatement(lexems, result);
    }
victor's avatar
 
victor committed
1009
    else if (lexems[0]->type==LxPriEndModule) {
victor's avatar
 
victor committed
1010 1011
        popEndModuleStatement(lexems, result);
    }
victor's avatar
 
victor committed
1012
    else if (lexems[0]->type==LxPriAlgHeader) {
victor's avatar
 
victor committed
1013 1014
        popAlgHeaderStatement(lexems, result);
    }
victor's avatar
 
victor committed
1015
    else if (lexems[0]->type==LxPriAlgBegin) {
victor's avatar
 
victor committed
1016 1017
        popAlgBeginStatement(lexems, result);
    }
victor's avatar
 
victor committed
1018
    else if (lexems[0]->type==LxPriAlgEnd) {
victor's avatar
 
victor committed
1019 1020
        popAlgEndStatement(lexems, result);
    }
victor's avatar
 
victor committed
1021
    else if (lexems[0]->type==LxPriPre) {
victor's avatar
 
victor committed
1022 1023
        popPreStatement(lexems, result);
    }
victor's avatar
 
victor committed
1024
    else if (lexems[0]->type==LxPriPost) {
victor's avatar
 
victor committed
1025 1026
        popPostStatement(lexems, result);
    }
victor's avatar
 
victor committed
1027
    else if (lexems[0]->type==LxPriIf) {
victor's avatar
 
victor committed
1028 1029
        popIfStatement(lexems, result);
    }
victor's avatar
 
victor committed
1030
    else if (lexems[0]->type==LxPriThen) {
victor's avatar
 
victor committed
1031 1032
        popThenStatement(lexems, result);
    }
victor's avatar
 
victor committed
1033
    else if (lexems[0]->type==LxPriElse) {
victor's avatar
 
victor committed
1034 1035
        popElseStatement(lexems, result);
    }
victor's avatar
 
victor committed
1036
    else if (lexems[0]->type==LxPriFi) {
victor's avatar
 
victor committed
1037 1038
        popFiStatement(lexems, result);
    }
victor's avatar
 
victor committed
1039
    else if (lexems[0]->type==LxPriSwitch) {
victor's avatar
 
victor committed
1040 1041
        popSwitchStatement(lexems, result);
    }
victor's avatar
 
victor committed
1042
    else if (lexems[0]->type==LxPriCase) {
victor's avatar
 
victor committed
1043 1044
        popCaseStatement(lexems, result);
    }
victor's avatar
 
victor committed
1045
    else if (lexems[0]->type==LxPriLoop) {
victor's avatar
 
victor committed
1046 1047
        popLoopStatement(lexems, result);
    }
victor's avatar
 
victor committed
1048
    else if (lexems[0]->type==LxPriEndLoop) {
victor's avatar
 
victor committed
1049 1050
        popEndLoopStatement(lexems, result);
    }
victor's avatar
 
victor committed
1051
    else if (lexems[0]->type==LxPriInput) {
victor's avatar
 
victor committed
1052 1053
        popInputStatement(lexems, result);
    }
victor's avatar
 
victor committed
1054
    else if (lexems[0]->type==LxPriOutput) {
victor's avatar
 
victor committed
1055 1056
        popOutputStatement(lexems, result);
    }
victor's avatar
 
victor committed
1057
    else if (lexems[0]->type==LxPriAssert) {
victor's avatar
 
victor committed
1058 1059
        popAssertStatement(lexems, result);
    }
victor's avatar
 
victor committed
1060
    else if (lexems[0]->type==LxPriImport) {
victor's avatar
 
victor committed
1061 1062
        popImportStatement(lexems, result);
    }
victor's avatar
 
victor committed
1063
    else if (lexems[0]->type==LxPriExit) {
victor's avatar
 
victor committed
1064 1065
        popExitStatement(lexems, result);
    }
victor's avatar
 
victor committed
1066 1067 1068 1069 1070 1071
    else if (lexems[0]->type==LxPriPause) {
        popPauseStatement(lexems, result);
    }
    else if (lexems[0]->type==LxPriHalt) {
        popHaltStatement(lexems, result);
    }
victor's avatar
 
victor committed
1072
    else if (lexems[0]->type==LxNameClass) {
victor's avatar
 
victor committed
1073 1074 1075
        popVarDeclStatement(lexems, result);
    }
    else {
victor's avatar
 
victor committed
1076 1077
        popLexemsUntilSemicolon(lexems, result);
        result.type = LxPriAssign;
victor's avatar
 
victor committed
1078 1079 1080 1081 1082
    }
}



victor's avatar
 
victor committed
1083
void popModuleStatement(QList<Lexem*> & lexems, Statement &result)
victor's avatar
 
victor committed
1084
{
victor's avatar
 
victor committed
1085
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1086 1087 1088 1089 1090
    result.data << lexems[0];
    lexems.pop_front();
    popLexemsUntilPrimaryKeywordOrVarDecl(lexems, result);
}

victor's avatar
 
victor committed
1091
void popEndModuleStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1092
{
victor's avatar
 
victor committed
1093
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1094 1095 1096 1097 1098
    result.data << lexems[0];
    lexems.pop_front();
    popLexemsUntilPrimaryKeywordOrVarDecl(lexems, result);
}

victor's avatar
 
victor committed
1099
void popAlgHeaderStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1100
{
victor's avatar
 
victor committed
1101
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1102 1103
    result.data << lexems[0];
    lexems.pop_front();
victor's avatar
 
victor committed
1104
    popLexemsUntilSemicolonOrBegin(lexems, result);
victor's avatar
 
victor committed
1105 1106
}

victor's avatar
 
victor committed
1107
void popAlgBeginStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1108
{
victor's avatar
 
victor committed
1109
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1110 1111 1112 1113 1114
    result.data << lexems[0];
    lexems.pop_front();
    popLexemsUntilPrimaryKeywordOrVarDecl(lexems, result);
}

victor's avatar
 
victor committed
1115
void popAlgEndStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1116
{
victor's avatar
 
victor committed
1117
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1118 1119 1120 1121 1122
    result.data << lexems[0];
    lexems.pop_front();
    popLexemsUntilPrimaryKeywordOrVarDecl(lexems, result);
}

victor's avatar
 
victor committed
1123
void popExitStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1124 1125 1126 1127
{
    result.type = lexems[0]->type;
    result.data << lexems[0];
    lexems.pop_front();
1128
    popLexemsUntilSemicolonOrBlockClose(lexems, result);
victor's avatar
 
victor committed
1129 1130 1131 1132 1133 1134 1135
}

void popPauseStatement(QList<Lexem*> &lexems, Statement &result)
{
    result.type = lexems[0]->type;
    result.data << lexems[0];
    lexems.pop_front();
1136
    popLexemsUntilSemicolonOrBlockClose(lexems, result);
victor's avatar
 
victor committed
1137 1138 1139
}

void popHaltStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1140 1141 1142 1143
{
    result.type = lexems[0]->type;
    result.data << lexems[0];
    lexems.pop_front();
1144
    popLexemsUntilSemicolonOrBlockClose(lexems, result);
victor's avatar
 
victor committed
1145 1146
}

victor's avatar
 
victor committed
1147

victor's avatar
 
victor committed
1148
void popPreStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1149
{
victor's avatar
 
victor committed
1150
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1151 1152
    result.data << lexems[0];
    lexems.pop_front();
1153
    popLexemsUntilSemicolon(lexems, result);
victor's avatar
 
victor committed
1154 1155
}

victor's avatar
 
victor committed
1156
void popPostStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1157
{
victor's avatar
 
victor committed
1158
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1159 1160
    result.data << lexems[0];
    lexems.pop_front();
1161
    popLexemsUntilSemicolon(lexems, result);
victor's avatar
 
victor committed
1162 1163
}

victor's avatar
 
victor committed
1164
void popIfStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1165
{
victor's avatar
 
victor committed
1166
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1167 1168
    result.data << lexems[0];
    lexems.pop_front();
1169
    popLexemsUntilPrimaryKeyword(lexems, result);
victor's avatar
 
victor committed
1170 1171
}

victor's avatar
 
victor committed
1172
void popThenStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1173
{
victor's avatar
 
victor committed
1174
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1175 1176
    result.data << lexems[0];
    lexems.pop_front();
victor's avatar
 
victor committed
1177
//    popLexemsUntilPrimaryKeywordOrVarDecl(lexems, result);
victor's avatar
 
victor committed
1178 1179
}

victor's avatar
 
victor committed
1180
void popElseStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1181
{
victor's avatar
 
victor committed
1182
    result.type = lexems[0]->type;
victor's avatar
 
victor committed
1183 1184
    result.data << lexems[0];
    lexems.pop_front();
victor's avatar
 
victor committed
1185
//    popLexemsUntilPrimaryKeywordOrVarDecl(lexems, result);
victor's avatar
 
victor committed
1186 1187
}

victor's avatar
 
victor committed
1188
void popFiStatement(QList<Lexem*> &lexems, Statement &result)
victor's avatar
 
victor committed
1189
{
victor's avatar