compmd 0.12.0

The CompMD processor: parses, validates and expands CompMD (Component Markdown) documents. In development.
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
//! Examples (spec ยง7.8): the processed document's ` ```compmd ` blocks with `render=output` or `render=both`, each
//! processed as a document of its own beside it, stage by stage. An example resolves from the processed document's
//! place, with its host's files, packs and policy, and loads with its render context, but its own inputs. What it
//! reports and the origins of what it expands are moved from the block's text to the processed document's, where the
//! block's text is written. Inside an example, a ` ```compmd ` block that would render shows its source (W123).

use super::{EditContext, Expanded, HostFiles, Loaded, Processor, RenderInputs, Validated, back};
use crate::AllowedOrigins;
use crate::diagnostic::{Code, Diagnostic, Location, Range};
use crate::expanded::{self, Root};
use crate::host::{Needed, Pending};
use crate::line_index::LineIndex;
use crate::load;
use crate::resolve::{HostComponent, Resolved};
use crate::tree::{self, Node, Point};
use crate::{context::RenderContext, expand};
use indexmap::IndexSet;
use std::collections::HashMap;
use std::hash::Hash;
use std::rc::Rc;

/// The language whose blocks are examples.
const COMPMD: &str = "compmd";

/// The processed document's examples, in the order they are written.
#[derive(Default)]
pub(super) struct Examples(Vec<Example>);

/// One example.
struct Example {
    /// Its place among the examples, from 1.
    number: usize,
    /// Where its block's fence starts in the processed document, which the expanded tree finds it by.
    fence: Point,
    /// Where its text is in the processed document.
    place: Place,
    /// Its text, by which an edit elsewhere keeps it (audit B5).
    text: String,
    /// Its processing, at the stage the processed document is at; `None` only while it moves to the next.
    stage: Option<Stage>,
    /// How many of its diagnostics the processed document has taken so far.
    reported: usize,
}

/// An example's processing, at a stage; boxed, as a processor is large.
enum Stage {
    Validated(Box<Processor<Validated>>),
    Loaded(Box<Processor<Loaded>>),
    Expanded(Box<Processor<Expanded>>),
}

impl Stage {
    fn diagnostics(&self) -> &[Diagnostic] {
        match self {
            Self::Validated(processor) => processor.diagnostics(),
            Self::Loaded(processor) => processor.diagnostics(),
            Self::Expanded(processor) => processor.diagnostics(),
        }
    }

    /// What stages 1 to 3 made.
    fn validated(&self) -> &Validated {
        match self {
            Self::Validated(processor) => &processor.state,
            Self::Loaded(processor) => &processor.state.validated,
            Self::Expanded(processor) => &processor.state.validated,
        }
    }
}

/// Where an example's text is in the processed document: its lines are the lines after the fence's, each indented as
/// the fence is, which the block's text leaves out.
struct Place {
    /// The fence's line.
    fence_line: usize,
    /// The fence's indentation, in code points.
    indent: usize,
    /// Where each of the example's lines starts in the processed document, in code points.
    starts: Vec<usize>,
}

impl Place {
    fn new(fence: Point, lines: usize, starts: &LineIndex) -> Self {
        Self {
            fence_line: fence.line,
            indent: fence.column - 1,
            starts: (1..=lines)
                .map(|line| starts.start(fence.line + line).unwrap_or_default())
                .collect(),
        }
    }

    fn location(&self, location: Location) -> Location {
        Location::new(
            self.fence_line + location.line,
            location.column + self.indent,
        )
    }

    fn point(&self, point: Point) -> Point {
        let start = self.starts.get(point.line - 1).copied().unwrap_or_default();
        Point {
            line: self.fence_line + point.line,
            column: point.column + self.indent,
            offset: start + self.indent + point.column - 1,
        }
    }

    fn range(&self, range: &mut Range) {
        range.start = self.location(range.start);
        range.end = range.end.map(|end| self.location(end));
    }

    /// `diagnostic` of the example, at its place in the processed document; one in another document stays.
    fn diagnostic(&self, diagnostic: &Diagnostic) -> Diagnostic {
        let mut moved = diagnostic.clone();
        if moved.file.is_none() {
            self.range(&mut moved.range);
            if let Some(fix) = &mut moved.fix {
                self.range(&mut fix.range);
            }
        }
        for related in moved
            .related
            .iter_mut()
            .filter(|related| related.file.is_none())
        {
            self.range(&mut related.range);
        }
        moved
    }

