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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// pest. The Elegant Parser
// Copyright (c) 2018-2022 Dragoș Tiselice, Tomas Tauber
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! # pest debugger
//!
//! This crate contains definitions for the debugger.
//! A sample CLI-based debugger is available in `main.rs`.
//! Other debugger frontends can be implemented using this
//! crate's `DebuggerContext`:
//!
//! ```
//! use pest_debugger::DebuggerContext;
//! use std::sync::mpsc::sync_channel;
//! let mut context = DebuggerContext::default();
//!
//! context
//! .load_grammar_direct(
//!     "testgrammar",
//!     r#"alpha = { 'a'..'z' | 'A'..'Z' }
//! digit = { '0'..'9' }
//!
//! ident = { !digit ~ (alpha | digit)+ }
//!
//! ident_list = _{ ident ~ (" " ~ ident)* }"#,
//! ).expect("Error: failed to load grammar");
//! context.load_input_direct("test test2".to_owned());
//!
//! let (sender, receiver) = sync_channel(1);
//!
//! context.add_breakpoint("ident".to_owned());
//! for b in context.list_breakpoints().iter() {
//!     println!("Breakpoint: {}", b);
//! }
//! context
//! .run("ident_list", sender)
//! .expect("Error: failed to run rule");
//!
//! let event = receiver.recv().expect("Error: failed to receive event");
//! println!("Received a debugger event: {:?}", event);
//!
//! context.cont().expect("Error: failed to continue");
//!
//! let event = receiver.recv().expect("Error: failed to receive event");
//! println!("Received a debugger event: {:?}", event);
//! ```
//! ## Current Limitations
//! - relies on OS threads instead of stack-full generators
//! - only shows position from the `ParserState` when it reaches a breakpoint
//! - no way to run another rule from a breakpoint, only from the start
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg",
    html_favicon_url = "https://raw.githubusercontent.com/pest-parser/pest/master/pest-logo.svg"
)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
use std::{
    collections::HashSet,
    fs::File,
    io::{self, Read},
    path::PathBuf,
    sync::{
        atomic::{AtomicBool, Ordering},
        mpsc::SyncSender as Sender,
        Arc, Mutex,
    },
    thread::{self, JoinHandle},
};

use pest::{error::Error, Position};
use pest_meta::{
    optimizer::OptimizedRule,
    parse_and_optimize,
    parser::{rename_meta_rule, Rule},
};
use pest_vm::Vm;

/// Possible errors that can occur in the debugger context.
#[derive(Debug, thiserror::Error)]
pub enum DebuggerError {
    /// Errors from opening files etc.
    #[error("I/O error: {0}")]
    Io(#[from] io::Error),
    /// When a filename can't be extracted from a grammar path.
    #[error("Missing filename")]
    MissingFilename,
    /// Running a debugger requires a grammar to be provided.
    #[error("Open grammar first")]
    GrammarNotOpened,
    /// Running a debugger requires a parsing input to be provided.
    #[error("Open input first")]
    InputNotOpened,
    /// Continuing a debugger session requires starting a session by running a rule.
    #[error("Run rule first")]
    RunRuleFirst,
    /// Parsing finished (i.e. cannot continue the session).
    #[error("End-of-input reached")]
    EofReached,
    /// Can't create a `Position` in a given input.
    #[error("Invalid position: {0}")]
    InvalidPosition(usize),
    /// The provided grammar is invalid.
    /// The first element contains a formatted error message.
    /// The second element (`Vec`) contains the errors.
    #[error("Grammar error: {0}")]
    IncorrectGrammar(String, Vec<Error<Rule>>),
    /// When restarting a session, the previous session
    /// seem to have panicked.
    #[error("Previous parsing execution panic: {0}")]
    PreviousRunPanic(String),
}

/// Events that are sent from the debugger.
#[derive(Debug, PartialEq, Eq)]
pub enum DebuggerEvent {
    /// A breakpoint encountered.
    /// The first element is the rule name.
    /// The second element is the position.
    Breakpoint(String, usize),
    /// The end of the input has been reached.
    Eof,
    /// A parsing error encountered.
    Error(String),
}

/// Debugger for pest grammars.
pub struct DebuggerContext {
    handle: Option<JoinHandle<()>>,
    is_done: Arc<AtomicBool>,
    grammar: Option<Vec<OptimizedRule>>,
    input: Option<String>,
    breakpoints: Arc<Mutex<HashSet<String>>>,
}

const POISONED_LOCK_PANIC: &str = "poisoned lock";
const CHANNEL_CLOSED_PANIC: &str = "channel closed";

impl DebuggerContext {
    fn file_to_string(path: &PathBuf) -> Result<String, DebuggerError> {
        let mut file = File::open(path)?;

        let mut string = String::new();
        file.read_to_string(&mut string)?;

        Ok(string)
    }

