Skip to content

Commit aadb4f8

Browse files
committed
dedupe value parser node creation
1 parent d0deedd commit aadb4f8

File tree

1 file changed

+9
-55
lines changed

1 file changed

+9
-55
lines changed

src/value-parser.ts

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ export class ValueParser {
100100

101101
switch (token_type) {
102102
case TOKEN_IDENT:
103-
return this.create_keyword_node(start, end)
103+
return this.create_node(NODE_VALUE_KEYWORD, start, end)
104104

105105
case TOKEN_NUMBER:
106-
return this.create_number_node(start, end)
106+
return this.create_node(NODE_VALUE_NUMBER, start, end)
107107

108108
case TOKEN_PERCENTAGE:
109109
case TOKEN_DIMENSION:
110-
return this.create_dimension_node(start, end)
110+
return this.create_node(NODE_VALUE_DIMENSION, start, end)
111111

112112
case TOKEN_STRING:
113-
return this.create_string_node(start, end)
113+
return this.create_node(NODE_VALUE_STRING, start, end)
114114

115115
case TOKEN_HASH:
116-
return this.create_color_node(start, end)
116+
return this.create_node(NODE_VALUE_COLOR, start, end)
117117

118118
case TOKEN_FUNCTION:
119119
return this.parse_function_node(start, end)
@@ -122,57 +122,17 @@ export class ValueParser {
122122
return this.parse_operator_node(start, end)
123123

124124
case TOKEN_COMMA:
125-
return this.create_operator_node(start, end)
125+
return this.create_node(NODE_VALUE_OPERATOR, start, end)
126126

127127
default:
128128
// Unknown token type, skip it
129129
return null
130130
}
131131
}
132132

133-
private create_keyword_node(start: number, end: number): number {
133+
private create_node(node_type: number, start: number, end: number): number {
134134
let node = this.arena.create_node()
135-
this.arena.set_type(node, NODE_VALUE_KEYWORD)
136-
this.arena.set_start_offset(node, start)
137-
this.arena.set_length(node, end - start)
138-
this.arena.set_content_start(node, start)
139-
this.arena.set_content_length(node, end - start)
140-
return node
141-
}
142-
143-
private create_number_node(start: number, end: number): number {
144-
let node = this.arena.create_node()
145-
this.arena.set_type(node, NODE_VALUE_NUMBER)
146-
this.arena.set_start_offset(node, start)
147-
this.arena.set_length(node, end - start)
148-
this.arena.set_content_start(node, start)
149-
this.arena.set_content_length(node, end - start)
150-
return node
151-
}
152-
153-
private create_dimension_node(start: number, end: number): number {
154-
let node = this.arena.create_node()
155-
this.arena.set_type(node, NODE_VALUE_DIMENSION)
156-
this.arena.set_start_offset(node, start)
157-
this.arena.set_length(node, end - start)
158-
this.arena.set_content_start(node, start)
159-
this.arena.set_content_length(node, end - start)
160-
return node
161-
}
162-
163-
private create_string_node(start: number, end: number): number {
164-
let node = this.arena.create_node()
165-
this.arena.set_type(node, NODE_VALUE_STRING)
166-
this.arena.set_start_offset(node, start)
167-
this.arena.set_length(node, end - start)
168-
this.arena.set_content_start(node, start)
169-
this.arena.set_content_length(node, end - start)
170-
return node
171-
}
172-
173-
private create_color_node(start: number, end: number): number {
174-
let node = this.arena.create_node()
175-
this.arena.set_type(node, NODE_VALUE_COLOR)
135+
this.arena.set_type(node, node_type)
176136
this.arena.set_start_offset(node, start)
177137
this.arena.set_length(node, end - start)
178138
this.arena.set_content_start(node, start)
@@ -181,13 +141,7 @@ export class ValueParser {
181141
}
182142

183143
private create_operator_node(start: number, end: number): number {
184-
let node = this.arena.create_node()
185-
this.arena.set_type(node, NODE_VALUE_OPERATOR)
186-
this.arena.set_start_offset(node, start)
187-
this.arena.set_length(node, end - start)
188-
this.arena.set_content_start(node, start)
189-
this.arena.set_content_length(node, end - start)
190-
return node
144+
return this.create_node(NODE_VALUE_OPERATOR, start, end)
191145
}
192146

193147
private parse_operator_node(start: number, end: number): number | null {

0 commit comments

Comments
 (0)