pklr 5.2.2

Pure Rust pkl configuration language parser and evaluator
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
//! Pkl glob patterns, following pkl-core's `GlobResolver`.
//!
//! A pattern is split on `/`. Leading parts without wildcards name a base
//! directory; each remaining part is matched against the names of the entries
//! of the directories reached so far. A part containing `**` also matches
//! entries in every subdirectory, by name. Within a part, `*` matches any run
//! of characters other than `/`, `**` any run of characters, `?` any one
//! character, `[...]` a character class (`[!...]` negated), `{a,b}` one of
//! the comma-separated sub-patterns, and `\` escapes one of `?*[{\`.

use super::*;

/// The most directories one glob may list before it is rejected as too
/// complex (pkl's `GlobResolver.maxListElements`, guarding against
/// CVE-2010-2632).
const MAX_LIST_ELEMENTS: usize = 16384;

/// Expand a Pkl glob pattern relative to a base directory.
///
/// An empty `base` (the parent of a bare relative path like `main.pkl`) means
/// the current directory. Matches are files, in pkl's order: within a
/// directory, files before subdirectories, each sorted by name. Symbolic links
/// are skipped, as pkl does to avoid cyclic globs.
pub fn expand_glob(base: &Path, pattern: &str) -> Result<Vec<PathBuf>> {
    Ok(resolve_file_glob(module_dir(base), pattern)?
        .into_iter()
        .map(|(_, path)| path)
        .collect())
}

/// Expand `pattern` against the directory `base`, returning each matched
/// file with its key: the pattern's leading literal parts followed by the
/// matched path under them, as pkl keys `import*` and `read*` results.
pub(super) fn resolve_file_glob(base: &Path, pattern: &str) -> Result<Vec<(String, PathBuf)>> {
    let (prefix, parts) = split_glob_pattern(pattern)?;
    let mut results = Vec::new();
    if parts.is_empty() {
        let path = base.join(pattern);
        if path.is_file() {
            results.push((pattern.to_string(), path));
        }
        return Ok(results);
    }
    let dir = base.join(&prefix);
    let mut listed = 0;
    resolve_glob_parts(&parts, &dir, &prefix, &mut listed, &mut results)?;
    Ok(results)
}

fn resolve_glob_parts(
    parts: &[(&str, Option<GlobPart>)],
    dir: &Path,
    key: &str,
    listed: &mut usize,
    results: &mut Vec<(String, PathBuf)>,
) -> Result<()> {
    let Some(((literal, part), rest)) = parts.split_first() else {
        return Ok(());
    };
    // `**/` may consume no directory components, so try the remaining
    // segments in the current directory before descending.
    if part.as_ref().is_some_and(|part| part.globstar) {
        resolve_glob_parts(rest, dir, key, listed, results)?;
    }
    let Some(part) = part else {
        let path = dir.join(literal);
        let key = join_glob_key(key, literal);
        if rest.is_empty() {
            if path.is_file() {
                results.push((key, path));
            }
            return Ok(());
        }
        return resolve_glob_parts(rest, &path, &key, listed, results);
    };
    let mut matched = Vec::new();
    expand_glob_part(part, dir, key, listed, &mut matched)?;
    for (key, path, is_dir) in matched {
        if rest.is_empty() {
            if !is_dir {
                results.push((key, path));
            }
        } else if is_dir {
            resolve_glob_parts(rest, &path, &key, listed, results)?;
        }
    }
    Ok(())
}

/// The entries of `dir` whose names match `part`, and with `**`, those of
/// every subdirectory too.
fn expand_glob_part(
    part: &GlobPart,
    dir: &Path,
    key: &str,
    listed: &mut usize,
    matched: &mut Vec<(String, PathBuf, bool)>,
) -> Result<()> {
    *listed += 1;
    if *listed > MAX_LIST_ELEMENTS {
        return Err(invalid_glob(
            &part.source,
            "The glob pattern is too complex.",
        ));
    }
    for (name, is_dir) in list_dir(dir)? {
        let path = dir.join(&name);
        let entry_key = join_glob_key(key, &name);
        if part.matches(&name) {
            matched.push((entry_key.clone(), path.clone(), is_dir));
        }
        if is_dir && part.globstar {
            expand_glob_part(part, &path, &entry_key, listed, matched)?;
        }
    }
    Ok(())
}

/// The entries of `dir` as `(name, is_dir)`, files first, each sorted by
/// name, skipping symbolic links. A missing directory has no entries.
fn list_dir(dir: &Path) -> Result<Vec<(String, bool)>> {
    let entries = match std::fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(error)
            if matches!(
                error.kind(),
                std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory
            ) =>
        {
            return Ok(Vec::new());
        }
        Err(error) => return Err(Error::Io(dir.to_path_buf(), error)),
    };
    let mut listed = Vec::new();
    for entry in entries {
        let entry = entry.map_err(|error| Error::Io(dir.to_path_buf(), error))?;
        let file_type = match entry.file_type() {
            Ok(file_type) => file_type,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => return Err(Error::Io(entry.path(), error)),
        };
        if file_type.is_symlink() {
            continue;
        }
        listed.push((
            entry.file_name().to_string_lossy().into_owned(),
            file_type.is_dir(),
        ));
    }
    listed.sort_by(|(a, a_dir), (b, b_dir)| a_dir.cmp(b_dir).then_with(|| a.cmp(b)));
    Ok(listed)
}

