datex_core/disassembler/
assertions.rs1use crate::{
2 disassembler::{
3 InstructionTree, disassemble_body, disassemble_body_to_string,
4 disassemble_instruction_tree_to_string, get_instruction_tree_from_list,
5 options::DisassemblerOptions,
6 },
7 global::protocol_structures::{
8 instructions::{
9 CountOrUnbounded, Instruction, NestedInstructionResolutionStrategy,
10 },
11 regular_instructions::RegularInstruction,
12 },
13 prelude::*,
14};
15use core::slice::Iter;
16
17#[macro_export]
18macro_rules! assert_instructions_equal {
19 ($dxb:expr, $expected:expr) => {{
20 use $crate::global::protocol_structures::instructions::NestedInstructionResolutionStrategy;
21 use $crate::disassembler::disassemble_body;
22
23 let (instructions, err) = disassemble_body($dxb, NestedInstructionResolutionStrategy::ResolveNestedScopesFlat);
24 if let Some(err) = err {
25 panic!("Parser error: {}", err);
26 }
27 assert_eq!(
28 &instructions.flatten(),
29 &$expected
30 );
31 }}
32}
33
34#[macro_export]
35macro_rules! assert_regular_instructions_equal {
36 ($dxb:expr, ($($expr:expr),* $(,)?)) => {{
37 use $crate::disassembler::assertions::{resolve_instructions, assert_instruction_lists_eq};
38 use $crate::disassembler::{InstructionTree};
39 use $crate::global::protocol_structures::instructions::Instruction;
40
41 let dxb = $dxb;
42 assert_instruction_lists_eq(
43 resolve_instructions(dxb),
44 InstructionTree::<Instruction>::from(vec![$(InstructionTree::<Instruction>::from($expr),)*]).flatten_instructions(),
45 dxb,
46 );
47 }};
48 ($dxb:expr, $vec:expr $(,)?) => {{
49 use $crate::disassembler::assertions::{resolve_instructions, assert_instruction_lists_eq};
50 use $crate::disassembler::{InstructionTree};
51 use $crate::global::protocol_structures::instructions::Instruction;
52
53 let dxb = $dxb;
54 assert_instruction_lists_eq(
55 resolve_instructions(dxb),
56 InstructionTree::<Instruction>::from($vec.into_iter().map(|i| i.into()).collect::<Vec<_>>()).flatten_instructions(),
57 dxb,
58 );
59 }}
60}
61
62pub fn assert_instruction_lists_eq(
63 output_instructions: Vec<Instruction>,
64 expected_instructions: Vec<Instruction>,
65 output_dxb: &[u8],
66) {
67 if output_instructions != expected_instructions {
68 let (expected_tree, expected_err) =
69 get_instruction_tree_from_list(expected_instructions);
70 panic!(
71 "Output did not match expected instructions:\n\nOutput:\n{}\n\nExpected:\n{}\n",
72 disassemble_body_to_string(
73 output_dxb,
74 DisassemblerOptions::default()
75 ),
76 disassemble_instruction_tree_to_string(
77 expected_tree,
78 expected_err,
79 DisassemblerOptions::default()
80 ),
81 );
82 }
83}
84
85pub fn resolve_instructions(dxb: &[u8]) -> Vec<Instruction> {
86 let (instructions, err) = disassemble_body(
87 dxb,
88 NestedInstructionResolutionStrategy::ResolveNestedScopesFlat,
89 );
90 if let Some(err) = err {
91 panic!("Parser error: {}", err);
92 }
93 instructions.flatten()
94}
95
96impl RegularInstruction {
97 pub fn with_children(
98 self,
99 children: Vec<InstructionTree<Instruction>>,
100 ) -> InstructionTree<Instruction> {
101 if children.is_empty()
103 && let CountOrUnbounded::Count(count) = self
104 .get_next_expected_instructions()
105 .total_count()
106 .expect("Expected count should be set for this instruction")
107 && count as usize != children.len()
108 {
109 panic!(
110 "Expected {} children for instruction {:?}, but got {}",
111 count,
112 self,
113 children.len()
114 );
115 }
116
117 InstructionTree::new_with_children(self.into(), children)
118 }
119
120 fn calculate_children_count(
122 children: &[InstructionTree<Instruction>],
123 ) -> u32 {
124 fn visit_next_child(
125 count: &mut u32,
126 skip: bool,
127 iterator: &mut Iter<InstructionTree<Instruction>>,
128 ) -> Option<u32> {
129 let current = match iterator.next() {
130 Some(next) => next,
131 None => return Some(*count),
132 };
133
134 if !skip {
135 *count += 1;
136 }
137
138 if current.children().is_empty()
140 && let Some(child_count) = current
141 .instruction()
142 .get_next_expected_instructions()
143 .total_count()
144 && let CountOrUnbounded::Count(child_count) = child_count
145 {
146 for _ in 0..child_count {
147 visit_next_child(count, true, iterator);
148 }
149 }
150
151 None
152 }
153
154 let iterator = &mut children.iter();
155 let mut count = 0;
156 loop {
157 if let Some(count) = visit_next_child(&mut count, false, iterator) {
158 return count;
159 }
160 }
161 }
162
163 pub fn statements_with_children(
164 terminated: bool,
165 children: Vec<InstructionTree<Instruction>>,
166 ) -> InstructionTree<Instruction> {
167 RegularInstruction::statements(
168 RegularInstruction::calculate_children_count(&children),
169 terminated,
170 )
171 .with_children(children)
172 }
173
174 pub fn list_with_children(
175 children: Vec<InstructionTree<Instruction>>,
176 ) -> InstructionTree<Instruction> {
177 RegularInstruction::list(RegularInstruction::calculate_children_count(
178 &children,
179 ))
180 .with_children(children)
181 }
182}
183
184#[macro_export]
185macro_rules! instructions {
186 ($($expr:expr),* $(,)?) => {vec![
187 $($expr.into(),)*
188 ]};
189}