@borgar/fx 4.0.0-rc.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,7 +18,7 @@ Documentation can be found under [docs/](./docs/):
18
18
 
19
19
  * The API is documented in [docs/API.md](./docs/API.md).
20
20
 
21
- * The AST format the parser emits is specified in [docs/AST format.md](./docs/AST format.md).
21
+ * The AST format the parser emits is specified in [docs/AST_format.md](./docs/AST_format.md).
22
22
 
23
23
  * A primer/terminology definitions of Excel references and ranges can be found in [docs/References.md](./docs/References.md).
24
24
 
package/dist/fx.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";const e="operator",t="context",n="range_beam",r="range_ternary",l="range_named",o="unknown",u="UnaryExpression",c="BinaryExpression",i="ReferenceIdentifier",s="CallExpression";function a(e){const t=/(?:\[(.+?)\])?([^[\]]+?)$/.exec(e);if(t){const[,e,n]=t;return{context:[e,n].filter(Boolean)}}}const f=e=>e&&":"===e.value&&{},p=e=>e&&"range"===e.type&&{r0:e.value},h=e=>e&&e.type===r&&{r0:e.value},g=e=>e&&"range"===e.type&&{r1:e.value},d=t=>t&&t.type===e&&"!"===t.value&&{},$=e=>e&&e.type===n&&{r0:e.value},y=e=>e&&"structured"===e.type&&{struct:e.value},v=e=>e&&e.type===t?a(e.value):e&&"context_quote"===e.type?a(e.value.slice(1,-1).replace(/''/g,"'")):void 0,x=e=>e&&e.type===l&&{name:e.value},m=[[h],[p,f,g],[p],[$],[v,d,h],[v,d,p,f,g],[v,d,p],[v,d,$]],E=m.concat([[x],[v,d,x],[y],[x,y],[v,d,x,y]]);function R(e,t){const n={withLocation:!1,mergeRefs:!1,allowTernary:!1,allowNamed:!0,r1c1:!1,...t},r=we(e,ge,n),l={context:[],r0:"",r1:"",name:""};r.length&&"fx_prefix"===r[0].type&&r.shift();const o=n.allowNamed?E:m;for(let e=0;e<o.length;e++){const t={...l};if(o[e].length===r.length){const n=o[e].every(((e,n)=>{const l=e(r[n]);return Object.assign(t,l),l}));if(n)return t}}return null}const w=/[^0-9A-Za-z._¡¤§¨ª\u00ad¯-\uffff]/;function C(e){let t="",n=0,r=0;const l=e.context||[];for(let e=l.length;e>-1;e--){const o=l[e];if(o){t=(r%2?"["+o+"]":o)+t,n+=w.test(o),r++}}return n&&(t="'"+t.replace(/'/g,"''")+"'"),t?t+"!":t}const N=(e,t,n)=>Math.min(Math.max(t,e),n),A=(e,t)=>(t?"$":"")+I(e),b=(e,t)=>(t?"$":"")+String(e+1);function T(e){const t=e||"",n=t.length;let r=0;if(n>2){const e=t.charCodeAt(n-3);r+=676*(1+e-(e>95?32:0)-65)}if(n>1){const e=t.charCodeAt(n-2);r+=26*(1+e-(e>95?32:0)-65)}if(n){const e=t.charCodeAt(n-1);r+=e-(e>95?32:0)-65}return r}function I(e){return(e>=702?String.fromCharCode(((e-702)/676-0)%26+65):"")+(e>=26?String.fromCharCode(Math.floor((e/26-1)%26+65)):"")+String.fromCharCode(e%26+65)}function L(e){const t=/^(?=.)(\$(?=\D))?([A-Za-z]{0,3})?(\$)?([1-9][0-9]{0,6})?$/.exec(e);return t&&(t[2]||t[4])?[t[4]?(n=t[4],+n-1):null,t[2]?T(t[2]):null,!!t[3],!!t[1]]:null;var n}function _(e){let t=null,n=null,r=null,l=null,o=!1,u=!1,c=!1,i=!1;const[s,a,f]=e.split(":");if(f)return null;const p=L(s),h=a?L(a):null;if(!p||a&&!h)return null;if(null!=p[0]&&null!=p[1]?[t,n,o,u]=p:null==p[0]&&null!=p[1]?[,n,,u]=p:null!=p[0]&&null==p[1]&&([t,,o]=p),a)null!=h[0]&&null!=h[1]?[r,l,c,i]=h:null==h[0]&&null!=h[1]?[,l,,i]=h:null!=h[0]&&null==h[1]&&([r,,c]=h);else{if(null==t||null==n)return null;r=t,l=n,c=o,i=u}return null!=l&&(null==n||null!=n&&l<n)&&([n,l,u,i]=[l,n,i,u]),null!=r&&(null==t||null!=t&&r<t)&&([t,r,o,c]=[r,t,c,o]),{top:t,left:n,bottom:r,right:l,$top:o,$left:u,$bottom:c,$right:i}}function O(e){let{allowNamed:t=!0,allowTernary:n=!1}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=R(e,{allowNamed:t,allowTernary:n,r1c1:!1});if(r&&(r.r0||r.name)){let e=null;return r.r0&&(e=_(r.r1?r.r0+":"+r.r1:r.r0)),r.name||e?(r.range=e,delete r.r0,delete r.r1,r):null}return null}function U(e){return C(e)+(e.name?e.name:function(e){let{top:t,left:n,bottom:r,right:l}=e;const{$left:o,$right:u,$top:c,$bottom:i}=e,s=null==n,a=null==l,f=null==t,p=null==r;return t=N(0,0|t,1048575),n=N(0,0|n,16383),!s&&!f&&a&&p?(r=t,l=n):(r=N(0,0|r,1048575),l=N(0,0|l,16383)),0===t&&r>=1048575||f&&p?A(n,o)+":"+A(l,u):0===n&&l>=16383||s&&a?b(t,c)+":"+b(r,i):s||f||a||!p?s||!f||a||p?s||f||!a||p?!s||f||a||p?l!==n||r!==t||u!==o||i!==c?A(n,o)+b(t,c)+":"+A(l,u)+b(r,i):A(n,o)+b(t,c):A(l,u)+b(t,c)+":"+b(r,i):A(n,o)+b(t,c)+":"+b(r,i):A(n,o)+b(r,i)+":"+A(l,u):A(n,o)+b(t,c)+":"+A(l,u)}(e.range))}function S(e){return null==e.top&&(e.top=0,e.$top=!1),null==e.bottom&&(e.bottom=1048575,e.$bottom=!1),null==e.left&&(e.left=0,e.$left=!1),null==e.right&&(e.right=16383,e.$right=!1),e}const F=/^\[('['#@[\]]|[^'#@[\]])+\]/i,M=/^([^#@[\]:]+)/i,k={headers:1,data:2,totals:4,all:8,"this row":16,"@":16},D=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return Object.freeze(t)},j={0:D(),1:D("headers"),2:D("data"),4:D("totals"),8:D("all"),16:D("this row"),3:D("headers","data"),6:D("data","totals")},z=function(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=F.exec(e);if(n){const e=n[0].slice(1,-1).replace(/'(['#@[\]])/g,"$1");return[n[0],e]}return t&&(n=M.exec(e),n)?[n[0],n[0]]:null};function q(e){const t=[];let n,r,l=0,o=e,u=0;if(!(n=/^(\[\s*)/.exec(o)))return null;if(r=/^\[#([a-z ]+)\]/i.exec(o)){const e=r[1].toLowerCase();if(l+=r[0].length,!k[e])return null;u|=k[e]}else if(r=z(o,!1))l+=r[0].length,t.push(r[1].trim());else{let r=!0;for(o=o.slice(n[1].length),l+=n[1].length;r&&(n=/^\[#([a-z ]+)\](\s*,\s*)?/i.exec(o));){const e=n[1].toLowerCase();if(!k[e])return null;u|=k[e],o=o.slice(n[0].length),l+=n[0].length,r=!!n[2]}if(r&&(n=/^@/.exec(o))&&(u|=k["@"],o=o.slice(1),l+=1,r="]"!==o[0]),!(u in j))return null;const c=r?z(e.slice(l)):null;if(c){if(l+=c[0].length,t.push(c[1].trim()),o=e.slice(l),":"===o[0]){o=o.slice(1),l++;const e=z(o);if(!e)return null;l+=e[0].length,t.push(e[1].trim())}r=!1}for(;" "===e[l];)l++;if(r||"]"!==e[l])return null;l++}const c=j[u];return{columns:t,sections:c?c.concat():c,length:l,token:e.slice(0,l)}}function B(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const n=R(e,t);if(n&&n.struct){const e=q(n.struct);if(e&&e.length===n.struct.length)return{context:n.context,table:n.name,columns:e.columns,sections:e.sections}}return null}function W(e){return e.replace(/([[\]#'@])/g,"'$1")}function Z(e){return e[0].toUpperCase()+e.slice(1).toLowerCase()}function X(e){let t=C(e);e.table&&(t+=e.table);const n=e.columns?.length??0,r=e.sections?.length??0;if(1!==r||n)if(r||1!==n){t+="[";const o=1===r&&"this row"===e.sections[0].toLowerCase();o?t+="@":r&&(t+=e.sections.map((e=>`[#${Z(e)}]`)).join(","),n&&(t+=",")),o&&1===e.columns.length&&(l=e.columns[0],/^[a-zA-Z0-9\u00a1-\uffff]+$/.test(l))?t+=W(e.columns[0]):n&&(t+=e.columns.slice(0,2).map((e=>`[${W(e)}]`)).join(":")),t+="]"}else t+=`[${W(e.columns[0])}]`;else t+=`[#${Z(e.sections[0])}]`;var l;return t}const P=/^(\[(?:[^\]])+\])?([0-9A-Za-z._¡¤§¨ª\u00ad¯-\uffff]+)(?=!)/,H=/^'(?:''|[^'])*('|$)(?=!)/,Y="\\$?[A-Z]{1,3}\\$?[1-9][0-9]{0,6}",G="\\$?[A-Z]{1,3}",K="\\$?[1-9][0-9]{0,6}",V=new RegExp(`^${G}:${G}`,"i"),Q=new RegExp(`^${K}:${K}`,"i"),J=new RegExp(`^${Y}`,"i"),ee=new RegExp(`^((${G}|${K}):${Y}|${Y}:(${G}|${K}))(?![\\w($.])`,"i"),te="(?:R(?:\\[[+-]?\\d+\\]|[1-9][0-9]{0,6})?)",ne="(?:C(?:\\[[+-]?\\d+\\]|[1-9][0-9]{0,4})?)",re=new RegExp(`^${ne}(:${ne})?(?=\\W|$)`,"i"),le=new RegExp(`^${te}(:${te})?(?=\\W|$)`,"i"),oe=new RegExp(`^(?:(?=[RC])${te}${ne})`,"i"),ue=new RegExp(`^(${te}${ne}(:${ne}|:${te})(?![[\\d])|(${te}|${ne})(:${te}${ne}))(?=\\W|$)`,"i"),ce=/^(?![CR]\b)[a-zA-Z\\_\u00a1-\uffff][a-zA-Z0-9\\_.?\u00a1-\uffff]{0,254}/i;function ie(e,t){return n=>{const r=t.exec(n);if(r)return{type:e,value:r[0]}}}function se(e){const t=q(e);if(t){let n=t.length;for(;" "===e[n];)n++;if("!"!==e[n])return{type:"structured",value:t.token}}return null}const ae=/([RC])(\[?)(-?\d+)/gi,fe=/(\d+|[a-zA-Z]+)/gi;function pe(e,t){let l,o;if(t.r1c1){if(t.allowTernary&&(l=ue.exec(e))?o={type:r,value:l[0]}:(l=oe.exec(e))?o={type:"range",value:l[0]}:((l=le.exec(e))||(l=re.exec(e)))&&(o={type:n,value:l[0]}),o){for(ae.lastIndex=0;null!==(l=ae.exec(o.value));){const e=("R"===l[1]?1048575:16383)+(l[2]?1:0),t=parseInt(l[3],10);if(t>=e||t<=-e)return null}return o}}else if(t.allowTernary&&(l=ee.exec(e))?o={type:r,value:l[0]}:(l=V.exec(e))||(l=Q.exec(e))?o={type:n,value:l[0]}:(l=J.exec(e))&&(o={type:"range",value:l[0]}),o){for(fe.lastIndex=0;null!==(l=fe.exec(o.value));)if(/^\d/.test(l[1])){if(parseInt(l[1],10)-1>1048575)return null}else if(T(l[1])>16383)return null;return o}}const he=[ie("error",/^#(NAME\?|FIELD!|CALC!|VALUE!|REF!|DIV\/0!|NULL!|NUM!|N\/A|GETTING_DATA\b|SPILL!|UNKNOWN!|FIELD\b|CALC\b|SYNTAX\?|ERROR!|CONNECT!|BLOCKED!|EXTERNAL!)/i),ie(e,/^(<=|>=|<>|[-+/*^%&<>=]|[{},;]|[()]|@|:|!|#)/),ie("func",/^[A-Z_]+[A-Z\d_.]*(?=\()/i),ie("bool",/^(TRUE|FALSE)\b/i),ie("newline",/^\n+/),ie("whitespace",/^[ \f\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/),ie("string",/^"(?:""|[^"])*("|$)/),ie("context_quote",H),ie(t,P),pe,se,ie("number",/^(?:\d+(\.\d+)?(?:[eE][+-]?\d+)?|\d+)/),ie(l,ce)],ge=[function(t,n){return n.r1c1?"!"===t[0]?{type:e,value:t[0]}:null:"!"===t[0]||":"===t[0]?{type:e,value:t[0]}:null},ie("context_quote",H),ie(t,P),pe,se,ie(l,ce)],de={};function $e(e,t){if(e.length){const n=e[0];t[n]=t[n]||{},$e(e.slice(1),t[n])}else t.$=!0}[["range",":","range"],["range"],[n],[r],[t,"!","range",":","range"],[t,"!","range"],[t,"!",n],[t,"!",r],["context_quote","!","range",":","range"],["context_quote","!","range"],["context_quote","!",n],["context_quote","!",r],[l],[t,"!",l],["context_quote","!",l],["structured"],[l,"structured"],[t,"!",l,"structured"],["context_quote","!",l,"structured"]].forEach((e=>$e(e.concat().reverse(),de)));const ye=function(t,n,r){let l=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;const o=t[r-l];if(o){const u=o.type===e?o.value:o.type;if(u in n)return ye(t,n[u],r,l+1)}return n.$?l:0};function ve(e){const t=[];for(let n=e.length-1;n>=0;n--){let r=e[n];const l=ye(e,de,n);if(l){const t=e.slice(n-l+1,n+1);r={...r},r.value=t.map((e=>e.value)).join(""),r.loc&&t[0].loc&&(r.loc[0]=t[0].loc[0]),n-=l-1}t.unshift(r)}return t}const xe=(e,t)=>e&&e.type===t,me={withLocation:!1,mergeRefs:!0,allowTernary:!1,negativeNumbers:!0,r1c1:!1},Ee=e=>e.type===l||"func"===e.type,Re=t=>!xe(t,e)||"%"===t.value||"}"===t.value||")"===t.value||"#"===t.value;function we(t,n){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const l=Object.assign({},me,r),{withLocation:u,mergeRefs:c,negativeNumbers:i}=l,s=[];let a=0,f=null,p=null,h=null;const g=e=>{const t=e.type===o,n=h&&h.type===o;h&&(t&&n||t&&Ee(h)||n&&Ee(e))?(h.value+=e.value,h.type=o,u&&(h.loc[1]=e.loc[1])):(s.push(e),h=e,"whitespace"!==e.type&&"newline"!==e.type&&(p=f,f=e))};if(/^=/.test(t)){a++,g({type:"fx_prefix",value:"=",...u?{loc:[0,1]}:{}})}for(;a<t.length;){const r=a,c=t.slice(a);let d="",$="";for(let e=0;e<n.length;e++){const t=n[e](c,l);if(t){d=t.type,$=t.value,a+=$.length;break}}d||(d=o,$=t[a],a++);const y={type:d,value:$,...u?{loc:[r,a]}:{}};if("string"===d){const e=$.length;if('""'===$);else if('"'===$||'"'!==$[e-1])y.unterminated=!0;else if('""'!==$&&'"'===$[e-2]){let t=e-1;for(;'"'===$[t];)t--;!(t+1)^(e-t+1)%2==0&&(y.unterminated=!0)}}if(i&&"number"===d){const t=h;if(t&&xe(t,e)&&"-"===t.value&&(!p||xe(p,"fx_prefix")||!Re(p))){const e=s.pop();y.value="-"+$,u&&(y.loc[0]=e.loc[0]),f=p,h=s[s.length-1]}}g(y)}return c?ve(s):s}function Ce(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return we(e,he,t)}function Ne(e){return!!e&&("range"===e.type||e.type===n||e.type===r)}function Ae(e){return!!e&&("range"===e.type||e.type===n||e.type===r||"structured"===e.type||e.type===l)}function be(e){return!!e&&("bool"===e.type||"error"===e.type||"number"===e.type||"string"===e.type)}function Te(e){return!!e&&("whitespace"===e.type||"newline"===e.type)}function Ie(e){return!!e&&"func"===e.type}function Le(t){return!!t&&t.type===e}const _e="(END)",Oe=["ANCHORARRAY","CHOOSE","DROP","IF","IFS","INDEX","INDIRECT","LAMBDA","LET","OFFSET","REDUCE","SINGLE","SWITCH","TAKE","XLOOKUP"],Ue=e=>e.type===i||"ErrorLiteral"===e.type&&"#REF!"===e.value||e.type===c&&(":"===e.operator||" "===e.operator||","===e.operator)||e.type===s&&Oe.includes(e.callee.name.toUpperCase()),Se={};let Fe,Me,ke,De=!1;function je(e){const t=new SyntaxError(e);throw t.source=Me.map((e=>e.value)).join(""),t}function ze(){let e,t=ke;do{e=Me[++t]}while(e&&(Te(e)||Le(e)&&"("===e.value));return(e=>{const t=(e&&e.value)+"";return!(!Ae(e)&&(!Le(e)||":"!==t&&","!==t&&t.trim())&&(!Ie(e)||!Oe.includes(t.toUpperCase()))&&(!function(e){return!!e&&"error"===e.type}(e)||"#REF!"!==t))})(e)}function qe(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if(e&&e!==Fe.id&&je(`Expected ${e} but got ${Fe.id}`),Te(Me[ke])&&!ze())for(;Te(Me[ke]);)ke++;if(ke>=Me.length)return void(Fe=Se[_e]);const t=Me[ke];let n;ke+=1,t.unterminated&&je("Encountered an unterminated token");let r=t.type;return Le(t)?(n=Se[t.value],n||je(`Unknown operator ${t.value}`)):Te(t)?n=Se["(WHITESPACE)"]:be(t)?n=Se.Literal:Ae(t)?(n=Se[i],r=i):Ie(t)?n=Se["(FUNCTION)"]:je(`Unexpected ${t.type} token: ${t.value}`),Fe=Object.create(n),Fe.type=r,Fe.value=t.value,t.loc&&(Fe.loc=[...t.loc]),Fe}function Be(e){let t,n=Fe;for(qe(),t=n.nud();e<Fe.lbp;)n=Fe,qe(),t=n.led(t);return t}const We={nud:()=>je("Undefined"),led:()=>je("Missing operator")};function Ze(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=Se[e];return n?t>=n.lbp&&(n.lbp=t):(n={...We},n.id=e,n.value=e,n.lbp=t,Se[e]=n),n}function Xe(e,t,n){const r=Ze(e,t);return r.led=n||function(e){this.type=c,this.operator=this.value,delete this.value;const n=Be(t);return this.arguments=[e,n],this.loc&&(this.loc=[e.loc[0],n.loc[1]]),this},r}function Pe(e,t){const n=Ze(e,0);return n.lbp=70,n.led=t||function(e){return this.type=u,this.operator=this.value,delete this.value,this.arguments=[e],this.loc&&(this.loc[0]=e.loc[0]),this},n}function He(e,t){const n=Ze(e);return n.nud=t||function(){this.type=u,this.operator=this.value,delete this.value;const e=Be(70);return this.arguments=[e],this.loc&&(this.loc[1]=e.loc[1]),this},n}function Ye(e,t){return Xe(e,t,(function(n){Ue(n)||je(`Unexpected ${e} operator`);const r=Be(t);return Ue(r)||je(`Unexpected ${Fe.type} following ${this.id}`),this.type=c,this.operator=this.value.trim()?this.value:" ",delete this.value,this.arguments=[n,r],this.loc&&(this.loc=[n.loc[0],r.loc[1]]),this}))}Ze(_e),Ye(":",80);const Ge=Ye(",",80);Ye("(WHITESPACE)",80);const Ke=e=>{const t=Ge.lbp>0;return null!=e&&(Ge.lbp=e?80:0),t};function Ve(){let e=1;return()=>"fxg"+e++}function Qe(e,t){return null==e&&null==t||e===t}function Je(e,t){return!e&&!t||String(e).toLowerCase()===String(t).toLowerCase()}function et(e,t){return(!e.name&&!t.name||e.name===t.name)&&(!!(!e.range&&!t.range||Qe(e.range.top,t.range.top)&&Qe(e.range.bottom,t.range.bottom)&&Qe(e.range.left,t.range.left)&&Qe(e.range.right,t.range.right))&&!(!Je(e.context[0],t.context[0])||!Je(e.context[1],t.context[1])))}Pe("%"),Pe("#",(function(e){return Ue(e)||je("# expects a reference"),this.type=u,this.operator=this.value,delete this.value,this.arguments=[e],this})),He("+"),He("-"),He("@"),Xe("^",50),Xe("*",40),Xe("/",40),Xe("+",30),Xe("-",30),Xe("&",20),Xe("=",10),Xe("<",10),Xe(">",10),Xe("<=",10),Xe(">=",10),Xe("<>",10),Ze("Literal").nud=function(){const{type:e,value:t}=this;if(this.type="Literal",this.raw=t,"number"===e)this.value=+t;else if("bool"===e)this.value="TRUE"===t.toUpperCase();else if("error"===e)this.type="ErrorLiteral",this.value=t.toUpperCase();else{if("string"!==e)throw new Error("Unsupported literal type: "+e);this.value=t.slice(1,-1).replace(/""/g,'"')}return this},Ze(i).nud=function(){return this.type=i,this},Ze(")"),He("(",(function(){const e=Ke(!0),t=Be(0);return qe(")"),Ke(e),t})),Ze("(FUNCTION)").nud=function(){return this},Xe("(",90,(function(e){"(FUNCTION)"!==e.id&&je("Cannot call a "+e.type);const t=[];let n=!1;if(")"!==Fe.id){const e=Ke(!1);for(;")"!==Fe.id;)if(","===Fe.id)t.push(null),n=!0,qe();else{const e=Be(0);t.push(e),n=!1,","===Fe.id&&(qe(","),n=!0)}Ke(e)}n&&t.push(null);const r=Fe;return qe(")"),delete this.value,this.type=s,this.callee={type:"Identifier",name:e.value},e.loc&&(this.callee.loc=[...e.loc]),this.arguments=t,e.loc&&(this.loc=[e.loc[0],r.loc[1]]),this})),Ze("}"),Ze(";"),He("{",(function(){"}"===Fe.id&&je("Unexpected empty array");let e=[],t=!1;const n=[e],r=Ke(!1);for(;!t;)be(Fe)?e.push(Se.Literal.nud.call(Fe)):De&&Ue(Fe)?e.push(Se[i].nud.call(Fe)):je(`Unexpected ${Fe.type} in array: ${Fe.value}`),qe(),","===Fe.id?qe(","):";"===Fe.id?(qe(";"),e=[],n.push(e)):t=!0;const l=Fe;return qe("}"),Ke(r),this.type="ArrayExpression",this.elements=n,this.loc&&(this.loc[1]=l.loc[1]),delete this.value,this}));const tt=(e,t,n)=>Math.min(Math.max(t,e),n);function nt(e,t){return t?String(e+1):e?"["+e+"]":""}function rt(e){let t=null,n=null,r=null,l=null;const o=/^R(?:\[([+-]?\d+)\]|(\d+))?/.exec(e);o&&(o[1]?(t=parseInt(o[1],10),r=!1):o[2]?(t=parseInt(o[2],10)-1,r=!0):(t=0,r=!1),e=e.slice(o[0].length));const u=/^C(?:\[([+-]?\d+)\]|(\d+))?/.exec(e);return u&&(u[1]?(n=parseInt(u[1],10),l=!1):u[2]?(n=parseInt(u[2],10)-1,l=!0):(n=0,l=!1),e=e.slice(u[0].length)),!o&&!u||e.length?null:[t,n,r,l]}function lt(e){let t=null;const[n,r]=e.split(":",2),l=rt(n);if(l){const[e,n,o,u]=l;if(!r)return null!=e&&null==n?{r0:e,c0:null,r1:e,c1:null,$r0:o,$c0:!1,$r1:o,$c1:!1}:null==e&&null!=n?{r0:null,c0:n,r1:null,c1:n,$r0:!1,$c0:u,$r1:!1,$c1:u}:{r0:e||0,c0:n||0,r1:e||0,c1:n||0,$r0:o||!1,$c0:u||!1,$r1:o||!1,$c1:u||!1};{const l=rt(r);if(!l)return null;{t={};const[r,c,i,s]=l;null!=e&&null!=r?(t.r0=o===i?Math.min(e,r):e,t.$r0=o,t.r1=o===i?Math.max(e,r):r,t.$r1=i):null!=e&&null==r?(t.r0=e,t.$r0=o,t.r1=null,t.$r1=o):null==e&&null!=r?(t.r0=r,t.$r0=i,t.r1=null,t.$r1=i):null==e&&null==r&&(t.r0=null,t.$r0=!1,t.r1=null,t.$r1=!1),null!=n&&null!=c?(t.c0=u===s?Math.min(n,c):n,t.$c0=u,t.c1=u===s?Math.max(n,c):c,t.$c1=s):null!=n&&null==c?(t.c0=n,t.$c0=u,t.c1=null,t.$c1=u):null==n&&null!=c?(t.c0=c,t.$c0=s,t.c1=null,t.$c1=s):null==n&&null==c&&(t.c0=null,t.$c0=!1,t.c1=null,t.$c1=!1)}}}return t}function ot(e){let{allowNamed:t=!0,allowTernary:n=!1}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=R(e,{allowNamed:t,allowTernary:n,r1c1:!0});if(r&&(r.r0||r.name)){const e=r.r1?lt(r.r0+":"+r.r1):lt(r.r0);return r.name||e?(r.range=e,delete r.r0,delete r.r1,r):null}return null}function ut(e){return C(e)+(e.name?e.name:function(e){let{r0:t,c0:n,r1:r,c1:l}=e;const{$c0:o,$c1:u,$r0:c,$r1:i}=e,s=null==t,a=null==n;let f=null==r,p=null==l;if(t=tt(c?0:-1048575,0|t,1048575),n=tt(o?0:-16383,0|n,16383),!s&&f&&!a&&p?(r=t,f=!1,l=n,p=!1):(r=tt(i?0:-1048575,0|r,1048575),l=tt(u?0:-16383,0|l,16383)),0===t&&r>=1048575||s&&f){const e=nt(n,o),t=nt(l,u);return"C"+(e===t?e:e+":C"+t)}if(0===n&&l>=16383||a&&p){const e=nt(t,c),n=nt(r,i);return"R"+(e===n?e:e+":R"+n)}const h=nt(t,c),g=nt(r,i),d=nt(n,o),$=nt(l,u);return s||f||a||p?(s?"":"R"+h)+(a?"":"C"+d)+":"+(f?"":"R"+g)+(p?"":"C"+$):h!==g||d!==$?"R"+h+"C"+d+":R"+g+"C"+$:"R"+h+"C"+d}(e.range))}const ct=(e,t,n)=>null==t?null:e?t:t-n,it={withLocation:!1,mergeRefs:!1,allowTernary:!0,r1c1:!1};function st(e,t,n,r){let l=!(arguments.length>4&&void 0!==arguments[4])||arguments[4],o=e;if(null!=o&&!t){if(o=n+e,o<0){if(!l)return NaN;o=r+o+1}if(o>r){if(!l)return NaN;o-=r+1}}return o}const at={wrapEdges:!0,mergeRefs:!0};const ft=Object.freeze({OPERATOR:e,BOOLEAN:"bool",ERROR:"error",NUMBER:"number",FUNCTION:"func",NEWLINE:"newline",WHITESPACE:"whitespace",STRING:"string",CONTEXT:t,CONTEXT_QUOTE:"context_quote",REF_RANGE:"range",REF_BEAM:n,REF_TERNARY:r,REF_NAMED:l,REF_STRUCT:"structured",FX_PREFIX:"fx_prefix",UNKNOWN:o}),pt=Object.freeze({UNARY:u,BINARY:c,REFERENCE:i,LITERAL:"Literal",ERROR:"ErrorLiteral",CALL:s,ARRAY:"ArrayExpression",IDENTIFIER:"Identifier"});exports.MAX_COLS=16383,exports.MAX_ROWS=1048575,exports.addA1RangeBounds=S,exports.addTokenMeta=function(e){let{sheetName:t="",workbookName:l=""}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const u=[];let c=null;const i=Ve(),s=[],a=()=>u.length+(c?1:0);return e.forEach(((e,f)=>{if(e.index=f,e.depth=a(),"("===e.value)u.push(e),e.depth=a();else if(")"===e.value){const t=u.pop();if(t){const n=i();e.groupId=n,e.depth=t.depth,t.groupId=n}else e.error=!0}else if("{"===e.value)c?e.error=!0:(c=e,e.depth=a());else if("}"===e.value){if(c){const t=i();e.groupId=t,e.depth=c.depth,c.groupId=t}else e.error=!0;c=null}else if("range"===e.type||e.type===n||e.type===r){const n=O(e.value,{allowTernary:!0});if(n&&n.range){if(n.source=e.value,n.context.length){if(1===n.context.length){const e=n.context[0];n.context=e===t||e===l?[l,t]:[l,e]}}else n.context=[l,t];const r=s.find((e=>et(e,n)));r?e.groupId=r.groupId:(n.groupId=i(),e.groupId=n.groupId,s.push(n))}}else e.type===o&&(e.error=!0)})),e},exports.fixRanges=function e(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{addBounds:!1};if("string"==typeof t)return e(Ce(t,n),n).map((e=>e.value)).join("");if(!Array.isArray(t))throw new Error("fixRanges expects an array of tokens");const{addBounds:r,r1c1:l}=n;if(l)throw new Error("fixRanges does not have an R1C1 mode");let o=0;return t.map((e=>{const t={...e};e.loc&&(t.loc=[...e.loc]);let n=0;if("structured"===t.type){const e=X(B(t.value));n=e.length-t.value.length,t.value=e}else if(Ne(t)){const e=O(t.value,{allowTernary:!0}),l=e.range;r&&S(l);const o=U(e);n=o.length-t.value.length,t.value=o}return o||n?(t.loc&&(t.loc[0]+=o),o+=n,t.loc&&(t.loc[1]+=o)):o+=n,t}))},exports.fromCol=T,exports.isRange=Ne,exports.isReference=Ae,exports.mergeRefTokens=ve,exports.nodeTypes=pt,exports.parse=function(e,t){if("string"==typeof e)Me=Ce(e,{withLocation:!0,...t,mergeRefs:!0});else{if(!Array.isArray(e))throw new Error("Parse requires a string or array of tokens.");Me=e}for(De=t?.permitArrayRanges,ke=0;Te(Me[ke])||(n=Me[ke])&&"fx_prefix"===n.type;)ke++;var n;qe(),Ke(!0);const r=Be(0);return qe(_e),r},exports.parseA1Ref=O,exports.parseR1C1Ref=ot,exports.parseStructRef=B,exports.stringifyA1Ref=U,exports.stringifyR1C1Ref=ut,exports.stringifyStructRef=X,exports.toCol=I,exports.tokenTypes=ft,exports.tokenize=Ce,exports.translateToA1=function(e,t){let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:at;const r=_(t),l="string"==typeof e,o={...at,...n},u=l?Ce(e,{withLocation:!1,mergeRefs:o.mergeRefs,allowTernary:!0,r1c1:!0}):e;let c=0;return u.forEach((e=>{if(Ne(e)){const t=e.value,n=ot(t,{allowTernary:!0}),l=n.range,u={},i=st(l.r0,l.$r0,r.top,1048575,o.wrapEdges),s=st(l.r1,l.$r1,r.top,1048575,o.wrapEdges);i>s?(u.top=s,u.$top=l.$r1,u.bottom=i,u.$bottom=l.$r0):(u.top=i,u.$top=l.$r0,u.bottom=s,u.$bottom=l.$r1);const a=st(l.c0,l.$c0,r.left,16383,o.wrapEdges),f=st(l.c1,l.$c1,r.left,16383,o.wrapEdges);a>f?(u.left=f,u.$left=l.$c1,u.right=a,u.$right=l.$c0):(u.left=a,u.$left=l.$c0,u.right=f,u.$right=l.$c1),isNaN(i)||isNaN(s)||isNaN(a)||isNaN(f)?(e.type="error",e.value="#REF!",delete e.groupId):(n.range=u,e.value=U(n)),e.loc&&(e.loc[0]+=c,c+=e.value.length-t.length,e.loc[1]+=c)}else c&&e.loc&&(e.loc[0]+=c,e.loc[1]+=c)})),l?u.map((e=>e.value)).join(""):u},exports.translateToR1C1=function(e,t){const{top:n,left:r}=_(t),l="string"==typeof e,o=l?Ce(e,it):e;let u=0;return o.forEach((e=>{if(Ne(e)){const t=e.value,l=O(t,{allowTernary:!0}),o=l.range,c={};c.r0=ct(o.$top,o.top,n),c.r1=ct(o.$bottom,o.bottom,n),c.c0=ct(o.$left,o.left,r),c.c1=ct(o.$right,o.right,r),c.$r0=o.$top,c.$r1=o.$bottom,c.$c0=o.$left,c.$c1=o.$right,l.range=c,e.value=ut(l),e.loc&&(e.loc[0]+=u,u+=e.value.length-t.length,e.loc[1]+=u)}else u&&e.loc&&(e.loc[0]+=u,e.loc[1]+=u)})),l?o.map((e=>e.value)).join(""):o};
1
+ "use strict";const e="operator",t="context",n="range_beam",r="range_ternary",l="range_named",o="unknown",u="UnaryExpression",c="BinaryExpression",i="ReferenceIdentifier",s="CallExpression";function a(e){const t=/(?:\[(.+?)\])?([^[\]]+?)$/.exec(e);if(t){const[,e,n]=t;return{context:[e,n].filter(Boolean)}}}const f=e=>e&&":"===e.value&&{},p=e=>e&&"range"===e.type&&{r0:e.value},h=e=>e&&e.type===r&&{r0:e.value},g=e=>e&&"range"===e.type&&{r1:e.value},d=t=>t&&t.type===e&&"!"===t.value&&{},$=e=>e&&e.type===n&&{r0:e.value},y=e=>e&&"structured"===e.type&&{struct:e.value},x=e=>e&&e.type===t?a(e.value):e&&"context_quote"===e.type?a(e.value.slice(1,-1).replace(/''/g,"'")):void 0,v=e=>e&&e.type===l&&{name:e.value},m=[[h],[p,f,g],[p],[$],[x,d,h],[x,d,p,f,g],[x,d,p],[x,d,$]],E=m.concat([[v],[x,d,v],[y],[v,y],[x,d,v,y]]);function R(e,t){const n={withLocation:!1,mergeRefs:!1,allowTernary:!1,allowNamed:!0,r1c1:!1,...t},r=we(e,ge,n),l={context:[],r0:"",r1:"",name:""};r.length&&"fx_prefix"===r[0].type&&r.shift();const o=n.allowNamed?E:m;for(let e=0;e<o.length;e++){const t={...l};if(o[e].length===r.length){const n=o[e].every(((e,n)=>{const l=e(r[n]);return Object.assign(t,l),l}));if(n)return t}}return null}const w=/[^0-9A-Za-z._¡¤§¨ª\u00ad¯-\uffff]/;function C(e){let t="",n=0,r=0;const l=e.context||[];for(let e=l.length;e>-1;e--){const o=l[e];if(o){t=(r%2?"["+o+"]":o)+t,n+=w.test(o),r++}}return n&&(t="'"+t.replace(/'/g,"''")+"'"),t?t+"!":t}const N=(e,t,n)=>Math.min(Math.max(t,e),n),A=(e,t)=>(t?"$":"")+I(e),b=(e,t)=>(t?"$":"")+String(e+1);function T(e){const t=e||"",n=t.length;let r=0;if(n>2){const e=t.charCodeAt(n-3);r+=676*(1+e-(e>95?32:0)-65)}if(n>1){const e=t.charCodeAt(n-2);r+=26*(1+e-(e>95?32:0)-65)}if(n){const e=t.charCodeAt(n-1);r+=e-(e>95?32:0)-65}return r}function I(e){return(e>=702?String.fromCharCode(((e-702)/676-0)%26+65):"")+(e>=26?String.fromCharCode(Math.floor((e/26-1)%26+65)):"")+String.fromCharCode(e%26+65)}function L(e){const t=/^(?=.)(\$(?=\D))?([A-Za-z]{0,3})?(\$)?([1-9][0-9]{0,6})?$/.exec(e);return t&&(t[2]||t[4])?[t[4]?(n=t[4],+n-1):null,t[2]?T(t[2]):null,!!t[3],!!t[1]]:null;var n}function O(e){let t=null,n=null,r=null,l=null,o=!1,u=!1,c=!1,i=!1;const[s,a,f]=e.split(":");if(f)return null;const p=L(s),h=a?L(a):null;if(!p||a&&!h)return null;if(null!=p[0]&&null!=p[1]?[t,n,o,u]=p:null==p[0]&&null!=p[1]?[,n,,u]=p:null!=p[0]&&null==p[1]&&([t,,o]=p),a)null!=h[0]&&null!=h[1]?[r,l,c,i]=h:null==h[0]&&null!=h[1]?[,l,,i]=h:null!=h[0]&&null==h[1]&&([r,,c]=h);else{if(null==t||null==n)return null;r=t,l=n,c=o,i=u}return null!=l&&(null==n||null!=n&&l<n)&&([n,l,u,i]=[l,n,i,u]),null!=r&&(null==t||null!=t&&r<t)&&([t,r,o,c]=[r,t,c,o]),{top:t,left:n,bottom:r,right:l,$top:o,$left:u,$bottom:c,$right:i}}function _(e){let{allowNamed:t=!0,allowTernary:n=!1}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=R(e,{allowNamed:t,allowTernary:n,r1c1:!1});if(r&&(r.r0||r.name)){let e=null;return r.r0&&(e=O(r.r1?r.r0+":"+r.r1:r.r0)),r.name||e?(r.range=e,delete r.r0,delete r.r1,r):null}return null}function U(e){return C(e)+(e.name?e.name:function(e){let{top:t,left:n,bottom:r,right:l}=e;const{$left:o,$right:u,$top:c,$bottom:i}=e,s=null==n,a=null==l,f=null==t,p=null==r;return t=N(0,0|t,1048575),n=N(0,0|n,16383),!s&&!f&&a&&p?(r=t,l=n):(r=N(0,0|r,1048575),l=N(0,0|l,16383)),0===t&&r>=1048575||f&&p?A(n,o)+":"+A(l,u):0===n&&l>=16383||s&&a?b(t,c)+":"+b(r,i):s||f||a||!p?s||!f||a||p?s||f||!a||p?!s||f||a||p?l!==n||r!==t||u!==o||i!==c?A(n,o)+b(t,c)+":"+A(l,u)+b(r,i):A(n,o)+b(t,c):A(l,u)+b(t,c)+":"+b(r,i):A(n,o)+b(t,c)+":"+b(r,i):A(n,o)+b(r,i)+":"+A(l,u):A(n,o)+b(t,c)+":"+A(l,u)}(e.range))}function F(e){return null==e.top&&(e.top=0,e.$top=!1),null==e.bottom&&(e.bottom=1048575,e.$bottom=!1),null==e.left&&(e.left=0,e.$left=!1),null==e.right&&(e.right=16383,e.$right=!1),e}const S=/^\[('['#@[\]]|[^'#@[\]])+\]/i,M=/^([^#@[\]:]+)/i,k={headers:1,data:2,totals:4,all:8,"this row":16,"@":16},D=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return Object.freeze(t)},j={0:D(),1:D("headers"),2:D("data"),4:D("totals"),8:D("all"),16:D("this row"),3:D("headers","data"),6:D("data","totals")},z=function(e){let t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=S.exec(e);if(n){const e=n[0].slice(1,-1).replace(/'(['#@[\]])/g,"$1");return[n[0],e]}return t&&(n=M.exec(e),n)?[n[0],n[0]]:null};function W(e){const t=[];let n,r,l=0,o=e,u=0;if(!(n=/^(\[\s*)/.exec(o)))return null;if(r=/^\[#([a-z ]+)\]/i.exec(o)){const e=r[1].toLowerCase();if(l+=r[0].length,!k[e])return null;u|=k[e]}else if(r=z(o,!1))l+=r[0].length,t.push(r[1].trim());else{let r=!0;for(o=o.slice(n[1].length),l+=n[1].length;r&&(n=/^\[#([a-z ]+)\](\s*,\s*)?/i.exec(o));){const e=n[1].toLowerCase();if(!k[e])return null;u|=k[e],o=o.slice(n[0].length),l+=n[0].length,r=!!n[2]}if(r&&(n=/^@/.exec(o))&&(u|=k["@"],o=o.slice(1),l+=1,r="]"!==o[0]),!(u in j))return null;const c=r?z(e.slice(l)):null;if(c){if(l+=c[0].length,t.push(c[1].trim()),o=e.slice(l),":"===o[0]){o=o.slice(1),l++;const e=z(o);if(!e)return null;l+=e[0].length,t.push(e[1].trim())}r=!1}for(;" "===e[l];)l++;if(r||"]"!==e[l])return null;l++}const c=j[u];return{columns:t,sections:c?c.concat():c,length:l,token:e.slice(0,l)}}function q(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const n=R(e,t);if(n&&n.struct){const e=W(n.struct);if(e&&e.length===n.struct.length)return{context:n.context,table:n.name,columns:e.columns,sections:e.sections}}return null}function B(e){return e.replace(/([[\]#'@])/g,"'$1")}function Z(e){return e[0].toUpperCase()+e.slice(1).toLowerCase()}function P(e){let t=C(e);e.table&&(t+=e.table);const n=e.columns?.length??0,r=e.sections?.length??0;if(1!==r||n)if(r||1!==n){t+="[";const o=1===r&&"this row"===e.sections[0].toLowerCase();o?t+="@":r&&(t+=e.sections.map((e=>`[#${Z(e)}]`)).join(","),n&&(t+=",")),o&&1===e.columns.length&&(l=e.columns[0],/^[a-zA-Z0-9\u00a1-\uffff]+$/.test(l))?t+=B(e.columns[0]):n&&(t+=e.columns.slice(0,2).map((e=>`[${B(e)}]`)).join(":")),t+="]"}else t+=`[${B(e.columns[0])}]`;else t+=`[#${Z(e.sections[0])}]`;var l;return t}const X=/^(\[(?:[^\]])+\])?([0-9A-Za-z._¡¤§¨ª\u00ad¯-\uffff]+)(?=!)/,H=/^'(?:''|[^'])*('|$)(?=!)/,Y="\\$?[A-Z]{1,3}\\$?[1-9][0-9]{0,6}",G="\\$?[A-Z]{1,3}",K="\\$?[1-9][0-9]{0,6}",V=new RegExp(`^${G}:${G}`,"i"),Q=new RegExp(`^${K}:${K}`,"i"),J=new RegExp(`^${Y}`,"i"),ee=new RegExp(`^((${G}|${K}):${Y}|${Y}:(${G}|${K}))(?![\\w($.])`,"i"),te="(?:R(?:\\[[+-]?\\d+\\]|[1-9][0-9]{0,6})?)",ne="(?:C(?:\\[[+-]?\\d+\\]|[1-9][0-9]{0,4})?)",re=new RegExp(`^${ne}(:${ne})?(?=\\W|$)`,"i"),le=new RegExp(`^${te}(:${te})?(?=\\W|$)`,"i"),oe=new RegExp(`^(?:(?=[RC])${te}${ne})`,"i"),ue=new RegExp(`^(${te}${ne}(:${ne}|:${te})(?![[\\d])|(${te}|${ne})(:${te}${ne}))(?=\\W|$)`,"i"),ce=/^(?![CR]\b)[a-zA-Z\\_\u00a1-\uffff][a-zA-Z0-9\\_.?\u00a1-\uffff]{0,254}/i;function ie(e,t){return n=>{const r=t.exec(n);if(r)return{type:e,value:r[0]}}}function se(e){const t=W(e);if(t){let n=t.length;for(;" "===e[n];)n++;if("!"!==e[n])return{type:"structured",value:t.token}}return null}const ae=/([RC])(\[?)(-?\d+)/gi,fe=/(\d+|[a-zA-Z]+)/gi;function pe(e,t){let l,o;if(t.r1c1){if(t.allowTernary&&(l=ue.exec(e))?o={type:r,value:l[0]}:(l=oe.exec(e))?o={type:"range",value:l[0]}:((l=le.exec(e))||(l=re.exec(e)))&&(o={type:n,value:l[0]}),o){for(ae.lastIndex=0;null!==(l=ae.exec(o.value));){const e=("R"===l[1]?1048575:16383)+(l[2]?1:0),t=parseInt(l[3],10);if(t>=e||t<=-e)return null}return o}}else if(t.allowTernary&&(l=ee.exec(e))?o={type:r,value:l[0]}:(l=V.exec(e))||(l=Q.exec(e))?o={type:n,value:l[0]}:(l=J.exec(e))&&(o={type:"range",value:l[0]}),o){for(fe.lastIndex=0;null!==(l=fe.exec(o.value));)if(/^\d/.test(l[1])){if(parseInt(l[1],10)-1>1048575)return null}else if(T(l[1])>16383)return null;return o}}const he=[ie("error",/^#(NAME\?|FIELD!|CALC!|VALUE!|REF!|DIV\/0!|NULL!|NUM!|N\/A|GETTING_DATA\b|SPILL!|UNKNOWN!|FIELD\b|CALC\b|SYNTAX\?|ERROR!|CONNECT!|BLOCKED!|EXTERNAL!)/i),ie(e,/^(<=|>=|<>|[-+/*^%&<>=]|[{},;]|[()]|@|:|!|#)/),ie("func",/^[A-Z_]+[A-Z\d_.]*(?=\()/i),ie("bool",/^(TRUE|FALSE)\b/i),ie("newline",/^\n+/),ie("whitespace",/^[ \f\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/),ie("string",/^"(?:""|[^"])*("|$)/),ie("context_quote",H),ie(t,X),pe,se,ie("number",/^(?:\d+(\.\d+)?(?:[eE][+-]?\d+)?|\d+)/),ie(l,ce)],ge=[function(t,n){return n.r1c1?"!"===t[0]?{type:e,value:t[0]}:null:"!"===t[0]||":"===t[0]?{type:e,value:t[0]}:null},ie("context_quote",H),ie(t,X),pe,se,ie(l,ce)],de={};function $e(e,t){if(e.length){const n=e[0];t[n]=t[n]||{},$e(e.slice(1),t[n])}else t.$=!0}[["range",":","range"],["range"],[n],[r],[t,"!","range",":","range"],[t,"!","range"],[t,"!",n],[t,"!",r],["context_quote","!","range",":","range"],["context_quote","!","range"],["context_quote","!",n],["context_quote","!",r],[l],[t,"!",l],["context_quote","!",l],["structured"],[l,"structured"],[t,"!",l,"structured"],["context_quote","!",l,"structured"]].forEach((e=>$e(e.concat().reverse(),de)));const ye=function(t,n,r){let l=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;const o=t[r-l];if(o){const u=o.type===e?o.value:o.type;if(u in n)return ye(t,n[u],r,l+1)}return n.$?l:0};function xe(e){const t=[];for(let n=e.length-1;n>=0;n--){let r=e[n];const l=ye(e,de,n);if(l){const t=e.slice(n-l+1,n+1);r={...r},r.value=t.map((e=>e.value)).join(""),r.loc&&t[0].loc&&(r.loc[0]=t[0].loc[0]),n-=l-1}t.unshift(r)}return t}const ve=(e,t)=>e&&e.type===t,me={withLocation:!1,mergeRefs:!0,allowTernary:!1,negativeNumbers:!0,r1c1:!1},Ee=e=>e.type===l||"func"===e.type,Re=t=>!ve(t,e)||"%"===t.value||"}"===t.value||")"===t.value||"#"===t.value;function we(t,n){let r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const l=Object.assign({},me,r),{withLocation:u,mergeRefs:c,negativeNumbers:i}=l,s=[];let a=0,f=null,p=null,h=null;const g=e=>{const t=e.type===o,n=h&&h.type===o;h&&(t&&n||t&&Ee(h)||n&&Ee(e))?(h.value+=e.value,h.type=o,u&&(h.loc[1]=e.loc[1])):(s.push(e),h=e,"whitespace"!==e.type&&"newline"!==e.type&&(p=f,f=e))};if(/^=/.test(t)){a++,g({type:"fx_prefix",value:"=",...u?{loc:[0,1]}:{}})}for(;a<t.length;){const r=a,c=t.slice(a);let d="",$="";for(let e=0;e<n.length;e++){const t=n[e](c,l);if(t){d=t.type,$=t.value,a+=$.length;break}}d||(d=o,$=t[a],a++);const y={type:d,value:$,...u?{loc:[r,a]}:{}};if("string"===d){const e=$.length;if('""'===$);else if('"'===$||'"'!==$[e-1])y.unterminated=!0;else if('""'!==$&&'"'===$[e-2]){let t=e-1;for(;'"'===$[t];)t--;!(t+1)^(e-t+1)%2==0&&(y.unterminated=!0)}}if(i&&"number"===d){const t=h;if(t&&ve(t,e)&&"-"===t.value&&(!p||ve(p,"fx_prefix")||!Re(p))){const e=s.pop();y.value="-"+$,u&&(y.loc[0]=e.loc[0]),f=p,h=s[s.length-1]}}g(y)}return c?xe(s):s}function Ce(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return we(e,he,t)}function Ne(e){return!!e&&("range"===e.type||e.type===n||e.type===r)}function Ae(e){return!!e&&("range"===e.type||e.type===n||e.type===r||"structured"===e.type||e.type===l)}function be(e){return!!e&&("bool"===e.type||"error"===e.type||"number"===e.type||"string"===e.type)}function Te(e){return!!e&&"error"===e.type}function Ie(e){return!!e&&("whitespace"===e.type||"newline"===e.type)}function Le(e){return!!e&&"func"===e.type}function Oe(e){return!!e&&"fx_prefix"===e.type}function _e(t){return!!t&&t.type===e}const Ue="(END)",Fe=["ANCHORARRAY","CHOOSE","DROP","IF","IFS","INDEX","INDIRECT","LAMBDA","LET","OFFSET","REDUCE","SINGLE","SWITCH","TAKE","XLOOKUP"],Se=e=>e.type===i||"ErrorLiteral"===e.type&&"#REF!"===e.value||e.type===c&&(":"===e.operator||" "===e.operator||","===e.operator)||e.type===s&&Fe.includes(e.callee.name.toUpperCase()),Me={};let ke,De,je,ze=!1;function We(e){const t=new SyntaxError(e);throw t.source=De.map((e=>e.value)).join(""),t}function qe(){let e,t=je;do{e=De[++t]}while(e&&(Ie(e)||_e(e)&&"("===e.value));return(e=>{const t=(e&&e.value)+"";return!(!Ae(e)&&(!_e(e)||":"!==t&&","!==t&&t.trim())&&(!Le(e)||!Fe.includes(t.toUpperCase()))&&(!Te(e)||"#REF!"!==t))})(e)}function Be(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if(e&&e!==ke.id&&We(`Expected ${e} but got ${ke.id}`),Ie(De[je])&&!qe())for(;Ie(De[je]);)je++;if(je>=De.length)return void(ke=Me[Ue]);const t=De[je];let n;je+=1,t.unterminated&&We("Encountered an unterminated token");let r=t.type;return _e(t)?(n=Me[t.value],n||We(`Unknown operator ${t.value}`)):Ie(t)?n=Me["(WHITESPACE)"]:be(t)?n=Me.Literal:Ae(t)?(n=Me[i],r=i):Le(t)?n=Me["(FUNCTION)"]:We(`Unexpected ${t.type} token: ${t.value}`),ke=Object.create(n),ke.type=r,ke.value=t.value,t.loc&&(ke.loc=[...t.loc]),ke}function Ze(e){let t,n=ke;for(Be(),t=n.nud();e<ke.lbp;)n=ke,Be(),t=n.led(t);return t}const Pe={nud:()=>We("Undefined"),led:()=>We("Missing operator")};function Xe(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=Me[e];return n?t>=n.lbp&&(n.lbp=t):(n={...Pe},n.id=e,n.value=e,n.lbp=t,Me[e]=n),n}function He(e,t,n){const r=Xe(e,t);return r.led=n||function(e){this.type=c,this.operator=this.value,delete this.value;const n=Ze(t);return this.arguments=[e,n],this.loc&&(this.loc=[e.loc[0],n.loc[1]]),this},r}function Ye(e,t){const n=Xe(e,0);return n.lbp=70,n.led=t||function(e){return this.type=u,this.operator=this.value,delete this.value,this.arguments=[e],this.loc&&(this.loc[0]=e.loc[0]),this},n}function Ge(e,t){const n=Xe(e);return n.nud=t||function(){this.type=u,this.operator=this.value,delete this.value;const e=Ze(70);return this.arguments=[e],this.loc&&(this.loc[1]=e.loc[1]),this},n}function Ke(e,t){return He(e,t,(function(n){Se(n)||We(`Unexpected ${e} operator`);const r=Ze(t);return Se(r)||We(`Unexpected ${ke.type} following ${this.id}`),this.type=c,this.operator=this.value.trim()?this.value:" ",delete this.value,this.arguments=[n,r],this.loc&&(this.loc=[n.loc[0],r.loc[1]]),this}))}Xe(Ue),Ke(":",80);const Ve=Ke(",",80);Ke("(WHITESPACE)",80);const Qe=e=>{const t=Ve.lbp>0;return null!=e&&(Ve.lbp=e?80:0),t};function Je(){let e=1;return()=>"fxg"+e++}function et(e,t){return null==e&&null==t||e===t}function tt(e,t){return!e&&!t||String(e).toLowerCase()===String(t).toLowerCase()}function nt(e,t){return(!e.name&&!t.name||e.name===t.name)&&(!!(!e.range&&!t.range||et(e.range.top,t.range.top)&&et(e.range.bottom,t.range.bottom)&&et(e.range.left,t.range.left)&&et(e.range.right,t.range.right))&&!(!tt(e.context[0],t.context[0])||!tt(e.context[1],t.context[1])))}Ye("%"),Ye("#",(function(e){return Se(e)||We("# expects a reference"),this.type=u,this.operator=this.value,delete this.value,this.arguments=[e],this})),Ge("+"),Ge("-"),Ge("@"),He("^",50),He("*",40),He("/",40),He("+",30),He("-",30),He("&",20),He("=",10),He("<",10),He(">",10),He("<=",10),He(">=",10),He("<>",10),Xe("Literal").nud=function(){const{type:e,value:t}=this;if(this.type="Literal",this.raw=t,"number"===e)this.value=+t;else if("bool"===e)this.value="TRUE"===t.toUpperCase();else if("error"===e)this.type="ErrorLiteral",this.value=t.toUpperCase();else{if("string"!==e)throw new Error("Unsupported literal type: "+e);this.value=t.slice(1,-1).replace(/""/g,'"')}return this},Xe(i).nud=function(){return this.type=i,this},Xe(")"),Ge("(",(function(){const e=Qe(!0),t=Ze(0);return Be(")"),Qe(e),t})),Xe("(FUNCTION)").nud=function(){return this},He("(",90,(function(e){"(FUNCTION)"!==e.id&&We("Cannot call a "+e.type);const t=[];let n=!1;if(")"!==ke.id){const e=Qe(!1);for(;")"!==ke.id;)if(","===ke.id)t.push(null),n=!0,Be();else{const e=Ze(0);t.push(e),n=!1,","===ke.id&&(Be(","),n=!0)}Qe(e)}n&&t.push(null);const r=ke;return Be(")"),delete this.value,this.type=s,this.callee={type:"Identifier",name:e.value},e.loc&&(this.callee.loc=[...e.loc]),this.arguments=t,e.loc&&(this.loc=[e.loc[0],r.loc[1]]),this})),Xe("}"),Xe(";"),Ge("{",(function(){"}"===ke.id&&We("Unexpected empty array");let e=[],t=!1;const n=[e],r=Qe(!1);for(;!t;)be(ke)?e.push(Me.Literal.nud.call(ke)):ze&&Se(ke)?e.push(Me[i].nud.call(ke)):We(`Unexpected ${ke.type} in array: ${ke.value}`),Be(),","===ke.id?Be(","):";"===ke.id?(Be(";"),e=[],n.push(e)):t=!0;const l=ke;return Be("}"),Qe(r),this.type="ArrayExpression",this.elements=n,this.loc&&(this.loc[1]=l.loc[1]),delete this.value,this}));const rt=(e,t,n)=>Math.min(Math.max(t,e),n);function lt(e,t){return t?String(e+1):e?"["+e+"]":""}function ot(e){let t=null,n=null,r=null,l=null;const o=/^R(?:\[([+-]?\d+)\]|(\d+))?/.exec(e);o&&(o[1]?(t=parseInt(o[1],10),r=!1):o[2]?(t=parseInt(o[2],10)-1,r=!0):(t=0,r=!1),e=e.slice(o[0].length));const u=/^C(?:\[([+-]?\d+)\]|(\d+))?/.exec(e);return u&&(u[1]?(n=parseInt(u[1],10),l=!1):u[2]?(n=parseInt(u[2],10)-1,l=!0):(n=0,l=!1),e=e.slice(u[0].length)),!o&&!u||e.length?null:[t,n,r,l]}function ut(e){let t=null;const[n,r]=e.split(":",2),l=ot(n);if(l){const[e,n,o,u]=l;if(!r)return null!=e&&null==n?{r0:e,c0:null,r1:e,c1:null,$r0:o,$c0:!1,$r1:o,$c1:!1}:null==e&&null!=n?{r0:null,c0:n,r1:null,c1:n,$r0:!1,$c0:u,$r1:!1,$c1:u}:{r0:e||0,c0:n||0,r1:e||0,c1:n||0,$r0:o||!1,$c0:u||!1,$r1:o||!1,$c1:u||!1};{const l=ot(r);if(!l)return null;{t={};const[r,c,i,s]=l;null!=e&&null!=r?(t.r0=o===i?Math.min(e,r):e,t.$r0=o,t.r1=o===i?Math.max(e,r):r,t.$r1=i):null!=e&&null==r?(t.r0=e,t.$r0=o,t.r1=null,t.$r1=o):null==e&&null!=r?(t.r0=r,t.$r0=i,t.r1=null,t.$r1=i):null==e&&null==r&&(t.r0=null,t.$r0=!1,t.r1=null,t.$r1=!1),null!=n&&null!=c?(t.c0=u===s?Math.min(n,c):n,t.$c0=u,t.c1=u===s?Math.max(n,c):c,t.$c1=s):null!=n&&null==c?(t.c0=n,t.$c0=u,t.c1=null,t.$c1=u):null==n&&null!=c?(t.c0=c,t.$c0=s,t.c1=null,t.$c1=s):null==n&&null==c&&(t.c0=null,t.$c0=!1,t.c1=null,t.$c1=!1)}}}return t}function ct(e){let{allowNamed:t=!0,allowTernary:n=!1}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=R(e,{allowNamed:t,allowTernary:n,r1c1:!0});if(r&&(r.r0||r.name)){const e=r.r1?ut(r.r0+":"+r.r1):ut(r.r0);return r.name||e?(r.range=e,delete r.r0,delete r.r1,r):null}return null}function it(e){return C(e)+(e.name?e.name:function(e){let{r0:t,c0:n,r1:r,c1:l}=e;const{$c0:o,$c1:u,$r0:c,$r1:i}=e,s=null==t,a=null==n;let f=null==r,p=null==l;if(t=rt(c?0:-1048575,0|t,1048575),n=rt(o?0:-16383,0|n,16383),!s&&f&&!a&&p?(r=t,f=!1,l=n,p=!1):(r=rt(i?0:-1048575,0|r,1048575),l=rt(u?0:-16383,0|l,16383)),0===t&&r>=1048575||s&&f){const e=lt(n,o),t=lt(l,u);return"C"+(e===t?e:e+":C"+t)}if(0===n&&l>=16383||a&&p){const e=lt(t,c),n=lt(r,i);return"R"+(e===n?e:e+":R"+n)}const h=lt(t,c),g=lt(r,i),d=lt(n,o),$=lt(l,u);return s||f||a||p?(s?"":"R"+h)+(a?"":"C"+d)+":"+(f?"":"R"+g)+(p?"":"C"+$):h!==g||d!==$?"R"+h+"C"+d+":R"+g+"C"+$:"R"+h+"C"+d}(e.range))}const st=(e,t,n)=>null==t?null:e?t:t-n,at={withLocation:!1,mergeRefs:!1,allowTernary:!0,r1c1:!1};function ft(e,t,n,r){let l=!(arguments.length>4&&void 0!==arguments[4])||arguments[4],o=e;if(null!=o&&!t){if(o=n+e,o<0){if(!l)return NaN;o=r+o+1}if(o>r){if(!l)return NaN;o-=r+1}}return o}const pt={wrapEdges:!0,mergeRefs:!0};const ht=Object.freeze({OPERATOR:e,BOOLEAN:"bool",ERROR:"error",NUMBER:"number",FUNCTION:"func",NEWLINE:"newline",WHITESPACE:"whitespace",STRING:"string",CONTEXT:t,CONTEXT_QUOTE:"context_quote",REF_RANGE:"range",REF_BEAM:n,REF_TERNARY:r,REF_NAMED:l,REF_STRUCT:"structured",FX_PREFIX:"fx_prefix",UNKNOWN:o}),gt=Object.freeze({UNARY:u,BINARY:c,REFERENCE:i,LITERAL:"Literal",ERROR:"ErrorLiteral",CALL:s,ARRAY:"ArrayExpression",IDENTIFIER:"Identifier"});exports.MAX_COLS=16383,exports.MAX_ROWS=1048575,exports.addA1RangeBounds=F,exports.addTokenMeta=function(e){let{sheetName:t="",workbookName:l=""}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const u=[];let c=null;const i=Je(),s=[],a=()=>u.length+(c?1:0);return e.forEach(((e,f)=>{if(e.index=f,e.depth=a(),"("===e.value)u.push(e),e.depth=a();else if(")"===e.value){const t=u.pop();if(t){const n=i();e.groupId=n,e.depth=t.depth,t.groupId=n}else e.error=!0}else if("{"===e.value)c?e.error=!0:(c=e,e.depth=a());else if("}"===e.value){if(c){const t=i();e.groupId=t,e.depth=c.depth,c.groupId=t}else e.error=!0;c=null}else if("range"===e.type||e.type===n||e.type===r){const n=_(e.value,{allowTernary:!0});if(n&&n.range){if(n.source=e.value,n.context.length){if(1===n.context.length){const e=n.context[0];n.context=e===t||e===l?[l,t]:[l,e]}}else n.context=[l,t];const r=s.find((e=>nt(e,n)));r?e.groupId=r.groupId:(n.groupId=i(),e.groupId=n.groupId,s.push(n))}}else e.type===o&&(e.error=!0)})),e},exports.fixRanges=function e(t){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{addBounds:!1};if("string"==typeof t)return e(Ce(t,n),n).map((e=>e.value)).join("");if(!Array.isArray(t))throw new Error("fixRanges expects an array of tokens");const{addBounds:r,r1c1:l}=n;if(l)throw new Error("fixRanges does not have an R1C1 mode");let o=0;return t.map((e=>{const t={...e};e.loc&&(t.loc=[...e.loc]);let n=0;if("structured"===t.type){const e=P(q(t.value));n=e.length-t.value.length,t.value=e}else if(Ne(t)){const e=_(t.value,{allowTernary:!0}),l=e.range;r&&F(l);const o=U(e);n=o.length-t.value.length,t.value=o}return o||n?(t.loc&&(t.loc[0]+=o),o+=n,t.loc&&(t.loc[1]+=o)):o+=n,t}))},exports.fromCol=T,exports.isError=Te,exports.isFunction=Le,exports.isFxPrefix=Oe,exports.isLiteral=be,exports.isOperator=_e,exports.isRange=Ne,exports.isReference=Ae,exports.isWhitespace=Ie,exports.mergeRefTokens=xe,exports.nodeTypes=gt,exports.parse=function(e,t){if("string"==typeof e)De=Ce(e,{withLocation:!0,...t,mergeRefs:!0});else{if(!Array.isArray(e))throw new Error("Parse requires a string or array of tokens.");De=e}for(ze=t?.permitArrayRanges,je=0;Ie(De[je])||Oe(De[je]);)je++;Be(),Qe(!0);const n=Ze(0);return Be(Ue),n},exports.parseA1Ref=_,exports.parseR1C1Ref=ct,exports.parseStructRef=q,exports.stringifyA1Ref=U,exports.stringifyR1C1Ref=it,exports.stringifyStructRef=P,exports.toCol=I,exports.tokenTypes=ht,exports.tokenize=Ce,exports.translateToA1=function(e,t){let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:pt;const r=O(t),l="string"==typeof e,o={...pt,...n},u=l?Ce(e,{withLocation:!1,mergeRefs:o.mergeRefs,allowTernary:!0,r1c1:!0}):e;let c=0;return u.forEach((e=>{if(Ne(e)){const t=e.value,n=ct(t,{allowTernary:!0}),l=n.range,u={},i=ft(l.r0,l.$r0,r.top,1048575,o.wrapEdges),s=ft(l.r1,l.$r1,r.top,1048575,o.wrapEdges);i>s?(u.top=s,u.$top=l.$r1,u.bottom=i,u.$bottom=l.$r0):(u.top=i,u.$top=l.$r0,u.bottom=s,u.$bottom=l.$r1);const a=ft(l.c0,l.$c0,r.left,16383,o.wrapEdges),f=ft(l.c1,l.$c1,r.left,16383,o.wrapEdges);a>f?(u.left=f,u.$left=l.$c1,u.right=a,u.$right=l.$c0):(u.left=a,u.$left=l.$c0,u.right=f,u.$right=l.$c1),isNaN(i)||isNaN(s)||isNaN(a)||isNaN(f)?(e.type="error",e.value="#REF!",delete e.groupId):(n.range=u,e.value=U(n)),e.loc&&(e.loc[0]+=c,c+=e.value.length-t.length,e.loc[1]+=c)}else c&&e.loc&&(e.loc[0]+=c,e.loc[1]+=c)})),l?u.map((e=>e.value)).join(""):u},exports.translateToR1C1=function(e,t){const{top:n,left:r}=O(t),l="string"==typeof e,o=l?Ce(e,at):e;let u=0;return o.forEach((e=>{if(Ne(e)){const t=e.value,l=_(t,{allowTernary:!0}),o=l.range,c={};c.r0=st(o.$top,o.top,n),c.r1=st(o.$bottom,o.bottom,n),c.c0=st(o.$left,o.left,r),c.c1=st(o.$right,o.right,r),c.$r0=o.$top,c.$r1=o.$bottom,c.$c0=o.$left,c.$c1=o.$right,l.range=c,e.value=it(l),e.loc&&(e.loc[0]+=u,u+=e.value.length-t.length,e.loc[1]+=u)}else u&&e.loc&&(e.loc[0]+=u,e.loc[1]+=u)})),l?o.map((e=>e.value)).join(""):o};
2
2
  //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZnguanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
package/lib/parser.js CHANGED
@@ -455,7 +455,7 @@ prefix('{', function () {
455
455
  * because it does not recognize reference context tokens.
456
456
  *
457
457
  * The AST Abstract Syntax Tree's format is documented in
458
- * [AST format.md][AST format.md]
458
+ * [AST_format.md][./AST_format.md]
459
459
  *
460
460
  * @see nodeTypes
461
461
  * @param {(string | Array<Object>)} formula An Excel formula string (an Excel expression) or an array of tokens.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@borgar/fx",
3
- "version": "4.0.0-rc.2",
3
+ "version": "4.0.0",
4
4
  "description": "Utilities for working with Excel formulas",
5
5
  "main": "dist/fx.js",
6
6
  "module": "lib/index.js",
File without changes