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
// pest. Elegant, efficient grammars
// Copyright (C) 2016  Dragoș Tiselice
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use super::Input;

/// A `trait` that defines a parser.
pub trait Parser<'a, T: Input<'a>> {
    type Rule;
    type Token;

    fn input(&self) -> &T;

    fn input_mut(&mut self) -> &mut T;

    /// Returns whether a `Parser` has reached its end.
    fn end(&self) -> bool;

    /// Returns whether a `Parser` has matched end-of-input.
    fn eoi_matched(&self) -> bool;

    /// Reset a `Parser`.
    fn reset(&mut self);

    /// Returns the queue of all matched `Token`s.
    fn queue(&self) -> &Vec<Self::Token>;

    /// Returns the mutable queue of all matched `Token`s.
    fn queue_mut(&mut self) -> &mut Vec<Self::Token>;

    /// Returns the queue of all matched `(Token, value)`s.
    fn queue_with_captures(&self) -> Vec<(Self::Token, String)>;

    /// Returns the current index within the queue. Used in `process!`.
    fn queue_index(&self) -> usize;

    /// Increments the current index within the queue. Used in `process!`.
    fn inc_queue_index(&self);

    /// Set the current index within the queue. Used in `process!`.
    fn set_queue_index(&self, index: usize);

    /// Skips whitespace and comments.
    fn skip(&mut self);

    /// Returns whether a `Parser` is currently inside an atomic rule.
    fn is_atomic(&self) -> bool;

    /// Sets a `Parser` to atomic rule mode, barring comment & white-space skipping.
    fn set_atomic(&mut self, value: bool);

    /// Keeps track of rule failures. It gets called when a `Rule` fails at `pos`.
    fn track(&mut self, failed: Self::Rule, pos: usize);

    /// Returns the length of the tracked `Rule`s.
    fn tracked_len_pos(&self) -> (usize, usize);

    /// Retuns a `Vec` of all expected `Rule`s at the deepest position where the parsing last
    /// stopped. It only returns leafs from the rule tree. Used for error reporting.
    fn expected(&mut self) -> (Vec<Self::Rule>, usize);

    /// Returns the stack `Vec`.
    fn stack(&self) -> &Vec<String>;

    /// Returns the mutable stack `Vec`.
    fn stack_mut(&mut self) -> &mut Vec<String>;
}