Merge branch 'master' into cleanup_sequences_files

This commit is contained in:
Jörg Breitbart
2019-07-06 20:45:50 +02:00
29 changed files with 1193 additions and 630 deletions
+80 -46
View File
@@ -3,11 +3,13 @@
* @license MIT
*/
import { IDcsHandler, IParsingState } from 'common/parser/Types';
import { IDcsHandler, IParsingState, IParams, ParamsArray } from 'common/parser/Types';
import { EscapeSequenceParser, TransitionTable, VT500_TRANSITION_TABLE } from 'common/parser/EscapeSequenceParser';
import * as chai from 'chai';
import { StringToUtf32, stringFromCodePoint } from 'common/input/TextDecoder';
import { ParserState } from 'common/parser/Constants';
import { Params } from 'common/parser/Params';
function r(a: number, b: number): string[] {
let c = b - a;
@@ -26,11 +28,14 @@ class TestEscapeSequenceParser extends EscapeSequenceParser {
public set osc(value: string) {
this._osc = value;
}
public get params(): number[] {
return this._params;
public get params(): ParamsArray {
return this._params.toArray();
}
public set params(value: number[]) {
this._params = value;
public set params(value: ParamsArray) {
this._params = Params.fromArray(value);
}
public get realParams(): IParams {
return this._params;
}
public get collect(): string {
return this._collect;
@@ -65,14 +70,14 @@ const testTerminal: any = {
actionExecute: function (flag: string): void {
this.calls.push(['exe', flag]);
},
actionCSI: function (collect: string, params: number[], flag: string): void {
this.calls.push(['csi', collect, params, flag]);
actionCSI: function (collect: string, params: IParams, flag: string): void {
this.calls.push(['csi', collect, params.toArray(), flag]);
},
actionESC: function (collect: string, flag: string): void {
this.calls.push(['esc', collect, flag]);
},
actionDCSHook: function (collect: string, params: number[], flag: string): void {
this.calls.push(['dcs hook', collect, params, flag]);
actionDCSHook: function (collect: string, params: IParams, flag: string): void {
this.calls.push(['dcs hook', collect, params.toArray(), flag]);
},
actionDCSPrint: function (data: Uint32Array, start: number, end: number): void {
let s = '';
@@ -88,7 +93,7 @@ const testTerminal: any = {
// dcs handler to map dcs actions into the test object `testTerminal`
class DcsTest implements IDcsHandler {
hook(collect: string, params: number[], flag: number): void {
hook(collect: string, params: IParams, flag: number): void {
testTerminal.actionDCSHook(collect, params, String.fromCharCode(flag));
}
put(data: Uint32Array, start: number, end: number): void {
@@ -120,7 +125,7 @@ let state: any;
// parser with Uint8Array based transition table
const testParser = new TestEscapeSequenceParser();
testParser.setPrintHandler(testTerminal.print.bind(testTerminal));
testParser.setCsiHandlerFallback((collect: string, params: number[], flag: number) => {
testParser.setCsiHandlerFallback((collect: string, params: IParams, flag: number) => {
testTerminal.actionCSI(collect, params, String.fromCharCode(flag));
});
testParser.setEscHandlerFallback((collect: string, flag: number) => {
@@ -562,16 +567,16 @@ describe('EscapeSequenceParser', function (): void {
testTerminal.clear();
}
});
it('trans CSI_ENTRY --> CSI_IGNORE', function (): void {
it('trans CSI_ENTRY --> CSI_PARAM for ":" (0x3a)', function (): void {
parser.reset();
parser.currentState = ParserState.CSI_ENTRY;
parse(parser, '\x3a');
chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE);
chai.expect(parser.currentState).equal(ParserState.CSI_PARAM);
parser.reset();
});
it('trans CSI_PARAM --> CSI_IGNORE', function (): void {
parser.reset();
const chars = ['\x3a', '\x3c', '\x3d', '\x3e', '\x3f'];
const chars = ['\x3c', '\x3d', '\x3e', '\x3f'];
for (let i = 0; i < chars.length; ++i) {
parser.currentState = ParserState.CSI_PARAM;
parse(parser, '\x3b' + chars[i]);
@@ -580,6 +585,18 @@ describe('EscapeSequenceParser', function (): void {
parser.reset();
}
});
it('trans CSI_PARAM --> CSI_IGNORE', function (): void {
parser.reset();
const chars = ['\x3c', '\x3d', '\x3e', '\x3f'];
for (let i = 0; i < chars.length; ++i) {
chai.expect(parser.params).eql([0]);
parser.currentState = ParserState.CSI_PARAM;
parse(parser, '\x3b' + chars[i]);
chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE);
chai.expect(parser.params).eql([0, 0]);
parser.reset();
}
});
it('trans CSI_INTERMEDIATE --> CSI_IGNORE', function (): void {
parser.reset();
const chars = r(0x30, 0x40);
@@ -785,16 +802,16 @@ describe('EscapeSequenceParser', function (): void {
chai.expect(parser.params).eql([0, 0]);
parser.reset();
});
it('trans DCS_ENTRY --> DCS_IGNORE', function (): void {
it('trans DCS_ENTRY --> DCS_PARAM for ":" (0x3a)', function (): void {
parser.reset();
parser.currentState = ParserState.DCS_ENTRY;
parse(parser, '\x3a');
chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE);
chai.expect(parser.currentState).equal(ParserState.DCS_PARAM);
parser.reset();
});
it('trans DCS_PARAM --> DCS_IGNORE', function (): void {
parser.reset();
const chars = ['\x3a', '\x3c', '\x3d', '\x3e', '\x3f'];
const chars = ['\x3c', '\x3d', '\x3e', '\x3f'];
for (let i = 0; i < chars.length; ++i) {
parser.currentState = ParserState.DCS_PARAM;
parse(parser, '\x3b' + chars[i]);
@@ -1028,6 +1045,23 @@ describe('EscapeSequenceParser', function (): void {
['print', 'defg']
], null);
});
it('colon notation in CSI params', () => {
test('\x1b[<31;5::123:;8mHello World! öäü€\nabc',
[
['csi', '<', [31, 5, [-1, 123, -1], 8], 'm'],
['print', 'Hello World! öäü€'],
['exe', '\n'],
['print', 'abc']
], null);
});
it('colon notation in DCS params', function (): void {
test('abc\x901;2::55;3+$abc;de\x9c', [
['print', 'abc'],
['dcs hook', '+$', [1, 2, [-1, 55], 3], 'a'],
['dcs put', 'bc;de'],
['dcs unhook']
], null);
});
});
describe('coverage tests', function (): void {
@@ -1078,10 +1112,10 @@ describe('EscapeSequenceParser', function (): void {
let parser2: TestEscapeSequenceParser;
let print = '';
const esc: string[] = [];
const csi: [string, number[], string][] = [];
const csi: [string, ParamsArray, string][] = [];
const exe: string[] = [];
const osc: [number, string][] = [];
const dcs: ([string] | [string, string] | [string, string, number[], number])[] = [];
const dcs: ([string] | [string, string] | [string, string, ParamsArray, number])[] = [];
function clearAccu(): void {
print = '';
esc.length = 0;
@@ -1128,8 +1162,8 @@ describe('EscapeSequenceParser', function (): void {
chai.expect(esc).eql([]);
});
it('CSI handler', function (): void {
parser2.setCsiHandler('m', function (params: number[], collect: string): void {
csi.push(['m', params, collect]);
parser2.setCsiHandler('m', function (params: IParams, collect: string): void {
csi.push(['m', params.toArray(), collect]);
});
parse(parser2, INPUT);
chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]);
@@ -1141,38 +1175,38 @@ describe('EscapeSequenceParser', function (): void {
});
describe('CSI custom handlers', () => {
it('Prevent fallback', () => {
const csiCustom: [string, number[], string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; });
const csiCustom: [string, ParamsArray, string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params.toArray(), collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params.toArray(), collect]); return true; });
parse(parser2, INPUT);
chai.expect(csi).eql([], 'Should not fallback to original handler');
chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]);
});
it('Allow fallback', () => {
const csiCustom: [string, number[], string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return false; });
const csiCustom: [string, ParamsArray, string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params.toArray(), collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params.toArray(), collect]); return false; });
parse(parser2, INPUT);
chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']], 'Should fallback to original handler');
chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]);
});
it('Multiple custom handlers fallback once', () => {
const csiCustom: [string, number[], string][] = [];
const csiCustom2: [string, number[], string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; });
parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params, collect]); return false; });
const csiCustom: [string, ParamsArray, string][] = [];
const csiCustom2: [string, ParamsArray, string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params.toArray(), collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params.toArray(), collect]); return true; });
parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params.toArray(), collect]); return false; });
parse(parser2, INPUT);
chai.expect(csi).eql([], 'Should not fallback to original handler');
chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]);
chai.expect(csiCustom2).eql([['m', [1, 31], ''], ['m', [0], '']]);
});
it('Multiple custom handlers no fallback', () => {
const csiCustom: [string, number[], string][] = [];
const csiCustom2: [string, number[], string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; });
parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params, collect]); return true; });
const csiCustom: [string, ParamsArray, string][] = [];
const csiCustom2: [string, ParamsArray, string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params.toArray(), collect]));
parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params.toArray(), collect]); return true; });
parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params.toArray(), collect]); return true; });
parse(parser2, INPUT);
chai.expect(csi).eql([], 'Should not fallback to original handler');
chai.expect(csiCustom).eql([], 'Should not fallback once');
@@ -1187,18 +1221,18 @@ describe('EscapeSequenceParser', function (): void {
chai.expect(order).eql([3, 2, 1]);
});
it('Dispose should work', () => {
const csiCustom: [string, number[], string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect]));
const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; });
const csiCustom: [string, ParamsArray, string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params.toArray(), collect]));
const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params.toArray(), collect]); return true; });
customHandler.dispose();
parse(parser2, INPUT);
chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]);
chai.expect(csiCustom).eql([], 'Should not use custom handler as it was disposed');
});
it('Should not corrupt the parser when dispose is called twice', () => {
const csiCustom: [string, number[], string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect]));
const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; });
const csiCustom: [string, ParamsArray, string][] = [];
parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params.toArray(), collect]));
const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params.toArray(), collect]); return true; });
customHandler.dispose();
customHandler.dispose();
parse(parser2, INPUT);
@@ -1302,8 +1336,8 @@ describe('EscapeSequenceParser', function (): void {
});
it('DCS handler', function (): void {
parser2.setDcsHandler('+p', {
hook: function (collect: string, params: number[], flag: number): void {
dcs.push(['hook', collect, params, flag]);
hook: function (collect: string, params: IParams, flag: number): void {
dcs.push(['hook', collect, params.toArray(), flag]);
},
put: function (data: Uint32Array, start: number, end: number): void {
let s = '';
@@ -1343,7 +1377,7 @@ describe('EscapeSequenceParser', function (): void {
currentState: ParserState.CSI_PARAM,
osc: '',
collect: '',
params: [1, 2, 0], // extra zero here
params: Params.fromArray([1, 2, 0]), // extra zero here
abort: false
});
parser2.clearErrorHandler();
+56 -32
View File
@@ -3,18 +3,19 @@
* @license MIT
*/
import { IParsingState, IDcsHandler, IEscapeSequenceParser } from 'common/parser/Types';
import { IParsingState, IDcsHandler, IEscapeSequenceParser, IParams } from 'common/parser/Types';
import { ParserState, ParserAction } from 'common/parser/Constants';
import { Disposable } from 'common/Lifecycle';
import { utf32ToString } from 'common/input/TextDecoder';
import { IDisposable } from 'common/Types';
import { fill } from 'common/TypedArrayUtils';
import { Params } from 'common/parser/Params';
interface IHandlerCollection<T> {
[key: string]: T[];
}
type CsiHandler = (params: number[], collect: string) => boolean | void;
type CsiHandler = (params: IParams, collect: string) => boolean | void;
type OscHandler = (data: string) => boolean | void;
/**
@@ -143,17 +144,14 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable {
// csi entries
table.add(0x5b, ParserState.ESCAPE, ParserAction.CLEAR, ParserState.CSI_ENTRY);
table.addMany(r(0x40, 0x7f), ParserState.CSI_ENTRY, ParserAction.CSI_DISPATCH, ParserState.GROUND);
table.addMany(r(0x30, 0x3a), ParserState.CSI_ENTRY, ParserAction.PARAM, ParserState.CSI_PARAM);
table.add(0x3b, ParserState.CSI_ENTRY, ParserAction.PARAM, ParserState.CSI_PARAM);
table.addMany(r(0x30, 0x3c), ParserState.CSI_ENTRY, ParserAction.PARAM, ParserState.CSI_PARAM);
table.addMany([0x3c, 0x3d, 0x3e, 0x3f], ParserState.CSI_ENTRY, ParserAction.COLLECT, ParserState.CSI_PARAM);
table.addMany(r(0x30, 0x3a), ParserState.CSI_PARAM, ParserAction.PARAM, ParserState.CSI_PARAM);
table.add(0x3b, ParserState.CSI_PARAM, ParserAction.PARAM, ParserState.CSI_PARAM);
table.addMany(r(0x30, 0x3c), ParserState.CSI_PARAM, ParserAction.PARAM, ParserState.CSI_PARAM);
table.addMany(r(0x40, 0x7f), ParserState.CSI_PARAM, ParserAction.CSI_DISPATCH, ParserState.GROUND);
table.addMany([0x3a, 0x3c, 0x3d, 0x3e, 0x3f], ParserState.CSI_PARAM, ParserAction.IGNORE, ParserState.CSI_IGNORE);
table.addMany([0x3c, 0x3d, 0x3e, 0x3f], ParserState.CSI_PARAM, ParserAction.IGNORE, ParserState.CSI_IGNORE);
table.addMany(r(0x20, 0x40), ParserState.CSI_IGNORE, ParserAction.IGNORE, ParserState.CSI_IGNORE);
table.add(0x7f, ParserState.CSI_IGNORE, ParserAction.IGNORE, ParserState.CSI_IGNORE);
table.addMany(r(0x40, 0x7f), ParserState.CSI_IGNORE, ParserAction.IGNORE, ParserState.GROUND);
table.add(0x3a, ParserState.CSI_ENTRY, ParserAction.IGNORE, ParserState.CSI_IGNORE);
table.addMany(r(0x20, 0x30), ParserState.CSI_ENTRY, ParserAction.COLLECT, ParserState.CSI_INTERMEDIATE);
table.addMany(r(0x20, 0x30), ParserState.CSI_INTERMEDIATE, ParserAction.COLLECT, ParserState.CSI_INTERMEDIATE);
table.addMany(r(0x30, 0x40), ParserState.CSI_INTERMEDIATE, ParserAction.IGNORE, ParserState.CSI_IGNORE);
@@ -173,9 +171,7 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable {
table.add(0x7f, ParserState.DCS_ENTRY, ParserAction.IGNORE, ParserState.DCS_ENTRY);
table.addMany(r(0x1c, 0x20), ParserState.DCS_ENTRY, ParserAction.IGNORE, ParserState.DCS_ENTRY);
table.addMany(r(0x20, 0x30), ParserState.DCS_ENTRY, ParserAction.COLLECT, ParserState.DCS_INTERMEDIATE);
table.add(0x3a, ParserState.DCS_ENTRY, ParserAction.IGNORE, ParserState.DCS_IGNORE);
table.addMany(r(0x30, 0x3a), ParserState.DCS_ENTRY, ParserAction.PARAM, ParserState.DCS_PARAM);
table.add(0x3b, ParserState.DCS_ENTRY, ParserAction.PARAM, ParserState.DCS_PARAM);
table.addMany(r(0x30, 0x3c), ParserState.DCS_ENTRY, ParserAction.PARAM, ParserState.DCS_PARAM);
table.addMany([0x3c, 0x3d, 0x3e, 0x3f], ParserState.DCS_ENTRY, ParserAction.COLLECT, ParserState.DCS_PARAM);
table.addMany(EXECUTABLES, ParserState.DCS_IGNORE, ParserAction.IGNORE, ParserState.DCS_IGNORE);
table.addMany(r(0x20, 0x80), ParserState.DCS_IGNORE, ParserAction.IGNORE, ParserState.DCS_IGNORE);
@@ -183,9 +179,8 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable {
table.addMany(EXECUTABLES, ParserState.DCS_PARAM, ParserAction.IGNORE, ParserState.DCS_PARAM);
table.add(0x7f, ParserState.DCS_PARAM, ParserAction.IGNORE, ParserState.DCS_PARAM);
table.addMany(r(0x1c, 0x20), ParserState.DCS_PARAM, ParserAction.IGNORE, ParserState.DCS_PARAM);
table.addMany(r(0x30, 0x3a), ParserState.DCS_PARAM, ParserAction.PARAM, ParserState.DCS_PARAM);
table.add(0x3b, ParserState.DCS_PARAM, ParserAction.PARAM, ParserState.DCS_PARAM);
table.addMany([0x3a, 0x3c, 0x3d, 0x3e, 0x3f], ParserState.DCS_PARAM, ParserAction.IGNORE, ParserState.DCS_IGNORE);
table.addMany(r(0x30, 0x3c), ParserState.DCS_PARAM, ParserAction.PARAM, ParserState.DCS_PARAM);
table.addMany([0x3c, 0x3d, 0x3e, 0x3f], ParserState.DCS_PARAM, ParserAction.IGNORE, ParserState.DCS_IGNORE);
table.addMany(r(0x20, 0x30), ParserState.DCS_PARAM, ParserAction.COLLECT, ParserState.DCS_INTERMEDIATE);
table.addMany(EXECUTABLES, ParserState.DCS_INTERMEDIATE, ParserAction.IGNORE, ParserState.DCS_INTERMEDIATE);
table.add(0x7f, ParserState.DCS_INTERMEDIATE, ParserAction.IGNORE, ParserState.DCS_INTERMEDIATE);
@@ -212,7 +207,7 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable {
* Dummy DCS handler as default fallback.
*/
class DcsDummy implements IDcsHandler {
hook(collect: string, params: number[], flag: number): void { }
hook(collect: string, params: IParams, flag: number): void { }
put(data: Uint32Array, start: number, end: number): void { }
unhook(): void { }
}
@@ -221,12 +216,22 @@ class DcsDummy implements IDcsHandler {
* EscapeSequenceParser.
* This class implements the ANSI/DEC compatible parser described by
* Paul Williams (https://vt100.net/emu/dec_ansi_parser).
*
* To implement custom ANSI compliant escape sequences it is not needed to
* alter this parser, instead consider registering a custom handler.
* For non ANSI compliant sequences change the transition table with
* the optional `transitions` contructor argument and
* reimplement the `parse` method.
* NOTE: The parameter element notation is currently not supported.
*
* This parser is currently hardcoded to operate in ZDM (Zero Default Mode)
* as suggested by the original parser, thus empty parameters are set to 0.
* This this is not in line with the latest ECMA specification
* (ZDM was part of the early specs and got completely removed later on).
*
* Other than the original parser from vt100.net this parser supports
* sub parameters in digital parameters separated by colons. Empty sub parameters
* are set to -1.
*
* TODO: implement error recovery hook via error handler return values
*/
export class EscapeSequenceParser extends Disposable implements IEscapeSequenceParser {
@@ -236,7 +241,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
// buffers over several parse calls
protected _osc: string;
protected _params: number[];
protected _params: Params;
protected _collect: string;
// handler lookup containers
@@ -252,7 +257,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
// fallback handlers
protected _printHandlerFb: (data: Uint32Array, start: number, end: number) => void;
protected _executeHandlerFb: (code: number) => void;
protected _csiHandlerFb: (collect: string, params: number[], flag: number) => void;
protected _csiHandlerFb: (collect: string, params: IParams, flag: number) => void;
protected _escHandlerFb: (collect: string, flag: number) => void;
protected _oscHandlerFb: (identifier: number, data: string) => void;
protected _dcsHandlerFb: IDcsHandler;
@@ -264,14 +269,15 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
this.initialState = ParserState.GROUND;
this.currentState = this.initialState;
this._osc = '';
this._params = [0];
this._params = new Params(); // defaults to 32 storable params/subparams
this._params.addParam(0); // ZDM
this._collect = '';
this.precedingCodepoint = 0;
// set default fallback handlers and handler lookup containers
this._printHandlerFb = (data, start, end): void => { };
this._executeHandlerFb = (code: number): void => { };
this._csiHandlerFb = (collect: string, params: number[], flag: number): void => { };
this._csiHandlerFb = (collect: string, params: IParams, flag: number): void => { };
this._escHandlerFb = (collect: string, flag: number): void => { };
this._oscHandlerFb = (identifier: number, data: string): void => { };
this._dcsHandlerFb = new DcsDummy();
@@ -329,13 +335,13 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
}
};
}
setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void {
setCsiHandler(flag: string, callback: (params: IParams, collect: string) => void): void {
this._csiHandlers[flag.charCodeAt(0)] = [callback];
}
clearCsiHandler(flag: string): void {
if (this._csiHandlers[flag.charCodeAt(0)]) delete this._csiHandlers[flag.charCodeAt(0)];
}
setCsiHandlerFallback(callback: (collect: string, params: number[], flag: number) => void): void {
setCsiHandlerFallback(callback: (collect: string, params: IParams, flag: number) => void): void {
this._csiHandlerFb = callback;
}
@@ -394,7 +400,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
reset(): void {
this.currentState = this.initialState;
this._osc = '';
this._params = [0];
this._params.reset();
this._params.addParam(0); // ZDM
this._collect = '';
this._activeDcsHandler = null;
this.precedingCodepoint = 0;
@@ -402,10 +409,12 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
/**
* Parse UTF32 codepoints in `data` up to `length`.
*
* Note: For several actions with high data load the parsing is optimized
* by using local read ahead loops with hardcoded conditions to
* avoid costly table lookups. Make sure that any change of table values
* will be reflected in the loop conditions as well. Affected states/actions:
* will be reflected in the loop conditions as well and vice versa.
* Affected states/actions:
* - GROUND:PRINT
* - CSI_PARAM:PARAM
* - DCS_PARAM:PARAM
@@ -418,7 +427,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
let currentState = this.currentState;
let osc = this._osc;
let collect = this._collect;
let params = this._params;
const params = this._params;
const table: Uint8Array = this.TRANSITIONS.table;
let dcsHandler: IDcsHandler | null = this._activeDcsHandler;
let callback: Function | null = null;
@@ -494,11 +503,23 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
this.precedingCodepoint = 0;
break;
case ParserAction.PARAM:
// inner loop: digits (0x30 - 0x39) and ; (0x3b)
// inner loop: digits (0x30 - 0x39) and ; (0x3b) and : (0x3a)
let isSub = false;
do {
if (code === 0x3b) params.push(0);
else params[params.length - 1] = params[params.length - 1] * 10 + code - 48;
} while (++i < length && (code = data[i]) > 0x2f && (code < 0x3a || code === 0x3b));
switch (code) {
case 0x3b:
params.addParam(0); // ZDM
isSub = false;
break;
case 0x3a:
params.addSubParam(-1);
isSub = true;
break;
default: // 0x30 - 0x39
if (isSub) params.addSubParamDigit(code - 48);
else params.addParamDigit(code - 48);
}
} while (++i < length && (code = data[i]) > 0x2f && code < 0x3c);
i--;
break;
case ParserAction.COLLECT:
@@ -512,7 +533,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
break;
case ParserAction.CLEAR:
osc = '';
params = [0];
params.reset();
params.addParam(0); // ZDM
collect = '';
break;
case ParserAction.DCS_HOOK:
@@ -540,7 +562,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
}
if (code === 0x1b) transition |= ParserState.ESCAPE;
osc = '';
params = [0];
params.reset();
params.addParam(0); // ZDM
collect = '';
this.precedingCodepoint = 0;
break;
@@ -586,7 +609,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP
}
if (code === 0x1b) transition |= ParserState.ESCAPE;
osc = '';
params = [0];
params.reset();
params.addParam(0); // ZDM
collect = '';
this.precedingCodepoint = 0;
break;
+220
View File
@@ -0,0 +1,220 @@
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { assert } from 'chai';
import { Params } from 'common/parser/Params';
import { ParamsArray } from 'common/parser/Types';
class TestParams extends Params {
public get subParams(): Int32Array {
return this._subParams;
}
public get subParamsLength(): number {
return this._subParamsLength;
}
}
/** `Params` parser shim */
function parse(params: Params, s: string): void {
params.reset();
params.addParam(0);
let isSub = false;
for (let i = 0; i < s.length; ++i) {
let code = s.charCodeAt(i);
do {
switch (code) {
case 0x3b:
params.addParam(0);
isSub = false;
break;
case 0x3a:
params.addSubParam(-1);
isSub = true;
break;
default: // 0x30 - 0x39
if (isSub) params.addSubParamDigit(code - 48);
else params.addParamDigit(code - 48);
}
} while (++i < s.length && (code = s.charCodeAt(i)) > 0x2f && code < 0x3c);
}
}
describe('Params', () => {
it('should respect ctor args', () => {
const params = new TestParams(12, 23);
assert.equal(params.params.length, 12);
assert.equal(params.subParams.length, 23);
assert.deepEqual(params.toArray(), []);
});
it('addParam', () => {
const params = new TestParams();
params.addParam(1);
assert.equal(params.length, 1);
assert.deepEqual(Array.prototype.slice.call(params.params, 0, params.length), [1]);
assert.deepEqual(params.toArray(), [1]);
params.addParam(23);
assert.equal(params.length, 2);
assert.deepEqual(Array.prototype.slice.call(params.params, 0, params.length), [1, 23]);
assert.deepEqual(params.toArray(), [1, 23]);
assert.equal(params.subParamsLength, 0);
});
it('addSubParam', () => {
const params = new TestParams();
params.addParam(1);
params.addSubParam(2);
params.addSubParam(3);
assert.equal(params.length, 1);
assert.equal(params.subParamsLength, 2);
assert.deepEqual(params.toArray(), [1, [2, 3]]);
params.addParam(12345);
params.addSubParam(-1);
assert.equal(params.length, 2);
assert.equal(params.subParamsLength, 3);
assert.deepEqual(params.toArray(), [1, [2, 3], 12345, [-1]]);
});
it('should not add sub params without previous param', () => {
const params = new TestParams();
params.addSubParam(2);
params.addSubParam(3);
assert.equal(params.length, 0);
assert.equal(params.subParamsLength, 0);
assert.deepEqual(params.toArray(), []);
params.addParam(1);
params.addSubParam(2);
params.addSubParam(3);
assert.equal(params.length, 1);
assert.equal(params.subParamsLength, 2);
assert.deepEqual(params.toArray(), [1, [2, 3]]);
});
it('reset', () => {
const params = new TestParams();
params.addParam(1);
params.addSubParam(2);
params.addSubParam(3);
params.addParam(12345);
params.addSubParam(-1);
params.reset();
assert.equal(params.length, 0);
assert.equal(params.subParamsLength, 0);
assert.deepEqual(params.toArray(), []);
params.addParam(1);
params.addSubParam(2);
params.addSubParam(3);
params.addParam(12345);
params.addSubParam(-1);
assert.equal(params.length, 2);
assert.equal(params.subParamsLength, 3);
assert.deepEqual(params.toArray(), [1, [2, 3], 12345, [-1]]);
});
it('Params.fromArray --> toArray', () => {
let data: ParamsArray = [];
assert.deepEqual(Params.fromArray(data).toArray(), data);
data = [1, [2, 3], 12345, [-1]];
assert.deepEqual(Params.fromArray(data).toArray(), data);
data = [38, 2, 50, 100, 150];
assert.deepEqual(Params.fromArray(data).toArray(), data);
data = [38, 2, 50, 100, [150]];
assert.deepEqual(Params.fromArray(data).toArray(), data);
data = [38, [2, 50, 100, 150]];
assert.deepEqual(Params.fromArray(data).toArray(), data);
// strip empty sub params
data = [38, [2, 50, 100, 150], 5, [], 6];
assert.deepEqual(Params.fromArray(data).toArray(), [38, [2, 50, 100, 150], 5, 6]);
});
it('clone', () => {
const params = Params.fromArray([38, [2, 50, 100, 150], 5, [], 6, 1, [2, 3], 12345, [-1]]);
assert.deepEqual(params.clone(), params);
});
it('hasSubParams / getSubParams', () => {
const params = Params.fromArray([38, [2, 50, 100, 150], 5, [], 6]);
assert.equal(params.hasSubParams(0), true);
assert.deepEqual(params.getSubParams(0), new Int32Array([2, 50, 100, 150]));
assert.equal(params.hasSubParams(1), false);
assert.deepEqual(params.getSubParams(1), null);
assert.equal(params.hasSubParams(2), false);
assert.deepEqual(params.getSubParams(2), null);
});
it('getSubParamsAll', () => {
const params = Params.fromArray([1, [2, 3], 7, 12345, [-1]]);
assert.deepEqual(params.getSubParamsAll(), {0: new Int32Array([2, 3]), 2: new Int32Array([-1])});
});
describe('parse tests', () => {
it('param defaults to 0 (ZDM - zero default mode)', () => {
const params = new Params();
parse(params, '');
assert.deepEqual(params.toArray(), [0]);
});
it('sub param defaults to -1', () => {
const params = new Params();
parse(params, ':');
assert.deepEqual(params.toArray(), [0, [-1]]);
});
it('should correctly reset on new sequence', () => {
const params = new Params();
parse(params, '1;2;3');
assert.deepEqual(params.toArray(), [1, 2, 3]);
parse(params, '4');
assert.deepEqual(params.toArray(), [4]);
parse(params, '4::123:5;6;7');
assert.deepEqual(params.toArray(), [4, [-1, 123, 5], 6, 7]);
parse(params, '');
assert.deepEqual(params.toArray(), [0]);
});
it('should handle length restrictions correctly', () => {
// restrict to 3 params and 3 sub params
const params = new Params(3, 3);
parse(params, '1;2;3');
assert.deepEqual(params.toArray(), [1, 2, 3]);
parse(params, '4');
assert.deepEqual(params.toArray(), [4]);
parse(params, '4::123:5;6;7');
assert.deepEqual(params.toArray(), [4, [-1, 123, 5], 6, 7]);
parse(params, '');
assert.deepEqual(params.toArray(), [0]);
// overlong params
parse(params, '1;2;3;4;5;6;7');
assert.deepEqual(params.toArray(), [1, 2, 3]);
// overlong sub params
parse(params, '4;38:2::50:100:150;48:5:22');
assert.deepEqual(params.toArray(), [4, 38, [2, -1, 50], 48]);
});
it('typical sequences', () => {
const params = new Params();
// SGR with semicolon syntax
parse(params, '0;4;38;2;50;100;150;48;5;22');
assert.deepEqual(params.toArray(), [0, 4, 38, 2, 50, 100, 150, 48, 5, 22]);
// SGR mixed style (partly wrong)
parse(params, '0;4;38;2;50:100:150;48;5:22');
assert.deepEqual(params.toArray(), [0, 4, 38, 2, 50, [100, 150], 48, 5, [22]]);
// SGR colon style
parse(params, '0;4;38:2::50:100:150;48:5:22');
assert.deepEqual(params.toArray(), [0, 4, 38, [2, -1, 50, 100, 150], 48, [5, 22]]);
});
});
describe('should not overflow to negative', () => {
it('reject params lesser -1', () => {
const params = new Params();
params.addParam(-1);
assert.throws(() => params.addParam(-2), 'values lesser than -1 are not allowed');
});
it('reject subparams lesser -1', () => {
const params = new Params();
params.addParam(-1);
params.addSubParam(-1);
assert.throws(() => params.addSubParam(-2), 'values lesser than -1 are not allowed');
assert.deepEqual(params.toArray(), [-1, [-1]]);
});
it('clamp parsed params', () => {
const params = new Params();
parse(params, '2147483648');
assert.deepEqual(params.toArray(), [0x7FFFFFFF]);
});
it('clamp parsed subparams', () => {
const params = new Params();
parse(params, ':2147483648');
assert.deepEqual(params.toArray(), [0, [0x7FFFFFFF]]);
});
});
});
+233
View File
@@ -0,0 +1,233 @@
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { IParams, ParamsArray } from 'common/parser/Types';
// max value supported for a single param/subparam (clamped to positive int32 range)
const MAX_VALUE = 0x7FFFFFFF;
// max allowed subparams for a single sequence (hardcoded limitation)
const MAX_SUBPARAMS = 256;
/**
* Params storage class.
* This type is used by the parser to accumulate sequence parameters and sub parameters
* and transmit them to the input handler actions.
*
* NOTES:
* - params object for action handlers is borrowed, use `.toArray` or `.clone` to get a copy
* - never read beyond `params.length - 1` (likely to contain arbitrary data)
* - `.getSubParams` returns a borrowed typed array, use `.getSubParamsAll` for cloned sub params
* - hardcoded limitations:
* - max. value for a single (sub) param is 2^31 - 1 (greater values are clamped to that)
* - max. 256 sub params possible
* - negative values are not allowed beside -1 (placeholder for default value)
*
* About ZDM (Zero Default Mode):
* ZDM is not orchestrated by this class. If the parser is in ZDM,
* it should add 0 for empty params, otherwise -1. This does not apply
* to subparams, empty subparams should always be added with -1.
*/
export class Params implements IParams {
// params store and length
public params: Int32Array;
public length: number;
// sub params store and length
protected _subParams: Int32Array;
protected _subParamsLength: number;
// sub params offsets from param: param idx --> [start, end] offset
private _subParamsIdx: Uint16Array;
private _rejectDigits: boolean;
private _rejectSubDigits: boolean;
/**
* Create a `Params` type from JS array representation.
*/
public static fromArray(values: ParamsArray): Params {
const params = new Params();
if (!values.length) {
return params;
}
// skip leading sub params
for (let i = (values[0] instanceof Array) ? 1 : 0; i < values.length; ++i) {
const value = values[i];
if (value instanceof Array) {
for (let k = 0; k < value.length; ++k) {
params.addSubParam(value[k]);
}
} else {
params.addParam(value);
}
}
return params;
}
/**
* @param maxLength max length of storable parameters
* @param maxSubParamsLength max length of storable sub parameters
*/
constructor(public maxLength: number = 32, public maxSubParamsLength: number = 32) {
if (maxSubParamsLength > MAX_SUBPARAMS) {
throw new Error('maxSubParamsLength must not be greater than 256');
}
this.params = new Int32Array(maxLength);
this.length = 0;
this._subParams = new Int32Array(maxSubParamsLength);
this._subParamsLength = 0;
this._subParamsIdx = new Uint16Array(maxLength);
this._rejectDigits = false;
this._rejectSubDigits = false;
}
/**
* Clone object.
*/
public clone(): Params {
const newParams = new Params(this.maxLength, this.maxSubParamsLength);
newParams.params.set(this.params);
newParams.length = this.length;
newParams._subParams.set(this._subParams);
newParams._subParamsLength = this._subParamsLength;
newParams._subParamsIdx.set(this._subParamsIdx);
return newParams;
}
/**
* Get a JS array representation of the current parameters and sub parameters.
* The array is structured as follows:
* sequence: "1;2:3:4;5::6"
* array : [1, 2, [3, 4], 5, [-1, 6]]
*/
public toArray(): ParamsArray {
const res: ParamsArray = [];
for (let i = 0; i < this.length; ++i) {
res.push(this.params[i]);
const start = this._subParamsIdx[i] >> 8;
const end = this._subParamsIdx[i] & 0xFF;
if (end - start > 0) {
res.push(Array.prototype.slice.call(this._subParams, start, end));
}
}
return res;
}
/**
* Reset to initial empty state.
*/
public reset(): void {
this.length = 0;
this._subParamsLength = 0;
this._rejectDigits = false;
this._rejectSubDigits = false;
}
/**
* Add a parameter value.
* `Params` only stores up to `maxLength` parameters, any later
* parameter will be ignored.
* Note: VT devices only stored up to 16 values, xterm seems to
* store up to 30.
*/
public addParam(value: number): void {
if (this.length >= this.maxLength) {
this._rejectDigits = true;
return;
}
if (value < -1) {
throw new Error('values lesser than -1 are not allowed');
}
this._subParamsIdx[this.length] = this._subParamsLength << 8 | this._subParamsLength;
this.params[this.length++] = value > MAX_VALUE ? MAX_VALUE : value;
}
/**
* Add a sub parameter value.
* The sub parameter is automatically associated with the last parameter value.
* Thus it is not possible to add a subparameter without any parameter added yet.
* `Params` only stores up to `subParamsLength` sub parameters, any later
* sub parameter will be ignored.
*/
public addSubParam(value: number): void {
if (!this.length) {
return;
}
if (this._subParamsLength >= this.maxSubParamsLength) {
this._rejectSubDigits = true;
return;
}
if (value < -1) {
throw new Error('values lesser than -1 are not allowed');
}
this._subParams[this._subParamsLength++] = value > MAX_VALUE ? MAX_VALUE : value;
this._subParamsIdx[this.length - 1]++;
}
/**
* Whether parameter at index `idx` has sub parameters.
*/
public hasSubParams(idx: number): boolean {
return ((this._subParamsIdx[idx] & 0xFF) - (this._subParamsIdx[idx] >> 8) > 0);
}
/**
* Return sub parameters for parameter at index `idx`.
* Note: The values are borrowed, thus you need to copy
* the values if you need to hold them in nonlocal scope.
*/
public getSubParams(idx: number): Int32Array | null {
const start = this._subParamsIdx[idx] >> 8;
const end = this._subParamsIdx[idx] & 0xFF;
if (end - start > 0) {
return this._subParams.subarray(start, end);
}
return null;
}
/**
* Return all sub parameters as {idx: subparams} mapping.
* Note: The values are not borrowed.
*/
public getSubParamsAll(): {[idx: number]: Int32Array} {
const result: {[idx: number]: Int32Array} = {};
for (let i = 0; i < this.length; ++i) {
const start = this._subParamsIdx[i] >> 8;
const end = this._subParamsIdx[i] & 0xFF;
if (end - start > 0) {
result[i] = this._subParams.slice(start, end);
}
}
return result;
}
/**
* Add a single digit value to current parameter.
* This is used by the parser to account digits on a char by char basis.
* Do not use this method directly, consider using `addParam` instead.
*/
public addParamDigit(value: number): void {
if (this._rejectDigits) {
return;
}
const v = this.params[this.length - 1] * 10 + value;
this.params[this.length - 1] = v > MAX_VALUE ? MAX_VALUE : v;
}
/**
* Add a single digit value to current sub parameter.
* This is used by the parser to account digits on a char by char basis.
* Do not use this method directly, consider using `addSubParam` instead.
*/
public addSubParamDigit(value: number): void {
if (!this._subParamsLength || this._rejectDigits || this._rejectSubDigits) {
return;
}
if (this._subParams[this._subParamsLength - 1] === -1) {
this._subParams[this._subParamsLength - 1] = value;
} else {
const v = this._subParams[this._subParamsLength - 1] * 10 + value;
this._subParams[this._subParamsLength - 1] = v > MAX_VALUE ? MAX_VALUE : v;
}
}
}
+39 -6
View File
@@ -6,6 +6,38 @@
import { IDisposable } from 'common/Types';
import { ParserState } from 'common/parser/Constants';
/** sequence params serialized to js arrays */
export type ParamsArray = (number | number[])[];
/** Params constructor type. */
export interface IParamsConstructor {
new(maxLength: number, maxSubParamsLength: number): IParams;
/** create params from ParamsArray */
fromArray(values: ParamsArray): IParams;
}
/** Interface of Params storage class. */
export interface IParams {
/** from ctor */
maxLength: number;
maxSubParamsLength: number;
/** param values and its length */
params: Int32Array;
length: number;
/** methods */
clone(): IParams;
toArray(): ParamsArray;
reset(): void;
addParam(value: number): void;
addSubParam(value: number): void;
hasSubParams(idx: number): boolean;
getSubParams(idx: number): Int32Array | null;
getSubParamsAll(): {[idx: number]: Int32Array};
}
/**
* Internal state of EscapeSequenceParser.
* Used as argument of the error handler to allow
@@ -26,7 +58,7 @@ export interface IParsingState {
// collect buffer with intermediate characters
collect: string;
// params buffer
params: number[];
params: IParams;
// should abort (default: false)
abort: boolean;
}
@@ -53,7 +85,7 @@ export interface IParsingState {
* `unhook` marks the end of the current DCS sequence.
*/
export interface IDcsHandler {
hook(collect: string, params: number[], flag: number): void;
hook(collect: string, params: IParams, flag: number): void;
put(data: Uint32Array, start: number, end: number): void;
unhook(): void;
}
@@ -68,13 +100,14 @@ export interface IEscapeSequenceParser extends IDisposable {
* It gets reset by the parser for any valid sequence beside REP itself.
*/
precedingCodepoint: number;
/**
* Reset the parser to its initial state (handlers are kept).
*/
reset(): void;
/**
* Parse string `data`.
* Parse UTF32 codepoints in `data` up to `length`.
* @param data The data to parse.
*/
parse(data: Uint32Array, length: number): void;
@@ -86,10 +119,10 @@ export interface IEscapeSequenceParser extends IDisposable {
clearExecuteHandler(flag: string): void;
setExecuteHandlerFallback(callback: (code: number) => void): void;
setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void;
setCsiHandler(flag: string, callback: (params: IParams, collect: string) => void): void;
clearCsiHandler(flag: string): void;
setCsiHandlerFallback(callback: (collect: string, params: number[], flag: number) => void): void;
addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable;
setCsiHandlerFallback(callback: (collect: string, params: IParams, flag: number) => void): void;
addCsiHandler(flag: string, callback: (params: IParams, collect: string) => boolean): IDisposable;
addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable;
setEscHandler(collectAndFlag: string, callback: () => void): void;