varar-core 0.7.0

Markdown-native BDD — pure functional core engine
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! The planner — port of `plan.ts` / `Plan.java`. Plans each text-bearing block
//! via the matcher, lifts block offsets to source spans, attaches trailing
//! table/fence nodes, handles the ```` ```error ```` fence, expands header-bound
//! tables into one example per row, and collects diagnostics.

use crate::ast::{Block, Fence, Row, SegmentOffset, Table, VarDoc};
use crate::cell_diff::RowCheck;
use crate::diagnostics::{Diagnostic, ambiguous_match, error_fence_without_step};
use crate::matcher::{Hit, ParamSpan, ResolvedSteps, find_hits, resolve_hits};
use crate::offsets::{java_trim, utf16_len};
use crate::registry::{FormatFn, Registry, StepRegistration};
use crate::sentences::split_sentences;
use crate::span::Span;
use crate::value::Value;
use regex::Regex;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::sync::LazyLock;

/// The result of planning a whole [`VarDoc`].
pub struct ExecutionPlan {
    pub var_doc: VarDoc,
    pub examples: Vec<PlannedExample>,
    pub diagnostics: Vec<Diagnostic>,
}

/// One matched-and-runnable example.
pub struct PlannedExample {
    pub name: String,
    pub scope_stack: Vec<String>,
    pub span: Span,
    pub steps: Vec<PlannedStep>,
    pub header_binding: Option<HeaderBinding>,
    pub row_checks: Option<Vec<RowCheck>>,
    pub expected_outcome: Option<String>,
    pub expected_error_message: Option<String>,
}

/// The binding paragraph shared by every row of a header-bound table.
pub struct HeaderBinding {
    pub match_span: Span,
    pub param_spans: Vec<Span>,
    pub step_def: Rc<StepRegistration>,
}

/// One matched step: text, source span, captured-parameter spans, args, and
/// attachments. `formats` aligns 1:1 with `args`.
#[derive(Clone)]
pub struct PlannedStep {
    pub text: String,
    pub match_span: Span,
    pub param_spans: Vec<Span>,
    pub step_def: Rc<StepRegistration>,
    pub args: Vec<Value>,
    pub formats: Vec<Option<FormatFn>>,
    pub data_table: Option<Table>,
    pub doc_string: Option<Fence>,
}

static WHITESPACE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
static WORD_CHAR_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[\p{L}\p{N}_]$").unwrap());

/// Plans `doc` against `registry`. Port of `plan()`.
pub fn plan(doc: &VarDoc, registry: &Registry) -> ExecutionPlan {
    let source = &doc.source;
    let mut diagnostics = Vec::new();

    // Phase 1: plan each candidate paragraph independently into a "unit".
    let units: Vec<CandidateUnit> = doc
        .examples
        .iter()
        .map(|ex| plan_candidate(ex, doc, registry, &mut diagnostics))
        .collect();

    // Phase 2: group adjacent candidates into examples. A matching candidate
    // continues the open example when no delimiter (heading / `---`) precedes it;
    // otherwise it starts a new one. A non-matching candidate (prose) is a
    // delimiter: it closes the open example and is dropped. A header-bound table
    // candidate is standalone — it already emits one example per row. See ADR 0012.
    let mut examples: Vec<PlannedExample> = Vec::new();
    let mut open: Option<MergedExample> = None;
    for unit in units {
        match unit {
            CandidateUnit::HeaderBound { rows } => {
                if let Some(m) = open.take() {
                    examples.push(finish_merged(m, source));
                }
                examples.extend(rows);
            }
            CandidateUnit::Steps(unit) => {
                if !unit.matched {
                    // Prose paragraph — a delimiter. Drop it and end the open example.
                    if let Some(m) = open.take() {
                        examples.push(finish_merged(m, source));
                    }
                    continue;
                }
                match open.as_mut() {
                    Some(m) if !unit.preceded_by_delimiter => merge_into(m, unit),
                    _ => {
                        if let Some(m) = open.take() {
                            examples.push(finish_merged(m, source));
                        }
                        open = Some(start_merged(unit));
                    }
                }
            }
        }
    }
    if let Some(m) = open.take() {
        examples.push(finish_merged(m, source));
    }

    // A table or fence that doesn't attach to a step is just Markdown content,
    // not a mistake — it produces no diagnostic.

    ExecutionPlan {
        var_doc: doc.clone(),
        examples,
        diagnostics,
    }
}

/// A step-bearing candidate accumulating into one example while adjacent matching
/// candidates keep merging in.
struct MergedExample {
    name: String,
    scope_stack: Vec<String>,
    start_offset: usize,
    end_offset: usize,
    steps: Vec<PlannedStep>,
    expected_outcome: Option<String>,
    expected_error_message: Option<String>,
}

