Fix lexer throwing errors when parsing a too large tuple index.

This commit is contained in:
KtorZ
2023-03-18 16:12:45 +01:00
parent df722d6f0f
commit 8a2af4cd2e
5 changed files with 33 additions and 15 deletions

View File

@@ -25,7 +25,7 @@ impl ParseError {
self
}
pub fn invalid_tuple_index(span: Span, index: u32, suffix: Option<String>) -> Self {
pub fn invalid_tuple_index(span: Span, index: String, suffix: Option<String>) -> Self {
let hint = suffix.map(|suffix| format!("Did you mean '{index}{suffix}'?"));
Self {
kind: ErrorKind::InvalidTupleIndex { hint },

View File

@@ -10,22 +10,26 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
let int = text::int(10).map(|value| Token::Int { value });
let ordinal = text::int(10)
.from_str()
.unwrapped()
.then_with(|index: u32| {
.then_with(|index: String| {
choice((just("st"), just("nd"), just("rd"), just("th")))
.map(move |suffix| (index, suffix))
.map(move |suffix| (index.to_string(), suffix))
})
.validate(|(index, suffix), span, emit| {
let expected_suffix = Ordinal(index).suffix();
if expected_suffix != suffix {
emit(ParseError::invalid_tuple_index(
span,
index,
Some(expected_suffix.to_string()),
))
.validate(|(index, suffix), span, emit| match index.parse() {
Err { .. } => {
emit(ParseError::invalid_tuple_index(span, index, None));
Token::Ordinal { index: 0 }
}
Ok(index) => {
let expected_suffix = Ordinal::<u32>(index).suffix();
if expected_suffix != suffix {
emit(ParseError::invalid_tuple_index(
span,
index.to_string(),
Some(expected_suffix.to_string()),
))
}
Token::Ordinal { index }
}
Token::Ordinal { index }
});
let op = choice((