summaryrefslogtreecommitdiff
path: root/tools/pdl/src/ast.rs
blob: 8d4e87592a4a5f851ad9521a95bdb336b291b141 (plain)
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
use codespan_reporting::diagnostic;
use codespan_reporting::files;
use serde::Serialize;
use std::fmt;
use std::ops;

/// File identfiier.
/// References a source file in the source database.
pub type FileId = usize;

/// Source database.
/// Stores the source file contents for reference.
pub type SourceDatabase = files::SimpleFiles<String, String>;

#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct SourceLocation {
    /// Byte offset into the file (counted from zero).
    pub offset: usize,
    /// Line number (counted from zero).
    pub line: usize,
    /// Column number (counted from zero)
    pub column: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct SourceRange {
    pub file: FileId,
    pub start: SourceLocation,
    pub end: SourceLocation,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename = "comment")]
pub struct Comment {
    pub loc: SourceRange,
    pub text: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EndiannessValue {
    LittleEndian,
    BigEndian,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename = "endianness_declaration")]
pub struct Endianness {
    pub loc: SourceRange,
    pub value: EndiannessValue,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind")]
pub enum Expr {
    #[serde(rename = "identifier")]
    Identifier { loc: SourceRange, name: String },
    #[serde(rename = "integer")]
    Integer { loc: SourceRange, value: usize },
    #[serde(rename = "unary_expr")]
    Unary { loc: SourceRange, op: String, operand: Box<Expr> },
    #[serde(rename = "binary_expr")]
    Binary { loc: SourceRange, op: String, operands: Box<(Expr, Expr)> },
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename = "tag")]
pub struct Tag {
    pub id: String,
    pub loc: SourceRange,
    pub value: usize,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename = "constraint")]
pub struct Constraint {
    pub id: String,
    pub loc: SourceRange,
    pub value: Expr,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind")]
pub enum Field {
    #[serde(rename = "checksum_field")]
    Checksum { loc: SourceRange, field_id: String },
    #[serde(rename = "padding_field")]
    Padding { loc: SourceRange, width: usize },
    #[serde(rename = "size_field")]
    Size { loc: SourceRange, field_id: String, width: usize },
    #[serde(rename = "count_field")]
    Count { loc: SourceRange, field_id: String, width: usize },
    #[serde(rename = "body_field")]
    Body { loc: SourceRange },
    #[serde(rename = "payload_field")]
    Payload { loc: SourceRange, size_modifier: Option<String> },
    #[serde(rename = "fixed_field")]
    Fixed {
        loc: SourceRange,
        width: Option<usize>,
        value: Option<usize>,
        enum_id: Option<String>,
        tag_id: Option<String>,
    },
    #[serde(rename = "reserved_field")]
    Reserved { loc: SourceRange, width: usize },
    #[serde(rename = "array_field")]
    Array {
        loc: SourceRange,
        id: String,
        width: Option<usize>,
        type_id: Option<String>,
        size_modifier: Option<String>,
        size: Option<usize>,
    },
    #[serde(rename = "scalar_field")]
    Scalar { loc: SourceRange, id: String, width: usize },
    #[serde(rename = "typedef_field")]
    Typedef { loc: SourceRange, id: String, type_id: String },
    #[serde(rename = "group_field")]
    Group { loc: SourceRange, group_id: String, constraints: Vec<Constraint> },
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind", rename = "test_case")]
pub struct TestCase {
    pub loc: SourceRange,
    pub input: String,
}

#[derive(Debug, Serialize)]
#[serde(tag = "kind")]
pub enum Decl {
    #[serde(rename = "checksum_declaration")]
    Checksum { id: String, loc: SourceRange, function: String, width: usize },
    #[serde(rename = "custom_field_declaration")]
    CustomField { id: String, loc: SourceRange, width: Option<usize>, function: String },
    #[serde(rename = "enum_declaration")]
    Enum { id: String, loc: SourceRange, tags: Vec<Tag>, width: usize },
    #[serde(rename = "packet_declaration")]
    Packet {
        id: String,
        loc: SourceRange,
        constraints: Vec<Constraint>,
        fields: Vec<Field>,
        parent_id: Option<String>,
    },
    #[serde(rename = "struct_declaration")]
    Struct {
        id: String,
        loc: SourceRange,
        constraints: Vec<Constraint>,
        fields: Vec<Field>,
        parent_id: Option<String>,
    },
    #[serde(rename = "group_declaration")]
    Group { id: String, loc: SourceRange, fields: Vec<Field> },
    #[serde(rename = "test_declaration")]
    Test { loc: SourceRange, type_id: String, test_cases: Vec<TestCase> },
}

#[derive(Debug, Serialize)]
pub struct Grammar {
    pub version: String,
    pub file: FileId,
    pub comments: Vec<Comment>,
    pub endianness: Option<Endianness>,
    pub declarations: Vec<Decl>,
}

impl SourceLocation {
    /// Construct a new source location.
    ///
    /// The `line_starts` indicates the byte offsets where new lines
    /// start in the file. The first element should thus be `0` since
    /// every file has at least one line starting at offset `0`.
    pub fn new(offset: usize, line_starts: &[usize]) -> SourceLocation {
        let mut loc = SourceLocation { offset, line: 0, column: offset };
        for (line, start) in line_starts.iter().enumerate() {
            if *start > offset {
                break;
            }
            loc = SourceLocation { offset, line, column: offset - start };
        }
        loc
    }
}

impl SourceRange {
    pub fn primary(&self) -> diagnostic::Label<FileId> {
        diagnostic::Label::primary(self.file, self.start.offset..self.end.offset)
    }
    pub fn secondary(&self) -> diagnostic::Label<FileId> {
        diagnostic::Label::secondary(self.file, self.start.offset..self.end.offset)
    }
}

impl fmt::Display for SourceRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.start.line == self.end.line {
            write!(f, "{}:{}-{}", self.start.line, self.start.column, self.end.column)
        } else {
            write!(
                f,
                "{}:{}-{}:{}",
                self.start.line, self.start.column, self.end.line, self.end.column
            )
        }
    }
}

