awkrs 0.4.12

Awk implementation in Rust with broad CLI compatibility, parallel records, and experimental Cranelift JIT
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Gawk-style source directives before parse: `@include`, `@load` (`.awk` only, like include), `@namespace`.

use crate::error::{Error, Result};
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};

fn take_double_quoted(rest: &str) -> Option<(String, &str)> {
    let rest = rest.trim_start();
    let b = rest.as_bytes();
    if b.first() != Some(&b'"') {
        return None;
    }
    let mut out = String::new();
    let mut i = 1usize;
    while i < b.len() {
        if b[i] == b'"' {
            return Some((out, &rest[i + 1..]));
        }
        if b[i] == b'\\' && i + 1 < b.len() {
            i += 1;
            match b[i] {
                b'n' => out.push('\n'),
                b't' => out.push('\t'),
                b'r' => out.push('\r'),
                b'\\' | b'"' => out.push(b[i] as char),
                x => out.push(x as char),
            }
            i += 1;
            continue;
        }
        if b[i] == b'\n' {
            return None;
        }
        let ch = rest[i..].chars().next()?;
        out.push(ch);
        i += ch.len_utf8();
    }
    None
}

/// gawk’s **bundled** extension module names (typically `@load "filefuncs"` or `filefuncs.so`).
/// awkrs implements these in Rust; the directive is accepted and ignored (no `dlopen`).
const NATIVE_GAWK_EXTENSIONS: &[&str] = &[
    "filefuncs",
    "readdir",
    "time",
    "inplace",
    "ordchr",
    "readfile",
    "revoutput",
    "revtwoway",
    "rwarray",
    "intdiv",
];

/// True when `path_str` refers to one of those modules (with or without `.so`, any directory prefix).
fn is_native_gawk_extension_path(path_str: &str) -> bool {
    let stem = Path::new(path_str)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(path_str);
    let name = stem.to_ascii_lowercase();
    NATIVE_GAWK_EXTENSIONS.contains(&name.as_str())
}

fn take_bare_ident(rest: &str) -> Option<(String, &str)> {
    let rest = rest.trim_start();
    let mut i = 0usize;
    let b = rest.as_bytes();
    let c0 = *b.first()?;
    if !(c0.is_ascii_alphabetic() || c0 == b'_') {
        return None;
    }
    i += 1;
    while i < b.len() {
        let c = b[i];
        if c.is_ascii_alphanumeric() || c == b'_' {
            i += 1;
        } else {
            break;
        }
    }
    Some((rest[..i].to_string(), &rest[i..]))
}

/// Expanded program text plus `@namespace` default (gawk-style).
#[derive(Debug, Clone)]
pub struct ExpandedSource {
    /// `text` field.
    pub text: String,
    /// `default_namespace` field.
    pub default_namespace: Option<String>,
}

/// Expand `@include` / `@load "*.awk"` recursively; apply `@namespace` (line removed; namespace recorded).
pub fn expand_source_directives(src: &str) -> Result<ExpandedSource> {
    let mut visited = HashSet::new();
    let mut default_ns = None;
    let text = expand_inner(src, None, &mut visited, &mut default_ns)?;
    Ok(ExpandedSource {
        text,
        default_namespace: default_ns,
    })
}

