|
| 1 | +package org.codejive.properties; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.function.Predicate; |
| 5 | + |
| 6 | +public class Cursor { |
| 7 | + private final List<PropertiesParser.Token> tokens; |
| 8 | + private int index; |
| 9 | + |
| 10 | + public static Cursor index(List<PropertiesParser.Token> tokens, int index) { |
| 11 | + return new Cursor(tokens, index); |
| 12 | + } |
| 13 | + |
| 14 | + public static Cursor first(List<PropertiesParser.Token> tokens) { |
| 15 | + return new Cursor(tokens, tokens.isEmpty() ? -1 : 0); |
| 16 | + } |
| 17 | + |
| 18 | + public static Cursor last(List<PropertiesParser.Token> tokens) { |
| 19 | + return new Cursor(tokens, tokens.size() - 1); |
| 20 | + } |
| 21 | + |
| 22 | + private Cursor(List<PropertiesParser.Token> tokens, int index) { |
| 23 | + this.tokens = tokens; |
| 24 | + this.index = index; |
| 25 | + } |
| 26 | + |
| 27 | + public boolean atStart() { |
| 28 | + return index < 0; |
| 29 | + } |
| 30 | + |
| 31 | + public int position() { |
| 32 | + return index; |
| 33 | + } |
| 34 | + |
| 35 | + public boolean hasToken() { |
| 36 | + return index >= 0 && index < tokens.size(); |
| 37 | + } |
| 38 | + |
| 39 | + public PropertiesParser.Token token() { |
| 40 | + return tokens.get(index); |
| 41 | + } |
| 42 | + |
| 43 | + public String raw() { |
| 44 | + return token().getRaw(); |
| 45 | + } |
| 46 | + |
| 47 | + public String text() { |
| 48 | + return token().getText(); |
| 49 | + } |
| 50 | + |
| 51 | + public PropertiesParser.Type type() { |
| 52 | + return token().getType(); |
| 53 | + } |
| 54 | + |
| 55 | + public boolean isType(PropertiesParser.Type... types) { |
| 56 | + if (index >= 0 && index < tokens.size()) { |
| 57 | + for (PropertiesParser.Type t : types) { |
| 58 | + if (t == tokens.get(index).getType()) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + public boolean isWhitespace() { |
| 67 | + return isType(PropertiesParser.Type.WHITESPACE) && !token().isEol(); |
| 68 | + } |
| 69 | + |
| 70 | + public boolean isEol() { |
| 71 | + return isType(PropertiesParser.Type.WHITESPACE) && token().isEol(); |
| 72 | + } |
| 73 | + |
| 74 | + public Cursor prev() { |
| 75 | + return skip(-1); |
| 76 | + } |
| 77 | + |
| 78 | + public Cursor next() { |
| 79 | + return skip(1); |
| 80 | + } |
| 81 | + |
| 82 | + public Cursor skip(int steps) { |
| 83 | + index += steps; |
| 84 | + if (index < -1) { |
| 85 | + index = -1; |
| 86 | + } else if (index > tokens.size()) { |
| 87 | + index = tokens.size(); |
| 88 | + } |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public boolean nextIf(PropertiesParser.Type type) { |
| 93 | + return nextIf(t -> t.getType() == type); |
| 94 | + } |
| 95 | + |
| 96 | + public boolean nextIf(Predicate<PropertiesParser.Token> accept) { |
| 97 | + if (hasToken() && accept.test(token())) { |
| 98 | + return next().hasToken(); |
| 99 | + } else { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public Cursor nextWhile(Predicate<PropertiesParser.Token> accept) { |
| 105 | + while (nextIf(accept)) {} |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + public int nextCount(Predicate<PropertiesParser.Token> accept) { |
| 110 | + int cnt = 0; |
| 111 | + while (nextIf(accept)) { |
| 112 | + cnt++; |
| 113 | + } |
| 114 | + return cnt; |
| 115 | + } |
| 116 | + |
| 117 | + public boolean prevIf(PropertiesParser.Type type) { |
| 118 | + return prevIf(t -> t.getType() == type); |
| 119 | + } |
| 120 | + |
| 121 | + public boolean prevIf(Predicate<PropertiesParser.Token> accept) { |
| 122 | + if (hasToken() && accept.test(token())) { |
| 123 | + return prev().hasToken(); |
| 124 | + } else { |
| 125 | + return false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public Cursor prevWhile(Predicate<PropertiesParser.Token> accept) { |
| 130 | + while (prevIf(accept)) {} |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public int prevCount(Predicate<PropertiesParser.Token> accept) { |
| 135 | + int cnt = 0; |
| 136 | + while (prevIf(accept)) { |
| 137 | + cnt++; |
| 138 | + } |
| 139 | + return cnt; |
| 140 | + } |
| 141 | + |
| 142 | + public Cursor add(PropertiesParser.Token token) { |
| 143 | + addToken(index++, token); |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public Cursor addEol() { |
| 148 | + return add(PropertiesParser.Token.EOL); |
| 149 | + } |
| 150 | + |
| 151 | + private void addToken(int index, PropertiesParser.Token token) { |
| 152 | + if (hasToken()) { |
| 153 | + tokens.add(index, token); |
| 154 | + } else { |
| 155 | + tokens.add(token); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public Cursor replace(PropertiesParser.Token token) { |
| 160 | + tokens.set(index, token); |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + public void remove() { |
| 165 | + tokens.remove(index); |
| 166 | + } |
| 167 | + |
| 168 | + public Cursor copy() { |
| 169 | + return Cursor.index(tokens, index); |
| 170 | + } |
| 171 | +} |
0 commit comments