summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/script/generator/main.cpp
blob: a841cbcae40804d726cfea25bbf809012c5aa9e9 (plain)
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
/****************************************************************************
**
** This file is part of the $PACKAGE_NAME$.
**
** Copyright (C) $THISYEAR$ $COMPANY_NAME$.
**
** $QT_EXTENDED_DUAL_LICENSE$
**
****************************************************************************/

#include <QList>
#include <QByteArray>


QT_BEGIN_NAMESPACE
struct Keyword {
    const char *lexem;
    const char *token;
};

struct State
{
    State(const char* token) : token(token)
    {
        ::memset(next, 0, sizeof(next));
    }
    State(const State &other) : token(other.token)
    {
        ::memcpy(next, other.next, sizeof(next));
    }
    State &operator=(const State &other)
    {
        token = other.token;
        ::memcpy(next, other.next, sizeof(next));
        return *this;
    }

    QByteArray token;
    int next[128];
};

Keyword keywords[] =
{
    {"<", "LANGLE" },
    {">", "RANGLE" },
    {"+", "PLUS" },
    {"-", "MINUS" },
    {"*", "STAR" },
    {"==", "EQUALS" },
    {"&&", "AND" },
    {".", "DOT"},
    {"true", "TOKEN_TRUE"},
    {"false", "TOKEN_FALSE"},
    {" ", "WHITESPACE"},
    {"\t", "WHITESPACE"},
    {0, 0}
};

bool is_character(char s)
{
    return (s >= 'a' && s <= 'z') ||
           (s >= 'A' && s <= 'Z') ||
           (s >= '0' && s <= '9') ||
           s == '_';
}

void newState(QList<State> &states, const char *token, const char *lexem)
{
    int state = 0;
    bool character = is_character(*lexem);

    while(*lexem) {
        int next = states[state].next[(int)*lexem];

        if (!next) {
            next = states.size();
            states += State(character?"CHARACTER":"INCOMPLETE");
            states[state].next[(int)*lexem] = next;
        }

        state = next;
        ++lexem;
        character = character && is_character(*lexem);
    }

    states[state].token = token;
}

void newState(QList<State> &states, const char *token, char lexem)
{
    int next = states[0].next[(int)lexem];
    if (!next) {
        next = states.size();
        states += State(token);
        states[0].next[(int)lexem] = next;
    } else {
        states[next].token = token;
    }
}

int main()
{
    QList<State> states;
    states += State("NOTOKEN");

    // identifiers
    for (int cc = 'a'; cc <= 'z'; ++cc)
        newState(states, "CHARACTER", cc);
    for (int cc = 'A'; cc <= 'Z'; ++cc)
        newState(states, "CHARACTER", cc);
    newState(states, "CHARACTER", '_');

    // add digits
    for (int cc = '0'; cc <= '9'; ++cc)
        newState(states, "DIGIT", cc);

    // keywords
    for (int ii = 0; keywords[ii].lexem; ++ii)
        newState(states, keywords[ii].token, keywords[ii].lexem);

    ::printf("static const struct\n{\n"
             "    Token token;\n"
             "    char next[128];\n"
             "} keywords[] = {\n");

    for (int ii = 0; ii < states.size(); ++ii) {
        printf("%s    { %s, { ", ii?",\n":"", states[ii].token.data());
        for (int jj = 0; jj < 128; jj++) 
            printf("%s%d", jj?",":"", states[ii].next[jj]);
        printf(" } }");
    }

    printf("\n};\n");
}
QT_END_NAMESPACE