1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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);
}), o("@ PARAM", function() {
return new Param($2, true);
}), o("PARAM . . .", function() {
}), o("PARAM ...", function() {
return new Param($1, false, true);
}), o("@ PARAM . . .", function() {
}), o("@ PARAM ...", function() {
return new Param($2, true, true);
})
],
Splat: [
o("Expression . . .", function() {
o("Expression ...", function() {
return new Splat($1);
})
],
@ -293,9 +293,9 @@
})
],
RangeDots: [
o(". .", function() {
o("..", function() {
return 'inclusive';
}), o(". . .", function() {
}), o("...", function() {
return 'exclusive';
})
],

View file

@ -1,5 +1,5 @@
(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;
_ref = require('./helpers'), include = _ref.include, count = _ref.count, starts = _ref.starts, compact = _ref.compact, last = _ref.last;
exports.Lexer = (function() {
@ -228,7 +228,7 @@
return true;
};
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))) {
return false;
}
@ -238,7 +238,8 @@
prev = last(this.tokens, 1);
size = indent.length - 1 - indent.lastIndexOf('\n');
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 (noNewlines) {
return this.suppressNewlines();
@ -598,7 +599,7 @@
IDENTIFIER = /^[a-zA-Z_$][\w$]*/;
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/;
OPERATOR = /^(?:-[-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)/;
OPERATOR = /^(?:-[-=>]?|\+[+=]?|\.\.\.?|[*&|\/%=<>^:!?]+)/;
WHITESPACE = /^[ \t]+/;
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
CODE = /^[-=]>/;
@ -612,6 +613,7 @@
HEREDOC_INDENT = /\n+([ \t]*)/g;
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/;
NEXT_CHARACTER = /^\s*(\S?)/;
NEXT_ELLIPSIS = /^\s*(\.\.\.?)/;
LEADING_SPACES = /^\s+/;
TRAILING_SPACES = /\s+$/;
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: [
o "PARAM", -> new Literal $1
o "@ PARAM", -> new Param $2, true
o "PARAM . . .", -> new Param $1, false, true
o "@ PARAM . . .", -> new Param $2, true, true
o "PARAM ...", -> new Param $1, false, true
o "@ PARAM ...", -> new Param $2, true, true
]
# A splat that occurs outside of a parameter list.
Splat: [
o "Expression . . .", -> new Splat $1
o "Expression ...", -> new Splat $1
]
# Variables and properties that can be assigned to.
@ -331,8 +331,8 @@ grammar =
]
RangeDots: [
o ". .", -> 'inclusive'
o ". . .", -> 'exclusive'
o "..", -> 'inclusive'
o "...", -> 'exclusive'
]
# A reference to a property on *this*.

View file

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

View file

@ -114,3 +114,12 @@ list = [a = 0, nums..., b = 4]
ok a is 0
ok b is 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