    /// Loads a grammar from a file.
    pub fn load_grammar(&mut self, path: &PathBuf) -> Result<(), DebuggerError> {
        let grammar = DebuggerContext::file_to_string(path)?;

        let file_name = path
            .file_name()
            .map(|string| string.to_string_lossy().into_owned())
            .ok_or(DebuggerError::MissingFilename)?;

        self.grammar = Some(DebuggerContext::parse_grammar(&file_name, &grammar)?);

        Ok(())
    }

    /// Loads a grammar from a string.
    pub fn load_grammar_direct(
        &mut self,
        grammar_name: &str,
        grammar: &str,
    ) -> Result<(), DebuggerError> {
        self.grammar = Some(DebuggerContext::parse_grammar(grammar_name, grammar)?);

        Ok(())
    }

    /// Loads a parsing input from a file.
    pub fn load_input(&mut self, path: &PathBuf) -> Result<(), DebuggerError> {
        let input = DebuggerContext::file_to_string(path)?;

        self.input = Some(input);

        Ok(())
    }

    /// Loads a parsing input from a string.
    pub fn load_input_direct(&mut self, input: String) {
        self.input = Some(input);
    }

    /// Adds all grammar rules as breakpoints.
    /// This is useful for stepping through the entire parsing process.
    /// It returns an error if the grammar hasn't been loaded yet.
    pub fn add_all_rules_breakpoints(&mut self) -> Result<(), DebuggerError> {
        let ast = self
            .grammar
            .as_ref()
            .ok_or(DebuggerError::GrammarNotOpened)?;
        let mut breakpoints = self.breakpoints.lock().expect(POISONED_LOCK_PANIC);
        for rule in ast {
            breakpoints.insert(rule.name.clone());
        }

        Ok(())
    }

    /// Adds a rule to breakpoints.
    pub fn add_breakpoint(&mut self, rule: String) {
        let mut breakpoints = self.breakpoints.lock().expect(POISONED_LOCK_PANIC);

        breakpoints.insert(rule);
    }

    /// Removes a rule from breakpoints.
    pub fn delete_breakpoint(&mut self, rule: &str) {
        let mut breakpoints = self.breakpoints.lock().expect(POISONED_LOCK_PANIC);

        breakpoints.remove(rule);
    }

    /// Removes all breakpoints.
    pub fn delete_all_breakpoints(&mut self) {
        let mut breakpoints = self.breakpoints.lock().expect(POISONED_LOCK_PANIC);

        breakpoints.clear();
    }

    /// Returns a list of all breakpoints.
    pub fn list_breakpoints(&self) -> Vec<String> {
        let breakpoints = self.breakpoints.lock().expect(POISONED_LOCK_PANIC);
        let mut breakpoints: Vec<_> = breakpoints.iter().map(ToOwned::to_owned).collect();
        breakpoints.sort();
        breakpoints
    }

