-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
ParsingExceptionImpl.java
168 lines (153 loc) · 4.81 KB
/
ParsingExceptionImpl.java
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
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.minimessage.internal.parser;
import java.util.Arrays;
import net.kyori.adventure.text.minimessage.ParsingException;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* An exception that happens while parsing.
*
* @since 4.10.0
*/
@ApiStatus.Internal
public class ParsingExceptionImpl extends ParsingException {
private static final long serialVersionUID = 2507190809441787202L;
private final String originalText;
private Token @NotNull [] tokens;
private final boolean withStackTrace;
/**
* Create a new parsing exception.
*
* @param message the detail message
* @param originalText the original text which was parsed
* @param tokens the token which caused the error
* @since 4.10.0
*/
public ParsingExceptionImpl(
final String message,
final @Nullable String originalText,
final @NotNull Token @NotNull ... tokens
) {
super(message);
this.tokens = tokens;
this.originalText = originalText;
this.withStackTrace = false;
}
/**
* Create a new parsing exception.
*
* @param message the detail message
* @param originalText the original text which was parsed
* @param cause the cause
* @param withStackTrace whether to generate a stacktrace
* @param tokens the token which caused the error
* @since 4.10.0
*/
public ParsingExceptionImpl(
final String message,
final @Nullable String originalText,
final @Nullable Throwable cause,
final boolean withStackTrace,
final @NotNull Token @NotNull ... tokens
) {
super(message, cause);
this.tokens = tokens;
this.originalText = originalText;
this.withStackTrace = withStackTrace;
}
@Override
public String getMessage() {
final String arrowInfo = this.tokens().length != 0
? "\n\t" + this.arrow()
: "";
final String messageInfo = this.originalText() != null
? "\n\t" + this.originalText() + arrowInfo
: "";
return super.getMessage() + messageInfo;
}
@Override
public @Nullable String detailMessage() {
return super.getMessage();
}
/**
* Get the message which caused this exception.
*
* @return the original message
* @since 4.10.0
*/
@Override
public @Nullable String originalText() {
return this.originalText;
}
/**
* Gets the tokens associated with this parsing error.
*
* @return the tokens for this error
* @since 4.10.0
*/
public @NotNull Token @NotNull [] tokens() {
return this.tokens;
}
/**
* Sets the tokens associated with this parsing error.
*
* @param tokens the tokens for this error
* @since 4.10.0
*/
public void tokens(final @NotNull Token @NotNull [] tokens) {
this.tokens = tokens;
}
private String arrow() {
final @NotNull Token[] ts = this.tokens();
final char[] chars = new char[ts[ts.length - 1].endIndex()];
int i = 0;
for (final Token t : ts) {
Arrays.fill(chars, i, t.startIndex(), ' ');
chars[t.startIndex()] = '^';
Arrays.fill(chars, t.startIndex() + 1, t.endIndex() - 1, '~');
chars[t.endIndex() - 1] = '^';
i = t.endIndex();
}
return new String(chars);
}
@Override
public Throwable fillInStackTrace() {
if (this.withStackTrace) {
return super.fillInStackTrace();
}
return this;
}
@Override
public int startIndex() {
if (this.tokens.length == 0) return LOCATION_UNKNOWN;
return this.tokens[0].startIndex();
}
@Override
public int endIndex() {
if (this.tokens.length == 0) return LOCATION_UNKNOWN;
return this.tokens[this.tokens.length - 1].endIndex();
}
}