Skip to main content

cddl_cat/
ast.rs

1//! This module defines the Abstract Syntax Tree.
2//!
3//! It contains a tree representation of CDDL rules, closely matching the
4//! syntax used in the original CDDL text.
5//!
6
7/// A literal value, i.e. `"foo"`, `1.0`, or `h'FFF7'`
8///
9/// CDDL ABNF grammar:
10/// ```text
11/// value = number / text / bytes
12/// ```
13#[derive(Debug, PartialEq)]
14pub enum Value {
15    /// A text-string literal.
16    Text(String),
17    /// An unsigned integer literal.
18    Uint(u64),
19    /// A negative integer literal.
20    Nint(i64),
21    /// A floating-point literal.
22    Float(f64),
23    /// A byte-tring literal.
24    Bytes(Vec<u8>),
25}
26
27/// The "key" part of a key-value group member.
28#[derive(Debug, PartialEq)]
29#[allow(missing_docs)]
30pub enum MemberKeyVal {
31    /// Any type specified with the `=>` separator.
32    Type1(Type1),
33    /// A type name.
34    Bareword(String),
35    /// A literal value.
36    Value(Value),
37}
38
39type IsCut = bool;
40
41/// The "key" part of a key-value group member, along with its "cut" semantics.
42///
43/// When validating a map, "cut" means that once the key has matched, no other
44/// keys in this CDDL group will be attempted.  See RFC8610 for more details.
45///
46#[derive(Debug, PartialEq)]
47pub struct MemberKey {
48    /// The actual key definition.
49    pub val: MemberKeyVal,
50    /// `true` if cut semantics are specified.
51    pub cut: IsCut,
52}
53
54/// A group member, typically one element of an array or map.
55#[derive(Debug, PartialEq)]
56#[allow(missing_docs)]
57pub struct Member {
58    pub key: Option<MemberKey>,
59    pub value: Type,
60}
61
62/// An "occurrence" which specifies how many elements can match a group member.
63#[allow(missing_docs)]
64#[derive(Debug, PartialEq, Eq, Clone)]
65pub enum Occur {
66    Optional,
67    ZeroOrMore,
68    OneOrMore,
69    Numbered(usize, usize),
70}
71
72/// The part of a "group entry" after the occurrence.
73#[derive(Debug, PartialEq)]
74#[allow(missing_docs)]
75pub enum GrpEntVal {
76    Member(Member),
77    Groupname(String),
78    Parenthesized(Group),
79}
80
81/// A group entry contains one element of a group.
82///
83/// Each key-value pair in map, each element of an array, or each group
84/// (inline or referenced by name) will be stored in a `GrpEnt`.
85///
86/// CDDL ABNF grammar:
87/// ```text
88/// grpent = [occur S] [memberkey S] type
89///        / [occur S] groupname [genericarg]  ; preempted by above
90///        / [occur S] "(" S group S ")"
91/// ```
92#[derive(Debug, PartialEq)]
93#[allow(missing_docs)]
94pub struct GrpEnt {
95    pub occur: Option<Occur>,
96    pub val: GrpEntVal,
97}
98
99/// A group choice contains one of the choices making up a group.
100///
101/// Each group choice is itself made up of individual group entries.
102///
103/// CDDL ABNF grammar:
104/// ```text
105/// grpchoice = *(grpent optcom)
106/// ```
107/// Translated: "zero-or-more group-entries separated by an optional comma"
108#[derive(Debug, PartialEq)]
109pub struct GrpChoice(pub Vec<GrpEnt>);
110
111/// A group contains a number of elements.
112///
113/// Each group is itself made up of group choices, only one of which needs to
114/// match.
115///
116/// CDDL ABNF grammar:
117/// ```text
118/// group = grpchoice *(S "//" S grpchoice)
119/// ```
120#[derive(Debug, PartialEq)]
121pub struct Group(pub Vec<GrpChoice>);
122
123/// A name identifier with generic arguments.
124///
125/// A name may have generic arguments, e.g. `message<K,V>`.
126/// A name without generic arguments will have an empty generic_args Vec.
127///
128/// CDDL ABNF grammar:
129/// ```text
130/// genericarg = "<" S type1 S *("," S type1 S ) ">"
131/// type2 = ...
132///       / typename [genericarg]
133///       / "~" S typename [genericarg]
134///       / "&" S groupname [genericarg]
135///       / ...
136/// ```
137#[derive(Debug, PartialEq)]
138pub struct NameGeneric {
139    /// A type or group name.
140    pub name: String,
141    /// Generic arguments, if any.
142    pub generic_args: Vec<Type1>,
143}
144
145/// Type2 is the main representation of a CDDL type.
146///
147/// Note: not all type2 syntax is implemented.
148/// Types starting with `&`, `#` are not yet supported.
149///
150/// CDDL ABNF grammar:
151/// ```text
152/// type2 = value
153///       / typename [genericarg]
154///       / "(" S type S ")"
155///       / "{" S group S "}"
156///       / "[" S group S "]"
157///       / "~" S typename [genericarg]
158///       / "&" S "(" S group S ")"
159///       / "&" S groupname [genericarg]
160///       / "#" "6" ["." uint] "(" S type S ")"
161///       / "#" DIGIT ["." uint]
162///       / "#"
163/// ```
164#[derive(Debug, PartialEq)]
165#[allow(missing_docs)]
166pub enum Type2 {
167    Value(Value),
168    Typename(NameGeneric),
169    Parethesized(Type),
170    Map(Group),
171    Array(Group),
172    Unwrap(NameGeneric),
173    ChoiceifyInline(Group),
174    Choiceify(NameGeneric),
175}
176
177/// A CDDL type, with an additional range or control operator.
178///
179/// CDDL ABNF grammar:
180/// ```text
181/// type1 = type2 [S (rangeop / ctlop) S type2]
182/// ```
183#[derive(Debug, PartialEq)]
184pub enum Type1 {
185    /// A `Type1` containing only a `Type2` with no operators
186    Simple(Type2),
187    /// A range (e.g. `1..10`)
188    Range(TypeRange),
189    /// A type with a control operator attached (e.g. `bstr .size 32`)
190    Control(TypeControl),
191}
192
193/// A CDDL type, specified with a range operator.
194///
195/// Range operators are `..` (inclusive range) and `...` (exclusive range).
196/// CDDL only allows the range operators on pairs of integers or floats.
197#[derive(Debug, PartialEq)]
198#[allow(missing_docs)]
199pub struct TypeRange {
200    pub start: Type2,
201    pub end: Type2,
202    pub inclusive: bool,
203}
204
205/// A CDDL type, specified with a control operator.
206///
207/// Control operators can express a range of possibilities, including
208/// `.size N` (limit size of a value in bytes) or `.regexp` (requiring a text
209/// string to match the given regular expression).
210#[derive(Debug, PartialEq)]
211#[allow(missing_docs)]
212pub struct TypeControl {
213    pub target: Type2,
214    pub arg: Type2,
215    pub op: String,
216}
217
218/// A CDDL type, with choices.
219///
220/// CDDL ABNF grammar:
221/// ```text
222/// type = type1 *(S "/" S type1)
223/// ```
224#[derive(Debug, PartialEq)]
225pub struct Type(pub Vec<Type1>);
226
227/// A CDDL data structure specification
228///
229/// Each CDDL rule has a name and a syntax tree.  Rules can be
230/// referenced by name by other rules, or even within the same rule.
231///
232/// Note: `genericparm` is not yet supported.
233/// Note: "extend" assignment operators (`/=`,`//=`) are not yet supported.
234///
235/// CDDL ABNF grammar:
236/// ```text
237/// rule = typename [genericparm] S assignt S type
238///      / groupname [genericparm] S assigng S grpent
239/// ```
240#[derive(Debug, PartialEq)]
241pub struct Rule {
242    /// The rule name.
243    pub name: String,
244    /// Generic parameters.
245    pub generic_parms: Vec<String>,
246    /// The rule syntax tree.
247    pub val: RuleVal,
248}
249
250/// A rule's syntax tree, in either [`Type`] or [`GrpEnt`] form.
251///
252/// Note: `genericparm` is not yet supported.
253/// Note: "extend" assignment operators (`/=`,`//=`) are not yet supported.
254///
255/// CDDL ABNF grammar:
256/// ```text
257/// rule = typename [genericparm] S assignt S type
258///      / groupname [genericparm] S assigng S grpent
259/// ```
260#[derive(Debug, PartialEq)]
261pub enum RuleVal {
262    /// A type assignment rule.
263    AssignType(Type),
264    /// A group assignment rule.
265    AssignGroup(GrpEnt),
266    // TODO: /= and //= operators --> ExtendType(Type), ExtendGroup(GrpEnt)
267}
268
269/// A CDDL specification, containing multiple rule syntax trees.
270///
271/// This is the output from the parser for a given CDDL text input.
272///
273#[derive(Debug, PartialEq)]
274pub struct Cddl {
275    /// Rules and their syntax trees.
276    pub rules: Vec<Rule>,
277}
278
279/// A CDDL specification, containing multiple rule syntax trees.
280///
281/// This is the output from the parser for a given CDDL text input.
282/// CddlSlice is exactly the same as [`Cddl`] except that it also
283/// preserves a copy of the string used to compose that rule.
284///
285#[derive(Debug, PartialEq)]
286pub struct CddlSlice {
287    /// Rules and their syntax trees.
288    pub rules: Vec<(Rule, String)>,
289}