-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
3 lines (3 loc) · 12 KB
/
index.js
1
2
3
(()=>{var p=class{constructor(e,t,r){this.type=e;this.lexeme=t;this.literal=r}toString(){return`Token(${this.type}, ${this.lexeme}, ${this.literal})`}},d=class{constructor(e){this.source=e}scanTokens(){for(this.tokens=[],this.start=0,this.current=0;!this.isAtEnd();)this.start=this.current,this.scanToken();return this.tokens.push(new p(36,"",null)),this.tokens}scanToken(){let e=this.advance();switch(e){case"(":this.addToken(0);break;case")":this.addToken(1);break;case"{":this.addToken(8);break;case"}":this.addToken(9);break;case"[":this.addToken(11);break;case"]":this.addToken(12);break;case",":this.addToken(2);break;case".":this.addToken(3);break;case"+":this.addToken(5);break;case"*":this.addToken(7);break;case"-":this.addToken(4);break;case"%":this.addToken(13);break;case"$":this.addToken(this.match("1")?23:22);break;case":":this.addToken(this.match(":")?28:10);break;case"=":this.addToken(this.match("~")?25:24);break;case"?":this.addToken(this.match("?")?21:20);break;case"!":this.addToken(this.match("=")?15:14);break;case">":this.addToken(this.match("=")?17:16);break;case"<":this.addToken(this.match("=")?19:18);break;case"|":this.match("|")?this.addToken(26):this.error("Invalid token");break;case"&":this.match("&")?this.addToken(27):this.error("Invalid token");break;case'"':this.string('"');break;case"'":this.string("'");break;case"/":let t=this.tokens[this.tokens.length-1];t&&(t.type==29||t.type==31||t.type==30||t.type==12||t.type==1||t.type==34||t.type==35||t.type==33?this.addToken(6):this.regex());break;case" ":case" ":break;case"\r":case`
`:this.error("New line not allowed");break;case"\0":break;default:this.isDigit(e)?this.number():this.isAlpha(e)?this.identifier():this.error(`Unexpected character ${e}`)}}addToken(e,t){let r=this.source.substring(this.start,this.current);this.tokens.push(new p(e,r,t))}isAtEnd(){return this.current>=this.source.length}advance(){return this.current++,this.source.charAt(this.current-1)}match(e){return this.isAtEnd()||this.source.charAt(this.current)!=e?!1:(this.current++,!0)}peek(){return this.isAtEnd()?"\0":this.source.charAt(this.current)}peekNext(){return this.current+1>=this.source.length?"\0":this.source.charAt(this.current+1)}string(e){let t="",r="";for(;!this.isAtEnd();){let s=this.peek();if(r=="\\")switch(s){case"n":t+=`
`;break;case"r":t+="\r";break;case"t":t+=" ";break;case"\\":t+="\\";break;case e:t+=e;break;default:this.error(`Invalid escape sequence \\${s}`)}else{if(s===e)break;t+=s}r=s,this.advance()}this.isAtEnd()&&this.error("Unterminated string"),this.advance(),this.addToken(30,t)}number(){for(;this.isDigit(this.peek());)this.advance();if(this.peek()=="."&&this.isDigit(this.peekNext()))for(this.advance();this.isDigit(this.peek());)this.advance();this.addToken(31,Number(this.source.substring(this.start,this.current)))}regex(){for(;;){let r=this.peek();if(this.isAtEnd())return this.error("Unterminated regex");if(r=="\\"&&this.peekNext()=="/")this.advance();else if(r=="/"){this.advance();break}this.advance()}let e=this.source.substring(this.start+1,this.current-1),t=[];for(;;){let r=this.peek();if(r=="i"||r=="g"||r=="s"||r=="m"||r=="u"||r=="y")t.push(this.advance());else break}this.addToken(32,new RegExp(e,t.join("")))}identifier(){for(;this.isAlphaNumeric(this.peek());)this.advance();let e=this.source.substring(this.start,this.current),t=29;e=="null"?t=33:e=="true"?t=34:e=="false"&&(t=35),this.addToken(t)}isDigit(e){return"0"<=e&&e<="9"}isAlpha(e){return"a"<=e&&e<="z"||"A"<=e&&e<="Z"||e=="_"}isAlphaNumeric(e){return this.isAlpha(e)||this.isDigit(e)}error(e){throw new Error(`[Syntax error] ${e}`)}},a=class{accept(e){return e.visit(this)}},E=class extends a{constructor(e,t){super();this.operator=e;this.right=t}accept(e){return e.visitUnary(this)}},c=class extends a{constructor(e,t,r){super();this.left=e;this.operator=t;this.right=r}accept(e){return e.visitBinary(this)}},y=class extends a{constructor(e){super();this.value=e}accept(e){return e.visitIdentifier(this)}},k=class extends a{constructor(e){super();this.properties=e}accept(e){return e.visitObjectExpression(this)}},A=class extends a{constructor(e,t){super();this.key=e;this.value=t}accept(e){return e.visitProperty(this)}},v=class extends a{constructor(e){super();this.elements=e}accept(e){return e.visitArrayExpression(this)}},o=class extends a{constructor(e){super();this.value=e}accept(e){return e.visitLiteral(this)}},f=class extends a{constructor(e,t,r){super();this.condition=e;this.ifTrue=t;this.ifFalse=r}accept(e){return e.visitTernary(this)}},m=class extends a{constructor(e,t){super();this.left=e;this.right=t}accept(e){return e.visitNullishCoalescing(this)}},b=class extends a{constructor(e,t){super();this.object=e;this.property=t}accept(e){return e.visitMemberAccess(this)}},x=class extends a{constructor(e,t){super();this.object=e;this.property=t}accept(e){return e.visitComputedMemberAccess(this)}},R=class extends a{constructor(e,t,r){super();this.left=e;this.operator=t;this.right=r}accept(e){return e.visitArrayOperation(this)}},N=class extends a{constructor(e,t,r){super();this.left=e;this.body=t;this.initialValue=r}accept(e){return e.visitArrayReducer(this)}},l=class extends a{constructor(e=0){super();this.index=e}accept(e){return e.visitCurrent(this)}},S=class{constructor(e){this.tokens=e,this.current=0}consume(e,t){if(this.check(e))return this.advance();this.error(t)}match(...e){return this.check(...e)?this.advance():!1}check(...e){return this.isAtEnd()?!1:e.some(t=>t===this.tokens[this.current].type)}advance(){return this.isAtEnd()||this.current++,this.previous()}isAtEnd(){return this.peek().type==36}peek(){return this.tokens[this.current]}peekNext(){return this.isAtEnd()?null:this.tokens[this.current+1]}previous(){return this.tokens[this.current-1]}error(e){throw new Error(e)}},g=class extends S{expression(){return this.ternary()}ternary(){let e=this.nullishCoalescing();if(this.match(20)){let t=this.ternary();this.consume(10,"Expected : after ternary");let r=this.ternary();return new f(e,t,r)}return e}nullishCoalescing(){let e=this.logicalOr();for(;this.match(21);){let t=this.nullishCoalescing();return new m(e,t)}return e}logicalOr(){let e=this.logicalAnd();for(;this.match(26);){let t=this.previous(),r=this.logicalAnd();e=new c(e,t,r)}return e}logicalAnd(){let e=this.equality();for(;this.match(27);){let t=this.previous(),r=this.equality();e=new c(e,t,r)}return e}equality(){let e=this.comparison();for(;this.match(15,24,25);){let t=this.previous(),r=this.comparison();e=new c(e,t,r)}return e}comparison(){let e=this.addition();for(;this.match(16,17,18,19);){let t=this.previous(),r=this.addition();e=new c(e,t,r)}return e}addition(){let e=this.multiplication();for(;this.match(4,5);){let t=this.previous(),r=this.multiplication();e=new c(e,t,r)}return e}multiplication(){let e=this.unary();for(;this.match(6,7,13);){let t=this.previous(),r=this.unary();e=new c(e,t,r)}return e}unary(){if(this.match(14,4)){let e=this.previous(),t=this.unary();return new E(e,t)}return this.memberAccess()}memberAccess(){let e=this.arrayOperation();for(;this.match(3,11);)if(this.previous().type==3){let t=this.consume(29,"Expected property name");e=new b(e,t.lexeme)}else{let t=this.expression();this.consume(12,"Expected ] after property access"),e=new x(e,t)}return e}arrayOperation(){let e=this.primary();for(;this.match(28);){let t=this.consume(29,"Expected array operator after ::");if(["any","all","count","filter","map","reduce"].indexOf(t.lexeme)!==-1){this.consume(0,"Expected ( after array operator type");let r=this.expression();if(t.lexeme==="reduce"){let s;this.match(2)&&(s=this.expression()),e=new N(e,r,s)}else e=new R(e,t.lexeme,r);this.consume(1,"Expected ) after array operator body")}else this.error(`Unknown array operator "${t.lexeme}"`)}return e}primary(){if(this.match(35))return new o(!1);if(this.match(34))return new o(!0);if(this.match(33))return new o(null);if(this.match(31))return new o(this.previous().literal);if(this.match(30))return new o(this.previous().literal);if(this.match(32))return new o(this.previous().literal);if(this.match(22))return new l;if(this.match(23))return new l(1);if(this.match(0)){let e=this.expression();return this.consume(1,"Expect ) after expression"),e}return this.match(11)?this.array():this.match(8)?this.object():this.match(29)?new y(this.previous().lexeme):new o(!0)}array(){let e=[];if(!this.check(12))for(;!this.check(12)&&(e.push(this.expression()),!!this.match(2)););return this.consume(12,"Expect ] after array"),new v(e)}object(){let e=[];if(!this.check(9))for(;!this.check(9);){let t;this.match(29)?t=this.previous().lexeme:this.match(30)?t=this.previous().literal:this.error("Expect property name"),this.consume(10,"Expect : after property name");let r=this.expression();if(e.push(new A(t,r)),!this.match(2))break}return this.consume(9,"Expect } after object"),new k(e)}},w=class{constructor(e){this.lookup=e;this.currentStack=[],this.previousStack=[]}visit(e){return e.accept(this)}visitCurrent(e){return e.index===0?this.currentStack[this.currentStack.length-1]:this.previousStack[this.previousStack.length-1]}visitArrayOperation(e){let t=this.visit(e.left);if(Array.isArray(t)){let r={any:"some",all:"every"};switch(e.operator){case"any":case"filter":case"map":case"all":return t[r[e.operator]??e.operator](s=>{this.currentStack.push(s);let n=this.visit(e.right);return this.currentStack.pop(),n});case"count":return e.right.value===!0?t.length:t.reduce((s,n)=>{this.currentStack.push(n);let h=this.visit(e.right);return this.currentStack.pop(),h?s+1:s},0)}}else return console.info("Array operation on non-array",e,"returning null"),null}visitArrayReducer(e){let t=this.visit(e.left);if(Array.isArray(t)){let r,s=0;e.initialValue!==void 0?r=this.visit(e.initialValue):(r=t[0],s=1);for(let n=s;n<t.length;n++)this.currentStack.push(t[n]),this.previousStack.push(r),r=this.visit(e.body),this.previousStack.pop(),this.currentStack.pop();return r}else return console.info("Array reducer on non-array",e,"returning null"),null}visitBinary(e){let t=this.visit(e.left),r=this.visit(e.right);switch(e.operator.type){case 5:return t+r;case 4:return t-r;case 7:return t*r;case 6:return t/r;case 16:return t>r;case 17:return t>=r;case 18:return t<r;case 19:return t<=r;case 15:return t!=r;case 24:return t==r;case 27:return t&&r;case 26:return t||r;case 13:return t%r;case 25:return this.search(t,r);default:throw new Error("Unknown operator: "+e.operator.lexeme)}}search(e,t){return typeof e=="string"&&typeof t=="string"?e.indexOf(t)!==-1:typeof e=="number"&&typeof t=="number"?e%t==0:typeof e=="string"&&t instanceof RegExp?t.test(e):Array.isArray(e)?Array.isArray(t)?t.every(r=>e.indexOf(r)!==-1):e.indexOf(t)!==-1:Array.isArray(t)?t.indexOf(e)!==-1:!1}visitArrayExpression(e){return e.elements.map(t=>this.visit(t))}visitLiteral(e){return e.value}visitIdentifier(e){return this.lookup(e.value)}visitNullishCoalescing(e){let t=this.visit(e.left);return t||this.visit(e.right)}visitObjectExpression(e){let t={};return e.properties.forEach(r=>{t[r.key]=this.visit(r.value)}),t}visitProperty(e){return this.visit(e.value)}visitUnary(e){let t=this.visit(e.right);switch(e.operator.type){case 4:return-t;case 14:return!t;default:throw new Error("Unknown operator: "+e.operator.lexeme)}}visitTernary(e){return this.visit(e.condition)?this.visit(e.ifTrue):this.visit(e.ifFalse)}visitComputedMemberAccess(e){let t=this.visit(e.object),r=this.visit(e.property);if(t!==void 0)return t[r]}visitMemberAccess(e){let t=this.visit(e.object),r=e.property;if(t!==void 0)return t[r]}};function T(i,e){let r=new d(e).scanTokens(),n=new g(r).expression();return new w(i).visit(n)}panel.plugin("rasteiner/whenquery",{use:[function(i){let e=i.observable({});function t(s){let n=i.component(s).options;i.component(s,{extends:n,created(){e.model=this.model},watch:{model:function(h){e.model=h}}})}t("k-page-view"),t("k-site-view"),t("k-file-view"),t("k-user-view"),t("k-account-view");let r=i.prototype.$helper.field.isVisible;i.prototype.$helper.field.isVisible=function(s,n){return r(s,n)?s.whenQuery?T(u=>u[0]==="_"?e?.model?.[u.substr(1)]:n?.[u.toLowerCase()]!==void 0?n[u.toLowerCase()]:panel?.app?.$store?.getters["content/values"]()[u.toLowerCase()],s.whenQuery):!0:!1}}]});})();