| 1 | //! Building blocks for a recursive descent LL(1) parsing method. |
| 2 | //! |
| 3 | //! The general idea is that a grammar (without left recursion) is translated to a series of |
| 4 | //! conditional and unconditional 'acceptance' methods. |
| 5 | //! |
| 6 | //! For example, assuming we have a parser for integers: |
| 7 | //! |
| 8 | //! sum = integer | integer + sum |
| 9 | //! |
| 10 | //! Can get translated as: (representing a sum as `LinkedList<u32>`): |
| 11 | //! |
| 12 | //! ```ignore |
| 13 | //! impl<T: Parse> Parse for LinkedList<T> { |
| 14 | //! fn parse(stream: &mut CharStream) -> Parsed<LinkedList<T>> { |
| 15 | //! let x = try_nonterminal(stream)?; |
| 16 | //! let mut tail = if is_syntax('+', stream)? { |
| 17 | //! expect_nonterminal(stream)? |
| 18 | //! } else { |
| 19 | //! LinkedList::new() |
| 20 | //! }; |
| 21 | //! tail.push_front(x); |
| 22 | //! |
| 23 | //! make(tail) |
| 24 | //! } |
| 25 | //! } |
| 26 | //! ``` |
| 27 | |
| 28 | /// Type holding a parsed object (or error information if parsing failed) |
| 29 | pub type Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#>; |
| 30 | |
| 31 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 32 | #[cfg_attr(test, derive(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:))] |
| 33 | pub struct Spanrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Span# { |
| 34 | pub startrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Span#start.: (usize, usize), |
| 35 | pub endrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Span#end.: (usize, usize), |
| 36 | } |
| 37 | |
| 38 | #[cfg_attr(test, derive(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:))] |
| 39 | pub enum Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status# { |
| 40 | Fatalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Fatal#(Spanrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Span#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), // not recoverable; stream in inconsistent state |
| 41 | Rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Reject#, // parsing failed by no input consumed |
| 42 | } |
| 43 | |
| 44 | pub fn makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().<T>(value: T) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> { |
| 45 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value) |
| 46 | } |
| 47 | |
| 48 | pub fn rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/reject().<T>() -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> { |
| 49 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#::Rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Reject#) |
| 50 | } |
| 51 | |
| 52 | macro_rules! unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable! { |
| 53 | (pos=$pos:expr, $stream:ident, $($str:expr),*) => { |
| 54 | return Err(crate::sudoers::basic_parser::Status::Fatal(Span { start: $pos, end: CharStream::get_pos($stream)}, format![$($str),*])) |
| 55 | }; |
| 56 | ($stream:ident, $($str:expr),*) => {{ |
| 57 | let pos = CharStream::get_pos($stream); |
| 58 | return Err(crate::sudoers::basic_parser::Status::Fatal(Span { start: pos, end: pos }, format![$($str),*])) |
| 59 | }}; |
| 60 | ($($str:expr),*) => { |
| 61 | return Err(crate::basic_parser::Status::Fatal(Default::default(), format![$($str),*])) |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | pub(superrust-analyzer cargo sudo-rs 0.2.14 sudoers/) use unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!; |
| 66 | |
| 67 | /// This recovers from a failed parsing. |
| 68 | pub fn mayberust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/maybe().<T>(status: Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T>) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>> { |
| 69 | match status { |
| 70 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(x) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x)), |
| 71 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#::Rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Reject#) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), |
| 72 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub use superrust-analyzer cargo sudo-rs 0.2.14 sudoers/::char_streamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/::CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#; |
| 77 | |
| 78 | /// All implementations of the Parse trait must satisfy this contract: |
| 79 | /// If the `parse` method of this trait returns None, the iterator is not advanced; otherwise it is |
| 80 | /// advanced beyond the accepted part of the input. i.e. if some input is consumed the method |
| 81 | /// *MUST* be producing a `Some` value. |
| 82 | pub trait Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# { |
| 83 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Self> |
| 84 | where |
| 85 | Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#; |
| 86 | } |
| 87 | |
| 88 | /// Structures representing whitespace (trailing whitespace can contain comments) |
| 89 | #[cfg_attr(test, derive(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:))] |
| 90 | struct LeadingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/LeadingWhitespace#; |
| 91 | |
| 92 | #[cfg_attr(test, derive(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:))] |
| 93 | struct TrailingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/TrailingWhitespace#; |
| 94 | |
| 95 | #[cfg_attr(test, derive(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:))] |
| 96 | struct Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment#; |
| 97 | |
| 98 | /// Accept zero or more whitespace characters; fails if the whitespace is not "leading" to something |
| 99 | /// (which can be used to detect end-of-input). |
| 100 | impl Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# for LeadingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/LeadingWhitespace# { |
| 101 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[LeadingWhitespace][Parse]parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[LeadingWhitespace][Parse]> { |
| 102 | let eat_space = |stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#| stream.next_ifrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]next_if().(|c| "\t ".containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(c)); |
| 103 | while eat_space(stream).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {} |
| 104 | |
| 105 | if stream.peekrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]peek().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 106 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(LeadingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/LeadingWhitespace# {}) |
| 107 | } else { |
| 108 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "superfluous whitespace") |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Accept zero or more whitespace characters; since this accepts zero characters, it |
| 114 | /// always succeeds (unless some serious error occurs). This parser also accepts comments, |
| 115 | /// since those can form part of trailing white space. |
| 116 | impl Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# for TrailingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/TrailingWhitespace# { |
| 117 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[TrailingWhitespace][Parse]parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[TrailingWhitespace][Parse]> { |
| 118 | loop { |
| 119 | let _ = LeadingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/LeadingWhitespace#::parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[LeadingWhitespace][Parse]parse().(stream); // don't propagate any errors |
| 120 | |
| 121 | // line continuations |
| 122 | if stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().('\\') { |
| 123 | // do the equivalent of expect_syntax('\n', stream)?, without recursion |
| 124 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().('\n') { |
| 125 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "stray escape sequence") |
| 126 | } |
| 127 | } else { |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(TrailingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/TrailingWhitespace# {}) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Parses a comment |
| 137 | impl Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# for Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment# { |
| 138 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[Comment][Parse]parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[Comment][Parse]> { |
| 139 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().('#') { |
| 140 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#::Rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Reject#); |
| 141 | } |
| 142 | stream.skip_to_newlinerust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]skip_to_newline().(); |
| 143 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment# {}) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | pub(superrust-analyzer cargo sudo-rs 0.2.14 sudoers/) fn skip_trailing_whitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/skip_trailing_whitespace().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<()> { |
| 148 | TrailingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/TrailingWhitespace#::parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[TrailingWhitespace][Parse]parse().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 149 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(()) |
| 150 | } |
| 151 | |
| 152 | /// Adheres to the contract of the [Parse] trait, accepts one character and consumes trailing whitespace. |
| 153 | pub fn try_syntaxrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_syntax().(syntax: char, stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<()> { |
| 154 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().(syntax) { |
| 155 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#::Rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Reject#); |
| 156 | } |
| 157 | skip_trailing_whitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/skip_trailing_whitespace().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 158 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(()) |
| 159 | } |
| 160 | |
| 161 | /// Similar to [try_syntax], but aborts parsing if the expected character is not found. |
| 162 | pub fn expect_syntaxrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_syntax().(syntax: char, stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<()> { |
| 163 | if try_syntaxrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_syntax().(syntax, stream).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 164 | let str = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = stream.peekrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]peek().() { |
| 165 | c.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 166 | } else { |
| 167 | "EOF".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 168 | }; |
| 169 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "expecting '{syntax}' but found '{str}'") |
| 170 | } |
| 171 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(()) |
| 172 | } |
| 173 | |
| 174 | /// Convenience function: usually try_syntax is called as a test criterion; if this returns true, the input was consumed. |
| 175 | pub fn is_syntaxrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/is_syntax().(syntax: char, stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<bool> { |
| 176 | let result = mayberust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/maybe().(try_syntaxrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_syntax().(syntax, stream))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 177 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(result.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()) |
| 178 | } |
| 179 | |
| 180 | /// Interface for working with types that implement the [Parse] trait; this allows parsing to use |
| 181 | /// type inference. Use this instead of calling [Parse::parse] directly. |
| 182 | pub fn try_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_nonterminal().<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#>(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> { |
| 183 | let result = T::parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#parse().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 184 | skip_trailing_whitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/skip_trailing_whitespace().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 185 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(result) |
| 186 | } |
| 187 | |
| 188 | /// Interface for working with types that implement the [Parse] trait; this expects to parse |
| 189 | /// the given type or gives a fatal parse error if this did not succeed. |
| 190 | use superrust-analyzer cargo sudo-rs 0.2.14 sudoers/::ast_namesrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/::UserFriendlyrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/UserFriendly#; |
| 191 | |
| 192 | pub fn expect_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_nonterminal().<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# + UserFriendlyrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/UserFriendly#>(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> { |
| 193 | let begin_pos = stream.get_posrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]get_pos().(); |
| 194 | match try_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_nonterminal().(stream) { |
| 195 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Statusrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#::Rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Status#Reject#) => { |
| 196 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(pos = begin_pos, stream, "expected {}", T::DESCRIPTIONrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/UserFriendly#DESCRIPTION.) |
| 197 | } |
| 198 | result => result, |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Something that implements the Token trait is a token (i.e. a string of characters defined by a |
| 203 | /// maximum length, character classes, and possible escaping). The class for the first character of |
| 204 | /// the token can be different than that of the rest. |
| 205 | pub trait Tokenrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 206 | const MAX_LENrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#MAX_LEN.: usize = 255; |
| 207 | |
| 208 | fn constructrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#construct().(s: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>; |
| 209 | |
| 210 | fn acceptrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#accept().(c: char) -> bool; |
| 211 | fn accept_1strust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#accept_1st().(c: char) -> bool { |
| 212 | Self::acceptrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#accept().(c) |
| 213 | } |
| 214 | |
| 215 | const ALLOW_ESCAPErust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#ALLOW_ESCAPE.: bool = false; |
| 216 | fn escapedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#escaped().(_: char) -> bool { |
| 217 | false |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Implementation of the [Parse] trait for anything that implements [Token] |
| 222 | impl<T: Tokenrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#> Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# for T { |
| 223 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[T][Parse]parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[T][Parse]> { |
| 224 | fn accept_escapedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/accept_escaped().<T: Tokenrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#>( |
| 225 | pred: fn(char) -> bool, |
| 226 | stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#, |
| 227 | ) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<char> { |
| 228 | const ESCAPErust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/ESCAPE.: char = '\\'; |
| 229 | if T::ALLOW_ESCAPErust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#ALLOW_ESCAPE. && stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().(ESCAPErust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/ESCAPE.) { |
| 230 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = stream.next_ifrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]next_if().(T::escapedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#escaped().) { |
| 231 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(c) |
| 232 | } else if pred(ESCAPErust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/ESCAPE.) { |
| 233 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ESCAPErust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/ESCAPE.) |
| 234 | } else if stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().('\n') { |
| 235 | if pred(' ') { |
| 236 | // escape + newline = line continuation (whitespace) |
| 237 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(' ') |
| 238 | } else { |
| 239 | rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/reject().() |
| 240 | } |
| 241 | } else { |
| 242 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "illegal escape sequence") |
| 243 | } |
| 244 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = stream.next_ifrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]next_if().(pred) { |
| 245 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(c) |
| 246 | } else { |
| 247 | rejectrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/reject().() |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | let start_pos = stream.get_posrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]get_pos().(); |
| 252 | let mut str = accept_escapedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/accept_escaped().::<T>(T::accept_1strust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#accept_1st()., stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 253 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = mayberust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/maybe().(accept_escapedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/accept_escaped().::<T>(T::acceptrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#accept()., stream))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 254 | if str.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().T::MAX_LENrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#MAX_LEN. { |
| 255 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "token exceeds maximum length") |
| 256 | } |
| 257 | str.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c) |
| 258 | } |
| 259 | |
| 260 | match T::constructrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token#construct().(str) { |
| 261 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result) => makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(result), |
| 262 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(msg) => unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(pos = start_pos, stream, "{msg}"), |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /// Parser for `Option<T>` (this can be used to make the code more readable) |
| 268 | impl<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#> Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# for Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T> { |
| 269 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[`Option<T>`][Parse]parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[`Option<T>`][Parse]> { |
| 270 | mayberust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/maybe().(T::parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#parse().(stream)) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /// Parsing method for lists of items separated by a given character; this adheres to the contract of the [Parse] trait. |
| 275 | pub(superrust-analyzer cargo sudo-rs 0.2.14 sudoers/) fn parse_listrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_list().<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# + UserFriendlyrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/UserFriendly#>( |
| 276 | sep_by: char, |
| 277 | max: usize, |
| 278 | stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#, |
| 279 | ) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>> { |
| 280 | let mut elems = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 281 | elems.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(try_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_nonterminal().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 282 | while mayberust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/maybe().(try_syntaxrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/try_syntax().(sep_by, stream))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 283 | if elems.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().max { |
| 284 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "too many items in list") |
| 285 | } |
| 286 | elems.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(expect_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_nonterminal().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 287 | } |
| 288 | |
| 289 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(elems) |
| 290 | } |
| 291 | |
| 292 | /// Types that implement the Many trait can be parsed multiple tokens into a `Vec<T>`; they are |
| 293 | /// separated by `SEP`. There should also be a limit on the number of items. |
| 294 | pub trait Manyrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many# { |
| 295 | const SEPrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many#SEP.: char = ','; |
| 296 | const LIMITrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many#LIMIT.: usize = 127; |
| 297 | } |
| 298 | |
| 299 | /// Generic implementation for parsing multiple items of a type `T` that implements the [Parse] and |
| 300 | /// [Many] traits. |
| 301 | impl<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# + Manyrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many# + UserFriendlyrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/UserFriendly#> Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> { |
| 302 | fn parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[`Vec<T>`][Parse]parse().(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[`Vec<T>`][Parse]> { |
| 303 | parse_listrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_list().(T::SEPrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many#SEP., T::LIMITrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many#LIMIT., stream) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /// Entry point utility function; parse a `Vec<T>` but with fatal error recovery per line |
| 308 | pub fn parse_linesrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_lines().<T>(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T>> |
| 309 | where |
| 310 | T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse# + UserFriendlyrust-analyzer cargo sudo-rs 0.2.14 sudoers/ast_names/UserFriendly#, |
| 311 | { |
| 312 | let mut result = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 313 | |
| 314 | // this will terminate; if the inner accept_if is an error, either a character will be consumed |
| 315 | // by the second accept_if (making progress), or the end of the stream will have been reacherd |
| 316 | // (which will cause the next iteration to fall through) |
| 317 | |
| 318 | while LeadingWhitespacerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/LeadingWhitespace#::parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[LeadingWhitespace][Parse]parse().(stream).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() { |
| 319 | let item = expect_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_nonterminal().(stream); |
| 320 | let parsed_item_ok = item.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().(); |
| 321 | result.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(item); |
| 322 | |
| 323 | let _ = mayberust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/maybe().(Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment#::parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/impl#[Comment][Parse]parse().(stream)); |
| 324 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().stream.eat_charrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]eat_char().('\n') { |
| 325 | if parsed_item_ok { |
| 326 | let msg = if stream.peekrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]peek().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 327 | "missing line terminator at end of file" |
| 328 | } else { |
| 329 | "garbage at end of line" |
| 330 | }; |
| 331 | let error = |stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#| unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "{msg}"); |
| 332 | result.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(error(stream)); |
| 333 | } |
| 334 | stream.skip_to_newlinerust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]skip_to_newline().(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | result |
| 339 | } |
| 340 | |
| 341 | #[cfg(test)] |
| 342 | fn expect_completerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_complete().<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#>(stream: &mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> { |
| 343 | let result = expect_nonterminalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_nonterminal().(stream)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 344 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = stream.peekrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]peek().() { |
| 345 | unrecoverablerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/unrecoverable!!(stream, "garbage at end of line: {c}") |
| 346 | } |
| 347 | makerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/make().(result) |
| 348 | } |
| 349 | |
| 350 | /// Convenience function (especially useful for writing test cases, to avoid having to write out the |
| 351 | /// AST constructors by hand. |
| 352 | #[cfg(test)] |
| 353 | pub fn parse_stringrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_string().<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#>(text: &str) -> Parsedrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parsed#<T> { |
| 354 | expect_completerust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/expect_complete().(&mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#::newrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]new().(text)) |
| 355 | } |
| 356 | |
| 357 | #[cfg(test)] |
| 358 | pub fn parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().<T: Parserust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Parse#>(text: &str) -> T { |
| 359 | parse_stringrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_string().(text).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 360 | } |
| 361 | |
| 362 | #[cfg(test)] |
| 363 | mod testrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/ { |
| 364 | use superrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/::*; |
| 365 | |
| 366 | impl Tokenrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Token# for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 367 | fn constructrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/impl#[String][Token]construct().(val: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/impl#[String][Token], Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 368 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) |
| 369 | } |
| 370 | |
| 371 | fn acceptrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/impl#[String][Token]accept().(c: char) -> bool { |
| 372 | c.is_ascii_alphanumericrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_alphanumeric().() |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | impl Manyrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Many# for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {} |
| 377 | |
| 378 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 379 | fn comment_testrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/comment_test().() { |
| 380 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment#>("# hello"), Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment#); |
| 381 | } |
| 382 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 383 | #[should_panic] |
| 384 | fn comment_test_failrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/comment_test_fail().() { |
| 385 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment#>("# hello\nsomething"), Commentrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/Comment#); |
| 386 | } |
| 387 | |
| 388 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 389 | fn lines_testrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/lines_test().() { |
| 390 | let input = |text: &str| parse_linesrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_lines().(&mut CharStreamrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/CharStream#::newrust-analyzer cargo sudo-rs 0.2.14 sudoers/char_stream/impl#[`CharStream<'a>`]new().(text)); |
| 391 | |
| 392 | let s = |text: &str| Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(text.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 393 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input("hello\nworld\n"), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![s("hello"), s("world")]); |
| 394 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input(" hello\nworld\n"), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![s("hello"), s("world")]); |
| 395 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input("hello \nworld\n"), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![s("hello"), s("world")]); |
| 396 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input("hello\n world\n"), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![s("hello"), s("world")]); |
| 397 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input("hello\nworld \n"), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![s("hello"), s("world")]); |
| 398 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(input("hello\nworld")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0..2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![s("hello"), s("world")]); |
| 399 | let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) = input("hello\nworld")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). else { |
| 400 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!() |
| 401 | }; |
| 402 | let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) = input("hello\nworld:\n")[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). else { |
| 403 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!() |
| 404 | }; |
| 405 | } |
| 406 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 407 | fn whitespace_testrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/test/whitespace_test().() { |
| 408 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 409 | parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>("hello,something"), |
| 410 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["hello", "something"] |
| 411 | ); |
| 412 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 413 | parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>("hello , something"), |
| 414 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["hello", "something"] |
| 415 | ); |
| 416 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 417 | parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>("hello, something"), |
| 418 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["hello", "something"] |
| 419 | ); |
| 420 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 421 | parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>("hello ,something"), |
| 422 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["hello", "something"] |
| 423 | ); |
| 424 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 425 | parse_evalrust-analyzer cargo sudo-rs 0.2.14 sudoers/basic_parser/parse_eval().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>("hello\\\n,something"), |
| 426 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["hello", "something"] |
| 427 | ); |
| 428 | } |
| 429 | } |