parser.js 2.0 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{validateLanguage as n,findScriptDependencies as e,checkScript as r,findFieldLiterals as t,validateScript as o,findExpectedFieldLiterals as i,referencesMember as c,referencesFunction as l}from"./treeAnalysis.js";import{parse as u}from"./lib/arcade-parser.js";function a(r,t=[]){const o=u("function _() { "+r+"\n}");if(null===o.body||void 0===o.body)throw new Error("No formula provided.");if(0===o.body.length)throw new Error("No formula provided.");if(0===o.body.length)throw new Error("No formula provided.");if("BlockStatement"!==o.body[0].body.type)throw new Error("Invalid formula content.");const i=n(o);if(""!==i)throw new Error(i);return e(o,t),o}function s(n,e,t,o,i){const c=[],l="function _() { \n".length-1,a="function _() { \n"+n+"\n}";try{const n=u(a,{tolerant:!0,loc:!0,range:!0}),s=n.errors;if(s.length>0)for(let e=0;e<s.length;e++)c.push({line:s[e].lineNumber-2,character:s[e].column,reason:s[e].description});const f=r(n,e,t,o,i);for(let e=0;e<f.length;e++)f[e].line=f[e].line-2,f[e].range&&(f[e].range=[f[e][0]-l,f[e][1]-l]),f[e].loc&&(f[e].loc.start.line=f[e].loc.start.line-2,f[e].loc.end.line=f[e].loc.end.line-2),c.push(f[e])}catch(s){try{if("Unexpected token }"===s.description){const n=a.split("\n").length;s.lineNumber===n?(s.index=a.length-1,c.push({line:s.lineNumber-4,character:s.column,reason:"Unexpected end of script"})):(s.index=a.length-1,c.push({line:s.lineNumber-2,character:s.column,reason:"Unexpected end of script"}))}else c.push({line:s.lineNumber-2,character:s.column,reason:s.description})}catch(f){}}return c}function f(n,e){return t(n)}function d(n,e,r){return o(n,e,r)}function h(n){return i(n)}function p(n,e){return c(n,e)}function m(n,e){return l(n,e)}export{h as extractExpectedFieldLiterals,f as extractFieldLiterals,a as parseScript,m as referencesFunction,p as referencesMember,s as scriptCheck,d as validateScript};