2017-10-02 00:59:51 +01:00
|
|
|
import * as Parsing from "./parsing"
|
2017-10-02 21:09:10 +01:00
|
|
|
import * as state from "./state"
|
2017-10-02 00:59:51 +01:00
|
|
|
|
|
|
|
/** Accepts keyevents, resolves them to maps, maps to exstrs, executes exstrs */
|
|
|
|
function *ParserController () {
|
|
|
|
while (true) {
|
|
|
|
let ex_str = ""
|
|
|
|
let keys = []
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
let keyevent = yield
|
|
|
|
let keypress = keyevent.key
|
|
|
|
|
|
|
|
// Special keys (e.g. Backspace) are not handled properly
|
|
|
|
// yet. So drop them. This also drops all modifier keys.
|
|
|
|
// When we put in handling for other special keys, remember
|
|
|
|
// to continue to ban modifiers.
|
|
|
|
if (keypress.length > 1) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.push(keypress)
|
|
|
|
let response = Parsing.normalmode.parser(keys)
|
2017-10-02 21:09:10 +01:00
|
|
|
switch(state.MODE){
|
|
|
|
case "NORMAL":
|
|
|
|
response = Parsing.normalmode.parser(keys)
|
|
|
|
break
|
|
|
|
|
|
|
|
case "INSERT":
|
|
|
|
response = Parsing.insertmode.parser(keys)
|
|
|
|
break
|
|
|
|
}
|
2017-10-02 00:59:51 +01:00
|
|
|
|
|
|
|
console.debug(keys, response)
|
|
|
|
|
|
|
|
if (response.ex_str){
|
|
|
|
ex_str = response.ex_str
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
keys = response.keys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acceptExCmd(ex_str)
|
|
|
|
} catch (e) {
|
|
|
|
// Rumsfeldian errors are caught here
|
|
|
|
console.error("Tridactyl ParserController fatally wounded:", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let generator = ParserController() // var rather than let stops weirdness in repl.
|
|
|
|
generator.next()
|
|
|
|
|
|
|
|
/** Feed keys to the ParserController */
|
|
|
|
export function acceptKey(keyevent: Event) {
|
|
|
|
generator.next(keyevent)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse and execute ExCmds */
|
|
|
|
export function acceptExCmd(ex_str: string) {
|
2017-10-05 13:26:28 +01:00
|
|
|
// TODO: Errors should go to CommandLine.
|
2017-10-02 00:59:51 +01:00
|
|
|
try {
|
2017-10-05 13:26:28 +01:00
|
|
|
let [func, args] = Parsing.exmode.parser(ex_str)
|
|
|
|
try {
|
|
|
|
func(...args)
|
|
|
|
} catch (e) {
|
|
|
|
// Errors from func are caught here (e.g. no next tab)
|
|
|
|
console.error(e)
|
|
|
|
}
|
2017-10-02 00:59:51 +01:00
|
|
|
} catch (e) {
|
2017-10-05 13:26:28 +01:00
|
|
|
// Errors from parser caught here
|
2017-10-02 00:59:51 +01:00
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
}
|