text: "use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports.Tokenizer=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_token=require("./token"),_yielder=require("./yielder"),INVISIBLE_CHAR_CODE_TOKEN_LIST=[10,13,32],STATE_INIT="init",STATE_KW_NULL="null",STATE_KW_TRUE="boolean_true",STATE_KW_FALSE="boolean_false",STATE_NUMBER="number",STATE_STRING="string",STATE_STRING_ESCAPE="escape",INITIAL_STATE={"[":_token.TokenType.LeftBracket,"]":_token.TokenType.RightBracket,"{":_token.TokenType.LeftBrace,"}":_token.TokenType.RightBrace,":":_token.TokenType.Colon,",":_token.TokenType.Comma},MOVE_TO={"[":STATE_INIT,"]":STATE_INIT,"{":STATE_INIT,"}":STATE_INIT,":":STATE_INIT,",":STATE_INIT,n:STATE_KW_NULL,t:STATE_KW_TRUE,f:STATE_KW_FALSE,0:STATE_NUMBER,1:STATE_NUMBER,2:STATE_NUMBER,3:STATE_NUMBER,4:STATE_NUMBER,5:STATE_NUMBER,6:STATE_NUMBER,7:STATE_NUMBER,8:STATE_NUMBER,9:STATE_NUMBER,"-":STATE_NUMBER,'"':STATE_STRING},Tokenizer=function(){function a(){(0,_classCallCheck2.default)(this,a),this.state=STATE_INIT,this.pos=0,this.sourceCode="",this.tokens=[],this.curToken=""}return(0,_createClass2.default)(a,[{key:"tokenizeArray",value:_regenerator.default.mark(function tokenizeArray(a){var b,c;return _regenerator.default.wrap(function(d){for(;;)switch(d.prev=d.next){case 0:this.sourceCode=a,this.type="uint8",b=a.length;case 3:if(!(this.pos<b)){d.next=28;break}if(!(0==(15&this.pos)&&(0,_yielder.yielder)())){d.next=7;break}return void(d.next=7);case 7:c=this.readCharacter(),d.t0=this.state,d.next=d.t0===STATE_INIT?11:d.t0===STATE_KW_NULL?13:d.t0===STATE_KW_TRUE?15:d.t0===STATE_KW_FALSE?17:d.t0===STATE_NUMBER?19:d.t0===STATE_STRING?21:d.t0===STATE_STRING_ESCAPE?23:25;break;case 11:return this.initToken(c),d.abrupt("break",26);case 13:return this.handleTokenNull(c),d.abrupt("break",26);case 15:return this.handleTokenTrue(c),d.abrupt("break",26);case 17:return this.handleTokenFalse(c),d.abrupt("break",26);case 19:return this.handleTokenNumber(c),d.abrupt("break",26);case 21:return this.handleTokenString(c),d.abrupt("break",26);case 23:return this.handleTokenStringEscape(c),d.abrupt("break",26);case 25:throw new Error("finite state machine get an unexpected state: ".concat(this.state));case 26:d.next=3;break;case 28:return d.abrupt("return",this.tokens);case 29:case"end":return d.stop();}},tokenizeArray,this)})},{key:"tokenize",value:_regenerator.default.mark(function tokenize(a){var b,c;return _regenerator.default.wrap(function(d){for(;;)switch(d.prev=d.next){case 0:if(!(a instanceof Uint8Array)){d.next=3;break}return d.delegateYield(this.tokenizeArray(a),"t0",2);case 2:return d.abrupt("return",d.t0);case 3:this.type="char",this.sourceCode=a,b=a.length;case 6:if(!(this.pos<b)){d.next=31;break}if(!(0==(7&this.pos)&&(0,_yielder.yielder)())){d.next=10;break}return void(d.next=10);case 10:c=this.read(),d.t1=this.state,d.next=d.t1===STATE_INIT?14:d.t1===STATE_KW_NULL?16:d.t1===STATE_KW_TRUE?18:d.t1===STATE_KW_FALSE?20:d.t1===STATE_NUMBER?22:d.t1===STATE_STRING?24:d.t1===STATE_STRING_ESCAPE?26:28;break;case 14:return this.initToken(c),d.abrupt("break",29);case 16:return this.handleTokenNull(c),d.abrupt("break",29);case 18:return this.handleTokenTrue(c),d.abrupt("break",29);case 20:return this.handleTokenFalse(c),d.abrupt("break",29);case 22:return this.handleTokenNumber(c),d.abrupt("break",29);case 24:return this.handleTokenString(c),d.abrupt("break",29);case 26:return this.handleTokenStringEscape(c),d.abrupt("break",29);case 28:throw new Error("finite state machine get an unexpected state: ".concat(this.state));case 29:d.next=6;break;case 31:return d.abrupt("return",this.tokens);case 32:case"end":return d.stop();}},tokenize,this)})},{key:"read",value:function read(){var a;do a=this.sourceCode[this.pos++];while(a&&32>a.charCodeAt(0));return a}},{key:"readCharacter",value:function readCharacter(){var a=this.sourceCode[this.pos++];switch(a>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:return String.fromCharCode(a);case 12:case 13:return String.fromCharCode((31&a)<<6|63&this.sourceCode[this.pos++]);case 14:return String.fromCharCode((15&a)<<12|(63&this.sourceCode[this.pos++])<<6|63&this.sourceCode[this.pos++]);default:throw new Error("Bad encoding");}}},{key:"peek",value:function peek(){var o=this.pos;if("char"===this.type){for(;this.sourceCode[o]&&32>this.sourceCode[o].charCodeAt(0);)o++;return this.sourceCode[o]}for(var a=function(){var a=b[o++];switch(a>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:return String.fromCharCode(a);case 12:case 13:return String.fromCharCode((31&a)<<6|63&b[o++]);case 14:return String.fromCharCode((15&a)<<12|(63&b[o++])<<6|63&b[o++]);default:throw new Error("Bad encoding");}},b=this.sourceCode,c=a();32>c.charCodeAt(0)&&o<b.length;)c=a();return 0===c.charCodeAt(0)?void 0:c}},{key:"initToken",value:function initToken(a){for(;a&&32>=a.charCodeAt(0);)a=this.sourceCode[this.pos++];var b=INITIAL_STATE[a];if(!b&&!INVISIBLE_CHAR_CODE_TOKEN_LIST.includes(a.charCodeAt(0)))throw new Error("state INIT, unexpected token ".concat(a));for(this.tokens.push({text:a,type:b});" "===this.peek();)this.pos++;var c=this.peek();if(void 0!==c&&(this.state=MOVE_TO[c],!this.state))throw new Error("Unexpected character [".concat(b,":").concat(c.charCodeAt(0),"] '").concat(a,"' @ ").concat(this.pos," - ").concat(this.sourceCode.slice(Math.max(0,this.pos-16),this.pos+16)))}},{key:"handleTokenNull",value:function handleTokenNull(a){switch(a){case"n":case"u":this.curToken+=a;break;case"l":if(this.curToken+=a,"null"===this.curToken){var b={text:this.curToken,type:_token.TokenType.Null};this.tokens.push(b),this.curToken="",this.state=STATE_INIT}else if("nul"===this.curToken);else throw new Error("state NULL, unexpected token ".concat(a));break;default:throw new Error("state NULL, unexpected token ".concat(a));}}},{key:"handleTokenTrue",value:function handleTokenTrue(a){switch(a){case"t":case"r":case"u":this.curToken+=a;break;case"e":if("tru"!==this.curToken)throw new Error("state TRUE, unexpected token ".concat(a));this.curToken+=a;var b={text:this.curToken,type:_token.TokenType.Boolean};this.tokens.push(b),this.curToken="",this.state=STATE_INIT;break;default:throw new Error("state TRUE, unexpected token ".concat(a));}}},{key:"handleTokenFalse",value:function handleTokenFalse(a){switch(a){case"f":case"a":case"l":case"s":this.curToken+=a;break;case"e":this.curToken+=a;var b={text:this.curToken,type:_token.TokenType.Boolean};this.tokens.push(b),this.curToken="",this.state=STATE_INIT;break;default:throw new Error("state FALSE, unexpected token ".concat(a));}}},{key:"handleTokenNumber",value:function handleTokenNumber(a){switch(a){case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":case"8":case"9":if(this.curToken+=a,!/[0-9]|\.|-/.test(this.peek())){var b={text:this.curToken,type:_token.TokenType.Number};this.tokens.push(b),this.curToken="",this.state=STATE_INIT}break;case"-":case".":this.curToken+=a;break;default:throw new Error("state NUMBER, unexpected token ".concat(a));}}},{key:"handleTokenStringEscape",value:function handleTokenStringEscape(a){return"u"===a?(this.curToken+=JSON.parse("\"\\u".concat(this.read()).concat(this.read()).concat(this.read()).concat(this.read(),"\"")),void(this.state=STATE_STRING)):void(this.curToken+=JSON.parse("\"\\".concat(a,"\"")),this.state=STATE_STRING)}},{key:"handleTokenString",value:function handleTokenString(a){switch(a){case"\\":this.state=STATE_STRING_ESCAPE;break;case"\"":if(""===this.curToken)this.curToken=a;else{this.curToken+=a;var b={text:this.curToken,type:_token.TokenType.String};this.tokens.push(b),this.curToken="",this.state=STATE_INIT}break;default:this.curToken+=a;}}}]),a}();exports.Tokenizer=Tokenizer;