UNPKG

tinylex

Version:

A simple iterative lexer written in TypeScript

1 lines 4.18 kB
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}("undefined"!=typeof self?self:this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var u,s=e[Symbol.iterator]();!(n=(u=s.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),u=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),s={onError:"tokenize"};t.TinyLex=function(){function e(t,r){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:s;o(this,e),this._code=t||"",this._rules=r||[],this._cursor=0,this._tokens=[],this._onToken=function(e){return e},this._errorAction=n.onError||"tokenize"}return u(e,[{key:"onToken",value:function(e){return this._onToken=e,this}},{key:"done",value:function(){return this._done}},{key:"lex",value:function(){if(this._done)throw new Error("lexer is consumed");for(;!this._done;){var e=this._scan();if(e){var t=this._onToken(e,this._lastMatch);if(t)return t}if(this._cursor>=this._code.length&&0===this._tokens.length){this._done=!0;var r=["EOF","EOF"];return this._onToken(r,null)||null}}}},{key:"tokenize",value:function(){return[].concat(n(this))}},{key:"_scan",value:function(){if(this._tokens.length)return this._tokens.shift();for(;this._cursor<this._code.length;){var e=this._code.slice(this._cursor),t=this._testRuleSet(e),r=i(t,2),n=r[0],o=r[1];if(o){if(this._lastMatch=o,!this._handleMatch(n,o,e))return null}else this._handleError(e)}}},{key:"next",value:function(){var e=this;return{next:function(){return{done:e.done(),value:!e.done()&&e.lex()}}}}},{key:Symbol.iterator,value:function(){return this.next()}},{key:"_testRuleSet",value:function(e){for(var t=this._rules.length,r=0;r<t;r++){var n=this._rules[r],o=n[0].exec(e);if(o)return[n,o]}return[null,null]}},{key:"_handleMatch",value:function(e,t,r){var n=[],o=e[1];if("string"==typeof o)n.push([o,null!=t[1]?t[1]:t[0]]),this._cursor+=t[0].length;else if("number"==typeof o){var i=t[o];n.push([i.toLocaleUpperCase(),i]),this._cursor+=t[0].length}else if("function"==typeof o){var u=o.call(this,t,n,r),s=t[0].length;this._cursor+="number"==typeof u?Math.floor(Math.abs(u))||s:s}else if(null==o)return this._cursor+=t[0].length,!1;return this._tokens.push.apply(this._tokens,n),!!n.length}},{key:"_handleError",value:function(e){switch(this._errorAction){case"throw":throw new Error(this._getErrorStr(e));case"ignore":this._cursor+=1;break;default:this._tokenizeChar(e)}}},{key:"_tokenizeChar",value:function(e){var t=e.slice(0,1);this._tokens.push([t.toLocaleUpperCase(),t]),this._cursor+=1}},{key:"_getErrorStr",value:function(e){return"lex error:"+this._lineAndCol()+'\n match not found for chunk: "'+e.replace(/\s+/g," ").slice(0,32)+'..."'}},{key:"_lineAndCol",value:function(){var e=this._code.slice(0,this._cursor).split("\n"),t=e[e.length-1].length+1;return e.length+":"+t}}]),e}()}])});