fn expand_inner(
    text: &str,
    base_dir: Option<&Path>,
    visited: &mut HashSet<PathBuf>,
    default_ns: &mut Option<String>,
) -> Result<String> {
    let mut out = String::new();
    for (line_no, line) in text.lines().enumerate() {
        let line_no = line_no + 1;
        let trimmed = line.trim_start();
        if let Some(rest) = trimmed.strip_prefix("@include") {
            let rest = rest.trim_start();
            let Some((path_str, _after)) = take_double_quoted(rest) else {
                return Err(Error::Parse {
                    line: line_no,
                    msg: "malformed `@include` (expected `@include \"file\"`)".into(),
                });
            };
            let resolved = resolve_include_path(base_dir, &path_str)?;
            let canon = fs::canonicalize(&resolved).unwrap_or_else(|_| resolved.clone());
            if !visited.insert(canon.clone()) {
                return Err(Error::Parse {
                    line: line_no,
                    msg: format!("@include cycle: {}", canon.display()),
                });
            }
            let inner = fs::read_to_string(&resolved)
                .map_err(|e| Error::ProgramFile(resolved.clone(), e))?;
            let expanded = expand_inner(&inner, resolved.parent(), visited, default_ns)?;
            visited.remove(&canon);
            out.push_str(&expanded);
            if !expanded.is_empty() && !expanded.ends_with('\n') {
                out.push('\n');
            }
            continue;
        }
        if let Some(rest) = trimmed.strip_prefix("@load") {
            let rest = rest.trim_start();
            let Some((path_str, _after)) = take_double_quoted(rest) else {
                return Err(Error::Parse {
                    line: line_no,
                    msg: "malformed `@load` (expected `@load \"file\"`)".into(),
                });
            };
            let pl = path_str.to_ascii_lowercase();
            if pl.ends_with(".awk") {
                let resolved = resolve_include_path(base_dir, &path_str)?;
                let canon = fs::canonicalize(&resolved).unwrap_or_else(|_| resolved.clone());
                if !visited.insert(canon.clone()) {
                    return Err(Error::Parse {
                        line: line_no,
                        msg: format!("@load cycle: {}", canon.display()),
                    });
                }
                let inner = fs::read_to_string(&resolved)
                    .map_err(|e| Error::ProgramFile(resolved.clone(), e))?;
                let expanded = expand_inner(&inner, resolved.parent(), visited, default_ns)?;
                visited.remove(&canon);
                out.push_str(&expanded);
                if !expanded.is_empty() && !expanded.ends_with('\n') {
                    out.push('\n');
                }
                continue;
            }
            if is_native_gawk_extension_path(&path_str) {
                // Builtins already present for the whole run; gawkapi / dlopen not used.
                continue;
            }
            return Err(Error::Parse {
                line: line_no,
                msg: format!(
                    "`@load` {path_str}: awkrs only inlines `.awk` source or recognizes gawk’s \
                     bundled extension names (implemented natively). Arbitrary third-party `.so` \
                     modules (gawkapi) are not loaded"
                ),
            });
        }
        if trimmed.starts_with("@namespace") {
            let rest = trimmed.strip_prefix("@namespace").unwrap().trim_start();
            // After the namespace identifier, capture any trailing source (e.g.
            // `; BEGIN { … }` on the same line) so we don't silently drop it.
            let after_ns: &str = if let Some((ns, after)) = take_double_quoted(rest) {
                *default_ns = Some(ns);
                after
            } else if let Some((ns, after)) = take_bare_ident(rest) {
                *default_ns = Some(ns);
                after
            } else {
                return Err(Error::Parse {
                    line: line_no,
                    msg: "malformed `@namespace` (expected `@namespace \"name\"` or `@namespace name`)"
                        .into(),
                });
            };
            // gawk parity: `@namespace "name"; rest_of_program` is legal; emit
            // the remainder so the parser sees it as if the directive line had
            // ended where the directive ended.
            let after_ns = after_ns.trim_start();
            let rest_after_semi = after_ns.strip_prefix(';').unwrap_or(after_ns);
            if !rest_after_semi.trim().is_empty() {
                out.push_str(rest_after_semi);
                out.push('\n');
            }
            continue;
        }
        out.push_str(line);
        out.push('\n');
    }
    Ok(out)
}

