12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.24/esri/copyright.txt for details.
- */
- import{ErrorHandler as e}from"./error-handler.js";import{AssignmentOperators as t,BinaryOperators as s,LogicalOperators as n,UnaryOperators as r}from"./nodes.js";import{Scanner as i}from"./scanner.js";import{TokenName as a,TokenType as c}from"./token.js";class o{constructor(){this.values=[],this.curly=this.paren=-1}beforeFunctionExpression(e){return["(","{","[","return",",",...t,...s,...n,...r].includes(e)}isRegexStart(){const e=this.values[this.values.length-1];let t=null!==e;switch(e){case"this":case"]":t=!1;break;case")":{const e=this.values[this.paren-1];t="if"===e||"while"===e||"for"===e||"with"===e}break;case"}":if(t=!0,"function"===this.values[this.curly-3]){const e=this.values[this.curly-4];t=!!e&&!this.beforeFunctionExpression(e)}else if("function"===this.values[this.curly-4]){const e=this.values[this.curly-5];t=!e||!this.beforeFunctionExpression(e)}}return t}push(e){e.type===c.Punctuator||e.type===c.Keyword?("{"===e.value?this.curly=this.values.length:"("===e.value&&(this.paren=this.values.length),this.values.push(e.value)):this.values.push(null)}}class l{constructor(t,s){this.errorHandler=new e,this.errorHandler.tolerant=!!s&&("boolean"==typeof s.tolerant&&s.tolerant),this.scanner=new i(t,this.errorHandler),this.scanner.trackComment=!!s&&("boolean"==typeof s.comment&&s.comment),this.trackRange=!!s&&("boolean"==typeof s.range&&s.range),this.trackLoc=!!s&&("boolean"==typeof s.loc&&s.loc),this.buffer=[],this.reader=new o}errors(){return this.errorHandler.errors}getNextToken(){if(0===this.buffer.length){const t=this.scanner.scanComments();if(t)for(let e=0;e<t.length;++e){const s=t[e],n=this.scanner.source.slice(s.slice[0],s.slice[1]),r={type:s.multiLine?"BlockComment":"LineComment",value:n};this.trackRange&&(r.range=s.range),this.trackLoc&&(r.loc=s.loc),this.buffer.push(r)}if(!this.scanner.eof()){let t=null;this.trackLoc&&(t={start:{line:this.scanner.lineNumber,column:this.scanner.index-this.scanner.lineStart},end:{line:0,column:0}});let s;if("/"===this.scanner.source[this.scanner.index]&&this.reader.isRegexStart()){const t=this.scanner.saveState();try{s=this.scanner.scanRegExp()}catch(e){this.scanner.restoreState(t),s=this.scanner.lex()}}else s=this.scanner.lex();this.reader.push(s);const n={type:a[s.type],value:this.scanner.source.slice(s.start,s.end)};if(this.trackRange&&(n.range=[s.start,s.end]),this.trackLoc&&t&&(t.end={line:this.scanner.lineNumber,column:this.scanner.index-this.scanner.lineStart},n.loc=t),s.type===c.RegularExpression){const e=s.pattern,t=s.flags;n.regex={pattern:e,flags:t}}this.buffer.push(n)}}return this.buffer.shift()}}export{l as Tokenizer};
|