    /// The origins of `nodes` and what they hold, at their places in the processed document.
    fn origins(&self, nodes: &mut [expanded::Node]) {
        for node in nodes {
            if let Some(origin) = &mut node.origin {
                origin.start = self.point(origin.start);
                origin.end = self.point(origin.end);
            }
            if let expanded::Content::ComponentContainer(container) = &mut node.content {
                for slot in &mut container.slots {
                    if let Some(origin) = &mut slot.origin {
                        origin.start = self.point(origin.start);
                        origin.end = self.point(origin.end);
                    }
                }
            }
            for list in node.content.lists_mut() {
                self.origins(list);
            }
        }
    }
}

/// The ` ```compmd ` blocks of `nodes` that render, at any depth of the body, in written order: not those in a
/// definition's body, which is another document's text.
fn blocks<'t>(nodes: &'t [Node], found: &mut Vec<&'t tree::Code>) {
    for node in nodes {
        match node {
            Node::ComponentDefinition(_) | Node::ComponentSignature(_) => {}
            Node::Code(code) if renders(code) => found.push(code),
            Node::ComponentContainer(container) => {
                blocks(&container.children, found);
                for slot in &container.slots {
                    blocks(&slot.children, found);
                }
            }
            _ => blocks(node.children(), found),
        }
    }
}

/// Whether `code` is a ` ```compmd ` block that renders.
fn renders(code: &tree::Code) -> bool {
    code.lang.as_deref() == Some(COMPMD)
        && code.info.as_ref().is_some_and(|info| info.render.draws())
}

impl Examples {
    /// The examples of `resolved`'s processed document, each processed through stage 3 with the processed document's
    /// `store`, `context` and `allowed_origins`, their diagnostics added to `diagnostics`. An example makes none: its
    /// blocks that would render are W123.
    ///
    /// Each resolves with the processed document's parses, so what they import alike (`@compmd/core`, the ambient
    /// packs, a shared file) isn't parsed again. One of `kept`, the examples before an edit, whose number and text
    /// are the same, is taken back to stage 3 rather than processed again; only its place moves.
    pub(super) fn of(
        resolved: &Resolved,
        store: &HostFiles,
        context: &EditContext,
        allowed_origins: &AllowedOrigins,
        mut kept: Self,
        diagnostics: &mut Vec<Diagnostic>,
    ) -> Self {
        let document = resolved.document(resolved.main);
        let mut found = Vec::new();
        blocks(&document.root.children, &mut found);
        if context.example {
            for code in found {
                diagnostics.push(
                    Diagnostic::new(
                        Code::W123,
                        tree::start_of(code.position),
                        "an example inside an example shows its source; render it where it is written as a document",
                    )
                    .in_stage(crate::diagnostic::Stage::Validate),
                );
            }
            return Self::default();
        }
        let examples = found
            .into_iter()
            .enumerate()
            .filter_map(|(index, code)| {
                let fence = code.position?.start;
                let lines = code.value.split('\n').count();
                let number = index + 1;
                let processor = kept.take(number, &code.value).unwrap_or_else(|| {
                    Processor::new(context.path.clone(), code.value.clone())
                        .parse_example(&context.options)
                        .resolve_in(
                            HostFiles {
                                files: Rc::clone(&store.files),
                                packs: Rc::clone(&store.packs),
                                props: Rc::clone(&store.props),
                            },
                            context.ambient.clone(),
                            context.policy.clone(),
                            context.parses.clone(),
                        )
                        .validate(allowed_origins)
                });
                let mut example = Example {
                    number,
                    fence,
                    place: Place::new(fence, lines, &document.line_starts),
                    text: code.value.clone(),
                    stage: Some(Stage::Validated(Box::new(processor))),
                    reported: 0,
                };
                example.report(diagnostics);
                Some(example)
            })
            .collect();
        Self(examples)
    }

    /// Whether the processed document has none.
    pub(super) fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// The example numbered `number` whose text is `text`, taken out and back to stage 3 with all its diagnostics, as
    /// a new one's would be; `None` when there is none.
    fn take(&mut self, number: usize, text: &str) -> Option<Processor<Validated>> {
        let example = self
            .0
            .iter_mut()
            .find(|example| example.number == number && example.text == text)?;
        match example.stage.take()? {
            Stage::Loaded(processor) => {
                Some(processor.then(|loaded, found| back(loaded.validated, found)))
            }
            Stage::Expanded(processor) => {
                Some(processor.then(|expanded, found| back(expanded.validated, found)))
            }
            Stage::Validated(processor) => Some(*processor),
        }
    }

