artifactize-tools 0.27.0

Portable scoped tools and safe file access for artifactize.
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
//! Tokens of the `.artf` language: identifiers, numbers, strings with typed `@{…}` slots,
//! punctuation and `//` comments. Strings are decoded once here, so a value spliced into a
//! string later is never read for slots again.

use std::ops::Range;

/// Identifiers start with an ASCII letter and are at most 64 bytes, like Artifact names.
pub const MAX_IDENTIFIER_BYTES: usize = 64;

#[derive(Debug, Clone, PartialEq)]
pub struct Token {
    pub kind: Kind,
    pub span: Range<usize>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Kind {
    Ident(String),
    /// A JSON number, kept as written.
    Number(String),
    Str(Literal),
    LBrace,
    RBrace,
    LParen,
    RParen,
    LBracket,
    RBracket,
    Eq,
    Comma,
    Colon,
    Amp,
    Bang,
    Dot,
    Spread,
    /// `=>`, between a `match` arm's label and its value.
    Arrow,
}

impl Kind {
    /// How a diagnostic names what it found.
    pub fn describe(&self) -> String {
        match self {
            Self::Ident(name) => format!("`{name}`"),
            Self::Number(number) => format!("number {number}"),
            Self::Str(_) => "a string".into(),
            Self::LBrace => "`{`".into(),
            Self::RBrace => "`}`".into(),
            Self::LParen => "`(`".into(),
            Self::RParen => "`)`".into(),
            Self::LBracket => "`[`".into(),
            Self::RBracket => "`]`".into(),
            Self::Eq => "`=`".into(),
            Self::Comma => "`,`".into(),
            Self::Colon => "`:`".into(),
            Self::Amp => "`&`".into(),
            Self::Bang => "`!`".into(),
            Self::Dot => "`.`".into(),
            Self::Spread => "`...`".into(),
            Self::Arrow => "`=>`".into(),
        }
    }
}

/// A decoded string: text and typed slots in order. A raw `'''…'''` string is one text part.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Literal {
    pub parts: Vec<Part>,
}

