artifactize-core 0.27.2

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
//! Parse `.artf` and `.artfprofiles` tokens into items. Only the syntax is checked here; names
//! and kinds resolve once every file has been read.

use super::ast::*;
use artifactize_tools::artf::lexer::{self, Kind, Token};

/// Names a binding may not take: they read as literals or as the enclosing Artifact.
const RESERVED: [&str; 4] = ["self", "none", "true", "false"];
/// How deep values may nest in arrays, blocks and calls: far deeper than a declaration needs, and
/// shallow enough that the recursive parser stays within the smallest thread stack it runs on, so
/// a hostile file is refused as input instead of overflowing the stack.
const MAX_NESTING: usize = 64;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub span: Span,
    pub message: String,
}

pub fn parse(file: FileId, source: &str) -> Result<Vec<Item>, ParseError> {
    let mut parser = Parser::new(file, source)?;
    let mut items = Vec::new();
    while parser.peek().is_some() {
        items.push(parser.item()?);
    }
    Ok(items)
}

/// An `.artfprofiles` file: `profile` declarations only.
pub fn parse_profiles(file: FileId, source: &str) -> Result<Vec<ProfileItem>, ParseError> {
    let mut parser = Parser::new(file, source)?;
    let mut items = Vec::new();
    while parser.peek().is_some() {
        items.push(parser.profile()?);
    }
    Ok(items)
}

struct Parser {
    file: FileId,
    tokens: Vec<Token>,
    position: usize,
    end: usize,
    /// How many values enclose the one being parsed.
    depth: usize,
}

impl Parser {
    fn new(file: FileId, source: &str) -> Result<Self, ParseError> {
        let lexed = lexer::lex(source).map_err(|error| ParseError {
            span: Span::new(file, error.offset..error.offset),
            message: error.message,
        })?;
        Ok(Self {
            file,
            tokens: lexed.tokens,
            position: 0,
            end: source.len(),
            depth: 0,
        })
    }

    fn peek(&self) -> Option<&Kind> {
        self.tokens.get(self.position).map(|token| &token.kind)
    }

    fn peek_at(&self, ahead: usize) -> Option<&Kind> {
        self.tokens
            .get(self.position + ahead)
            .map(|token| &token.kind)
    }

    fn span_here(&self) -> Span {
        self.tokens.get(self.position).map_or_else(
            || Span::new(self.file, self.end..self.end),
            |token| Span::new(self.file, token.span.clone()),
        )
    }

    fn previous_span(&self) -> Span {
        let token = &self.tokens[self.position - 1];
        Span::new(self.file, token.span.clone())
    }

    fn error<T>(&self, expected: &str) -> Result<T, ParseError> {
        let found = self
            .peek()
            .map_or_else(|| "the end of the file".to_owned(), Kind::describe);
        Err(ParseError {
            span: self.span_here(),
            message: format!("Expected {expected}, found {found}."),
        })
    }

    fn eat(&mut self, kind: &Kind) -> bool {
        if self.peek() == Some(kind) {
            self.position += 1;
            true
        } else {
            false
        }
    }

    fn expect(&mut self, kind: &Kind, expected: &str) -> Result<Span, ParseError> {
        if self.eat(kind) {
            Ok(self.previous_span())
        } else {
            self.error(expected)
        }
    }

    fn ident(&mut self, expected: &str) -> Result<Name, ParseError> {
        match self.peek() {
            Some(Kind::Ident(text)) => {
                let text = text.clone();
                self.position += 1;
                Ok(Name {
                    text,
                    span: self.previous_span(),
                })
            }
            _ => self.error(expected),
        }
    }

    /// `a.b.c`, joined without spaces around its dots.
    fn dotted(&mut self, expected: &str) -> Result<Name, ParseError> {
        let mut name = self.ident(expected)?;
        while self.peek() == Some(&Kind::Dot) && matches!(self.peek_at(1), Some(Kind::Ident(_))) {
            self.position += 1;
            let part = self.ident("a name after `.`")?;
            name.text.push('.');
            name.text.push_str(&part.text);
            name.span = name.span.to(part.span);
        }
        Ok(name)
    }

    fn binding(&mut self, kind: ItemKind) -> Result<Name, ParseError> {
        let name = if kind == ItemKind::Tool {
            self.dotted("the tool's name")?
        } else {
            self.ident("a name")?
        };
        if name.text.split('.').any(|part| RESERVED.contains(&part)) {
            return Err(ParseError {
                span: name.span,
                message: format!("`{}` is reserved and cannot name a declaration.", name.text),
            });
        }
        Ok(name)
    }

