Implemented rescoping with the do keyword.

This commit is contained in:
Timothy Jones 2010-10-25 00:02:39 +13:00
parent f52ef98cee
commit d096f69c78
7 changed files with 207 additions and 183 deletions

View File

@ -38,7 +38,7 @@
return new Literal($1);
})
],
Expression: [o("Value"), o("Invocation"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Existence"), o("Comment")],
Expression: [o("Value"), o("Invocation"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Existence"), o("Comment"), o("Do")],
Block: [
o("INDENT Body OUTDENT", function() {
return $2;
@ -106,6 +106,11 @@
return new Existence($1);
})
],
Do: [
o("DO Code", function() {
return new Call($2, $2.params);
})
],
Code: [
o("PARAM_START ParamList PARAM_END FuncGlyph Block", function() {
return new Code($2, $5, $4);

View File

@ -585,7 +585,7 @@
};
return Lexer;
})();
JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'try', 'catch', 'finally', 'class', 'extends', 'super'];
JS_KEYWORDS = ['true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'try', 'catch', 'finally', 'class', 'extends', 'super', 'do'];
COFFEE_KEYWORDS = ['then', 'unless', 'until', 'loop', 'of', 'by', 'when'];
for (op in COFFEE_ALIASES = {
and: '&&',
@ -600,7 +600,7 @@
}) {
COFFEE_KEYWORDS.push(op);
}
RESERVED = ['case', 'default', 'do', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice'];
RESERVED = ['case', 'default', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice'];
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED);
IDENTIFIER = /^([$A-Za-z_][$\w]*)([^\n\S]*:(?!:))?/;
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;

View File

@ -1728,7 +1728,7 @@
}).call(this);
Push = {
wrap: function(name, expressions) {
if (expressions.empty() || (last(expressions.expressions)).containsPureStatement()) {
if (expressions.empty() || last(expressions.expressions).containsPureStatement()) {
return expressions;
}
return expressions.push(new Call(new Value(new Literal(name), [new Accessor(new Literal('push'))]), [expressions.pop()]));

File diff suppressed because one or more lines are too long

View File

@ -103,6 +103,7 @@ grammar =
o "Class"
o "Existence"
o "Comment"
o "Do"
]
# An indented block of expressions. Note that the [Rewriter](rewriter.html)
@ -169,6 +170,10 @@ grammar =
Existence: [
o "Expression ?", -> new Existence $1
]
Do: [
o "DO Code", -> new Call $2, $2.params
]
# The **Code** node is the function literal. It's defined by an indented block
# of **Expressions** preceded by a function arrow, with an optional parameter

View File

@ -515,7 +515,7 @@ JS_KEYWORDS = [
'new', 'delete', 'typeof', 'in', 'instanceof'
'return', 'throw', 'break', 'continue', 'debugger'
'if', 'else', 'switch', 'for', 'while', 'try', 'catch', 'finally'
'class', 'extends', 'super'
'class', 'extends', 'super', 'do'
]
# CoffeeScript-only keywords.
@ -535,7 +535,7 @@ COFFEE_KEYWORDS.push op for all op of COFFEE_ALIASES =
# used by CoffeeScript internally. We throw an error when these are encountered,
# to avoid having a JavaScript error at runtime.
RESERVED = [
'case', 'default', 'do', 'function', 'var', 'void', 'with'
'case', 'default', 'function', 'var', 'void', 'with'
'const', 'let', 'enum', 'export', 'import', 'native'
'__hasProp', '__extends', '__slice'
]

View File

@ -345,3 +345,13 @@ eq ok, new ->
ok
### Should `return` implicitly ###
### even with trailing comments. ###
# Rescoping with the `do` keyword.
v1 = 1
v2 = 2
do (v1) ->
v1 = 3
v2 = 4
ok v1 is 1
ok v2 is 4