| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | package org.mozilla.javascript.ast; |
| 8 | |
| 9 | import org.mozilla.javascript.Token; |
| 10 | |
| 11 | /** |
| 12 | * AST node for an E4X XML {@code [expr]} member-ref expression. |
| 13 | * The node type is {@link Token#REF_MEMBER}.<p> |
| 14 | * |
| 15 | * Syntax:<p> |
| 16 | * |
| 17 | * <pre> @<i><sub>opt</sub></i> ns:: <i><sub>opt</sub></i> [ expr ]</pre> |
| 18 | * |
| 19 | * Examples include {@code ns::[expr]}, {@code @ns::[expr]}, {@code @[expr]}, |
| 20 | * {@code *::[expr]} and {@code @*::[expr]}.<p> |
| 21 | * |
| 22 | * Note that the form {@code [expr]} (i.e. no namespace or |
| 23 | * attribute-qualifier) is not a legal {@code XmlElemRef} expression, |
| 24 | * since it's already used for standard JavaScript {@link ElementGet} |
| 25 | * array-indexing. Hence, an {@code XmlElemRef} node always has |
| 26 | * either the attribute-qualifier, a non-{@code null} namespace node, |
| 27 | * or both.<p> |
| 28 | * |
| 29 | * The node starts at the {@code @} token, if present. Otherwise it starts |
| 30 | * at the namespace name. The node bounds extend through the closing |
| 31 | * right-bracket, or if it is missing due to a syntax error, through the |
| 32 | * end of the index expression.<p> |
| 33 | */ |
| 34 | public class XmlElemRef extends XmlRef { |
| 35 | |
| 36 | private AstNode indexExpr; |
| 37 | private int lb = -1; |
| 38 | private int rb = -1; |
| 39 | |
| 40 | { |
| 41 | type = Token.REF_MEMBER; |
| 42 | } |
| 43 | |
| 44 | public XmlElemRef() { |
| 45 | } |
| 46 | |
| 47 | public XmlElemRef(int pos) { |
| 48 | super(pos); |
| 49 | } |
| 50 | |
| 51 | public XmlElemRef(int pos, int len) { |
| 52 | super(pos, len); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns index expression: the 'expr' in {@code @[expr]} |
| 57 | * or {@code @*::[expr]}. |
| 58 | */ |
| 59 | public AstNode getExpression() { |
| 60 | return indexExpr; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sets index expression, and sets its parent to this node. |
| 65 | * @throws IllegalArgumentException if {@code expr} is {@code null} |
| 66 | */ |
| 67 | public void setExpression(AstNode expr) { |
| 68 | assertNotNull(expr); |
| 69 | indexExpr = expr; |
| 70 | expr.setParent(this); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns left bracket position, or -1 if missing. |
| 75 | */ |
| 76 | public int getLb() { |
| 77 | return lb; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets left bracket position, or -1 if missing. |
| 82 | */ |
| 83 | public void setLb(int lb) { |
| 84 | this.lb = lb; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns left bracket position, or -1 if missing. |
| 89 | */ |
| 90 | public int getRb() { |
| 91 | return rb; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets right bracket position, -1 if missing. |
| 96 | */ |
| 97 | public void setRb(int rb) { |
| 98 | this.rb = rb; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sets both bracket positions. |
| 103 | */ |
| 104 | public void setBrackets(int lb, int rb) { |
| 105 | this.lb = lb; |
| 106 | this.rb = rb; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String toSource(int depth) { |
| 111 | StringBuilder sb = new StringBuilder(); |
| 112 | sb.append(makeIndent(depth)); |
| 113 | if (isAttributeAccess()) { |
| 114 | sb.append("@"); |
| 115 | } |
| 116 | if (namespace != null) { |
| 117 | sb.append(namespace.toSource(0)); |
| 118 | sb.append("::"); |
| 119 | } |
| 120 | sb.append("["); |
| 121 | sb.append(indexExpr.toSource(0)); |
| 122 | sb.append("]"); |
| 123 | return sb.toString(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Visits this node, then the namespace if provided, then the |
| 128 | * index expression. |
| 129 | */ |
| 130 | @Override |
| 131 | public void visit(NodeVisitor v) { |
| 132 | if (v.visit(this)) { |
| 133 | if (namespace != null) { |
| 134 | namespace.visit(v); |
| 135 | } |
| 136 | indexExpr.visit(v); |
| 137 | } |
| 138 | } |
| 139 | } |