This commit is contained in:
Aleksey Kladov 2018-08-01 00:14:26 +03:00
parent a31cb829ed
commit a750135a0a
8 changed files with 120 additions and 7 deletions

View File

@ -60,6 +60,7 @@ Grammar(
"loop", "loop",
"while", "while",
"if", "if",
"else",
"match", "match",
"const", "const",
"static", "static",
@ -134,6 +135,7 @@ Grammar(
"LAMBDA_EXPR", "LAMBDA_EXPR",
"STRUCT_LIT", "STRUCT_LIT",
"STRUCT_LIT_FIELD", "STRUCT_LIT_FIELD",
"IF_EXPR",
"EXTERN_BLOCK", "EXTERN_BLOCK",
"ENUM_VARIANT", "ENUM_VARIANT",

View File

@ -145,14 +145,16 @@ fn atom_expr(p: &mut Parser) -> Option<CompletedMarker> {
return Some(path_expr(p)); return Some(path_expr(p));
} }
match p.current() { let done = match p.current() {
L_PAREN => Some(tuple_expr(p)), L_PAREN => tuple_expr(p),
PIPE => Some(lambda_expr(p)), PIPE => lambda_expr(p),
IF_KW => if_expr(p),
_ => { _ => {
p.err_and_bump("expected expression"); p.err_and_bump("expected expression");
None return None;
} }
} };
Some(done)
} }
fn tuple_expr(p: &mut Parser) -> CompletedMarker { fn tuple_expr(p: &mut Parser) -> CompletedMarker {
@ -182,6 +184,29 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker {
m.complete(p, LAMBDA_EXPR) m.complete(p, LAMBDA_EXPR)
} }
// test if_expr
// fn foo() {
// if true {};
// if true {} else {};
// if true {} else if false {} else {}
// }
fn if_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(IF_KW));
let m = p.start();
p.bump();
expr(p);
block(p);
if p.at(ELSE_KW) {
p.bump();
if p.at(IF_KW) {
if_expr(p);
} else {
block(p);
}
}
m.complete(p, IF_EXPR)
}
// test call_expr // test call_expr
// fn foo() { // fn foo() {
// let _ = f(); // let _ = f();

View File

