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
//! Parse Syzkaller syntax descriptions
//! [syzlang](https://github.com/google/syzkaller/blob/master/docs/syscall_descriptions_syntax.md)
//! into a more suitable format for analysis with Rust.
//!
//! Parsing is done in several steps:
//! 1. Tokenize into [token::Token]
//! 2. Parse into [parser::Parsed]
//!

#![feature(extract_if)]
#![feature(test)]

pub mod parser;
pub mod token;

/// All the different errors the crate can produce
#[derive(thiserror::Error, Debug)]
pub enum Error {
	/// Some arbitrary string describing the error in more detail
	#[error("Error: {0}")]
	Error(String),

	/// The string passed in was unexpected / invalid
	#[error("Invalid String: {0}")]
	InvalidString(String),

	/// Tokenize error, string contains more details
	#[error("Tokenize: {0}")]
	Tokenize(String),

	#[error("Unexpected token")]
	UnexpectedToken,

	#[error("Unexpected length")]
	UnexpectedLength,

	#[error("Unexpected value")]
	UnexpectedValue,

	#[error("Unsupported")]
	Unsupported,

	/// Parser error, string contains more details
	#[error("Parse: {0}")]
	Parser(String),

	#[error("serde_json")]
	Serde(#[from] serde_json::Error),

	#[error("IO")]
	Io(#[from] std::io::Error),

	#[error("Utf8")]
	Utf8(#[from] std::str::Utf8Error),
}
type Result<T> = std::result::Result<T, Error>;

macro_rules! errloc {
	() => {{
		let line = line!();
		let file = file!();
		format!("{file}:{line}")
	}};
}

pub(crate) use errloc;

macro_rules! parsererror {
	($msg:expr) => {{
		let errloc = crate::errloc!();
		let err = format!("{errloc}: error: {}", $msg);
		log::error!("{err}");
		Err(crate::Error::Parser(err))
	}};
}
pub(crate) use parsererror;

macro_rules! generror {
	($msg:expr, $log:ident) => {{
		let errloc = crate::errloc!();
		let err = format!("{errloc}: error: {}", $msg);
		log::$log!("{err}");
		Err(crate::Error::Error(err))
	}};
	($msg:expr) => {
		generror!($msg, error)
	};
}
pub(crate) use generror;

macro_rules! verify {
	($expr:expr, $error:ident) => {
		if (!($expr)) {
			let errloc = crate::errloc!();
			log::error!("{errloc}: expression failed: {}", stringify!($expr));
			return Err(crate::Error::$error);
		}
	};
}
pub(crate) use verify;

macro_rules! consume {
	($tokens:expr, $check:expr) => {{
		if $tokens.is_empty() {
			let errloc = crate::errloc!();
			let msg = format!("{errloc}: expected {:?}, but tokens is empty", $check);
			log::error!("{msg}");
			return Err(crate::Error::Parser(msg));
		}
		let t = $tokens.remove(0);
		if t != $check {
			let errloc = crate::errloc!();
			let msg = format!("{errloc}: expected {:?}, but got token {:?}", $check, t);
			log::error!("{msg}");
			return Err(crate::Error::Parser(msg));
		}
		t
	}};
	($tokens:expr) => {{
		if $tokens.is_empty() {
			let errloc = crate::errloc!();
			let msg = format!("{errloc}: expected new value but tokens is empty");
			log::error!("{msg}");
			return Err(crate::Error::Parser(msg));
		}
		$tokens.remove(0)
	}};
}
pub(crate) use consume;

macro_rules! check_empty {
	($tokens:expr) => {
		if !$tokens.is_empty() {
			let errloc = crate::errloc!();
			let msg = format!("{errloc}: expected tokens to be empty, left: {:?}", $tokens);
			log::error!("{msg}");
			return Err(crate::Error::Parser(msg));
		}
	};
}
pub(crate) use check_empty;

macro_rules! gen_get_ident {
	($name:ident) => {
		/// Get identifier for object
		pub fn identifier(&self) -> &Identifier {
			&self.$name
		}
	};
}
pub(crate) use gen_get_ident;

macro_rules! gen_get {
	($funcname:ident, $field:ident, $val:ty) => {
		pub fn $funcname(&self) -> &$val {
			&self.$field
		}
	};
	($field:ident, $val:ty) => {
		gen_get! { $field, $field, $val }
	};
}

pub(crate) use gen_get;

macro_rules! gen_get_mut {
	($funcname:ident, $field:ident, $val:ty) => {
		pub fn $funcname(&mut self) -> &mut $val {
			&mut self.$field
		}
	};
	($field:ident, $val:ty) => {
		gen_get_mut! { $field, $field, $val }
	};
}

pub(crate) use gen_get_mut;

macro_rules! gen_get_iter {
	($funcname:ident, $field:ident, $val:ty) => {
		pub fn $funcname(&self) -> std::slice::Iter<'_, $val> {
			self.$field.iter()
		}
	};
	($field:ident, $val:ty) => {
		gen_get_iter! { $field, $field, $val }
	};
}
pub(crate) use gen_get_iter;

macro_rules! gen_find_ident {
	($field:ident) => {
		/// Find vector entry with a matching [Identifier]
		pub fn find_ident<'a>(entries: &'a [Self], ident: &Identifier) -> Option<&'a Self> {
			for val in entries.iter() {
				if val.$field == *ident {
					return Some(val);
				}
			}
			None
		}
	};
	($field:ident, $val:ty) => {
		gen_get_iter! { $field, $field, $val }
	};
}
pub(crate) use gen_find_ident;

macro_rules! gen_get_ident_matches {
	($name:ident) => {
		/// Get identifier for object
		pub fn ident_matches(&self, name: &str) -> bool {
			self.$name.name == name
		}
	};
}
pub(crate) use gen_get_ident_matches;

macro_rules! gen_find_by_ident {
	($name:ident, $field:ident, $val:ty) => {
		pub fn $name(&self, ident: &Identifier) -> Option<&$val> {
			self.$field.iter().find(|&s| s.identifier() == ident)
		}
	};
}
pub(crate) use gen_find_by_ident;

macro_rules! gen_find_by_name {
	($name:ident, $field:ident, $val:ty) => {
		pub fn $name<I: Into<Identifier>>(&self, name: I) -> Option<&$val> {
			let ident: Identifier = name.into();
			self.$field.iter().find(|&s| s.identifier() == &ident)
		}
	};
}
pub(crate) use gen_find_by_name;

#[cfg(test)]
#[ctor::ctor]
fn global_test_setup() {
	env_logger::init();
}