File: parser.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (680 lines) | stat: -rw-r--r-- 15,009 bytes parent folder | download
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
668
669
670
671
672
673
674
675
676
677
678
679
680
#include "parser.hpp"

#include <algorithm>
#include <cctype>
#include <sstream>
#include <stdexcept>
#include <vector>

#include <apps/opencs/model/world/idcollection.hpp>
#include <apps/opencs/model/world/record.hpp>

#include <components/esm3/filter.hpp>
#include <components/misc/strings/lower.hpp>

#include "../world/columns.hpp"
#include "../world/data.hpp"

#include "andnode.hpp"
#include "booleannode.hpp"
#include "notnode.hpp"
#include "ornode.hpp"
#include "textnode.hpp"
#include "valuenode.hpp"

namespace
{
    bool isAlpha(char c)
    {
        return std::isalpha(static_cast<unsigned char>(c));
    }

    bool isDigit(char c)
    {
        return std::isdigit(static_cast<unsigned char>(c));
    }
}

namespace CSMFilter
{
    struct Token
    {
        enum Type
        {
            Type_EOS,
            Type_None,
            Type_String,
            Type_Number,
            Type_Open,
            Type_Close,
            Type_OpenSquare,
            Type_CloseSquare,
            Type_Comma,
            Type_OneShot,
            Type_Keyword_True, ///< \attention Keyword enums must be arranged continuously.
            Type_Keyword_False,
            Type_Keyword_And,
            Type_Keyword_Or,
            Type_Keyword_Not,
            Type_Keyword_Text,
            Type_Keyword_Value
        };

        Type mType;
        std::string mString;
        double mNumber;

        Token(Type type = Type_None);

        Token(Type type, const std::string& string);
        ///< Non-string type that can also be interpreted as a string.

        Token(const std::string& string);

        Token(double number);

        operator bool() const;

        bool isString() const;
    };

    Token::Token(Type type)
        : mType(type)
        , mNumber(0.0)
    {
    }

    Token::Token(Type type, const std::string& string)
        : mType(type)
        , mString(string)
        , mNumber(0.0)
    {
    }

    Token::Token(const std::string& string)
        : mType(Type_String)
        , mString(string)
        , mNumber(0.0)
    {
    }

    Token::Token(double number)
        : mType(Type_Number)
        , mNumber(number)
    {
    }

    bool Token::isString() const
    {
        return mType == Type_String || mType >= Type_Keyword_True;
    }

    Token::operator bool() const
    {
        return mType != Type_None;
    }

    bool operator==(const Token& left, const Token& right)
    {
        if (left.mType != right.mType)
            return false;

        switch (left.mType)
        {
            case Token::Type_String:
                return left.mString == right.mString;
            case Token::Type_Number:
                return left.mNumber == right.mNumber;

            default:
                return true;
        }
    }
}

CSMFilter::Token CSMFilter::Parser::getStringToken()
{
    std::string string;

    int size = static_cast<int>(mInput.size());

    for (; mIndex < size; ++mIndex)
    {
        char c = mInput[mIndex];

        if (isAlpha(c) || c == ':' || c == '_' || (!string.empty() && isDigit(c)) || c == '"'
            || (!string.empty() && string[0] == '"'))
            string += c;
        else
            break;

        if (c == '"' && string.size() > 1)
        {
            ++mIndex;
            break;
        }
    };

    if (!string.empty())
    {
        if (string[0] == '"' && (string[string.size() - 1] != '"' || string.size() < 2))
        {
            error();
            return Token(Token::Type_None);
        }

        if (string[0] != '"' && string[string.size() - 1] == '"')
        {
            error();
            return Token(Token::Type_None);
        }

        if (string[0] == '"')
            return string.substr(1, string.size() - 2);
    }

    return checkKeywords(string);
}

CSMFilter::Token CSMFilter::Parser::getNumberToken()
{
    std::string string;

    int size = static_cast<int>(mInput.size());

    bool hasDecimalPoint = false;
    bool hasDigit = false;

    for (; mIndex < size; ++mIndex)
    {
        char c = mInput[mIndex];

        if (isDigit(c))
        {
            string += c;
            hasDigit = true;
        }
        else if (c == '.' && !hasDecimalPoint)
        {
            string += c;
            hasDecimalPoint = true;
        }
        else if (string.empty() && c == '-')
            string += c;
        else
            break;
    }

    if (!hasDigit)
    {
        error();
        return Token(Token::Type_None);
    }

    float value;
    std::istringstream stream(string.c_str());
    stream >> value;

    return value;
}

