Skip to content

Commit 750ffac

Browse files
authored
Merge pull request #29 from ratel-rust/keywords-as-keys
Closes #23
2 parents 71c43b9 + a0d2fbe commit 750ffac

File tree

8 files changed

+394
-107
lines changed

8 files changed

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

core/src/codegen.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,14 @@ impl Code for ClassMember {
561561

562562
ClassMember::Method {
563563
is_static,
564-
ref name,
564+
ref key,
565565
ref params,
566566
ref body,
567567
} => {
568568
if is_static {
569569
gen.write_bytes(b"static ");
570570
}
571-
gen.write(name);
571+
gen.write(key);
572572
gen.write_byte(b'(');
573573
gen.write_list(params);
574574
gen.write_min(b") {", b"){");
@@ -578,13 +578,13 @@ impl Code for ClassMember {
578578

579579
ClassMember::Property {
580580
is_static,
581-
ref name,
581+
ref key,
582582
ref value,
583583
} => {
584584
if is_static {
585585
gen.write_bytes(b"static ");
586586
}
587-
gen.write(name);
587+
gen.write(key);
588588
gen.write_min(b" = ", b"=");
589589
gen.write(value);
590590
gen.write_byte(b';');
@@ -593,6 +593,24 @@ impl Code for ClassMember {
593593
}
594594
}
595595

596+
impl Code for ClassKey {
597+
fn to_code(&self, gen: &mut Generator) {
598+
match *self {
599+
ClassKey::Literal(ref name) => gen.write(name),
600+
601+
ClassKey::Number(ref name) => gen.write(name),
602+
603+
ClassKey::Binary(ref num) => gen.write(num),
604+
605+
ClassKey::Computed(ref expr) => {
606+
gen.write_byte(b'[');
607+
gen.write(expr);
608+
gen.write_byte(b']');
609+
}
610+
}
611+
}
612+
}
613+
596614
impl Code for VariableDeclarator {
597615
#[inline]
598616
fn to_code(&self, gen: &mut Generator) {
@@ -642,7 +660,13 @@ impl Code for Statement {
642660
Statement::Expression {
643661
ref value,
644662
} => {
645-
gen.write(value);
663+
if value.is_allowed_as_bare_statement() {
664+
gen.write(value);
665+
} else {
666+
gen.write_byte(b'(');
667+
gen.write(value);
668+
gen.write_byte(b')');
669+
}
646670
gen.write_byte(b';');
647671
},
648672

core/src/grammar.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ impl Expression {
367367
_ => false
368368
}
369369
}
370+
371+
#[inline]
372+
pub fn is_allowed_as_bare_statement(&self) -> bool {
373+
match *self {
374+
Expression::Object(_) => false,
375+
Expression::Function { .. } => false,
376+
377+
_ => true,
378+
}
379+
}
370380
}
371381

372382
impl From<&'static str> for Expression {
@@ -421,17 +431,36 @@ pub enum ClassMember {
421431
},
422432
Method {
423433
is_static: bool,
424-
name: OwnedSlice,
434+
key: ClassKey,
425435
params: Vec<Parameter>,
426436
body: Vec<Statement>,
427437
},
428438
Property {
429439
is_static: bool,
430-
name: OwnedSlice,
440+
key: ClassKey,
431441
value: Expression,
432442
}
433443
}
434444

445+
#[derive(Debug, PartialEq, Clone)]
446+
pub enum ClassKey {
447+
Computed(Expression),
448+
Literal(OwnedSlice),
449+
Number(OwnedSlice),
450+
Binary(u64),
451+
}
452+
453+
impl ClassKey {
454+
#[inline]
455+
pub fn is_constructor(&self) -> bool {
456+
match *self {
457+
ClassKey::Literal(ref name) => name.as_str() == "constructor",
458+
459+
_ => false
460+
}
461+
}
462+
}
463+
435464
#[derive(Debug, PartialEq, Clone, Copy)]
436465
pub enum VariableDeclarationKind {
437466
Var,

core/src/lexicon.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,51 @@ pub enum Token {
6565
Literal(Value),
6666
Template(TemplateKind),
6767
}
68+
69+
impl Token {
70+
pub fn as_word(&self) -> Option<&'static str> {
71+
use self::Token::*;
72+
use grammar::OperatorType::*;
73+
use grammar::Value::*;
74+
75+
match *self {
76+
Break => Some("break"),
77+
Do => Some("do"),
78+
Case => Some("case"),
79+
Else => Some("else"),
80+
Catch => Some("catch"),
81+
Export => Some("export"),
82+
Class => Some("class"),
83+
Extends => Some("extends"),
84+
Return => Some("return"),
85+
While => Some("while"),
86+
Finally => Some("finally"),
87+
Super => Some("super"),
88+
With => Some("with"),
89+
Continue => Some("continue"),
90+
For => Some("for"),
91+
Switch => Some("switch"),
92+
Yield => Some("yield"),
93+
Debugger => Some("debugger"),
94+
Function => Some("function"),
95+
This => Some("this"),
96+
Default => Some("default"),
97+
If => Some("if"),
98+
Throw => Some("throw"),
99+
Import => Some("import"),
100+
Try => Some("try"),
101+
Static => Some("static"),
102+
Operator(New) => Some("new"),
103+
Operator(Typeof) => Some("typeof"),
104+
Operator(Void) => Some("void"),
105+
Operator(Delete) => Some("delte"),
106+
Operator(Instanceof) => Some("instanceof"),
107+
Literal(True) => Some("true"),
108+
Literal(False) => Some("false"),
109+
Literal(Null) => Some("null"),
110+
Literal(Undefined) => Some("undefined"),
111+
112+
_ => None,
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)