forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathName.java
More file actions
152 lines (133 loc) · 4.78 KB
/
Copy pathName.java
File metadata and controls
152 lines (133 loc) · 4.78 KB
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
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript.ast;
import org.mozilla.javascript.Token;
/**
* AST node for a simple name. A simple name is an identifier that is
* not a keyword. Node type is {@link Token#NAME}.<p>
*
* This node type is also used to represent certain non-identifier names that
* are part of the language syntax. It's used for the "get" and "set"
* pseudo-keywords for object-initializer getter/setter properties, and it's
* also used for the "*" wildcard in E4X XML namespace and name expressions.
*/
public class Name extends AstNode {
private String identifier;
private Scope scope;
{
type = Token.NAME;
}
public Name() {
}
public Name(int pos) {
super(pos);
}
public Name(int pos, int len) {
super(pos, len);
}
/**
* Constructs a new {@link Name}
* @param pos node start position
* @param len node length
* @param name the identifier associated with this {@code Name} node
*/
public Name(int pos, int len, String name) {
super(pos, len);
setIdentifier(name);
}
public Name(int pos, String name) {
super(pos);
setIdentifier(name);
setLength(name.length());
}
/**
* Returns the node's identifier
*/
public String getIdentifier() {
return identifier;
}
/**
* Sets the node's identifier
* @throws IllegalArgumentException if identifier is null
*/
public void setIdentifier(String identifier) {
assertNotNull(identifier);
this.identifier = identifier;
setLength(identifier.length());
}
/**
* Set the {@link Scope} associated with this node. This method does not
* set the scope's ast-node field to this node. The field exists only
* for temporary storage by the code generator. Not every name has an
* associated scope - typically only function and variable names (but not
* property names) are registered in a scope.
*
* @param s the scope. Can be null. Doesn't set any fields in the
* scope.
*/
@Override
public void setScope(Scope s) {
scope = s;
}
/**
* Return the {@link Scope} associated with this node. This is
* <em>only</em> used for (and set by) the code generator, so it will always
* be null in frontend AST-processing code. Use {@link #getDefiningScope}
* to find the lexical {@code Scope} in which this {@code Name} is defined,
* if any.
*/
@Override
public Scope getScope() {
return scope;
}
/**
* Returns the {@link Scope} in which this {@code Name} is defined.
* @return the scope in which this name is defined, or {@code null}
* if it's not defined in the current lexical scope chain
*/
public Scope getDefiningScope() {
Scope enclosing = getEnclosingScope();
String name = getIdentifier();
return enclosing == null ? null : enclosing.getDefiningScope(name);
}
/**
* Return true if this node is known to be defined as a symbol in a
* lexical scope other than the top-level (global) scope.
*
* @return {@code true} if this name appears as local variable, a let-bound
* variable not in the global scope, a function parameter, a loop
* variable, the property named in a {@link PropertyGet}, or in any other
* context where the node is known not to resolve to the global scope.
* Returns {@code false} if the node is defined in the top-level scope
* (i.e., its defining scope is an {@link AstRoot} object), or if its
* name is not defined as a symbol in the symbol table, in which case it
* may be an external or built-in name (or just an error of some sort.)
*/
public boolean isLocalName() {
Scope scope = getDefiningScope();
return scope != null && scope.getParentScope() != null;
}
/**
* Return the length of this node's identifier, to let you pretend
* it's a {@link String}. Don't confuse this method with the
* {@link AstNode#getLength} method, which returns the range of
* characters that this node overlaps in the source input.
*/
public int length() {
return identifier == null ? 0 : identifier.length();
}
@Override
public String toSource(int depth) {
return makeIndent(depth) + (identifier == null ? "<null>" : identifier);
}
/**
* Visits this node. There are no children to visit.
*/
@Override
public void visit(NodeVisitor v) {
v.visit(this);
}
}