Parse do yeet expressions

This commit is contained in:
Maybe Waffle 2022-12-28 22:42:26 +00:00
parent 3033c3ddbf
commit 4a16afa264
7 changed files with 73 additions and 5 deletions

View File

@ -646,7 +646,7 @@ pub fn add_one(x: i32) -> i32 {
x + 1
}
pub fn do() {
pub fn r#do() {
add_one($0
}"#,
expect![[r##"

View File

@ -48,6 +48,7 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet =
T![unsafe],
T![return],
T![yield],
T![do],
T![break],
T![continue],
T![async],
@ -93,6 +94,7 @@ pub(super) fn atom_expr(
T![match] => match_expr(p),
T![return] => return_expr(p),
T![yield] => yield_expr(p),
T![do] if p.nth_at_contextual_kw(1, T![yeet]) => yeet_expr(p),
T![continue] => continue_expr(p),
T![break] => break_expr(p, r),
@ -533,6 +535,7 @@ fn return_expr(p: &mut Parser<'_>) -> CompletedMarker {
}
m.complete(p, RETURN_EXPR)
}
// test yield_expr
// fn foo() {
// yield;
@ -548,6 +551,23 @@ fn yield_expr(p: &mut Parser<'_>) -> CompletedMarker {
m.complete(p, YIELD_EXPR)
}
// test yeet_expr
// fn foo() {
// do yeet;
// do yeet 1
// }
fn yeet_expr(p: &mut Parser<'_>) -> CompletedMarker {
assert!(p.at(T![do]));
assert!(p.nth_at_contextual_kw(1, T![yeet]));
let m = p.start();
p.bump(T![do]);
p.bump_remap(T![yeet]);
if p.at_ts(EXPR_FIRST) {
expr(p);
}
m.complete(p, YEET_EXPR)
}
// test continue_expr
// fn foo() {
// loop {

View File

@ -148,11 +148,16 @@ impl<'t> Parser<'t> {
kinds.contains(self.current())
}
/// Checks if the current token is contextual keyword with text `t`.
/// Checks if the current token is contextual keyword `kw`.
pub(crate) fn at_contextual_kw(&self, kw: SyntaxKind) -> bool {
self.inp.contextual_kind(self.pos) == kw
}
/// Checks if the nth token is contextual keyword `kw`.
pub(crate) fn nth_at_contextual_kw(&self, n: usize, kw: SyntaxKind) -> bool {
self.inp.contextual_kind(self.pos + n) == kw
}
/// Starts a new node in the syntax tree. All nodes and tokens
/// consumed between the `start` and the corresponding `Marker::complete`
/// belong to the same node.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,31 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "foo"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
YEET_EXPR
DO_KW "do"
WHITESPACE " "
YEET_KW "yeet"
SEMICOLON ";"
WHITESPACE "\n "
YEET_EXPR
DO_KW "do"
WHITESPACE " "
YEET_KW "yeet"
WHITESPACE " "
LITERAL
INT_NUMBER "1"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"

View File

@ -0,0 +1,4 @@
fn foo() {
do yeet;
do yeet 1
}

View File

@ -65,12 +65,12 @@ pub(crate) const KINDS_SRC: KindsSrc<'_> = KindsSrc {
(">>=", "SHREQ"),
],
keywords: &[
"as", "async", "await", "box", "break", "const", "continue", "crate", "dyn", "else",
"as", "async", "await", "box", "break", "const", "continue", "crate", "do", "dyn", "else",
"enum", "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "macro",
"match", "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct",
"super", "trait", "true", "try", "type", "unsafe", "use", "where", "while", "yield",
],
contextual_keywords: &["auto", "default", "existential", "union", "raw", "macro_rules"],
contextual_keywords: &["auto", "default", "existential", "union", "raw", "macro_rules", "yeet"],
literals: &["INT_NUMBER", "FLOAT_NUMBER", "CHAR", "BYTE", "STRING", "BYTE_STRING"],
tokens: &["ERROR", "IDENT", "WHITESPACE", "LIFETIME_IDENT", "COMMENT", "SHEBANG"],
nodes: &[
@ -142,6 +142,7 @@ pub(crate) const KINDS_SRC: KindsSrc<'_> = KindsSrc {
"STMT_LIST",
"RETURN_EXPR",
"YIELD_EXPR",
"YEET_EXPR",
"LET_EXPR",
"UNDERSCORE_EXPR",
"MACRO_EXPR",