fn join_glob_key(key: &str, name: &str) -> String {
    if key.is_empty() || key.ends_with('/') {
        format!("{key}{name}")
    } else {
        format!("{key}/{name}")
    }
}

/// A `/`-separated glob pattern split into its leading literal parts (joined,
/// each followed by `/`) and the remaining parts, compiled unless literal.
#[allow(clippy::type_complexity)]
pub(super) fn split_glob_pattern(pattern: &str) -> Result<(String, Vec<(&str, Option<GlobPart>)>)> {
    let parts: Vec<&str> = pattern.split('/').collect();
    let literal = parts
        .iter()
        .take_while(|part| is_literal_glob_part(part))
        .count();
    let prefix = parts[..literal]
        .iter()
        .map(|part| format!("{part}/"))
        .collect::<String>();
    let rest = parts[literal..]
        .iter()
        .map(|part| {
            let compiled = if is_literal_glob_part(part) {
                None
            } else {
                Some(GlobPart::compile(part, pattern)?)
            };
            Ok((*part, compiled))
        })
        .collect::<Result<_>>()?;
    Ok((prefix, rest))
}

fn is_literal_glob_part(part: &str) -> bool {
    !part.contains(['\\', '{', '[', '?', '*'])
}

fn invalid_glob(pattern: &str, reason: &str) -> Error {
    Error::Eval(format!("Invalid glob pattern `{pattern}`. {reason}"))
}

/// One element of a compiled glob.
#[derive(Debug, Clone, PartialEq)]
enum GlobToken {
    Char(char),
    /// `*`: any run of characters other than `/`.
    Star,
    /// `**`: any run of characters.
    StarStar,
    /// `?`: any one character.
    Any,
    /// `[...]`: one character other than `/` in (or, negated, not in) the set.
    Class {
        negated: bool,
        items: Vec<(char, char)>,
    },
    /// `{a,b}`: any one of the branches. Branches contain no groups.
    Group(Vec<Vec<GlobToken>>),
}

/// A compiled glob. `{a,b}` sub-patterns are matched in place rather than
/// expanded, so adjacent groups cannot multiply into exponentially many
/// alternatives.
#[derive(Debug, Clone)]
pub(super) struct GlobPart {
    source: String,
    tokens: Vec<GlobToken>,
    globstar: bool,
}

impl GlobPart {
    /// Compile `part`, reporting errors against the whole `pattern`. Mirrors
    /// `GlobResolver.toRegexString`.
    pub(super) fn compile(part: &str, pattern: &str) -> Result<Self> {
        let chars: Vec<char> = part.chars().collect();
        let mut tokens: Vec<GlobToken> = Vec::new();
        let mut group: Option<Vec<Vec<GlobToken>>> = None;
        let mut i = 0;
        let push = |group: &mut Option<Vec<Vec<GlobToken>>>,
                    tokens: &mut Vec<GlobToken>,
                    token: GlobToken| {
            match group {
                Some(branches) => branches.last_mut().unwrap().push(token),
                None => tokens.push(token),
            }
        };
        while i < chars.len() {
            let next = chars.get(i + 1).copied();
            match chars[i] {
                '{' => {
                    if group.is_some() {
                        return Err(invalid_glob(
                            pattern,
                            "Sub-patterns cannot be nested. To fix, remove or escape the inner `{` character.",
                        ));
                    }
                    group = Some(vec![Vec::new()]);
                }
                '}' if group.is_some() => {
                    let branches = group.take().unwrap();
                    tokens.push(GlobToken::Group(branches));
                }
                ',' if group.is_some() => group.as_mut().unwrap().push(Vec::new()),
                '\\' => {
                    let Some(next) = next else {
                        return Err(invalid_glob(
                            pattern,
                            "The backslash (`\\`) is the escape character, and cannot terminate a glob pattern.",
                        ));
                    };
                    if !matches!(next, '?' | '*' | '[' | '{' | '\\') {
                        return Err(invalid_glob(
                            pattern,
                            &format!("Invalid escape character `\\{next}`."),
                        ));
                    }
                    push(&mut group, &mut tokens, GlobToken::Char(next));
                    i += 1;
                }
                '[' => {
                    let (token, end) = compile_glob_class(&chars, i, pattern)?;
                    push(&mut group, &mut tokens, token);
                    i = end;
                }
                c @ ('?' | '*' | '+' | '@' | '!') if next == Some('(') => {
                    let _ = c;
                    return Err(invalid_glob(
                        pattern,
                        "Extended globbing features are not supported.",
                    ));
                }
                '?' => push(&mut group, &mut tokens, GlobToken::Any),
                '*' if next == Some('*') => {
                    push(&mut group, &mut tokens, GlobToken::StarStar);
                    i += 1;
                }
                '*' => push(&mut group, &mut tokens, GlobToken::Star),
                c => push(&mut group, &mut tokens, GlobToken::Char(c)),
            }
            i += 1;
        }
        if group.is_some() {
            return Err(invalid_glob(
                pattern,
                "Missing `}` character to terminate sub-pattern.",
            ));
        }
        Ok(Self {
            source: pattern.to_string(),
            tokens,
            globstar: part.contains("**"),
        })
    }

