-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathreworkcss-value.ts
More file actions
131 lines (112 loc) · 2.91 KB
/
reworkcss-value.ts
File metadata and controls
131 lines (112 loc) · 2.91 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
export interface CSSValue {
type: string;
string: string;
unit?: string;
value?: number;
}
export interface CommaToken {
type: 'comma';
string: ',';
}
export interface IdentToken {
type: 'ident';
string: string;
}
export interface NumberToken {
type: 'number';
string: string;
unit: string;
value: number;
}
export interface StringToken {
type: 'string';
quote: '"' | "'";
string: string;
value: string;
}
export type Token = CommaToken | IdentToken | NumberToken | StringToken;
/**
* Parse a string into an array of tokens.
*/
export function parse(str: string): Token[] {
return new Parser(str).parse();
}
class Parser {
private str: string;
constructor(str: string) {
this.str = str;
}
private skip(match: RegExpExecArray): void {
this.str = this.str.slice(match[0].length);
}
/** Comma tokens: "," */
private comma(): CommaToken | undefined {
const m = /^, */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'comma', string: ',' };
}
/** Identifier tokens: word or dash sequences */
private ident(): IdentToken | undefined {
const m = /^([\w-]+) */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'ident', string: m[1] };
}
/** Integer tokens, possibly with unit */
private int(): NumberToken | undefined {
const m = /^(([-+]?\d+)(\S+)?) */.exec(this.str);
if (!m) return;
this.skip(m);
const n = parseInt(m[2], 10);
const u = m[3] || '';
return { type: 'number', string: m[1], unit: u, value: n };
}
/** Float tokens, possibly with unit */
private float(): NumberToken | undefined {
const m = /^(((?:[-+]?\d+)?\.\d+)(\S+)?) */.exec(this.str);
if (!m) return;
this.skip(m);
const n = parseFloat(m[2]);
const u = m[3] || '';
return { type: 'number', string: m[1], unit: u, value: n };
}
/** Number tokens, either float or int */
private number(): NumberToken | undefined {
return this.float() || this.int();
}
/** Double-quoted strings */
private double(): StringToken | undefined {
const m = /^"([^"]*)" */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'string', quote: '"', string: `"${m[1]}"`, value: m[1] };
}
/** Single-quoted strings */
private single(): StringToken | undefined {
const m = /^'([^']*)' */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'string', quote: "'", string: `'${m[1]}'`, value: m[1] };
}
/** String tokens: single or double quoted */
private string(): StringToken | undefined {
return this.single() || this.double();
}
/** Attempt to parse any token */
private value(): Token | undefined {
return this.number() || this.ident() || this.string() || this.comma();
}
/** Run the full parse loop */
public parse(): Token[] {
const vals: Token[] = [];
while (this.str.length > 0) {
const obj = this.value();
if (!obj) {
throw new Error(`failed to parse near \`${this.str.slice(0, 10)}...\``);
}
vals.push(obj);
}
return vals;
}
}