aboutsummaryrefslogtreecommitdiff
path: root/src/VersionParser.cpp
blob: eccd109ecb0486ba78e806ccb4cd39a87781507d (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
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
#include "../include/VersionParser.hpp"
#include "../include/StringView.hpp"

namespace sibs
{
    static int stringToIntNoVerify(const StringView &str)
    {
        int result = 0;
        for(usize i = 0; i < str.size; ++i)
        {
            result = (result * 10) + (str[i] - '0');
        }
        return result;
    }
    
    static bool isNum(char c)
    {
        return c >= '0' && c <= '9';
    }
    
    static bool isAlpha(char c)
    {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }

    Result<PackageVersion> parsePackageVersion(const StringView &versionStr, int *size)
    {
        PackageVersion result = { 0, 0, 0 };
        int *versionPtr = &result.major;
        int digitStart = 0;
        int i = 0;

        for(; i < (int)versionStr.size; ++i)
        {
            char c = versionStr[i];
            if(isNum(c))
            {
                if(digitStart == -1)
                    digitStart = i;
            }
            else
            {
                int length = i - digitStart;
                if(digitStart == -1 || length == 0)
                    return Result<PackageVersion>::Err("Package version string is in invalid format. Expected to be in format xxx.xxx.xxx where x is a number");

                *versionPtr = stringToIntNoVerify({ versionStr.data + digitStart, (usize)(i - digitStart) });
                bool endOfVersions = versionPtr == &result.patch;
                ++versionPtr;
                digitStart = -1;

                if(c != '.' || endOfVersions)
                    break;
            }
        }

        if(i == 0)
            return Result<PackageVersion>::Err("version can't be empty");

        if(digitStart != -1)
        {
            *versionPtr = stringToIntNoVerify({ versionStr.data + digitStart, (usize)(i - digitStart) });
            ++versionPtr;
        }

        if(size)
            *size = i;

        if(versionPtr == &result.major)
            return Result<PackageVersion>::Err("version can't be empty");

        return Result<PackageVersion>::Ok(result);
    }

    VersionTokenizer::VersionTokenizer() : 
        start(nullptr),
        code(nullptr),
        size(0),
        index(0)
    {

    }

    VersionTokenizer::VersionTokenizer(const char *_start, const usize _size) : 
        start(_start),
        code(_start),
        size(_size),
        index(0)
    {

    }

    VersionTokenizer::VersionTokenizer(const VersionTokenizer &other)
    {
        start = other.start;
        code = other.code;
        size = other.size;
        index = other.index;
    }

    VersionToken VersionTokenizer::next()
    {
        while(index < size)
        {
            char c = code[index];
            if(c == ' ' || c == '\t' || c == '\n' || c == '\r')
                ++index;
            else
                break;
        }

        if(index >= size)
            return VersionToken::END_OF_FILE;

        char c = code[index];
        if(isNum(c))
        {
            int versionStrSize = 0;
            identifier.data = code + index;
            Result<PackageVersion> packageVersion = parsePackageVersion({ code + index, (usize)(size - index) }, &versionStrSize);
            identifier.size = versionStrSize;
            index += versionStrSize;
            if(!packageVersion)
            {
                errMsg = packageVersion.getErrMsg();
                return VersionToken::INVALID;
            }
            version = packageVersion.unwrap();
            return VersionToken::VERSION_NUMBER;
        }
        else if(isAlpha(c))
        {
            usize identifierStart = index;
            ++index;
            while(index < size && isAlpha(code[index]))
            {
                ++index;
            }
            usize identifierEnd = index;
            usize identifierLength = identifierEnd - identifierStart;
            
            if(identifierLength == 3 && strncmp(code + identifierStart, "and", 3) == 0)
            {
                return VersionToken::AND;
            }
            else
            {
                errMsg = "Invalid identifier ";
                errMsg += std::string(code + identifierStart, identifierLength);
                return VersionToken::INVALID;
            }
        }
        else if(c == '<')
        {
            ++index;
            if(index < size && code[index] == '=')
            {
                ++index;
                operation = VersionOperation::LESS_EQUAL;
                return VersionToken::OPERATION;
            }
            operation = VersionOperation::LESS;
            return VersionToken::OPERATION;
        }
        else if(c == '=')
        {
            ++index;
            operation = VersionOperation::EQUAL;
            return VersionToken::OPERATION;
        }
        else if(c == '>')
        {
            ++index;
            if(index < size && code[index] == '=')
            {
                ++index;
                operation = VersionOperation::GREATER_EQUAL;
                return VersionToken::OPERATION;
            }
            operation = VersionOperation::GREATER;
            return VersionToken::OPERATION;
        }
        else
        {
            errMsg = "Unexpected character ";
            errMsg += c;
            return VersionToken::INVALID;
        }
    }

    Result<PackageVersionRange> VersionParser::parse(const char *code, const usize size)
    {
        versionRange = PackageVersionRange();
        tokenizer = VersionTokenizer(code, size);
        VersionToken token = parseStart();

        if(token == VersionToken::END_OF_FILE)
        {
            if(!versionRange.startDefined)
                return Result<PackageVersionRange>::Err("version can't be empty");
            
            if(versionRange.startOperation == VersionOperation::NONE)
            {
                versionRange.startOperation = VersionOperation::GREATER_EQUAL;
                versionRange.end.major = versionRange.start.major + 1;
                versionRange.end.minor = 0;
                versionRange.end.patch = 0;
                versionRange.endOperation = VersionOperation::LESS;
                versionRange.endDefined = true;
            }
            return Result<PackageVersionRange>::Ok(versionRange);
        }
        else if(token == VersionToken::INVALID)
            return Result<PackageVersionRange>::Err(tokenizer.errMsg);
        else
        {
            std::string errMsg = "Unexpected token '";
            switch(token)
            {
                case VersionToken::NONE:
                {
                    errMsg += "<none>";
                    break;
                }
                case VersionToken::OPERATION:
                {
                    errMsg += "operation ";
                    errMsg += asString(tokenizer.operation);
                    break;
                }
                case VersionToken::AND:
                {
                    errMsg += "and";
                    break;
                }
                case VersionToken::VERSION_NUMBER:
                {
                    errMsg += "version ";
                    errMsg += std::string(tokenizer.identifier.data, tokenizer.identifier.size);
                    break;
                }
                default:
                    break;
            }
            errMsg += "'";
            return Result<PackageVersionRange>::Err(errMsg);
        }
    }

    VersionToken VersionParser::parseStart()
    {
        VersionToken token = tokenizer.next();
        if(token == VersionToken::VERSION_NUMBER)
        {
            versionRange.startOperation = VersionOperation::NONE;
            versionRange.start = tokenizer.version;
            versionRange.startDefined = true;
            token = tokenizer.next();
            if(token == VersionToken::AND)
            {
                token = VersionToken::INVALID;
                tokenizer.errMsg = "Unexpected end version when start version does not have operation defined";
            }
        }
        else if(token == VersionToken::OPERATION)
        {
            versionRange.startOperation = tokenizer.operation;
            token = tokenizer.next();
            if(token == VersionToken::VERSION_NUMBER)
            {
                versionRange.start = tokenizer.version;
                versionRange.startDefined = true;
                switch(versionRange.startOperation)
                {
                    case VersionOperation::LESS:
                    {
                        token = VersionToken::INVALID;
                        tokenizer.errMsg = "Unexpected version end when start version is expected to less than ";
                        tokenizer.errMsg += versionRange.start.toString();
                        return token;
                    }
                    case VersionOperation::LESS_EQUAL:
                    {
                        token = VersionToken::INVALID;
                        tokenizer.errMsg = "Unexpected version end when start version is expected to be less or equal to ";
                        tokenizer.errMsg += versionRange.start.toString();
                        return token;
                    }
                    case VersionOperation::EQUAL:
                    {
                        token = VersionToken::INVALID;
                        tokenizer.errMsg = "Unexpected version end when start version is expected to be exactly ";
                        tokenizer.errMsg += versionRange.start.toString();
                        return token;
                    }
                    default:
                        break;
                }
                
                token = tokenizer.next();
                if(token == VersionToken::AND)
                {
                    return parseEnd();
                }
            }
            else if(token == VersionToken::INVALID)
                return token;
            else
            {
                token = VersionToken::INVALID;
                tokenizer.errMsg = "Expected version after operation";
            }
        }
        return token;
    }

    VersionToken VersionParser::parseEnd()
    {
        VersionToken token = tokenizer.next();
        if(token == VersionToken::OPERATION)
        {
            versionRange.endOperation = tokenizer.operation;
            switch(versionRange.endOperation)
            {
                case VersionOperation::EQUAL:
                case VersionOperation::GREATER:
                case VersionOperation::GREATER_EQUAL:
                {
                    token = VersionToken::INVALID;
                    tokenizer.errMsg = "End version can only have operations < and <=";
                    return token;
                }
                default:
                    break;
            }

            token = tokenizer.next();
            if(token == VersionToken::VERSION_NUMBER)
            {
                versionRange.end = tokenizer.version;
                versionRange.endDefined = true;
                if(versionRange.end <= versionRange.start)
                {
                    token = VersionToken::INVALID;
                    tokenizer.errMsg = "Expected version end to be greater than ";
                    tokenizer.errMsg += versionRange.start.toString();
                    tokenizer.errMsg += ", was ";
                    tokenizer.errMsg += versionRange.end.toString();
                    return token;
                }
                token = tokenizer.next();
            }
            else if(token == VersionToken::INVALID)
                return token;
            else
            {
                token = VersionToken::INVALID;
                tokenizer.errMsg = "Expected version after operation";
            }
        }
        else
        {
            token = VersionToken::INVALID;
            tokenizer.errMsg = "Expected end version to have operation defined";
        }
        return token;
    }
}