impl ops::Add<SourceRange> for SourceRange {
    type Output = SourceRange;

    fn add(self, rhs: SourceRange) -> SourceRange {
        assert!(self.file == rhs.file);
        SourceRange {
            file: self.file,
            start: self.start.min(rhs.start),
            end: self.end.max(rhs.end),
        }
    }
}

impl Grammar {
    pub fn new(file: FileId) -> Grammar {
        Grammar {
            version: "1,0".to_owned(),
            comments: vec![],
            endianness: None,
            declarations: vec![],
            file,
        }
    }
}

impl Decl {
    pub fn loc(&self) -> &SourceRange {
        match self {
            Decl::Checksum { loc, .. }
            | Decl::CustomField { loc, .. }
            | Decl::Enum { loc, .. }
            | Decl::Packet { loc, .. }
            | Decl::Struct { loc, .. }
            | Decl::Group { loc, .. }
            | Decl::Test { loc, .. } => loc,
        }
    }

    pub fn id(&self) -> Option<&String> {
        match self {
            Decl::Test { .. } => None,
            Decl::Checksum { id, .. }
            | Decl::CustomField { id, .. }
            | Decl::Enum { id, .. }
            | Decl::Packet { id, .. }
            | Decl::Struct { id, .. }
            | Decl::Group { id, .. } => Some(id),
        }
    }
}

impl Field {
    pub fn loc(&self) -> &SourceRange {
        match self {
            Field::Checksum { loc, .. }
            | Field::Padding { loc, .. }
            | Field::Size { loc, .. }
            | Field::Count { loc, .. }
            | Field::Body { loc, .. }
            | Field::Payload { loc, .. }
            | Field::Fixed { loc, .. }
            | Field::Reserved { loc, .. }
            | Field::Array { loc, .. }
            | Field::Scalar { loc, .. }
            | Field::Typedef { loc, .. }
            | Field::Group { loc, .. } => loc,
        }
    }

    pub fn id(&self) -> Option<&String> {
        match self {
            Field::Checksum { .. }
            | Field::Padding { .. }
            | Field::Size { .. }
            | Field::Count { .. }
            | Field::Body { .. }
            | Field::Payload { .. }
            | Field::Fixed { .. }
            | Field::Reserved { .. }
            | Field::Group { .. } => None,
            Field::Array { id, .. } | Field::Scalar { id, .. } | Field::Typedef { id, .. } => {
                Some(id)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn source_location_new() {
        let line_starts = &[0, 20, 80, 120, 150];
        assert_eq!(
            SourceLocation::new(0, line_starts),
            SourceLocation { offset: 0, line: 0, column: 0 }
        );
        assert_eq!(
            SourceLocation::new(10, line_starts),
            SourceLocation { offset: 10, line: 0, column: 10 }
        );
        assert_eq!(
            SourceLocation::new(50, line_starts),
            SourceLocation { offset: 50, line: 1, column: 30 }
        );
        assert_eq!(
            SourceLocation::new(100, line_starts),
            SourceLocation { offset: 100, line: 2, column: 20 }
        );
        assert_eq!(
            SourceLocation::new(1000, line_starts),
            SourceLocation { offset: 1000, line: 4, column: 850 }
        );
    }

    #[test]
    fn source_location_new_no_crash_with_empty_line_starts() {
        let loc = SourceLocation::new(100, &[]);
        assert_eq!(loc, SourceLocation { offset: 100, line: 0, column: 100 });
    }
}