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 | var Literal = module.exports = function Literal(str){
|
23 | Node.call(this);
|
24 | this.val = str;
|
25 | this.string = str;
|
26 | this.prefixed = false;
|
27 | };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Literal.prototype.__proto__ = Node.prototype;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | Literal.prototype.__defineGetter__('hash', function(){
|
43 | return this.val;
|
44 | });
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | Literal.prototype.toString = function(){
|
54 | return this.val;
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Literal.prototype.coerce = function(other){
|
66 | switch (other.nodeName) {
|
67 | case 'ident':
|
68 | case 'string':
|
69 | case 'literal':
|
70 | return new Literal(other.string);
|
71 | default:
|
72 | return Node.prototype.coerce.call(this, other);
|
73 | }
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Literal.prototype.operate = function(op, right){
|
86 | var val = right.first;
|
87 | switch (op) {
|
88 | case '+':
|
89 | return new nodes.Literal(this.string + this.coerce(val).string);
|
90 | default:
|
91 | return Node.prototype.operate.call(this, op, right);
|
92 | }
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | Literal.prototype.toJSON = function(){
|
103 | return {
|
104 | __type: 'Literal',
|
105 | val: this.val,
|
106 | string: this.string,
|
107 | prefixed: this.prefixed,
|
108 | lineno: this.lineno,
|
109 | column: this.column,
|
110 | filename: this.filename
|
111 | };
|
112 | };
|