From 7a0d95c61243f8bd90750e2f411edf814666d5df Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 14 Aug 2010 19:56:00 -0400 Subject: [PATCH] Issue #575. Allowing implicit objects to force implicit calls. --- Cakefile | 3 +-- lib/rewriter.js | 30 +++++++++++++++++++++++------- src/grammar.coffee | 3 +-- src/lexer.coffee | 3 +-- src/nodes.coffee | 16 ++++------------ src/rewriter.coffee | 22 +++++++++++++++++----- test/test_literals.coffee | 14 ++++++++++++++ 7 files changed, 61 insertions(+), 30 deletions(-) diff --git a/Cakefile b/Cakefile index 9273c3bf..bc051908 100644 --- a/Cakefile +++ b/Cakefile @@ -97,10 +97,9 @@ task 'test', 'run the CoffeeScript language test suite', -> passedTests = failedTests = 0 startTime = new Date originalOk = ok - helpers.extend global, { + helpers.extend global, ok: (args...) -> passedTests += 1; originalOk(args...) CoffeeScript: CoffeeScript - } process.on 'exit', -> time = ((new Date - startTime) / 1000).toFixed(2) message = "passed #{passedTests} tests in #{time} seconds#{reset}" diff --git a/lib/rewriter.js b/lib/rewriter.js index f0ae8b45..ee049802 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -1,5 +1,5 @@ (function() { - var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair; + var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, helpers, include, pair; var __hasProp = Object.prototype.hasOwnProperty; if (typeof process !== "undefined" && process !== null) { _a = require('./helpers'); @@ -145,7 +145,7 @@ var stack; stack = []; return this.scanTokens(function(token, i) { - var action, condition, idx, last; + var action, condition, idx, last, tok; if (include(EXPRESSION_START, token[0])) { stack.push((token[0] === 'INDENT' && (this.tag(i - 1) === '{')) ? '{' : token[0]); } @@ -156,7 +156,9 @@ if (token[0] === ':' && (!last || last[0] !== '{')) { stack.push('{'); idx = this.tag(i - 2) === '@' ? i - 2 : i - 1; - this.tokens.splice(idx, 0, ['{', '{', token[2]]); + tok = ['{', '{', token[2]]; + tok.generated = true; + this.tokens.splice(idx, 0, tok); condition = function(token, i) { var _c, _d, _e, one, three, two; _c = this.tokens.slice(i + 1, i + 4); @@ -178,20 +180,33 @@ }); }; Rewriter.prototype.addImplicitParentheses = function() { + var classLine; + classLine = false; return this.scanTokens(function(token, i) { - var _c, action, condition, prev; + var _c, action, callObject, condition, idx, next, prev; + if (token[0] === 'CLASS') { + classLine = true; + } prev = this.tokens[i - 1]; - if (prev && prev.spaced && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, token[0]) && !(token[0] === 'UNARY' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c)))) { + next = this.tokens[i + 1]; + idx = 1; + callObject = !classLine && token[0] === 'INDENT' && next && next.generated && next[0] === '{' && prev && include(IMPLICIT_FUNC, prev[0]); + if (callObject) { + idx = 2; + } + if (include(LINEBREAKS, token[0])) { + classLine = false; + } + if (prev && (prev.spaced && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, token[0]) && !(token[0] === 'UNARY' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c)))) || callObject) { this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]); condition = function(token, i) { return (!token.generated && this.tokens[i - 1][0] !== ',' && include(IMPLICIT_END, token[0]) && !(token[0] === 'INDENT' && (include(IMPLICIT_BLOCK, this.tag(i - 1)) || this.tag(i - 2) === 'CLASS'))) || token[0] === 'PROPERTY_ACCESS' && this.tag(i - 1) === 'OUTDENT'; }; action = function(token, i) { - var idx; idx = token[0] === 'OUTDENT' ? i + 1 : i; return this.tokens.splice(idx, 0, ['CALL_END', ')', token[2]]); }; - this.detectEnd(i + 1, condition, action); + this.detectEnd(i + idx, condition, action); return 2; } return 1; @@ -380,4 +395,5 @@ IMPLICIT_END = ['POST_IF', 'POST_UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'TERMINATOR', 'INDENT']; SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN']; SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']; + LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT']; })(); diff --git a/src/grammar.coffee b/src/grammar.coffee index 9803f590..4f531acc 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -600,9 +600,8 @@ for name, alternatives of grammar # rules, and the name of the root. Reverse the operators because Jison orders # precedence from low to high, and we have it high to low # (as in [Yacc](http://dinosaur.compilertools.net/yacc/index.html)). -exports.parser = new Parser { +exports.parser = new Parser tokens: tokens.join ' ' bnf: grammar operators: operators.reverse() startSymbol: 'Root' -} diff --git a/src/lexer.coffee b/src/lexer.coffee index b41bd576..95b0c6d5 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -579,11 +579,10 @@ CALLABLE = ['IDENTIFIER', 'SUPER', ')', ']', '}', 'STRING', '@', 'THIS', '?', ': LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR'] # Conversions from CoffeeScript operators into JavaScript ones. -CONVERSIONS = { +CONVERSIONS = 'and': '&&' 'or': '||' 'is': '==' 'isnt': '!=' 'not': '!' '===': '==' -} diff --git a/src/nodes.coffee b/src/nodes.coffee index 526f86a2..391cd2c6 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1055,10 +1055,9 @@ exports.WhileNode = class WhileNode extends BaseNode exports.OpNode = class OpNode extends BaseNode # The map of conversions from CoffeeScript to JavaScript symbols. - CONVERSIONS: { + CONVERSIONS: '==': '===' '!=': '!==' - } # The list of operators for which we perform # [Python-style comparison chaining](http://docs.python.org/reference/expressions.html#notin). @@ -1472,8 +1471,7 @@ exports.IfNode = class IfNode extends BaseNode # generation to generate other combinations of nodes. The **PushNode** creates # the tree for `array.push(value)`, which is helpful for recording the result # arrays from comprehensions. -PushNode = exports.PushNode = { - +PushNode = exports.PushNode = wrap: (array, expressions) -> expr = expressions.unwrap() return expressions if expr.isPureStatement() or expr.containsPureStatement() @@ -1481,12 +1479,10 @@ PushNode = exports.PushNode = { new ValueNode(literal(array), [new AccessorNode(literal('push'))]), [expr] )]) -} - #### ClosureNode # A faux-node used to wrap an expressions body in a closure. -ClosureNode = exports.ClosureNode = { +ClosureNode = exports.ClosureNode = # Wrap the expressions body, unless it contains a pure statement, # in which case, no dice. If the body mentions `this` or `arguments`, @@ -1508,12 +1504,10 @@ ClosureNode = exports.ClosureNode = { call = new CallNode(func, args) if statement then Expressions.wrap([call]) else call -} - # Utility Functions # ----------------- -UTILITIES = { +UTILITIES = # Correctly set up a prototype chain for inheritance, including a reference # to the superclass for `super()` calls. See: @@ -1540,8 +1534,6 @@ UTILITIES = { hasProp: 'Object.prototype.hasOwnProperty' slice: 'Array.prototype.slice' -} - # Constants # --------- diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 52918012..a642ae2e 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -134,7 +134,9 @@ exports.Rewriter = class Rewriter if token[0] is ':' and (not last or last[0] isnt '{') stack.push '{' idx = if @tag(i - 2) is '@' then i - 2 else i - 1 - @tokens.splice idx, 0, ['{', '{', token[2]] + tok = ['{', '{', token[2]] + tok.generated = yes + @tokens.splice idx, 0, tok condition = (token, i) -> [one, two, three] = @tokens.slice(i + 1, i + 4) return false if 'HERECOMMENT' in [@tag(i + 1), @tag(i - 1)] @@ -151,10 +153,17 @@ exports.Rewriter = class Rewriter # Insert the implicit parentheses here, so that the parser doesn't have to # deal with them. addImplicitParentheses: -> + classLine = no @scanTokens (token, i) -> - prev = @tokens[i - 1] - if prev and prev.spaced and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, token[0]) and - not (token[0] is 'UNARY' and (@tag(i + 1) in ['IN', 'OF'])) + classLine = yes if token[0] is 'CLASS' + prev = @tokens[i - 1] + next = @tokens[i + 1] + idx = 1 + callObject = not classLine and token[0] is 'INDENT' and next and next.generated and next[0] is '{' and prev and include(IMPLICIT_FUNC, prev[0]) + idx = 2 if callObject + classLine = no if include(LINEBREAKS, token[0]) + if prev and (prev.spaced and include(IMPLICIT_FUNC, prev[0]) and include(IMPLICIT_CALL, token[0]) and + not (token[0] is 'UNARY' and (@tag(i + 1) in ['IN', 'OF']))) or callObject @tokens.splice i, 0, ['CALL_START', '(', token[2]] condition = (token, i) -> (not token.generated and @tokens[i - 1][0] isnt ',' and include(IMPLICIT_END, token[0]) and @@ -163,7 +172,7 @@ exports.Rewriter = class Rewriter action = (token, i) -> idx = if token[0] is 'OUTDENT' then i + 1 else i @tokens.splice idx, 0, ['CALL_END', ')', token[2]] - @detectEnd i + 1, condition, action + @detectEnd i + idx, condition, action return 2 return 1 @@ -331,3 +340,6 @@ IMPLICIT_END = ['POST_IF', 'POST_UNLESS', 'FOR', 'WHILE', 'UNTIL', 'LOOP', ' # The grammar can't disambiguate them, so we insert the implicit indentation. SINGLE_LINERS = ['ELSE', "->", "=>", 'TRY', 'FINALLY', 'THEN'] SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN'] + +# Tokens that end a line. +LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT'] diff --git a/test/test_literals.coffee b/test/test_literals.coffee index 5c1d3c57..2daad9a6 100644 --- a/test/test_literals.coffee +++ b/test/test_literals.coffee @@ -165,6 +165,20 @@ ok obj.options.value is yes ok obj.fn() is null +# Implicit arguments to function calls: +func = (obj) -> obj.a + +result = func + a: 10 + +ok result is 10 + +result = func + "a": 20 + +ok result is 20 + + # Implicit objects with wacky indentation: obj = 'reverse': (obj) ->