File: stringparser.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (105 lines) | stat: -rw-r--r-- 3,273 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
#include "stringparser.hpp"

#include <algorithm>
#include <iterator>

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

#include "context.hpp"
#include "extensions.hpp"
#include "generator.hpp"
#include "scanner.hpp"

namespace Compiler
{
    StringParser::StringParser(ErrorHandler& errorHandler, const Context& context, Literals& literals)
        : Parser(errorHandler, context)
        , mLiterals(literals)
        , mSmashCase(false)
        , mDiscard(false)
    {
    }

    bool StringParser::parseName(const std::string& name, const TokenLoc& loc, Scanner& scanner)
    {
        start();
        mTokenLoc = loc;

        if (!mDiscard)
        {
            if (mSmashCase)
                Generator::pushString(mCode, mLiterals, Misc::StringUtils::lowerCase(name));
            else
                Generator::pushString(mCode, mLiterals, name);
        }

        return false;
    }

    bool StringParser::parseKeyword(int keyword, const TokenLoc& loc, Scanner& scanner)
    {
        if (const Extensions* extensions = getContext().getExtensions())
        {
            std::string argumentType; // ignored
            bool hasExplicit = false; // ignored
            if (extensions->isInstruction(keyword, argumentType, hasExplicit))
            {
                // pretend this is not a keyword
                std::string name = loc.mLiteral;
                if (name.size() >= 2 && name[0] == '"' && name[name.size() - 1] == '"')
                    name = name.substr(1, name.size() - 2);
                return parseName(name, loc, scanner);
            }
        }

        if (keyword == Scanner::K_end || keyword == Scanner::K_begin || keyword == Scanner::K_short
            || keyword == Scanner::K_long || keyword == Scanner::K_float || keyword == Scanner::K_if
            || keyword == Scanner::K_endif || keyword == Scanner::K_else || keyword == Scanner::K_elseif
            || keyword == Scanner::K_while || keyword == Scanner::K_endwhile || keyword == Scanner::K_return
            || keyword == Scanner::K_messagebox || keyword == Scanner::K_set || keyword == Scanner::K_to)
        {
            // pretend this is not a keyword
            std::string name = loc.mLiteral;
            if (name.size() >= 2 && name[0] == '"' && name[name.size() - 1] == '"')
                name = name.substr(1, name.size() - 2);
            return parseName(name, loc, scanner);
        }

        return Parser::parseKeyword(keyword, loc, scanner);
    }

    bool StringParser::parseInt(int value, const TokenLoc& loc, Scanner& scanner)
    {
        reportWarning("Treating integer argument as a string", loc);
        return parseName(loc.mLiteral, loc, scanner);
    }

    void StringParser::append(std::vector<Interpreter::Type_Code>& code)
    {
        std::copy(mCode.begin(), mCode.end(), std::back_inserter(code));
    }

    void StringParser::reset()
    {
        mCode.clear();
        mSmashCase = false;
        mTokenLoc = TokenLoc();
        mDiscard = false;
        Parser::reset();
    }

    void StringParser::smashCase()
    {
        mSmashCase = true;
    }

    const TokenLoc& StringParser::getTokenLoc() const
    {
        return mTokenLoc;
    }

    void StringParser::discard()
    {
        mDiscard = true;
    }
}