    pub(super) fn matches(&self, name: &str) -> bool {
        let name: Vec<char> = name.chars().collect();
        glob_tokens_match(&self.tokens, &name)
    }
}

/// Compile the character class starting at `chars[start] == '['`, returning
/// it and the index of its closing `]`.
fn compile_glob_class(chars: &[char], start: usize, pattern: &str) -> Result<(GlobToken, usize)> {
    let unterminated = || invalid_glob(pattern, "Missing `]` to terminate character class.");
    let mut i = start + 1;
    let mut negated = false;
    let mut members = Vec::new();
    match chars.get(i) {
        Some('!') => {
            negated = true;
            i += 1;
        }
        // A leading `^` or `]` is literal.
        Some(&c @ ('^' | ']')) => {
            members.push(c);
            i += 1;
        }
        None => return Err(unterminated()),
        _ => {}
    }
    loop {
        let Some(&c) = chars.get(i) else {
            return Err(unterminated());
        };
        if c == ']' {
            break;
        }
        if c == '[' && matches!(chars.get(i + 1), Some(':' | '=' | '.')) {
            return Err(invalid_glob(
                pattern,
                "Glob patterns do not support named character classes, collating symbols, nor equivalence class expressions.",
            ));
        }
        if c == '/' {
            return Err(invalid_glob(
                pattern,
                "The character `/` is not valid in a character class.",
            ));
        }
        members.push(c);
        i += 1;
    }
    let mut items = Vec::new();
    let mut j = 0;
    while j < members.len() {
        if j + 2 < members.len() && members[j + 1] == '-' {
            items.push((members[j], members[j + 2]));
            j += 3;
        } else {
            items.push((members[j], members[j]));
            j += 1;
        }
    }
    Ok((GlobToken::Class { negated, items }, i))
}

fn glob_tokens_match(tokens: &[GlobToken], name: &[char]) -> bool {
    // `matched[j]`: the tokens so far can match `name[..j]`.
    let mut matched = vec![false; name.len() + 1];
    matched[0] = true;
    matched = advance_glob_tokens(tokens, name, matched);
    matched[name.len()]
}

/// The positions in `name` reachable by matching `tokens` from any position
/// set in `matched`.
fn advance_glob_tokens(tokens: &[GlobToken], name: &[char], mut matched: Vec<bool>) -> Vec<bool> {
    for token in tokens {
        let mut next = vec![false; name.len() + 1];
        match token {
            GlobToken::Star | GlobToken::StarStar => {
                next[0] = matched[0];
                for j in 1..=name.len() {
                    let can_extend = *token == GlobToken::StarStar || name[j - 1] != '/';
                    next[j] = matched[j] || (can_extend && next[j - 1]);
                }
            }
            GlobToken::Group(branches) => {
                for branch in branches {
                    let reached = advance_glob_tokens(branch, name, matched.clone());
                    for (next, reached) in next.iter_mut().zip(reached) {
                        *next |= reached;
                    }
                }
            }
            _ => {
                for j in 1..=name.len() {
                    next[j] = matched[j - 1] && glob_token_matches_char(token, name[j - 1]);
                }
            }
        }
        if !next.contains(&true) {
            return next;
        }
        matched = next;
    }
    matched
}

fn glob_token_matches_char(token: &GlobToken, c: char) -> bool {
    match token {
        GlobToken::Char(expected) => *expected == c,
        GlobToken::Any => true,
        GlobToken::Class { negated, items } => {
            c != '/' && items.iter().any(|(lo, hi)| (*lo..=*hi).contains(&c)) != *negated
        }
        GlobToken::Star | GlobToken::StarStar | GlobToken::Group(_) => {
            unreachable!("handled by advance_glob_tokens")
        }
    }
}

/// Treat an empty directory (the parent of a bare relative path) as `.`, so it
/// can be read from and stripped as a prefix of the paths found under it.
pub(super) fn module_dir(dir: &Path) -> &Path {
    if dir.as_os_str().is_empty() {
        Path::new(".")
    } else {
        dir
    }
}

/// Get a relative path string from `path` relative to `base`, or the full path if not a prefix.
pub(super) fn pathdiff_or_full(path: &Path, base: &Path) -> String {
    let path = path
        .strip_prefix(base)
        .unwrap_or(path)
        .to_string_lossy()
        .to_string();
    normalize_pkl_path(&path)
}

pub(super) fn normalize_pkl_path(path: &str) -> String {
    path.replace('\\', "/")
}