ahk 0.6.2

The ahk language: a lexer, an AST, a parser and an interpreter for AutoHotkey-style scripts
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! The class types of v2: the names that a script reads as a class.
//!
//! Measured on the binary of v2.0.19: a class type of the
//! dialect is a value, and its `__Class` is "Class". The name `Hotkey` is
//! the one that is not: it is a function of the dialect, and the class of
//! a hotkey is the class of the object that the function makes.

use ahk::desktop::{
    Clipboard, Com, Desktop, Dialogs, Files, Hotkeys, Keyboard, Machine, Native, Network, Pictures,
    Pointer, Processes, Screen, Sound, Timers, Windows,
};
use ahk::parse;
use ahk::runtime::{Report, Runtime};
use std::time::Duration;

/// A desktop that does nothing.
#[derive(Debug, Default)]
struct Fake;

impl Desktop for Fake {}
impl Pictures for Fake {}
impl Network for Fake {}
impl Sound for Fake {}
impl Machine for Fake {}
impl Dialogs for Fake {}

impl Files for Fake {}

impl Clipboard for Fake {
    fn clipboard_read_all(&mut self) -> Result<Vec<u8>, String> {
        Ok(Vec::new())
    }
    fn clipboard_write_all(&mut self, _data: &[u8]) -> Result<(), String> {
        Ok(())
    }
}

impl Timers for Fake {}

impl Hotkeys for Fake {}

impl Keyboard for Fake {
    fn type_text(&mut self, _text: &str) -> Result<String, String> {
        Ok(String::new())
    }
    fn press_key(&mut self, _name: &str, _modifiers: &[String]) -> Result<String, String> {
        Ok(String::new())
    }
}

impl Pointer for Fake {
    fn click(&mut self, _x: i64, _y: i64, _button: &str, _count: u32) -> Result<String, String> {
        Ok(String::new())
    }
}

impl Screen for Fake {
    fn capture(
        &mut self,
        _x: i64,
        _y: i64,
        _width: i64,
        _height: i64,
        _filename: &str,
    ) -> Result<String, String> {
        Ok(String::new())
    }
    fn pixel(&mut self, _x: i64, _y: i64) -> Result<String, String> {
        Ok("#000000".into())
    }
}

impl Windows for Fake {}

impl Processes for Fake {
    fn run(&mut self, _command: &str, _window: &str) -> Result<(i32, String, String), String> {
        Ok((0, String::new(), String::new()))
    }
}

impl Native for Fake {}
impl Com for Fake {}

/// A script, and the report of it.
fn run(source: &str) -> Report {
    let mut desktop = Fake;
    let statements = parse(source).expect("parses");
    Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements)
}

/// The class of the value of a name.
fn the_class_of(name: &str) -> String {
    let report = run(&format!("x := {name}\nMsgBox x.__Class"));
    assert!(report.ok, "{:?}", report.error);
    report.output.first().cloned().unwrap_or_default()
}

#[test]
fn a_class_type_of_the_dialect_is_a_class() {
    for name in [
        "Any",
        "Array",
        "BoundFunc",
        "Buffer",
        "Class",
        "ClipboardAll",
        "Closure",
        "ComObject",
        "ComValue",
        "Enumerator",
        "Error",
        "File",
        "Func",
        "Gui",
        "InputHook",
        "Map",
        "Menu",
        "Object",
        "RegExMatchInfo",
    ] {
        assert_eq!(the_class_of(name), "Class", "the class of {name}");
    }
}

#[test]
fn the_class_of_the_errors_is_a_class() {
    for name in [
        "MemoryError",
        "OSError",
        "TargetError",
        "TimeoutError",
        "TypeError",
        "UnsetError",
        "MemberError",
        "PropertyError",
        "MethodError",
        "UnsetItemError",
        "ValueError",
        "IndexError",
        "ZeroDivisionError",
    ] {
        assert_eq!(the_class_of(name), "Class", "the class of {name}");
    }
}

#[test]
fn the_name_of_a_hotkey_is_a_function() {
    // Measured on the binary of v2.0.19: `x := Hotkey` is a
    // function of the dialect, and the class of a hotkey is the class of
    // the object that the function makes.
    assert_eq!(the_class_of("Hotkey"), "Func");
}

