Repository navigation
Expand file tree
/
Copy pathCursor.java
More file actions
183 lines (148 loc) · 4.24 KB
/
Copy pathCursor.java
File metadata and controls
183 lines (148 loc) · 4.24 KB
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
package org.codejive.properties;
import java.util.List;
import java.util.function.Predicate;
public class Cursor {
private final List<PropertiesParser.Token> tokens;
private int index;
public static Cursor index(List<PropertiesParser.Token> tokens, int index) {
return new Cursor(tokens, index);
}
public static Cursor first(List<PropertiesParser.Token> tokens) {
return new Cursor(tokens, tokens.isEmpty() ? -1 : 0);
}
public static Cursor last(List<PropertiesParser.Token> tokens) {
return new Cursor(tokens, tokens.size() - 1);
}
private Cursor(List<PropertiesParser.Token> tokens, int index) {
this.tokens = tokens;
this.index = index;
}
public boolean atStart() {
return index < 0;
}
public int position() {
return index;
}
public boolean hasToken() {
return hasToken(index);
}
private boolean hasToken(int idx) {
return idx >= 0 && idx < tokens.size();
}
public PropertiesParser.Token token() {
return tokens.get(index);
}
public String raw() {
return token().getRaw();
}
public String text() {
return token().getText();
}
public PropertiesParser.Type type() {
return token().getType();
}
public boolean isType(PropertiesParser.Type... types) {
if (index >= 0 && index < tokens.size()) {
for (PropertiesParser.Type t : types) {
if (t == tokens.get(index).getType()) {
return true;
}
}
}
return false;
}
public boolean isWhitespace() {
return isType(PropertiesParser.Type.WHITESPACE) && !token().isEol();
}
public boolean isEol() {
return isType(PropertiesParser.Type.WHITESPACE) && token().isEol();
}
public Cursor prev() {
return skip(-1);
}
public Cursor next() {
return skip(1);
}
public Cursor skip(int steps) {
index += steps;
if (index < -1) {
index = -1;
} else if (index > tokens.size()) {
index = tokens.size();
}
return this;
}
public boolean nextIf(PropertiesParser.Type type) {
return nextIf(t -> t.getType() == type);
}
public boolean nextIf(Predicate<PropertiesParser.Token> accept) {
if (hasToken() && accept.test(token())) {
next();
return true;
} else {
return false;
}
}
public Cursor nextWhile(Predicate<PropertiesParser.Token> accept) {
while (nextIf(accept)) {}
return this;
}
public int nextCount(Predicate<PropertiesParser.Token> accept) {
int cnt = 0;
while (nextIf(accept)) {
cnt++;
}
return cnt;
}
public boolean prevIf(PropertiesParser.Type type) {
return prevIf(t -> t.getType() == type);
}
public boolean prevIf(Predicate<PropertiesParser.Token> accept) {
if (hasToken() && accept.test(token())) {
prev();
return true;
} else {
return false;
}
}
public Cursor prevWhile(Predicate<PropertiesParser.Token> accept) {
while (prevIf(accept)) {}
return this;
}
public int prevCount(Predicate<PropertiesParser.Token> accept) {
int cnt = 0;
while (prevIf(accept)) {
cnt++;
}
return cnt;
}
public Cursor add(PropertiesParser.Token token) {
index = Math.max(index, 0);
addToken(index++, token);
return this;
}
public Cursor addEol() {
return add(PropertiesParser.Token.EOL);
}
private void addToken(int index, PropertiesParser.Token token) {
if (hasToken(index)) {
tokens.add(index, token);
} else {
tokens.add(token);
}
}
public Cursor replace(PropertiesParser.Token token) {
tokens.set(index, token);
return this;
}
public void remove() {
tokens.remove(index);
}
public Cursor copy() {
return Cursor.index(tokens, index);
}
@Override
public String toString() {
return (hasToken() ? token() + " " : "") + "@" + position();
}
}