1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! PDL parser and analyzer.
16 
17 pub mod analyzer;
18 pub mod ast;
19 pub mod backends;
20 pub mod parser;
21 #[cfg(test)]
22 pub mod test_utils;
23 
24 #[cfg(test)]
25 mod test {
26     use super::*;
27 
28     #[test]
rust_no_allocation_output_is_deterministic()29     fn rust_no_allocation_output_is_deterministic() {
30         // The generated code should be deterministic, to avoid unnecessary rebuilds during
31         // incremental builds.
32         let src = r#"
33 little_endian_packets
34 
35 enum Enum1 : 8 {
36   ENUM_VARIANT_ONE = 0x01,
37   ENUM_VARIANT_TWO = 0x02,
38 }
39 
40 packet Packet1 {
41   opcode : Enum1,
42   _payload_,
43 }
44 
45 struct Struct1 {
46   handle : 16,
47 }
48 
49 struct Struct2 {
50   _payload_
51 }
52 
53 struct Struct3 {
54   handle : Struct1,
55   value : Struct2,
56 }
57 
58 packet Packet2 : Packet1(opcode = ENUM_VARIANT_ONE) {
59   handle : Struct1,
60   value : Struct2,
61 }
62 "#
63         .to_owned();
64 
65         let mut sources1 = ast::SourceDatabase::new();
66         let mut sources2 = ast::SourceDatabase::new();
67         let mut sources3 = ast::SourceDatabase::new();
68 
69         let file1 = parser::parse_inline(&mut sources1, "foo", src.clone()).unwrap();
70         let file2 = parser::parse_inline(&mut sources2, "foo", src.clone()).unwrap();
71         let file3 = parser::parse_inline(&mut sources3, "foo", src).unwrap();
72 
73         let schema1 = backends::intermediate::generate(&file1).unwrap();
74         let schema2 = backends::intermediate::generate(&file2).unwrap();
75         let schema3 = backends::intermediate::generate(&file3).unwrap();
76 
77         let result1 = backends::rust_no_allocation::generate(&file1, &schema1).unwrap();
78         let result2 = backends::rust_no_allocation::generate(&file2, &schema2).unwrap();
79         let result3 = backends::rust_no_allocation::generate(&file3, &schema3).unwrap();
80 
81         assert_eq!(result1, result2);
82         assert_eq!(result2, result3);
83     }
84 }
85