    fn item(&mut self) -> Result<Item, ParseError> {
        let start = self.span_here();
        let kind = match self.peek() {
            Some(Kind::Ident(word)) => match word.as_str() {
                "artf" => ItemKind::Artf,
                "tool" => ItemKind::Tool,
                "eval" => {
                    return Err(ParseError {
                        span: start,
                        message: "An eval is declared inside the `artf` block it judges: `eval ID : PROFILE = { … }`.".into(),
                    });
                }
                "profile" => {
                    return Err(ParseError {
                        span: start,
                        message: "Profiles are declared in `.artfprofiles` files: `profile NAME : agent = { … }`.".into(),
                    });
                }
                _ => return self.error("a declaration: `artf` or `tool`"),
            },
            _ => return self.error("a declaration: `artf` or `tool`"),
        };
        self.position += 1;
        let is_macro = self.eat(&Kind::Bang);
        if is_macro && kind == ItemKind::Tool {
            return Err(ParseError {
                span: start.to(self.previous_span()),
                message: "There are no `tool!` macros: declare a toolset once with `tool NAME = { … }` and compose it with `&`.".into(),
            });
        }
        let name = self.binding(kind)?;
        let params = if is_macro { Some(self.params()?) } else { None };
        let header = if self.eat(&Kind::Colon) {
            Some(self.expr()?)
        } else {
            None
        };
        self.expect(&Kind::Eq, "`=`")?;
        let body = self.expr()?;
        Ok(Item {
            kind,
            params,
            name,
            header,
            span: start.to(body.span()),
            body,
        })
    }

    fn params(&mut self) -> Result<Vec<Param>, ParseError> {
        self.expect(&Kind::LParen, "`(` and the macro's parameters")?;
        let mut params = Vec::new();
        while !self.eat(&Kind::RParen) {
            let name = self.ident("a parameter name or `)`")?;
            if RESERVED.contains(&name.text.as_str()) {
                return Err(ParseError {
                    span: name.span,
                    message: format!("`{}` is reserved and cannot name a parameter.", name.text),
                });
            }
            let default = if self.eat(&Kind::Eq) {
                Some(self.expr()?)
            } else {
                None
            };
            params.push(Param { name, default });
            if !self.eat(&Kind::Comma) && self.peek() != Some(&Kind::RParen) {
                return self.error("`,` or `)`");
            }
        }
        Ok(params)
    }

    fn args(&mut self) -> Result<Vec<Arg>, ParseError> {
        let mut args = Vec::new();
        while !self.eat(&Kind::RParen) {
            let name = self.ident("an argument name or `)`")?;
            self.expect(
                &Kind::Eq,
                "`=` after the argument's name: arguments are named",
            )?;
            let value = self.expr()?;
            args.push(Arg { name, value });
            if !self.eat(&Kind::Comma) && self.peek() != Some(&Kind::RParen) {
                return self.error("`,` or `)`");
            }
        }
        Ok(args)
    }

    fn expr(&mut self) -> Result<Expr, ParseError> {
        let first = self.term()?;
        if self.peek() != Some(&Kind::Amp) {
            return Ok(first);
        }
        let mut terms = vec![first];
        while self.eat(&Kind::Amp) {
            terms.push(self.term()?);
        }
        let span = terms[0].span().to(terms[terms.len() - 1].span());
        Ok(Expr::And(terms, span))
    }

    /// One value, counted against [`MAX_NESTING`]: arrays, blocks and calls nest through here.
    fn term(&mut self) -> Result<Expr, ParseError> {
        if self.depth == MAX_NESTING {
            return Err(ParseError {
                span: self.span_here(),
                message: format!("Values nest more than {MAX_NESTING} deep."),
            });
        }
        self.depth += 1;
        let term = self.value();
        self.depth -= 1;
        term
    }

    fn value(&mut self) -> Result<Expr, ParseError> {
        let span = self.span_here();
        let Some(kind) = self.peek().cloned() else {
            return self.error("a value");
        };
        match kind {
            Kind::Str(literal) => {
                self.position += 1;
                Ok(Expr::Str(literal, span))
            }
            Kind::Number(number) => {
                self.position += 1;
                Ok(Expr::Number(number, span))
            }
            Kind::LBracket => {
                self.position += 1;
                let mut elements = Vec::new();
                while !self.eat(&Kind::RBracket) {
                    if self.eat(&Kind::Spread) {
                        let spread = self.previous_span();
                        let value = self.term()?;
                        elements.push(Element::Spread(value, spread));
                    } else {
                        elements.push(Element::Value(self.expr()?));
                    }
                    if !self.eat(&Kind::Comma) && self.peek() != Some(&Kind::RBracket) {
                        return self.error("`,` or `]`");
                    }
                }
                Ok(Expr::Array(elements, span.to(self.previous_span())))
            }
            Kind::LBrace => self.block().map(Expr::Block),
            Kind::Ident(word) if matches!(word.as_str(), "true" | "false") => {
                self.position += 1;
                Ok(Expr::Bool(word == "true", span))
            }
            Kind::Ident(word) if word == "none" => Err(ParseError {
                span,
                message: "`none` is not a value: leave the field out.".into(),
            }),
            Kind::Ident(_) => {
                let name = self.dotted("a value")?;
                if self.peek() == Some(&Kind::Bang) && self.peek_at(1) == Some(&Kind::LParen) {
                    self.position += 2;
                    let args = self.args()?;
                    Ok(Expr::Macro {
                        span: name.span.to(self.previous_span()),
                        name,
                        args,
                    })
                } else if self.eat(&Kind::LParen) {
                    let args = self.args()?;
                    Ok(Expr::Call {
                        span: name.span.to(self.previous_span()),
                        name,
                        args,
                    })
                } else {
                    Ok(Expr::Ident(name))
                }
            }
            _ => self.error("a value"),
        }
    }

