xterm
[VSoRC/.git] / node_modules / xterm / src / common / parser / Types.d.ts
1 /**
2  * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { IDisposable } from 'common/Types';
7 import { ParserState } from 'common/parser/Constants';
8
9 /** sequence params serialized to js arrays */
10 export type ParamsArray = (number | number[])[];
11
12 /** Params constructor type. */
13 export interface IParamsConstructor {
14   new(maxLength: number, maxSubParamsLength: number): IParams;
15
16   /** create params from ParamsArray */
17   fromArray(values: ParamsArray): IParams;
18 }
19
20 /** Interface of Params storage class. */
21 export interface IParams {
22   /** from ctor */
23   maxLength: number;
24   maxSubParamsLength: number;
25
26   /** param values and its length */
27   params: Int32Array;
28   length: number;
29
30   /** methods */
31   clone(): IParams;
32   toArray(): ParamsArray;
33   reset(): void;
34   addParam(value: number): void;
35   addSubParam(value: number): void;
36   hasSubParams(idx: number): boolean;
37   getSubParams(idx: number): Int32Array | null;
38   getSubParamsAll(): {[idx: number]: Int32Array};
39 }
40
41 /**
42  * Internal state of EscapeSequenceParser.
43  * Used as argument of the error handler to allow
44  * introspection at runtime on parse errors.
45  * Return it with altered values to recover from
46  * faulty states (not yet supported).
47  * Set `abort` to `true` to abort the current parsing.
48  */
49 export interface IParsingState {
50   // position in parse string
51   position: number;
52   // actual character code
53   code: number;
54   // current parser state
55   currentState: ParserState;
56   // collect buffer with intermediate characters
57   collect: number;
58   // params buffer
59   params: IParams;
60   // should abort (default: false)
61   abort: boolean;
62 }
63
64 /**
65  * Command handler interfaces.
66  */
67
68 /**
69  * CSI handler types.
70  * Note: `params` is borrowed.
71  */
72 export type CsiHandlerType = (params: IParams) => boolean | void;
73 export type CsiFallbackHandlerType = (ident: number, params: IParams) => void;
74
75 /**
76  * DCS handler types.
77  */
78 export interface IDcsHandler {
79   /**
80    * Called when a DCS command starts.
81    * Prepare needed data structures here.
82    * Note: `params` is borrowed.
83    */
84   hook(params: IParams): void;
85   /**
86    * Incoming payload chunk.
87    * Note: `params` is borrowed.
88    */
89   put(data: Uint32Array, start: number, end: number): void;
90   /**
91    * End of DCS command. `success` indicates whether the
92    * command finished normally or got aborted, thus final
93    * execution of the command should depend on `success`.
94    * To save memory also cleanup data structures here.
95    */
96   unhook(success: boolean): void | boolean;
97 }
98 export type DcsFallbackHandlerType = (ident: number, action: 'HOOK' | 'PUT' | 'UNHOOK', payload?: any) => void;
99
100 /**
101  * ESC handler types.
102  */
103 export type EscHandlerType = () => boolean | void;
104 export type EscFallbackHandlerType = (identifier: number) => void;
105
106 /**
107  * EXECUTE handler types.
108  */
109 export type ExecuteHandlerType = () => boolean | void;
110 export type ExecuteFallbackHandlerType = (ident: number) => void;
111
112 /**
113  * OSC handler types.
114  */
115 export interface IOscHandler {
116   /**
117    * Announces start of this OSC command.
118    * Prepare needed data structures here.
119    */
120   start(): void;
121   /**
122    * Incoming data chunk.
123    * Note: Data is borrowed.
124    */
125   put(data: Uint32Array, start: number, end: number): void;
126   /**
127    * End of OSC command. `success` indicates whether the
128    * command finished normally or got aborted, thus final
129    * execution of the command should depend on `success`.
130    * To save memory also cleanup data structures here.
131    */
132   end(success: boolean): void | boolean;
133 }
134 export type OscFallbackHandlerType = (ident: number, action: 'START' | 'PUT' | 'END', payload?: any) => void;
135
136 /**
137  * PRINT handler types.
138  */
139 export type PrintHandlerType = (data: Uint32Array, start: number, end: number) => void;
140 export type PrintFallbackHandlerType = PrintHandlerType;
141
142
143 /**
144 * EscapeSequenceParser interface.
145 */
146 export interface IEscapeSequenceParser extends IDisposable {
147   /**
148    * Preceding codepoint to get REP working correctly.
149    * This must be set by the print handler as last action.
150    * It gets reset by the parser for any valid sequence beside REP itself.
151    */
152   precedingCodepoint: number;
153
154   /**
155    * Reset the parser to its initial state (handlers are kept).
156    */
157   reset(): void;
158
159   /**
160    * Parse UTF32 codepoints in `data` up to `length`.
161    * @param data The data to parse.
162    */
163   parse(data: Uint32Array, length: number): void;
164
165   /**
166    * Get string from numercial function identifier `ident`.
167    * Useful in fallback handlers which expose the low level
168    * numcerical function identifier for debugging purposes.
169    * Note: A full back translation to `IFunctionIdentifier`
170    * is not implemented.
171    */
172   identToString(ident: number): string;
173
174   setPrintHandler(handler: PrintHandlerType): void;
175   clearPrintHandler(): void;
176
177   setEscHandler(id: IFunctionIdentifier, handler: EscHandlerType): void;
178   clearEscHandler(id: IFunctionIdentifier): void;
179   setEscHandlerFallback(handler: EscFallbackHandlerType): void;
180   addEscHandler(id: IFunctionIdentifier, handler: EscHandlerType): IDisposable;
181
182   setExecuteHandler(flag: string, handler: ExecuteHandlerType): void;
183   clearExecuteHandler(flag: string): void;
184   setExecuteHandlerFallback(handler: ExecuteFallbackHandlerType): void;
185
186   setCsiHandler(id: IFunctionIdentifier, handler: CsiHandlerType): void;
187   clearCsiHandler(id: IFunctionIdentifier): void;
188   setCsiHandlerFallback(callback: CsiFallbackHandlerType): void;
189   addCsiHandler(id: IFunctionIdentifier, handler: CsiHandlerType): IDisposable;
190
191   setDcsHandler(id: IFunctionIdentifier, handler: IDcsHandler): void;
192   clearDcsHandler(id: IFunctionIdentifier): void;
193   setDcsHandlerFallback(handler: DcsFallbackHandlerType): void;
194   addDcsHandler(id: IFunctionIdentifier, handler: IDcsHandler): IDisposable;
195
196   setOscHandler(ident: number, handler: IOscHandler): void;
197   clearOscHandler(ident: number): void;
198   setOscHandlerFallback(handler: OscFallbackHandlerType): void;
199   addOscHandler(ident: number, handler: IOscHandler): IDisposable;
200
201   setErrorHandler(handler: (state: IParsingState) => IParsingState): void;
202   clearErrorHandler(): void;
203 }
204
205 /**
206  * Subparser interfaces.
207  * The subparsers are instantiated in `EscapeSequenceParser` and
208  * called during `EscapeSequenceParser.parse`.
209  */
210 export interface ISubParser<T, U> extends IDisposable {
211   reset(): void;
212   addHandler(ident: number, handler: T): IDisposable;
213   setHandler(ident: number, handler: T): void;
214   clearHandler(ident: number): void;
215   setHandlerFallback(handler: U): void;
216   put(data: Uint32Array, start: number, end: number): void;
217 }
218
219 export interface IOscParser extends ISubParser<IOscHandler, OscFallbackHandlerType> {
220   start(): void;
221   end(success: boolean): void;
222 }
223
224 export interface IDcsParser extends ISubParser<IDcsHandler, DcsFallbackHandlerType> {
225   hook(ident: number, params: IParams): void;
226   unhook(success: boolean): void;
227 }
228
229 /**
230  * Interface to denote a specific ESC, CSI or DCS handler slot.
231  * The values are used to create an integer respresentation during handler
232  * regristation before passed to the subparsers as `ident`.
233  * The integer translation is made to allow a faster handler access
234  * in `EscapeSequenceParser.parse`.
235  */
236 export interface IFunctionIdentifier {
237   prefix?: string;
238   intermediates?: string;
239   final: string;
240 }
241
242 export interface IHandlerCollection<T> {
243   [key: string]: T[];
244 }