UNPKG

subscript

Version:

Modular expression parser & evaluator

5 lines (4 loc) 8.26 kB
var s,p,f=r=>(s=0,p=r,f.enter?.(),r=g(),p[s]?S():r||""),S=(r="Unexpected token",t=s,e=p.slice(0,t).split(` `),n=e.pop(),o=p.slice(Math.max(0,t-40),t),m="\u032D",u=(p[t]||" ")+m,d=p.slice(t+1,t+20))=>{throw SyntaxError(`${r} at ${e.length+1}:${n.length+1} ${o}${u}${d}`)},v=(r,t=s)=>(Array.isArray(r)&&(r.loc=t),r),_=(r,t=s,e)=>{for(;e=r(p.charCodeAt(s));)s+=e;return p.slice(t,s)},w=(r=1)=>p[s+=r],H=r=>s=r,g=(r=0,t)=>{let e,n,o;for(t&&f.enter?.(r,t);(e=f.space())&&e!==t&&(o=f.step(n,r,e,g));)n=o;return t&&(e==t?(s++,f.exit?.(r,t)):S("Unclosed "+String.fromCharCode(t-(t>42?2:1)))),n},hr=(r=s)=>{for(;p.charCodeAt(r)<=32;)r++;return p.charCodeAt(r)},Wr=f.id=r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||r==36||r==95||r>=192&&r!=215&&r!=247,zr=(r,t=r.length)=>p.substr(s,t)===r&&!f.id(p.charCodeAt(s+t)),Jr=()=>(w(),g(0,41)),c=[],D={},y=(r,t=32,e,n=r.charCodeAt(0),o=r.length,m=c[n],u=r.toUpperCase()!==r,d,A)=>(t=D[r]=!m&&D[r]||t,c[n]=(C,E,L,Q=s)=>(d=L,(L?r==L:(o<2||r.charCodeAt(1)===p.charCodeAt(s+1)&&(o<3||p.substr(s,o)==r))&&(!u||!f.id(p.charCodeAt(s+o)))&&(d=L=r))&&E<t&&(s+=o,(A=e(C))?v(A,Q):(s=Q,d=0,!u&&!m&&!C&&S()),A)||m?.(C,E,d))),h=(r,t,e=!1)=>y(r,t,n=>n&&(o=>o&&[r,n,o])(g(t-(e?.5:0)))),I=(r,t,e)=>y(r,t,n=>e?n&&[r,n]:!n&&(n=g(t-.5))&&[r,n]),Kr=(r,t)=>y(r,200,e=>!e&&[,t]),N=(r,t,e,n)=>y(r,t,(o,m,u=o)=>(m=g(t-(e?.5:0)),o?.[0]!==r&&(o=[r,o||null]),m?.[0]===r?o.push(...m.slice(1)):m?o.push(m):n||hr()===r.charCodeAt(0)?o.push(null):u&&o.length===2&&(o=o[1]),o)),G=(r,t)=>y(r[0],t,e=>!e&&[r,g(0,r.charCodeAt(1))||null]),F=(r,t)=>y(r[0],t,e=>e&&[r,e,g(0,r.charCodeAt(1))||null]),Ar=(r,t)=>(f.space(),t=p.charCodeAt(s),f.id(t)&&(t<48||t>57)?_(f.id):g(r)),W=(r,t)=>y(r,t,e=>e&&(n=>n&&[r,e,n])(Ar(t))),Vr=(r,t,e,n=r.charCodeAt(0),o=r.length,m=c[n],u)=>(D[r]??=t,c[n]=(d,A,C,E=s)=>!d&&(C?r==C:(o<2||p.substr(s,o)==r)&&(C=r))&&A<t&&!f.id(p.charCodeAt(s+o))&&(!f.prop||f.prop(s+o))&&(H(s+o),(u=e())?v(u,E):H(E),u)||m?.(d,A,C));f.space=r=>{for(;(r=p.charCodeAt(s))<=32;)s++;return r};f.step=(r,t,e,n,o)=>(o=c[e])&&o(r,t)||(r?null:_(f.id)||null);var M={},l=(r,t,e=M[r])=>M[r]=(...n)=>t(...n)||e?.(...n),i=r=>Array.isArray(r)?r[0]==null?(t=>()=>t)(r[1]):M[r[0]]?.(...r.slice(1))??S(`Unknown operator: ${r[0]}`,r?.loc):r===void 0?()=>{}:t=>t?.[r];var $=46,R=48,U=57,z=69,J=101,Cr=43,cr=45,K=95,V=110,gr=97,yr=102,Er=65,Sr=70,Y=r=>r.indexOf("_")<0?r:r.replaceAll("_",""),B=r=>{let t=Y(_(e=>e===$&&(e=p.charCodeAt(s+1))!==$&&!(f.id(e)&&e>U&&e!==J&&e!==z)||e>=R&&e<=U||e===K||((e===z||e===J)&&((e=p.charCodeAt(s+1))>=R&&e<=U||e===Cr||e===cr)?2:0)));return p.charCodeAt(s)===V?(w(),[,BigInt(t)]):(r=+t)!=r?S():[,r]},wr=r=>t=>t===K||t>=R&&t<=U&&t-R<r||r===16&&(t>=gr&&t<=yr||t>=Er&&t<=Sr);f.number=null;c[$]=r=>!r&&p.charCodeAt(s+1)>=R&&p.charCodeAt(s+1)<=U&&B();for(let r=R;r<=U;r++)c[r]=t=>t?void 0:B();c[R]=r=>{if(r)return;let t=f.number;if(t){for(let[e,n]of Object.entries(t))if(e[0]==="0"&&p[s+1]?.toLowerCase()===e[1]){w(2);let o=Y(_(wr(n)));return p.charCodeAt(s)===V?(w(),[,BigInt("0"+e[1]+o)]):[,parseInt(o,n)]}}return B()};var _r=92,Z=34,q=39,x=117,j=120,Ir=123,Rr=125,a=10,Ur=13,Lr={n:` `,r:"\r",t:" ",b:"\b",f:"\f",v:"\v",0:"\0"},b=r=>r>=48&&r<=57?r-48:r>=65&&r<=70?r-55:r>=97&&r<=102?r-87:-1,rr=r=>(t,e,n="",o=String.fromCharCode(r))=>{if(t||!f.string?.[o])return;w();let m=()=>{let u=p.charCodeAt(s+1);if(u===a)return 2;if(u===Ur)return p.charCodeAt(s+2)===a?3:2;if(u===j||u===x&&p.charCodeAt(s+2)!==Ir){let d=u===j?2:4,A=0,C;for(let E=0;E<d;E++){if((C=b(p.charCodeAt(s+2+E)))<0)return n+=p[s+1],2;A=A*16+C}return n+=String.fromCharCode(A),2+d}if(u===x){let d=0,A=s+3,C;for(;(C=b(p.charCodeAt(A)))>=0;)d=d*16+C,A++;return A>s+3&&d<=1114111&&p.charCodeAt(A)===Rr?(n+=String.fromCodePoint(d),A-s+1):(n+=p[s+1],2)}return n+=Lr[p[s+1]]||p[s+1],2};return _(u=>u-r&&(u!==_r?(n+=p[s],1):m())),p[s]===o?w():S("Bad string"),[,n]};c[Z]=rr(Z);c[q]=rr(q);f.string={'"':!0};var Pr=20;"= += -= *= /= %= |= &= ^= >>= <<=".split(" ").map(r=>h(r,Pr,!0));var Tr=30,kr=40,Dr=140;I("!",Dr);h("||",Tr);h("&&",kr);var Mr=50,Nr=60,Fr=70,tr=100,$r=140;h("|",Mr);h("&",Fr);h("^",Nr);h(">>",tr);h("<<",tr);I("~",$r);var P=90;h("<",P);h(">",P);h("<=",P);h(">=",P);var er=80;h("==",er);h("!=",er);var or=110,X=120,nr=140;h("+",or);h("-",or);h("*",X);h("/",X);h("%",X);I("+",nr);I("-",nr);var T=150;y("++",T,r=>r?["++",r,null]:["++",g(T-1)]);y("--",T,r=>r?["--",r,null]:["--",g(T-1)]);var Br=5,Xr=10;N(",",Xr);N(";",Br,!0,!0);var Or=170;G("()",Or);var ir=170,Qr=160;F("[]",ir);W(".",ir);F("()",Qr);var pr=(r,t,e,n)=>typeof r=="string"?o=>t(o,r,o):r[0]==="."?(e=i(r[1]),n=r[2],o=>t(e(o),n,o)):r[0]==="[]"&&r.length===3?(e=i(r[1]),n=i(r[2]),o=>t(e(o),n(o),o)):r[0]==="()"&&r.length===2?pr(r[1],t):(()=>{throw Error("Invalid assignment target")})(),sr={"=":(r,t,e)=>r[t]=e,"+=":(r,t,e)=>r[t]+=e,"-=":(r,t,e)=>r[t]-=e,"*=":(r,t,e)=>r[t]*=e,"/=":(r,t,e)=>r[t]/=e,"%=":(r,t,e)=>r[t]%=e,"|=":(r,t,e)=>r[t]|=e,"&=":(r,t,e)=>r[t]&=e,"^=":(r,t,e)=>r[t]^=e,">>=":(r,t,e)=>r[t]>>=e,"<<=":(r,t,e)=>r[t]<<=e};for(let r in sr)l(r,(t,e)=>(e=i(e),pr(t,(n,o,m)=>sr[r](n,o,e(m)))));l("!",r=>(r=i(r),t=>!r(t)));l("||",(r,t)=>(r=i(r),t=i(t),e=>r(e)||t(e)));l("&&",(r,t)=>(r=i(r),t=i(t),e=>r(e)&&t(e)));l("~",r=>(r=i(r),t=>~r(t)));l("|",(r,t)=>(r=i(r),t=i(t),e=>r(e)|t(e)));l("&",(r,t)=>(r=i(r),t=i(t),e=>r(e)&t(e)));l("^",(r,t)=>(r=i(r),t=i(t),e=>r(e)^t(e)));l(">>",(r,t)=>(r=i(r),t=i(t),e=>r(e)>>t(e)));l("<<",(r,t)=>(r=i(r),t=i(t),e=>r(e)<<t(e)));l(">",(r,t)=>(r=i(r),t=i(t),e=>r(e)>t(e)));l("<",(r,t)=>(r=i(r),t=i(t),e=>r(e)<t(e)));l(">=",(r,t)=>(r=i(r),t=i(t),e=>r(e)>=t(e)));l("<=",(r,t)=>(r=i(r),t=i(t),e=>r(e)<=t(e)));l("==",(r,t)=>(r=i(r),t=i(t),e=>r(e)==t(e)));l("!=",(r,t)=>(r=i(r),t=i(t),e=>r(e)!=t(e)));l("+",(r,t)=>t!==void 0?(r=i(r),t=i(t),e=>r(e)+t(e)):(r=i(r),e=>+r(e)));l("-",(r,t)=>t!==void 0?(r=i(r),t=i(t),e=>r(e)-t(e)):(r=i(r),e=>-r(e)));l("*",(r,t)=>(r=i(r),t=i(t),e=>r(e)*t(e)));l("/",(r,t)=>(r=i(r),t=i(t),e=>r(e)/t(e)));l("%",(r,t)=>(r=i(r),t=i(t),e=>r(e)%t(e)));var O=(r,t,e,n)=>typeof r=="string"?o=>t(o,r):r[0]==="."?(e=i(r[1]),n=r[2],o=>t(e(o),n)):r[0]==="[]"&&r.length===3?(e=i(r[1]),n=i(r[2]),o=>t(e(o),n(o))):r[0]==="()"&&r.length===2?O(r[1],t):(()=>{throw Error("Invalid increment target")})();l("++",(r,t)=>O(r,t===null?(e,n)=>e[n]++:(e,n)=>++e[n]));l("--",(r,t)=>O(r,t===null?(e,n)=>e[n]--:(e,n)=>--e[n]));var mr=(...r)=>(r=r.map(i),t=>{let e;for(let n of r)e=n(t);return e});l(",",mr);l(";",mr);var lr=r=>r?.[0]==="_"&&r[1]==="_"||r==="constructor"||r==="prototype",k=r=>{throw Error(r)};l("[]",(r,t)=>t===void 0?(r=r?r[0]===","?r.slice(1):[r]:[],r=r.map(e=>e==null?(()=>{}):e[0]==="..."?(e=i(e[1]),n=>e(n)):(e=i(e),n=>[e(n)])),e=>r.flatMap(n=>n(e))):(t==null&&k("Missing index"),r=i(r),t=i(t),e=>{let n=t(e);return lr(n)?void 0:r(e)[n]}));l(".",(r,t)=>(r=i(r),t=t[0]?t:t[1],lr(t)?()=>{}:e=>r(e)[t]));l("()",(r,t)=>{if(t===void 0)return r==null?k("Empty ()"):i(r);let e=o=>o?.[0]===","&&o.slice(1).some(m=>m==null||e(m));e(t)&&k("Empty argument");let n=t?t[0]===","?(t=t.slice(1).map(i),o=>t.map(m=>m(o))):(t=i(t),o=>[t(o)]):()=>[];return ur(r,(o,m,u)=>o[m](...n(u)))});var ur=(r,t,e,n)=>r==null?k("Empty ()"):r[0]==="()"&&r.length==2?ur(r[1],t):typeof r=="string"?o=>t(o,r,o):r[0]==="."?(e=i(r[1]),n=r[2],o=>t(e(o),n,o)):r[0]==="?."?(e=i(r[1]),n=r[2],o=>{let m=e(o);return m==null?void 0:t(m,n,o)}):r[0]==="[]"&&r.length===3?(e=i(r[1]),n=i(r[2]),o=>t(e(o),n(o),o)):r[0]==="?.[]"?(e=i(r[1]),n=i(r[2]),o=>{let m=e(o);return m==null?void 0:t(m,n(o),o)}):(r=i(r),o=>t([r(o)],0,o));var fr=new WeakMap,Hr=(r,...t)=>typeof r=="string"?i(f(r)):fr.get(r)||fr.set(r,vr(r,t)).get(r),dr=57344,vr=(r,t)=>{let e=r.reduce((m,u,d)=>m+(d?String.fromCharCode(dr+d-1):"")+u,""),n=f(e),o=m=>{if(typeof m=="string"&&m.length===1){let u=m.charCodeAt(0)-dr,d;if(u>=0&&u<t.length)return d=t[u],Gr(d)?d:[,d]}return Array.isArray(m)?m.map(o):m};return i(o(n))},Gr=r=>typeof r=="string"||Array.isArray(r)&&(typeof r[0]=="string"||r[0]===void 0),zt=Hr;export{F as access,h as binary,i as compile,p as cur,zt as default,S as err,g as expr,G as group,Wr as id,s as idx,Vr as keyword,Kr as literal,v as loc,c as lookup,W as member,N as nary,_ as next,l as operator,M as operators,Jr as parens,f as parse,hr as peek,D as prec,Ar as propName,H as seek,w as skip,y as token,I as unary,zr as word};