aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/featurenameparser.cpp
blob: 6177c117caa1ddb8a8f123ad5c9cbf8ad829eb32 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "featurenameparser.h"
#include "featurenamebuilder.h"
#include <vespa/vespalib/util/stringfmt.h>

#include <vespa/log/log.h>
LOG_SETUP(".fef.featurenameparser");

namespace {

//-----------------------------------------------------------------------------

int decodeHex(char c) {
    if (c >= '0' && c <= '9') {
        return (c - '0');
    }
    if (c >= 'a' && c <= 'f') {
        return ((c - 'a') + 10);
    }
    if (c >= 'A' && c <= 'F') {
        return ((c - 'A') + 10);
    }
    return -1;
}

//-----------------------------------------------------------------------------

template <typename A>
class IsLogged
{
private:
    A           _a;
    vespalib::string _name;

public:
    IsLogged(A a) : _a(a), _name(a.getName()) {}
    bool operator()(char c) {
        bool res = _a(c);
        LOG(info, "%s returned %s for char '%c'",
            _name.c_str(), res ? "true" : "false", c);
        return res;
    }
};

template <typename A>
class DoLog
{
private:
    A           _a;
    vespalib::string _name;

public:
    DoLog(A a) : _a(a), _name(a.getName()) {}
    bool operator()(char c) {
        bool res = _a(c);
        LOG(info, "%s returned %s for char '%c'",
            _name.c_str(), res ? "true" : "false", c);
        return res;
    }
    bool done() {
        bool res = _a.done();
        LOG(info, "%s returned %s on done signal",
            _name.c_str(), res ? "true" : "false");
        return res;
    }
};

//-----------------------------------------------------------------------------

template <typename A>
IsLogged<A> isLogged(A a) {
    return IsLogged<A>(a);
}

template <typename A>
DoLog<A> doLog(A a) {
    return DoLog<A>(a);
}

//-----------------------------------------------------------------------------

class ParseContext
{
private:
    const vespalib::string &_str;   // the input string
    uint32_t           _pos;   // current position
    char               _curr;  // current character, 0 means eos
    bool               _error; // flag indicating whether we have a parse error

public:
    ParseContext(const vespalib::string &in) : _str(in), _pos(0),
                                          _curr((in.empty()) ? 0 : in[0]),
                                          _error(false) {}
    uint32_t pos() const { return _pos; }
    char get() const { return _curr; }
    bool eos() const { return !_curr; }
    bool signalError() {
        _curr = 0; // also signals eos
        _error = true;
        return false;
    }
    bool error() {
        return _error;
    }
    void next() {
        if (eos()) {
            return;
        }
        if (++_pos < _str.size()) {
            _curr = _str[_pos];
        } else {
            _curr = 0;
        }
    }
    bool eatChar(char c) {
        if (get() != c) {
            return false;
        }
        next();
        return true;
    }
    template <typename CHECK, typename SINK>
    bool scan(CHECK check, SINK sink) {
        while (!eos()) {
            if (!check(get())) {
                break;
            }
            if (!sink(get())) {
                signalError();
            }
            next();
        }
        if (!sink.done()) {
            signalError();
        }
        return !error();
    }
};

//-----------------------------------------------------------------------------

class IsSpace
{
public:
    bool operator()(char c) const {
        switch (c) {
        case ' ':
        case '\t':
        case '\n':
        case '\r':
        case '\f':
            return true;
        default:
            return false;
        }
    }
    vespalib::string getName() const { return "IsSpace"; }
};

class Ident
{
public:
    Ident() {
        for(size_t i(0), m(256); i < m; i++) { _valid[i] = false; }
        for(size_t i('a'), m('z'); i <= m; i++) { _valid[i] = true; }
        for(size_t i('A'), m('Z'); i <= m; i++) { _valid[i] = true; }
        for(size_t i('0'), m('9'); i <= m; i++) { _valid[i] = true; }
        _valid[uint8_t('_')] = true;
        _valid[uint8_t('+')] = true;
        _valid[uint8_t('-')] = true;
        _valid[uint8_t('$')] = true;
        _valid[uint8_t('@')] = true;
    }
    bool isValid(uint8_t c) {  return _valid[c]; }
private:
    bool _valid[256];
};

static Ident _G_ident;

class IsIdent
{
public:
    bool operator()(char c) const {
        return _G_ident.isValid(c);
    }
    vespalib::string getName() const { return "IsIdent"; }
};

class IsChar
{
private:
    char _c;

public:
    IsChar(char c) : _c(c) {}
    bool operator()(char c) const {
        return (c == _c);
    }
    vespalib::string getName() const { return vespalib::make_string("IsChar(%c)", _c); }
};

template <typename A>
class IsNot
{
private:
    A _a;

public:
    IsNot(A a) : _a(a) {}
    bool operator()(char c) {
        return !(_a(c));
    }
    vespalib::string getName() const { return vespalib::make_string("IsNot(%s)", _a.getName().c_str()); }
};

template <typename A, typename B>
class IsEither
{
private:
    A _a;
    B _b;

public:
    IsEither(A a, B b) : _a(a), _b(b) {}
    bool operator()(char c) {
        return (_a(c) || _b(c));
    }
    vespalib::string getName() const { return vespalib::make_string("IsEither(%s,%s)",
                                          _a.getName().c_str(), _b.getName().c_str()); }
};

class IsEndQuote
{
private:
    bool _escape;

public:
    IsEndQuote() : _escape(false) {}
    bool operator()(char c) {
        if (_escape) {
            _escape = false;
            return false;
        }
        if (c == '\\') {
            _escape = true;
            return false;
        }
        return (c == '"');
    }
    vespalib::string getName() const { return "IsEndQuote"; }
};

//-----------------------------------------------------------------------------

class DoIgnore
{
public:
    bool operator()(char) { return true; }
    bool done() { return true; }
    vespalib::string getName() const { return "doIgnore"; }
};

class DoSave
{
private:
    vespalib::string &_dst;

public:
    DoSave(vespalib::string &str) : _dst(str) {}
    bool operator()(char c) {
        _dst.push_back(c);
        return true;
    }
    bool done() { return !_dst.empty(); }
    vespalib::string getName() const { return "doSave"; }
};

class DoDequote
{
private:
    bool          _escape; // true means we are dequoting something
    int           _hex;    // how many hex numbers left to read
    unsigned char _c;      // save up hex decoded char here
    vespalib::string  &_dst;    // where to save the dequoted string

public:
    DoDequote(vespalib::string &str) : _escape(false), _hex(0), _c(0), _dst(str) {}
    bool operator()(char c) {
        if (_escape) {
            if (_hex > 0) {
                --_hex;
                int val = decodeHex(c);
                if (val < 0) {
                    return false;
                }
                _c |= ((val & 0xf) << (_hex * 4));
                if (_hex == 0) {
                    if (_c == 0) {
                        return false;
                    }
                    _dst.push_back(_c);
                    _escape = false;
                }
            } else {
                switch (c) {
                case '"':
                    _dst.push_back('\"');
                    _escape = false;
                    break;
                case '\\':
                    _dst.push_back('\\');
                    _escape = false;
                    break;
                case 't':
                    _dst.push_back('\t');
                    _escape = false;
                    break;
                case 'n':
                    _dst.push_back('\n');
                    _escape = false;
                    break;
                case 'r':
                    _dst.push_back('\r');
                    _escape = false;
                    break;
                case 'f':
                    _dst.push_back('\f');
                    _escape = false;
                    break;
                case 'x':
                    _hex = 2;
                    _c = 0;
                    break;
                default:
                    return false; // signal error
                }
            }
        } else {
            if (c == '\\') {
                _escape = true;
            } else {
                _dst.push_back(c); // normal case (no dequoting needed)
            }
        }
        return true;
    }
    bool done() { return !_escape; }
    vespalib::string getName() const { return "doDequote"; }
};

//-----------------------------------------------------------------------------

IsSpace isSpace() { return IsSpace(); }

IsIdent isIdent() { return IsIdent(); }

IsChar isChar(char c) { return IsChar(c); }

template <typename A>
IsNot<A> isNot(A a) {
    return IsNot<A>(a);
}

template <typename A, typename B>
IsEither<A, B> isEither(A a, B b) {
    return IsEither<A, B>(a, b);
}

IsEndQuote isEndQuote() { return IsEndQuote(); }

DoIgnore doIgnore() { return DoIgnore(); }

DoSave doSave(vespalib::string &str) { return DoSave(str); }

DoDequote doDequote(vespalib::string &str) { return DoDequote(str); }

//-----------------------------------------------------------------------------

// need forward declaration of this for recursive parsing
bool normalizeFeatureName(ParseContext &ctx, vespalib::string &name);

bool parseParameters(ParseContext &ctx, std::vector<vespalib::string> &parameters)
{
    ctx.scan(isSpace(), doIgnore());
    if (!ctx.eatChar('(')) {
        return true; // no parameters = ok
    }
    for (;;) {
        vespalib::string param;
        ctx.scan(isSpace(), doIgnore());
        switch (ctx.get()) {
        case ')':
        case ',':
            break;      // empty param
        case '"':       // parse param as quoted string
            ctx.next(); // eat opening '"'
            if (!ctx.scan(isNot(isEndQuote()), doDequote(param))) {
                return false;
            }
            if (!ctx.eatChar('"')) {   // missing end quote
                return ctx.signalError();
            }
            break;
        default:        // parse param as feature name
            if (!normalizeFeatureName(ctx, param)) {
                return false;
            }
            break;
        }
        parameters.push_back(param);
        ctx.scan(isSpace(), doIgnore());
        if (ctx.eatChar(')')) {         // done
            return true;
        } else if (!ctx.eatChar(',')) { // illegal param list
            return ctx.signalError();
        }
    }
}

bool parseOutput(ParseContext &ctx, vespalib::string &output)
{
    ctx.scan(isSpace(), doIgnore());
    if (!ctx.eatChar('.')) {
        return true; // output is optional
    }
    ctx.scan(isSpace(), doIgnore());
    return ctx.scan(isEither(isIdent(), isChar('.')), doSave(output));
}

bool parseFeatureName(ParseContext &ctx, vespalib::string &baseName,
                      std::vector<vespalib::string> &parameters, vespalib::string &output)
{
    return (ctx.scan(isIdent(), doSave(baseName)) &&
            parseParameters(ctx, parameters) &&
            parseOutput(ctx, output));
}

bool normalizeFeatureName(ParseContext &ctx, vespalib::string &name) {
    vespalib::string              baseName;
    std::vector<vespalib::string> params;
    vespalib::string              output;
    if (!parseFeatureName(ctx, baseName, params, output)) {
        return false;
    }
    search::fef::FeatureNameBuilder builder;
    builder.baseName(baseName);
    for (uint32_t i = 0; i < params.size(); ++i) {
        builder.parameter(params[i]);
    }
    builder.output(output);
    name = builder.buildName();
    return true;
}

} // namespace <unnamed>

namespace search {
namespace fef {

FeatureNameParser::FeatureNameParser(const string &input)
    : _valid(false),
      _endPos(0),
      _baseName(),
      _parameters(),
      _output(),
      _executorName(),
      _featureName()
{
    ParseContext ctx(input);
    ctx.scan(isSpace(), doIgnore());
    _valid = parseFeatureName(ctx, _baseName, _parameters, _output);
    ctx.scan(isSpace(), doIgnore());
    if (!ctx.eos()) {
        _valid = ctx.signalError();
    }
    _endPos = ctx.pos();
    if (_valid && ctx.eos()) {
        FeatureNameBuilder builder;
        builder.baseName(_baseName);
        for (uint32_t i = 0; i < _parameters.size(); ++i) {
            builder.parameter(_parameters[i]);
        }
        _executorName = builder.buildName();
        builder.output(_output);
        _featureName = builder.buildName();
    } else {
        _baseName = "";
        {
            StringVector tmp;
            _parameters.swap(tmp);
        }
        _output = "";
    }
}

FeatureNameParser::~FeatureNameParser() { }


} // namespace fef
} // namespace search