-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytl.js
129 lines (119 loc) · 3.95 KB
/
ytl.js
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
/// <reference types="./ytl.d.ts" />
// deno-lint-ignore-file no-this-alias prefer-const
// syntax:
// root = nodes
// node = value (attr | merge)+ '{' nodes '}'
// attr = value ('=' value)?
// name = [\w_-]+
// merge = '...' value
// nodes = (node | merge | $input)*
// value = string | name | $input
const TOKENS = /([\w_-]+)|(?:"([^]*?)")|(\s+|\/\/.*)|(\.\.\.)|(.)/g;
// ^ name ^ string ^ ignore ^ dots ^ symbol
const
/** unquoted names, like 'foo', 'bar', 'hello-world' */
TokenName = 1,
/** quoted values, like '"foo"', '"hello world"' */
TokenString = 2,
/** comments, like '// ignore this' or whitespace like ' ' */
TokenIgnore = 3,
/** dots, used to indicate that a value should be merged into the current one instead of appended */
TokenDots = 4,
/** anything else that wasn't already matched */
TokenSymbol = 5;
const
ModeTag = 0,
ModeAttrKey = 1,
ModeAttrVal = 2,
ModeNodesMerge = 3,
ModeAttrsMerge = 4;
/**
* @param {string[]} strings
* @param {...unknown} values - the interpolated values to insert
*/
export default function ytl(strings, ...values) {
const h = this;
// the current parsing/insertion mode
let mode = ModeTag;
// last node will be the current node that is being parsed
let nodes = [];
// current attribute [key, value]
let attrKey;
// we use this stack of nodes to keep track of what node to append to when parsing of a node is finished
let parents = [nodes];
/**
* @param {unknown} value
* @param {unknown} [token]
*/
const handleMode = (value, token) => {
const current = nodes.at(-1);
// we use a table here to switch because bree think's it *may* be faster (benchmarks needed)
({
[ModeTag]: () => {
if (!token) {
// if there's no token then we're interpolating a value as a tag
// this is what allows components to work
parents.at(-1).push([value, {}]);
mode = ModeAttrKey;
} else if (token[TokenDots]) {
mode = ModeNodesMerge;
} else if (token[TokenString]) {
// convenience for strings so we don't need to `...` to merge them every time
nodes[nodes.length - 1] = current.concat(value);
mode = ModeTag;
} else if (token[TokenSymbol] === "}") {
// put the child into the new (old) current node
// todo: error?
parents.pop();
const child = nodes.pop();
// apply might be better here to reduce cost of destructing + argument stack
parents.at(-1).push(h(...child));
} else {
nodes.push([value, {}]);
mode = ModeAttrKey;
}
},
[ModeAttrKey]: () => {
if (!token || token[TokenName] || token[TokenString]) {
current[1][attrKey = value] = undefined;
} else if (token[TokenSymbol] === "{") {
parents.push(current);
mode = ModeTag;
} else if (token[TokenSymbol] === "=") {
mode = ModeAttrVal;
} else if (token[TokenDots]) {
mode = ModeAttrsMerge;
}
// todo: error?
},
[ModeAttrVal]: () => {
// todo: error if invalid syntax?
current[1][attrKey] = value
mode = ModeAttrKey;
},
[ModeNodesMerge]: () => {
// todo: error?
nodes[nodes.length - 1] = current.concat(value);
mode = ModeTag;
},
[ModeAttrsMerge]: () => {
// might be inefficient?
// todo: check performance
current[1] = { ...current[1], ...value };
mode = ModeAttrKey;
},
})[mode]();
};
for (let i = 0; i < strings.length; i++) {
const string = strings[i];
const tokens = string.matchAll(TOKENS);
for (const token of tokens) {
if (token[TokenIgnore]) continue;
// todo: change structure so this isn't needed
handleMode(token.slice(1).find((_) => _), token);
}
const value = values[i];
if (i < values.length) handleMode(value);
}
return nodes;
}