    /// Stage 4a for each example, with `render`'s context and data, but the example's own inputs: the values `render`
    /// gives for its number, or else their defaults.
    pub(super) fn load(&mut self, render: &RenderInputs, diagnostics: &mut Vec<Diagnostic>) {
        for example in &mut self.0 {
            if let Some(Stage::Validated(processor)) = example.stage.take() {
                let inputs = RenderInputs {
                    context: RenderContext {
                        inputs: render
                            .examples
                            .get(&example.number)
                            .cloned()
                            .unwrap_or_default(),
                        ..render.context.clone()
                    },
                    fetch: Rc::clone(&render.fetch),
                    data: Rc::clone(&render.data),
                    examples: HashMap::new(),
                };
                example.stage = Some(Stage::Loaded(Box::new(processor.load_with(inputs))));
            }
            example.report(diagnostics);
        }
    }

    /// Stage 4b for each example: its expanded root, by where its fence starts in the processed document, in code
    /// points, with its number and its origins in the processed document; and what they spent together. Examples
    /// count toward their document's limits (spec ยง8.5): each gets what those before it left of `options`'.
    pub(super) fn expand(
        &mut self,
        options: &expand::Options,
        diagnostics: &mut Vec<Diagnostic>,
    ) -> (HashMap<usize, Root>, expand::Used) {
        let mut roots = HashMap::new();
        let mut used = expand::Used::default();
        for example in &mut self.0 {
            if let Some(Stage::Loaded(processor)) = example.stage.take() {
                let expanded = processor.expand(&options.after(used));
                example.stage = Some(Stage::Expanded(Box::new(expanded)));
            }
            example.report(diagnostics);
            if let Some(Stage::Expanded(processor)) = &example.stage {
                used = used + processor.state.result.used;
                let mut root = processor.expanded().clone();
                root.example = Some(example.number);
                example.place.origins(&mut root.children);
                roots.insert(example.fence.offset, root);
            }
        }
        (roots, used)
    }

    /// Each example back to stage 3, its later diagnostics dropped, as the processed document goes back.
    pub(super) fn back(&mut self) {
        for example in &mut self.0 {
            let validated = match example.stage.take() {
                Some(Stage::Loaded(processor)) => {
                    processor.then(|loaded, found| back(loaded.validated, found))
                }
                Some(Stage::Expanded(processor)) => {
                    processor.then(|expanded, found| back(expanded.validated, found))
                }
                Some(Stage::Validated(processor)) => *processor,
                None => continue,
            };
            example.reported = validated.diagnostics().len();
            example.stage = Some(Stage::Validated(Box::new(validated)));
        }
    }

    /// The examples' host-language components, each with its example's number.
    pub(super) fn host_components(&self) -> Vec<HostComponent> {
        self.each()
            .flat_map(|(number, validated)| {
                let mut components = validated.resolved.host_components(validated.store.store());
                for component in &mut components {
                    component.example = Some(number);
                }
                components
            })
            .collect()
    }

    /// What the examples need besides what the processed document does, for a render with `context`, added to
    /// `needed`, each once.
    pub(super) fn sources(&self, context: &RenderContext, needed: &mut Needed) {
        let context = RenderContext {
            inputs: serde_json::Map::new(),
            ..context.clone()
        };
        for (_, validated) in self.each() {
            let theirs = load::sources(
                &validated.resolved,
                &validated.checks,
                &context,
                &validated.allowed_origins,
            );
            add(&mut needed.remote, theirs.remote);
            add(&mut needed.host, theirs.host);
            add(&mut needed.disallowed, theirs.disallowed);
            add(&mut needed.packs, theirs.packs);
        }
    }

    /// What the examples treated as pending, added to `pending`, each once.
    pub(super) fn pending(&self, pending: &mut Pending) {
        for example in &self.0 {
            if let Some(Stage::Expanded(processor)) = &example.stage {
                let theirs = processor.pending();
                add(&mut pending.packs, theirs.packs);
                add(&mut pending.remote, theirs.remote);
                add(&mut pending.host, theirs.host);
            }
        }
    }

    /// Each example's number, with what its stages 1 to 3 made.
    fn each(&self) -> impl Iterator<Item = (usize, &Validated)> {
        self.0
            .iter()
            .filter_map(|example| Some((example.number, example.stage.as_ref()?.validated())))
    }
}

impl Example {
    /// Adds what it reported since the last time to `diagnostics`, at their places in the processed document.
    fn report(&mut self, diagnostics: &mut Vec<Diagnostic>) {
        let Some(stage) = &self.stage else { return };
        let found = stage.diagnostics();
        diagnostics.extend(
            found[self.reported.min(found.len())..]
                .iter()
                .map(|diagnostic| self.place.diagnostic(diagnostic)),
        );
        self.reported = found.len();
    }
}

/// `theirs` added to `ours`, each once, `ours` first, in order.
fn add<T: Eq + Hash>(ours: &mut Vec<T>, theirs: Vec<T>) {
    let mut each: IndexSet<T> = ours.drain(..).collect();
    each.extend(theirs);
    ours.extend(each);
}