Debian Codegraph @

net/sf/saxon/expr/parser/RoleDiagnostic.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.parser;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.Literal;
12 import net.sf.saxon.expr.StaticProperty;
13 import net.sf.saxon.om.AxisInfo;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.trans.Err;
16 import net.sf.saxon.tree.util.FastStringBuffer;
17 import net.sf.saxon.type.ItemType;
18 import net.sf.saxon.type.TypeHierarchy;
19 import net.sf.saxon.type.UType;
20 import net.sf.saxon.value.SequenceType;
21
22 import java.util.Optional;
23
24 /**
25 * A RoleDiagnostic (formerly RoleLocator) identifies the role in which an expression is used,
26 * for example as the third argument of the concat() function. This information is stored in an
27 * ItemChecker or CardinalityChecker so that good diagnostics can be
28 * achieved when run-time type errors are detected.
29 */
30 public class RoleDiagnostic {
31
32 private int kind;
33 private String operation;
34 private int operand;
35 private String errorCode = "XPTY0004"; // default error code for type errors
36
37 public static final int FUNCTION = 0;
38 public static final int BINARY_EXPR = 1;
39 public static final int TYPE_OP = 2;
40 public static final int VARIABLE = 3;
41 public static final int INSTRUCTION = 4;
42 public static final int FUNCTION_RESULT = 5;
43 public static final int ORDER_BY = 6;
44 public static final int TEMPLATE_RESULT = 7;
45 public static final int PARAM = 8;
46 public static final int UNARY_EXPR = 9;
47 public static final int UPDATING_EXPR = 10;
48 //public static final int GROUPING_KEY = 11; // 9.8 and earlier
49 public static final int EVALUATE_RESULT = 12;
50 public static final int CONTEXT_ITEM = 13;
51 public static final int AXIS_STEP = 14;
52 public static final int OPTION = 15;
53 public static final int CHARACTER_MAP_EXPANSION = 16;
54 //public static final int DOCUMENT_ORDER = 17; // 9.8 and earlier
55 //public static final int MAP_CONSTRUCTOR = 18; // 9.8 and earlier
56 public static final int MATCH_PATTERN = 19;
57 public static final int MISC = 20;
58
59
60 /**
61 * Create information about the role of a subexpression within its parent expression
62 *
63 * @param kind the kind of parent expression, e.g. a function call or a variable reference
64 * @param operation the name of the object in the parent expression, e.g. a function name or
65 * instruction name. A QName is provided in display form, prefix:local.
66 * For a string, the special format element/attribute is recognized, for example xsl:for-each/select,
67 * to identify the role of an XPath expression in a stylesheet.
68 * @param operand Ordinal position of this subexpression, e.g. the position of an argument in
69 * a function call
70 */
71
72
73
74 public RoleDiagnostic(int kind, String operation, int operand) {
75 this.kind = kind;
76 this.operation = operation;
77 this.operand = operand;
78 }
79
80 /**
81 * Set the error code to be produced if a type error is detected
82 *
83 * @param code The error code
84 */
85
86 public void setErrorCode(/*@Nullable*/ String code) {
87 if (code != null) {
88 this.errorCode = code;
89 }
90 }
91
92 /**
93 * Get the error code to be produced if a type error is detected
94 *
95 * @return code The error code
96 */
97
98 public String getErrorCode() {
99 return errorCode;
100 }
101
102 /**
103 * Ask whether the error code represents a type error
104 *
105 * @return true if the error is treated as a type error
106 */
107
108 public boolean isTypeError() {
109 return !errorCode.startsWith("FORG") && !errorCode.equals("XPDY0050");
110 }
111
112 /**
113 * Construct and return the error message indicating a type error
114 *
115 * @return the constructed error message
116 */
117 public String getMessage() {
118 String name = operation;
119
120 switch (kind) {
121 case FUNCTION:
122 if (name.equals("saxon:call") || name.equals("saxon:apply")) {
123 if (operand == 0) {
124 return "target of the dynamic function call";
125 } else {
126 return ordinal(operand) + " argument of the dynamic function call";
127 }
128 } else {
129 return ordinal(operand + 1) + " argument of " +
130 (name.isEmpty() ? "the anonymous function" : name + "()");
131 }
132 case BINARY_EXPR:
133 return ordinal(operand + 1) + " operand of '" + name + '\'';
134 case UNARY_EXPR:
135 return "operand of '-'";
136 case TYPE_OP:
137 return "value in '" + name + "' expression";
138 case VARIABLE:
139 if (name.equals("saxon:context-item")) {
140 return "context item";
141 } else {
142 return "value of variable $" + name;
143 }
144 case INSTRUCTION:
145 int slash = name.indexOf('/');
146 String attributeName = "";
147 if (slash >= 0) {
148 attributeName = name.substring(slash + 1);
149 name = name.substring(0, slash);
150 }
151 return "@" + attributeName + " attribute of " + (name.equals("LRE") ? "a literal result element" : name);
152 case FUNCTION_RESULT:
153 if (name.isEmpty()) {
154 return "result of the anonymous function";
155 } else {
156 return "result of a call to " + name;
157 }
158 case TEMPLATE_RESULT:
159 return "result of template " + name;
160 case ORDER_BY:
161 return ordinal(operand + 1) + " sort key";
162 case PARAM:
163 return "value of parameter $" + name;
164 case UPDATING_EXPR:
165 return "value of the " + ordinal(operand + 1) + " operand of " + name + " expression";
166 case EVALUATE_RESULT:
167 return "result of the expression {" + name + "} evaluated by xsl:evaluate";
168 case CONTEXT_ITEM:
169 return "context item";
170 case AXIS_STEP:
171 return "context item for the " + AxisInfo.axisName[operand] + " axis";
172 case OPTION:
173 return "value of the " + name + " option";
174 case CHARACTER_MAP_EXPANSION:
175 return "substitute value for character '" + name + "' in the character map";
176 case MATCH_PATTERN:
177 return "match pattern";
178 case MISC:
179 return operation;
180 default:
181 return "";
182 }
183 }
184
185 /**
186 * Construct the part of the message giving the required item type
187 *
188 * @param requiredItemType the item type required by the context of a particular expression
189 * @return a message of the form "Required item type of X is Y"
190 */
191
192 public String composeRequiredMessage(ItemType requiredItemType) {
193 return "The required item type of the " + getMessage() +
194 " is " + requiredItemType;
195 }
196
197 /**
198 * Construct a full error message
199 *
200 * @param requiredItemType the item type required by the context of a particular expression
201 * @param suppliedItemType the item type inferred by static analysis of an expression
202 * @return a message of the form "Required item type of A is R; supplied value has item type S"
203 */
204
205 public String composeErrorMessage(ItemType requiredItemType, ItemType suppliedItemType) {
206 return composeRequiredMessage(requiredItemType) +
207 "; supplied value has item type " +
208 suppliedItemType;
209 }
210
211 /**
212 * Construct a full error message, containing the supplied expression (for use when
213 * the error is reported statically)
214 *
215 * @param requiredItemType the item type required by the context of a particular expression
216 * @param supplied the supplied expression
217 * @return a message of the form "Required item type of A is R; supplied value has item type S"
218 */
219
220 public String composeErrorMessage(ItemType requiredItemType, Expression supplied, TypeHierarchy th) {
221 if (supplied instanceof Literal) {
222 String s = composeRequiredMessage(requiredItemType);
223 Optional<String> more = SequenceType.makeSequenceType(requiredItemType, StaticProperty.ALLOWS_ZERO_OR_MORE)
224 .explainMismatch(((Literal)supplied).getValue(), th);
225 if (more.isPresent()) {
226 s = s + ". " + more.get();
227 }
228 return s;
229 }
230 return composeRequiredMessage(requiredItemType) +
231 "; supplied expression (" + supplied.toShortString() + ") has item type " +
232 supplied.getItemType();
233 }
234
235
236 /**
237 * Construct a full error message, displaying the item in error (suitable for use when
238 * a type error is reported dynamically)
239 *
240 * @param requiredItemType the item type required by the context of a particular expression
241 * @param item the actual item in error. Must NOT be null (unlike earlier releases).
242 * @return a message of the form "Required item type of A is R; supplied value has item type S"
243 */
244
245 public String composeErrorMessage(ItemType requiredItemType, Item item, TypeHierarchy th) {
246
247 FastStringBuffer message = new FastStringBuffer(256);
248 message.append(composeRequiredMessage(requiredItemType));
249 message.append("; the supplied value ");
250 message.append(Err.depict(item));
251
252 if (requiredItemType.getGenre() != item.getGenre()) {
253 message.append(" is ");
254 message.append(item.getGenre().getDescription());
255 } else {
256 message.append(" does not match. ");
257 if (th != null) {
258 Optional<String> more = requiredItemType.explainMismatch(item, th);
259 more.ifPresent(message::append);
260 }
261 }
262 return message.toString();
263 }
264
265
266 /**
267 * Construct a full error message, in terms of UTypes
268 *
269 * @param requiredItemType the item type required by the context of a particular expression
270 * @param suppliedItemType the item type inferred by static analysis of an expression
271 * @return a message of the form "Required item type of A is R; supplied value has item type S"
272 */
273
274 public String composeErrorMessage(ItemType requiredItemType, UType suppliedItemType) {
275 return composeRequiredMessage(requiredItemType) +
276 "; supplied value has item type " +
277 suppliedItemType;
278 }
279
280 /**
281 * Save as a string, for use when serializing the expression tree
282 * @return a string representation of the object
283 */
284
285 public String save() {
286 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
287 fsb.append(kind + "|");
288 fsb.append(operand + "|");
289 fsb.append(errorCode.equals("XPTY0004") ? "" : errorCode);
290 fsb.append("|");
291 fsb.append(operation);
292 return fsb.toString();
293 }
294
295 /**
296 * Reconstruct from a saved string
297 * @param in the saved string representation of the RoleDiagnostic
298 */
299
300 public static RoleDiagnostic reconstruct(String in) {
301 int v = in.indexOf('|');
302 int kind = Integer.parseInt(in.substring(0, v));
303 int w = in.indexOf('|', v+1);
304 int operand = Integer.parseInt(in.substring(v+1, w));
305 int x = in.indexOf('|', w+1);
306 String errorCode = in.substring(w+1, x);
307 String operation = in.substring(x+1);
308 RoleDiagnostic cd = new RoleDiagnostic(kind, operation, operand);
309 if (!errorCode.isEmpty()) {
310 cd.setErrorCode(errorCode);
311 }
312 return cd;
313 }
314
315 /**
316 * Get the ordinal representation of a number (used to identify which argument of a function
317 * is in error)
318 *
319 * @param n the cardinal number
320 * @return the ordinal representation
321 */
322 public static String ordinal(int n) {
323 switch (n) {
324 case 1:
325 return "first";
326 case 2:
327 return "second";
328 case 3:
329 return "third";
330 default:
331 if (n >= 21) {
332 switch (n % 10) {
333 case 1:
334 return n + "st";
335 case 2:
336 return n + "nd";
337 case 3:
338 return n + "rd";
339 }
340 }
341 return n + "th";
342 }
343 }
344
345
346 }
347