#[test]
fn a_class_type_is_spelled_the_way_v2_spells_it() {
    for (name, spelled) in [
        ("BoundFunc", "BoundFunc"),
        ("ClipboardAll", "ClipboardAll"),
        ("Closure", "Closure"),
        ("ComObject", "ComObject"),
        ("ComValue", "ComValue"),
        ("InputHook", "InputHook"),
        ("Map", "Map"),
        ("RegExMatchInfo", "RegExMatchInfo"),
        ("TypeError", "TypeError"),
    ] {
        // Measured on the binary of v2.0.19: a class value
        // has no name of its own, and the spelling of the class is seen
        // through its prototype.
        let report = run(&format!("x := {name}\nMsgBox x.Prototype.__Class"));
        assert!(report.ok, "{:?}", report.error);
        assert_eq!(report.output.first().cloned().unwrap_or_default(), spelled);
    }
}

/// The failure of a call: the message, the `What` and the `Extra`.
///
/// The three of them are the three of v2, and the script reads them
/// through a catch: the failure of the dialect is not a value.
fn the_failure_of(source: &str) -> String {
    let report = run(&format!(
        "try {{\n    {source}\n}} catch Any as e {{\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}}\n"
    ));
    assert!(report.ok, "{:?}", report.error);
    report
        .output
        .first()
        .cloned()
        .unwrap_or_default()
        .trim()
        .to_string()
}

#[test]
fn the_call_of_a_class_that_v2_does_not_make_is_refused() {
    // Measured on the binary of v2.0.19: the `BoundFunc`,
    // the `Closure` and the `RegExMatchInfo` are classes of the dialect
    // that the runtime makes, and not a script: the call of them is
    // refused with "Invalid base.", the way the call of the `File` is.
    for name in ["BoundFunc", "Closure", "RegExMatchInfo"] {
        assert_eq!(
            the_failure_of(&format!("{name}()")),
            "Invalid base. | Object.Call |",
            "the call of {name}"
        );
    }
}

#[test]
fn the_calls_of_the_classes_are_the_calls_of_v2() {
    // Measured on the binary of v2.0.19.
    for (call, answer) in [
        // The `Any` is the type of any value, and it is not a class that
        // can be made: the call of it goes through the `Call` method of
        // the class, and a class has none.
        (
            "Any()",
            "This value of type \"Class\" has no method named \"Call\". |  |",
        ),
        (
            "Any(1)",
            "This value of type \"Class\" has no method named \"Call\". |  |",
        ),
        // The number of the arguments of the constructors, and the names
        // that go in the `What` and in the `Extra` of the refusal.
        (
            "ComObject()",
            "Too few parameters passed to function. |  | ComObject.Call",
        ),
        (
            "ComObject(1, 2, 3)",
            "Too many parameters passed to function. |  | ComObject.Call",
        ),
        (
            "ComValue()",
            "Too few parameters passed to function. |  | ComValue.Call",
        ),
        (
            "ComValue(1)",
            "Too few parameters passed to function. |  | ComValue.Call",
        ),
        (
            "ComValue(\"x\", 1)",
            "Parameter #1 of ComValue.Call requires a Number, but received a String. | ComValue.Call | x",
        ),
        (
            "ComValue(1, 2, \"x\")",
            "Parameter #3 of ComValue.Call requires a Number, but received a String. | ComValue.Call | x",
        ),
        (
            "Class(1)",
            "Too many parameters passed to function. | Object.Call |",
        ),
        (
            "Object(1)",
            "Too many parameters passed to function. | Object.Call |",
        ),
        (
            "Menu(1)",
            "Too many parameters passed to function. | Menu.Call |",
        ),
        (
            "ClipboardAll(\"x\")",
            "Parameter #1 of ClipboardAll.Prototype.__New requires a Number, but received a String. | ClipboardAll.Prototype.__New | x",
        ),
        (
            "ClipboardAll(0)",
            "Parameter #1 of ClipboardAll.Prototype.__New is invalid. | ClipboardAll.Prototype.__New | 0",
        ),
        (
            "ClipboardAll(-1)",
            "Parameter #2 of ClipboardAll.Prototype.__New must not be omitted in this case. | ClipboardAll.Prototype.__New |",
        ),
        (
            "ClipboardAll(1, 2, 3)",
            "Too many parameters passed to function. | ClipboardAll.Call | ClipboardAll.Prototype.__New",
        ),
        (
            "InputHook(1, 2, 3, 4)",
            "Too many parameters passed to function. | InputHook.Call | InputHook.Prototype.__New",
        ),
        (
            "Buffer(1, 2, 3)",
            "Too many parameters passed to function. | Buffer.Call | Buffer.Prototype.__New",
        ),
        (
            "Error(\"a\", \"b\", \"c\", \"d\")",
            "Too many parameters passed to function. | Object.Call | Error.Prototype.__New",
        ),
        // The classes that v2 does not make: the call goes through the
        // base object, and the base of them has no such thing.
        ("File()", "Invalid base. | Object.Call |"),
        ("Func()", "Invalid base. | Object.Call |"),
        ("Enumerator()", "Invalid base. | Object.Call |"),
    ] {
        assert_eq!(the_failure_of(call), answer, "the call of {call}");
    }
}