CSMFilter::Token CSMFilter::Parser::checkKeywords(const Token& token)
{
    static const char* sKeywords[] = {
        "true",
        "false",
        "and",
        "or",
        "not",
        "string",
        "value",
        nullptr,
    };

    std::string string = Misc::StringUtils::lowerCase(token.mString);

    for (int i = 0; sKeywords[i]; ++i)
        if (sKeywords[i] == string || (string.size() == 1 && sKeywords[i][0] == string[0]))
            return Token(static_cast<Token::Type>(i + Token::Type_Keyword_True), token.mString);

    return token;
}

CSMFilter::Token CSMFilter::Parser::getNextToken()
{
    int size = static_cast<int>(mInput.size());

    char c = 0;

    for (; mIndex < size; ++mIndex)
    {
        c = mInput[mIndex];

        if (c != ' ')
            break;
    }

    if (mIndex >= size)
        return Token(Token::Type_EOS);

    switch (c)
    {
        case '(':
            ++mIndex;
            return Token(Token::Type_Open);
        case ')':
            ++mIndex;
            return Token(Token::Type_Close);
        case '[':
            ++mIndex;
            return Token(Token::Type_OpenSquare);
        case ']':
            ++mIndex;
            return Token(Token::Type_CloseSquare);
        case ',':
            ++mIndex;
            return Token(Token::Type_Comma);
        case '!':
            ++mIndex;
            return Token(Token::Type_OneShot);
    }

    if (c == '"' || c == '_' || isAlpha(c) || c == ':')
        return getStringToken();

    if (c == '-' || c == '.' || isDigit(c))
        return getNumberToken();

    error();
    return Token(Token::Type_None);
}

std::shared_ptr<CSMFilter::Node> CSMFilter::Parser::parseImp(bool allowEmpty, bool ignoreOneShot)
{
    if (Token token = getNextToken())
    {
        if (token == Token(Token::Type_OneShot))
            token = getNextToken();

        if (token)
            switch (token.mType)
            {
                case Token::Type_Keyword_True:

                    return std::make_shared<BooleanNode>(true);

                case Token::Type_Keyword_False:

                    return std::make_shared<BooleanNode>(false);

                case Token::Type_Keyword_And:
                case Token::Type_Keyword_Or:

                    return parseNAry(token);

                case Token::Type_Keyword_Not:
                {
                    std::shared_ptr<CSMFilter::Node> node = parseImp();

                    if (mError)
                        return std::shared_ptr<Node>();

                    return std::make_shared<NotNode>(node);
                }

                case Token::Type_Keyword_Text:

                    return parseText();

                case Token::Type_Keyword_Value:

                    return parseValue();

                case Token::Type_EOS:

                    if (!allowEmpty)
                        error();

                    return std::shared_ptr<Node>();

                default:

                    error();
            }
    }

    return std::shared_ptr<Node>();
}

std::shared_ptr<CSMFilter::Node> CSMFilter::Parser::parseNAry(const Token& keyword)
{
    std::vector<std::shared_ptr<Node>> nodes;

    Token token = getNextToken();

    if (token.mType != Token::Type_Open)
    {
        error();
        return std::shared_ptr<Node>();
    }

    for (;;)
    {
        std::shared_ptr<Node> node = parseImp();

        if (mError)
            return std::shared_ptr<Node>();

        nodes.push_back(node);

        token = getNextToken();

        if (!token || (token.mType != Token::Type_Close && token.mType != Token::Type_Comma))
        {
            error();
            return std::shared_ptr<Node>();
        }

        if (token.mType == Token::Type_Close)
            break;
    }

    switch (keyword.mType)
    {
        case Token::Type_Keyword_And:
            return std::make_shared<AndNode>(nodes);
        case Token::Type_Keyword_Or:
            return std::make_shared<OrNode>(nodes);
        default:
            error();
            return std::shared_ptr<Node>();
    }
}

std::shared_ptr<CSMFilter::Node> CSMFilter::Parser::parseText()
{
    Token token = getNextToken();

    if (token.mType != Token::Type_Open)
    {
        error();
        return std::shared_ptr<Node>();
    }

    token = getNextToken();

    if (!token)
        return std::shared_ptr<Node>();

    // parse column ID
    int columnId = -1;

    if (token.mType == Token::Type_Number)
    {
        if (static_cast<int>(token.mNumber) == token.mNumber)
            columnId = static_cast<int>(token.mNumber);
    }
    else if (token.isString())
    {
        columnId = CSMWorld::Columns::getId(token.mString);
    }

    if (columnId < 0)
    {
        error();
        return std::shared_ptr<Node>();
    }

    token = getNextToken();

    if (token.mType != Token::Type_Comma)
    {
        error();
        return std::shared_ptr<Node>();
    }

    // parse text pattern
    token = getNextToken();

    if (!token.isString())
    {
        error();
        return std::shared_ptr<Node>();
    }

    std::string text = token.mString;

    token = getNextToken();

    if (token.mType != Token::Type_Close)
    {
        error();
        return std::shared_ptr<Node>();
    }

    auto node = std::make_shared<TextNode>(columnId, text);
    if (!node->isValid())
        error();
    return node;
}

