File: rygel-search-criteria-parser.vala

package info (click to toggle)
rygel 0.36.2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 40,656 kB
  • sloc: ansic: 237,502; sh: 4,986; xml: 1,706; makefile: 1,203; sed: 16
file content (238 lines) | stat: -rw-r--r-- 8,866 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2009 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using GUPnP;
using Gee;

errordomain Rygel.SearchCriteriaError {
    SYNTAX_ERROR
}

private enum Rygel.SearchCriteriaSymbol {
    EQ = SearchCriteriaOp.EQ,
    NEQ,
    LESS,
    LEQ,
    GREATER,
    GEQ,
    CONTAINS,
    DOES_NOT_CONTAIN,
    DERIVED_FROM,
    EXISTS,

    ASTERISK,
    AND,
    OR,
    TRUE,
    FALSE
}

private struct Rygel.SearchCriteriaToken {
    public string str_symbol;
    public SearchCriteriaSymbol symbol;
}

/**
 * Parses a search criteria string and creates SearchExpression as a result.
 */
internal class Rygel.SearchCriteriaParser : Object, StateMachine {
    // The original string representation received from client
    public string str;
    public SearchExpression expression; // The root expression
    public Error err;

    public Cancellable cancellable { get; set; }

    private SearchCriteriaSymbol token {
        get {
            return (SearchCriteriaSymbol) this.scanner.token;
        }
    }

    private string context {
        owned get {
            return this.scanner.line.to_string () + "." +
                   this.scanner.position.to_string ();
        }
    }

    private Scanner scanner;

    private const SearchCriteriaToken[] TOKENS = {
        { "=",              SearchCriteriaSymbol.EQ },
        { "!=",             SearchCriteriaSymbol.NEQ },
        { "<",              SearchCriteriaSymbol.LESS },
        { "<=",             SearchCriteriaSymbol.LEQ },
        { ">",              SearchCriteriaSymbol.GREATER },
        { ">=",             SearchCriteriaSymbol.GEQ },
        { "contains",       SearchCriteriaSymbol.CONTAINS },
        { "doesNotContain", SearchCriteriaSymbol.DOES_NOT_CONTAIN },
        { "derivedfrom",    SearchCriteriaSymbol.DERIVED_FROM },
        { "exists",         SearchCriteriaSymbol.EXISTS },
        { "*",              SearchCriteriaSymbol.ASTERISK },
        { "and",            SearchCriteriaSymbol.AND },
        { "or",             SearchCriteriaSymbol.OR },
        { "true",           SearchCriteriaSymbol.TRUE },
        { "false",          SearchCriteriaSymbol.FALSE }
    };

    public SearchCriteriaParser (string str) throws Error {
        this.str = str;
        scanner = new Scanner (null);
        scanner.config.cset_skip_characters = " \t\n\r\012" +
                                              "\013\014\015";
        scanner.config.scan_identifier_1char = true;
        scanner.config.cset_identifier_first = CharacterSet.a_2_z +
                                               "_*<>=!@" +
                                               CharacterSet.A_2_Z;
        scanner.config.cset_identifier_nth   = CharacterSet.a_2_z + "_" +
                                               CharacterSet.DIGITS + "=:@" +
                                               CharacterSet.A_2_Z +
                                               CharacterSet.LATINS +
                                               CharacterSet.LATINC;
        scanner.config.symbol_2_token        = true;

        foreach (var token in TOKENS) {
            scanner.scope_add_symbol (0,
                                      token.str_symbol,
                                      ((int) token.symbol).to_pointer ());
        }
    }

    // This implementation is not really async
    public async void run () {
        if (this.str == "*") {
            // Wildcard
            this.completed ();

            return;
        }

        this.scanner.input_text (this.str, (uint) this.str.length);
        this.scanner.get_next_token ();
        try {
            this.expression = this.parse_or_expression ();
        } catch (Error err) {
            this.err = err;
        }
        this.completed ();
    }

    private SearchExpression? parse_and_expression () throws Error {
        var exp = this.parse_rel_expression ();
        while (this.token == SearchCriteriaSymbol.AND) {
            this.scanner.get_next_token ();
            var exp2 = new LogicalExpression();
            exp2.operand1 = exp;
            exp2.op = LogicalOperator.AND;
            exp2.operand2 = this.parse_rel_expression ();
            exp = exp2;
        }

        return exp;
    }

    private SearchExpression? parse_rel_expression () throws Error {
        var exp = new RelationalExpression ();
        if (this.scanner.token == TokenType.IDENTIFIER) {
            exp.operand1 = this.scanner.value.identifier;
            this.scanner.get_next_token ();

            if (this.token == SearchCriteriaSymbol.EQ ||
                this.token == SearchCriteriaSymbol.NEQ ||
                this.token == SearchCriteriaSymbol.LESS ||
                this.token == SearchCriteriaSymbol.LEQ ||
                this.token == SearchCriteriaSymbol.GREATER ||
                this.token == SearchCriteriaSymbol.GEQ ||
                this.token == SearchCriteriaSymbol.CONTAINS ||
                this.token == SearchCriteriaSymbol.DOES_NOT_CONTAIN ||
                this.token == SearchCriteriaSymbol.DERIVED_FROM) {
                exp.op = (SearchCriteriaOp) this.scanner.token;
                this.scanner.get_next_token ();
               if (this.scanner.token == TokenType.STRING) {
                   exp.operand2 = this.scanner.value.string;
                   this.scanner.get_next_token ();

                   return exp;
               } else {
                    throw new SearchCriteriaError.SYNTAX_ERROR (this.context +
                                                                ": expected " +
                                                                "\"STRING\"");
               }
            } else if (this.token == SearchCriteriaSymbol.EXISTS) {
                exp.op = (SearchCriteriaOp) this.scanner.token;
                this.scanner.get_next_token ();
                if (this.token == SearchCriteriaSymbol.TRUE) {
                    exp.operand2 = "true";
                    this.scanner.get_next_token ();

                    return exp;
                } else if (this.token == SearchCriteriaSymbol.FALSE) {
                    exp.operand2 = "false";
                    this.scanner.get_next_token ();

                    return exp;
                } else {
                    throw new SearchCriteriaError.SYNTAX_ERROR (this.context +
                                                                ": expected " +
                                                                "\"true\"|\"" +
                                                                "false\"");
                }
            } else {
                throw new SearchCriteriaError.SYNTAX_ERROR (this.context +
                                                            ": expected " +
                                                            "operator");
            }
        } else if (this.scanner.token == TokenType.LEFT_PAREN) {
            this.scanner.get_next_token ();
            var exp2 = this.parse_or_expression ();
            if (this.scanner.token != TokenType.RIGHT_PAREN) {
                throw new SearchCriteriaError.SYNTAX_ERROR (this.context +
                                                            ": expected ')'");
            } else {
                this.scanner.get_next_token ();

                return exp2;
            }

        } else {
            throw new SearchCriteriaError.SYNTAX_ERROR (this.context +
                                                        ": expected " +
                                                        "identifier or '('");
        }
    }

    private SearchExpression? parse_or_expression () throws Error {
        var exp = this.parse_and_expression ();
        while (this.token == SearchCriteriaSymbol.OR) {
            this.scanner.get_next_token ();
            var exp2 = new LogicalExpression();
            exp2.operand1 = exp;
            exp2.op = LogicalOperator.OR;
            exp2.operand2 = this.parse_and_expression ();
            exp = exp2;
        }

        return exp;
    }

}