Skip to content

Commit

Permalink
fix: More object literal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 29, 2023
1 parent aae9d63 commit 1a2f537
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 31 deletions.
17 changes: 8 additions & 9 deletions server/src/lang/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,9 @@ impl<'a> Parser<'a> {
match_next!(self, kw!(Break), break_statement);
match_next!(self, kw!(Continue), continue_statement);
match_next!(self, kw!(Return), return_statement);
if self.peek_next_all_of(&[
sym!(LeftBrace),
Identifier { dollar: false },
sym!(Colon),
]) {
if self.peek_next_all_of(&[sym!(LeftBrace), Identifier { dollar: false }, sym!(Colon)])
|| self.peek_next_all_of(&[sym!(LeftBrace), sym!(RightBrace)])
{
return self.expression_statement();
}
match_next!(self, sym!(LeftBrace), block);
Expand Down Expand Up @@ -267,18 +265,19 @@ impl<'a> Parser<'a> {
fn block(&mut self) -> ParseResult<StmtId> {
let mut statements: Vec<StmtId> = vec![];

let opening_brace = self.peek_bw(1);

while !self.cmp_tok(&sym!(RightBrace)) && !self.is_at_end() {
statements.push(self.declaration()?);
}

let closing_brace = self.peek_bw(1);

self.expected(sym!(RightBrace))?;

Ok(self.arena.statement(Stmt::Block {
body: statements.clone(),
span: self.get_merged_span(
self.arena.get_statement(statements[0]),
self.arena.get_statement(statements[statements.len() - 1]),
),
span: self.get_merged_span(&opening_brace.span, &closing_brace.span),
}))
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/lang/tests/generic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod array_literal;
pub mod binary;
pub mod function_call;
pub mod grouping;
pub mod number_literal;
pub mod object_literal;
pub mod array_literal;
pub mod unary;
pub mod variable;
79 changes: 58 additions & 21 deletions server/src/lang/tests/generic/object_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ use crate::assert_parsing;

#[cfg(test)]
assert_parsing! {
plain_declare: {
"var $obj = { a : 1, b : `q` };" => {
empty_expr: {
"{};" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Literal",
"raw": "",
"value": {
"type": "Object",
"value": []
}
}
}
]
}
},
empty_declare: {
"var $obj = {};" => {
"type": "Stmt::Program",
"body": [
{
Expand All @@ -21,24 +39,7 @@ assert_parsing! {
"raw": "",
"value": {
"type": "Object",
"value": [
{
"key": "b",
"value": {
"raw": "q",
"type": "Expr::Literal",
"value": "Str(\"q\")"
}
},
{
"key": "a",
"value": {
"raw": "1",
"type": "Expr::Literal",
"value": "Num(1.0)"
}
}
]
"value": []
}
}
}
Expand Down Expand Up @@ -79,5 +80,41 @@ assert_parsing! {
}
]
}
}
},
plain_declare: {
"var $obj = { a : 1, b : `q` };" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Declaration",
"variable": "$obj",
"expr": {
"type": "Expr::Literal",
"raw": "",
"value": {
"type": "Object",
"value": [
{
"key": "b",
"value": {
"raw": "q",
"type": "Expr::Literal",
"value": "Str(\"q\")"
}
},
{
"key": "a",
"value": {
"raw": "1",
"type": "Expr::Literal",
"value": "Num(1.0)"
}
}
]
}
}
}
]
}
}
}

0 comments on commit 1a2f537

Please sign in to comment.