    fn block(&mut self) -> Result<Block, ParseError> {
        let open = self.expect(&Kind::LBrace, "`{`")?;
        let mut entries = Vec::new();
        while !self.eat(&Kind::RBrace) {
            entries.push(self.entry()?);
            self.eat(&Kind::Comma);
        }
        Ok(Block {
            entries,
            span: open.to(self.previous_span()),
        })
    }

    fn entry(&mut self) -> Result<Entry, ParseError> {
        let start = self.span_here();
        if self.eat(&Kind::Spread) {
            let value = self.term()?;
            return Ok(Entry::Spread(value.clone(), start.to(value.span())));
        }
        if let Some(Kind::Str(literal)) = self.peek().cloned() {
            self.position += 1;
            let Some(text) = literal.plain() else {
                return Err(ParseError {
                    span: start,
                    message: "A key cannot hold a slot.".into(),
                });
            };
            self.expect(&Kind::Eq, "`=` after the key")?;
            let value = self.expr()?;
            return Ok(Entry::Field {
                key: Name { text, span: start },
                value,
            });
        }
        let first = self.ident("a field, a declaration, `...` or `}`")?;
        if self.eat(&Kind::Eq) {
            let value = self.expr()?;
            return Ok(Entry::Field { key: first, value });
        }
        let name = self.dotted("a name, or `=` after a field")?;
        let header = if self.eat(&Kind::Colon) {
            Some(self.expr()?)
        } else {
            None
        };
        self.expect(&Kind::Eq, "`=`")?;
        let value = self.expr()?;
        Ok(Entry::Declare {
            span: start.to(value.span()),
            keyword: first,
            name,
            header,
            value,
        })
    }

    /// `profile NAME : KIND = { … }` or `profile NAME : KIND = match VAR { LABEL => { … }, … }`.
    fn profile(&mut self) -> Result<ProfileItem, ParseError> {
        let start = self.span_here();
        if !matches!(self.peek(), Some(Kind::Ident(word)) if word == "profile") {
            return self
                .error("a `profile` declaration: an `.artfprofiles` file holds only profiles");
        }
        self.position += 1;
        if self.peek() == Some(&Kind::Bang) {
            return self.error("a profile's name: there are no `profile!` macros");
        }
        let name = self.ident("the profile's name")?;
        if RESERVED.contains(&name.text.as_str()) {
            return Err(ParseError {
                span: name.span,
                message: format!("`{}` is reserved and cannot name a declaration.", name.text),
            });
        }
        self.expect(
            &Kind::Colon,
            "`:` and what evaluates with the profile: `profile NAME : agent = { … }`",
        )?;
        let kind = self.ident("what evaluates with the profile: `agent`")?;
        self.expect(&Kind::Eq, "`=`")?;
        let body = if matches!(self.peek(), Some(Kind::Ident(word)) if word == "match") {
            self.position += 1;
            let variable = self.ident("the environment variable the profile matches")?;
            self.expect(&Kind::LBrace, "`{` and the profile's branches")?;
            let mut arms = Vec::new();
            while !self.eat(&Kind::RBrace) {
                let label = self.ident("a branch: `LABEL => { … }`, or `}`")?;
                self.expect(&Kind::Arrow, "`=>` after the branch's label")?;
                arms.push((label, self.block()?));
                if !self.eat(&Kind::Comma) && self.peek() != Some(&Kind::RBrace) {
                    return self.error("`,` or `}`");
                }
            }
            ProfileBody::Match { variable, arms }
        } else {
            ProfileBody::Setting(self.block()?)
        };
        Ok(ProfileItem {
            name,
            kind,
            body,
            span: start.to(self.previous_span()),
        })
    }
}

#[cfg(test)]
mod tests;