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

52
.vscode/launch.json vendored

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

69
src/lib/syntax/parser.rs

@ -530,11 +530,37 @@ impl Parser {
} }
}; };
self.pos += 1; self.pos += 1;
self.expect( let value = match self.get_token(self.pos)?.data {
TokenData::Punctuator(Punctuator::Colon), TokenData::Punctuator(Punctuator::Colon) => {
"object declaration", self.pos += 1;
)?; self.parse()?
let value = 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); 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