summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSakthipriyan Vairamani <thechargingvolcano@gmail.com>2015-07-12 01:22:33 +0000
committerSakthipriyan Vairamani <thechargingvolcano@gmail.com>2015-07-25 00:33:50 +0530
commit81ea52aa01aa264b1424e00bc240b956b19ddaa5 (patch)
tree88c9b987c6afc1664529006f69fef79eb5be57a1 /lib
parent30edb5aee9c21c9696afbd61c28b51be461c3bc2 (diff)
downloadandroid-node-v8-81ea52aa01aa264b1424e00bc240b956b19ddaa5.tar.gz
android-node-v8-81ea52aa01aa264b1424e00bc240b956b19ddaa5.tar.bz2
android-node-v8-81ea52aa01aa264b1424e00bc240b956b19ddaa5.zip
repl: improving line continuation handling
As it is, REPL doesn't honour the line continuation feature very well. This patch 1. keeps track of the beginning of the string literals and if they don't end or current line doesn't end with line continuation, then error out. 2. monitors if the line continuation character is used without the string literal and errors out if that happens. PR-URL: https://github.com/nodejs/io.js/pull/2163 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/repl.js90
1 files changed, 75 insertions, 15 deletions
diff --git a/lib/repl.js b/lib/repl.js
index 0f93bfdc40..d978f27dbf 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -174,6 +174,7 @@ function REPLServer(prompt,
self._domain.on('error', function(e) {
debug('domain error');
self.outputStream.write((e.stack || e) + '\n');
+ self._currentStringLiteral = null;
self.bufferedCommand = '';
self.lines.level = [];
self.displayPrompt();
@@ -200,6 +201,8 @@ function REPLServer(prompt,
self.outputStream = output;
self.resetContext();
+ // Initialize the current string literal found, to be null
+ self._currentStringLiteral = null;
self.bufferedCommand = '';
self.lines.level = [];
@@ -258,21 +261,86 @@ function REPLServer(prompt,
sawSIGINT = false;
}
+ self._currentStringLiteral = null;
self.bufferedCommand = '';
self.lines.level = [];
self.displayPrompt();
});
+ function parseLine(line, currentStringLiteral) {
+ var previous = null, current = null;
+
+ for (var i = 0; i < line.length; i += 1) {
+ if (previous === '\\') {
+ // if it is a valid escaping, then skip processing
+ previous = current;
+ continue;
+ }
+
+ current = line.charAt(i);
+ if (current === currentStringLiteral) {
+ currentStringLiteral = null;
+ } else if (current === '\'' ||
+ current === '"' &&
+ currentStringLiteral === null) {
+ currentStringLiteral = current;
+ }
+ previous = current;
+ }
+
+ return currentStringLiteral;
+ }
+
+ function getFinisherFunction(cmd, defaultFn) {
+ if ((self._currentStringLiteral === null &&
+ cmd.charAt(cmd.length - 1) === '\\') ||
+ (self._currentStringLiteral !== null &&
+ cmd.charAt(cmd.length - 1) !== '\\')) {
+
+ // If the line continuation is used outside string literal or if the
+ // string continuation happens with out line continuation, then fail hard.
+ // Even if the error is recoverable, get the underlying error and use it.
+ return function(e, ret) {
+ var error = e instanceof Recoverable ? e.err : e;
+
+ if (arguments.length === 2) {
+ // using second argument only if it is actually passed. Otherwise
+ // `undefined` will be printed when invalid REPL commands are used.
+ return defaultFn(error, ret);
+ }
+
+ return defaultFn(error);
+ };
+ }
+ return defaultFn;
+ }
+
self.on('line', function(cmd) {
debug('line %j', cmd);
sawSIGINT = false;
var skipCatchall = false;
+ var finisherFn = finish;
// leading whitespaces in template literals should not be trimmed.
if (self._inTemplateLiteral) {
self._inTemplateLiteral = false;
} else {
- cmd = trimWhitespace(cmd);
+ const wasWithinStrLiteral = self._currentStringLiteral !== null;
+ self._currentStringLiteral = parseLine(cmd, self._currentStringLiteral);
+ const isWithinStrLiteral = self._currentStringLiteral !== null;
+
+ if (!wasWithinStrLiteral && !isWithinStrLiteral) {
+ // Current line has nothing to do with String literals, trim both ends
+ cmd = cmd.trim();
+ } else if (wasWithinStrLiteral && !isWithinStrLiteral) {
+ // was part of a string literal, but it is over now, trim only the end
+ cmd = cmd.trimRight();
+ } else if (isWithinStrLiteral && !wasWithinStrLiteral) {
+ // was not part of a string literal, but it is now, trim only the start
+ cmd = cmd.trimLeft();
+ }
+
+ finisherFn = getFinisherFunction(cmd, finish);
}
// Check to see if a REPL keyword was used. If it returns true,
@@ -305,9 +373,9 @@ function REPLServer(prompt,
}
debug('eval %j', evalCmd);
- self.eval(evalCmd, self.context, 'repl', finish);
+ self.eval(evalCmd, self.context, 'repl', finisherFn);
} else {
- finish(null);
+ finisherFn(null);
}
function finish(e, ret) {
@@ -318,6 +386,7 @@ function REPLServer(prompt,
self.outputStream.write('npm should be run outside of the ' +
'node repl, in your normal shell.\n' +
'(Press Control-D to exit.)\n');
+ self._currentStringLiteral = null;
self.bufferedCommand = '';
self.displayPrompt();
return;
@@ -339,6 +408,7 @@ function REPLServer(prompt,
}
// Clear buffer if no SyntaxErrors
+ self._currentStringLiteral = null;
self.bufferedCommand = '';
// If we got any output - print it (if no error)
@@ -870,6 +940,7 @@ function defineDefaultCommands(repl) {
repl.defineCommand('break', {
help: 'Sometimes you get stuck, this gets you out',
action: function() {
+ this._currentStringLiteral = null;
this.bufferedCommand = '';
this.displayPrompt();
}
@@ -884,6 +955,7 @@ function defineDefaultCommands(repl) {
repl.defineCommand('clear', {
help: clearMessage,
action: function() {
+ this._currentStringLiteral = null;
this.bufferedCommand = '';
if (!this.useGlobal) {
this.outputStream.write('Clearing context...\n');
@@ -949,18 +1021,6 @@ function defineDefaultCommands(repl) {
});
}
-
-function trimWhitespace(cmd) {
- const trimmer = /^\s*(.+)\s*$/m;
- var matches = trimmer.exec(cmd);
-
- if (matches && matches.length === 2) {
- return matches[1];
- }
- return '';
-}
-
-
function regexpEscape(s) {
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}