artifactize-core 0.27.1

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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Typed reads of expressions. A value is read where it is used, in the scope it was written
//! in: a parameter stands for its argument read in the caller's scope, so names never capture.

use std::rc::Rc;

use artifactize_tools::artf::lexer::{Part, SlotKind};
use serde_json::{Map, Value};

use super::{
    LangError,
    ast::{Element, Entry, Expr, Span},
    expand::{Expander, Piece, SlotRef, SlotTarget},
    scope::Env,
};

/// How strings become JSON for a constructor's arguments.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Strings {
    /// Text only: `@{param …}` is filled in, no other slot is allowed.
    Plain,
    /// Text that may name its own Artifact: `@{artf self}`.
    Described,
    /// A command argument: one `@{path …}` slot at its end, after an optional `--flag=`.
    Argument,
}

/// The longest chain of macro calls one declaration may expand through.
pub(super) const MAX_DEPTH: usize = 32;
/// Every expression read counts once; this bounds a declaration that would expand
/// exponentially through macros that call each other several times.
pub(super) const MAX_NODES: usize = 1_000_000;

impl<'a> Expander<'a> {
    pub(super) fn tick(&self, span: Span) -> Result<(), LangError> {
        let nodes = self.nodes.get() + 1;
        self.nodes.set(nodes);
        if nodes > MAX_NODES {
            return Err(self.error(
                span,
                format!("Expanding the project reads more than {MAX_NODES} expressions; a macro expands too much."),
            ));
        }
        Ok(())
    }

    /// Follow parameters to the expression they stand for and the scope to read it in.
    pub(super) fn chase(
        &self,
        mut expr: &'a Expr,
        mut env: Rc<Env<'a>>,
    ) -> Result<(&'a Expr, Rc<Env<'a>>), LangError> {
        loop {
            self.tick(expr.span())?;
            let Expr::Ident(name) = expr else {
                return Ok((expr, env));
            };
            let Some(bound) = env.get(&name.text) else {
                return Ok((expr, env));
            };
            let bound = bound.clone();
            expr = bound.expr;
            env = bound.env;
        }
    }

