pubfn parse<B: Buffer>(input: B) -> Result<Literal<B>, ParseError> { let (first, rest) = input.as_bytes().split_first().ok_or(perr(None, Empty))?; let second = input.as_bytes().get(1).copied();
match first {
b'f'if &*input == "false" => Ok(Literal::Bool(BoolLit::False)),
b't'if &*input == "true" => Ok(Literal::Bool(BoolLit::True)),
// A number literal (integer or float).
b'0'..=b'9' => { // To figure out whether this is a float or integer, we do some // quick inspection here. Yes, this is technically duplicate // work with what is happening in the integer/float parse // methods, but it makes the code way easier for now and won't // be a huge performance loss. // // The first non-decimal char in a float literal must // be '.', 'e' or 'E'. match input.as_bytes().get(1 + end_dec_digits(rest)) {
Some(b'.') | Some(b'e') | Some(b'E')
=> FloatLit::parse(input).map(Literal::Float),
/// Returns the index of the first non-underscore, non-decimal digit in `input`, /// or the `input.len()` if all characters are decimal digits. pub(crate) fn end_dec_digits(input: &[u8]) -> usize {
input.iter()
.position(|b| !matches!(b, b'_' | b'0'..=b'9'))
.unwrap_or(input.len())
}
/// Makes sure that `s` is a valid literal suffix. pub(crate) fn check_suffix(s: &str) -> Result<(), ParseErrorKind> { if s.is_empty() { return Ok(());
}
letmut chars = s.chars(); let first = chars.next().unwrap(); let rest = chars.as_str(); if first == '_' && rest.is_empty() { return Err(InvalidSuffix);
}
// This is just an extra check to improve the error message. If the first // character of the "suffix" is already some invalid ASCII // char, "unexpected character" seems like the more fitting error. if first.is_ascii() && !(first.is_ascii_alphabetic() || first == '_') { return Err(UnexpectedChar);
}
// Proper check is optional as it's not really necessary in proc macro // context. #[cfg(feature = "check_suffix")] fn is_valid_suffix(first: char, rest: &str) -> bool { use unicode_xid::UnicodeXID;
// When avoiding the dependency on `unicode_xid`, we just do a best effort // to catch the most common errors. #[cfg(not(feature = "check_suffix"))] fn is_valid_suffix(first: char, rest: &str) -> bool { if first.is_ascii() && !(first.is_ascii_alphabetic() || first == '_') { returnfalse;
} for c in rest.chars() { if c.is_ascii() && !(c.is_ascii_alphanumeric() || c == '_') { returnfalse;
}
} true
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.