Skip to content

Commit 25a32ff

Browse files
authored
Merge pull request #33 from ratel-rust/for_loops
Adding support for if/for/while statements w/o body
2 parents 72e7266 + f774d37 commit 25a32ff

File tree

8 files changed

+203
-41
lines changed

8 files changed

+203
-41
lines changed

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ratel"
3-
version = "0.6.1"
3+
version = "0.6.2"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "JavaScript transpiler in Rust"

core/src/codegen.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,10 @@ impl Code for Statement {
679679
}
680680
},
681681

682+
Statement::Empty => {
683+
gen.write_byte(b';');
684+
}
685+
682686
Statement::Expression {
683687
ref value,
684688
} => {
@@ -771,7 +775,7 @@ impl Code for Statement {
771775
if let Some(ref alternate) = *alternate {
772776
gen.write_bytes(b" else ");
773777
gen.write(alternate);
774-
};
778+
}
775779
},
776780

777781
Statement::While {

core/src/grammar.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub struct VariableDeclarator {
273273

274274
#[derive(Debug, PartialEq, Clone)]
275275
pub enum Statement {
276+
Empty,
276277
Block {
277278
body: Vec<Statement>,
278279
},

core/src/parser.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,6 @@ impl<'a> Parser<'a> {
225225
Ok(Expression::Object(try!(self.object_member_list())))
226226
}
227227

228-
#[inline]
229-
fn block_or_statement(&mut self) -> Result<Statement> {
230-
match peek!(self) {
231-
BraceOpen => {
232-
self.consume();
233-
234-
Ok(Statement::Block {
235-
body: try!(self.block_body_tail())
236-
})
237-
},
238-
_ => {
239-
let token = next!(self);
240-
self.expression_statement(token)
241-
}
242-
}
243-
}
244-
245228
#[inline]
246229
fn block_statement(&mut self) -> Result<Statement> {
247230
Ok(Statement::Block {
@@ -757,21 +740,16 @@ impl<'a> Parser<'a> {
757740

758741
expect!(self, ParenClose);
759742

760-
let consequent = Box::new(try!(self.block_or_statement()));
743+
let token = next!(self);
744+
let consequent = Box::new(try!(self.statement(token)));
761745

762746
let alternate = match peek!(self) {
763747
Else => {
764748
self.consume();
765749

766-
match peek!(self) {
767-
If => {
768-
self.consume();
769-
770-
Some(Box::new(try!(self.if_statement())))
771-
},
750+
let token = next!(self);
772751

773-
_ => Some(Box::new(try!(self.block_or_statement())))
774-
}
752+
Some(Box::new(try!(self.statement(token))))
775753
},
776754

777755
_ => None
@@ -792,9 +770,12 @@ impl<'a> Parser<'a> {
792770

793771
expect!(self, ParenClose);
794772

773+
let token = next!(self);
774+
let body = Box::new(try!(self.statement(token)));
775+
795776
Ok(Statement::While {
796777
test: test,
797-
body: Box::new(try!(self.block_or_statement())),
778+
body: body,
798779
})
799780
}
800781

@@ -854,11 +835,14 @@ impl<'a> Parser<'a> {
854835
expect!(self, ParenClose);
855836
}
856837

838+
let token = next!(self);
839+
let body = Box::new(try!(self.statement(token)));
840+
857841
Ok(Statement::For {
858842
init: init,
859843
test: test,
860844
update: update,
861-
body: Box::new(try!(self.block_or_statement())),
845+
body: body,
862846
})
863847
}
864848

@@ -868,10 +852,13 @@ impl<'a> Parser<'a> {
868852

869853
expect!(self, ParenClose);
870854

855+
let token = next!(self);
856+
let body = Box::new(try!(self.statement(token)));
857+
871858
Ok(Statement::ForIn {
872859
left: left,
873860
right: right,
874-
body: Box::new(try!(self.block_or_statement())),
861+
body: body,
875862
})
876863
}
877864

@@ -880,10 +867,13 @@ impl<'a> Parser<'a> {
880867

881868
expect!(self, ParenClose);
882869

870+
let token = next!(self);
871+
let body = Box::new(try!(self.statement(token)));
872+
883873
Ok(Statement::ForIn {
884874
left: left,
885875
right: right,
886-
body: Box::new(try!(self.block_or_statement())),
876+
body: body,
887877
})
888878
}
889879

@@ -892,10 +882,13 @@ impl<'a> Parser<'a> {
892882

893883
expect!(self, ParenClose);
894884

885+
let token = next!(self);
886+
let body = Box::new(try!(self.statement(token)));
887+
895888
Ok(Statement::ForOf {
896889
left: left,
897890
right: right,
898-
body: Box::new(try!(self.block_or_statement())),
891+
body: body,
899892
})
900893
}
901894

@@ -1058,7 +1051,7 @@ impl<'a> Parser<'a> {
10581051
#[inline]
10591052
fn statement(&mut self, token: Token) -> Result<Statement> {
10601053
match token {
1061-
Semicolon => Ok(Statement::Transparent { body: Vec::new() }),
1054+
Semicolon => Ok(Statement::Empty),
10621055
BraceOpen => self.block_statement(),
10631056
Declaration(kind) => self.variable_declaration_statement(kind),
10641057
Return => self.return_statement(),

core/src/transformer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,10 @@ impl Transformable for Statement {
888888
return;
889889
},
890890

891+
Statement::Empty => {
892+
return;
893+
},
894+
891895
Statement::Return {
892896
ref mut value,
893897
} => {

0 commit comments

Comments
 (0)