Skip to content

Commit 72e7266

Browse files
authored
Merge pull request #34 from ratel-rust/class_expressions
Fixes and some refactor
2 parents 7793122 + 3f9a279 commit 72e7266

File tree

12 files changed

+346
-312
lines changed

12 files changed

+346
-312
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.0"
3+
version = "0.6.1"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "JavaScript transpiler in Rust"

core/src/codegen.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ptr;
22
use std::io::Write;
33

44
use grammar::*;
5-
use grammar::OperatorType::*;
5+
use operator::OperatorKind;
66
use owned_slice::OwnedSlice;
77

88
/// The `Generator` is a wrapper around an owned `String` that's used to
@@ -160,9 +160,11 @@ impl<T: Code> Code for Option<T> {
160160
}
161161
}
162162

163-
impl Code for OperatorType {
163+
impl Code for OperatorKind {
164164
#[inline]
165165
fn to_code(&self, gen: &mut Generator) {
166+
use ::operator::OperatorKind::*;
167+
166168
gen.write_bytes(match *self {
167169
FatArrow => b"=>",
168170
Accessor => b".",
@@ -451,6 +453,7 @@ impl Code for Expression {
451453
..
452454
} => {
453455
let bp = self.binding_power();
456+
let spacing = operator.is_word() || !gen.minify;
454457

455458
if left.binding_power() < bp {
456459
gen.write_byte(b'(');
@@ -459,9 +462,14 @@ impl Code for Expression {
459462
} else {
460463
gen.write(left);
461464
}
462-
gen.write_min(b" ", b"");
465+
466+
if spacing {
467+
gen.write_byte(b' ');
468+
}
463469
gen.write(operator);
464-
gen.write_min(b" ", b"");
470+
if spacing {
471+
gen.write_byte(b' ');
472+
}
465473

466474
if right.needs_parens(bp) {
467475
gen.write_byte(b'(');
@@ -477,6 +485,9 @@ impl Code for Expression {
477485
ref operand,
478486
} => {
479487
gen.write(operator);
488+
if operator.is_word() {
489+
gen.write_byte(b' ');
490+
}
480491
gen.write(operand);
481492
},
482493

@@ -729,6 +740,24 @@ impl Code for Statement {
729740
gen.new_line();
730741
},
731742

743+
Statement::Class {
744+
ref name,
745+
ref extends,
746+
ref body,
747+
} => {
748+
gen.new_line();
749+
gen.write_bytes(b"class ");
750+
gen.write(name);
751+
if let &Some(ref super_class) = extends {
752+
gen.write_bytes(b" extends ");
753+
gen.write(super_class);
754+
}
755+
gen.write_min(b" {", b"{");
756+
gen.write_block(body);
757+
gen.write_byte(b'}');
758+
gen.new_line();
759+
},
760+
732761
Statement::If {
733762
ref test,
734763
ref consequent,
@@ -799,24 +828,6 @@ impl Code for Statement {
799828
gen.write(body);
800829
},
801830

802-
Statement::Class {
803-
ref name,
804-
ref extends,
805-
ref body,
806-
} => {
807-
gen.new_line();
808-
gen.write_bytes(b"class ");
809-
gen.write(name);
810-
if let &Some(ref super_class) = extends {
811-
gen.write_bytes(b" extends ");
812-
gen.write(super_class);
813-
}
814-
gen.write_min(b" {", b"{");
815-
gen.write_block(body);
816-
gen.write_byte(b'}');
817-
gen.new_line();
818-
},
819-
820831
Statement::Throw {
821832
ref value,
822833
} => {

core/src/grammar.rs

Lines changed: 10 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use owned_slice::OwnedSlice;
2+
use operator::OperatorKind;
23

34
#[derive(Debug, PartialEq, Clone, Copy)]
45
pub enum Value {
@@ -18,214 +19,6 @@ pub struct Parameter {
1819
pub default: Option<Box<Expression>>
1920
}
2021

21-
#[derive(Debug, PartialEq, Clone, Copy)]
22-
pub enum OperatorType {
23-
FatArrow, // … => …
24-
Accessor, // … . …
25-
New, // new …
26-
Increment, // ++ … | … ++
27-
Decrement, // -- … | … --
28-
LogicalNot, // ! …
29-
BitwiseNot, // ~ …
30-
Typeof, // typeof …
31-
Void, // void …
32-
Delete, // delete …
33-
Multiplication, // … * …
34-
Division, // … / …
35-
Remainder, // … % …
36-
Exponent, // … ** …
37-
Addition, // … + … | + …
38-
Substraction, // … - … | - …
39-
BitShiftLeft, // … << …
40-
BitShiftRight, // … >> …
41-
UBitShiftRight, // … >>> …
42-
Lesser, // … < …
43-
LesserEquals, // … <= …
44-
Greater, // … > …
45-
GreaterEquals, // … >= …
46-
Instanceof, // … instanceof …
47-
In, // … in …
48-
StrictEquality, // … === …
49-
StrictInequality, // … !== …
50-
Equality, // … == …
51-
Inequality, // … != …
52-
BitwiseAnd, // … & …
53-
BitwiseXor, // … ^ …
54-
BitwiseOr, // … | …
55-
LogicalAnd, // … && …
56-
LogicalOr, // … || …
57-
Conditional, // … ? … : …
58-
Assign, // … = …
59-
AddAssign, // … += …
60-
SubstractAssign, // … -= …
61-
ExponentAssign, // … **= …
62-
MultiplyAssign, // … *= …
63-
DivideAssign, // … /= …
64-
RemainderAssign, // … %= …
65-
BSLAssign, // … <<= …
66-
BSRAssign, // … >>= …
67-
UBSRAssign, // … >>>= …
68-
BitAndAssign, // … &= …
69-
BitXorAssign, // … ^= …
70-
BitOrAssign, // … |= …
71-
Spread, // ... …
72-
}
73-
use self::OperatorType::*;
74-
75-
impl OperatorType {
76-
/// According to the Operator Precedence Table
77-
/// Note: Unary opearotrs default to 15!
78-
pub fn binding_power(&self) -> u8 {
79-
match *self {
80-
FatArrow |
81-
Accessor => 18,
82-
83-
New => 17,
84-
85-
Increment |
86-
Decrement => 16,
87-
88-
LogicalNot |
89-
BitwiseNot |
90-
Typeof |
91-
Void |
92-
Delete => 15,
93-
94-
Multiplication |
95-
Division |
96-
Remainder |
97-
Exponent => 14,
98-
99-
Addition |
100-
Substraction => 13,
101-
102-
BitShiftLeft |
103-
BitShiftRight |
104-
UBitShiftRight => 12,
105-
106-
Lesser |
107-
LesserEquals |
108-
Greater |
109-
GreaterEquals |
110-
Instanceof |
111-
In => 11,
112-
113-
StrictEquality |
114-
StrictInequality |
115-
Equality |
116-
Inequality => 10,
117-
118-
BitwiseAnd => 9,
119-
BitwiseXor => 8,
120-
BitwiseOr => 7,
121-
LogicalAnd => 6,
122-
LogicalOr => 5,
123-
Conditional => 4,
124-
125-
Assign |
126-
AddAssign |
127-
SubstractAssign |
128-
ExponentAssign |
129-
MultiplyAssign |
130-
DivideAssign |
131-
RemainderAssign |
132-
BSLAssign |
133-
BSRAssign |
134-
UBSRAssign |
135-
BitAndAssign |
136-
BitXorAssign |
137-
BitOrAssign => 3,
138-
139-
Spread => 1,
140-
}
141-
}
142-
143-
pub fn prefix(&self) -> bool {
144-
match *self {
145-
LogicalNot |
146-
BitwiseNot |
147-
Typeof |
148-
Void |
149-
Delete |
150-
New |
151-
Spread |
152-
Increment |
153-
Decrement |
154-
Addition |
155-
Substraction => true,
156-
157-
_ => false
158-
}
159-
}
160-
161-
pub fn infix(&self) -> bool {
162-
match *self {
163-
FatArrow |
164-
Accessor |
165-
Multiplication |
166-
Division |
167-
Remainder |
168-
Exponent |
169-
StrictEquality |
170-
StrictInequality |
171-
Equality |
172-
Inequality |
173-
Lesser |
174-
LesserEquals |
175-
Greater |
176-
GreaterEquals |
177-
Instanceof |
178-
In |
179-
BitShiftLeft |
180-
BitShiftRight |
181-
UBitShiftRight |
182-
BitwiseAnd |
183-
BitwiseXor |
184-
BitwiseOr |
185-
LogicalAnd |
186-
LogicalOr |
187-
Conditional |
188-
Addition |
189-
Substraction |
190-
Assign |
191-
AddAssign |
192-
SubstractAssign |
193-
ExponentAssign |
194-
MultiplyAssign |
195-
DivideAssign |
196-
RemainderAssign |
197-
BSLAssign |
198-
BSRAssign |
199-
UBSRAssign |
200-
BitAndAssign |
201-
BitXorAssign |
202-
BitOrAssign => true,
203-
204-
_ => false
205-
}
206-
}
207-
208-
pub fn assignment(&self) -> bool {
209-
match *self {
210-
Assign |
211-
AddAssign |
212-
SubstractAssign |
213-
ExponentAssign |
214-
MultiplyAssign |
215-
DivideAssign |
216-
RemainderAssign |
217-
BSLAssign |
218-
BSRAssign |
219-
UBSRAssign |
220-
BitAndAssign |
221-
BitXorAssign |
222-
BitOrAssign => true,
223-
224-
_ => false
225-
}
226-
}
227-
}
228-
22922
#[derive(Debug, PartialEq, Clone)]
23023
pub enum Expression {
23124
This,
@@ -257,16 +50,16 @@ pub enum Expression {
25750
},
25851
Binary {
25952
parenthesized: bool,
260-
operator: OperatorType,
53+
operator: OperatorKind,
26154
left: Box<Expression>,
26255
right: Box<Expression>,
26356
},
26457
Prefix {
265-
operator: OperatorType,
58+
operator: OperatorKind,
26659
operand: Box<Expression>,
26760
},
26861
Postfix {
269-
operator: OperatorType,
62+
operator: OperatorKind,
27063
operand: Box<Expression>,
27164
},
27265
Conditional {
@@ -323,7 +116,7 @@ impl Expression {
323116
}
324117

325118
#[inline]
326-
pub fn binary<E: Into<Expression>>(left: E, operator: OperatorType, right: E) -> Self {
119+
pub fn binary<E: Into<Expression>>(left: E, operator: OperatorKind, right: E) -> Self {
327120
Expression::Binary {
328121
parenthesized: false,
329122
operator: operator,
@@ -511,6 +304,11 @@ pub enum Statement {
511304
params: Vec<Parameter>,
512305
body: Vec<Statement>,
513306
},
307+
Class {
308+
name: OwnedSlice,
309+
extends: Option<OwnedSlice>,
310+
body: Vec<ClassMember>,
311+
},
514312
If {
515313
test: Expression,
516314
consequent: Box<Statement>,
@@ -536,11 +334,6 @@ pub enum Statement {
536334
right: Expression,
537335
body: Box<Statement>,
538336
},
539-
Class {
540-
name: OwnedSlice,
541-
extends: Option<OwnedSlice>,
542-
body: Vec<ClassMember>,
543-
},
544337
Throw {
545338
value: Expression
546339
},

0 commit comments

Comments
 (0)