fn resolve_include_path(base_dir: Option<&Path>, path_str: &str) -> Result<PathBuf> {
    let p = Path::new(path_str);
    if p.is_absolute() {
        Ok(p.to_path_buf())
    } else if let Some(dir) = base_dir {
        Ok(dir.join(p))
    } else {
        std::env::current_dir()
            .map(|cwd| cwd.join(p))
            .map_err(Error::Io)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn take_quoted_parses_path() {
        let (s, tail) = take_double_quoted(r#" "a/b.awk" x"#).unwrap();
        assert_eq!(s, "a/b.awk");
        assert_eq!(tail.trim(), "x");
    }

    #[test]
    fn take_double_quoted_parses_escapes() {
        let (s, tail) = take_double_quoted(r#" "a\nb\t\"\\" tail"#).unwrap();
        assert_eq!(s, "a\nb\t\"\\");
        assert_eq!(tail.trim(), "tail");
    }

    #[test]
    fn take_double_quoted_unclosed_returns_none() {
        assert!(take_double_quoted(r#" "no_close"#).is_none());
    }

    #[test]
    fn take_double_quoted_raw_newline_in_string_returns_none() {
        assert!(take_double_quoted(" \"x\ny\"").is_none());
    }

    #[test]
    fn namespace_last_line_wins() {
        let e = expand_source_directives("@namespace \"first\"\n@namespace second\nBEGIN {}\n")
            .unwrap();
        assert_eq!(e.default_namespace.as_deref(), Some("second"));
        assert!(!e.text.contains("@namespace"));
    }

    #[test]
    fn namespace_line_dropped_and_recorded() {
        let e = expand_source_directives("@namespace \"ns\"\nBEGIN { }\n").unwrap();
        assert!(!e.text.contains("@namespace"));
        assert!(e.text.contains("BEGIN"));
        assert_eq!(e.default_namespace.as_deref(), Some("ns"));
    }

    #[test]
    fn load_bundled_extension_name_is_noop() {
        let e = expand_source_directives("@load \"filefuncs\"\nBEGIN { x = 1 }\n").unwrap();
        assert!(!e.text.contains("@load"));
        assert!(e.text.contains("BEGIN"));
    }

    #[test]
    fn load_bundled_extension_so_suffix_is_noop() {
        let e = expand_source_directives("@load \"./filefuncs.so\"\nBEGIN { }\n").unwrap();
        assert!(!e.text.contains("@load"));
    }

    #[test]
    fn load_arbitrary_so_still_errors() {
        let r = expand_source_directives("@load \"vendor_foo.so\"\n");
        assert!(r.is_err(), "{r:?}");
    }

    #[test]
    fn load_awk_file_inlines_like_include() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let inc = dir.join(format!("awkrs_load_inc_{id}.awk"));
        std::fs::write(&inc, "function f() { return 1 }\n").unwrap();
        let main = format!("@load \"{}\"\nBEGIN {{ print f() }}\n", inc.display());
        let e = expand_source_directives(&main).unwrap();
        assert!(e.text.contains("function f"));
        let _ = std::fs::remove_file(&inc);
    }

    #[test]
    fn namespace_bare_identifier_accepted() {
        let e = expand_source_directives("@namespace myns\nBEGIN { }\n").unwrap();
        assert_eq!(e.default_namespace.as_deref(), Some("myns"));
        assert!(!e.text.contains("@namespace"));
        assert!(e.text.contains("BEGIN"));
    }

    #[test]
    fn namespace_malformed_errors() {
        let r = expand_source_directives("@namespace\nBEGIN {}\n");
        assert!(r.is_err(), "{r:?}");
    }

    #[test]
    fn include_malformed_missing_quote_errors() {
        let r = expand_source_directives("@include foo.awk\n");
        assert!(r.is_err(), "{r:?}");
    }

    #[test]
    fn include_cycle_errors() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let a = dir.join(format!("awkrs_inc_a_{id}.awk"));
        let b = dir.join(format!("awkrs_inc_b_{id}.awk"));
        std::fs::write(
            &a,
            format!(
                "@include \"{}\"\n",
                b.file_name().unwrap().to_string_lossy()
            ),
        )
        .unwrap();
        std::fs::write(
            &b,
            format!(
                "@include \"{}\"\n",
                a.file_name().unwrap().to_string_lossy()
            ),
        )
        .unwrap();
        let main = format!("@include \"{}\"\n", a.display());
        let r = expand_source_directives(&main);
        assert!(r.is_err(), "expected cycle error, got {r:?}");
        let _ = std::fs::remove_file(&a);
        let _ = std::fs::remove_file(&b);
    }

    #[test]
    fn load_native_extension_case_insensitive_stem() {
        let e = expand_source_directives("@load \"./FileFuncs.So\"\nBEGIN {}\n").unwrap();
        assert!(!e.text.contains("@load"));
        assert!(e.text.contains("BEGIN"));
    }

    #[test]
    fn include_inlines_twice_sequential() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let one = dir.join(format!("awkrs_inc_one_{id}.awk"));
        let two = dir.join(format!("awkrs_inc_two_{id}.awk"));
        std::fs::write(&one, "function one() { return 1 }\n").unwrap();
        std::fs::write(&two, "function two() { return 2 }\n").unwrap();
        let main = format!(
            "@include \"{}\"\n@include \"{}\"\nBEGIN {{ }}\n",
            one.display(),
            two.display()
        );
        let e = expand_source_directives(&main).unwrap();
        assert!(e.text.contains("function one"));
        assert!(e.text.contains("function two"));
        let _ = std::fs::remove_file(&one);
        let _ = std::fs::remove_file(&two);
    }

    #[test]
    fn include_empty_file_expands_to_nothing_between_directives() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let empty = dir.join(format!("awkrs_inc_empty_{id}.awk"));
        std::fs::write(&empty, "").unwrap();
        let main = format!("@include \"{}\"\nBEGIN {{ x = 1 }}\n", empty.display());
        let e = expand_source_directives(&main).unwrap();
        assert!(e.text.contains("BEGIN") && e.text.contains("x = 1"));
        let _ = std::fs::remove_file(&empty);
    }

    #[test]
    fn include_missing_file_errors() {
        let p = std::env::temp_dir().join(format!(
            "awkrs_no_such_include_{}_{}.awk",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        ));
        let main = format!("@include \"{}\"\nBEGIN {{}}\n", p.display());
        let r = expand_source_directives(&main);
        assert!(r.is_err(), "expected error for missing include, got {r:?}");
    }

    #[test]
    fn include_nested_recursive() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let p1 = dir.join(format!("awkrs_inc_n1_{id}.awk"));
        let p2 = dir.join(format!("awkrs_inc_n2_{id}.awk"));

        std::fs::write(
            &p1,
            format!(
                "@include \"{}\"\nfunction f1() {{}}",
                p2.file_name().unwrap().to_str().unwrap()
            ),
        )
        .unwrap();
        std::fs::write(&p2, "function f2() {}").unwrap();

        let main = format!("@include \"{}\"", p1.display());
        let e = expand_source_directives(&main).unwrap();
        assert!(e.text.contains("function f2"));
        assert!(e.text.contains("function f1"));

        let _ = std::fs::remove_file(&p1);
        let _ = std::fs::remove_file(&p2);
    }

    #[test]
    fn take_bare_ident_logic() {
        assert_eq!(take_bare_ident("  abc_123 def").unwrap().0, "abc_123");
        assert_eq!(take_bare_ident("_start ").unwrap().0, "_start");
        assert!(take_bare_ident("  123abc").is_none());
    }

    #[test]
    fn resolve_include_path_absolute() {
        let p = if cfg!(windows) {
            "C:\\foo.awk"
        } else {
            "/tmp/foo.awk"
        };
        let res = resolve_include_path(None, p).unwrap();
        assert!(res.is_absolute());
        assert_eq!(res.to_str().unwrap(), p);
    }

    #[test]
    fn include_relative_path_v2() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let inc1 = dir.join(format!("awkrs_inc1_{id}.awk"));
        let inc2 = dir.join(format!("awkrs_inc2_{id}.awk"));

        // inc1 includes inc2 via relative path
        std::fs::write(
            &inc1,
            format!(
                "@include \"{}\"\n",
                inc2.file_name().unwrap().to_str().unwrap()
            ),
        )
        .unwrap();
        std::fs::write(&inc2, "function f() { return 2 }\n").unwrap();

        // main includes inc1 via absolute path
        let main = format!("@include \"{}\"\nBEGIN {{ print f() }}\n", inc1.display());
        let e = expand_source_directives(&main).unwrap();
        assert!(e.text.contains("function f"));

        let _ = std::fs::remove_file(&inc1);
        let _ = std::fs::remove_file(&inc2);
    }

    #[test]
    fn multiple_directives_on_one_line_v2() {
        // gawk doesn't typically support multiple @directives on one line if they consume the rest of the line,
        // but let's see how our expander handles it.
        let main = "@load \"filefuncs\" @include \"nonexistent.awk\"\nBEGIN {}";
        // If it treats '@load' as consuming the line, it might ignore @include.
        let e = expand_source_directives(main).unwrap();
        assert!(!e.text.contains("@load"));
    }

    #[test]
    fn namespace_with_trailing_comment_v7() {
        let e = expand_source_directives("@namespace \"ns\" # comment\nBEGIN {}").unwrap();
        assert_eq!(e.default_namespace.as_deref(), Some("ns"));
        // The expander preserves trailing text after the namespace identifier
        assert!(e.text.contains("# comment"));
    }

    #[test]
    fn include_with_leading_whitespace_v7() {
        let dir = std::env::temp_dir();
        let id = std::process::id();
        let inc = dir.join(format!("awkrs_inc_ws_{id}.awk"));
        std::fs::write(&inc, "BEGIN { x=1 }\n").unwrap();

        let main = format!("  @include \"{}\"\n", inc.display());
        let e = expand_source_directives(&main).unwrap();
        assert!(e.text.contains("BEGIN"));

        let _ = std::fs::remove_file(&inc);
    }

    #[test]
    fn load_with_relative_path_and_no_base_v7() {
        // If no base_dir, it uses current_dir.
        // We can't easily rely on current_dir containing a specific file,
        // but we can test it doesn't panic.
        let r = expand_source_directives("@load \"nonexistent.awk\"");
        assert!(r.is_err());
    }

    #[test]
    fn take_bare_ident_leading_underscore_v7() {
        assert_eq!(take_bare_ident("  _var").unwrap().0, "_var");
    }

    #[test]
    fn take_bare_ident_empty_fails_v7() {
        assert!(take_bare_ident("  ").is_none());
    }
}