feat: add check keyword and new assignment syntax

This commit is contained in:
rvcas 2022-11-23 19:04:50 -05:00 committed by Lucas
parent 6687b9fe4c
commit 34d7a28351
6 changed files with 23 additions and 1 deletions

View File

@ -618,6 +618,7 @@ impl<A, B> Pattern<A, B> {
pub enum AssignmentKind {
Let,
Assert,
Check,
}
pub type MultiPattern<PatternConstructor, Type> = Vec<Pattern<PatternConstructor, Type>>;

View File

@ -568,6 +568,7 @@ impl<'comments> Formatter<'comments> {
let keyword = match kind {
Some(AssignmentKind::Let) => "let ",
Some(AssignmentKind::Assert) => "assert ",
Some(AssignmentKind::Check) => "check ",
None => "try ",
};

View File

@ -494,7 +494,7 @@ pub fn expr_parser(
let assert_parser = just(Token::Assert)
.ignore_then(pattern_parser())
.then(just(Token::Colon).ignore_then(type_parser()).or_not())
.then_ignore(just(Token::Equal))
.then_ignore(just(Token::Is))
.then(r.clone())
.map_with_span(
|((pattern, annotation), value), span| expr::UntypedExpr::Assignment {
@ -506,6 +506,21 @@ pub fn expr_parser(
},
);
let check_parser = just(Token::Check)
.ignore_then(pattern_parser())
.then(just(Token::Colon).ignore_then(type_parser()).or_not())
.then_ignore(just(Token::Is))
.then(r.clone())
.map_with_span(
|((pattern, annotation), value), span| expr::UntypedExpr::Assignment {
location: span,
value: Box::new(value),
pattern,
kind: ast::AssignmentKind::Check,
annotation,
},
);
let if_parser = just(Token::If)
.ignore_then(r.clone().then(block_parser.clone()).map_with_span(
|(condition, body), span| ast::IfBranch {
@ -551,6 +566,7 @@ pub fn expr_parser(
when_parser,
let_parser,
assert_parser,
check_parser,
if_parser,
));

View File

@ -63,6 +63,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = ParseError> {
let keyword = text::ident().map(|s: String| match s.as_str() {
"as" => Token::As,
"assert" => Token::Assert,
"check" => Token::Assert,
"const" => Token::Const,
"fn" => Token::Fn,
"if" => Token::If,

View File

@ -58,6 +58,7 @@ pub enum Token {
// Keywords (alphabetically):
As,
Assert,
Check,
Const,
Fn,
If,
@ -130,6 +131,7 @@ impl fmt::Display for Token {
Token::EmptyLine => "EMPTYLINE",
Token::As => "as",
Token::Assert => "assert",
Token::Check => "check",
Token::When => "when",
Token::Is => "is",
Token::Const => "const",

View File

@ -449,6 +449,7 @@ fn str_to_keyword(word: &str) -> Option<Token> {
match word {
"as" => Some(Token::As),
"assert" => Some(Token::Assert),
"check" => Some(Token::Check),
"when" => Some(Token::When),
"const" => Some(Token::Const),
"fn" => Some(Token::Fn),