mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-04-11 03:00:13 -04:00
minor refactors to balanced_string
This commit is contained in:
47
lib/lexer.js
47
lib/lexer.js
@@ -169,7 +169,8 @@
|
|||||||
};
|
};
|
||||||
// Matches regular expression literals. Lexing regular expressions is difficult
|
// Matches regular expression literals. Lexing regular expressions is difficult
|
||||||
// to distinguish from division, so we borrow some basic heuristics from
|
// to distinguish from division, so we borrow some basic heuristics from
|
||||||
// JavaScript and Ruby.
|
// JavaScript and Ruby, borrow slash balancing from `@balanced_token`, and
|
||||||
|
// borrow interpolation from `@interpolate_string`.
|
||||||
Lexer.prototype.regex_token = function regex_token() {
|
Lexer.prototype.regex_token = function regex_token() {
|
||||||
var flags, regex, str;
|
var flags, regex, str;
|
||||||
if (!(this.chunk.match(REGEX_START))) {
|
if (!(this.chunk.match(REGEX_START))) {
|
||||||
@@ -412,26 +413,27 @@
|
|||||||
levels = [];
|
levels = [];
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < str.length) {
|
while (i < str.length) {
|
||||||
_a = delimited;
|
if (levels.length && starts(str, '\\', i)) {
|
||||||
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
i += 1;
|
||||||
pair = _a[_b];
|
} else {
|
||||||
_d = pair;
|
_a = delimited;
|
||||||
open = _d[0];
|
for (_b = 0, _c = _a.length; _b < _c; _b++) {
|
||||||
close = _d[1];
|
pair = _a[_b];
|
||||||
if (levels.length && starts(str, '\\', i)) {
|
_d = pair;
|
||||||
i += 1;
|
open = _d[0];
|
||||||
break;
|
close = _d[1];
|
||||||
} else if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
|
if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
|
||||||
levels.pop();
|
levels.pop();
|
||||||
i += close.length - 1;
|
i += close.length - 1;
|
||||||
if (!(levels.length)) {
|
if (!(levels.length)) {
|
||||||
i += 1;
|
i += 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} else if (starts(str, open, i)) {
|
||||||
|
levels.push(pair);
|
||||||
|
i += open.length - 1;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
} else if (starts(str, open, i)) {
|
|
||||||
levels.push(pair);
|
|
||||||
i += open.length - 1;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!levels.length || slash && starts(str, '\n', i)) {
|
if (!levels.length || slash && starts(str, '\n', i)) {
|
||||||
@@ -445,10 +447,7 @@
|
|||||||
}
|
}
|
||||||
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
||||||
}
|
}
|
||||||
if (i === 0) {
|
return !i ? false : str.substring(0, i);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return str.substring(0, i);
|
|
||||||
};
|
};
|
||||||
// Expand variables and expressions inside double-quoted strings using
|
// Expand variables and expressions inside double-quoted strings using
|
||||||
// [ECMA Harmony's interpolation syntax](http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation)
|
// [ECMA Harmony's interpolation syntax](http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation)
|
||||||
|
|||||||
@@ -124,7 +124,8 @@ exports.Lexer: class Lexer
|
|||||||
|
|
||||||
# Matches regular expression literals. Lexing regular expressions is difficult
|
# Matches regular expression literals. Lexing regular expressions is difficult
|
||||||
# to distinguish from division, so we borrow some basic heuristics from
|
# to distinguish from division, so we borrow some basic heuristics from
|
||||||
# JavaScript and Ruby.
|
# JavaScript and Ruby, borrow slash balancing from `@balanced_token`, and
|
||||||
|
# borrow interpolation from `@interpolate_string`.
|
||||||
regex_token: ->
|
regex_token: ->
|
||||||
return false unless @chunk.match REGEX_START
|
return false unless @chunk.match REGEX_START
|
||||||
return false if include NOT_REGEX, @tag()
|
return false if include NOT_REGEX, @tag()
|
||||||
@@ -311,27 +312,26 @@ exports.Lexer: class Lexer
|
|||||||
levels: []
|
levels: []
|
||||||
i: 0
|
i: 0
|
||||||
while i < str.length
|
while i < str.length
|
||||||
for pair in delimited
|
if levels.length and starts str, '\\', i
|
||||||
[open, close]: pair
|
i += 1
|
||||||
if levels.length and starts str, '\\', i
|
else
|
||||||
i += 1
|
for pair in delimited
|
||||||
break
|
[open, close]: pair
|
||||||
else if levels.length and starts(str, close, i) and levels[levels.length - 1] is pair
|
if levels.length and starts(str, close, i) and levels[levels.length - 1] is pair
|
||||||
levels.pop()
|
levels.pop()
|
||||||
i += close.length - 1
|
i += close.length - 1
|
||||||
i += 1 unless levels.length
|
i += 1 unless levels.length
|
||||||
break
|
break
|
||||||
else if starts str, open, i
|
else if starts str, open, i
|
||||||
levels.push(pair)
|
levels.push(pair)
|
||||||
i += open.length - 1
|
i += open.length - 1
|
||||||
break
|
break
|
||||||
break if not levels.length or slash and starts str, '\n', i
|
break if not levels.length or slash and starts str, '\n', i
|
||||||
i += 1
|
i += 1
|
||||||
if levels.length
|
if levels.length
|
||||||
return false if slash
|
return false if slash
|
||||||
throw new Error "SyntaxError: Unterminated ${levels.pop()[0]} starting on line ${@line + 1}"
|
throw new Error "SyntaxError: Unterminated ${levels.pop()[0]} starting on line ${@line + 1}"
|
||||||
return false if i is 0
|
if not i then false else str.substring(0, i)
|
||||||
return str.substring(0, i)
|
|
||||||
|
|
||||||
# Expand variables and expressions inside double-quoted strings using
|
# Expand variables and expressions inside double-quoted strings using
|
||||||
# [ECMA Harmony's interpolation syntax](http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation)
|
# [ECMA Harmony's interpolation syntax](http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation)
|
||||||
|
|||||||
Reference in New Issue
Block a user