Debian Codegraph @

net/sf/saxon/expr/parser/Token.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 java.util.HashMap;
11
12 /**
13 * This class holds static constants and methods defining the lexical tokens used in
14 * XPath and XQuery, and associated keywords.
15 */
16
17 public abstract class Token {
18
19 /**
20 * Token numbers. Those in the range 0 to LAST_OPERATOR are tokens that can be followed
21 * by a name or expression; those above this range are tokens that can be
22 * followed by an binary operator.
23 */
24
25 /**
26 * Pseudo-token representing the end of the expression
27 */
28 public static final int EOF = 0;
29 /**
30 * "union" or "|" token
31 */
32 public static final int UNION = 1;
33 /**
34 * Forwards "/"
35 */
36 public static final int SLASH = 2;
37 /**
38 * At token, "@"
39 */
40 public static final int AT = 3;
41 /**
42 * Left square bracket
43 */
44 public static final int LSQB = 4;
45 /**
46 * Left parenthesis
47 */
48 public static final int LPAR = 5;
49 /**
50 * Equals token ("=")
51 */
52 public static final int EQUALS = 6;
53 /**
54 * Comma token
55 */
56 public static final int COMMA = 7;
57 /**
58 * Double forwards slash, "//"
59 */
60 public static final int SLASH_SLASH = 8;
61 /**
62 * Operator "or"
63 */
64 public static final int OR = 9;
65 /**
66 * Operator "and"
67 */
68 public static final int AND = 10;
69 /**
70 * Operator ">"
71 */
72 public static final int GT = 11;
73 /**
74 * Operator "<"
75 */
76 public static final int LT = 12;
77 /**
78 * Operator ">="
79 */
80 public static final int GE = 13;
81 /**
82 * Operator "<="
83 */
84 public static final int LE = 14;
85 /**
86 * Operator "+"
87 */
88 public static final int PLUS = 15;
89 /**
90 * Binary minus operator
91 */
92 public static final int MINUS = 16;
93 /**
94 * Multiply operator, "*" when used in an operator context
95 */
96 public static final int MULT = 17;
97 /**
98 * Operator "div"
99 */
100 public static final int DIV = 18;
101 /**
102 * Operator "mod"
103 */
104 public static final int MOD = 19;
105 /**
106 * Operator "is"
107 */
108 public static final int IS = 20;
109 /**
110 * "$" symbol
111 */
112 public static final int DOLLAR = 21;
113 /**
114 * Operator not-equals. That is, "!="
115 */
116 public static final int NE = 22;
117 /**
118 * Operator "intersect"
119 */
120 public static final int INTERSECT = 23;
121 /**
122 * Operator "except"
123 */
124 public static final int EXCEPT = 24;
125 /**
126 * Keyword "return"
127 */
128 public static final int RETURN = 25;
129 /**
130 * Ketword "then"
131 */
132 public static final int THEN = 26;
133 /**
134 * Keyword "else"
135 */
136 public static final int ELSE = 27;
137 /**
138 * Keyword "where"
139 */
140 public static final int WHERE = 28;
141 /**
142 * Operator "to"
143 */
144 public static final int TO = 29;
145 /**
146 * Operator "||"
147 */
148 public static final int CONCAT = 30;
149 /**
150 * Keyword "in"
151 */
152 public static final int IN = 31;
153 /**
154 * Keyword "some"
155 */
156 public static final int SOME = 32;
157 /**
158 * Keyword "every"
159 */
160 public static final int EVERY = 33;
161 /**
162 * Keyword "satisfies"
163 */
164 public static final int SATISFIES = 34;
165 /**
166 * Token representing the name of a function and the following "(" symbol
167 */
168 public static final int FUNCTION = 35;
169
170 /**
171 * Token representing the name of an axis and the following "::" symbol
172 */
173 public static final int AXIS = 36;
174 /**
175 * Keyword "if"
176 */
177 public static final int IF = 37;
178 /**
179 * Operator "<<"
180 */
181 public static final int PRECEDES = 38;
182 /**
183 * Operator ">>"
184 */
185 public static final int FOLLOWS = 39;
186 /**
187 * Operator "!"
188 */
189 public static final int BANG = 40;
190 /**
191 * "::" symbol
192 */
193 public static final int COLONCOLON = 41;
194 /**
195 * ":*" symbol
196 */
197 public static final int COLONSTAR = 42;
198 /**
199 * Token representing a function name and the following "#" symbol
200 */
201 public static final int NAMED_FUNCTION_REF = 43;
202 /**
203 * # symbol
204 */
205 public static final int HASH = 44;
206 /**
207 * operator "instance of"
208 */
209 public static final int INSTANCE_OF = 45;
210 /**
211 * operator "cast as"
212 */
213 public static final int CAST_AS = 46;
214 /**
215 * operator "treat as"
216 */
217 public static final int TREAT_AS = 47;
218 /**
219 * Tilde, used in XSLT patterns
220 */
221 public static final int TILDE = 48;
222 /**
223 * Pilcrow, used in export files
224 */
225 public static final int PILCROW = 49;
226 /**
227 * operator "eq"
228 */
229 public static final int FEQ = 50; // "Fortran" style comparison operators eq, ne, etc
230 /**
231 * operator "ne"
232 */
233 public static final int FNE = 51;
234 /**
235 * operator "gt"
236 */
237 public static final int FGT = 52;
238 /**
239 * operator "lt"
240 */
241 public static final int FLT = 53;
242 /**
243 * operator "ge"
244 */
245 public static final int FGE = 54;
246 /**
247 * opeartor "le"
248 */
249 public static final int FLE = 55;
250 /**
251 * operator "idiv"
252 */
253 public static final int IDIV = 56;
254 /**
255 * operator "castable as"
256 */
257 public static final int CASTABLE_AS = 57;
258 /**
259 * ":=" symbol (XQuery only)
260 */
261 public static final int ASSIGN = 58;
262 /**
263 * "{" symbol (XQuery only)
264 */
265 public static final int LCURLY = 59;
266 /**
267 * composite token: <keyword "{"> (XQuery only)
268 */
269 public static final int KEYWORD_CURLY = 60;
270 /**
271 * composite token <'element' QNAME> (XQuery only)
272 */
273 public static final int ELEMENT_QNAME = 61;
274 /**
275 * composite token <'attribute' QNAME> (XQuery only)
276 */
277 public static final int ATTRIBUTE_QNAME = 62;
278 /**
279 * composite token <'pi' QNAME> (XQuery only)
280 */
281 public static final int PI_QNAME = 63;
282 /**
283 * composite token <'namespace' QNAME> (XQuery only)
284 */
285 public static final int NAMESPACE_QNAME = 64;
286 /**
287 * Keyword "typeswitch"
288 */
289 public static final int TYPESWITCH = 65;
290 /**
291 * Keyword "switch" (XQuery 1.1)
292 */
293 public static final int SWITCH = 66;
294 /**
295 * Keyword "case"
296 */
297 public static final int CASE = 67;
298 /**
299 * Keyword "modify"
300 */
301 public static final int MODIFY = 68;
302
303 /**
304 * Node kind, e.g. "node()" or "comment()"
305 */
306 public static final int NODEKIND = 69;
307 /**
308 * "*:" token
309 */
310 public static final int SUFFIX = 70; // e.g. *:suffix - the suffix is actually a separate token
311 /**
312 * "as" (in XQuery Update rename expression)
313 */
314 public static final int AS = 71;
315 /*
316 * "group by" (XQuery 3.0)
317 */
318 public static final int GROUP_BY = 72;
319 /**
320 * "for tumbling" (XQuery 3.0)
321 */
322 public static final int FOR_TUMBLING = 73;
323 /**
324 * "for sliding" (XQuery 3.0)
325 */
326 public static final int FOR_SLIDING = 74;
327
328 // map colon key-entry separator
329 /**
330 * ":" (XPath 3.0 maps)
331 */
332 public static final int COLON = 75;
333 /**
334 * Arrow operator "=>" (XQuery 3.1)
335 */
336 public static final int ARROW = 76;
337 /**
338 * First part of a string template
339 */
340 public static final int STRING_TEMPLATE_INITIAL = 77;
341 /**
342 * Intermediate part of a string template
343 */
344 public static final int STRING_TEMPLATE_INTERMEDIATE = 78;
345 /**
346 * "andAlso" (Saxon extension)
347 */
348 public static final int AND_ALSO = 79;
349 /**
350 * "orElse" (Saxon extension)
351 */
352 public static final int OR_ELSE = 80;
353
354
355 // The following tokens are used only in the query prolog. They are categorized
356 // as operators on the basis that a following name is treated as a name rather than
357 // an operator.
358
359
360 /**
361 * "xquery version"
362 */
363 public static final int XQUERY_VERSION = 88;
364 /**
365 * "xquery encoding"
366 */
367 public static final int XQUERY_ENCODING = 89;
368 /**
369 * "declare namespace"
370 */
371 public static final int DECLARE_NAMESPACE = 90;
372 /**
373 * "declare default"
374 */
375 public static final int DECLARE_DEFAULT = 91;
376 /**
377 * "declare construction"
378 */
379 public static final int DECLARE_CONSTRUCTION = 92;
380 /**
381 * "declare base-uri"
382 */
383 public static final int DECLARE_BASEURI = 93;
384 /**
385 * "declare boundary-space"
386 */
387 public static final int DECLARE_BOUNDARY_SPACE = 94;
388 /**
389 * "declare decimal-format"
390 */
391 public static final int DECLARE_DECIMAL_FORMAT = 95;
392 /**
393 * "import schema"
394 */
395 public static final int IMPORT_SCHEMA = 96;
396 /**
397 * "import module"
398 */
399 public static final int IMPORT_MODULE = 97;
400 /**
401 * "declare variable"
402 */
403 public static final int DECLARE_VARIABLE = 98;
404 /**
405 * "declare context"
406 */
407 public static final int DECLARE_CONTEXT = 99;
408 /**
409 * "declare function"
410 */
411 public static final int DECLARE_FUNCTION = 100;
412 /**
413 * "module namespace"
414 */
415 public static final int MODULE_NAMESPACE = 101;
416 /**
417 * Various compound symbols supporting XQuery validation expression
418 */
419 public static final int VALIDATE = 102;
420 public static final int VALIDATE_STRICT = 103;
421 public static final int VALIDATE_LAX = 104;
422 public static final int VALIDATE_TYPE = 105;
423 /**
424 * percent sign '%'
425 */
426 public static final int PERCENT = 106;
427
428 /**
429 * "declare xmlspace"
430 */
431 public static final int DECLARE_ORDERING = 107;
432
433 /**
434 * "declare copy-namespaces"
435 */
436 public static final int DECLARE_COPY_NAMESPACES = 108;
437 /**
438 * "declare option"
439 */
440 public static final int DECLARE_OPTION = 109;
441 /**
442 * "declare revalidation"
443 */
444 public static final int DECLARE_REVALIDATION = 110;
445 /**
446 * "insert node/nodes"
447 */
448 public static final int INSERT_NODE = 111;
449 /**
450 * "delete node/nodes"
451 */
452 public static final int DELETE_NODE = 112;
453 /**
454 * "replace node/nodes"
455 */
456 public static final int REPLACE_NODE = 113;
457 /**
458 * "replace value"
459 */
460 public static final int REPLACE_VALUE = 114;
461 /**
462 * "rename node"
463 */
464 public static final int RENAME_NODE = 115;
465 /**
466 * "first into"
467 */
468 public static final int FIRST_INTO = 116;
469 /**
470 * "last into"
471 */
472 public static final int LAST_INTO = 117;
473 /**
474 * "after"
475 */
476 public static final int AFTER = 118;
477 /**
478 * "before"
479 */
480 public static final int BEFORE = 119;
481 /**
482 * "into"
483 */
484 public static final int INTO = 120;
485 /**
486 * "with"
487 */
488 public static final int WITH = 121;
489 /**
490 * "declare updating [function]"
491 */
492 public static final int DECLARE_UPDATING = 122;
493 /**
494 * declare %
495 */
496 public static final int DECLARE_ANNOTATED = 123;
497 /**
498 * Saxon extension: declare type
499 */
500 public static final int DECLARE_TYPE = 124;
501 /**
502 * semicolon separator
503 */
504 public static final int SEMICOLON = 149;
505
506
507 /**
508 * Constant identifying the token number of the last token to be classified as an operator
509 */
510 static int LAST_OPERATOR = 150;
511
512 // Tokens that set "operator" context, so an immediately following "div" is recognized
513 // as an operator, not as an element name
514
515 /**
516 * Name token (a QName, in general)
517 */
518 public static final int NAME = 201;
519 /**
520 * String literal
521 */
522 public static final int STRING_LITERAL = 202;
523 /**
524 * Right square bracket
525 */
526 public static final int RSQB = 203;
527 /**
528 * Right parenthesis
529 */
530 public static final int RPAR = 204;
531 /**
532 * "." symbol
533 */
534 public static final int DOT = 205;
535 /**
536 * ".." symbol
537 */
538 public static final int DOTDOT = 206;
539 /**
540 * "*" symbol when used as a wildcard
541 */
542 public static final int STAR = 207;
543 /**
544 * "prefix:*" token
545 */
546 public static final int PREFIX = 208; // e.g. prefix:*
547 /**
548 * Numeric literal
549 */
550 public static final int NUMBER = 209;
551
552 /**
553 * "for" keyword
554 */
555 public static final int FOR = 211;
556
557 /**
558 * Keyword "default"
559 */
560 public static final int DEFAULT = 212;
561 /**
562 * Question mark symbol. That is, "?"
563 */
564 public static final int QMARK = 213;
565 /**
566 * "}" symbol (XQuery only)
567 */
568 public static final int RCURLY = 215;
569 /**
570 * "let" keyword (XQuery only)
571 */
572 public static final int LET = 216;
573 /**
574 * "<" at the start of a tag (XQuery only). The pseudo-XML syntax that
575 * follows is read character-by-character by the XQuery parser
576 */
577 public static final int TAG = 217;
578 /**
579 * A token representing an XQuery pragma.
580 * This construct "(# .... #)" is regarded as a single token, for the QueryParser to sort out.
581 */
582 public static final int PRAGMA = 218;
583 /**
584 * "copy" keyword
585 */
586 public static final int COPY = 219;
587 /**
588 * "count" keyword
589 */
590 public static final int COUNT = 220;
591 /**
592 * Final part of a string template
593 */
594 public static final int STRING_TEMPLATE_FINAL = 221;
595 /**
596 * Complete string template with no embedded expressions
597 */
598 public static final int STRING_TEMPLATE_COMPLETE = 222;
599
600 /**
601 * Unary minus sign
602 */
603 public static final int NEGATE = 299; // unary minus: not actually a token, but we
604 // use token numbers to identify operators.
605
606
607 /**
608 * The following strings are used to represent tokens in error messages
609 */
610
611 public final static String[] tokens = new String[300];
612
613 static {
614 tokens[EOF] = "<eof>";
615 tokens[UNION] = "|";
616 tokens[SLASH] = "/";
617 tokens[AT] = "@";
618 tokens[TILDE] = "~";
619 tokens[LSQB] = "[";
620 tokens[LPAR] = "(";
621 tokens[EQUALS] = "=";
622 tokens[COMMA] = ",";
623 tokens[SLASH_SLASH] = "//";
624 tokens[OR] = "or";
625 tokens[AND] = "and";
626 tokens[GT] = ">";
627 tokens[LT] = "<";
628 tokens[GE] = ">=";
629 tokens[LE] = "<=";
630 tokens[PLUS] = "+";
631 tokens[MINUS] = "-";
632 tokens[MULT] = "*";
633 tokens[DIV] = "div";
634 tokens[MOD] = "mod";
635 tokens[IS] = "is";
636 tokens[DOLLAR] = "$";
637 tokens[NE] = "!=";
638 tokens[BANG] = "!";
639 tokens[CONCAT] = "||";
640 tokens[INTERSECT] = "intersect";
641 tokens[EXCEPT] = "except";
642 tokens[RETURN] = "return";
643 tokens[THEN] = "then";
644 tokens[ELSE] = "else";
645 tokens[TO] = "to";
646 tokens[IN] = "in";
647 tokens[SOME] = "some";
648 tokens[EVERY] = "every";
649 tokens[SATISFIES] = "satisfies";
650 tokens[FUNCTION] = "<function>(";
651 tokens[AXIS] = "<axis>";
652 tokens[IF] = "if(";
653 tokens[PRECEDES] = "<<";
654 tokens[FOLLOWS] = ">>";
655 tokens[COLONCOLON] = "::";
656 tokens[COLONSTAR] = ":*";
657 tokens[HASH] = "#";
658 tokens[INSTANCE_OF] = "instance of";
659 tokens[CAST_AS] = "cast as";
660 tokens[TREAT_AS] = "treat as";
661 tokens[FEQ] = "eq";
662 tokens[FNE] = "ne";
663 tokens[FGT] = "gt";
664 tokens[FGE] = "ge";
665 tokens[FLT] = "lt";
666 tokens[FLE] = "le";
667 tokens[IDIV] = "idiv";
668 tokens[CASTABLE_AS] = "castable as";
669 tokens[ASSIGN] = ":=";
670 tokens[SWITCH] = "switch";
671 tokens[TYPESWITCH] = "typeswitch";
672 tokens[CASE] = "case";
673 tokens[DEFAULT] = "default";
674 //tokens [ AS_FIRST ] = "as first";
675 //tokens [ AS_LAST ] = "as last";
676 tokens[AFTER] = "after";
677 tokens[BEFORE] = "before";
678 tokens[INTO] = "into";
679 tokens[WITH] = "with";
680 tokens[MODIFY] = "modify";
681 tokens[AS] = "as";
682 tokens[PILCROW] = "\u00B6";
683
684 tokens[COLON] = ":";
685 tokens[ARROW] = "=>";
686 tokens[AND_ALSO] = "andAlso";
687 tokens[OR_ELSE] = "orElse";
688 tokens[STRING_TEMPLATE_INITIAL] = "``[<string>`{";
689 tokens[STRING_TEMPLATE_INTERMEDIATE] = "}`<string>`{";
690 tokens[STRING_TEMPLATE_FINAL] = "}`<string>]``";
691 tokens[STRING_TEMPLATE_COMPLETE] = "``[<string>]``";
692
693 tokens[NAME] = "<name>";
694 tokens[STRING_LITERAL] = "<string-literal>";
695 tokens[RSQB] = "]";
696 tokens[RPAR] = ")";
697 tokens[DOT] = ".";
698 tokens[DOTDOT] = "..";
699 tokens[STAR] = "*";
700 tokens[PREFIX] = "<prefix:*>";
701 tokens[NUMBER] = "<numeric-literal>";
702 tokens[NODEKIND] = "<node-type>()";
703 tokens[FOR] = "for";
704 tokens[SUFFIX] = "<*:local-name>";
705 tokens[QMARK] = "?";
706 tokens[LCURLY] = "{";
707 tokens[KEYWORD_CURLY] = "<keyword> {";
708 tokens[RCURLY] = "}";
709 tokens[LET] = "let";
710 tokens[VALIDATE] = "validate {";
711 tokens[TAG] = "<element>";
712 tokens[PRAGMA] = "(# ... #)";
713 tokens[SEMICOLON] = ";";
714 tokens[COPY] = "copy";
715 tokens[NEGATE] = "-";
716 tokens[PERCENT] = "%";
717 }
718
719 /**
720 * Lookup table for composite (two-keyword) tokens
721 */
722 /*@NotNull*/ public static HashMap<String, Integer> doubleKeywords = new HashMap<String, Integer>(30);
723 /**
724 * Pseudo-token representing the start of the expression
725 */
726 public static final int UNKNOWN = -1;
727
728 private Token() {
729 }
730
731 static {
732 mapDouble("instance of", INSTANCE_OF);
733 mapDouble("cast as", CAST_AS);
734 mapDouble("treat as", TREAT_AS);
735 mapDouble("castable as", CASTABLE_AS);
736 mapDouble("group by", GROUP_BY);
737 mapDouble("for tumbling", FOR_TUMBLING);
738 mapDouble("for sliding", FOR_SLIDING);
739 mapDouble("xquery version", XQUERY_VERSION);
740 mapDouble("xquery encoding", XQUERY_ENCODING);
741 mapDouble("declare namespace", DECLARE_NAMESPACE);
742 mapDouble("declare default", DECLARE_DEFAULT);
743 mapDouble("declare construction", DECLARE_CONSTRUCTION);
744 mapDouble("declare base-uri", DECLARE_BASEURI);
745 mapDouble("declare boundary-space", DECLARE_BOUNDARY_SPACE);
746 mapDouble("declare decimal-format", DECLARE_DECIMAL_FORMAT);
747 mapDouble("declare ordering", DECLARE_ORDERING);
748 mapDouble("declare copy-namespaces", DECLARE_COPY_NAMESPACES);
749 mapDouble("declare option", DECLARE_OPTION);
750 mapDouble("declare revalidation", DECLARE_REVALIDATION);
751 mapDouble("declare type", DECLARE_TYPE); // Saxon extension
752 mapDouble("import schema", IMPORT_SCHEMA);
753 mapDouble("import module", IMPORT_MODULE);
754 mapDouble("declare variable", DECLARE_VARIABLE);
755 mapDouble("declare context", DECLARE_CONTEXT);
756 mapDouble("declare function", DECLARE_FUNCTION);
757 mapDouble("declare updating", DECLARE_UPDATING);
758 mapDouble("module namespace", MODULE_NAMESPACE);
759 mapDouble("validate strict", VALIDATE_STRICT);
760 mapDouble("validate lax", VALIDATE_LAX);
761 mapDouble("validate type", VALIDATE_TYPE);
762 mapDouble("insert node", INSERT_NODE);
763 mapDouble("insert nodes", INSERT_NODE);
764 mapDouble("delete node", DELETE_NODE);
765 mapDouble("delete nodes", DELETE_NODE);
766 mapDouble("replace node", REPLACE_NODE);
767 mapDouble("replace value", REPLACE_VALUE);
768 mapDouble("rename node", RENAME_NODE);
769 mapDouble("rename nodes", RENAME_NODE);
770 mapDouble("first into", FIRST_INTO);
771 mapDouble("last into", LAST_INTO);
772 }
773
774 private static void mapDouble(String doubleKeyword, int token) {
775 doubleKeywords.put(doubleKeyword, token);
776 tokens[token] = doubleKeyword;
777 }
778
779 /**
780 * Return the inverse of a relational operator, so that "a op b" can be
781 * rewritten as "b inverse(op) a"
782 *
783 * @param operator the operator whose inverse is required
784 * @return the inverse operator
785 */
786
787 public static int inverse(int operator) {
788 switch (operator) {
789 case LT:
790 return GT;
791 case LE:
792 return GE;
793 case GT:
794 return LT;
795 case GE:
796 return LE;
797 case FLT:
798 return FGT;
799 case FLE:
800 return FGE;
801 case FGT:
802 return FLT;
803 case FGE:
804 return FLE;
805 default:
806 return operator;
807 }
808 }
809
810 /**
811 * Return the negation of a relational operator, so that "a op b" can be
812 * rewritten as not(b op' a)
813 *
814 * @param operator the operator to be negated
815 * @return the negated operator
816 */
817
818 public static int negate(int operator) {
819 switch (operator) {
820 case FEQ:
821 return FNE;
822 case FNE:
823 return FEQ;
824 case FLT:
825 return FGE;
826 case FLE:
827 return FGT;
828 case FGT:
829 return FLE;
830 case FGE:
831 return FLT;
832 default:
833 throw new IllegalArgumentException("Invalid operator for negate()");
834 }
835 }
836
837 public static boolean isOrderedOperator(int operator) {
838 return operator != FEQ && operator != FNE;
839 }
840 }
841