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
use crate::{
error::{Error, ErrorKind, TokenPattern},
parser::*,
};
/// A parser that matches a fixed identifier string in the input.
///
/// `PIdent` attempts to parse a specific string slice (`ident`) from the input tokens.
/// The input tokens are converted into characters to compare with the target identifier.
///
/// # Lifetime
///
/// * `'a` - Lifetime of the input string slice `ident`.
#[derive(Clone)]
pub struct PIdent<'a> {
/// The string slice representing the identifier to match.
ident: &'a str,
}
/// Creates a new `PIdent` parser that matches the exact string `ident`.
/// Note that this is only implemented for input types [char] or [u8] since
/// these are the logical types to compare against a [str].
///
/// # Arguments
///
/// * `ident` - The identifier string to match against the input.
///
/// # Returns
///
/// A `PIdent` instance that implements `ParserCore` and attempts to match
/// the given identifier string from the input tokens.
pub fn pident<'a>(ident: &'a str) -> PIdent<'a> {
PIdent { ident }
}
impl<'a> ParserCore<'a, u8, &'a str> for PIdent<'a> {
/// Attempts to parse the identifier string from the input of u8.
///
/// This method extracts a [u8] from the input tokens of length equal to the
/// identifier's length, and directly compares to the ident `.as_bytes()`
///
/// # Arguments
///
/// * `i` - The parser input containing tokens and current location.
///
/// # Returns
///
/// * `Ok(PSuccess)` with the matched identifier string and updated input location
/// if the input matches the identifier exactly.
/// * `Err(Error)` containing an error message, the span of the failed match,
/// and the input position after the attempted parse if the match fails.
fn parse(&self, i: PInput<'a, u8>) -> Result<PSuccess<'a, u8, &'a str>, Error<'a, u8>> {
let ident_len = self.ident.len();
if i.tokens.len() < i.loc + ident_len {
return Err(Error {
kind: vec![ErrorKind::Unexpected {
expected: vec![TokenPattern::String(std::borrow::Cow::Borrowed(self.ident))],
found: TokenPattern::String(std::borrow::Cow::Borrowed("Not enough tokens")),
}],
span: (i.loc, i.tokens.len()),
state: PInput {
tokens: i.tokens,
loc: i.loc,
},
});
}
// Compare input tokens to identified
if &i.tokens[i.loc..i.loc + ident_len] == self.ident.as_bytes() {
// Successful parse: return matched string and updated location
Ok(PSuccess {
val: self.ident,
rest: PInput {
tokens: i.tokens,
loc: i.loc + ident_len,
},
})
} else {
// Failed parse: return error message, span, and updated input location
Err(Error {
kind: vec![ErrorKind::Unexpected {
expected: vec![TokenPattern::String(std::borrow::Cow::Borrowed(self.ident))],
found: TokenPattern::Tokens(std::borrow::Cow::Borrowed(
&i.tokens[i.loc..i.loc + ident_len],
)),
}],
span: (i.loc, i.loc + ident_len),
state: PInput {
tokens: i.tokens,
loc: i.loc,
},
})
}
}
}
impl<'a> ParserCore<'a, char, &'a str> for PIdent<'a> {
/// Attempts to parse the identifier string from the input of char.
///
/// This method extracts a slice from the input [char]s of length equal to the
/// identifier's length, then converts the ident into chars and compares each
/// element.
///
/// # Arguments
///
/// * `i` - The parser input containing tokens and current location.
///
/// # Returns
///
/// * `Ok(PSuccess)` with the matched identifier string and updated input location
/// if the input matches the identifier exactly.
/// * `Err(Error)` containing an error message, the span of the failed match,
/// and the input position after the attempted parse if the match fails.
fn parse(&self, i: PInput<'a, char>) -> Result<PSuccess<'a, char, &'a str>, Error<'a, char>> {
let ident_len = self.ident.len();
if i.tokens.len() < i.loc + ident_len {
return Err(Error {
kind: vec![ErrorKind::Unexpected {
expected: vec![TokenPattern::String(std::borrow::Cow::Borrowed(self.ident))],
found: TokenPattern::String(std::borrow::Cow::Borrowed("Not enough tokens")),
}],
span: (i.loc, i.tokens.len()),
state: PInput {
tokens: i.tokens,
loc: i.loc,
},
});
}
// Compare input tokens to identified
if self
.ident
.chars()
.zip(i.tokens[i.loc..i.loc + ident_len].iter())
.all(|(a, b)| a == *b)
{
// Successful parse: return matched string and updated location
Ok(PSuccess {
val: self.ident,
rest: PInput {
tokens: i.tokens,
loc: i.loc + ident_len,
},
})
} else {
// Failed parse: return error message, span, and updated input location
Err(Error {
kind: vec![ErrorKind::Unexpected {
expected: vec![TokenPattern::String(std::borrow::Cow::Borrowed(self.ident))],
found: TokenPattern::Tokens(std::borrow::Cow::Borrowed(
&i.tokens[i.loc..i.loc + ident_len],
)),
}],
span: (i.loc, i.loc + ident_len),
state: PInput {
tokens: i.tokens,
loc: i.loc,
},
})
}
}
}
impl<'a> Parser<'a, u8, &'a str> for PIdent<'a> {}
impl<'a> Parser<'a, char, &'a str> for PIdent<'a> {}