/// One candidate paragraph, planned in isolation.
enum CandidateUnit {
    HeaderBound { rows: Vec<PlannedExample> },
    Steps(StepsUnit),
}

struct StepsUnit {
    matched: bool,
    preceded_by_delimiter: bool,
    name: String,
    scope_stack: Vec<String>,
    span: Span,
    steps: Vec<PlannedStep>,
    expected_outcome: Option<String>,
    expected_error_message: Option<String>,
}

fn start_merged(unit: StepsUnit) -> MergedExample {
    MergedExample {
        name: unit.name,
        scope_stack: unit.scope_stack,
        start_offset: unit.span.start_offset,
        end_offset: unit.span.end_offset,
        steps: unit.steps,
        expected_outcome: unit.expected_outcome,
        expected_error_message: unit.expected_error_message,
    }
}

fn merge_into(open: &mut MergedExample, unit: StepsUnit) {
    open.end_offset = unit.span.end_offset;
    open.steps.extend(unit.steps);
    // Any error fence in a merged part marks the whole example expected-to-fail;
    // keep the first message we see.
    if unit.expected_outcome.as_deref() == Some("fail") {
        open.expected_outcome = Some("fail".to_string());
        if open.expected_error_message.is_none() && unit.expected_error_message.is_some() {
            open.expected_error_message = unit.expected_error_message;
        }
    }
}

fn finish_merged(open: MergedExample, source: &str) -> PlannedExample {
    let span = Span::from_offsets(source, open.start_offset, open.end_offset);
    PlannedExample {
        name: open.name,
        scope_stack: open.scope_stack,
        span,
        steps: open.steps,
        header_binding: None,
        row_checks: None,
        expected_outcome: open.expected_outcome,
        expected_error_message: open.expected_error_message,
    }
}

