| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.util; |
| 23 |
|
| 24 |
import org.jetbrains.annotations.NotNull; |
| 25 |
|
| 26 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 27 |
|
| 28 |
public class ParserBase { |
| 29 |
protected final @NotNull String input; |
| 30 |
private final int inputLength; |
| 31 |
private int index; |
| 32 |
|
| 33 |
public ParserBase(@NotNull String input, int startIndex) { |
| 34 |
this.input = input; |
| 35 |
this.inputLength = input.length(); |
| 36 |
this.index = startIndex; |
| 37 |
} |
| 38 |
|
| 39 |
protected char peek() { |
| 40 |
return input.charAt(index); |
| 41 |
} |
| 42 |
|
| 43 |
protected void consume() { |
| 44 |
index++; |
| 45 |
} |
| 46 |
|
| 47 |
public boolean hasNext() { |
| 48 |
return index < inputLength; |
| 49 |
} |
| 50 |
|
| 51 |
public void consumeWhiteSpaceOrSeparator() { |
| 52 |
while (hasNext() && isWhiteSpaceOrSeparator(peek())) { |
| 53 |
consume(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
private boolean isWhiteSpaceOrSeparator(char c) { |
| 58 |
return c == ',' || Character.isWhitespace(c); |
| 59 |
} |
| 60 |
|
| 61 |
private String nextNumberString() { |
| 62 |
int start = index; |
| 63 |
NumberCharState state = new NumberCharState(); |
| 64 |
while (hasNext() && isValidNumberChar(peek(), state)) { |
| 65 |
consume(); |
| 66 |
} |
| 67 |
int end = index; |
| 68 |
return input.substring(start, end); |
| 69 |
} |
| 70 |
|
| 71 |
protected float nextFloatOrUnspecified() { |
| 72 |
if (!hasNext()) return Length.UNSPECIFIED_RAW; |
| 73 |
return nextFloat(); |
| 74 |
} |
| 75 |
|
| 76 |
public float nextFloat() throws NumberFormatException { |
| 77 |
int start = index; |
| 78 |
String token = nextNumberString(); |
| 79 |
try { |
| 80 |
return Float.parseFloat(token); |
| 81 |
} catch (NumberFormatException e) { |
| 82 |
String msg = "Unexpected token '" + token + "' rest=" |
| 83 |
+ input.substring(start, Math.min(input.length(), start + 10)) |
| 84 |
+ currentLocation(); |
| 85 |
throw new IllegalStateException(msg, e); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public double nextDouble() throws NumberFormatException { |
| 90 |
int start = index; |
| 91 |
String token = nextNumberString(); |
| 92 |
try { |
| 93 |
return Double.parseDouble(token); |
| 94 |
} catch (NumberFormatException e) { |
| 95 |
String msg = "Unexpected token '" + token + "' rest=" |
| 96 |
+ input.substring(start, Math.min(input.length(), start + 10)) |
| 97 |
+ currentLocation(); |
| 98 |
throw new IllegalStateException(msg, e); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
protected @NotNull String currentLocation() { |
| 103 |
return "(index=" + index + " in input=" + input + ")"; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private boolean isValidNumberChar(char c, NumberCharState state) { |
| 110 |
boolean valid = '0' <= c && c <= '9'; |
| 111 |
if (valid && state.iteration == 1 && input.charAt(index - 1) == '0') { |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
state.signAllowed = state.signAllowed && !valid; |
| 116 |
if (state.dotAllowed && !valid) { |
| 117 |
valid = c == '.'; |
| 118 |
state.dotAllowed = !valid; |
| 119 |
} |
| 120 |
if (state.signAllowed && !valid) { |
| 121 |
valid = c == '+' || c == '-'; |
| 122 |
state.signAllowed = valid; |
| 123 |
} |
| 124 |
if (state.exponentAllowed && !valid) { |
| 125 |
|
| 126 |
valid = c == 'e' || c == 'E'; |
| 127 |
state.exponentAllowed = !valid; |
| 128 |
state.signAllowed = valid; |
| 129 |
state.dotAllowed = !valid; |
| 130 |
} |
| 131 |
state.iteration++; |
| 132 |
return valid; |
| 133 |
} |
| 134 |
|
| 135 |
private static final class NumberCharState { |
| 136 |
int iteration = 0; |
| 137 |
boolean dotAllowed = true; |
| 138 |
boolean signAllowed = true; |
| 139 |
boolean exponentAllowed = true; |
| 140 |
} |
| 141 |
} |