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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
#[cfg(feature = "alloc")] use lib::std::vec::Vec; #[cfg(feature = "std")] pub trait HexDisplay { /// Converts the value of `self` to a hex dump, returning the owned /// string. fn to_hex(&self, chunk_size: usize) -> String; /// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned /// string. fn to_hex_from(&self, chunk_size: usize, from: usize) -> String; } #[cfg(feature = "std")] static CHARS: &'static [u8] = b"0123456789abcdef"; #[cfg(feature = "std")] impl HexDisplay for [u8] { #[allow(unused_variables)] fn to_hex(&self, chunk_size: usize) -> String { self.to_hex_from(chunk_size, 0) } #[allow(unused_variables)] fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { let mut v = Vec::with_capacity(self.len() * 3); let mut i = from; for chunk in self.chunks(chunk_size) { let s = format!("{:08x}", i); for &ch in s.as_bytes().iter() { v.push(ch); } v.push(b'\t'); i += chunk_size; for &byte in chunk { v.push(CHARS[(byte >> 4) as usize]); v.push(CHARS[(byte & 0xf) as usize]); v.push(b' '); } if chunk_size > chunk.len() { for j in 0..(chunk_size - chunk.len()) { v.push(b' '); v.push(b' '); v.push(b' '); } } v.push(b'\t'); for &byte in chunk { if (byte >= 32 && byte <= 126) || byte >= 128 { v.push(byte); } else { v.push(b'.'); } } v.push(b'\n'); } String::from_utf8_lossy(&v[..]).into_owned() } } #[cfg(feature = "std")] impl HexDisplay for str { #[allow(unused_variables)] fn to_hex(&self, chunk_size: usize) -> String { self.to_hex_from(chunk_size, 0) } #[allow(unused_variables)] fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { self.as_bytes().to_hex_from(chunk_size, from) } } #[macro_export] macro_rules! nom_line ( () => (line!()); ); #[macro_export] macro_rules! nom_println ( ($($args:tt)*) => (println!($($args)*)); ); #[macro_export] macro_rules! nom_stringify ( ($($args:tt)*) => (stringify!($($args)*)); ); /// Prints a message if the parser fails /// /// The message prints the `Error` or `Incomplete` /// and the parser's calling code /// /// ``` /// # #[macro_use] extern crate nom; /// # fn main() { /// named!(f, dbg!( tag!( "abcd" ) ) ); /// /// let a = &b"efgh"[..]; /// /// // Will print the following message: /// // Error(Position(0, [101, 102, 103, 104])) at l.5 by ' tag ! ( "abcd" ) ' /// f(a); /// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! dbg ( ($i: expr, $submac:ident!( $($args:tt)* )) => ( { use $crate::lib::std::result::Result::*; let l = nom_line!(); match $submac!($i, $($args)*) { Err(e) => { nom_println!("Err({:?}) at l.{} by ' {} '", e, l, nom_stringify!($submac!($($args)*))); Err(e) }, a => a, } } ); ($i:expr, $f:ident) => ( dbg!($i, call!($f)); ); ); /// Prints a message and the input if the parser fails /// /// The message prints the `Error` or `Incomplete` /// and the parser's calling code. /// /// It also displays the input in hexdump format /// /// ```ignore /// # #[macro_use] extern crate nom; /// # fn main() { /// named!(f, dbg_dmp!( tag!( "abcd" ) ) ); /// /// let a = &b"efghijkl"[..]; /// /// // Will print the following message: /// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) ' /// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl /// f(a); /// # } #[macro_export(local_inner_macros)] #[cfg(feature = "std")] macro_rules! dbg_dmp ( ($i: expr, $submac:ident!( $($args:tt)* )) => ( { use $crate::HexDisplay; let l = nom_line!(); match $submac!($i, $($args)*) { Err(e) => { nom_println!("Error({:?}) at l.{} by ' {} '\n{}", e, l, nom_stringify!($submac!($($args)*)), $i.to_hex(8)); Err(e) }, a => a, } } ); ($i:expr, $f:ident) => ( dbg_dmp!($i, call!($f)); ); ); /*FIXME: convert functions to new error management pub fn error_to_list<P: Clone, E: Clone>(e: &Context<P, E>) -> Vec<(P, ErrorKind<E>)> { match e { &Context::Code(ref i, ref err) => { let mut v = Vec::new(); v.push((i.clone(), err.clone())); return v; } &Context::List(ref v) => { let mut v2 = v.clone(); v2.reverse(); v2 } } } pub fn compare_error_paths<P: Clone + PartialEq, E: Clone + PartialEq>(e1: &Context<P, E>, e2: &Context<P, E>) -> bool { error_to_list(e1) == error_to_list(e2) } #[cfg(feature = "std")] use lib::std::hash::Hash; #[cfg(feature = "std")] pub fn add_error_pattern<'a, I: Clone + Hash + Eq, O, E: Clone + Hash + Eq>( h: &mut HashMap<Vec<(I, ErrorKind<E>)>, &'a str>, res: IResult<I, O, E>, message: &'a str, ) -> bool { match res { Err(Err::Error(e)) | Err(Err::Failure(e)) => { h.insert(error_to_list(&e), message); true } _ => false, } } pub fn slice_to_offsets(input: &[u8], s: &[u8]) -> (usize, usize) { let start = input.as_ptr(); let off1 = s.as_ptr() as usize - start as usize; let off2 = off1 + s.len(); (off1, off2) } #[cfg(feature = "std")] pub fn prepare_errors<O, E: Clone>(input: &[u8], res: IResult<&[u8], O, E>) -> Option<Vec<(ErrorKind<E>, usize, usize)>> { if let Err(Err::Error(e)) = res { let mut v: Vec<(ErrorKind<E>, usize, usize)> = Vec::new(); match e { Context::Code(p, kind) => { let (o1, o2) = slice_to_offsets(input, p); v.push((kind, o1, o2)); } Context::List(mut l) => { for (p, kind) in l.drain(..) { let (o1, o2) = slice_to_offsets(input, p); v.push((kind, o1, o2)); } v.reverse() } } v.sort_by(|a, b| a.1.cmp(&b.1)); Some(v) } else { None } } #[cfg(feature = "std")] pub fn print_error<O, E: Clone>(input: &[u8], res: IResult<&[u8], O, E>) { if let Some(v) = prepare_errors(input, res) { let colors = generate_colors(&v); println!("parser codes: {}", print_codes(&colors, &HashMap::new())); println!("{}", print_offsets(input, 0, &v)); } else { println!("not an error"); } } #[cfg(feature = "std")] pub fn generate_colors<E>(v: &[(ErrorKind<E>, usize, usize)]) -> HashMap<u32, u8> { let mut h: HashMap<u32, u8> = HashMap::new(); let mut color = 0; for &(ref c, _, _) in v.iter() { h.insert(error_to_u32(c), color + 31); color = color + 1 % 7; } h } pub fn code_from_offset(v: &[(ErrorKind, usize, usize)], offset: usize) -> Option<u32> { let mut acc: Option<(u32, usize, usize)> = None; for &(ref ek, s, e) in v.iter() { let c = error_to_u32(ek); if s <= offset && offset <= e { if let Some((_, start, end)) = acc { if start <= s && e <= end { acc = Some((c, s, e)); } } else { acc = Some((c, s, e)); } } } if let Some((code, _, _)) = acc { return Some(code); } else { return None; } } #[cfg(feature = "alloc")] pub fn reset_color(v: &mut Vec<u8>) { v.push(0x1B); v.push(b'['); v.push(0); v.push(b'm'); } #[cfg(feature = "alloc")] pub fn write_color(v: &mut Vec<u8>, color: u8) { v.push(0x1B); v.push(b'['); v.push(1); v.push(b';'); let s = color.to_string(); let bytes = s.as_bytes(); v.extend(bytes.iter().cloned()); v.push(b'm'); } #[cfg(feature = "std")] #[cfg_attr(feature = "cargo-clippy", allow(implicit_hasher))] pub fn print_codes(colors: &HashMap<u32, u8>, names: &HashMap<u32, &str>) -> String { let mut v = Vec::new(); for (code, &color) in colors { if let Some(&s) = names.get(code) { let bytes = s.as_bytes(); write_color(&mut v, color); v.extend(bytes.iter().cloned()); } else { let s = code.to_string(); let bytes = s.as_bytes(); write_color(&mut v, color); v.extend(bytes.iter().cloned()); } reset_color(&mut v); v.push(b' '); } reset_color(&mut v); String::from_utf8_lossy(&v[..]).into_owned() } #[cfg(feature = "std")] pub fn print_offsets<E>(input: &[u8], from: usize, offsets: &[(ErrorKind<E>, usize, usize)]) -> String { let mut v = Vec::with_capacity(input.len() * 3); let mut i = from; let chunk_size = 8; let mut current_code: Option<u32> = None; let mut current_code2: Option<u32> = None; let colors = generate_colors(&offsets); for chunk in input.chunks(chunk_size) { let s = format!("{:08x}", i); for &ch in s.as_bytes().iter() { v.push(ch); } v.push(b'\t'); let mut k = i; let mut l = i; for &byte in chunk { if let Some(code) = code_from_offset(&offsets, k) { if let Some(current) = current_code { if current != code { reset_color(&mut v); current_code = Some(code); if let Some(&color) = colors.get(&code) { write_color(&mut v, color); } } } else { current_code = Some(code); if let Some(&color) = colors.get(&code) { write_color(&mut v, color); } } } v.push(CHARS[(byte >> 4) as usize]); v.push(CHARS[(byte & 0xf) as usize]); v.push(b' '); k = k + 1; } reset_color(&mut v); if chunk_size > chunk.len() { for _ in 0..(chunk_size - chunk.len()) { v.push(b' '); v.push(b' '); v.push(b' '); } } v.push(b'\t'); for &byte in chunk { if let Some(code) = code_from_offset(&offsets, l) { if let Some(current) = current_code2 { if current != code { reset_color(&mut v); current_code2 = Some(code); if let Some(&color) = colors.get(&code) { write_color(&mut v, color); } } } else { current_code2 = Some(code); if let Some(&color) = colors.get(&code) { write_color(&mut v, color); } } } if (byte >= 32 && byte <= 126) || byte >= 128 { v.push(byte); } else { v.push(b'.'); } l = l + 1; } reset_color(&mut v); v.push(b'\n'); i = i + chunk_size; } String::from_utf8_lossy(&v[..]).into_owned() } */ pub trait Convert<T> { fn convert(T) -> Self; }