/// Plan a single candidate paragraph (plus its attached tables/fences) in
/// isolation. Emits ambiguity / error-fence diagnostics into `diagnostics`.
fn plan_candidate(
    ex: &crate::ast::Example,
    doc: &VarDoc,
    registry: &Registry,
    diagnostics: &mut Vec<Diagnostic>,
) -> CandidateUnit {
    let source = &doc.source;
    let mut had_ambiguous = false;
    let body = &ex.body;

    // Pass 1: plan each text-bearing block, collecting steps per body index.
    let mut steps_by_block: BTreeMap<usize, Vec<PlannedStep>> = BTreeMap::new();
    for (idx, block) in body.iter().enumerate() {
        if !is_text_bearing(block) {
            continue;
        }
        let text = text_of(block);
        let (block_hits, ambiguities) = plan_block(text, registry);
        for collision in &ambiguities {
            let span = lift_span(source, block, collision.match_start, collision.match_end);
            diagnostics.push(ambiguous_match(span));
            had_ambiguous = true;
        }
        if !had_ambiguous && !block_hits.is_empty() {
            let block_steps: Vec<PlannedStep> = block_hits
                .into_iter()
                .map(|hit| PlannedStep {
                    text: crate::offsets::utf16_slice(text, hit.match_start, hit.match_end)
                        .to_string(),
                    match_span: lift_span(source, block, hit.match_start, hit.match_end),
                    param_spans: hit
                        .param_spans
                        .iter()
                        .map(|p| lift_span(source, block, p.start, p.end))
                        .collect(),
                    step_def: hit.step_def,
                    args: hit.args,
                    formats: hit.formats,
                    data_table: None,
                    doc_string: None,
                })
                .collect();
            steps_by_block.insert(idx, block_steps);
        }
    }

    // Header-bound table: iterate row by row.
    let bound = if had_ambiguous {
        None
    } else {
        detect_header_bound(body, &steps_by_block, source)
    };
    if let Some(bound) = bound {
        let header_binding = HeaderBinding {
            match_span: bound.step.match_span,
            param_spans: bound.header_spans.clone(),
            step_def: bound.step.step_def.clone(),
        };
        let header_cells = &bound.table.header.cells;
        let mut rows = Vec::new();
        for row in &bound.table.rows {
            let mut row_object = BTreeMap::new();
            for (i, header) in header_cells.iter().enumerate() {
                row_object.insert(header.clone(), Value::from(cell_at(row, i)));
            }
            let mut row_args = bound.step.args.clone();
            row_args.push(Value::Map(row_object));
            let row_step = PlannedStep {
                text: bound.step.text.clone(),
                match_span: row.span,
                param_spans: bound.step.param_spans.clone(),
                step_def: bound.step.step_def.clone(),
                args: row_args,
                formats: bound.step.formats.clone(),
                data_table: None,
                doc_string: None,
            };
            let row_checks: Vec<RowCheck> = header_cells
                .iter()
                .enumerate()
                .map(|(i, header)| {
                    RowCheck::new(header.clone(), cell_at(row, i), cell_span_at(row, i))
                })
                .collect();
            let mut nested_scope = ex.scope_stack.clone();
            nested_scope.push(bound.step.text.clone());
            rows.push(PlannedExample {
                name: row.cells.join(" / "),
                scope_stack: nested_scope,
                span: row.span,
                steps: vec![row_step],
                header_binding: Some(HeaderBinding {
                    match_span: header_binding.match_span,
                    param_spans: header_binding.param_spans.clone(),
                    step_def: header_binding.step_def.clone(),
                }),
                row_checks: Some(row_checks),
                expected_outcome: None,
                expected_error_message: None,
            });
        }
        return CandidateUnit::HeaderBound { rows };
    }

    // An ```error fence anywhere marks the candidate expected-to-fail.
    let error_fence: Option<&Fence> = body.iter().find_map(|b| match b {
        Block::Fence(f) if f.info == "error" => Some(f),
        _ => None,
    });

    // Pass 2: table/fence immediately after a step-bearing block.
    let mut attachments: BTreeMap<usize, (Option<Table>, Option<Fence>)> = BTreeMap::new();
    for (idx, here) in body.iter().enumerate().skip(1) {
        match here {
            Block::Table(table) if steps_by_block.contains_key(&(idx - 1)) => {
                attachments.entry(idx - 1).or_default().0 = Some(table.clone());
            }
            Block::Fence(fence)
                if fence.info != "error" && steps_by_block.contains_key(&(idx - 1)) =>
            {
                attachments.entry(idx - 1).or_default().1 = Some(fence.clone());
            }
            _ => {}
        }
    }

    // Pass 3: rebuild the final step list, applying attachments to the last
    // step of each block.
    let mut final_steps = Vec::new();
    for idx in 0..body.len() {
        let Some(steps_at_idx) = steps_by_block.get(&idx) else {
            continue;
        };
        let attach = attachments.get(&idx);
        let last = steps_at_idx.len() - 1;
        for (s, step) in steps_at_idx.iter().enumerate() {
            if s == last {
                if let Some((data_table, doc_string)) = attach {
                    let mut with_attach = step.clone();
                    with_attach.data_table = data_table.clone();
                    with_attach.doc_string = doc_string.clone();
                    final_steps.push(with_attach);
                    continue;
                }
            }
            final_steps.push(step.clone());
        }
    }

    let runnable_steps = if had_ambiguous {
        Vec::new()
    } else {
        final_steps
    };

    // An `error` fence declares the candidate expected-to-fail, but here there's
    // no runnable step to produce that failure (nothing matched, or the match was
    // ambiguous). That's an author mistake, not silent Markdown — flag it.
    if let Some(fence) = error_fence {
        if runnable_steps.is_empty() {
            diagnostics.push(error_fence_without_step(fence.span));
        }
    }

    let (expected_outcome, expected_error_message) = match error_fence {
        Some(fence) => {
            let trimmed = java_trim(&fence.body);
            let msg = if trimmed.is_empty() {
                None
            } else {
                Some(trimmed.to_string())
            };
            (Some("fail".to_string()), msg)
        }
        None => (None, None),
    };

    CandidateUnit::Steps(StepsUnit {
        matched: !runnable_steps.is_empty(),
        preceded_by_delimiter: ex.preceded_by_delimiter,
        name: derive_example_name(body),
        scope_stack: ex.scope_stack.clone(),
        span: ex.span,
        steps: runnable_steps,
        expected_outcome,
        expected_error_message,
    })
}

struct Ambiguity {
    match_start: usize,
    match_end: usize,
}

fn plan_block(text: &str, registry: &Registry) -> (Vec<Hit>, Vec<Ambiguity>) {
    let mut all_steps = Vec::new();
    let mut all_ambiguities = Vec::new();
    for sentence in split_sentences(text) {
        let off = sentence.start_offset;
        let adjusted: Vec<Hit> = find_hits(&sentence.text, registry)
            .into_iter()
            .map(|h| {
                let param_spans = h
                    .param_spans
                    .iter()
                    .map(|p| ParamSpan {
                        start: p.start + off,
                        end: p.end + off,
                    })
                    .collect();
                Hit {
                    expression: h.expression,
                    step_def: h.step_def,
                    match_start: h.match_start + off,
                    match_end: h.match_end + off,
                    args: h.args,
                    param_spans,
                    formats: h.formats,
                }
            })
            .collect();
        match resolve_hits(adjusted) {
            ResolvedSteps::Ambiguous(collisions) => {
                for c in collisions {
                    all_ambiguities.push(Ambiguity {
                        match_start: c.match_start,
                        match_end: c.match_end,
                    });
                }
            }
            ResolvedSteps::Ok(steps) => {
                if !steps.is_empty() {
                    all_steps.extend(steps);
                }
            }
        }
    }
    (all_steps, all_ambiguities)
}