std::shared_ptr<CSMFilter::Node> CSMFilter::Parser::parseValue()
{
    Token token = getNextToken();

    if (token.mType != Token::Type_Open)
    {
        error();
        return std::shared_ptr<Node>();
    }

    token = getNextToken();

    if (!token)
        return std::shared_ptr<Node>();

    // parse column ID
    int columnId = -1;

    if (token.mType == Token::Type_Number)
    {
        if (static_cast<int>(token.mNumber) == token.mNumber)
            columnId = static_cast<int>(token.mNumber);
    }
    else if (token.isString())
    {
        columnId = CSMWorld::Columns::getId(token.mString);
    }

    if (columnId < 0)
    {
        error();
        return std::shared_ptr<Node>();
    }

    token = getNextToken();

    if (token.mType != Token::Type_Comma)
    {
        error();
        return std::shared_ptr<Node>();
    }

    // parse value
    double lower = 0;
    double upper = 0;
    ValueNode::Type lowerType = ValueNode::Type_Open;
    ValueNode::Type upperType = ValueNode::Type_Open;

    token = getNextToken();

    if (token.mType == Token::Type_Number)
    {
        // single value
        lower = upper = token.mNumber;
        lowerType = upperType = ValueNode::Type_Closed;
    }
    else
    {
        // interval
        if (token.mType == Token::Type_OpenSquare)
            lowerType = ValueNode::Type_Closed;
        else if (token.mType != Token::Type_CloseSquare && token.mType != Token::Type_Open)
        {
            error();
            return std::shared_ptr<Node>();
        }

        token = getNextToken();

        if (token.mType == Token::Type_Number)
        {
            lower = token.mNumber;

            token = getNextToken();

            if (token.mType != Token::Type_Comma)
            {
                error();
                return std::shared_ptr<Node>();
            }
        }
        else if (token.mType == Token::Type_Comma)
        {
            lowerType = ValueNode::Type_Infinite;
        }
        else
        {
            error();
            return std::shared_ptr<Node>();
        }

        token = getNextToken();

        if (token.mType == Token::Type_Number)
        {
            upper = token.mNumber;

            token = getNextToken();
        }
        else
            upperType = ValueNode::Type_Infinite;

        if (token.mType == Token::Type_CloseSquare)
        {
            if (upperType != ValueNode::Type_Infinite)
                upperType = ValueNode::Type_Closed;
        }
        else if (token.mType != Token::Type_OpenSquare && token.mType != Token::Type_Close)
        {
            error();
            return std::shared_ptr<Node>();
        }
    }

    token = getNextToken();

    if (token.mType != Token::Type_Close)
    {
        error();
        return std::shared_ptr<Node>();
    }

    return std::make_shared<ValueNode>(columnId, lowerType, upperType, lower, upper);
}

void CSMFilter::Parser::error()
{
    mError = true;
}

CSMFilter::Parser::Parser(const CSMWorld::Data& data)
    : mIndex(0)
    , mError(false)
    , mData(data)
{
}

bool CSMFilter::Parser::parse(const std::string& filter, bool allowPredefined)
{
    // reset
    mFilter.reset();
    mError = false;
    mInput = filter;
    mIndex = 0;

    Token token;

    if (allowPredefined)
        token = getNextToken();

    if (allowPredefined && token == Token(Token::Type_EOS))
    {
        mFilter.reset();
        return true;
    }
    else if (!allowPredefined || token == Token(Token::Type_OneShot))
    {
        std::shared_ptr<Node> node = parseImp(true, token != Token(Token::Type_OneShot));

        if (mError)
            return false;

        if (getNextToken() != Token(Token::Type_EOS))
        {
            error();
            return false;
        }

        if (node)
            mFilter = std::move(node);
        else
        {
            // Empty filter string equals to filter "true".
            mFilter = std::make_shared<BooleanNode>(true);
        }

        return true;
    }
    // We do not use isString() here, because there could be a pre-defined filter with an ID that is
    // equal a filter keyword.
    else if (token.mType == Token::Type_String)
    {
        if (getNextToken() != Token(Token::Type_EOS))
        {
            error();
            return false;
        }

        const int index = mData.getFilters().searchId(ESM::RefId::stringRefId(token.mString));

        if (index == -1)
        {
            error();
            return false;
        }

        const CSMWorld::Record<ESM::Filter>& record = mData.getFilters().getRecord(index);

        if (record.isDeleted())
        {
            error();
            return false;
        }

        return parse(record.get().mFilter, false);
    }
    else
    {
        error();
        return false;
    }
}

std::shared_ptr<CSMFilter::Node> CSMFilter::Parser::getFilter() const
{
    if (mError)
        throw std::logic_error("No filter available");

    return mFilter;
}