impl Literal {
    /// The text when the string has no slot.
    pub fn plain(&self) -> Option<String> {
        self.parts
            .iter()
            .map(|part| match part {
                Part::Text(text) => Some(text.as_str()),
                Part::Slot(_) => None,
            })
            .collect()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Part {
    Text(String),
    Slot(Slot),
}

/// `@{kind word …}`: what kind of value fills it and the words naming it, each with its span.
#[derive(Debug, Clone, PartialEq)]
pub struct Slot {
    pub kind: SlotKind,
    pub words: Vec<(String, Range<usize>)>,
    pub span: Range<usize>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotKind {
    Artf,
    Path,
    Param,
}

impl SlotKind {
    pub fn name(self) -> &'static str {
        match self {
            Self::Artf => "artf",
            Self::Path => "path",
            Self::Param => "param",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
pub struct SyntaxError {
    pub offset: usize,
    pub message: String,
}

fn error(offset: usize, message: impl Into<String>) -> SyntaxError {
    SyntaxError {
        offset,
        message: message.into(),
    }
}

/// The tokens of a source and the spans of its comments.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Lexed {
    pub tokens: Vec<Token>,
    pub comments: Vec<Range<usize>>,
}

pub fn lex(source: &str) -> Result<Lexed, SyntaxError> {
    let bytes = source.as_bytes();
    let mut lexed = Lexed::default();
    let mut index = 0;
    while index < bytes.len() {
        let start = index;
        let byte = bytes[index];
        let single = |kind| Some((kind, 1));
        let punct = match byte {
            b'{' => single(Kind::LBrace),
            b'}' => single(Kind::RBrace),
            b'(' => single(Kind::LParen),
            b')' => single(Kind::RParen),
            b'[' => single(Kind::LBracket),
            b']' => single(Kind::RBracket),
            b'=' if bytes.get(index + 1) == Some(&b'>') => Some((Kind::Arrow, 2)),
            b'=' => single(Kind::Eq),
            b',' => single(Kind::Comma),
            b':' => single(Kind::Colon),
            b'&' => single(Kind::Amp),
            b'!' => single(Kind::Bang),
            b'.' if source[index..].starts_with("...") => Some((Kind::Spread, 3)),
            b'.' => single(Kind::Dot),
            _ => None,
        };
        if let Some((kind, width)) = punct {
            index += width;
            lexed.tokens.push(Token {
                kind,
                span: start..index,
            });
            continue;
        }
        match byte {
            b' ' | b'\t' | b'\r' | b'\n' => index += 1,
            b'/' if bytes.get(index + 1) == Some(&b'/') => {
                index = source[index..]
                    .find('\n')
                    .map_or(bytes.len(), |end| index + end);
                lexed.comments.push(start..index);
            }
            b'"' | b'\'' => {
                let (literal, end) = string(source, index)?;
                index = end;
                lexed.tokens.push(Token {
                    kind: Kind::Str(literal),
                    span: start..index,
                });
            }
            b'-' | b'0'..=b'9' => {
                index = number(source, index)?;
                lexed.tokens.push(Token {
                    kind: Kind::Number(source[start..index].to_owned()),
                    span: start..index,
                });
            }
            byte if byte.is_ascii_alphabetic() => {
                while index < bytes.len()
                    && (bytes[index].is_ascii_alphanumeric() || matches!(bytes[index], b'_' | b'-'))
                {
                    index += 1;
                }
                if index - start > MAX_IDENTIFIER_BYTES {
                    return Err(error(
                        start,
                        format!("An identifier is at most {MAX_IDENTIFIER_BYTES} bytes."),
                    ));
                }
                lexed.tokens.push(Token {
                    kind: Kind::Ident(source[start..index].to_owned()),
                    span: start..index,
                });
            }
            _ => {
                let character = source[index..].chars().next().expect("index on a char");
                return Err(error(index, format!("Unexpected character {character:?}.")));
            }
        }
    }
    Ok(lexed)
}

/// `-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?`, as JSON writes numbers.
fn number(source: &str, start: usize) -> Result<usize, SyntaxError> {
    let bytes = source.as_bytes();
    let digits = |mut index: usize| {
        let from = index;
        while index < bytes.len() && bytes[index].is_ascii_digit() {
            index += 1;
        }
        (index, index - from)
    };
    let mut index = start + usize::from(bytes[start] == b'-');
    let (after, count) = digits(index);
    if count == 0 {
        return Err(error(start, "A number needs a digit after `-`."));
    }
    if count > 1 && bytes[index] == b'0' {
        return Err(error(start, "A number has no leading zero."));
    }
    index = after;
    if bytes.get(index) == Some(&b'.') {
        let (after, count) = digits(index + 1);
        if count == 0 {
            return Err(error(index, "A number needs a digit after `.`."));
        }
        index = after;
    }
    if matches!(bytes.get(index), Some(b'e' | b'E')) {
        index += 1;
        if matches!(bytes.get(index), Some(b'+' | b'-')) {
            index += 1;
        }
        let (after, count) = digits(index);
        if count == 0 {
            return Err(error(index, "A number needs a digit in its exponent."));
        }
        index = after;
    }
    if bytes
        .get(index)
        .is_some_and(|byte| byte.is_ascii_alphabetic() || matches!(byte, b'_'))
    {
        return Err(error(start, "A number cannot run into a name."));
    }
    Ok(index)
}

/// Decode one string starting at `start` (its opening quote); return it and the offset after it.
fn string(source: &str, start: usize) -> Result<(Literal, usize), SyntaxError> {
    let rest = &source[start..];
    if let Some(body) = rest.strip_prefix("\"\"\"") {
        let close = body
            .find("\"\"\"")
            .ok_or_else(|| error(start, "This `\"\"\"` string has no closing `\"\"\"`."))?;
        let content = start + 3..start + 3 + close;
        let (text, map) = dedent(source, content);
        Ok((decode(&text, &map, false)?, start + 3 + close + 3))
    } else if let Some(body) = rest.strip_prefix("'''") {
        let close = body
            .find("'''")
            .ok_or_else(|| error(start, "This `'''` string has no closing `'''`."))?;
        let content = start + 3..start + 3 + close;
        let (text, map) = dedent(source, content);
        Ok((decode(&text, &map, true)?, start + 3 + close + 3))
    } else if rest.starts_with('"') {
        let bytes = source.as_bytes();
        let mut index = start + 1;
        loop {
            match bytes.get(index) {
                None | Some(b'\n') => {
                    return Err(error(start, "This string has no closing `\"` on its line."));
                }
                Some(b'\\') => index += 2,
                Some(b'"') => break,
                Some(_) => index += 1,
            }
        }
        let content = &source[start + 1..index];
        let map: Vec<usize> = (start + 1..index).collect();
        Ok((decode(content, &map, false)?, index + 1))
    } else {
        Err(error(
            start,
            "A single quote starts only a raw `'''` string; use `\"` for a string.",
        ))
    }
}

/// Multi-line strings drop the line break after the opening quotes, the last line when it
/// holds only the closing quotes' indentation, and the indentation every other line shares.
/// Return the text and the source offset of each of its bytes.
fn dedent(source: &str, content: Range<usize>) -> (String, Vec<usize>) {
    let mut start = content.start;
    let body = &source[content.clone()];
    if body.starts_with("\r\n") {
        start += 2;
    } else if body.starts_with('\n') {
        start += 1;
    }
    let mut lines: Vec<Range<usize>> = Vec::new();
    let mut line_start = start;
    for (offset, byte) in source[start..content.end].bytes().enumerate() {
        if byte == b'\n' {
            let end = start + offset;
            let end = if end > line_start && source.as_bytes()[end - 1] == b'\r' {
                end - 1
            } else {
                end
            };
            lines.push(line_start..end);
            line_start = start + offset + 1;
        }
    }
    lines.push(line_start..content.end);
    if lines.len() > 1
        && source[lines.last().expect("a line").clone()]
            .bytes()
            .all(|byte| matches!(byte, b' ' | b'\t'))
    {
        lines.pop();
    }
    let blank = |line: &Range<usize>| source[line.clone()].trim().is_empty();
    let indent = lines
        .iter()
        .filter(|line| !blank(line))
        .map(|line| {
            source[line.clone()]
                .bytes()
                .take_while(|byte| matches!(byte, b' ' | b'\t'))
                .count()
        })
        .min()
        .unwrap_or(0);
    let mut text = String::new();
    let mut map = Vec::new();
    for (number, line) in lines.iter().enumerate() {
        if number > 0 {
            text.push('\n');
            map.push(line.start.saturating_sub(1));
        }
        if blank(line) {
            continue;
        }
        let from = line.start + indent;
        text.push_str(&source[from..line.end]);
        map.extend(from..line.end);
    }
    (text, map)
}

/// Escapes and slots, read once. A raw string keeps every byte as written.
fn decode(text: &str, map: &[usize], raw: bool) -> Result<Literal, SyntaxError> {
    let at = |index: usize| {
        map.get(index)
            .copied()
            .unwrap_or_else(|| map.last().map_or(0, |last| last + 1))
    };
    if raw {
        return Ok(Literal {
            parts: vec![Part::Text(text.to_owned())],
        });
    }
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut chars = text.char_indices().peekable();
    while let Some((index, character)) = chars.next() {
        let rest = &text[index..];
        if rest.starts_with("@@{") {
            current.push_str("@{");
            chars.next();
            chars.next();
            continue;
        }
        if rest.starts_with("@{") {
            let close = rest
                .find('}')
                .ok_or_else(|| error(at(index), "This `@{` slot has no closing `}`."))?;
            let slot = slot(
                &rest[2..close],
                index + 2,
                map,
                at(index)..at(index + close) + 1,
            )?;
            if !current.is_empty() {
                parts.push(Part::Text(std::mem::take(&mut current)));
            }
            parts.push(Part::Slot(slot));
            while chars.peek().is_some_and(|(next, _)| *next <= index + close) {
                chars.next();
            }
            continue;
        }
        if character != '\\' {
            current.push(character);
            continue;
        }
        let Some((_, escaped)) = chars.next() else {
            return Err(error(at(index), "A string cannot end with `\\`."));
        };
        match escaped {
            '"' => current.push('"'),
            '\\' => current.push('\\'),
            '/' => current.push('/'),
            'b' => current.push('\u{8}'),
            'f' => current.push('\u{c}'),
            'n' => current.push('\n'),
            'r' => current.push('\r'),
            't' => current.push('\t'),
            'u' => {
                let hex: String = (0..4)
                    .filter_map(|_| chars.next().map(|(_, c)| c))
                    .collect();
                let value = u32::from_str_radix(&hex, 16)
                    .ok()
                    .filter(|_| hex.len() == 4)
                    .and_then(char::from_u32)
                    .ok_or_else(|| {
                        error(at(index), "`\\u` takes four hex digits of a character.")
                    })?;
                current.push(value);
            }
            '\n' => {
                // A line-ending backslash joins the next line without its indentation.
                while chars
                    .peek()
                    .is_some_and(|(_, next)| matches!(next, ' ' | '\t' | '\n' | '\r'))
                {
                    chars.next();
                }
            }
            other => {
                return Err(error(
                    at(index),
                    format!("Unknown escape `\\{other}`; write `\\\\` for a backslash."),
                ));
            }
        }
    }
    if !current.is_empty() || parts.is_empty() {
        parts.push(Part::Text(current));
    }
    Ok(Literal { parts })
}

/// The words of `@{kind word …}`, checked for their count; what they name is checked later.
fn slot(
    inner: &str,
    offset: usize,
    map: &[usize],
    span: Range<usize>,
) -> Result<Slot, SyntaxError> {
    let at = |index: usize| map.get(offset + index).copied().unwrap_or(span.start);
    let mut words = Vec::new();
    let mut starts = Vec::new();
    let mut cursor = 0;
    for word in inner.split([' ', '\t', '\n', '\r']) {
        if !word.is_empty() {
            words.push((word.to_owned(), at(cursor)..at(cursor) + word.len()));
            starts.push(cursor);
        }
        cursor += word.len() + 1;
    }
    // A path under an Artifact is a file name, which may hold spaces: `@{path NAME/a b.txt}`
    // runs to the closing brace.
    if words.first().is_some_and(|(kind, _)| kind == "path")
        && words.len() > 2
        && words[1].0.contains('/')
    {
        let start = starts[1];
        let rest = inner[start..].trim_end();
        words.truncate(1);
        words.push((rest.to_owned(), at(start)..at(start) + rest.len()));
    }
    let Some((kind, kind_span)) = words.first().cloned() else {
        return Err(error(
            span.start,
            "An empty `@{}` slot; write `@{artf NAME}`, `@{path NAME}` or `@{param NAME}`, or `@@{` for a literal `@{`.",
        ));
    };
    let kind = match kind.as_str() {
        "artf" => SlotKind::Artf,
        "path" => SlotKind::Path,
        "param" => SlotKind::Param,
        "tool" => {
            return Err(error(
                kind_span.start,
                "There is no tool slot: composing a toolset gives an eval its tools, and `@{artf NAME}` names an Artifact whose tools it reads.",
            ));
        }
        other => {
            return Err(error(
                kind_span.start,
                format!(
                    "Unknown slot kind `{other}`: a slot is `@{{artf …}}`, `@{{path …}}` or `@{{param …}}`; write `@@{{` for a literal `@{{`."
                ),
            ));
        }
    };
    words.remove(0);
    if words.len() != 1 {
        return Err(error(
            span.start,
            format!(
                "A `{}` slot names one thing: `@{{{} NAME}}`.",
                kind.name(),
                kind.name()
            ),
        ));
    }
    Ok(Slot { kind, words, span })
}

#[cfg(test)]
mod tests;