Skip to content

Commit

Permalink
Automatically handle multi line input
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Mar 13, 2022
1 parent 328d413 commit 4893679
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
"digitaljs_lua": "github:yuyichao/digitaljs_lua#dev",
"hammerjs": "^2.0.8",
"lodash": "^4.17.21",
"luaparse": "^0.3.1",
"path-browserify": "^1.0.1",
"resize-observer-polyfill": "^1.5.1",
"split-grid": "^1.0.11",
Expand Down
39 changes: 37 additions & 2 deletions src/lua_terminal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
'use strict';

import REPL from './repl.mjs';
import * as luaparse from 'luaparse';

const unexpected_eof_suffix = (() => {
// This is basically how lua.c detect parser error...
// It would be nice if luaparse could give us a simpler ID to match...
// It's good enough for now and I don't really want to monkey patch luaparse ATM.
const err = luaparse.errors.unexpectedEOF;
return err.substring(err.length - 7);
})();

class ParseResult {
static OK = 1
static EOF = 2
static ERROR = 3
}

function try_parse(str) {
try {
luaparse.parse(str);
}
catch (e) {
if (e.message && e.message.endsWith(unexpected_eof_suffix))
return ParseResult.EOF;
return ParseResult.ERROR;
}
return ParseResult.OK;
}

function try_parse_with_return(str) {
// Do not append `;` here since we want the string to be at the end of the input.
const res = try_parse(`return ${str}`);
if (res === ParseResult.OK)
return res;
return Math.min(res, try_parse(str));
}

export class LuaTerminal extends REPL {
#view
Expand All @@ -28,8 +63,8 @@ export class LuaTerminal extends REPL {
}

#try_submit(data) {
// TODO:
// * automatically detect if we should take more input lines
if (try_parse_with_return(data) === ParseResult.EOF)
return false;
this.#view.post({
command: 'runlua',
name: `REPL[${this.#id++}]`,
Expand Down

0 comments on commit 4893679

Please sign in to comment.