feat: add check keyword and new assignment syntax
This commit is contained in:
parent
6687b9fe4c
commit
34d7a28351
|
@ -618,6 +618,7 @@ impl<A, B> Pattern<A, B> {
|
||||||
pub enum AssignmentKind {
|
pub enum AssignmentKind {
|
||||||
Let,
|
Let,
|
||||||
Assert,
|
Assert,
|
||||||
|
Check,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type MultiPattern<PatternConstructor, Type> = Vec<Pattern<PatternConstructor, Type>>;
|
pub type MultiPattern<PatternConstructor, Type> = Vec<Pattern<PatternConstructor, Type>>;
|
||||||
|
|
|
@ -568,6 +568,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
let keyword = match kind {
|
let keyword = match kind {
|
||||||
Some(AssignmentKind::Let) => "let ",
|
Some(AssignmentKind::Let) => "let ",
|
||||||
Some(AssignmentKind::Assert) => "assert ",
|
Some(AssignmentKind::Assert) => "assert ",
|
||||||
|
Some(AssignmentKind::Check) => "check ",
|
||||||
None => "try ",
|
None => "try ",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -494,7 +494,7 @@ pub fn expr_parser(
|
||||||
let assert_parser = just(Token::Assert)
|
let assert_parser = just(Token::Assert)
|
||||||
.ignore_then(pattern_parser())
|
.ignore_then(pattern_parser())
|
||||||
.then(just(Token::Colon).ignore_then(type_parser()).or_not())
|
.then(just(Token::Colon).ignore_then(type_parser()).or_not())
|
||||||
.then_ignore(just(Token::Equal))
|
.then_ignore(just(Token::Is))
|
||||||
.then(r.clone())
|
.then(r.clone())
|
||||||
.map_with_span(
|
.map_with_span(
|
||||||
|((pattern, annotation), value), span| expr::UntypedExpr::Assignment {
|
|((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)
|
let if_parser = just(Token::If)
|
||||||
.ignore_then(r.clone().then(block_parser.clone()).map_with_span(
|
.ignore_then(r.clone().then(block_parser.clone()).map_with_span(
|
||||||
|(condition, body), span| ast::IfBranch {
|
|(condition, body), span| ast::IfBranch {
|
||||||
|
@ -551,6 +566,7 @@ pub fn expr_parser(
|
||||||
when_parser,
|
when_parser,
|
||||||
let_parser,
|
let_parser,
|
||||||
assert_parser,
|
assert_parser,
|
||||||
|
check_parser,
|
||||||
if_parser,
|
if_parser,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
let keyword = text::ident().map(|s: String| match s.as_str() {
|
||||||
"as" => Token::As,
|
"as" => Token::As,
|
||||||
"assert" => Token::Assert,
|
"assert" => Token::Assert,
|
||||||
|
"check" => Token::Assert,
|
||||||
"const" => Token::Const,
|
"const" => Token::Const,
|
||||||
"fn" => Token::Fn,
|
"fn" => Token::Fn,
|
||||||
"if" => Token::If,
|
"if" => Token::If,
|
||||||
|
|
|
@ -58,6 +58,7 @@ pub enum Token {
|
||||||
// Keywords (alphabetically):
|
// Keywords (alphabetically):
|
||||||
As,
|
As,
|
||||||
Assert,
|
Assert,
|
||||||
|
Check,
|
||||||
Const,
|
Const,
|
||||||
Fn,
|
Fn,
|
||||||
If,
|
If,
|
||||||
|
@ -130,6 +131,7 @@ impl fmt::Display for Token {
|
||||||
Token::EmptyLine => "EMPTYLINE",
|
Token::EmptyLine => "EMPTYLINE",
|
||||||
Token::As => "as",
|
Token::As => "as",
|
||||||
Token::Assert => "assert",
|
Token::Assert => "assert",
|
||||||
|
Token::Check => "check",
|
||||||
Token::When => "when",
|
Token::When => "when",
|
||||||
Token::Is => "is",
|
Token::Is => "is",
|
||||||
Token::Const => "const",
|
Token::Const => "const",
|
||||||
|
|
|
@ -449,6 +449,7 @@ fn str_to_keyword(word: &str) -> Option<Token> {
|
||||||
match word {
|
match word {
|
||||||
"as" => Some(Token::As),
|
"as" => Some(Token::As),
|
||||||
"assert" => Some(Token::Assert),
|
"assert" => Some(Token::Assert),
|
||||||
|
"check" => Some(Token::Check),
|
||||||
"when" => Some(Token::When),
|
"when" => Some(Token::When),
|
||||||
"const" => Some(Token::Const),
|
"const" => Some(Token::Const),
|
||||||
"fn" => Some(Token::Fn),
|
"fn" => Some(Token::Fn),
|
||||||
|
|
Loading…
Reference in New Issue