jc-marked
Version:
Markdown AST (Abstract syntax tree) parser based on finite-state machine (FSM).
1 lines • 10.7 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Loader=void 0;const __1=require(".."),exceps_1=require("./exceps"),regs_1=require("./regs");class Loader extends Array{constructor(){super(),this._currentState=__1.State.IDLE,this._nextState=null,this._nodes=[{type:"initial"}],this._newInput=!1,this._endFlag=!1}get nodes(){return this._nodes.slice(1,this._nodes.length)}runClock(){for(;this._newInput;)this._runActions(),this._countNewState(),this._enterState()}push(t){return super.push(t),this._newInput=!0,this.runClock(),this.length}_runActions(){if(this._newInput){switch(this._currentState){case __1.State.IDLE:(0,__1.noop)();break;case __1.State.META:this._nodes.push({type:"meta",index:this.length-2});break;case __1.State.TITLE:this._nodes.push({type:"title",index:this.length-2});break;case __1.State.BREAK_LINE:this._nodes.push({type:"breakLine",index:this.length-2});break;case __1.State.CODE:this._recordingPosition("code");break;case __1.State.CODE_GROUP:this._recordingPosition("code-group");break;case __1.State.CHART:this._recordingPosition("chart");break;case __1.State.QUOTE:this._recordingPosition("quote");break;case __1.State.INFO:this._recordingPosition("info");break;case __1.State.MATH:this._recordingPosition("math");break;case __1.State.TABLE:var t;"table"===this._nodes[this._nodes.length-1].type?this._nodes[this._nodes.length-1].to++:((t=this._nodes[this._nodes.length-1]).from===t.to?this._nodes.pop():t.to--,this._nodes.push({type:"table",from:this.length-3,to:this.length-2}));break;case __1.State.TEXT:this._recordingPosition("text");break;default:throw(0,exceps_1.StateNotExistError)(`There is no state named "${this._currentState}"`)}this._newInput=!1}}_countNewState(){var t=this.length;const e=this[t-2],_=this[t-1];function s(t){regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?t._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?t._nextState=__1.State.META:/^#/.test(_)?t._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?t._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?t._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?t._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?t._nextState=__1.State.CHART:regs_1.default.regMathJax.test(_)?t._nextState=__1.State.MATH:regs_1.default.regInfo.test(_)?t._nextState=__1.State.INFO:/\s*\-\-\-\s*/.test(_)?t._nextState=__1.State.BREAK_LINE:"initial"!==t._nodes[t._nodes.length-1].type&&(t._nextState=__1.State.TEXT)}switch(this._currentState){case __1.State.IDLE:regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?this._nextState=__1.State.CHART:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT);break;case __1.State.META:case __1.State.BREAK_LINE:case __1.State.TITLE:s(this);break;case __1.State.CODE:this._endFlag&&(regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?this._nextState=__1.State.CHART:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT)),regs_1.default.regCode.test(_)&&this._setEndFlag();break;case __1.State.TABLE:regs_1.default.regTableRow.test(_)?(0,__1.noop)():(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?this._nextState=__1.State.CHART:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT);break;case __1.State.QUOTE:""===_&&(regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?this._nextState=__1.State.CHART:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT));break;case __1.State.TOPICS:s(this);break;case __1.State.MATH:this._endFlag&&(regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?this._nextState=__1.State.CHART:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT)),regs_1.default.regMathJax.test(_)&&this._setEndFlag();break;case __1.State.CODE_GROUP:this._endFlag&&(regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT)),regs_1.default.regCodeGroupEnd.test(_)&&this._setEndFlag();break;case __1.State.CHART:this._endFlag&&(regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regInfo.test(_)?this._nextState=__1.State.INFO:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT)),regs_1.default.regChartEnd.test(_)&&this._setEndFlag();break;case __1.State.INFO:this._endFlag&&(regs_1.default.regTableRow2.test(_)&®s_1.default.regTableRow.test(e)?this._nextState=__1.State.TABLE:(0,__1.regTestAny)([regs_1.default.regTitle,regs_1.default.regSubTitle,regs_1.default.regAuthor,regs_1.default.regLastArticle,regs_1.default.regNextArticle,regs_1.default.regSummary],_)?this._nextState=__1.State.META:/^#/.test(_)?this._nextState=__1.State.TITLE:regs_1.default.topics.test(_)?this._nextState=__1.State.TOPICS:regs_1.default.regCode.test(_)?this._nextState=__1.State.CODE:regs_1.default.regCodeGroup.test(_)?this._nextState=__1.State.CODE_GROUP:regs_1.default.regChart.test(_)?this._nextState=__1.State.CHART:regs_1.default.regMathJax.test(_)?this._nextState=__1.State.MATH:/\s*\-\-\-\s*/.test(_)?this._nextState=__1.State.BREAK_LINE:"initial"!==this._nodes[this._nodes.length-1].type&&(this._nextState=__1.State.TEXT)),regs_1.default.regInfo.test(_)&&this._setEndFlag();break;case __1.State.TEXT:s(this)}}_enterState(){this._currentState!==this._nextState&&null!==this._nextState&&(this._currentState=this._nextState,[__1.State.CODE,__1.State.MATH,__1.State.INFO,__1.State.CODE_GROUP,__1.State.CHART].includes(this._currentState))&&this._initStateWithEndFlag()}_recordingPosition(t){this._nodes[this._nodes.length-1].type===t?this._nodes[this._nodes.length-1].to+=1:this._nodes.push({type:t,from:this.length-2,to:this.length-2})}_initStateWithEndFlag(){this._endFlag=!1}_setEndFlag(){this._endFlag=!0}}exports.Loader=Loader;