Debian Codegraph @

src/com/github/weisj/jsvg/util/ParserBase.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2023 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // This only checks for the rough structure of a number as we need to know
107 // when to separate the next token.
108 // Explicit parsing is done by Float#parseFloat.
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 // Break up combined zeros into multiple numbers.
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 // Possible exponent notation. Needs at least one preceding number
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 }