Tries to rewrite the path to a simple map expression. @param cc compilation context @return original or new expression @throws QueryException query exception
(final CompileContext cc)
| 684 | * @throws QueryException query exception |
| 685 | */ |
| 686 | private Expr toMap(final CompileContext cc) throws QueryException { |
| 687 | // do not rewrite relative paths with single step |
| 688 | final int sl = steps.length; |
| 689 | if(root == null && sl == 1) return this; |
| 690 | |
| 691 | Expr s1 = sl > 1 ? steps[sl - 2] : root, s2 = steps[sl - 1]; |
| 692 | final Type type1 = s1.seqType().type, type2 = s2.seqType().type; |
| 693 | |
| 694 | /* rewrite if: |
| 695 | * - previous expression yields nodes (otherwise, an error must be raised at runtime) |
| 696 | * - last expression is no step, and yields a single result or no node */ |
| 697 | if(!(type1 instanceof NodeType) || s2 instanceof Step || size() != 1 && |
| 698 | !type2.instanceOf(BasicType.ANY_ATOMIC_TYPE) && !type2.instanceOf(Types.FUNCTION)) |
| 699 | return this; |
| 700 | |
| 701 | /* remove last step from new root expression. examples: |
| 702 | * - (<a/>, <b/>)/map { name(): . } → (<a/>, <b/>) ! map { name(): . } |
| 703 | * - <a/>/<b/> → <a/> ! <b/> |
| 704 | * - $a/b/string → $a/b ! string() */ |
| 705 | if(sl > 1) s1 = get(cc, info, root, Arrays.copyOfRange(steps, 0, sl - 1)); |
| 706 | if(s1 != null) s2 = SimpleMap.get(cc, info, s1, s2); |
| 707 | return cc.replaceWith(this, s2); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Returns an equivalent expression which accesses an index. |
no test coverage detected