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. 54
      .vscode/launch.json
  2. 69
      src/lib/syntax/parser.rs
  3. 12
      tests/js/test.js

54
.vscode/launch.json vendored

@ -1,29 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"args": [],
"program": "./target/debug/bin",
"cwd": "${workspaceRoot}",
"stopOnEntry": false,
"sourceLanguages": [
"rust"
]
},
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/bin.exe",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
}
]
}
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"args": [],
"program": "./target/debug/boa",
"cwd": "${workspaceRoot}",
"stopOnEntry": false,
"sourceLanguages": ["rust"]
},
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/boa.exe",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": []
}
]
}

69
src/lib/syntax/parser.rs

@ -530,11 +530,37 @@ impl Parser {
}
};
self.pos += 1;
self.expect(
TokenData::Punctuator(Punctuator::Colon),
"object declaration",
)?;
let value = self.parse()?;
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.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::OpenParen),
],
tk,
"object declaration",
))
}
};
map.insert(name, value);
self.pos += 1;
}
@ -554,6 +580,13 @@ impl Parser {
self.pos += 1;
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!(
self,
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]
fn check_array() {

12
tests/js/test.js

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

Loading…
Cancel
Save