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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! Surface AST — uniform syntax before resolve-time dispatch.
//!
//! `(op child1 child2 ..rest)` looks the same regardless of operator kind.
//! The lowering pass inspects `OpKind` to produce the strongly-typed `Pattern`.
use crate::ast::{Action, Command, MultSpec, RhsTerm, Span};
/// A child in a surface pattern.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SurfacePatChild {
/// Single element pattern.
Elem(SurfacePattern),
/// Element with multiplicity: `x:2`, `x:k`, `x:k>=2`.
ElemMult(SurfacePattern, MultSpec),
}
/// Uniform pattern — `(op children...)` with optional `..rest` and `:mult`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SurfacePattern {
/// Bare identifier = pattern variable.
Var(String, Span),
/// Literal constant.
Lit(String, Span),
/// `(op [..pre] child1 child2 ... [..suf])`.
App {
op: String,
prefix: Option<(String, Span)>,
children: Vec<SurfacePatChild>,
suffix: Option<(String, Span)>,
span: Span,
},
}
impl SurfacePattern {
pub fn span(&self) -> Span {
match self {
SurfacePattern::Var(_, s) | SurfacePattern::Lit(_, s) => *s,
SurfacePattern::App { span, .. } => *span,
}
}
}
/// Surface command — pattern-bearing commands use `SurfacePattern`,
/// everything else passes through as `Command`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SurfaceCommand {
Rewrite {
lhs: SurfacePattern,
rhs: RhsTerm,
when: Vec<SurfacePattern>,
subsume: bool,
/// `:ruleset name`, or `None` for the default ruleset.
ruleset: Option<String>,
},
Rule {
body: Vec<SurfacePattern>,
head: Vec<Action>,
/// `:ruleset name`, or `None` for the default ruleset.
ruleset: Option<String>,
},
Pass(Command),
}