UNPKG

khufu

Version:

A template language for incremental-dom or DSL for javascript views

270 lines (234 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } // The code here is derived from https://github.com/aaditmshah/lexer // // The original code have the following copyright: // // The MIT License (MIT) // Copyright (c) 2013 Aadit M Shah // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. // var Lexer = function () { function Lexer() { _classCallCheck(this, Lexer); this._rules = []; } _createClass(Lexer, [{ key: "addRule", value: function addRule(pattern, action, start) { var global = pattern.global; if (!global) { var flags = "g"; if (pattern.multiline) flags += "m"; if (pattern.ignoreCase) flags += "i"; pattern = new RegExp(pattern.source, flags); } if (Object.prototype.toString.call(start) !== "[object Array]") start = [0]; this._rules.push({ pattern: pattern, global: global, action: action, start: start }); return this; } }, { key: "factory", value: function factory() { var proto = Object.create(new LexerInstance()); proto._rules = this._rules.slice(); return proto; } }]); return Lexer; }(); exports.default = Lexer; var LexerInstance = function () { function LexerInstance() { _classCallCheck(this, LexerInstance); } _createClass(LexerInstance, [{ key: "setInput", value: function setInput(input) { this._tokens = []; this._remove = 0; this.state = 0; this.index = 0; this.input = input; this.yylineno = 1; this.column = 1; this.brackets = []; this.templates = 0; this.original_lexeme = ''; this.indent = [0]; this.newline = true; } }, { key: "showPosition", value: function showPosition() { var lex = this.original_lexeme || ''; var prefix = /(?:\n|^)(.*)$/.exec(this.input.substr(0, this.index))[1]; var suffix = /.*$/m.exec(this.input.substr(this.index))[0]; var indent = prefix.substr(0, prefix.length - lex.length); var arrow = lex.replace(/[\s\S]/g, '^') || '^'; var ln = this.yylineno + ': '; return ln + prefix + suffix + "\n" + ln + indent.replace(/[\s\S]/g, ' ') + arrow + ' ---'; } }, { key: "defunct", value: function defunct(chr) { throw Error('Unexpected character "' + chr + '" (state: ' + this.state + ')\n' + this.showPosition()); } }, { key: "lex", value: function lex() { var old_index = this.index; var token = this._lex(); var loc = { first_line: this.yylineno, first_column: this.column }; this.original_lexeme = this.input.substr(old_index, this.index - old_index); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = this.original_lexeme[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var i = _step.value; if (i == '\n') { this.yylineno += 1; this.column = 1; } else if (i == '\t') { this.column += 8; } else { this.column += 1; } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } this.yylloc = _extends({ last_column: this.column, last_line: this.yylineno }, loc); return token; } }, { key: "_lex", value: function _lex() { if (this._tokens.length) return this._tokens.shift(); this.reject = true; while (this.index <= this.input.length) { var matches = this._scan().splice(this._remove); var index = this.index; while (matches.length) { if (this.reject) { var match = matches.shift(); var result = match.result; var length = match.length; this.index += length; this.reject = false; this._remove++; var token = match.action.apply(this, result); if (this.reject) this.index = result.index;else if (typeof token !== "undefined") { switch (Object.prototype.toString.call(token)) { case "[object Array]": this._tokens = token.slice(1); token = token[0]; default: if (length) this._remove = 0; return token; } } } else break; } var input = this.input; if (index < input.length) { if (this.reject) { this._remove = 0; var token = this.defunct(input.charAt(this.index++)); if (typeof token !== "undefined") { if (Object.prototype.toString.call(token) === "[object Array]") { this._tokens = token.slice(1); return token[0]; } else return token; } } else { if (this.index !== index) this._remove = 0; this.reject = true; } } else if (matches.length) this.reject = true;else break; } } }, { key: "_scan", value: function _scan() { var matches = []; var index = 0; var state = this.state; var lastIndex = this.index; var input = this.input; for (var i = 0, length = this._rules.length; i < length; i++) { var rule = this._rules[i]; var start = rule.start; var states = start.length; if (!states || start.indexOf(state) >= 0 || state % 2 && states === 1 && !start[0]) { var pattern = rule.pattern; pattern.lastIndex = lastIndex; var result = pattern.exec(input); if (result && result.index === lastIndex) { var j = matches.push({ result: result, action: rule.action, length: result[0].length }); if (rule.global) index = j; while (--j > index) { var k = j - 1; if (matches[j].length > matches[k].length) { var temple = matches[j]; matches[j] = matches[k]; matches[k] = temple; } } } } } return matches; } }]); return LexerInstance; }();