-
Notifications
You must be signed in to change notification settings - Fork 1
/
literal.go
183 lines (155 loc) · 4.16 KB
/
literal.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package estree
import (
"encoding/json"
"errors"
"fmt"
)
// Literal is a literal token. Note that a literal can be an expression.
type Literal interface {
Expression
isLiteral()
isLiteralOrIdentifier()
isVariableDeclarationOrLiteral()
}
func unmarshalLiteral(m json.RawMessage) (l Literal, match bool, err error) {
var x struct {
Type string `json:"type"`
Loc SourceLocation `json:"loc"`
Value interface{} `json:"value"`
Regex struct {
Pattern string `json:"pattern"`
Flags string `json:"flags"`
} `json:"regex"`
}
if err = json.Unmarshal(m, &x); err != nil {
match = true
} else if x.Type != (baseLiteral{}).Type() {
err = fmt.Errorf("%w %s, got %q", ErrWrongType, baseLiteral{}.Type(), x.Type)
} else if x.Regex.Pattern != "" || x.Regex.Flags != "" {
match = true
l = RegExpLiteral{Loc: x.Loc, Pattern: x.Regex.Pattern, Flags: x.Regex.Flags}
// TODO: complain if Value is non-nil?
} else {
match = true
switch v := x.Value.(type) {
case string:
l = StringLiteral{Loc: x.Loc, Value: v}
case bool:
l = BoolLiteral{Loc: x.Loc, Value: v}
case nil:
l = NullLiteral{Loc: x.Loc}
case float64:
l = NumberLiteral{Loc: x.Loc, Value: v}
default:
err = fmt.Errorf("%w string, bool, null, number, or regexp got %v", ErrWrongType, v)
}
}
return
}
type LiteralOrIdentifier interface {
Node
isLiteralOrIdentifier()
}
func unmarshalLiteralOrIdentifier(m json.RawMessage) (LiteralOrIdentifier, bool, error) {
if l, match, err := unmarshalLiteral(m); match {
return l, true, err
}
var i Identifier
if err := i.UnmarshalJSON([]byte(m)); !errors.Is(err, ErrWrongType) {
return i, true, err
}
return nil, false, fmt.Errorf("%w Literal or Identifier, got %v", ErrWrongType, string(m))
}
type baseLiteral struct {
baseExpression
}
func (baseLiteral) Type() string { return "Literal" }
func (baseLiteral) MinVersion() Version { return ES5 }
func (baseLiteral) IsZero() bool { return false }
func (baseLiteral) Errors() []error { return nil }
func (baseLiteral) isLiteral() {}
func (baseLiteral) isLiteralOrIdentifier() {}
func (baseLiteral) isVariableDeclarationOrLiteral() {}
type StringLiteral struct {
baseLiteral
Loc SourceLocation
Value string
}
func (sl StringLiteral) Location() SourceLocation { return sl.Loc }
func (sl StringLiteral) Walk(v Visitor) {
if v = v.Visit(sl); v != nil {
v.Visit(nil)
}
}
func (sl StringLiteral) MarshalJSON() ([]byte, error) {
x := nodeToMap(sl)
x["value"] = sl.Value
return json.Marshal(x)
}
type BoolLiteral struct {
baseLiteral
Loc SourceLocation
Value bool
}
func (bl BoolLiteral) Location() SourceLocation { return bl.Loc }
func (bl BoolLiteral) Walk(v Visitor) {
if v = v.Visit(bl); v != nil {
v.Visit(nil)
}
}
func (bl BoolLiteral) MarshalJSON() ([]byte, error) {
x := nodeToMap(bl)
x["value"] = bl.Value
return json.Marshal(x)
}
type NullLiteral struct {
baseLiteral
Loc SourceLocation
}
func (nl NullLiteral) Location() SourceLocation { return nl.Loc }
func (nl NullLiteral) Walk(v Visitor) {
if v = v.Visit(nl); v != nil {
v.Visit(nil)
}
}
func (nl NullLiteral) MarshalJSON() ([]byte, error) {
return json.Marshal(nodeToMap(nl))
}
type NumberLiteral struct {
baseLiteral
Loc SourceLocation
Value float64
}
func (nl NumberLiteral) Location() SourceLocation { return nl.Loc }
func (nl NumberLiteral) Walk(v Visitor) {
if v = v.Visit(nl); v != nil {
v.Visit(nil)
}
}
func (nl NumberLiteral) MarshalJSON() ([]byte, error) {
x := nodeToMap(nl)
x["value"] = nl.Value
return json.Marshal(x)
}
type RegExpLiteral struct {
baseLiteral
Loc SourceLocation
Pattern string
Flags string
}
func (rel RegExpLiteral) Location() SourceLocation { return rel.Loc }
// TODO: I think empty regex should still return false for IsZero; otherwise
// we should override IsZero and Errors here.
func (rel RegExpLiteral) Walk(v Visitor) {
if v = v.Visit(rel); v != nil {
v.Visit(nil)
}
}
func (rel RegExpLiteral) MarshalJSON() ([]byte, error) {
x := nodeToMap(rel)
x["regex"] = map[string]interface{}{
"pattern": rel.Pattern,
"flags": rel.Flags,
}
return json.Marshal(x)
}