5 import { C0 } from './EscapeSequences';
6 import { IInputHandler } from './Interfaces';
7 import { CHARSETS } from './Charsets';
9 const normalStateHandler: {[key: string]: (parser: Parser, handler: IInputHandler) => void} = {};
10 normalStateHandler[C0.BEL] = (parser, handler) => handler.bell();
11 normalStateHandler[C0.LF] = (parser, handler) => handler.lineFeed();
12 normalStateHandler[C0.VT] = normalStateHandler[C0.LF];
13 normalStateHandler[C0.FF] = normalStateHandler[C0.LF];
14 normalStateHandler[C0.CR] = (parser, handler) => handler.carriageReturn();
15 normalStateHandler[C0.BS] = (parser, handler) => handler.backspace();
16 normalStateHandler[C0.HT] = (parser, handler) => handler.tab();
17 normalStateHandler[C0.SO] = (parser, handler) => handler.shiftOut();
18 normalStateHandler[C0.SI] = (parser, handler) => handler.shiftIn();
19 normalStateHandler[C0.ESC] = (parser, handler) => parser.setState(ParserState.ESCAPED);
21 // TODO: Remove terminal when parser owns params and currentParam
22 const escapedStateHandler: {[key: string]: (parser: Parser, terminal: any) => void} = {};
23 escapedStateHandler['['] = (parser, terminal) => {
24 // ESC [ Control Sequence Introducer (CSI is 0x9b)
26 terminal.currentParam = 0;
27 parser.setState(ParserState.CSI_PARAM);
29 escapedStateHandler[']'] = (parser, terminal) => {
30 // ESC ] Operating System Command (OSC is 0x9d)
32 terminal.currentParam = 0;
33 parser.setState(ParserState.OSC);
35 escapedStateHandler['P'] = (parser, terminal) => {
36 // ESC P Device Control String (DCS is 0x90)
38 terminal.currentParam = 0;
39 parser.setState(ParserState.DCS);
41 escapedStateHandler['_'] = (parser, terminal) => {
42 // ESC _ Application Program Command ( APC is 0x9f).
43 parser.setState(ParserState.IGNORE);
45 escapedStateHandler['^'] = (parser, terminal) => {
46 // ESC ^ Privacy Message ( PM is 0x9e).
47 parser.setState(ParserState.IGNORE);
49 escapedStateHandler['c'] = (parser, terminal) => {
50 // ESC c Full Reset (RIS).
53 escapedStateHandler['E'] = (parser, terminal) => {
54 // ESC E Next Line ( NEL is 0x85).
57 parser.setState(ParserState.NORMAL);
59 escapedStateHandler['D'] = (parser, terminal) => {
60 // ESC D Index ( IND is 0x84).
62 parser.setState(ParserState.NORMAL);
64 escapedStateHandler['M'] = (parser, terminal) => {
65 // ESC M Reverse Index ( RI is 0x8d).
66 terminal.reverseIndex();
67 parser.setState(ParserState.NORMAL);
69 escapedStateHandler['%'] = (parser, terminal) => {
70 // ESC % Select default/utf-8 character set.
71 // @ = default, G = utf-8
72 terminal.setgLevel(0);
73 terminal.setgCharset(0, CHARSETS.US);
74 parser.setState(ParserState.NORMAL);
75 parser.skipNextChar();
78 const csiParamStateHandler: {[key: string]: (parser: Parser) => void} = {};
79 csiParamStateHandler['?'] = (parser) => parser.setPrefix('?');
80 csiParamStateHandler['>'] = (parser) => parser.setPrefix('>');
81 csiParamStateHandler['!'] = (parser) => parser.setPrefix('!');
82 csiParamStateHandler['0'] = (parser) => parser.setParam(parser.getParam() * 10);
83 csiParamStateHandler['1'] = (parser) => parser.setParam(parser.getParam() * 10 + 1);
84 csiParamStateHandler['2'] = (parser) => parser.setParam(parser.getParam() * 10 + 2);
85 csiParamStateHandler['3'] = (parser) => parser.setParam(parser.getParam() * 10 + 3);
86 csiParamStateHandler['4'] = (parser) => parser.setParam(parser.getParam() * 10 + 4);
87 csiParamStateHandler['5'] = (parser) => parser.setParam(parser.getParam() * 10 + 5);
88 csiParamStateHandler['6'] = (parser) => parser.setParam(parser.getParam() * 10 + 6);
89 csiParamStateHandler['7'] = (parser) => parser.setParam(parser.getParam() * 10 + 7);
90 csiParamStateHandler['8'] = (parser) => parser.setParam(parser.getParam() * 10 + 8);
91 csiParamStateHandler['9'] = (parser) => parser.setParam(parser.getParam() * 10 + 9);
92 csiParamStateHandler['$'] = (parser) => parser.setPostfix('$');
93 csiParamStateHandler['"'] = (parser) => parser.setPostfix('"');
94 csiParamStateHandler[' '] = (parser) => parser.setPostfix(' ');
95 csiParamStateHandler['\''] = (parser) => parser.setPostfix('\'');
96 csiParamStateHandler[';'] = (parser) => parser.finalizeParam();
98 const csiStateHandler: {[key: string]: (handler: IInputHandler, params: number[], prefix: string, postfix: string) => void} = {};
99 csiStateHandler['@'] = (handler, params, prefix) => handler.insertChars(params);
100 csiStateHandler['A'] = (handler, params, prefix) => handler.cursorUp(params);
101 csiStateHandler['B'] = (handler, params, prefix) => handler.cursorDown(params);
102 csiStateHandler['C'] = (handler, params, prefix) => handler.cursorForward(params);
103 csiStateHandler['D'] = (handler, params, prefix) => handler.cursorBackward(params);
104 csiStateHandler['E'] = (handler, params, prefix) => handler.cursorNextLine(params);
105 csiStateHandler['F'] = (handler, params, prefix) => handler.cursorPrecedingLine(params);
106 csiStateHandler['G'] = (handler, params, prefix) => handler.cursorCharAbsolute(params);
107 csiStateHandler['H'] = (handler, params, prefix) => handler.cursorPosition(params);
108 csiStateHandler['I'] = (handler, params, prefix) => handler.cursorForwardTab(params);
109 csiStateHandler['J'] = (handler, params, prefix) => handler.eraseInDisplay(params);
110 csiStateHandler['K'] = (handler, params, prefix) => handler.eraseInLine(params);
111 csiStateHandler['L'] = (handler, params, prefix) => handler.insertLines(params);
112 csiStateHandler['M'] = (handler, params, prefix) => handler.deleteLines(params);
113 csiStateHandler['P'] = (handler, params, prefix) => handler.deleteChars(params);
114 csiStateHandler['S'] = (handler, params, prefix) => handler.scrollUp(params);
115 csiStateHandler['T'] = (handler, params, prefix) => {
116 if (params.length < 2 && !prefix) {
117 handler.scrollDown(params);
120 csiStateHandler['X'] = (handler, params, prefix) => handler.eraseChars(params);
121 csiStateHandler['Z'] = (handler, params, prefix) => handler.cursorBackwardTab(params);
122 csiStateHandler['`'] = (handler, params, prefix) => handler.charPosAbsolute(params);
123 csiStateHandler['a'] = (handler, params, prefix) => handler.HPositionRelative(params);
124 csiStateHandler['b'] = (handler, params, prefix) => handler.repeatPrecedingCharacter(params);
125 csiStateHandler['c'] = (handler, params, prefix) => handler.sendDeviceAttributes(params);
126 csiStateHandler['d'] = (handler, params, prefix) => handler.linePosAbsolute(params);
127 csiStateHandler['e'] = (handler, params, prefix) => handler.VPositionRelative(params);
128 csiStateHandler['f'] = (handler, params, prefix) => handler.HVPosition(params);
129 csiStateHandler['g'] = (handler, params, prefix) => handler.tabClear(params);
130 csiStateHandler['h'] = (handler, params, prefix) => handler.setMode(params);
131 csiStateHandler['l'] = (handler, params, prefix) => handler.resetMode(params);
132 csiStateHandler['m'] = (handler, params, prefix) => handler.charAttributes(params);
133 csiStateHandler['n'] = (handler, params, prefix) => handler.deviceStatus(params);
134 csiStateHandler['p'] = (handler, params, prefix) => {
136 case '!': handler.softReset(params); break;
139 csiStateHandler['q'] = (handler, params, prefix, postfix) => {
140 if (postfix === ' ') {
141 handler.setCursorStyle(params);
144 csiStateHandler['r'] = (handler, params) => handler.setScrollRegion(params);
145 csiStateHandler['s'] = (handler, params) => handler.saveCursor(params);
146 csiStateHandler['u'] = (handler, params) => handler.restoreCursor(params);
148 // TODO: Many codes/charsets appear to not be supported
149 // See: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
152 'A': CHARSETS.UK, // United Kingdom
153 'B': CHARSETS.US, // United States (USASCII)
155 'C': CHARSETS.Finnish,
156 '5': CHARSETS.Finnish,
157 'f': CHARSETS.French,
158 'Q': CHARSETS.FrenchCanadian,
159 'K': CHARSETS.German,
160 'Y': CHARSETS.Italian,
161 'E': CHARSETS.NorwegianDanish,
162 '6': CHARSETS.NorwegianDanish,
163 'Z': CHARSETS.Spanish,
164 'H': CHARSETS.Swedish,
165 '7': CHARSETS.Swedish,
167 '/': CHARSETS.ISOLatin // ISOLatin is actually /A
182 * The terminal's parser, all input into the terminal goes through the parser
183 * which parses and defers the actual input handling the the IInputHandler
184 * specified in the constructor.
186 export class Parser {
187 private _state: ParserState;
188 private _position: number;
190 // TODO: Remove terminal when handler can do everything
192 private _inputHandler: IInputHandler,
193 private _terminal: any
195 this._state = ParserState.NORMAL;
199 * Parse and handle data.
201 * @param data The data to parse.
203 public parse(data: string) {
204 let l = data.length, j, cs, ch, code, low;
207 // apply leftover surrogate high from last write
208 if (this._terminal.surrogate_high) {
209 data = this._terminal.surrogate_high + data;
210 this._terminal.surrogate_high = '';
213 for (; this._position < l; this._position++) {
214 ch = data[this._position];
216 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
217 // --> maybe move to default
218 code = data.charCodeAt(this._position);
219 if (0xD800 <= code && code <= 0xDBFF) {
220 // we got a surrogate high
221 // get surrogate low (next 2 bytes)
222 low = data.charCodeAt(this._position + 1);
224 // end of data stream, save surrogate high
225 this._terminal.surrogate_high = ch;
228 code = ((code - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
229 ch += data.charAt(this._position + 1);
231 // surrogate low - already handled above
232 if (0xDC00 <= code && code <= 0xDFFF)
235 switch (this._state) {
236 case ParserState.NORMAL:
237 if (ch in normalStateHandler) {
238 normalStateHandler[ch](this, this._inputHandler);
240 this._inputHandler.addChar(ch, code);
243 case ParserState.ESCAPED:
244 if (ch in escapedStateHandler) {
245 escapedStateHandler[ch](this, this._terminal);
246 // Skip switch as it was just handled
251 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
252 case '(': // <-- this seems to get all the attention
260 this._terminal.gcharset = 0;
263 this._terminal.gcharset = 1;
266 this._terminal.gcharset = 2;
269 this._terminal.gcharset = 3;
272 this._terminal.gcharset = 1;
275 this._terminal.gcharset = 2;
278 this._state = ParserState.CHARSET;
281 // Designate G3 Character Set (VT300).
282 // A = ISO Latin-1 Supplemental.
285 this._terminal.gcharset = 3;
286 this._state = ParserState.CHARSET;
291 // Single Shift Select of G2 Character Set
292 // ( SS2 is 0x8e). This affects next character only.
296 // Single Shift Select of G3 Character Set
297 // ( SS3 is 0x8f). This affects next character only.
301 // Invoke the G2 Character Set as GL (LS2).
303 this._terminal.setgLevel(2);
306 // Invoke the G3 Character Set as GL (LS3).
308 this._terminal.setgLevel(3);
311 // Invoke the G3 Character Set as GR (LS3R).
313 this._terminal.setgLevel(3);
316 // Invoke the G2 Character Set as GR (LS2R).
318 this._terminal.setgLevel(2);
321 // Invoke the G1 Character Set as GR (LS1R).
323 this._terminal.setgLevel(1);
326 // ESC 7 Save Cursor (DECSC).
328 this._inputHandler.saveCursor();
329 this._state = ParserState.NORMAL;
332 // ESC 8 Restore Cursor (DECRC).
334 this._inputHandler.restoreCursor();
335 this._state = ParserState.NORMAL;
338 // ESC # 3 DEC line height/width
340 this._state = ParserState.NORMAL;
344 // ESC H Tab Set (HTS is 0x88).
346 this._terminal.tabSet();
347 this._state = ParserState.NORMAL;
350 // ESC = Application Keypad (DECKPAM).
352 this._terminal.log('Serial port requested application keypad.');
353 this._terminal.applicationKeypad = true;
354 this._terminal.viewport.syncScrollArea();
355 this._state = ParserState.NORMAL;
358 // ESC > Normal Keypad (DECKPNM).
360 this._terminal.log('Switching back to normal keypad.');
361 this._terminal.applicationKeypad = false;
362 this._terminal.viewport.syncScrollArea();
363 this._state = ParserState.NORMAL;
367 this._state = ParserState.NORMAL;
368 this._terminal.error('Unknown ESC control: %s.', ch);
373 case ParserState.CHARSET:
374 if (ch in charsetMap) {
376 if (ch === '/') { // ISOLatin is actually /A
380 cs = CHARSETS.US; // Default
382 this._terminal.setgCharset(this._terminal.gcharset, cs);
383 this._terminal.gcharset = null;
384 this._state = ParserState.NORMAL;
387 case ParserState.OSC:
390 // Set Text Parameters.
391 if (ch === C0.ESC || ch === C0.BEL) {
392 if (ch === C0.ESC) this._position++;
394 this._terminal.params.push(this._terminal.currentParam);
396 switch (this._terminal.params[0]) {
400 if (this._terminal.params[1]) {
401 this._terminal.title = this._terminal.params[1];
402 this._terminal.handleTitle(this._terminal.title);
410 // change dynamic colors
422 // change dynamic ui colors
434 // manipulate selection data
451 this._terminal.params = [];
452 this._terminal.currentParam = 0;
453 this._state = ParserState.NORMAL;
455 if (!this._terminal.params.length) {
456 if (ch >= '0' && ch <= '9') {
457 this._terminal.currentParam =
458 this._terminal.currentParam * 10 + ch.charCodeAt(0) - 48;
459 } else if (ch === ';') {
460 this._terminal.params.push(this._terminal.currentParam);
461 this._terminal.currentParam = '';
464 this._terminal.currentParam += ch;
469 case ParserState.CSI_PARAM:
470 if (ch in csiParamStateHandler) {
471 csiParamStateHandler[ch](this);
474 this.finalizeParam();
475 // Fall through the CSI as this character should be the CSI code.
476 this._state = ParserState.CSI;
478 case ParserState.CSI:
479 if (ch in csiStateHandler) {
480 csiStateHandler[ch](this._inputHandler, this._terminal.params, this._terminal.prefix, this._terminal.postfix);
482 this._terminal.error('Unknown CSI code: %s.', ch);
485 this._state = ParserState.NORMAL;
486 this._terminal.prefix = '';
487 this._terminal.postfix = '';
490 case ParserState.DCS:
491 if (ch === C0.ESC || ch === C0.BEL) {
492 if (ch === C0.ESC) this._position++;
494 switch (this._terminal.prefix) {
495 // User-Defined Keys (DECUDK).
499 // Request Status String (DECRQSS).
500 // test: echo -e '\eP$q"p\e\\'
502 let pt = this._terminal.currentParam
519 + (this._terminal.scrollTop + 1)
521 + (this._terminal.scrollBottom + 1)
531 this._terminal.error('Unknown DCS Pt: %s.', pt);
536 this._terminal.send(C0.ESC + 'P' + +valid + '$r' + pt + C0.ESC + '\\');
539 // Set Termcap/Terminfo Data (xterm, experimental).
543 // Request Termcap/Terminfo String (xterm, experimental)
544 // Regular xterm does not even respond to this sequence.
545 // This can cause a small glitch in vim.
546 // test: echo -ne '\eP+q6b64\e\\'
548 // TODO: Don't declare pt twice
549 /*let*/ pt = this._terminal.currentParam
552 this._terminal.send(C0.ESC + 'P' + +valid + '+r' + pt + C0.ESC + '\\');
556 this._terminal.error('Unknown DCS prefix: %s.', this._terminal.prefix);
560 this._terminal.currentParam = 0;
561 this._terminal.prefix = '';
562 this._state = ParserState.NORMAL;
563 } else if (!this._terminal.currentParam) {
564 if (!this._terminal.prefix && ch !== '$' && ch !== '+') {
565 this._terminal.currentParam = ch;
566 } else if (this._terminal.prefix.length === 2) {
567 this._terminal.currentParam = ch;
569 this._terminal.prefix += ch;
572 this._terminal.currentParam += ch;
576 case ParserState.IGNORE:
578 if (ch === C0.ESC || ch === C0.BEL) {
579 if (ch === C0.ESC) this._position++;
580 this._state = ParserState.NORMAL;
588 * Set the parser's current parsing state.
590 * @param state The new state.
592 public setState(state: ParserState): void {
597 * Sets the parsier's current prefix. CSI codes can have prefixes of '?', '>'
600 * @param prefix The prefix.
602 public setPrefix(prefix: string): void {
603 this._terminal.prefix = prefix;
607 * Sets the parsier's current prefix. CSI codes can have postfixes of '$',
610 * @param postfix The postfix.
612 public setPostfix(postfix: string): void {
613 this._terminal.postfix = postfix;
617 * Sets the parser's current parameter.
619 * @param param the parameter.
621 public setParam(param: number) {
622 this._terminal.currentParam = param;
626 * Gets the parser's current parameter.
628 public getParam(): number {
629 return this._terminal.currentParam;
633 * Finalizes the parser's current parameter, adding it to the list of
634 * parameters and setting the new current parameter to 0.
636 public finalizeParam(): void {
637 this._terminal.params.push(this._terminal.currentParam);
638 this._terminal.currentParam = 0;
642 * Tell the parser to skip the next character.
644 public skipNextChar(): void {
649 * Tell the parser to repeat parsing the current character (for example if it
650 * needs parsing using a different state.
652 // public repeatChar(): void {