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
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
5 changed files with 33 additions and 15 deletions

View File

@ -20,6 +20,7 @@
- **aiken-lang**: assignment and clause guard are now always formatted on a new line - **aiken-lang**: assignment and clause guard are now always formatted on a new line
- **aiken-lang**: unused let-bindings are now fully removed from generated code and discarded unused let-binding now raise a warning - **aiken-lang**: unused let-bindings are now fully removed from generated code and discarded unused let-binding now raise a warning
- **aiken-lang**: support multi-clause patterns (only as a syntactic sugar) - **aiken-lang**: support multi-clause patterns (only as a syntactic sugar)
- **aiken-lang**: fix lexer panic when parsing too large (> u32) tuple-indexes
## [v0.0.29] - 2023-MM-DD ## [v0.0.29] - 2023-MM-DD

View File

@ -1110,7 +1110,11 @@ pub fn expr_parser(
}) })
.validate(|index, span, emit| { .validate(|index, span, emit| {
if index < 1 { if index < 1 {
emit(ParseError::invalid_tuple_index(span, index, None)); emit(ParseError::invalid_tuple_index(
span,
index.to_string(),
None,
));
Chain::TupleIndex(0, span) Chain::TupleIndex(0, span)
} else { } else {
Chain::TupleIndex(index as usize - 1, span) Chain::TupleIndex(index as usize - 1, span)

View File

@ -25,7 +25,7 @@ impl ParseError {
self 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}'?")); let hint = suffix.map(|suffix| format!("Did you mean '{index}{suffix}'?"));
Self { Self {
kind: ErrorKind::InvalidTupleIndex { hint }, 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 int = text::int(10).map(|value| Token::Int { value });
let ordinal = text::int(10) let ordinal = text::int(10)
.from_str() .then_with(|index: String| {
.unwrapped()
.then_with(|index: u32| {
choice((just("st"), just("nd"), just("rd"), just("th"))) 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| { .validate(|(index, suffix), span, emit| match index.parse() {
let expected_suffix = Ordinal(index).suffix(); Err { .. } => {
if expected_suffix != suffix { emit(ParseError::invalid_tuple_index(span, index, None));
emit(ParseError::invalid_tuple_index( Token::Ordinal { index: 0 }
span, }
index, Ok(index) => {
Some(expected_suffix.to_string()), 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(( let op = choice((

View File

@ -1864,6 +1864,15 @@ fn parse_tuple2() {
); );
} }
#[test]
fn large_integer_constants() {
let code = indoc! {r#"
pub const my_big_int = 999999999999999999999999
"#};
assert_definitions(code, vec![])
}
#[test] #[test]
fn plain_bytearray_literals() { fn plain_bytearray_literals() {
let code = indoc! {r#" let code = indoc! {r#"