aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/yql/ParameterListParser.java
blob: 1993871aa4cc75c1ac421ce6f916117e5df0c588 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.yql;

import com.yahoo.prelude.query.WeightedSetItem;

import java.util.Arrays;

/**
 * Parser of parameter lists on the form {key:value, key:value} or [[key,value], [key,value], ...]
 *
 * @author bratseth
 */
class ParameterListParser {

    public static void addItemsFromString(String string, WeightedSetItem out) {
        var s = new ParsableString(string);
        switch (s.peek()) {
            case '[' : addArrayItems(s, out); break;
            case '{' : addMapItems(s, out); break;
            default : throw new IllegalArgumentException("Expected a string starting by '[' or '{', " +
                                                         "but was '" + s.peek() + "'");
        }
    }

    private static void addArrayItems(ParsableString s, WeightedSetItem out) {
        s.pass('[');
        while (s.peek() != ']') {
            s.pass('[');
            long key = s.longTo(s.position(','));
            s.pass(',');
            int value = s.intTo(s.position(']'));
            s.pass(']');
            out.addToken(key, value);
            s.passOptional(',');
            if (s.atEnd()) throw new IllegalArgumentException("Expected an array ending by ']'");
        }
        s.pass(']');
    }

    private static void addMapItems(ParsableString s, WeightedSetItem out) {
        s.pass('{');
        while (s.peek() != '}') {
            String key;
            if (s.passOptional('\'')) {
                key = s.stringTo(s.position('\''));
                s.pass('\'');
            }
            else if (s.passOptional('"')) {
                key = s.stringTo(s.position('"'));
                s.pass('"');
            }
            else {
                key = s.stringTo(s.position(':')).trim();
            }
            s.pass(':');
            int value = s.intTo(s.position(',','}'));
            out.addToken(key, value);
            s.passOptional(',');
            if (s.atEnd()) throw new IllegalArgumentException("Expected a map ending by '}'");
        }
        s.pass('}');
    }

    private static class ParsableString {

        int position = 0;
        String s;

        ParsableString(String s) {
            this.s = s;
        }

        /**
         * Returns the next non-space character or UNASSIGNED if we have reached the end of the string.
         * The current position is not changed.
         */
        char peek() {
            int localPosition = position;
            while (localPosition < s.length()) {
                char nextChar = s.charAt(localPosition++);
                if (!Character.isSpaceChar(nextChar))
                    return nextChar;
            }
            return Character.UNASSIGNED;
        }

        /**
         * Verifies that the next non-space character is the given and moves the position past it.
         *
         * @throws IllegalArgumentException if the next non-space character is not the given character
         */
        void pass(char character) {
            while (position < s.length()) {
                char nextChar = s.charAt(position++);
                if (!Character.isSpaceChar(nextChar)) {
                    if (nextChar == character)
                        return;
                    else
                        throw new IllegalArgumentException("Expected '" + character + "' at position " + (position-1) +
                                                           " but got '" + nextChar + "'");
                }
            }
            throw new IllegalArgumentException("Expected '" + character + "' at position " + (position-1) +
                                               " but reached the end");
        }

        /**
         * Checks if the next non-space character is the given and moves the position past it if so.
         * Does not change the position otherwise.
         *
         * @return true if the next non-space character was the given character
         */
        boolean passOptional(char character) {
            int localPosition = position;
            while (localPosition < s.length()) {
                char nextChar = s.charAt(localPosition++);
                if (!Character.isSpaceChar(nextChar)) {
                    if (nextChar == character) {
                        position = localPosition;
                        return true;
                    } else {
                        return false;
                    }
                }
            }
            return false;
        }

        /**
         * Returns the position of the next occurrence of any of the given characters.
         *
         * @throws IllegalArgumentException if there are no further occurrences of any of the given characters
         */
        int position(char ... characters) {
            int localPosition = position;
            while (localPosition < s.length()) {
                char nextChar = s.charAt(localPosition);
                for (char character : characters)
                    if (nextChar == character) return localPosition;
                localPosition++;
            }
            throw new IllegalArgumentException("Expected one of " + Arrays.toString(characters) + " after " + position);
        }

        boolean atEnd() {
            return position >= s.length();
        }

        /**
         * Returns the string value from the current to the given position, and moves the current
         * position to the next character.
         *
         * @throws IllegalArgumentException if end is beyond the last position of the string
         */
        String stringTo(int end) {
            try {
                String value = s.substring(position, end);
                position = end;
                return value;
            }
            catch (IndexOutOfBoundsException e) {
                throw new IllegalArgumentException(end + " is larger than the size of the string,  " + s.length());
            }
        }

        /**
         * Returns the int value from the current to the given position, and moves the current
         * position to the next character.
         *
         * @throws IllegalArgumentException if the string cannot be parsed to an int or end is larger than the string
         */
        int intTo(int end) {
            int start = position;
            String value = stringTo(end);
            try {
                return Integer.parseInt(value.trim());
            }
            catch (NumberFormatException e) {
                throw new IllegalArgumentException("Expected an integer between positions " + start + " and " + end +
                                                   ", but got " + value);
            }
        }

        /**
         * Returns the long value from the current to the given position, and moves the current
         * position to the next character.
         *
         * @throws IllegalArgumentException if the string cannot be parsed to a long or end is larger than the string
         */
        long longTo(int end) {
            int start = position;
            String value = stringTo(end);
            try {
                return Long.parseLong(value.trim());
            }
            catch (NumberFormatException e) {
                throw new IllegalArgumentException("Expected an integer between positions " + start + " and " + end +
                                                   ", but got " + value);
            }
        }

    }

}