use crate::parser::*;
pub struct PIdent<'a> {
ident: &'a str,
}
pub fn pident<'a>(ident: &'a str) -> PIdent<'a> {
PIdent { ident }
}
impl<'a, K> ParserCore<'a, K, &'a str> for PIdent<'a>
where
K: PartialEq + Clone + PartialEq<u8> + 'a,
{
fn parse(&self, i: PInput<'a, K>) -> Result<PSuccess<'a, K, &'a str>, PFail<'a, K>> {
let ident_len = self.ident.len();
if i.tokens.len() < i.loc + ident_len {
return Err(PFail {
error: vec![format!("Not enough tokens to match {}", self.ident)],
span: vec![(i.loc, i.tokens.len())],
rest: PInput {
tokens: i.tokens,
loc: i.loc,
},
});
}
if i.tokens[i.loc..i.loc + ident_len]
.iter()
.zip(self.ident.as_bytes())
.any(|(a, b)| !(a.eq(b)))
{
Err(PFail {
error: vec![format!("Expected {}", self.ident)],
span: vec![(i.loc, i.loc + ident_len)],
rest: PInput {
tokens: i.tokens,
loc: i.loc + ident_len,
},
})
} else {
Ok(PSuccess {
val: self.ident,
rest: PInput {
tokens: i.tokens,
loc: i.loc + ident_len,
},
})
}
}
}
impl<'a, K> Parser<'a, K, &'a str> for PIdent<'a> where K: PartialEq + Clone + PartialEq<u8> + 'a {}