lib/coffee_script/rewriter.rb in coffee-script-0.3.1 vs lib/coffee_script/rewriter.rb in coffee-script-0.3.2

- old
+ new

@@ -20,13 +20,13 @@ # Tokens pairs that, in immediate succession, indicate an implicit call. IMPLICIT_FUNC = [:IDENTIFIER, :SUPER, ')', :CALL_END, ']', :INDEX_END] IMPLICIT_END = [:IF, :UNLESS, :FOR, :WHILE, "\n", :OUTDENT] IMPLICIT_CALL = [:IDENTIFIER, :NUMBER, :STRING, :JS, :REGEX, :NEW, :PARAM_START, - :TRY, :DELETE, :TYPEOF, :SWITCH, :ARGUMENTS, + :TRY, :DELETE, :TYPEOF, :SWITCH, :TRUE, :FALSE, :YES, :NO, :ON, :OFF, '!', '!!', :NOT, - '->', '=>', '[', '(', '{'] + '@', '->', '=>', '[', '(', '{'] # The inverse mappings of token pairs we're trying to fix up. INVERSES = BALANCED_PAIRS.inject({}) do |memo, pair| memo[pair.first] = pair.last memo[pair.last] = pair.first @@ -149,10 +149,34 @@ end next 1 end end + # Methods may be optionally called without parentheses, for simple cases. + # Insert the implicit parentheses here, so that the parser doesn't have to + # deal with them. + def add_implicit_parentheses + stack = [0] + scan_tokens do |prev, token, post, i| + stack.push(0) if token[0] == :INDENT + if token[0] == :OUTDENT + last = stack.pop + stack[-1] += last + end + if stack.last > 0 && (IMPLICIT_END.include?(token[0]) || post.nil?) + idx = token[0] == :OUTDENT ? i + 1 : i + stack.last.times { @tokens.insert(idx, [:CALL_END, Value.new(')', token[1].line)]) } + size, stack[-1] = stack[-1] + 1, 0 + next size + end + next 1 unless IMPLICIT_FUNC.include?(prev[0]) && IMPLICIT_CALL.include?(token[0]) + @tokens.insert(i, [:CALL_START, Value.new('(', token[1].line)]) + stack[-1] += 1 + next 2 + end + end + # Because our grammar is LALR(1), it can't handle some single-line # expressions that lack ending delimiters. Use the lexer to add the implicit # blocks, so it doesn't need to. # ')' can close a single-line block, but we need to make sure it's balanced. def add_implicit_indentation @@ -178,33 +202,9 @@ parens -= 1 if tok[0] == ')' end next 1 unless token[0] == :THEN @tokens.delete_at(i) next 0 - end - end - - # Methods may be optionally called without parentheses, for simple cases. - # Insert the implicit parentheses here, so that the parser doesn't have to - # deal with them. - def add_implicit_parentheses - stack = [0] - scan_tokens do |prev, token, post, i| - stack.push(0) if token[0] == :INDENT - if token[0] == :OUTDENT - last = stack.pop - stack[-1] += last - end - if stack.last > 0 && (IMPLICIT_END.include?(token[0]) || post.nil?) - idx = token[0] == :OUTDENT ? i + 1 : i - stack.last.times { @tokens.insert(idx, [:CALL_END, Value.new(')', token[1].line)]) } - size, stack[-1] = stack[-1] + 1, 0 - next size - end - next 1 unless IMPLICIT_FUNC.include?(prev[0]) && IMPLICIT_CALL.include?(token[0]) - @tokens.insert(i, [:CALL_START, Value.new('(', token[1].line)]) - stack[-1] += 1 - next 2 end end # Ensure that all listed pairs of tokens are correctly balanced throughout # the course of the token stream. \ No newline at end of file