Fix todo/error parser on when clauses.

This commit is contained in:
KtorZ 2023-02-16 00:33:13 +01:00
parent 808ff97c68
commit 56258dc815
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
5 changed files with 104 additions and 108 deletions

83
Cargo.lock generated vendored
View File

@ -29,15 +29,6 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "ahash"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217"
dependencies = [
"const-random",
]
[[package]]
name = "ahash"
version = "0.7.6"
@ -390,11 +381,12 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chumsky"
version = "0.8.0"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d02796e4586c6c41aeb68eae9bfb4558a522c35f1430c14b40136c3706e09e4"
checksum = "c4d619fba796986dd538d82660b76e0b9756c6e19b2e4d4559ba5a57f9f00810"
dependencies = [
"ahash 0.3.8",
"hashbrown",
"stacker",
]
[[package]]
@ -451,28 +443,6 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cec318a675afcb6a1ea1d4340e2d377e56e47c266f28043ceccbf4412ddfdd3b"
[[package]]
name = "const-random"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "368a7a772ead6ce7e1de82bfb04c485f3db8ec744f72925af5735e29a22cc18e"
dependencies = [
"const-random-macro",
"proc-macro-hack",
]
[[package]]
name = "const-random-macro"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d7d6ab3c3a2282db210df5f02c4dab6e0a7057af0fb7ebd4070f30fe05c0ddb"
dependencies = [
"getrandom",
"once_cell",
"proc-macro-hack",
"tiny-keccak",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
@ -556,12 +526,6 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "crypto-bigint"
version = "0.4.9"
@ -955,7 +919,7 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash 0.7.6",
"ahash",
]
[[package]]
@ -1800,12 +1764,6 @@ dependencies = [
"version_check",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.20+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
[[package]]
name = "proc-macro2"
version = "1.0.49"
@ -1835,6 +1793,15 @@ dependencies = [
"tempfile",
]
[[package]]
name = "psm"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"
dependencies = [
"cc",
]
[[package]]
name = "pulldown-cmark"
version = "0.8.0"
@ -2275,6 +2242,19 @@ dependencies = [
"der",
]
[[package]]
name = "stacker"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce"
dependencies = [
"cc",
"cfg-if",
"libc",
"psm",
"winapi",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
@ -2461,15 +2441,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "tinyvec"
version = "1.6.0"

View File

@ -11,7 +11,7 @@ authors = ["Lucas Rosa <x@rvcas.dev>", "Kasey White <kwhitemsg@gmail.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chumsky = "0.8.0"
chumsky = "0.9.0"
hex = "0.4.3"
indexmap = "1.9.1"
indoc = "1.0.7"

View File

@ -446,6 +446,18 @@ impl UntypedExpr {
}
}
pub fn error(location: Span, reason: Option<Self>) -> Self {
UntypedExpr::Trace {
location,
kind: TraceKind::Error,
then: Box::new(UntypedExpr::ErrorTerm { location }),
text: Box::new(reason.unwrap_or_else(|| UntypedExpr::String {
location,
value: DEFAULT_ERROR_STR.to_string(),
})),
}
}
pub fn append_in_sequence(self, next: Self) -> Self {
let location = Span {
start: self.location().start,

View File

@ -591,8 +591,12 @@ pub fn expr_seq_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseE
then: Box::new(then_),
text: Box::new(text),
}),
todo_parser(r.clone(), Token::ErrorTerm),
todo_parser(r.clone(), Token::Todo),
just(Token::ErrorTerm)
.ignore_then(expr_parser(r.clone()).or_not())
.map_with_span(|reason, span| expr::UntypedExpr::error(span, reason)),
just(Token::Todo)
.ignore_then(expr_parser(r.clone()).or_not())
.map_with_span(|reason, span| expr::UntypedExpr::todo(span, reason)),
expr_parser(r.clone())
.then(r.repeated())
.foldl(|current, next| current.append_in_sequence(next)),
@ -600,31 +604,6 @@ pub fn expr_seq_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseE
})
}
pub fn todo_parser(
r: Recursive<'_, Token, expr::UntypedExpr, ParseError>,
keyword: Token,
) -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> + '_ {
just(keyword.clone())
.ignore_then(expr_parser(r.clone()).or_not())
.map_with_span(move |text, span| {
let (kind, value) = match keyword {
Token::ErrorTerm => (TraceKind::Error, expr::DEFAULT_ERROR_STR.to_string()),
Token::Todo => (TraceKind::Todo, expr::DEFAULT_TODO_STR.to_string()),
_ => unreachable!(),
};
expr::UntypedExpr::Trace {
kind,
location: span,
then: Box::new(expr::UntypedExpr::ErrorTerm { location: span }),
text: Box::new(text.unwrap_or(expr::UntypedExpr::String {
location: span,
value,
})),
}
})
}
pub fn expr_parser(
seq_r: Recursive<'_, Token, expr::UntypedExpr, ParseError>,
) -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> + '_ {
@ -934,8 +913,6 @@ pub fn expr_parser(
choice((just(Token::LeftParen), just(Token::NewLineLeftParen))),
just(Token::RightParen),
),
just(Token::ErrorTerm).rewind().ignore_then(seq_r.clone()),
just(Token::Todo).rewind().ignore_then(seq_r.clone()),
));
let anon_fn_parser = just(Token::Fn)
@ -983,7 +960,23 @@ pub fn expr_parser(
}),
)))
// TODO: add hint "Did you mean to wrap a multi line clause in curly braces?"
.then(r.clone())
.then(choice((
r.clone(),
just(Token::Todo)
.ignore_then(
r.clone()
.then_ignore(one_of(Token::RArrow).not().rewind())
.or_not(),
)
.map_with_span(|reason, span| expr::UntypedExpr::todo(span, reason)),
just(Token::ErrorTerm)
.ignore_then(
r.clone()
.then_ignore(just(Token::RArrow).not().rewind())
.or_not(),
)
.map_with_span(|reason, span| expr::UntypedExpr::error(span, reason)),
)))
.map_with_span(
|(((patterns, alternative_patterns_opt), guard), then), span| ast::UntypedClause {
location: span,

View File

@ -2830,8 +2830,9 @@ fn parse_keyword_todo() {
fn bar() {
when x is {
Something -> Void
_ -> todo
Foo -> todo
Bar -> True
_ -> False
}
}
"#};
@ -2862,18 +2863,44 @@ fn parse_keyword_todo() {
ast::Definition::Fn(Function {
arguments: vec![],
body: expr::UntypedExpr::When {
location: Span::new((), 52..107),
location: Span::new((), 52..120),
subjects: vec![expr::UntypedExpr::Var {
location: Span::new((), 57..58),
name: "x".to_string(),
}],
clauses: vec![
ast::Clause {
location: Span::new((), 70..87),
location: Span::new((), 70..81),
pattern: vec![ast::Pattern::Constructor {
is_record: false,
location: Span::new((), 70..79),
name: "Something".to_string(),
location: Span::new((), 70..73),
name: "Foo".to_string(),
arguments: vec![],
module: None,
constructor: (),
with_spread: false,
tipo: (),
}],
alternative_patterns: vec![],
guard: None,
then: expr::UntypedExpr::Trace {
kind: ast::TraceKind::Todo,
location: Span::new((), 77..81),
then: Box::new(expr::UntypedExpr::ErrorTerm {
location: Span::new((), 77..81),
}),
text: Box::new(expr::UntypedExpr::String {
location: Span::new((), 77..81),
value: "aiken::todo".to_string(),
}),
},
},
ast::Clause {
location: Span::new((), 88..99),
pattern: vec![ast::Pattern::Constructor {
is_record: false,
location: Span::new((), 88..91),
name: "Bar".to_string(),
arguments: vec![],
module: None,
constructor: (),
@ -2883,28 +2910,21 @@ fn parse_keyword_todo() {
alternative_patterns: vec![],
guard: None,
then: expr::UntypedExpr::Var {
location: Span::new((), 83..87),
name: "Void".to_string(),
location: Span::new((), 95..99),
name: "True".to_string(),
},
},
ast::Clause {
location: Span::new((), 94..103),
location: Span::new((), 106..116),
pattern: vec![ast::Pattern::Discard {
name: "_".to_string(),
location: Span::new((), 94..95),
location: Span::new((), 106..107),
}],
alternative_patterns: vec![],
guard: None,
then: expr::UntypedExpr::Trace {
kind: ast::TraceKind::Todo,
location: Span::new((), 99..103),
then: Box::new(expr::UntypedExpr::ErrorTerm {
location: Span::new((), 99..103),
}),
text: Box::new(expr::UntypedExpr::String {
location: Span::new((), 99..103),
value: "aiken::todo".to_string(),
}),
then: expr::UntypedExpr::Var {
location: Span::new((), 111..116),
name: "False".to_string(),
},
},
],
@ -2915,7 +2935,7 @@ fn parse_keyword_todo() {
public: false,
return_annotation: None,
return_type: (),
end_position: 108,
end_position: 121,
}),
],
)