1
0
Fork 0

Add support for omitting semicolons after blocks

This commit is contained in:
Vili Sinervä 2025-01-31 18:53:35 +02:00
parent 000fa5b77b
commit a2f96cc8df
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996
2 changed files with 15 additions and 4 deletions

View file

@ -156,10 +156,21 @@ fn parse_block<'source>(pos: &mut usize, tokens: &[Token<'source>]) -> Expressio
loop {
expressions.push(parse_block_level_expressions(pos, tokens));
// Last expression left as return expression, if no semicolon is present
if peek(pos, tokens).text == "}" {
break;
}
consume_string(pos, tokens, ";");
// Blocks don't need to be followed by a semicolon, but can be
if peek(&mut (*pos - 1), tokens).text == "}" {
if peek(pos, tokens).text == ";" {
consume_string(pos, tokens, ";");
}
} else {
consume_string(pos, tokens, ";");
}
// If the last expression of the block ended in a semicolon, empty return
if peek(pos, tokens).text == "}" {
expressions.push(EmptyLiteral());
break;

View file

@ -450,7 +450,7 @@ fn test_omitting_semicolons() {
Box::new(Block(vec![Identifier("a")])),
None
),
Block(vec![Identifier("b")]),
Identifier("b"),
])
);
@ -463,7 +463,7 @@ fn test_omitting_semicolons() {
Box::new(Block(vec![Identifier("a")])),
None
),
Block(vec![Identifier("b")]),
Identifier("b"),
])
);
@ -476,7 +476,7 @@ fn test_omitting_semicolons() {
Box::new(Block(vec![Identifier("a")])),
Some(Box::new(Block(vec![Identifier("b")])))
),
Block(vec![Identifier("c")]),
Identifier("c"),
])
);