    fn handle(
        &self,
        ast: Vec<OptimizedRule>,
        rule: String,
        input: String,
        sender: Sender<DebuggerEvent>,
    ) -> JoinHandle<()> {
        let breakpoints = Arc::clone(&self.breakpoints);
        let is_done = Arc::clone(&self.is_done);
        let is_done_signal = Arc::clone(&self.is_done);

        let rsender = sender.clone();
        thread::spawn(move || {
            let vm = Vm::new_with_listener(
                ast,
                Box::new(move |rule, pos| {
                    if is_done_signal.load(Ordering::SeqCst) {
                        return true;
                    }
                    let lock = breakpoints.lock().expect(POISONED_LOCK_PANIC);

                    if lock.contains(&rule) {
                        rsender
                            .send(DebuggerEvent::Breakpoint(rule, pos.pos()))
                            .expect(CHANNEL_CLOSED_PANIC);

                        thread::park();
                    }
                    false
                }),
            );

            match vm.parse(&rule, &input) {
                Ok(_) => sender.send(DebuggerEvent::Eof).expect(CHANNEL_CLOSED_PANIC),
                Err(error) => sender
                    .send(DebuggerEvent::Error(error.to_string()))
                    .expect(CHANNEL_CLOSED_PANIC),
            };

            is_done.store(true, Ordering::SeqCst);
        })
    }

    fn parse_grammar(file_name: &str, grammar: &str) -> Result<Vec<OptimizedRule>, DebuggerError> {
        match parse_and_optimize(grammar) {
            Ok((_, ast)) => Ok(ast),
            Err(errors) => {
                let msg = format!(
                    "error parsing {:?}\n\n{}",
                    file_name,
                    errors
                        .iter()
                        .cloned()
                        .map(|error| format!("{}", error.renamed_rules(rename_meta_rule)))
                        .collect::<Vec<_>>()
                        .join("\n")
                );
                Err(DebuggerError::IncorrectGrammar(msg, errors))
            }
        }
    }

    /// Starts a debugger session: runs a rule on an input and stops at breakpoints.
    /// When the debugger is stopped, an event is sent to the channel using `sender`.
    /// The debugger can be resumed by calling `cont`.
    /// This naturally returns errors if the grammar or input haven't been loaded yet etc.
    pub fn run(&mut self, rule: &str, sender: Sender<DebuggerEvent>) -> Result<(), DebuggerError> {
        if let Some(handle) = self.handle.take() {
            if !(self.is_done.load(Ordering::Relaxed)) {
                self.is_done.store(true, Ordering::SeqCst);
                handle.thread().unpark();
            }
            handle
                .join()
                .map_err(|e| DebuggerError::PreviousRunPanic(format!("{:?}", e)))?;
        }

        self.is_done.store(false, Ordering::SeqCst);
        let ast = self
            .grammar
            .as_ref()
            .ok_or(DebuggerError::GrammarNotOpened)?;
        match self.input {
            Some(ref input) => {
                let rule = rule.to_owned();
                let input = input.clone();

                self.handle = Some(self.handle(ast.clone(), rule, input, sender));
                Ok(())
            }
            None => Err(DebuggerError::InputNotOpened),
        }
    }

    /// Continue the debugger session from the breakpoint.
    /// It returns an error if the session finished or wasn't started yet.
    pub fn cont(&self) -> Result<(), DebuggerError> {
        if self.is_done.load(Ordering::SeqCst) {
            return Err(DebuggerError::EofReached);
        }

        match self.handle {
            Some(ref handle) => {
                handle.thread().unpark();
                Ok(())
            }
            None => Err(DebuggerError::RunRuleFirst),
        }
    }

    /// Returns a `Position` from the loaded input.
    pub fn get_position(&self, pos: usize) -> Result<Position<'_>, DebuggerError> {
        match self.input {
            Some(ref input) => Position::new(input, pos).ok_or(DebuggerError::InvalidPosition(pos)),
            None => Err(DebuggerError::InputNotOpened),
        }
    }
}

