Browse Source

short function syntax fixes #152 (#199)

* short function syntax

* updating parser plus adding test
pull/203/head
Jason Williams 5 years ago committed by GitHub
parent
commit
3465e6bbca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      .vscode/launch.json
  2. 63
      src/lib/syntax/parser.rs
  3. 12
      tests/js/test.js

10
.vscode/launch.json vendored

@ -9,21 +9,19 @@
"request": "launch", "request": "launch",
"name": "Launch", "name": "Launch",
"args": [], "args": [],
"program": "./target/debug/bin", "program": "./target/debug/boa",
"cwd": "${workspaceRoot}", "cwd": "${workspaceRoot}",
"stopOnEntry": false, "stopOnEntry": false,
"sourceLanguages": [ "sourceLanguages": ["rust"]
"rust"
]
}, },
{ {
"name": "(Windows) Launch", "name": "(Windows) Launch",
"type": "cppvsdbg", "type": "cppvsdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/target/debug/bin.exe", "program": "${workspaceFolder}/target/debug/boa.exe",
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"environment": [], "environment": []
} }
] ]
} }

63
src/lib/syntax/parser.rs

@ -529,12 +529,38 @@ impl Parser {
)) ))
} }
}; };
self.pos += 1;
let value = match self.get_token(self.pos)?.data {
TokenData::Punctuator(Punctuator::Colon) => {
self.pos += 1;
self.parse()?
}
TokenData::Punctuator(Punctuator::OpenParen) => {
self.pos += 1; self.pos += 1;
self.expect( self.expect(
TokenData::Punctuator(Punctuator::CloseParen),
"Method Block",
)?;
self.pos += 1; // {
let expr = self.parse()?;
self.pos += 1;
mk!(
self,
ExprDef::FunctionDecl(None, vec!(), Box::new(expr)),
token
)
}
_ => {
return Err(ParseError::Expected(
vec![
TokenData::Punctuator(Punctuator::Colon), TokenData::Punctuator(Punctuator::Colon),
TokenData::Punctuator(Punctuator::OpenParen),
],
tk,
"object declaration", "object declaration",
)?; ))
let value = self.parse()?; }
};
map.insert(name, value); map.insert(name, value);
self.pos += 1; self.pos += 1;
} }
@ -554,6 +580,13 @@ impl Parser {
self.pos += 1; self.pos += 1;
mk!(self, ExprDef::Block(exprs), token) mk!(self, ExprDef::Block(exprs), token)
} }
// Empty Block
TokenData::Punctuator(Punctuator::CloseBlock)
if self.get_token(self.pos.wrapping_sub(2))?.data
== TokenData::Punctuator(Punctuator::OpenBlock) =>
{
mk!(self, ExprDef::Block(vec!()), token)
}
TokenData::Punctuator(Punctuator::Sub) => mk!( TokenData::Punctuator(Punctuator::Sub) => mk!(
self, self,
ExprDef::UnaryOp(UnaryOp::Minus, Box::new(self.parse()?)) ExprDef::UnaryOp(UnaryOp::Minus, Box::new(self.parse()?))
@ -889,6 +922,32 @@ mod tests {
))))], ))))],
); );
} }
#[test]
fn check_object() {
// Testing short function syntax
let mut object_properties: BTreeMap<String, Expr> = BTreeMap::new();
object_properties.insert(
String::from("a"),
Expr::new(ExprDef::Const(Const::Bool(true))),
);
object_properties.insert(
String::from("b"),
Expr::new(ExprDef::FunctionDecl(
None,
vec![],
Box::new(Expr::new(ExprDef::Block(vec![]))),
)),
);
check_parser(
"{
a: true,
b() {}
};
",
&[Expr::new(ExprDef::ObjectDecl(Box::new(object_properties)))],
);
}
#[test] #[test]
fn check_array() { fn check_array() {

12
tests/js/test.js

@ -1,6 +1,8 @@
/// let a = {
// Use this file to test your changes to boa. a: true,
/// b() {
return 2;
}
};
let a = Boolean(0); console.log(a["b"]());
typeof a;

Loading…
Cancel
Save