| 1 |
#![cfg(feature = "std")] |
| 2 |
|
| 3 |
use std::io::Write; |
| 4 |
|
| 5 |
use crate::error::ParserError; |
| 6 |
use crate::stream::Stream; |
| 7 |
use crate::*; |
| 8 |
|
| 9 |
pub(crate) struct Trace<P, D, I, O, E> |
| 10 |
where |
| 11 |
P: Parser<I, O, E>, |
| 12 |
I: Stream, |
| 13 |
D: std::fmt::Display, |
| 14 |
E: ParserError<I>, |
| 15 |
{ |
| 16 |
parser: P, |
| 17 |
name: D, |
| 18 |
call_count: usize, |
| 19 |
i: core::marker::PhantomData<I>, |
| 20 |
o: core::marker::PhantomData<O>, |
| 21 |
e: core::marker::PhantomData<E>, |
| 22 |
} |
| 23 |
|
| 24 |
impl<P, D, I, O, E> Trace<P, D, I, O, E> |
| 25 |
where |
| 26 |
P: Parser<I, O, E>, |
| 27 |
I: Stream, |
| 28 |
D: std::fmt::Display, |
| 29 |
E: ParserError<I>, |
| 30 |
{ |
| 31 |
#[inline(always)] |
| 32 |
pub(crate) fn new(parser: P, name: D) -> Self { |
| 33 |
Self { |
| 34 |
parser, |
| 35 |
name, |
| 36 |
call_count: 0, |
| 37 |
i: Default::default(), |
| 38 |
o: Default::default(), |
| 39 |
e: Default::default(), |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl<P, D, I, O, E> Parser<I, O, E> for Trace<P, D, I, O, E> |
| 45 |
where |
| 46 |
P: Parser<I, O, E>, |
| 47 |
I: Stream, |
| 48 |
D: std::fmt::Display, |
| 49 |
E: ParserError<I>, |
| 50 |
{ |
| 51 |
#[inline] |
| 52 |
fn parse_next(&mut self, i: &mut I) -> Result<O, E> { |
| 53 |
let depth = Depth::new(); |
| 54 |
let original = i.checkpoint(); |
| 55 |
start(*depth, &self.name, self.call_count, i); |
| 56 |
|
| 57 |
let res = self.parser.parse_next(i); |
| 58 |
|
| 59 |
let consumed = i.offset_from(&original); |
| 60 |
let severity = Severity::with_result(&res); |
| 61 |
end(*depth, &self.name, self.call_count, consumed, severity); |
| 62 |
self.call_count += 1; |
| 63 |
|
| 64 |
res |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
pub(crate) struct Depth { |
| 69 |
depth: usize, |
| 70 |
inc: bool, |
| 71 |
} |
| 72 |
|
| 73 |
impl Depth { |
| 74 |
pub(crate) fn new() -> Self { |
| 75 |
let depth = DEPTH.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 76 |
let inc = true; |
| 77 |
Self { depth, inc } |
| 78 |
} |
| 79 |
|
| 80 |
pub(crate) fn existing() -> Self { |
| 81 |
let depth = DEPTH.load(std::sync::atomic::Ordering::SeqCst); |
| 82 |
let inc = false; |
| 83 |
Self { depth, inc } |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl Drop for Depth { |
| 88 |
fn drop(&mut self) { |
| 89 |
if self.inc { |
| 90 |
let _ = DEPTH.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl AsRef<usize> for Depth { |
| 96 |
#[inline(always)] |
| 97 |
fn as_ref(&self) -> &usize { |
| 98 |
&self.depth |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl crate::lib::std::ops::Deref for Depth { |
| 103 |
type Target = usize; |
| 104 |
|
| 105 |
#[inline(always)] |
| 106 |
fn deref(&self) -> &Self::Target { |
| 107 |
&self.depth |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static DEPTH: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 112 |
|
| 113 |
pub(crate) enum Severity { |
| 114 |
Success, |
| 115 |
Backtrack, |
| 116 |
Cut, |
| 117 |
Incomplete, |
| 118 |
} |
| 119 |
|
| 120 |
impl Severity { |
| 121 |
pub(crate) fn with_result<T, I: Stream, E: ParserError<I>>(result: &Result<T, E>) -> Self { |
| 122 |
match result { |
| 123 |
Ok(_) => Self::Success, |
| 124 |
Err(e) if e.is_backtrack() => Self::Backtrack, |
| 125 |
Err(e) if e.is_incomplete() => Self::Incomplete, |
| 126 |
_ => Self::Cut, |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
pub(crate) fn start<I: Stream>( |
| 132 |
depth: usize, |
| 133 |
name: &dyn crate::lib::std::fmt::Display, |
| 134 |
count: usize, |
| 135 |
input: &I, |
| 136 |
) { |
| 137 |
let gutter_style = anstyle::Style::new().bold(); |
| 138 |
let input_style = anstyle::Style::new().underline(); |
| 139 |
let eof_style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Cyan.into())); |
| 140 |
|
| 141 |
let (call_width, input_width) = column_widths(); |
| 142 |
|
| 143 |
let count = if 0 < count { |
| 144 |
format!(":{count}") |
| 145 |
} else { |
| 146 |
"".to_owned() |
| 147 |
}; |
| 148 |
let call_column = format!("{:depth$}> {name}{count}", ""); |
| 149 |
|
| 150 |
// The debug version of `slice` might be wider, either due to rendering one byte as two nibbles or |
| 151 |
// escaping in strings. |
| 152 |
let mut debug_slice = format!("{:#?}", input.raw()); |
| 153 |
let (debug_slice, eof) = if let Some(debug_offset) = debug_slice |
| 154 |
.char_indices() |
| 155 |
.enumerate() |
| 156 |
.find_map(|(pos, (offset, _))| (input_width <= pos).then_some(offset)) |
| 157 |
{ |
| 158 |
debug_slice.truncate(debug_offset); |
| 159 |
let eof = ""; |
| 160 |
(debug_slice, eof) |
| 161 |
} else { |
| 162 |
let eof = if debug_slice.chars().count() < input_width { |
| 163 |
"∅" |
| 164 |
} else { |
| 165 |
"" |
| 166 |
}; |
| 167 |
(debug_slice, eof) |
| 168 |
}; |
| 169 |
|
| 170 |
let writer = anstream::stderr(); |
| 171 |
let mut writer = writer.lock(); |
| 172 |
let _ = writeln!( |
| 173 |
writer, |
| 174 |
"{call_column:call_width$} {gutter_style}|{gutter_reset} {input_style}{debug_slice}{input_reset}{eof_style}{eof}{eof_reset}", |
| 175 |
gutter_style=gutter_style.render(), |
| 176 |
gutter_reset=gutter_style.render_reset(), |
| 177 |
input_style=input_style.render(), |
| 178 |
input_reset=input_style.render_reset(), |
| 179 |
eof_style=eof_style.render(), |
| 180 |
eof_reset=eof_style.render_reset(), |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
pub(crate) fn end( |
| 185 |
depth: usize, |
| 186 |
name: &dyn crate::lib::std::fmt::Display, |
| 187 |
count: usize, |
| 188 |
consumed: usize, |
| 189 |
severity: Severity, |
| 190 |
) { |
| 191 |
let gutter_style = anstyle::Style::new().bold(); |
| 192 |
|
| 193 |
let (call_width, _) = column_widths(); |
| 194 |
|
| 195 |
let count = if 0 < count { |
| 196 |
format!(":{count}") |
| 197 |
} else { |
| 198 |
"".to_owned() |
| 199 |
}; |
| 200 |
let call_column = format!("{:depth$}< {name}{count}", ""); |
| 201 |
|
| 202 |
let (status_style, status) = match severity { |
| 203 |
Severity::Success => { |
| 204 |
let style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Green.into())); |
| 205 |
let status = format!("+{consumed}"); |
| 206 |
(style, status) |
| 207 |
} |
| 208 |
Severity::Backtrack => ( |
| 209 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Yellow.into())), |
| 210 |
"backtrack".to_owned(), |
| 211 |
), |
| 212 |
Severity::Cut => ( |
| 213 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())), |
| 214 |
"cut".to_owned(), |
| 215 |
), |
| 216 |
Severity::Incomplete => ( |
| 217 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())), |
| 218 |
"incomplete".to_owned(), |
| 219 |
), |
| 220 |
}; |
| 221 |
|
| 222 |
let writer = anstream::stderr(); |
| 223 |
let mut writer = writer.lock(); |
| 224 |
let _ = writeln!( |
| 225 |
writer, |
| 226 |
"{status_style}{call_column:call_width$}{status_reset} {gutter_style}|{gutter_reset} {status_style}{status}{status_reset}", |
| 227 |
gutter_style=gutter_style.render(), |
| 228 |
gutter_reset=gutter_style.render_reset(), |
| 229 |
status_style=status_style.render(), |
| 230 |
status_reset=status_style.render_reset(), |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
pub(crate) fn result(depth: usize, name: &dyn crate::lib::std::fmt::Display, severity: Severity) { |
| 235 |
let gutter_style = anstyle::Style::new().bold(); |
| 236 |
|
| 237 |
let (call_width, _) = column_widths(); |
| 238 |
|
| 239 |
let call_column = format!("{:depth$}| {name}", ""); |
| 240 |
|
| 241 |
let (status_style, status) = match severity { |
| 242 |
Severity::Success => ( |
| 243 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Green.into())), |
| 244 |
"", |
| 245 |
), |
| 246 |
Severity::Backtrack => ( |
| 247 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Yellow.into())), |
| 248 |
"backtrack", |
| 249 |
), |
| 250 |
Severity::Cut => ( |
| 251 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())), |
| 252 |
"cut", |
| 253 |
), |
| 254 |
Severity::Incomplete => ( |
| 255 |
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())), |
| 256 |
"incomplete", |
| 257 |
), |
| 258 |
}; |
| 259 |
|
| 260 |
let writer = anstream::stderr(); |
| 261 |
let mut writer = writer.lock(); |
| 262 |
let _ = writeln!( |
| 263 |
writer, |
| 264 |
"{status_style}{call_column:call_width$}{status_reset} {gutter_style}|{gutter_reset} {status_style}{status}{status_reset}", |
| 265 |
gutter_style=gutter_style.render(), |
| 266 |
gutter_reset=gutter_style.render_reset(), |
| 267 |
status_style=status_style.render(), |
| 268 |
status_reset=status_style.render_reset(), |
| 269 |
); |
| 270 |
} |
| 271 |
|
| 272 |
fn column_widths() -> (usize, usize) { |
| 273 |
let term_width = term_width(); |
| 274 |
|
| 275 |
let min_call_width = 40; |
| 276 |
let min_input_width = 20; |
| 277 |
let decor_width = 3; |
| 278 |
let extra_width = term_width |
| 279 |
.checked_sub(min_call_width + min_input_width + decor_width) |
| 280 |
.unwrap_or_default(); |
| 281 |
let call_width = min_call_width + 2 * extra_width / 3; |
| 282 |
let input_width = min_input_width + extra_width / 3; |
| 283 |
|
| 284 |
(call_width, input_width) |
| 285 |
} |
| 286 |
|
| 287 |
fn term_width() -> usize { |
| 288 |
columns_env().or_else(query_width).unwrap_or(80) |
| 289 |
} |
| 290 |
|
| 291 |
fn query_width() -> Option<usize> { |
| 292 |
use is_terminal::IsTerminal; |
| 293 |
if std::io::stderr().is_terminal() { |
| 294 |
terminal_size::terminal_size().map(|(w, _h)| w.0.into()) |
| 295 |
} else { |
| 296 |
None |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
fn columns_env() -> Option<usize> { |
| 301 |
std::env::var("COLUMNS") |
| 302 |
.ok() |
| 303 |
.and_then(|c| c.parse::<usize>().ok()) |
| 304 |
} |