Fixing splats after newlines - #754.

This commit is contained in:
Sam Stephenson 2010-10-10 11:34:22 -05:00
parent f6ca5d814c
commit 20a07c174c
6 changed files with 36 additions and 23 deletions

View File

@ -138,14 +138,14 @@
return new Literal($1); return new Literal($1);
}), o("@ PARAM", function() { }), o("@ PARAM", function() {
return new Param($2, true); return new Param($2, true);
}), o("PARAM . . .", function() { }), o("PARAM ...", function() {
return new Param($1, false, true); return new Param($1, false, true);
}), o("@ PARAM . . .", function() { }), o("@ PARAM ...", function() {
return new Param($2, true, true); return new Param($2, true, true);
}) })
], ],
Splat: [ Splat: [
o("Expression . . .", function() { o("Expression ...", function() {
return new Splat($1); return new Splat($1);
}) })
], ],
@ -293,9 +293,9 @@
}) })
], ],
RangeDots: [ RangeDots: [
o(". .", function() { o("..", function() {
return 'inclusive'; return 'inclusive';
}), o(". . .", function() { }), o("...", function() {
return 'exclusive'; return 'exclusive';
}) })
], ],

View File

@ -1,5 +1,5 @@
(function() { (function() {
var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LEADING_SPACES, LINE_BREAK, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, TRAILING_SPACES, UNARY, WHITESPACE, _ref, compact, count, include, last, op, starts; var ASSIGNED, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LEADING_SPACES, LINE_BREAK, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NEXT_CHARACTER, NEXT_ELLIPSIS, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, TRAILING_SPACES, UNARY, WHITESPACE, _ref, compact, count, include, last, op, starts;
Rewriter = require('./rewriter').Rewriter; Rewriter = require('./rewriter').Rewriter;
_ref = require('./helpers'), include = _ref.include, count = _ref.count, starts = _ref.starts, compact = _ref.compact, last = _ref.last; _ref = require('./helpers'), include = _ref.include, count = _ref.count, starts = _ref.starts, compact = _ref.compact, last = _ref.last;
exports.Lexer = (function() { exports.Lexer = (function() {
@ -228,7 +228,7 @@
return true; return true;
}; };
Lexer.prototype.lineToken = function() { Lexer.prototype.lineToken = function() {
var diff, indent, match, nextCharacter, noNewlines, prev, size; var _ref2, diff, indent, match, nextCharacter, nextEllipsis, noNewlines, prev, size;
if (!(match = MULTI_DENT.exec(this.chunk))) { if (!(match = MULTI_DENT.exec(this.chunk))) {
return false; return false;
} }
@ -238,7 +238,8 @@
prev = last(this.tokens, 1); prev = last(this.tokens, 1);
size = indent.length - 1 - indent.lastIndexOf('\n'); size = indent.length - 1 - indent.lastIndexOf('\n');
nextCharacter = NEXT_CHARACTER.exec(this.chunk)[1]; nextCharacter = NEXT_CHARACTER.exec(this.chunk)[1];
noNewlines = (('.' === nextCharacter || ',' === nextCharacter)) || this.unfinished(); nextEllipsis = (((_ref2 = NEXT_ELLIPSIS.exec(this.chunk)) != null) ? _ref2[1] : undefined);
noNewlines = (('.' === nextCharacter || ',' === nextCharacter) && !nextEllipsis) || this.unfinished();
if (size - this.indebt === this.indent) { if (size - this.indebt === this.indent) {
if (noNewlines) { if (noNewlines) {
return this.suppressNewlines(); return this.suppressNewlines();
@ -598,7 +599,7 @@
IDENTIFIER = /^[a-zA-Z_$][\w$]*/; IDENTIFIER = /^[a-zA-Z_$][\w$]*/;
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i; NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/; HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/;
OPERATOR = /^(?:-[-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)/; OPERATOR = /^(?:-[-=>]?|\+[+=]?|\.\.\.?|[*&|\/%=<>^:!?]+)/;
WHITESPACE = /^[ \t]+/; WHITESPACE = /^[ \t]+/;
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/; COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
CODE = /^[-=]>/; CODE = /^[-=]>/;
@ -612,6 +613,7 @@
HEREDOC_INDENT = /\n+([ \t]*)/g; HEREDOC_INDENT = /\n+([ \t]*)/g;
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/; ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/;
NEXT_CHARACTER = /^\s*(\S?)/; NEXT_CHARACTER = /^\s*(\S?)/;
NEXT_ELLIPSIS = /^\s*(\.\.\.?)/;
LEADING_SPACES = /^\s+/; LEADING_SPACES = /^\s+/;
TRAILING_SPACES = /\s+$/; TRAILING_SPACES = /\s+$/;
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/; NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/;

File diff suppressed because one or more lines are too long

View File

@ -203,13 +203,13 @@ grammar =
Param: [ Param: [
o "PARAM", -> new Literal $1 o "PARAM", -> new Literal $1
o "@ PARAM", -> new Param $2, true o "@ PARAM", -> new Param $2, true
o "PARAM . . .", -> new Param $1, false, true o "PARAM ...", -> new Param $1, false, true
o "@ PARAM . . .", -> new Param $2, true, true o "@ PARAM ...", -> new Param $2, true, true
] ]
# A splat that occurs outside of a parameter list. # A splat that occurs outside of a parameter list.
Splat: [ Splat: [
o "Expression . . .", -> new Splat $1 o "Expression ...", -> new Splat $1
] ]
# Variables and properties that can be assigned to. # Variables and properties that can be assigned to.
@ -331,8 +331,8 @@ grammar =
] ]
RangeDots: [ RangeDots: [
o ". .", -> 'inclusive' o "..", -> 'inclusive'
o ". . .", -> 'exclusive' o "...", -> 'exclusive'
] ]
# A reference to a property on *this*. # A reference to a property on *this*.

View File

@ -233,7 +233,8 @@ exports.Lexer = class Lexer
prev = last @tokens, 1 prev = last @tokens, 1
size = indent.length - 1 - indent.lastIndexOf '\n' size = indent.length - 1 - indent.lastIndexOf '\n'
nextCharacter = NEXT_CHARACTER.exec(@chunk)[1] nextCharacter = NEXT_CHARACTER.exec(@chunk)[1]
noNewlines = (nextCharacter in ['.', ',']) or @unfinished() nextEllipsis = NEXT_ELLIPSIS.exec(@chunk)?[1]
noNewlines = (nextCharacter in ['.', ','] and not nextEllipsis) or @unfinished()
if size - @indebt is @indent if size - @indebt is @indent
return @suppressNewlines() if noNewlines return @suppressNewlines() if noNewlines
return @newlineToken indent return @newlineToken indent
@ -547,7 +548,7 @@ JS_FORBIDDEN = JS_KEYWORDS.concat RESERVED
IDENTIFIER = /^[a-zA-Z_$][\w$]*/ IDENTIFIER = /^[a-zA-Z_$][\w$]*/
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/ HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/
OPERATOR = /// ^ (?: -[-=>]? | \+[+=]? | [*&|/%=<>^:!?]+ ) /// OPERATOR = /// ^ (?: -[-=>]? | \+[+=]? | \.\.\.? | [*&|/%=<>^:!?]+ ) ///
WHITESPACE = /^[ \t]+/ WHITESPACE = /^[ \t]+/
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/ COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/
CODE = /^[-=]>/ CODE = /^[-=]>/
@ -572,6 +573,7 @@ MULTILINER = /\n/g
HEREDOC_INDENT = /\n+([ \t]*)/g HEREDOC_INDENT = /\n+([ \t]*)/g
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/ ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/
NEXT_CHARACTER = /^\s*(\S?)/ NEXT_CHARACTER = /^\s*(\S?)/
NEXT_ELLIPSIS = /^\s*(\.\.\.?)/
LEADING_SPACES = /^\s+/ LEADING_SPACES = /^\s+/
TRAILING_SPACES = /\s+$/ TRAILING_SPACES = /\s+$/
NO_NEWLINE = /// ^ NO_NEWLINE = /// ^

View File

@ -114,3 +114,12 @@ list = [a = 0, nums..., b = 4]
ok a is 0 ok a is 0
ok b is 4 ok b is 4
ok list.join(' ') is '0 1 2 3 4' ok list.join(' ') is '0 1 2 3 4'
# Splat on a line by itself is invalid.
failed = true
try
CoffeeScript.compile "x 'a'\n...\n"
failed = false
catch err
ok failed