impl Default for DebuggerContext {
    fn default() -> Self {
        Self {
            handle: None,
            is_done: Arc::new(AtomicBool::new(false)),
            grammar: None,
            input: None,
            breakpoints: Arc::new(Mutex::new(HashSet::new())),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::sync::mpsc::sync_channel;

    fn get_test_context() -> DebuggerContext {
        let mut context = DebuggerContext::default();

        context
            .load_grammar_direct(
                "testgrammar",
                r#"alpha = { 'a'..'z' | 'A'..'Z' }
            digit = { '0'..'9' }
            
            ident = { !digit ~ (alpha | digit)+ }
            
            ident_list = _{ ident ~ (" " ~ ident)* }"#,
            )
            .expect("Error: failed to load grammar");
        context.load_input_direct("test test2".to_owned());
        context
    }

    #[test]
    fn test_full_flow() {
        let mut context = get_test_context();

        let (sender, receiver) = sync_channel(1);

        assert_eq!(context.list_breakpoints().len(), 0);
        context.add_breakpoint("ident".to_owned());
        assert_eq!(context.list_breakpoints().len(), 1);
        context
            .run("ident_list", sender)
            .expect("Error: failed to run rule");

        let event = receiver.recv().expect("Error: failed to receive event");
        assert_eq!(event, DebuggerEvent::Breakpoint("ident".to_owned(), 0));

        context.cont().expect("Error: failed to continue");

        let event = receiver.recv().expect("Error: failed to receive event");
        assert_eq!(event, DebuggerEvent::Breakpoint("ident".to_owned(), 5));
        context.cont().expect("Error: failed to continue");
        let event = receiver.recv().expect("Error: failed to receive event");

        assert_eq!(event, DebuggerEvent::Eof);
        context
            .add_all_rules_breakpoints()
            .expect("grammar is loaded");
        assert_eq!(context.list_breakpoints().len(), 4);
        context.delete_breakpoint("ident");
        assert_eq!(context.list_breakpoints().len(), 3);
        context.delete_all_breakpoints();
        assert_eq!(context.list_breakpoints().len(), 0);
    }

    #[test]
    fn test_restart() {
        let mut context = get_test_context();

        let (sender, receiver) = sync_channel(1);

        assert_eq!(context.list_breakpoints().len(), 0);
        context.add_breakpoint("ident".to_owned());
        assert_eq!(context.list_breakpoints().len(), 1);
        context
            .run("ident_list", sender)
            .expect("Error: failed to run rule");

        let event = receiver.recv().expect("Error: failed to receive event");
        assert_eq!(event, DebuggerEvent::Breakpoint("ident".to_owned(), 0));
        let (sender2, receiver2) = sync_channel(1);

        context
            .run("ident_list", sender2)
            .expect("Error: failed to run rule");
        let event = receiver2.recv().expect("Error: failed to receive event");
        assert_eq!(event, DebuggerEvent::Breakpoint("ident".to_owned(), 0));
    }

    #[test]
    pub fn test_errors() {
        let mut context = DebuggerContext::default();

        assert!(context.load_input(&PathBuf::from(".")).is_err());
        let pest_readme = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../README.md"));
        let pest_grammar = PathBuf::from(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../meta/src/grammar.pest"
        ));

        assert!(context.load_grammar(&pest_readme).is_err());
        assert!(context.add_all_rules_breakpoints().is_err());
        assert!(context.cont().is_err());
        assert!(context.run("rule", sync_channel(1).0).is_err());
        assert!(context.load_grammar(&pest_grammar).is_ok());
        assert!(context.run("rule", sync_channel(1).0).is_err());
        assert!(context.get_position(0).is_err());
        context.load_input_direct("".to_owned());
        assert!(context.get_position(0).is_ok());
        assert!(context.get_position(1).is_err());
        assert!(context.load_input(&pest_grammar).is_ok());
        let (sender, _receiver) = sync_channel(1);
        assert!(context.run("ANY", sender).is_ok());
        while context.cont().is_ok() {}
        assert!(context.cont().is_err());
    }
}