struct HeaderBoundResult {
    table: Table,
    step: PlannedStep,
    header_spans: Vec<Span>,
}

fn detect_header_bound(
    body: &[Block],
    steps_by_block: &BTreeMap<usize, Vec<PlannedStep>>,
    source: &str,
) -> Option<HeaderBoundResult> {
    for idx in 1..body.len() {
        let Block::Table(table) = &body[idx] else {
            continue;
        };
        let above = &body[idx - 1];
        if !is_text_bearing(above) {
            continue;
        }
        let Some(steps) = steps_by_block.get(&(idx - 1)) else {
            continue;
        };
        if steps.is_empty() {
            continue;
        }
        let above_text = text_of(above);
        let header_cells = &table.header.cells;
        let mut offsets = Vec::with_capacity(header_cells.len());
        let mut any_missing = false;
        for cell in header_cells {
            match word_offset(above_text, cell) {
                Some(o) => offsets.push(o),
                None => {
                    any_missing = true;
                    offsets.push(0);
                }
            }
        }
        if any_missing {
            continue;
        }
        let header_spans: Vec<Span> = header_cells
            .iter()
            .zip(&offsets)
            .map(|(cell, &o)| lift_span(source, above, o, o + utf16_len(cell)))
            .collect();
        return Some(HeaderBoundResult {
            table: table.clone(),
            step: steps.last().unwrap().clone(),
            header_spans,
        });
    }
    None
}

/// UTF-16 offset of `word` in `haystack` as a whole word (case-sensitive), or
/// `None`. Manual scan replacing Java's lookbehind/lookaround regex.
fn word_offset(haystack: &str, word: &str) -> Option<usize> {
    if word.is_empty() {
        return None;
    }
    let mut from = 0;
    while let Some(rel) = haystack[from..].find(word) {
        let at = from + rel;
        let before_ok = haystack[..at]
            .chars()
            .next_back()
            .is_none_or(|c| !is_word_char(c));
        let after = at + word.len();
        let after_ok = haystack[after..]
            .chars()
            .next()
            .is_none_or(|c| !is_word_char(c));
        if before_ok && after_ok {
            return Some(crate::offsets::utf16_index(haystack, at));
        }
        from = at + haystack[at..].chars().next().map_or(1, char::len_utf8);
    }
    None
}

fn is_word_char(c: char) -> bool {
    let mut buf = [0u8; 4];
    WORD_CHAR_RE.is_match(c.encode_utf8(&mut buf))
}

/// The example name: the primary block's text with whitespace collapsed and a
/// single trailing terminator stripped. Port of `deriveExampleName`.
pub(crate) fn derive_example_name(body: &[Block]) -> String {
    let Some(primary) = body.iter().find(|b| is_text_bearing(b)) else {
        return String::new();
    };
    let collapsed = WHITESPACE_RE.replace_all(text_of(primary), " ");
    let mut name = java_trim(&collapsed).to_string();
    if let Some(last) = name.chars().last() {
        if last == '.' || last == '!' || last == '?' {
            name.pop();
        }
    }
    name
}

fn is_text_bearing(block: &Block) -> bool {
    matches!(block, Block::Paragraph(_) | Block::ListItem(_) | Block::Blockquote(_))
}

fn text_of(block: &Block) -> &str {
    match block {
        Block::Paragraph(p) => &p.text,
        Block::ListItem(l) => &l.text,
        Block::Blockquote(b) => &b.text,
        _ => panic!("not a text-bearing block"),
    }
}

fn cell_at(row: &Row, i: usize) -> &str {
    row.cells.get(i).map_or("", |c| c.as_str())
}

fn cell_span_at(row: &Row, i: usize) -> Span {
    row.cell_spans.get(i).copied().unwrap_or(row.span)
}

fn segment_map_of(block: &Block) -> Option<&[SegmentOffset]> {
    match block {
        Block::Paragraph(p) => Some(&p.segment_map),
        Block::ListItem(l) => Some(&l.segment_map),
        Block::Blockquote(b) => Some(&b.segment_map),
        _ => None,
    }
}

fn lift_span(source: &str, block: &Block, block_start: usize, block_end: usize) -> Span {
    match segment_map_of(block) {
        Some(sm) => {
            let start = lift_segment_offset(sm, block_start);
            let end = lift_segment_offset(sm, block_end);
            Span::from_offsets(source, start, end)
        }
        None => block.span(),
    }
}

fn lift_segment_offset(segment_map: &[SegmentOffset], text_offset: usize) -> usize {
    let mut best = segment_map.first();
    for entry in segment_map {
        if entry.text_offset <= text_offset {
            best = Some(entry);
        }
    }
    let best = best.expect("empty segmentMap");
    best.source_offset + (text_offset - best.text_offset)
}