1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Node = require('./node')
|
13 | , nodes = require('./');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var Ident = module.exports = function Ident(name, val, mixin){
|
24 | Node.call(this);
|
25 | this.name = name;
|
26 | this.string = name;
|
27 | this.val = val || nodes.null;
|
28 | this.mixin = !!mixin;
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | Ident.prototype.__defineGetter__('isEmpty', function(){
|
39 | return undefined == this.val;
|
40 | });
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | Ident.prototype.__defineGetter__('hash', function(){
|
50 | return this.name;
|
51 | });
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | Ident.prototype.__proto__ = Node.prototype;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | Ident.prototype.clone = function(parent){
|
67 | var clone = new Ident(this.name);
|
68 | clone.val = this.val.clone(parent, clone);
|
69 | clone.mixin = this.mixin;
|
70 | clone.lineno = this.lineno;
|
71 | clone.column = this.column;
|
72 | clone.filename = this.filename;
|
73 | clone.property = this.property;
|
74 | clone.rest = this.rest;
|
75 | return clone;
|
76 | };
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Ident.prototype.toJSON = function(){
|
86 | return {
|
87 | __type: 'Ident',
|
88 | name: this.name,
|
89 | val: this.val,
|
90 | mixin: this.mixin,
|
91 | property: this.property,
|
92 | rest: this.rest,
|
93 | lineno: this.lineno,
|
94 | column: this.column,
|
95 | filename: this.filename
|
96 | };
|
97 | };
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | Ident.prototype.toString = function(){
|
107 | return this.name;
|
108 | };
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | Ident.prototype.coerce = function(other){
|
119 | switch (other.nodeName) {
|
120 | case 'ident':
|
121 | case 'string':
|
122 | case 'literal':
|
123 | return new Ident(other.string);
|
124 | case 'unit':
|
125 | return new Ident(other.toString());
|
126 | default:
|
127 | return Node.prototype.coerce.call(this, other);
|
128 | }
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | Ident.prototype.operate = function(op, right){
|
141 | var val = right.first;
|
142 | switch (op) {
|
143 | case '-':
|
144 | if ('unit' == val.nodeName) {
|
145 | var expr = new nodes.Expression;
|
146 | val = val.clone();
|
147 | val.val = -val.val;
|
148 | expr.push(this);
|
149 | expr.push(val);
|
150 | return expr;
|
151 | }
|
152 | case '+':
|
153 | return new nodes.Ident(this.string + this.coerce(val).string);
|
154 | }
|
155 | return Node.prototype.operate.call(this, op, right);
|
156 | };
|