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

import java.util.List;

/**
 * An iterator-like view of a list of tokens, but typed, random-accessible
 * and with more convenience methods
 *
 * @author bratseth
 */
final class TokenPosition {

    private List<Token> tokenList;

    private int position = 0;

    /**
     * Creates an empty token position which must be {@link #initialize initialized}
     * before use
     */
    public TokenPosition() {}

    /**
     * Initializes this token position. Must be done once or more before use
     *
     * @param tokens a list of tokens, which is not modified, and not used
     *        outside the calling thread
     */
    public void initialize(List<Token> tokens) {
        this.tokenList = tokens;
        position = 0;
    }

    /**
     * Returns the current token without changing the position.
     * Returns null (no exception) if there are no more tokens.
     */
    public Token current() {
        return current(0);
    }

    /**
     * Returns the current token without changing the position,
     * and without ignoring spaces.
     * Returns null (no exception) if there are no more tokens.
     */
    public Token currentNoIgnore() {
        return currentNoIgnore(0);
    }

    /**
     * Returns the token at <code>offset</code> steps from here.
     * Null (no exception) if there is no token at that position
     */
    public Token current(int offset) {
        int i = position + offset;

        while (i < tokenList.size()) {
            Token token = tokenList.get(i++);

            if (token.kind != Token.Kind.SPACE) {
                return token;
            }
        }
        return null;
    }

    /**
     * Returns the token at <code>offset</code> steps from here,
     * without ignoring spaces.
     * Null (no exception) if there is no token at that position
     */
    public Token currentNoIgnore(int offset) {
        if (tokenList.size() <= position + offset) {
            return null;
        }
        return tokenList.get(position + offset);
    }

    /**
     * Returns whether the current token is of the given kind.
     * False also if there is no token at the current position
     */
    public boolean currentIs(Token.Kind kind) {
        Token current = current();

        if (current == null) {
            return false;
        }
        return current.kind == kind;
    }

    /**
     * Returns whether the current token is of the given kind,
     * without skipping spaces.
     * False also if there is no token at the current position
     */
    public boolean currentIsNoIgnore(Token.Kind kind) {
        Token current = currentNoIgnore();

        if (current == null) {
            return false;
        }
        return current.kind == kind;
    }

    /** Returns whether more tokens are available */
    public boolean hasNext() {
        return tokenList.size() > (position + 1);
    }

    /**
     * Returns the current token and increases the position by one.
     * Returns null (no exception) if there are no more tokens
     */
    public Token next() {
        // Go to the next-non-space. Then set token, then increase position by one
        while (position < tokenList.size()) {
            Token current = tokenList.get(position++);

            if (current.kind != Token.Kind.SPACE) {
                return current;
            }
        }
        return null;
    }

    /** Skips past the current token */
    public void skip() {
        next();
    }

    /** Skips to the next token, even if the next is a space */
    public void skipNoIgnore() {
        position++;
    }

    /** Sets the position */
    public void setPosition(int position) {
        this.position = position;
    }

    /** Returns the current position */
    public int getPosition() {
        return position;
    }

    /**
     * Skips one or more tokens of the given kind
     *
     * @return true if at least one was skipped, false if there was none
     */
    public boolean skipMultiple(Token.Kind kind) {
        boolean skipped = false;

        while (hasNext() && current().kind == kind) {
            skipped = true;
            skip();
        }
        return skipped;
    }

    /**
     * Skips one or more tokens of the given kind, without ignoring spaces
     *
     * @return true if at least one was skipped, false if there was none
     */
    public boolean skipMultipleNoIgnore(Token.Kind kind) {
        boolean skipped = false;

        while (hasNext() && currentNoIgnore().kind == kind) {
            skipped = true;
            skip();
        }
        return skipped;
    }

    /**
     * Skips one or zero items of the given kind.
     *
     * @return true if one item was skipped, false if none was, or if there are no more tokens
     */
    public boolean skip(Token.Kind kind) {
        Token current = current();

        if (current == null || current.kind != kind) {
            return false;
        }

        skip();
        return true;
    }

    /**
     * Skips one or zero items of the given kind, without ignoring spaces
     *
     * @return true if one item was skipped, false if none was or if there are no more tokens
     */
    public boolean skipNoIgnore(Token.Kind kind) {
        Token current = currentNoIgnore();

        if (current == null || current.kind != kind) return false;

        skipNoIgnore();
        return true;
    }
    
    @Override
    public String toString() {
        return "token " + current();
    }

}