#[test]
fn the_own_properties_of_a_function_are_none() {
    // Measured on the binary of v2.0.19: the members of a
    // function -- the name, the number of the parameters and the rest --
    // live on the prototype of the class of it, and the function owns none
    // of them.
    let report = run("f := Abs\ns := \"\"\nfor p in f.OwnProps()\n    s .= p\nMsgBox s");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output.first().cloned().unwrap_or_default(), "");
    let report = run("f := Abs\nMsgBox f.HasOwnProp(\"Name\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output.first().cloned().unwrap_or_default(), "0");
    // The name is read all the same.
    let report = run("f := Abs\nMsgBox f.Name");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output.first().cloned().unwrap_or_default(), "Abs");
    // A method of a class of the script is a function as well.
    let report = run(
        "class C {\n    static Say() {\n    }\n}\nf := C.Say\ns := \"\"\nfor p in f.OwnProps()\n    s .= p\nMsgBox s",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output.first().cloned().unwrap_or_default(), "");
}

#[test]
fn the_call_of_a_class_value_makes_an_object() {
    // Measured on the binary of v2.0.19: a class is a value,
    // and the call of it makes an object of that class -- `c := Map; c()`
    // makes a map, the way `Map()` does.
    for (name, class) in [
        ("Array", "Array"),
        ("Buffer", "Buffer"),
        ("Class", "Class"),
        ("ClipboardAll", "ClipboardAll"),
        ("Error", "Error"),
        ("Gui", "Gui"),
        ("InputHook", "InputHook"),
        ("Map", "Map"),
        ("Menu", "Menu"),
        ("Object", "Object"),
    ] {
        let report = run(&format!("c := {name}\nx := c()\nMsgBox x.__Class"));
        assert!(report.ok, "{:?}", report.error);
        assert_eq!(
            report.output.first().cloned().unwrap_or_default(),
            class,
            "the call of {name}"
        );
    }
}

#[test]
fn a_class_that_the_dialect_does_not_make_says_so_when_it_is_called() {
    // The `Any` is not a class that can be made, and the `File`, the
    // `Func` and the `Enumerator` are not made at all.
    assert_eq!(
        the_failure_of("c := Any\nc()"),
        "This value of type \"Class\" has no method named \"Call\". |  |"
    );
    for name in ["File", "Func", "Enumerator"] {
        assert_eq!(
            the_failure_of(&format!("c := {name}\nc()")),
            "Invalid base. | Object.Call |",
            "the call of {name}"
        );
    }
}

#[test]
fn a_class_of_windows_that_this_runtime_does_not_carry_says_so() {
    // Measured on the binary of v2.0.19: the `ComObject`
    // and the `ComValue` are classes of the dialect, and the binary makes
    // the COM object of them. This runtime has no COM, and it says so.
    for call in ["ComObject(\"Shell.Application\")", "ComValue(1, 2)"] {
        assert_eq!(
            the_failure_of(call),
            "the COM objects are a thing of Windows, and this runtime does not carry them. |  |",
            "the call of {call}"
        );
    }
}

#[test]
fn a_class_that_this_runtime_does_not_carry_says_why() {
    // The `InputHook` is a keyboard hook, and this runtime has none. The
    // name is there, and the tree does not carry it: the refusal says so,
    // and it does not say that the name is not there.
    for (source, answer) in [
        (
            "ih := InputHook()\nih.Visible",
            "this runtime does not carry that class yet. |  |",
        ),
        (
            "ih := InputHook()\nih.KeyOpt(\"a\")",
            "this runtime does not carry that class yet. |  |",
        ),
    ] {
        assert_eq!(the_failure_of(source), answer, "the member of {source}");
    }
}

#[test]
fn a_class_value_keeps_the_words_of_the_dialect() {
    // A class value is a class of the dialect, and the refusal of a member
    // that is not there keeps the words of the dialect -- the class is
    // there, and the tree carries it.
    for source in ["a := Any\nx := a.Nope", "o := Object\nx := o.Nope"] {
        assert_eq!(
            the_failure_of(source),
            "This value of type \"Class\" has no property named \"Nope\". |  |",
            "the member of {source}"
        );
    }
}

#[test]
fn the_clipboard_all_is_made_with_nothing() {
    // The call of the class with no arguments captures the clipboard
    // (the fake has an empty one) and makes the object.
    let report = run("x := ClipboardAll()\nMsgBox x.__Class");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output.first().cloned().unwrap_or_default(),
        "ClipboardAll"
    );
}