bern-compiler 0.1.1

Collects Bern declarations, checks them and produces the shared IR
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Elements: their shape, their props by kind and their children.

use super::{Checker, Ctx, FieldExpect, Root, Scope, is_labeled_enum};
use bern_ir::spec::{self, ChildShape, ComponentSpec, ElementShape, PropKind};
use bern_ir::{self as ir, text_key};
use bern_protocol::Type;
use bern_syntax as ast;
use std::collections::BTreeMap;

/// An element as lowered so far: what its props give before its
/// children are lowered.
struct Parts {
    /// The element's kind, such as `Button`.
    kind: String,
    props: BTreeMap<String, ir::Prop>,
    id: Option<String>,
    template: Option<ir::Template>,
    /// The context the children see.
    child_ctx: Ctx,
    /// Template variables pushed onto the scope for the children.
    pushed: usize,
}

impl Parts {
    /// Bind the element's template variable `var` to `ty` for its
    /// children.
    fn bind(&mut self, var: String, ty: Type, scope: &mut Scope) {
        scope.push(var.clone(), Root::Local(ty.clone(), false));
        self.pushed += 1;
        self.template = Some(ir::Template { var, ty });
    }
}

impl Checker<'_> {
    /// Check one element and lower it: its shape, the props that set what
    /// its children see, its other props, its children and its action.
    pub(super) fn element(
        &mut self,
        element: &ast::Element,
        path: &str,
        ctx: &Ctx,
        scope: &mut Scope,
    ) -> Option<ir::Element> {
        let name = element.name.to_string();
        let component = self.shape(element, &name, ctx)?;
        let mut parts = Parts {
            child_ctx: Ctx {
                // A repetition places its template where it stands itself.
                parent: if name == "ForEach" {
                    ctx.parent.clone()
                } else {
                    Some(name.clone())
                },
                is_root: false,
                selection: ctx.selection.clone(),
                route: ctx.route.clone(),
            },
            kind: name,
            props: BTreeMap::new(),
            id: None,
            template: None,
            pushed: 0,
        };
        self.context_props(element, component, ctx, scope, &mut parts);
        for prop in &element.props {
            let prop_name = prop.name.to_string();
            let Some(prop_spec) = component.prop(&prop_name) else {
                continue;
            };
            let Some(lowered) = self.prop(prop, prop_spec.kind, path, ctx, &parts, scope) else {
                continue;
            };
            if let ir::Prop::Id { value } = &lowered {
                parts.id = Some(value.clone());
            }
            parts.props.insert(prop_name, lowered);
        }

        if parts.kind == "Picker"
            && let Some(ir::Prop::Expr { expr }) = parts.props.get("selected")
            && !is_labeled_enum(&expr.ty, self.types)
        {
            self.error(
                element.span,
                "`selected` must be an enum whose variants all have labels",
            );
        }
        if parts.kind == "Picker"
            && matches!(parts.props.get("style"), Some(ir::Prop::Keyword { value }) if value == "chips")
        {
            // Chips are one control per option; `shape` and `height` size
            // the single button of a picker shown as a menu.
            for prop in ["shape", "height"] {
                if let Some(given) = element.prop(prop) {
                    self.error(
                        given.name.span(),
                        format!(
                            "`style: chips` shows a chip per option, so `{prop}:` has no button to size; leave it out"
                        ),
                    );
                }
            }
        }
        // The shortcut a user chooses is state: the action keeps it.
        if parts.kind == "ShortcutInput"
            && let Some(ir::Prop::Keyword { .. }) = parts.props.get("current")
        {
            self.error(
                element
                    .prop("current")
                    .map_or(element.span, |prop| prop.value.span()),
                "`current` is the shortcut the state holds, such as `app.keys.open`; the action keeps the one chosen",
            );
        }
        if parts.kind == "Tab"
            && matches!(parts.props.get("display"), Some(ir::Prop::Keyword { value }) if value == "icon")
            && !parts.props.contains_key("icon")
        {
            self.error(element.span, "a `Tab` with `display: icon` needs an `icon`");
        }
        let children = self.children(element, path, &parts.child_ctx, scope);
        if parts.kind == "Tabs" {
            self.tab_accessory(element, &children);
        }
        if parts.kind == "Table"
            && let Some(template) = &parts.template
        {
            self.table_parts(element, &template.var, &children);
        }
        if let (Some(ir::Prop::Number { value: min }), Some(ir::Prop::Number { value: max })) =
            (parts.props.get("min"), parts.props.get("max"))
            && min >= max
        {
            self.error(
                element.span,
                format!("`{}` needs `min` below `max`", parts.kind),
            );
        }
        for _ in 0..parts.pushed {
            scope.pop();
        }
        let action = element
            .action
            .as_ref()
            .and_then(|action| self.action(action, scope));
        Some(ir::Element {
            kind: parts.kind,
            id: parts.id,
            path: path.to_string(),
            props: parts.props,
            template: parts.template,
            children,
            action,
            source: self.loc(element.span),
        })
    }

    /// The headings of a table's columns and its row menu belong to no
    /// row: they must not read the row variable `var`.
    fn table_parts(&mut self, element: &ast::Element, var: &str, children: &[ir::Node]) {
        let reads_row = |expr: &ir::Expr| {
            let mut found = false;
            expr.walk(&mut |e| {
                if let ir::ExprKind::Binding {
                    binding: ir::Binding::Local { name, .. },
                } = &e.kind
                    && name == var
                {
                    found = true;
                }
            });
            found
        };
        let prop_reads = |prop: &ir::Prop| match prop {
            ir::Prop::Expr { expr } => reads_row(expr),
            ir::Prop::Text { text } => text.exprs().into_iter().any(reads_row),
            _ => false,
        };
        let action_reads = |action: &Option<ir::Action>| match action {
            Some(ir::Action::Engine { args, .. }) => args.iter().any(reads_row),
            Some(ir::Action::Session {
                op: ir::SessionOp::Set { value } | ir::SessionOp::PushStr { value },
                ..
            }) => reads_row(value),
            _ => false,
        };
        let mut misplaced = None;
        for child in children {
            let ir::Node::Element(part) = child else {
                continue;
            };
            if part.kind == "TableColumn"
                && (part.props.values().any(prop_reads) || action_reads(&part.action))
            {
                misplaced = Some("a column's heading and sort action");
            }
            if part.kind == "TableMenu" {
                child.walk(&mut |node| {
                    if let ir::Node::Element(item) = node
                        && (item.props.values().any(prop_reads) || action_reads(&item.action))
                    {
                        misplaced = Some("the row menu");
                    }
                });
            }
        }
        if let Some(what) = misplaced {
            self.error(
                element.span,
                format!(
                    "{what} belong to no row, so they cannot read `{var}`; act on the table's `selection`"
                ),
            );
        }
    }

    /// Check an element against its component's spec: its props, template,
    /// children, action and where it stands. `None` once reported.
    fn shape(
        &mut self,
        element: &ast::Element,
        name: &str,
        ctx: &Ctx,
    ) -> Option<&'static ComponentSpec> {
        let prop_names: Vec<String> = element
            .props
            .iter()
            .map(|prop| prop.name.to_string())
            .collect();
        // Items of navigation containers may be conditional or repeated:
        // what counts is the elements in each branch and template.
        let child_names: Option<Vec<Option<String>>> = element
            .children
            .as_ref()
            .map(|children| ast::child_names(children, spec::DYNAMIC_ITEMS.contains(&name)));
        let keywords: Vec<(String, String)> = element
            .props
            .iter()
            .filter_map(|prop| Some((prop.name.to_string(), prop.keyword()?)))
            .collect();
        let shape = ElementShape {
            name,
            props: prop_names.iter().map(String::as_str).collect(),
            keywords: keywords
                .iter()
                .map(|(name, value)| (name.as_str(), value.as_str()))
                .collect(),
            has_template: element.template.is_some(),
            children: child_names.as_ref().map(|children| {
                children
                    .iter()
                    .map(|child| match child {
                        Some(name) => ChildShape::Element(name),
                        None => ChildShape::Other,
                    })
                    .collect()
            }),
            has_action: element.action.is_some(),
            parent: ctx.parent.as_deref(),
            is_root: ctx.is_root,
        };
        let shape_errors = spec::check_shape(&shape);
        let failed = !shape_errors.is_empty();
        for error in shape_errors {
            let span = error
                .prop
                .as_ref()
                .and_then(|prop| element.prop(prop))
                .map(|prop| prop.name.span())
                .unwrap_or(element.span);
            self.error(span, error.message);
        }
        let component = spec::component(name)?;
        if failed {
            return None;
        }
        Some(component)
    }

    /// The props that set what the children see, lowered before the
    /// others: selections, navigation paths, and the queries, items and
    /// routes a template variable stands for.
    fn context_props(
        &mut self,
        element: &ast::Element,
        component: &ComponentSpec,
        ctx: &Ctx,
        scope: &mut Scope,
        parts: &mut Parts,
    ) {
        for prop in &element.props {
            let prop_name = prop.name.to_string();
            let Some(prop_spec) = component.prop(&prop_name) else {
                continue;
            };
            match prop_spec.kind {
                PropKind::SelectionField => {
                    if let Some(field) =
                        self.session_field(&prop.value, scope, FieldExpect::Selection)
                    {
                        parts.child_ctx.selection = Some(field.ty.clone());
                        parts.props.insert(prop_name, ir::Prop::Field { field });
                    }
                }
                PropKind::PathField => {
                    if let Some(field) = self.session_field(&prop.value, scope, FieldExpect::Path) {
                        if let Type::List { inner } = &field.ty {
                            parts.child_ctx.route = Some((**inner).clone());
                        }
                        parts.props.insert(prop_name, ir::Prop::Field { field });
                    }
                }
                PropKind::Query => self.query_prop(element, prop, scope, parts),
                PropKind::Items => self.items_prop(element, prop, scope, parts),
                _ => {}
            }
        }
        if parts.kind == "Destination"
            && let Some(var) = &element.template
        {
            match &ctx.route {
                Some(route) => parts.bind(var.to_string(), route.clone(), scope),
                None => self.error(element.span, "`Destination` must be inside `Navigation`"),
            }
        }
    }

    /// A query's rows, or its one result, are what the template stands
    /// for.
    fn query_prop(
        &mut self,
        element: &ast::Element,
        prop: &ast::Prop,
        scope: &mut Scope,
        parts: &mut Parts,
    ) {
        let Some(query) = self.query(&prop.value, scope) else {
            return;
        };
        let shown = match &query.result {
            Type::Optional { inner } => (**inner).clone(),
            other => other.clone(),
        };
        if let Some(empty) = element.prop("empty")
            && !matches!(query.result, Type::Optional { .. } | Type::List { .. })
        {
            self.error(
                empty.name.span(),
                format!(
                    "`empty:` needs a query whose result can be empty (a list or an \
                     optional), found {}",
                    query.result.display()
                ),
            );
        }
        if let Some(var) = &element.template {
            parts.bind(var.to_string(), shown, scope);
        }
        parts
            .props
            .insert(prop.name.to_string(), ir::Prop::Query { query });
    }

    /// `items` is a list of records; the template stands for one.
    fn items_prop(
        &mut self,
        element: &ast::Element,
        prop: &ast::Prop,
        scope: &mut Scope,
        parts: &mut Parts,
    ) {
        let Some(expr) = self.expr(&prop.value, scope) else {
            return;
        };
        let item_ty = match &expr.ty {
            Type::List { inner } if matches!(**inner, Type::Record { .. }) => (**inner).clone(),
            other => {
                self.error(
                    prop.value.span(),
                    format!("`items` needs a list of records, found {}", other.display()),
                );
                return;
            }
        };
        if let Some(var) = &element.template {
            parts.bind(var.to_string(), item_ty, scope);
        }
        parts
            .props
            .insert(prop.name.to_string(), ir::Prop::Expr { expr });
    }

    /// Lower one prop by its kind; the props `context_props` lowers are
    /// skipped here.
    fn prop(
        &mut self,
        prop: &ast::Prop,
        kind: PropKind,
        path: &str,
        ctx: &Ctx,
        parts: &Parts,
        scope: &Scope,
    ) -> Option<ir::Prop> {
        let name = parts.kind.as_str();
        let prop_name = prop.name.to_string();
        let value = &prop.value;
        // A corner may name a radius of `style!`, alone or in its
        // expression; the platform's radius takes its place at build time.
        if prop_name == "corner" && matches!(kind, PropKind::Number | PropKind::Length) {
            if let Some(corner) = self.corner_name(value) {
                return Some(ir::Prop::Expr { expr: corner });
            }
            if let ast::Expr::Path(path) = value
                && let [name] = path.segments.as_slice()
                && scope.get(&name.to_string()).is_none()
            {
                let message = crate::style::unknown_corner(
                    &name.to_string(),
                    self.corners.iter().map(|(name, _)| name.as_str()),
                );
                self.error(path.span(), message);
                return None;
            }
            if matches!(kind, PropKind::Length)
                && !matches!(value, ast::Expr::Literal { .. } | ast::Expr::Unary { .. })
            {
                self.corner_names = true;
                let lowered = self.length(&prop_name, value, scope);
                self.corner_names = false;
                return lowered;
            }
        }
        match kind {
            PropKind::Id => self.id(value).map(|value| ir::Prop::Id { value }),
            PropKind::Keyword(allowed) => self.keyword(value, allowed),
            PropKind::Flag => self.flag(value),
            PropKind::DisplayText => self
                .display(
                    value,
                    scope,
                    text_key(&self.component, path, &prop_name),
                    false,
                )
                .map(|text| ir::Prop::Text { text }),
            PropKind::LocalizedText => self
                .display(
                    value,
                    scope,
                    text_key(&self.component, path, &prop_name),
                    true,
                )
                .map(|text| ir::Prop::Text { text }),
            PropKind::Icon => self.icon(value),
            PropKind::Shortcut => self.shortcut(value, scope),
            // A picture is a declared resource, or a `MediaSource`
            // the engine gives: a file or an http(s) URL.
            PropKind::SvgResource => self.svg_resource(value),
            PropKind::ImageResource => match value {
                ast::Expr::Path(_) => self.media_source(value, scope),
                _ => self.image_resource(value),
            },
            // A literal length is checked like a number; any other
            // numeric expression is a length that follows it.
            PropKind::Length
                if !matches!(value, ast::Expr::Literal { .. } | ast::Expr::Unary { .. }) =>
            {
                self.length(&prop_name, value, scope)
            }
            PropKind::Number | PropKind::Length => self.literal_number(name, &prop_name, value),
            PropKind::NumberValue => self.number_value(&prop_name, value, scope),
            PropKind::Bool => self
                .expect(value, &Type::Bool, scope, "this property")
                .map(|expr| ir::Prop::Expr { expr }),
            PropKind::TextValue => self
                .expect(value, &Type::Text, scope, "this property")
                .map(|expr| ir::Prop::Expr { expr }),
            PropKind::TextField => self.field_prop(value, scope, FieldExpect::Text),
            PropKind::BoolField => self.field_prop(value, scope, FieldExpect::Bool),
            PropKind::NumberField => self.field_prop(value, scope, FieldExpect::Number),
            PropKind::FileField => self.field_prop(value, scope, FieldExpect::File),
            PropKind::PositionField => self.field_prop(value, scope, FieldExpect::Position),
            PropKind::KeysField => self.field_prop(value, scope, FieldExpect::Keys),
            PropKind::FileKinds => self.file_kinds(value),
            PropKind::PickerField => self.field_prop(value, scope, FieldExpect::Picker),
            // A container's `fallback` is one of its own values.
            PropKind::SelectionValue => {
                let selection = if prop_name == "fallback" {
                    parts.child_ctx.selection.clone()
                } else {
                    ctx.selection.clone()
                };
                self.selection_value(value, selection, scope)
            }
            PropKind::Route => self.route(value, ctx.route.clone(), scope),
            PropKind::KeyField => self.key(value, parts.template.as_ref()),
            PropKind::WindowId => self.window_id(value, name, &prop_name),
            // Slots are expanded before elements are lowered.
            PropKind::SlotName => None,
            PropKind::Transition | PropKind::MotionToken | PropKind::HeroName => {
                self.style_name(kind, &prop_name, value)
            }
            PropKind::ColorRole if name == "Text" && prop_name == "color" => self.text_color(value),
            PropKind::ColorRole => self.color_role(value),
            PropKind::PatternName => self.pattern_name(value),
            PropKind::NumberList => self.number_list(&prop_name, value, scope),
            PropKind::KeyValue => self.key_value(&prop_name, value, scope),
            PropKind::Watched => self.watched(&prop_name, value, scope),
            PropKind::StyleVariant => self.style_variant(name, value),
            PropKind::TypeName => self.type_name(value),
            PropKind::MediaSource => self.media_source(value, scope),
            PropKind::Player => self.player(value, scope),
            PropKind::Feature => self.feature(value),
            PropKind::SelectionField | PropKind::PathField | PropKind::Items | PropKind::Query => {
                None
            }
        }
    }

    /// Lower an element's children with the context its props set.
    fn children(
        &mut self,
        element: &ast::Element,
        path: &str,
        child_ctx: &Ctx,
        scope: &mut Scope,
    ) -> Vec<ir::Node> {
        match (&element.children, element.name.to_string().as_str()) {
            // The layouts of one `Adaptive` are alternatives: the same
            // control id in two of them names the same control.
            (Some(children), "Adaptive") => {
                let before = self.ids.clone();
                let mut all = before.clone();
                let mut lowered = Vec::new();
                for (index, child) in children.iter().enumerate() {
                    self.ids = before.clone();
                    lowered.extend(self.node(child, &format!("{path}.{index}"), child_ctx, scope));
                    all.append(&mut self.ids);
                }
                self.ids = all;
                self.adaptive(element, &lowered);
                lowered
            }
            (Some(children), _) => self.nodes(children, path, child_ctx, scope),
            (None, _) => Vec::new(),
        }
    }

    /// A `Tabs` keeps at most one `TabAccessory`, written directly in it:
    /// the bar is there for every page, and `shown:` hides it.
    fn tab_accessory(&mut self, element: &ast::Element, children: &[ir::Node]) {
        let direct = children
            .iter()
            .filter(|node| matches!(node, ir::Node::Element(child) if child.kind == "TabAccessory"))
            .count();
        // The tabs' own items: through conditions and `ForEach`, not into
        // the pages, which may hold tabs of their own.
        fn count(nodes: &[ir::Node]) -> usize {
            nodes
                .iter()
                .map(|node| match node {
                    ir::Node::Element(element) if element.kind == "TabAccessory" => 1,
                    ir::Node::Element(element) if element.kind == "ForEach" => {
                        count(&element.children)
                    }
                    ir::Node::Element(_) | ir::Node::Component(_) => 0,
                    ir::Node::If(node) => {
                        node.branches
                            .iter()
                            .map(|(_, body)| count(body))
                            .sum::<usize>()
                            + count(&node.otherwise)
                    }
                    ir::Node::IfLet(node) => count(&node.then) + count(&node.otherwise),
                    ir::Node::Match(node) => node.arms.iter().map(|arm| count(&arm.body)).sum(),
                })
                .sum()
        }
        let all = count(children);
        if direct > 1 {
            self.error(element.span, "`Tabs` keeps one `TabAccessory`");
        } else if all > direct {
            self.error(
                element.span,
                "write a `TabAccessory` directly in its `Tabs` and hide it with `shown:`, not inside `if` or `ForEach`",
            );
        }
    }

    /// An `Adaptive` has one `Layout` per size, and needs `[layouts]`; the
    /// `tv` layout is optional.
    fn adaptive(&mut self, element: &ast::Element, layouts: &[ir::Node]) {
        if self.layouts.is_none() {
            self.error(
                element.span,
                "error[B402]: `Adaptive` needs the breakpoints of `[layouts]` in Bern.toml, e.g. `compact_below = 600`",
            );
        }
        let sizes: Vec<&str> = layouts
            .iter()
            .filter_map(|node| match node {
                ir::Node::Element(layout) => layout.keyword("size"),
                _ => None,
            })
            .collect();
        for size in ir::LayoutSize::ALL {
            let count = sizes.iter().filter(|name| **name == size.name()).count();
            let required = ir::LayoutSize::REQUIRED.contains(&size);
            if (required && count != 1) || count > 1 {
                self.error(
                    element.span,
                    format!(
                        "error[B401]: `Adaptive` needs exactly one `Layout(size: {})`, found {count}",
                        size.name()
                    ),
                );
            }
            // An optional size needs its breakpoint to have any width.
            if count == 1
                && !required
                && let Some(config) = self.layouts
                && !config.has(size)
            {
                let breakpoint = match size {
                    ir::LayoutSize::Medium => "medium_below",
                    _ => "expanded_below",
                };
                self.error(
                    element.span,
                    format!(
                        "error[B401]: `Layout(size: {})` needs `{breakpoint}` in `[layouts]` of Bern.toml",
                        size.name()
                    ),
                );
            }
        }
        // A television's layout is chosen by the device, not the width:
        // optional, one at most, and without a breakpoint.
        let tv = sizes.iter().filter(|name| **name == "tv").count();
        if tv > 1 {
            self.error(
                element.span,
                format!("error[B401]: `Adaptive` has at most one `Layout(size: tv)`, found {tv}"),
            );
        }
    }
}