@ -61,6 +61,7 @@ pub enum SyntaxKind {
LOOP_KW, LOOP_KW,
WHILE_KW, WHILE_KW,
IF_KW, IF_KW,
ELSE_KW,
MATCH_KW, MATCH_KW,
CONST_KW, CONST_KW,
STATIC_KW, STATIC_KW,
@ -125,6 +126,7 @@ pub enum SyntaxKind {
LAMBDA_EXPR, LAMBDA_EXPR,
STRUCT_LIT, STRUCT_LIT,
STRUCT_LIT_FIELD, STRUCT_LIT_FIELD,
IF_EXPR,
EXTERN_BLOCK, EXTERN_BLOCK,
ENUM_VARIANT, ENUM_VARIANT,
NAMED_FIELD, NAMED_FIELD,
@ -223,6 +225,7 @@ impl SyntaxKind {
LOOP_KW => &SyntaxInfo { name: "LOOP_KW" }, LOOP_KW => &SyntaxInfo { name: "LOOP_KW" },
WHILE_KW => &SyntaxInfo { name: "WHILE_KW" }, WHILE_KW => &SyntaxInfo { name: "WHILE_KW" },
IF_KW => &SyntaxInfo { name: "IF_KW" }, IF_KW => &SyntaxInfo { name: "IF_KW" },
ELSE_KW => &SyntaxInfo { name: "ELSE_KW" },
MATCH_KW => &SyntaxInfo { name: "MATCH_KW" }, MATCH_KW => &SyntaxInfo { name: "MATCH_KW" },
CONST_KW => &SyntaxInfo { name: "CONST_KW" }, CONST_KW => &SyntaxInfo { name: "CONST_KW" },
STATIC_KW => &SyntaxInfo { name: "STATIC_KW" }, STATIC_KW => &SyntaxInfo { name: "STATIC_KW" },
@ -287,6 +290,7 @@ impl SyntaxKind {
LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" }, LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" },
STRUCT_LIT => &SyntaxInfo { name: "STRUCT_LIT" }, STRUCT_LIT => &SyntaxInfo { name: "STRUCT_LIT" },
STRUCT_LIT_FIELD => &SyntaxInfo { name: "STRUCT_LIT_FIELD" }, STRUCT_LIT_FIELD => &SyntaxInfo { name: "STRUCT_LIT_FIELD" },
IF_EXPR => &SyntaxInfo { name: "IF_EXPR" },
EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
@ -344,6 +348,7 @@ impl SyntaxKind {
"loop" => LOOP_KW, "loop" => LOOP_KW,
"while" => WHILE_KW, "while" => WHILE_KW,
"if" => IF_KW, "if" => IF_KW,
"else" => ELSE_KW,
"match" => MATCH_KW, "match" => MATCH_KW,
"const" => CONST_KW, "const" => CONST_KW,
"static" => STATIC_KW, "static" => STATIC_KW,
@ -445,6 +450,7 @@ impl SyntaxKind {
LOOP_KW => "loop", LOOP_KW => "loop",
WHILE_KW => "while", WHILE_KW => "while",
IF_KW => "if", IF_KW => "if",
ELSE_KW => "else",
MATCH_KW => "match", MATCH_KW => "match",
CONST_KW => "const", CONST_KW => "const",
STATIC_KW => "static", STATIC_KW => "static",

View File

@ -1,3 +1,3 @@
fn use struct trait enum impl true false as extern crate fn use struct trait enum impl true false as extern crate
mod pub self super in where for loop while if match const mod pub self super in where for loop while if match const
static mut type ref let static mut type ref let else

View File

@ -53,4 +53,6 @@ WHITESPACE 1 " "
REF_KW 3 "ref" REF_KW 3 "ref"
WHITESPACE 1 " " WHITESPACE 1 " "
LET_KW 3 "let" LET_KW 3 "let"
WHITESPACE 1 " "
ELSE_KW 4 "else"
WHITESPACE 1 "\n" WHITESPACE 1 "\n"

View File

@ -37,7 +37,7 @@ FILE@[0; 95)
WHITESPACE@[45; 50) WHITESPACE@[45; 50)
R_CURLY@[50; 51) R_CURLY@[50; 51)
WHITESPACE@[51; 52) WHITESPACE@[51; 52)
IDENT@[52; 56) "else" ELSE_KW@[52; 56)
WHITESPACE@[56; 57) WHITESPACE@[56; 57)
L_CURLY@[57; 58) L_CURLY@[57; 58)
WHITESPACE@[58; 67) WHITESPACE@[58; 67)

View File

@ -0,0 +1,5 @@
fn foo() {
if true {};
if true {} else {};
if true {} else if false {} else {}
}

View File

@ -0,0 +1,73 @@
FILE@[0; 93)
FN_ITEM@[0; 93)
FN_KW@[0; 2)
NAME@[2; 6)
WHITESPACE@[2; 3)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 9)
L_PAREN@[6; 7)
R_PAREN@[7; 8)
WHITESPACE@[8; 9)
BLOCK@[9; 93)
L_CURLY@[9; 10)
EXPR_STMT@[10; 31)
IF_EXPR@[10; 25)
WHITESPACE@[10; 15)
IF_KW@[15; 17)
LITERAL@[17; 23)
WHITESPACE@[17; 18)
TRUE_KW@[18; 22)
WHITESPACE@[22; 23)
BLOCK@[23; 25)
L_CURLY@[23; 24)
R_CURLY@[24; 25)
SEMI@[25; 26)
WHITESPACE@[26; 31)
EXPR_STMT@[31; 55)
IF_EXPR@[31; 49)
IF_KW@[31; 33)
LITERAL@[33; 39)
WHITESPACE@[33; 34)
TRUE_KW@[34; 38)
WHITESPACE@[38; 39)
BLOCK@[39; 42)
L_CURLY@[39; 40)
R_CURLY@[40; 41)
WHITESPACE@[41; 42)
ELSE_KW@[42; 46)
BLOCK@[46; 49)
WHITESPACE@[46; 47)
L_CURLY@[47; 48)
R_CURLY@[48; 49)
SEMI@[49; 50)
WHITESPACE@[50; 55)
IF_EXPR@[55; 91)
IF_KW@[55; 57)
LITERAL@[57; 63)
WHITESPACE@[57; 58)
TRUE_KW@[58; 62)
WHITESPACE@[62; 63)
BLOCK@[63; 66)
L_CURLY@[63; 64)
R_CURLY@[64; 65)
WHITESPACE@[65; 66)
ELSE_KW@[66; 70)
IF_EXPR@[70; 91)
WHITESPACE@[70; 71)
IF_KW@[71; 73)
LITERAL@[73; 80)
WHITESPACE@[73; 74)
FALSE_KW@[74; 79)
WHITESPACE@[79; 80)
BLOCK@[80; 83)
L_CURLY@[80; 81)
R_CURLY@[81; 82)
WHITESPACE@[82; 83)
ELSE_KW@[83; 87)
BLOCK@[87; 91)
WHITESPACE@[87; 88)
L_CURLY@[88; 89)
R_CURLY@[89; 90)
WHITESPACE@[90; 91)
R_CURLY@[91; 92)
WHITESPACE@[92; 93)