    /// A string with its slots, parameters filled in and other slots kept typed.
    pub(super) fn template(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<Vec<Piece>, LangError> {
        let (expr, env) = self.chase(expr, env.clone())?;
        match expr {
            Expr::Str(literal, _) => {
                let mut pieces = Vec::new();
                for part in &literal.parts {
                    match part {
                        Part::Text(text) => pieces.push(Piece::Text(text.clone())),
                        Part::Slot(slot) => {
                            let span = self.span(slot.span.clone(), expr.span());
                            let word = |index: usize| -> (String, Span) {
                                let (text, range) = &slot.words[index];
                                (text.clone(), self.span(range.clone(), expr.span()))
                            };
                            match slot.kind {
                                SlotKind::Param => {
                                    let (name, name_span) = word(0);
                                    let bound = env.get(&name).cloned().ok_or_else(|| {
                                        self.error(
                                            name_span,
                                            format!(
                                                "`@{{param {name}}}` names no parameter in scope."
                                            ),
                                        )
                                    })?;
                                    pieces.extend(
                                        self.spliced(bound.expr, &bound.env, &name, name_span)?,
                                    );
                                }
                                SlotKind::Artf => {
                                    let (target, target_span) = word(0);
                                    pieces.push(Piece::Slot(SlotRef::Artf {
                                        target: self.slot_target(&target, target_span)?,
                                        span,
                                    }));
                                }
                                SlotKind::Path => {
                                    let (text, target_span) = word(0);
                                    let (target, path) =
                                        text.split_once('/').unwrap_or((text.as_str(), ""));
                                    if text.ends_with('/') {
                                        return Err(self.error(
                                            target_span,
                                            format!("`@{{path {text}}}`: write the Artifact as `@{{path {target}}}`, or a path after the `/`."),
                                        ));
                                    }
                                    artifactize_tools::scope::logical_path(path).map_err(
                                        |error| {
                                            self.error(
                                                target_span,
                                                format!("`@{{path {text}}}`: {error}"),
                                            )
                                        },
                                    )?;
                                    let start = target_span.range().start;
                                    let name_span =
                                        Span::new(target_span.file, start..start + target.len());
                                    pieces.push(Piece::Slot(SlotRef::Path {
                                        target: self.slot_target(target, name_span)?,
                                        path: path.to_owned(),
                                        span,
                                    }));
                                }
                            }
                        }
                    }
                }
                Ok(pieces)
            }
            Expr::Number(number, _) => Ok(vec![Piece::Text(number.clone())]),
            Expr::Bool(value, _) => Ok(vec![Piece::Text(value.to_string())]),
            other => Err(self.error(other.span(), format!("{what} takes a string."))),
        }
    }

    /// Prose (a title, an instruction, a description) is written out where it stands: no
    /// parameter fills it, whole or as `@{param …}`.
    pub(super) fn written_out(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<(), LangError> {
        let message = || {
            format!(
                "{what} is written out where it stands: a macro's parameters fill paths and arguments, not prose."
            )
        };
        match expr {
            Expr::Ident(name) if env.get(&name.text).is_some() => {
                Err(self.error(name.span, message()))
            }
            Expr::Str(literal, span)
                if literal.parts.iter().any(
                    |part| matches!(part, Part::Slot(slot) if slot.kind == SlotKind::Param),
                ) =>
            {
                Err(self.error(*span, message()))
            }
            _ => Ok(()),
        }
    }

    /// A parameter's value spliced into a string: its own pieces, never read for slots again.
    fn spliced(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        name: &str,
        span: Span,
    ) -> Result<Vec<Piece>, LangError> {
        let (resolved, resolved_env) = self.chase(expr, env.clone())?;
        match resolved {
            Expr::Str(..) | Expr::Number(..) | Expr::Bool(..) => {
                self.template(resolved, &resolved_env, "A parameter in a string")
            }
            Expr::Ident(global) => Ok(vec![Piece::Text(global.text.clone())]),
            other => Err(self
                .error(
                    span,
                    format!("`@{{param {name}}}` is not text, so it cannot fill a string."),
                )
                .note(other.span(), "its value")),
        }
    }

    fn slot_target(&self, word: &str, span: Span) -> Result<SlotTarget, LangError> {
        if word == "self" {
            return Ok(SlotTarget::SelfRef);
        }
        crate::config::validation::artifact_name(word, "An Artifact name")
            .map_err(|message| self.error(span, message))?;
        self.names.borrow_mut().insert((span, word.to_owned()));
        Ok(SlotTarget::Named(word.to_owned(), span))
    }

    /// A span inside a string literal: the slot's own range in the literal's file.
    fn span(&self, range: std::ops::Range<usize>, literal: Span) -> Span {
        Span::new(literal.file, range)
    }

    /// Text without slots other than parameters.
    pub(super) fn text(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<String, LangError> {
        let pieces = self.template(expr, env, what)?;
        self.plain(pieces, what)
    }

    pub(super) fn plain(&self, pieces: Vec<Piece>, what: &str) -> Result<String, LangError> {
        pieces
            .into_iter()
            .map(|piece| match piece {
                Piece::Text(text) => Ok(text),
                Piece::Slot(slot) => Err(self.error(
                    slot.span(),
                    format!("{what} takes text; `@{{artf}}` belongs in instructions and `@{{path}}` in arguments."),
                )),
            })
            .collect()
    }

    pub(super) fn boolean(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<bool, LangError> {
        match self.chase(expr, env.clone())?.0 {
            Expr::Bool(value, _) => Ok(*value),
            other => Err(self.error(other.span(), format!("{what} takes `true` or `false`."))),
        }
    }

    /// The items of a list, its spreads flattened, each with its scope.
    pub(super) fn list(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<Vec<(&'a Expr, Rc<Env<'a>>)>, LangError> {
        let (expr, env) = self.chase(expr, env.clone())?;
        let Expr::Array(elements, _) = expr else {
            return Err(self.error(expr.span(), format!("{what} takes a list `[…]`.")));
        };
        let mut items = Vec::new();
        for element in elements {
            match element {
                Element::Value(value) => items.push((value, env.clone())),
                Element::Spread(value, _) => items.extend(self.list(value, &env, what)?),
            }
        }
        Ok(items)
    }

    pub(super) fn texts(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<Vec<String>, LangError> {
        self.list(expr, env, what)?
            .into_iter()
            .map(|(item, env)| self.text(item, &env, what))
            .collect()
    }

    /// Artifacts named by identifier: `[engine-core, ...deps]`.
    pub(super) fn artifacts(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        what: &str,
    ) -> Result<Vec<(String, Span)>, LangError> {
        self.list(expr, env, what)?
            .into_iter()
            .map(|(item, env)| {
                let (item, _) = self.chase(item, env)?;
                match item {
                    Expr::Ident(name) if !name.text.contains('.') => {
                        if !self.globals.artf.concrete.contains_key(name.text.as_str()) {
                            let mut error = self.error(name.span, format!("Unknown Artifact `{}`.", name.text));
                            if self.globals.artf.macros.contains_key(name.text.as_str()) {
                                error = error.note(
                                    self.globals.artf.macros[name.text.as_str()].item.name.span,
                                    format!("`{}` is an `artf!` macro; bind an Artifact with `artf NAME = {}!(…)`", name.text, name.text),
                                );
                            }
                            return Err(error);
                        }
                        self.names.borrow_mut().insert((name.span, name.text.clone()));
                        Ok((name.text.clone(), name.span))
                    }
                    other => Err(self.error(other.span(), format!("{what} lists Artifacts by name."))),
                }
            })
            .collect()
    }

    /// JSON for a constructor's arguments: blocks are objects.
    pub(super) fn json(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        strings: Strings,
    ) -> Result<Value, LangError> {
        if strings == Strings::Described {
            self.written_out(expr, env, "A description")?;
        }
        let (expr, env) = self.chase(expr, env.clone())?;
        Ok(match expr {
            Expr::Str(..) => Value::String(self.string(expr, &env, strings)?),
            Expr::Number(number, span) => Value::Number(
                number
                    .parse::<serde_json::Number>()
                    .map_err(|_| self.error(*span, "This number has no JSON form."))?,
            ),
            Expr::Bool(value, _) => Value::Bool(*value),
            Expr::Array(..) => Value::Array(
                self.list(expr, &env, "This value")?
                    .into_iter()
                    .map(|(item, env)| self.json(item, &env, strings))
                    .collect::<Result<_, _>>()?,
            ),
            Expr::Block(block) => {
                let mut object = Map::new();
                for entry in &block.entries {
                    let Entry::Field { key, value } = entry else {
                        return Err(self.error(block.span, "An object holds `key = value` fields."));
                    };
                    if object
                        .insert(key.text.clone(), self.json(value, &env, strings)?)
                        .is_some()
                    {
                        return Err(
                            self.error(key.span, format!("Field `{}` is given twice.", key.text))
                        );
                    }
                }
                Value::Object(object)
            }
            Expr::Ident(name) => Value::String(name.text.clone()),
            other => {
                return Err(self.error(
                    other.span(),
                    "Expected a string, number, boolean, list or object.",
                ));
            }
        })
    }

    /// A string as a constructor argument: an `Argument` keeps its one path slot in the
    /// `@{path NAME/…}` form the declaration types parse.
    pub(super) fn string(
        &self,
        expr: &'a Expr,
        env: &Rc<Env<'a>>,
        strings: Strings,
    ) -> Result<String, LangError> {
        let pieces = self.template(expr, env, "This value")?;
        let mut text = String::new();
        for piece in pieces {
            match (piece, strings) {
                (Piece::Text(part), Strings::Argument) => text.push_str(&part.replace("@{", "@@{")),
                (Piece::Text(part), _) => text.push_str(&part),
                (
                    Piece::Slot(SlotRef::Artf {
                        target: target @ SlotTarget::SelfRef,
                        span,
                    }),
                    Strings::Described,
                ) => {
                    text.push_str(&self.bound(&target, span)?);
                }
                (Piece::Slot(SlotRef::Path { target, path, span }), Strings::Argument) => {
                    let name = self.bound(&target, span)?;
                    text.push_str("@{path ");
                    text.push_str(&name);
                    if !path.is_empty() {
                        text.push('/');
                        text.push_str(&path);
                    }
                    text.push('}');
                }
                (Piece::Slot(slot), _) => {
                    return Err(self.error(
                        slot.span(),
                        match strings {
                            Strings::Argument => {
                                "An argument takes a `@{path …}` slot, not this one."
                            }
                            Strings::Described => {
                                "A description may name its Artifact with `@{artf self}` only."
                            }
                            Strings::Plain => {
                                "This value takes text; slots belong in instructions and arguments."
                            }
                        },
                    ));
                }
            }
        }
        Ok(text)
    }

    /// The Artifact a slot names, `self` bound to the one being expanded.
    pub(super) fn bound(&self, target: &SlotTarget, span: Span) -> Result<String, LangError> {
        match target {
            SlotTarget::Named(name, _) => Ok(name.clone()),
            SlotTarget::SelfRef => self.current().ok_or_else(|| {
                self.error(
                    span,
                    "`self` names the Artifact being declared; there is none here.",
                )
            }),
        }
    }

    /// Named arguments of a constructor as one JSON object.
    pub(super) fn arguments(
        &self,
        args: &'a [super::ast::Arg],
        env: &Rc<Env<'a>>,
        strings: impl Fn(&str) -> Strings,
    ) -> Result<Map<String, Value>, LangError> {
        let mut object = Map::new();
        for arg in args {
            if object
                .insert(
                    arg.name.text.clone(),
                    self.json(&arg.value, env, strings(&arg.name.text))?,
                )
                .is_some()
            {
                return Err(self.error(
                    arg.name.span,
                    format!("Argument `{}` is given twice.", arg.name.text),
                ));
            }
        }
        Ok(object)
    }
}