@overmvp/kumi 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/animation/index.ts +4 -0
- package/animation/motion-policy.ts +20 -0
- package/animation/slide.ts +61 -0
- package/animation/spring.ts +75 -0
- package/animation/staggered-reveal.ts +34 -0
- package/builder/css-gradients.mjs +74 -0
- package/builder/css-mask-assets.mjs +52 -0
- package/builder/css-mask-assets.test.mjs +31 -0
- package/builder/css-v1.mjs +73 -22
- package/builder/css-v1.test.mjs +42 -2
- package/builder/module-declarations.mjs +2 -2
- package/builder/module-declarations.test.mjs +4 -2
- package/builder/src/compiler.rs +25 -0
- package/builder/src/lua_upvalues.rs +641 -0
- package/builder/src/main.rs +1 -0
- package/builder/style-modules.mjs +21 -3
- package/builder/style-modules.test.mjs +24 -0
- package/builder/style-states.mjs +6 -4
- package/package.json +1 -1
- package/router/index.ts +1 -0
- package/router/link.tsx +32 -0
- package/router/router-view.ts +5 -3
- package/scripts/addon-build.mjs +17 -2
- package/scripts/addon-project.mjs +22 -11
- package/scripts/kumi-cli.mjs +0 -0
- package/src/bindings.ts +10 -4
- package/src/context.ts +31 -7
- package/src/css-tokens.ts +11 -2
- package/src/css-v1.ts +3 -0
- package/src/elements.ts +20 -1
- package/src/flex-layout.ts +26 -5
- package/src/frame-scheduler.ts +3 -1
- package/src/index.ts +2 -2
- package/src/keyed-list.ts +9 -6
- package/src/module-ui.ts +22 -2
- package/src/module.ts +26 -7
- package/src/reactivity.ts +14 -1
- package/src/style-classes.ts +22 -1
- package/src/style-states.ts +13 -6
- package/state/setup-store.ts +3 -3
- package/templates/addon/.agents/skills/kumi-ui/SKILL.md +1 -1
- package/templates/addon/.claude/skills/kumi-ui/SKILL.md +1 -1
- package/templates/addon/AGENTS.md +1 -1
- package/templates/addon/kumi.config.ts +17 -17
- package/templates/addon/src/app/app.module.ts +3 -5
- package/templates/addon/src/app/layout/{AppWindow.module.scss → AppWindow.module.css} +10 -12
- package/templates/addon/src/modules/greeting/greeting.module.ts +2 -2
- package/templates/addon/src/modules/greeting/ui/{Greeting.module.scss → Greeting.module.css} +9 -11
- package/templates/addon/src/modules/playtime/playtime.module.ts +3 -5
- package/templates/addon/src/modules/playtime/ui/{Playtime.module.scss → Playtime.module.css} +3 -5
- package/templates/addon/src/shared/styles/tokens.css +13 -0
- package/templates/addon/src/shared/ui/{Controls.module.scss → Controls.module.css} +18 -20
- package/utils/format.ts +13 -0
- package/utils/index.ts +1 -1
- package/wow/action-button.ts +26 -1
- package/wow/addon-manifest.ts +2 -0
- package/wow/animation.ts +19 -18
- package/wow/app-ui.ts +150 -31
- package/wow/aura-container.ts +132 -38
- package/wow/aura-countdown.ts +19 -3
- package/wow/aura-follower.ts +32 -0
- package/wow/aura-view.ts +49 -0
- package/wow/css-background-image.ts +75 -0
- package/wow/css-box.ts +14 -9
- package/wow/css-image-mask.ts +32 -0
- package/wow/css-surfaces.ts +88 -35
- package/wow/element-events.ts +33 -13
- package/wow/frames.ts +2 -0
- package/wow/heal-prediction.ts +2 -4
- package/wow/hooks.ts +17 -0
- package/wow/hotkeys.ts +221 -0
- package/wow/index.ts +11 -6
- package/wow/initial-view.ts +99 -0
- package/wow/layout-view.ts +19 -10
- package/wow/module-replacements.ts +103 -0
- package/wow/native.ts +1 -0
- package/wow/pixel.ts +25 -0
- package/wow/pointer-click.ts +35 -0
- package/wow/protected-view.ts +49 -13
- package/wow/rounded-texture.ts +9 -4
- package/wow/scroll-view.ts +5 -4
- package/wow/state-paint.ts +5 -14
- package/wow/status-bar.ts +65 -0
- package/wow/texture-fill.ts +10 -0
- package/wow/unit-button.ts +25 -4
- package/wow/unit-cast.ts +6 -4
- package/wow/view-nodes.ts +11 -4
- package/wow/view.ts +387 -278
- package/templates/addon/src/shared/styles/_tokens.scss +0 -12
package/builder/src/compiler.rs
CHANGED
|
@@ -2121,6 +2121,7 @@ impl<'a> Emitter<'a> {
|
|
|
2121
2121
|
prelude.push_str("if nativeRegistry.definitions ~= nil then error('Duplicate native Kumi package definitions', 0) end\n");
|
|
2122
2122
|
self.write_helpers(&mut prelude, true, true)?;
|
|
2123
2123
|
prelude.push_str("local definitions = {}\n");
|
|
2124
|
+
validate_fragment_lua(&bundle, &prelude, &out, &fragments)?;
|
|
2124
2125
|
let mut packet = b"kumi-addon-fragments-1\n".to_vec();
|
|
2125
2126
|
write_field(&mut packet, &bundle)?;
|
|
2126
2127
|
write_field(&mut packet, &prelude)?;
|
|
@@ -2133,6 +2134,7 @@ impl<'a> Emitter<'a> {
|
|
|
2133
2134
|
return Ok(packet);
|
|
2134
2135
|
}
|
|
2135
2136
|
bundle.push_str(&out);
|
|
2137
|
+
crate::lua_upvalues::validate(&bundle)?;
|
|
2136
2138
|
Ok(bundle.into_bytes())
|
|
2137
2139
|
}
|
|
2138
2140
|
|
|
@@ -2195,6 +2197,28 @@ impl<'a> Emitter<'a> {
|
|
|
2195
2197
|
|
|
2196
2198
|
const LUA_HEADER: &str = "-- Generated by kumi-pack compile. Lua 5.1.\n";
|
|
2197
2199
|
|
|
2200
|
+
fn validate_fragment_lua(
|
|
2201
|
+
header: &str,
|
|
2202
|
+
prelude: &str,
|
|
2203
|
+
footer: &str,
|
|
2204
|
+
fragments: &[[String; 5]],
|
|
2205
|
+
) -> Compile<()> {
|
|
2206
|
+
// The linker can place a factory in either chunk. The helpers in scope
|
|
2207
|
+
// differ, so validate both arrangements before publishing the fragment.
|
|
2208
|
+
let mut private = header.to_owned();
|
|
2209
|
+
let mut shared = prelude.to_owned();
|
|
2210
|
+
for (index, [_, _, prefix, factory, suffix]) in fragments.iter().enumerate() {
|
|
2211
|
+
private.push_str(prefix);
|
|
2212
|
+
private.push_str(factory);
|
|
2213
|
+
private.push_str(suffix);
|
|
2214
|
+
writeln!(shared, "definitions[{}] = {factory}", index + 1)?;
|
|
2215
|
+
}
|
|
2216
|
+
private.push_str(footer);
|
|
2217
|
+
crate::lua_upvalues::validate(&private).map_err(|error| format!("Addon chunk: {error}"))?;
|
|
2218
|
+
crate::lua_upvalues::validate(&shared).map_err(|error| format!("KumiShared.lua: {error}"))?;
|
|
2219
|
+
Ok(())
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2198
2222
|
fn write_field(packet: &mut Vec<u8>, field: &str) -> Compile<()> {
|
|
2199
2223
|
packet.extend_from_slice(&u32::try_from(field.len())?.to_le_bytes());
|
|
2200
2224
|
packet.extend_from_slice(field.as_bytes());
|
|
@@ -2331,6 +2355,7 @@ pub fn compile(project: &Path, request: ProjectCompile<'_>) -> Compile<()> {
|
|
|
2331
2355
|
pub fn emit_native(input: &Path, output: &Path) -> Compile<()> {
|
|
2332
2356
|
let ir = crate::native_ir::read_signal(input)?;
|
|
2333
2357
|
let lua = crate::native_signal::emit(ir);
|
|
2358
|
+
crate::lua_upvalues::validate(&lua)?;
|
|
2334
2359
|
fs::write(output, lua)?;
|
|
2335
2360
|
Ok(())
|
|
2336
2361
|
}
|
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
use std::collections::{HashMap, HashSet};
|
|
2
|
+
|
|
3
|
+
const MAX_UPVALUES: usize = 60;
|
|
4
|
+
|
|
5
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
6
|
+
enum Kind {
|
|
7
|
+
Name,
|
|
8
|
+
Number,
|
|
9
|
+
String,
|
|
10
|
+
Other,
|
|
11
|
+
Eof,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#[derive(Clone, Copy)]
|
|
15
|
+
struct Token<'a> {
|
|
16
|
+
text: &'a str,
|
|
17
|
+
kind: Kind,
|
|
18
|
+
line: usize,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
pub fn validate(source: &str) -> Result<(), String> {
|
|
22
|
+
let tokens = tokenize(source);
|
|
23
|
+
let mut parser = Parser {
|
|
24
|
+
tokens,
|
|
25
|
+
cursor: 0,
|
|
26
|
+
scopes: vec![HashMap::new()],
|
|
27
|
+
functions: vec![Function::new(1)],
|
|
28
|
+
next_binding: 0,
|
|
29
|
+
};
|
|
30
|
+
parser.block()?;
|
|
31
|
+
parser.expect("")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fn long_bracket(input: &[u8], start: usize) -> Option<(usize, usize)> {
|
|
35
|
+
if input.get(start) != Some(&b'[') {
|
|
36
|
+
return None;
|
|
37
|
+
}
|
|
38
|
+
let mut at = start + 1;
|
|
39
|
+
while input.get(at) == Some(&b'=') {
|
|
40
|
+
at += 1;
|
|
41
|
+
}
|
|
42
|
+
(input.get(at) == Some(&b'[')).then_some((at - start - 1, at + 1))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fn skip_long(input: &[u8], start: usize, equals: usize) -> usize {
|
|
46
|
+
for at in start..input.len() {
|
|
47
|
+
if input[at] == b']'
|
|
48
|
+
&& input.get(at + equals + 1) == Some(&b']')
|
|
49
|
+
&& input[at + 1..at + equals + 1]
|
|
50
|
+
.iter()
|
|
51
|
+
.all(|&byte| byte == b'=')
|
|
52
|
+
{
|
|
53
|
+
return at + equals + 2;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
input.len()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fn skip_quoted(input: &[u8], start: usize, quote: u8) -> usize {
|
|
60
|
+
let mut at = start + 1;
|
|
61
|
+
while at < input.len() {
|
|
62
|
+
match input[at] {
|
|
63
|
+
b'\\' => at = (at + 2).min(input.len()),
|
|
64
|
+
byte if byte == quote => return at + 1,
|
|
65
|
+
_ => at += 1,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
at
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fn number_end(input: &[u8], start: usize) -> usize {
|
|
72
|
+
let mut at = start;
|
|
73
|
+
if matches!(input.get(start..start + 2), Some(b"0x" | b"0X")) {
|
|
74
|
+
at += 2;
|
|
75
|
+
while input.get(at).is_some_and(u8::is_ascii_hexdigit) {
|
|
76
|
+
at += 1;
|
|
77
|
+
}
|
|
78
|
+
return at;
|
|
79
|
+
}
|
|
80
|
+
while input.get(at).is_some_and(u8::is_ascii_digit) {
|
|
81
|
+
at += 1;
|
|
82
|
+
}
|
|
83
|
+
if input.get(at) == Some(&b'.') && input.get(at + 1) != Some(&b'.') {
|
|
84
|
+
at += 1;
|
|
85
|
+
while input.get(at).is_some_and(u8::is_ascii_digit) {
|
|
86
|
+
at += 1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if matches!(input.get(at), Some(b'e' | b'E')) {
|
|
90
|
+
at += 1;
|
|
91
|
+
if matches!(input.get(at), Some(b'+' | b'-')) {
|
|
92
|
+
at += 1;
|
|
93
|
+
}
|
|
94
|
+
while input.get(at).is_some_and(u8::is_ascii_digit) {
|
|
95
|
+
at += 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
at
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
fn tokenize(source: &str) -> Vec<Token<'_>> {
|
|
102
|
+
let input = source.as_bytes();
|
|
103
|
+
let mut tokens = Vec::new();
|
|
104
|
+
let mut at = 0;
|
|
105
|
+
let mut line = 1;
|
|
106
|
+
while let Some(&byte) = input.get(at) {
|
|
107
|
+
let start = at;
|
|
108
|
+
let kind = if byte.is_ascii_whitespace() {
|
|
109
|
+
if byte == b'\n' {
|
|
110
|
+
line += 1;
|
|
111
|
+
}
|
|
112
|
+
at += 1;
|
|
113
|
+
continue;
|
|
114
|
+
} else if input.get(at..at + 2) == Some(b"--") {
|
|
115
|
+
at = match long_bracket(input, at + 2) {
|
|
116
|
+
Some((equals, content)) => skip_long(input, content, equals),
|
|
117
|
+
None => input[at..]
|
|
118
|
+
.iter()
|
|
119
|
+
.position(|&ch| ch == b'\n')
|
|
120
|
+
.map_or(input.len(), |end| at + end),
|
|
121
|
+
};
|
|
122
|
+
line += input[start..at].iter().filter(|&&ch| ch == b'\n').count();
|
|
123
|
+
continue;
|
|
124
|
+
} else if byte == b'\'' || byte == b'"' {
|
|
125
|
+
at = skip_quoted(input, at, byte);
|
|
126
|
+
Kind::String
|
|
127
|
+
} else if let Some((equals, content)) = long_bracket(input, at) {
|
|
128
|
+
at = skip_long(input, content, equals);
|
|
129
|
+
Kind::String
|
|
130
|
+
} else if byte.is_ascii_alphabetic() || byte == b'_' {
|
|
131
|
+
at += 1;
|
|
132
|
+
while input
|
|
133
|
+
.get(at)
|
|
134
|
+
.is_some_and(|ch| ch.is_ascii_alphanumeric() || *ch == b'_')
|
|
135
|
+
{
|
|
136
|
+
at += 1;
|
|
137
|
+
}
|
|
138
|
+
let word = &source[start..at];
|
|
139
|
+
match word {
|
|
140
|
+
"and" | "break" | "do" | "else" | "elseif" | "end" | "false" | "for"
|
|
141
|
+
| "function" | "if" | "in" | "local" | "nil" | "not" | "or" | "repeat"
|
|
142
|
+
| "return" | "then" | "true" | "until" | "while" => Kind::Other,
|
|
143
|
+
_ => Kind::Name,
|
|
144
|
+
}
|
|
145
|
+
} else if byte.is_ascii_digit()
|
|
146
|
+
|| byte == b'.' && input.get(at + 1).is_some_and(u8::is_ascii_digit)
|
|
147
|
+
{
|
|
148
|
+
at = number_end(input, at);
|
|
149
|
+
Kind::Number
|
|
150
|
+
} else {
|
|
151
|
+
at += match input.get(at..at + 3) {
|
|
152
|
+
Some(b"...") => 3,
|
|
153
|
+
_ if matches!(
|
|
154
|
+
input.get(at..at + 2),
|
|
155
|
+
Some(b".." | b"==" | b"~=" | b"<=" | b">=")
|
|
156
|
+
) =>
|
|
157
|
+
{
|
|
158
|
+
2
|
|
159
|
+
}
|
|
160
|
+
_ => source[at..]
|
|
161
|
+
.chars()
|
|
162
|
+
.next()
|
|
163
|
+
.expect("token byte exists")
|
|
164
|
+
.len_utf8(),
|
|
165
|
+
};
|
|
166
|
+
Kind::Other
|
|
167
|
+
};
|
|
168
|
+
tokens.push(Token {
|
|
169
|
+
text: &source[start..at],
|
|
170
|
+
kind,
|
|
171
|
+
line,
|
|
172
|
+
});
|
|
173
|
+
line += input[start..at].iter().filter(|&&ch| ch == b'\n').count();
|
|
174
|
+
}
|
|
175
|
+
tokens.push(Token {
|
|
176
|
+
text: "",
|
|
177
|
+
kind: Kind::Eof,
|
|
178
|
+
line,
|
|
179
|
+
});
|
|
180
|
+
tokens
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
#[derive(Clone, Copy)]
|
|
184
|
+
struct Binding {
|
|
185
|
+
id: usize,
|
|
186
|
+
function: usize,
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
struct Function {
|
|
190
|
+
line: usize,
|
|
191
|
+
upvalues: HashSet<usize>,
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
impl Function {
|
|
195
|
+
fn new(line: usize) -> Self {
|
|
196
|
+
Self {
|
|
197
|
+
line,
|
|
198
|
+
upvalues: HashSet::new(),
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
type Parse = Result<(), String>;
|
|
204
|
+
|
|
205
|
+
struct Parser<'a> {
|
|
206
|
+
tokens: Vec<Token<'a>>,
|
|
207
|
+
cursor: usize,
|
|
208
|
+
scopes: Vec<HashMap<&'a str, Binding>>,
|
|
209
|
+
functions: Vec<Function>,
|
|
210
|
+
next_binding: usize,
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
impl<'a> Parser<'a> {
|
|
214
|
+
fn peek(&self) -> Token<'a> {
|
|
215
|
+
self.tokens[self.cursor]
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
fn take(&mut self) -> Token<'a> {
|
|
219
|
+
let token = self.peek();
|
|
220
|
+
if token.kind != Kind::Eof {
|
|
221
|
+
self.cursor += 1;
|
|
222
|
+
}
|
|
223
|
+
token
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
fn is(&self, text: &str) -> bool {
|
|
227
|
+
let token = self.peek();
|
|
228
|
+
matches!(token.kind, Kind::Name | Kind::Other | Kind::Eof) && token.text == text
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
fn eat(&mut self, text: &str) -> bool {
|
|
232
|
+
if !self.is(text) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
self.take();
|
|
236
|
+
true
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fn expected(&self, what: &str) -> String {
|
|
240
|
+
let found = self.peek();
|
|
241
|
+
format!(
|
|
242
|
+
"Could not check generated Lua at line {}: expected {}, found {:?}",
|
|
243
|
+
found.line, what, found.text
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
fn expect(&mut self, text: &str) -> Parse {
|
|
248
|
+
if self.eat(text) {
|
|
249
|
+
Ok(())
|
|
250
|
+
} else {
|
|
251
|
+
Err(self.expected(text))
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
fn name(&mut self) -> Result<&'a str, String> {
|
|
256
|
+
if self.peek().kind != Kind::Name {
|
|
257
|
+
return Err(self.expected("a name"));
|
|
258
|
+
}
|
|
259
|
+
Ok(self.take().text)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
fn declare(&mut self, name: &'a str) {
|
|
263
|
+
let binding = Binding {
|
|
264
|
+
id: self.next_binding,
|
|
265
|
+
function: self.functions.len() - 1,
|
|
266
|
+
};
|
|
267
|
+
self.next_binding += 1;
|
|
268
|
+
self.scopes
|
|
269
|
+
.last_mut()
|
|
270
|
+
.expect("active scope")
|
|
271
|
+
.insert(name, binding);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn reference(&mut self, name: &str) -> Parse {
|
|
275
|
+
if let Some(binding) = self.scopes.iter().rev().find_map(|scope| scope.get(name)) {
|
|
276
|
+
// Lua passes a captured local through every intervening closure.
|
|
277
|
+
for function in &mut self.functions[binding.function + 1..] {
|
|
278
|
+
function.upvalues.insert(binding.id);
|
|
279
|
+
if function.upvalues.len() > MAX_UPVALUES {
|
|
280
|
+
return Err(format!(
|
|
281
|
+
"Lua 5.1 function at generated line {} captures more than {} upvalues",
|
|
282
|
+
function.line, MAX_UPVALUES
|
|
283
|
+
));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
Ok(())
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
fn block_end(&self) -> bool {
|
|
291
|
+
["", "end", "else", "elseif", "until"]
|
|
292
|
+
.into_iter()
|
|
293
|
+
.any(|text| self.is(text))
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn block(&mut self) -> Parse {
|
|
297
|
+
while !self.block_end() {
|
|
298
|
+
self.statement()?;
|
|
299
|
+
}
|
|
300
|
+
Ok(())
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
fn scoped_block(&mut self, locals: &[&'a str]) -> Parse {
|
|
304
|
+
self.scopes.push(HashMap::new());
|
|
305
|
+
for &name in locals {
|
|
306
|
+
self.declare(name);
|
|
307
|
+
}
|
|
308
|
+
self.block()?;
|
|
309
|
+
self.scopes.pop();
|
|
310
|
+
Ok(())
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
fn statement(&mut self) -> Parse {
|
|
314
|
+
let token = self.peek();
|
|
315
|
+
match token.text {
|
|
316
|
+
";" | "break" => {
|
|
317
|
+
self.take();
|
|
318
|
+
Ok(())
|
|
319
|
+
}
|
|
320
|
+
"local" => {
|
|
321
|
+
self.take();
|
|
322
|
+
self.local()
|
|
323
|
+
}
|
|
324
|
+
"function" => {
|
|
325
|
+
self.take();
|
|
326
|
+
self.named_function(token.line)
|
|
327
|
+
}
|
|
328
|
+
"do" => {
|
|
329
|
+
self.take();
|
|
330
|
+
self.scoped_block(&[])?;
|
|
331
|
+
self.expect("end")
|
|
332
|
+
}
|
|
333
|
+
"while" => {
|
|
334
|
+
self.take();
|
|
335
|
+
self.expression()?;
|
|
336
|
+
self.expect("do")?;
|
|
337
|
+
self.scoped_block(&[])?;
|
|
338
|
+
self.expect("end")
|
|
339
|
+
}
|
|
340
|
+
"repeat" => {
|
|
341
|
+
self.take();
|
|
342
|
+
self.scopes.push(HashMap::new());
|
|
343
|
+
self.block()?;
|
|
344
|
+
self.expect("until")?;
|
|
345
|
+
self.expression()?;
|
|
346
|
+
self.scopes.pop();
|
|
347
|
+
Ok(())
|
|
348
|
+
}
|
|
349
|
+
"if" => {
|
|
350
|
+
self.take();
|
|
351
|
+
self.conditional()
|
|
352
|
+
}
|
|
353
|
+
"for" => {
|
|
354
|
+
self.take();
|
|
355
|
+
self.for_loop()
|
|
356
|
+
}
|
|
357
|
+
"return" => {
|
|
358
|
+
self.take();
|
|
359
|
+
if !self.block_end() && !self.is(";") {
|
|
360
|
+
self.expression_list()?;
|
|
361
|
+
}
|
|
362
|
+
Ok(())
|
|
363
|
+
}
|
|
364
|
+
_ => {
|
|
365
|
+
self.prefix()?;
|
|
366
|
+
while self.eat(",") {
|
|
367
|
+
self.prefix()?;
|
|
368
|
+
}
|
|
369
|
+
if self.eat("=") {
|
|
370
|
+
self.expression_list()?;
|
|
371
|
+
}
|
|
372
|
+
Ok(())
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
fn conditional(&mut self) -> Parse {
|
|
378
|
+
loop {
|
|
379
|
+
self.expression()?;
|
|
380
|
+
self.expect("then")?;
|
|
381
|
+
self.scoped_block(&[])?;
|
|
382
|
+
if !self.eat("elseif") {
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if self.eat("else") {
|
|
387
|
+
self.scoped_block(&[])?;
|
|
388
|
+
}
|
|
389
|
+
self.expect("end")
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
fn names(&mut self) -> Result<Vec<&'a str>, String> {
|
|
393
|
+
let mut names = vec![self.name()?];
|
|
394
|
+
while self.eat(",") {
|
|
395
|
+
names.push(self.name()?);
|
|
396
|
+
}
|
|
397
|
+
Ok(names)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
fn for_loop(&mut self) -> Parse {
|
|
401
|
+
let names = self.names()?;
|
|
402
|
+
if !self.eat("=") {
|
|
403
|
+
self.expect("in")?;
|
|
404
|
+
}
|
|
405
|
+
self.expression_list()?;
|
|
406
|
+
self.expect("do")?;
|
|
407
|
+
self.scoped_block(&names)?;
|
|
408
|
+
self.expect("end")
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
fn local(&mut self) -> Parse {
|
|
412
|
+
let token = self.peek();
|
|
413
|
+
if self.eat("function") {
|
|
414
|
+
let name = self.name()?;
|
|
415
|
+
self.declare(name);
|
|
416
|
+
return self.function(token.line, false);
|
|
417
|
+
}
|
|
418
|
+
let names = self.names()?;
|
|
419
|
+
if self.eat("=") {
|
|
420
|
+
self.expression_list()?;
|
|
421
|
+
}
|
|
422
|
+
for name in names {
|
|
423
|
+
self.declare(name);
|
|
424
|
+
}
|
|
425
|
+
Ok(())
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
fn named_function(&mut self, line: usize) -> Parse {
|
|
429
|
+
let name = self.name()?;
|
|
430
|
+
self.reference(name)?;
|
|
431
|
+
while self.eat(".") {
|
|
432
|
+
self.name()?;
|
|
433
|
+
}
|
|
434
|
+
let method = self.eat(":");
|
|
435
|
+
if method {
|
|
436
|
+
self.name()?;
|
|
437
|
+
}
|
|
438
|
+
self.function(line, method)
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
fn function(&mut self, line: usize, method: bool) -> Parse {
|
|
442
|
+
self.functions.push(Function::new(line));
|
|
443
|
+
self.scopes.push(HashMap::new());
|
|
444
|
+
if method {
|
|
445
|
+
self.declare("self");
|
|
446
|
+
}
|
|
447
|
+
self.expect("(")?;
|
|
448
|
+
if !self.is(")") {
|
|
449
|
+
loop {
|
|
450
|
+
if self.eat("...") {
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
let name = self.name()?;
|
|
454
|
+
self.declare(name);
|
|
455
|
+
if !self.eat(",") {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
self.expect(")")?;
|
|
461
|
+
self.block()?;
|
|
462
|
+
self.expect("end")?;
|
|
463
|
+
self.scopes.pop();
|
|
464
|
+
self.functions.pop();
|
|
465
|
+
Ok(())
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
fn expression_list(&mut self) -> Parse {
|
|
469
|
+
self.expression()?;
|
|
470
|
+
while self.eat(",") {
|
|
471
|
+
self.expression()?;
|
|
472
|
+
}
|
|
473
|
+
Ok(())
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
fn expression(&mut self) -> Parse {
|
|
477
|
+
self.term()?;
|
|
478
|
+
while matches!(self.peek().kind, Kind::Other)
|
|
479
|
+
&& matches!(
|
|
480
|
+
self.peek().text,
|
|
481
|
+
"+" | "-"
|
|
482
|
+
| "*"
|
|
483
|
+
| "/"
|
|
484
|
+
| "%"
|
|
485
|
+
| "^"
|
|
486
|
+
| ".."
|
|
487
|
+
| "<"
|
|
488
|
+
| ">"
|
|
489
|
+
| "<="
|
|
490
|
+
| ">="
|
|
491
|
+
| "=="
|
|
492
|
+
| "~="
|
|
493
|
+
| "and"
|
|
494
|
+
| "or"
|
|
495
|
+
)
|
|
496
|
+
{
|
|
497
|
+
self.take();
|
|
498
|
+
self.term()?;
|
|
499
|
+
}
|
|
500
|
+
Ok(())
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
fn term(&mut self) -> Parse {
|
|
504
|
+
while self.eat("not") || self.eat("#") || self.eat("-") {}
|
|
505
|
+
let token = self.peek();
|
|
506
|
+
if self.eat("function") {
|
|
507
|
+
return self.function(token.line, false);
|
|
508
|
+
}
|
|
509
|
+
if self.is("{") {
|
|
510
|
+
return self.table();
|
|
511
|
+
}
|
|
512
|
+
if matches!(token.kind, Kind::Number | Kind::String)
|
|
513
|
+
|| self.is("true")
|
|
514
|
+
|| self.is("false")
|
|
515
|
+
|| self.is("nil")
|
|
516
|
+
|| self.is("...")
|
|
517
|
+
{
|
|
518
|
+
self.take();
|
|
519
|
+
return Ok(());
|
|
520
|
+
}
|
|
521
|
+
self.prefix()
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
fn prefix(&mut self) -> Parse {
|
|
525
|
+
if self.eat("(") {
|
|
526
|
+
self.expression()?;
|
|
527
|
+
self.expect(")")?;
|
|
528
|
+
} else {
|
|
529
|
+
let name = self.name()?;
|
|
530
|
+
self.reference(name)?;
|
|
531
|
+
}
|
|
532
|
+
loop {
|
|
533
|
+
if self.eat("[") {
|
|
534
|
+
self.expression()?;
|
|
535
|
+
self.expect("]")?;
|
|
536
|
+
} else if self.eat(".") {
|
|
537
|
+
self.name()?;
|
|
538
|
+
} else if self.eat(":") {
|
|
539
|
+
self.name()?;
|
|
540
|
+
self.arguments()?;
|
|
541
|
+
} else if self.is("(") || self.is("{") || self.peek().kind == Kind::String {
|
|
542
|
+
self.arguments()?;
|
|
543
|
+
} else {
|
|
544
|
+
return Ok(());
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
fn arguments(&mut self) -> Parse {
|
|
550
|
+
if self.eat("(") {
|
|
551
|
+
if !self.is(")") {
|
|
552
|
+
self.expression_list()?;
|
|
553
|
+
}
|
|
554
|
+
return self.expect(")");
|
|
555
|
+
}
|
|
556
|
+
if self.is("{") {
|
|
557
|
+
return self.table();
|
|
558
|
+
}
|
|
559
|
+
if self.peek().kind == Kind::String {
|
|
560
|
+
self.take();
|
|
561
|
+
return Ok(());
|
|
562
|
+
}
|
|
563
|
+
Err(self.expected("function arguments"))
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
fn table(&mut self) -> Parse {
|
|
567
|
+
self.expect("{")?;
|
|
568
|
+
while !self.is("}") {
|
|
569
|
+
if self.eat("[") {
|
|
570
|
+
self.expression()?;
|
|
571
|
+
self.expect("]")?;
|
|
572
|
+
self.expect("=")?;
|
|
573
|
+
} else if self.peek().kind == Kind::Name && self.tokens[self.cursor + 1].text == "=" {
|
|
574
|
+
self.take();
|
|
575
|
+
self.take();
|
|
576
|
+
}
|
|
577
|
+
self.expression()?;
|
|
578
|
+
if !self.eat(",") && !self.eat(";") {
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
self.expect("}")
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
#[cfg(test)]
|
|
587
|
+
mod tests {
|
|
588
|
+
use super::*;
|
|
589
|
+
|
|
590
|
+
fn names(count: usize) -> Vec<String> {
|
|
591
|
+
(1..=count).map(|index| format!("value{index}")).collect()
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
#[test]
|
|
595
|
+
fn accepts_sixty_and_reports_the_next_capture() {
|
|
596
|
+
for count in [60, 61] {
|
|
597
|
+
let locals = names(count).join(", ");
|
|
598
|
+
let source = format!("local {locals}\nreturn function() return {locals} end");
|
|
599
|
+
let actual = validate(&source);
|
|
600
|
+
if count == 60 {
|
|
601
|
+
assert_eq!(actual, Ok(()));
|
|
602
|
+
} else {
|
|
603
|
+
assert_eq!(
|
|
604
|
+
actual,
|
|
605
|
+
Err(
|
|
606
|
+
"Lua 5.1 function at generated line 2 captures more than 60 upvalues"
|
|
607
|
+
.into()
|
|
608
|
+
)
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
#[test]
|
|
615
|
+
fn counts_captures_propagated_through_nested_closures() {
|
|
616
|
+
let locals = names(61);
|
|
617
|
+
let source = format!(
|
|
618
|
+
"local {}\nreturn function()\n return function() return {} end, function() return {} end\nend",
|
|
619
|
+
locals.join(", "),
|
|
620
|
+
locals[..30].join(", "),
|
|
621
|
+
locals[30..].join(", "),
|
|
622
|
+
);
|
|
623
|
+
assert_eq!(
|
|
624
|
+
validate(&source),
|
|
625
|
+
Err("Lua 5.1 function at generated line 2 captures more than 60 upvalues".into())
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
#[test]
|
|
630
|
+
fn keeps_function_parameters_and_block_locals_out_of_the_outer_capture_count() {
|
|
631
|
+
let outer = names(60).join(", ");
|
|
632
|
+
let source = format!(
|
|
633
|
+
"local {outer}, shadowed\nreturn function(shadowed)\n\
|
|
634
|
+
do local value1 = external; local read = function() return value1, shadowed end end\n\
|
|
635
|
+
for index = 1, 5 do local item = function() return index end end\n\
|
|
636
|
+
repeat local done = check() until done\n\
|
|
637
|
+
return {outer}, shadowed, object.value1, {{value2 = [=[function end]=]}}, 'x'\nend"
|
|
638
|
+
);
|
|
639
|
+
assert_eq!(validate(&source), Ok(()));
|
|
640
|
+
}
|
|
641
|
+
}
|