Use array instead of linkedlist, add typings
This commit is contained in:
+49
-61
@@ -11,6 +11,13 @@ interface IHandlerLink extends IDisposable {
|
|||||||
nextHandler: IHandlerLink | null;
|
nextHandler: IHandlerLink | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IHandlerCollection<T> {
|
||||||
|
[key: string]: T[];
|
||||||
|
}
|
||||||
|
|
||||||
|
type CsiHandler = (params: number[], collect: string) => boolean | void;
|
||||||
|
type OscHandler = (data: string) => boolean | void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array filled with numbers between the low and high parameters (right exclusive).
|
* Returns an array filled with numbers between the low and high parameters (right exclusive).
|
||||||
* @param low The low number.
|
* @param low The low number.
|
||||||
@@ -46,7 +53,7 @@ export class TransitionTable {
|
|||||||
* @param action parser action to be done
|
* @param action parser action to be done
|
||||||
* @param next next parser state
|
* @param next next parser state
|
||||||
*/
|
*/
|
||||||
add(code: number, state: number, action: number | null, next: number | null): void {
|
add(code: number, state: number, action: number | null, next: number | null): void {
|
||||||
this.table[state << 8 | code] = ((action | 0) << 4) | ((next === undefined) ? state : next);
|
this.table[state << 8 | code] = ((action | 0) << 4) | ((next === undefined) ? state : next);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,9 +234,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
|
|||||||
// handler lookup containers
|
// handler lookup containers
|
||||||
protected _printHandler: (data: string, start: number, end: number) => void;
|
protected _printHandler: (data: string, start: number, end: number) => void;
|
||||||
protected _executeHandlers: any;
|
protected _executeHandlers: any;
|
||||||
protected _csiHandlers: any;
|
protected _csiHandlers: IHandlerCollection<CsiHandler>;
|
||||||
protected _escHandlers: any;
|
protected _escHandlers: any;
|
||||||
protected _oscHandlers: any;
|
protected _oscHandlers: IHandlerCollection<OscHandler>;
|
||||||
protected _dcsHandlers: any;
|
protected _dcsHandlers: any;
|
||||||
protected _activeDcsHandler: IDcsHandler | null;
|
protected _activeDcsHandler: IDcsHandler | null;
|
||||||
protected _errorHandler: (state: IParsingState) => IParsingState;
|
protected _errorHandler: (state: IParsingState) => IParsingState;
|
||||||
@@ -284,16 +291,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
|
|||||||
this._printHandler = null;
|
this._printHandler = null;
|
||||||
this._executeHandlers = null;
|
this._executeHandlers = null;
|
||||||
this._escHandlers = null;
|
this._escHandlers = null;
|
||||||
let handlers;
|
|
||||||
while ((handlers = this._csiHandlers) && handlers.dispose !== undefined) {
|
|
||||||
handlers.dispose();
|
|
||||||
if (handlers === this._csiHandlers) { break; } // sanity check
|
|
||||||
}
|
|
||||||
this._csiHandlers = null;
|
this._csiHandlers = null;
|
||||||
while ((handlers = this._oscHandlers) && handlers.dispose !== undefined) {
|
|
||||||
handlers.dispose();
|
|
||||||
if (handlers === this._oscHandlers) { break; } // sanity check
|
|
||||||
}
|
|
||||||
this._oscHandlers = null;
|
this._oscHandlers = null;
|
||||||
this._dcsHandlers = null;
|
this._dcsHandlers = null;
|
||||||
this._activeDcsHandler = null;
|
this._activeDcsHandler = null;
|
||||||
@@ -317,42 +315,18 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
|
|||||||
this._executeHandlerFb = callback;
|
this._executeHandlerFb = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _linkHandler(handlers: object[], index: number, newCallback: object): IDisposable {
|
addCsiHandler(flag: string, callback: CsiHandler): IDisposable {
|
||||||
const newHead: any = newCallback;
|
|
||||||
newHead.nextHandler = handlers[index] as IHandlerLink;
|
|
||||||
newHead.dispose = function (): void {
|
|
||||||
let previous = null;
|
|
||||||
let cur = handlers[index] as IHandlerLink;
|
|
||||||
for (; cur && cur.nextHandler;
|
|
||||||
previous = cur, cur = cur.nextHandler) {
|
|
||||||
if (cur === newHead) {
|
|
||||||
if (previous) { previous.nextHandler = cur.nextHandler; }
|
|
||||||
else { handlers[index] = cur.nextHandler; }
|
|
||||||
cur.nextHandler = null;
|
|
||||||
handlers = null; newCallback = null; // just in case
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
handlers[index] = newHead;
|
|
||||||
return newHead;
|
|
||||||
}
|
|
||||||
|
|
||||||
addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable {
|
|
||||||
const index = flag.charCodeAt(0);
|
const index = flag.charCodeAt(0);
|
||||||
const newHead =
|
if (this._csiHandlers[index] === undefined) {
|
||||||
(params: number[], collect: string): void => {
|
this._csiHandlers[index] = [];
|
||||||
if (! callback(params, collect)) {
|
}
|
||||||
const next = (newHead as unknown as IHandlerLink).nextHandler;
|
this._csiHandlers[index].push(callback);
|
||||||
if (next) { (next as any)(params, collect); }
|
return {
|
||||||
else { this._csiHandlerFb(collect, params, index); }
|
dispose: () => this._csiHandlers[index].splice(this._csiHandlers[index].indexOf(callback))
|
||||||
}
|
};
|
||||||
};
|
|
||||||
return this._linkHandler(this._csiHandlers, index, newHead);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void {
|
setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void {
|
||||||
this._csiHandlers[flag.charCodeAt(0)] = callback;
|
this._csiHandlers[flag.charCodeAt(0)] = [callback];
|
||||||
}
|
}
|
||||||
clearCsiHandler(flag: string): void {
|
clearCsiHandler(flag: string): void {
|
||||||
if (this._csiHandlers[flag.charCodeAt(0)]) delete this._csiHandlers[flag.charCodeAt(0)];
|
if (this._csiHandlers[flag.charCodeAt(0)]) delete this._csiHandlers[flag.charCodeAt(0)];
|
||||||
@@ -372,18 +346,16 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
|
|||||||
}
|
}
|
||||||
|
|
||||||
addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable {
|
addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable {
|
||||||
const newHead =
|
if (this._oscHandlers[ident] === undefined) {
|
||||||
(data: string): void => {
|
this._oscHandlers[ident] = [];
|
||||||
if (! callback(data)) {
|
}
|
||||||
const next = (newHead as unknown as IHandlerLink).nextHandler;
|
this._oscHandlers[ident].push(callback);
|
||||||
if (next) { (next as any)(data); }
|
return {
|
||||||
else { this._oscHandlerFb(ident, data); }
|
dispose: () => this._oscHandlers[ident].splice(this._oscHandlers[ident].indexOf(callback))
|
||||||
}
|
};
|
||||||
};
|
|
||||||
return this._linkHandler(this._oscHandlers, ident, newHead);
|
|
||||||
}
|
}
|
||||||
setOscHandler(ident: number, callback: (data: string) => void): void {
|
setOscHandler(ident: number, callback: (data: string) => void): void {
|
||||||
this._oscHandlers[ident] = callback;
|
this._oscHandlers[ident] = [callback];
|
||||||
}
|
}
|
||||||
clearOscHandler(ident: number): void {
|
clearOscHandler(ident: number): void {
|
||||||
if (this._oscHandlers[ident]) delete this._oscHandlers[ident];
|
if (this._oscHandlers[ident]) delete this._oscHandlers[ident];
|
||||||
@@ -520,9 +492,17 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ParserAction.CSI_DISPATCH:
|
case ParserAction.CSI_DISPATCH:
|
||||||
callback = this._csiHandlers[code];
|
// Trigger CSI Handler
|
||||||
if (callback) callback(params, collect);
|
const handlers = this._csiHandlers[code];
|
||||||
else this._csiHandlerFb(collect, params, code);
|
let j: number;
|
||||||
|
for (j = handlers.length - 1; j >= 0; j--) {
|
||||||
|
if (handlers[j](params, collect)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j < 0) {
|
||||||
|
this._csiHandlerFb(collect, params, code);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ParserAction.PARAM:
|
case ParserAction.PARAM:
|
||||||
if (code === 0x3b) params.push(0);
|
if (code === 0x3b) params.push(0);
|
||||||
@@ -597,9 +577,17 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
|
|||||||
// or with an explicit NaN OSC handler
|
// or with an explicit NaN OSC handler
|
||||||
const identifier = parseInt(osc.substring(0, idx));
|
const identifier = parseInt(osc.substring(0, idx));
|
||||||
const content = osc.substring(idx + 1);
|
const content = osc.substring(idx + 1);
|
||||||
callback = this._oscHandlers[identifier];
|
// Trigger OSC Handler
|
||||||
if (callback) callback(content);
|
const handlers = this._oscHandlers[identifier];
|
||||||
else this._oscHandlerFb(identifier, content);
|
let j: number;
|
||||||
|
for (j = handlers.length - 1; j >= 0; j--) {
|
||||||
|
if (handlers[j](content)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j < 0) {
|
||||||
|
this._oscHandlerFb(identifier, content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (code === 0x1b) transition |= ParserState.ESCAPE;
|
if (code === 0x1b) transition |= ParserState.ESCAPE;
|
||||||
|
|||||||
Reference in New Issue
Block a user