diff --git a/gen/repl.c b/gen/repl.c index 2097e64..4386915 100644 Binary files a/gen/repl.c and b/gen/repl.c differ diff --git a/repl.js b/repl.js index 5c4be05..0451e2d 100644 --- a/repl.js +++ b/repl.js @@ -551,51 +551,60 @@ import * as os from "os"; cursor_pos = 0; } - function get_context_word(line, pos) { - var s = ""; - while (pos > 0 && is_word(line[pos - 1])) { + function get_context_word(line, end) { + var pos = end; + while (pos > 0 && is_word(line[pos - 1])) pos--; - s = line[pos] + s; - } - return s; + return line.slice(pos, end); } function get_context_object(line, pos) { - var obj, base, c; - if (pos <= 0 || " ~!%^&*(-+={[|:;,<>?/".indexOf(line[pos - 1]) >= 0) + if (pos <= 0) return g; - if (pos >= 2 && line[pos - 1] === ".") { + var c = line[pos - 1]; + if (pos === 1 && c === '\\') + return directives; + if ("'\"`@#)]}\\".indexOf(c) >= 0) + return void 0; + if (pos >= 2 && c === ".") { pos--; - obj = {}; switch (c = line[pos - 1]) { case '\'': case '\"': + case '`': return "a"; case ']': - return []; - case '}': - return {}; + return []; // incorrect for a[b]. case '/': return / /; default: if (is_word(c)) { - base = get_context_word(line, pos); - if (["true", "false", "null", "this"].includes(base) || !isNaN(+base)) - return eval(base); - // Check if `base` is a set of regexp flags - if (pos - base.length >= 3 && line[pos - base.length - 1] === '/') - return new RegExp('', base); - obj = get_context_object(line, pos - base.length); + var base = get_context_word(line, pos); + var base_pos = pos - base.length; + if (base === 'true' || base === 'false') + return true; + if (base === 'null') + return null; + if (base === 'this') + return g; + if (!isNaN(+base)) // number literal, incorrect for 1. + return 0; + var obj = get_context_object(line, base_pos); if (obj === null || obj === void 0) return obj; - if (obj === g && obj[base] === void 0) - return eval(base); - else + if (typeof obj[base] !== 'undefined') return obj[base]; + // Check if `base` is a set of regexp flags + // TODO(chqrlie): this is incorrect for a/i... + // Should use colorizer to determine the token type + if (base_pos >= 3 && line[base_pos - 1] === '/' && base.match(/^[dgimsuvy]+$/)) + return RegExp(); + // base is a local identifier, complete as generic object } - return {}; + break; } + return {}; } - return void 0; + return g; } function get_completions(line, pos) { @@ -1044,6 +1053,10 @@ import * as os from "os"; "\\q exit\n"); } + var directives = Object.setPrototypeOf({ + h: "h", help: "help", load: "load", x: "x", d: "d", t: "t", + clear: "clear", q: "q" }, null); + function eval_and_print(expr) { var result;