Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 1 | // Copyright (c) 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | 'use strict'; |
| 6 | |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 7 | /** |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 8 | * Parses the key definition syntax used for user keyboard customizations. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 9 | * @constructor |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 10 | */ |
| 11 | hterm.Parser = function() { |
| 12 | /** |
| 13 | * @type {string} The source string. |
| 14 | */ |
| 15 | this.source = ''; |
| 16 | |
| 17 | /** |
| 18 | * @type {number} The current position. |
| 19 | */ |
| 20 | this.pos = 0; |
| 21 | |
| 22 | /** |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 23 | * @type {?string} The character at the current position. |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 24 | */ |
| 25 | this.ch = null; |
| 26 | }; |
| 27 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 28 | /** |
| 29 | * @param {string} message |
| 30 | * @return {!Error} |
| 31 | */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 32 | hterm.Parser.prototype.error = function(message) { |
| 33 | return new Error('Parse error at ' + this.pos + ': ' + message); |
| 34 | }; |
| 35 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 36 | /** @return {boolean} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 37 | hterm.Parser.prototype.isComplete = function() { |
| 38 | return this.pos == this.source.length; |
| 39 | }; |
| 40 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 41 | /** |
| 42 | * @param {string} source |
| 43 | * @param {number=} opt_pos |
| 44 | */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 45 | hterm.Parser.prototype.reset = function(source, opt_pos) { |
| 46 | this.source = source; |
| 47 | this.pos = opt_pos || 0; |
| 48 | this.ch = source.substr(0, 1); |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Parse a key sequence. |
| 53 | * |
Robert Ginda | ca5b287 | 2015-06-15 14:24:52 -0700 | [diff] [blame] | 54 | * A key sequence is zero or more of the key modifiers defined in |
| 55 | * hterm.Parser.identifiers.modifierKeys followed by a key code. Key |
| 56 | * codes can be an integer or an identifier from |
| 57 | * hterm.Parser.identifiers.keyCodes. Modifiers and keyCodes should be joined |
| 58 | * by the dash character. |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 59 | * |
Robert Ginda | ca5b287 | 2015-06-15 14:24:52 -0700 | [diff] [blame] | 60 | * An asterisk "*" can be used to indicate that the unspecified modifiers |
| 61 | * are optional. |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 62 | * |
Robert Ginda | ca5b287 | 2015-06-15 14:24:52 -0700 | [diff] [blame] | 63 | * For example: |
| 64 | * A: Matches only an unmodified "A" character. |
| 65 | * 65: Same as above. |
| 66 | * 0x41: Same as above. |
| 67 | * Ctrl-A: Matches only Ctrl-A. |
| 68 | * Ctrl-65: Same as above. |
| 69 | * Ctrl-0x41: Same as above. |
| 70 | * Ctrl-Shift-A: Matches only Ctrl-Shift-A. |
| 71 | * Ctrl-*-A: Matches Ctrl-A, as well as any other key sequence that includes |
| 72 | * at least the Ctrl and A keys. |
| 73 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 74 | * @return {!hterm.Keyboard.KeyDown} An object with shift, ctrl, alt, meta, |
| 75 | * keyCode properties. |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 76 | */ |
| 77 | hterm.Parser.prototype.parseKeySequence = function() { |
| 78 | var rv = { |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 79 | keyCode: null |
| 80 | }; |
| 81 | |
Robert Ginda | ca5b287 | 2015-06-15 14:24:52 -0700 | [diff] [blame] | 82 | for (var k in hterm.Parser.identifiers.modifierKeys) { |
| 83 | rv[hterm.Parser.identifiers.modifierKeys[k]] = false; |
| 84 | } |
| 85 | |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 86 | while (this.pos < this.source.length) { |
| 87 | this.skipSpace(); |
| 88 | |
| 89 | var token = this.parseToken(); |
| 90 | if (token.type == 'integer') { |
| 91 | rv.keyCode = token.value; |
| 92 | |
| 93 | } else if (token.type == 'identifier') { |
Mike Frysinger | e672167 | 2017-05-26 01:26:55 -0400 | [diff] [blame] | 94 | var ucValue = token.value.toUpperCase(); |
| 95 | if (ucValue in hterm.Parser.identifiers.modifierKeys && |
| 96 | hterm.Parser.identifiers.modifierKeys.hasOwnProperty(ucValue)) { |
| 97 | var mod = hterm.Parser.identifiers.modifierKeys[ucValue]; |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 98 | if (rv[mod] && rv[mod] != '*') |
| 99 | throw this.error('Duplicate modifier: ' + token.value); |
| 100 | rv[mod] = true; |
| 101 | |
Mike Frysinger | e672167 | 2017-05-26 01:26:55 -0400 | [diff] [blame] | 102 | } else if (ucValue in hterm.Parser.identifiers.keyCodes && |
| 103 | hterm.Parser.identifiers.keyCodes.hasOwnProperty(ucValue)) { |
| 104 | rv.keyCode = hterm.Parser.identifiers.keyCodes[ucValue]; |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 105 | |
| 106 | } else { |
| 107 | throw this.error('Unknown key: ' + token.value); |
| 108 | } |
| 109 | |
| 110 | } else if (token.type == 'symbol') { |
| 111 | if (token.value == '*') { |
Robert Ginda | ca5b287 | 2015-06-15 14:24:52 -0700 | [diff] [blame] | 112 | for (var id in hterm.Parser.identifiers.modifierKeys) { |
| 113 | var p = hterm.Parser.identifiers.modifierKeys[id]; |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 114 | if (!rv[p]) |
| 115 | rv[p] = '*'; |
Robert Ginda | ca5b287 | 2015-06-15 14:24:52 -0700 | [diff] [blame] | 116 | } |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 117 | } else { |
| 118 | throw this.error('Unexpected symbol: ' + token.value); |
| 119 | } |
| 120 | } else { |
| 121 | throw this.error('Expected integer or identifier'); |
| 122 | } |
| 123 | |
| 124 | this.skipSpace(); |
| 125 | |
| 126 | if (this.ch != '-') |
| 127 | break; |
| 128 | |
| 129 | if (rv.keyCode != null) |
| 130 | throw this.error('Extra definition after target key'); |
| 131 | |
| 132 | this.advance(1); |
| 133 | } |
| 134 | |
| 135 | if (rv.keyCode == null) |
| 136 | throw this.error('Missing target key'); |
| 137 | |
| 138 | return rv; |
| 139 | }; |
| 140 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 141 | /** @return {string} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 142 | hterm.Parser.prototype.parseKeyAction = function() { |
| 143 | this.skipSpace(); |
| 144 | |
| 145 | var token = this.parseToken(); |
| 146 | |
| 147 | if (token.type == 'string') |
| 148 | return token.value; |
| 149 | |
| 150 | if (token.type == 'identifier') { |
Mike Frysinger | 9837fff | 2017-05-26 00:40:41 -0400 | [diff] [blame] | 151 | if (token.value in hterm.Parser.identifiers.actions && |
| 152 | hterm.Parser.identifiers.actions.hasOwnProperty(token.value)) |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 153 | return hterm.Parser.identifiers.actions[token.value]; |
| 154 | |
| 155 | throw this.error('Unknown key action: ' + token.value); |
| 156 | } |
| 157 | |
| 158 | throw this.error('Expected string or identifier'); |
| 159 | |
| 160 | }; |
| 161 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 162 | /** @return {boolean} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 163 | hterm.Parser.prototype.peekString = function() { |
| 164 | return this.ch == '\'' || this.ch == '"'; |
| 165 | }; |
| 166 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 167 | /** @return {boolean} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 168 | hterm.Parser.prototype.peekIdentifier = function() { |
Joel Hockey | c98a0ce | 2019-09-20 16:26:20 -0700 | [diff] [blame^] | 169 | return !!this.ch.match(/[a-z_]/i); |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 172 | /** @return {boolean} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 173 | hterm.Parser.prototype.peekInteger = function() { |
Joel Hockey | c98a0ce | 2019-09-20 16:26:20 -0700 | [diff] [blame^] | 174 | return !!this.ch.match(/[0-9]/); |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 177 | /** @return {!Object} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 178 | hterm.Parser.prototype.parseToken = function() { |
| 179 | if (this.ch == '*') { |
| 180 | var rv = {type: 'symbol', value: this.ch}; |
| 181 | this.advance(1); |
| 182 | return rv; |
| 183 | } |
| 184 | |
| 185 | if (this.peekIdentifier()) |
| 186 | return {type: 'identifier', value: this.parseIdentifier()}; |
| 187 | |
| 188 | if (this.peekString()) |
| 189 | return {type: 'string', value: this.parseString()}; |
| 190 | |
| 191 | if (this.peekInteger()) |
| 192 | return {type: 'integer', value: this.parseInteger()}; |
| 193 | |
| 194 | |
| 195 | throw this.error('Unexpected token'); |
| 196 | }; |
| 197 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 198 | /** @return {string} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 199 | hterm.Parser.prototype.parseIdentifier = function() { |
| 200 | if (!this.peekIdentifier()) |
| 201 | throw this.error('Expected identifier'); |
| 202 | |
| 203 | return this.parsePattern(/[a-z0-9_]+/ig); |
| 204 | }; |
| 205 | |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 206 | /** @return {number} */ |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 207 | hterm.Parser.prototype.parseInteger = function() { |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 208 | if (this.ch == '0' && this.pos < this.source.length - 1 && |
| 209 | this.source.substr(this.pos + 1, 1) == 'x') { |
Joel Hockey | c98a0ce | 2019-09-20 16:26:20 -0700 | [diff] [blame^] | 210 | return parseInt(this.parsePattern(/0x[0-9a-f]+/gi), undefined); |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Joel Hockey | c98a0ce | 2019-09-20 16:26:20 -0700 | [diff] [blame^] | 213 | return parseInt(this.parsePattern(/\d+/g), 10); |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | /** |
| 217 | * Parse a single or double quoted string. |
| 218 | * |
| 219 | * The current position should point at the initial quote character. Single |
| 220 | * quoted strings will be treated literally, double quoted will process escapes. |
| 221 | * |
| 222 | * TODO(rginda): Variable interpolation. |
| 223 | * |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 224 | * @return {string} |
| 225 | */ |
| 226 | hterm.Parser.prototype.parseString = function() { |
| 227 | var result = ''; |
| 228 | |
| 229 | var quote = this.ch; |
| 230 | if (quote != '"' && quote != '\'') |
| 231 | throw this.error('String expected'); |
| 232 | |
| 233 | this.advance(1); |
| 234 | |
| 235 | var re = new RegExp('[\\\\' + quote + ']', 'g'); |
| 236 | |
| 237 | while (this.pos < this.source.length) { |
| 238 | re.lastIndex = this.pos; |
| 239 | if (!re.exec(this.source)) |
| 240 | throw this.error('Unterminated string literal'); |
| 241 | |
| 242 | result += this.source.substring(this.pos, re.lastIndex - 1); |
| 243 | |
| 244 | this.advance(re.lastIndex - this.pos - 1); |
| 245 | |
| 246 | if (quote == '"' && this.ch == '\\') { |
| 247 | this.advance(1); |
| 248 | result += this.parseEscape(); |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | if (quote == '\'' && this.ch == '\\') { |
| 253 | result += this.ch; |
| 254 | this.advance(1); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | if (this.ch == quote) { |
| 259 | this.advance(1); |
| 260 | return result; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | throw this.error('Unterminated string literal'); |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * Parse an escape code from the current position (which should point to |
| 270 | * the first character AFTER the leading backslash.) |
| 271 | * |
| 272 | * @return {string} |
| 273 | */ |
| 274 | hterm.Parser.prototype.parseEscape = function() { |
| 275 | var map = { |
| 276 | '"': '"', |
| 277 | '\'': '\'', |
| 278 | '\\': '\\', |
| 279 | 'a': '\x07', |
| 280 | 'b': '\x08', |
| 281 | 'e': '\x1b', |
| 282 | 'f': '\x0c', |
| 283 | 'n': '\x0a', |
| 284 | 'r': '\x0d', |
| 285 | 't': '\x09', |
| 286 | 'v': '\x0b', |
| 287 | 'x': function() { |
| 288 | var value = this.parsePattern(/[a-z0-9]{2}/ig); |
| 289 | return String.fromCharCode(parseInt(value, 16)); |
| 290 | }, |
| 291 | 'u': function() { |
| 292 | var value = this.parsePattern(/[a-z0-9]{4}/ig); |
| 293 | return String.fromCharCode(parseInt(value, 16)); |
| 294 | } |
| 295 | }; |
| 296 | |
Mike Frysinger | 9837fff | 2017-05-26 00:40:41 -0400 | [diff] [blame] | 297 | if (!(this.ch in map && map.hasOwnProperty(this.ch))) |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 298 | throw this.error('Unknown escape: ' + this.ch); |
| 299 | |
| 300 | var value = map[this.ch]; |
| 301 | this.advance(1); |
| 302 | |
| 303 | if (typeof value == 'function') |
| 304 | value = value.call(this); |
| 305 | |
| 306 | return value; |
| 307 | }; |
| 308 | |
| 309 | /** |
| 310 | * Parse the given pattern starting from the current position. |
| 311 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 312 | * @param {!RegExp} pattern A pattern representing the characters to span. MUST |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 313 | * include the "global" RegExp flag. |
| 314 | * @return {string} |
| 315 | */ |
| 316 | hterm.Parser.prototype.parsePattern = function(pattern) { |
| 317 | if (!pattern.global) |
| 318 | throw this.error('Internal error: Span patterns must be global'); |
| 319 | |
| 320 | pattern.lastIndex = this.pos; |
| 321 | var ary = pattern.exec(this.source); |
| 322 | |
| 323 | if (!ary || pattern.lastIndex - ary[0].length != this.pos) |
| 324 | throw this.error('Expected match for: ' + pattern); |
| 325 | |
| 326 | this.pos = pattern.lastIndex - 1; |
| 327 | this.advance(1); |
| 328 | |
| 329 | return ary[0]; |
| 330 | }; |
| 331 | |
| 332 | |
| 333 | /** |
| 334 | * Advance the current position. |
| 335 | * |
| 336 | * @param {number} count |
| 337 | */ |
| 338 | hterm.Parser.prototype.advance = function(count) { |
| 339 | this.pos += count; |
| 340 | this.ch = this.source.substr(this.pos, 1); |
| 341 | }; |
| 342 | |
| 343 | /** |
| 344 | * @param {string=} opt_expect A list of valid non-whitespace characters to |
| 345 | * terminate on. |
| 346 | * @return {void} |
| 347 | */ |
| 348 | hterm.Parser.prototype.skipSpace = function(opt_expect) { |
| 349 | if (!/\s/.test(this.ch)) |
| 350 | return; |
| 351 | |
| 352 | var re = /\s+/gm; |
| 353 | re.lastIndex = this.pos; |
| 354 | |
| 355 | var source = this.source; |
| 356 | if (re.exec(source)) |
| 357 | this.pos = re.lastIndex; |
| 358 | |
| 359 | this.ch = this.source.substr(this.pos, 1); |
| 360 | |
| 361 | if (opt_expect) { |
| 362 | if (this.ch.indexOf(opt_expect) == -1) { |
| 363 | throw this.error('Expected one of ' + opt_expect + ', found: ' + |
| 364 | this.ch); |
| 365 | } |
| 366 | } |
| 367 | }; |