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", "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]] [[package]]
name = "ahash" name = "ahash"
version = "0.7.6" version = "0.7.6"
@ -390,11 +381,12 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chumsky" name = "chumsky"
version = "0.8.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d02796e4586c6c41aeb68eae9bfb4558a522c35f1430c14b40136c3706e09e4" checksum = "c4d619fba796986dd538d82660b76e0b9756c6e19b2e4d4559ba5a57f9f00810"
dependencies = [ dependencies = [
"ahash 0.3.8", "hashbrown",
"stacker",
] ]
[[package]] [[package]]
@ -451,28 +443,6 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cec318a675afcb6a1ea1d4340e2d377e56e47c266f28043ceccbf4412ddfdd3b" 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]] [[package]]
name = "constant_time_eq" name = "constant_time_eq"
version = "0.1.5" version = "0.1.5"
@ -556,12 +526,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]] [[package]]
name = "crypto-bigint" name = "crypto-bigint"
version = "0.4.9" version = "0.4.9"
@ -955,7 +919,7 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [ dependencies = [
"ahash 0.7.6", "ahash",
] ]
[[package]] [[package]]
@ -1800,12 +1764,6 @@ dependencies = [
"version_check", "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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.49" version = "1.0.49"
@ -1835,6 +1793,15 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "psm"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "pulldown-cmark" name = "pulldown-cmark"
version = "0.8.0" version = "0.8.0"
@ -2275,6 +2242,19 @@ dependencies = [
"der", "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]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"
@ -2461,15 +2441,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" 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]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chumsky = "0.8.0" chumsky = "0.9.0"
hex = "0.4.3" hex = "0.4.3"
indexmap = "1.9.1" indexmap = "1.9.1"
indoc = "1.0.7" 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 { pub fn append_in_sequence(self, next: Self) -> Self {
let location = Span { let location = Span {
start: self.location().start, 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_), then: Box::new(then_),
text: Box::new(text), text: Box::new(text),
}), }),
todo_parser(r.clone(), Token::ErrorTerm), just(Token::ErrorTerm)
todo_parser(r.clone(), Token::Todo), .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()) expr_parser(r.clone())
.then(r.repeated()) .then(r.repeated())
.foldl(|current, next| current.append_in_sequence(next)), .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( pub fn expr_parser(
seq_r: Recursive<'_, Token, expr::UntypedExpr, ParseError>, seq_r: Recursive<'_, Token, expr::UntypedExpr, ParseError>,
) -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> + '_ { ) -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> + '_ {
@ -934,8 +913,6 @@ pub fn expr_parser(
choice((just(Token::LeftParen), just(Token::NewLineLeftParen))), choice((just(Token::LeftParen), just(Token::NewLineLeftParen))),
just(Token::RightParen), 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) 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?" // 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( .map_with_span(
|(((patterns, alternative_patterns_opt), guard), then), span| ast::UntypedClause { |(((patterns, alternative_patterns_opt), guard), then), span| ast::UntypedClause {
location: span, location: span,

View File

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