UNPKG

hyperscript.org

Version:

a small scripting language for the web

12 lines (11 loc) 13.1 kB
(()=>{var L=class{#i;#s=[];#f=null;#a=[];source;constructor(t,e){this.#i=t,this.source=e,this.consumeWhitespace()}get list(){return this.#i}get consumed(){return this.#s}toString(){var t=this.currentToken(),e=this.source.split(` `),s=t?.line?t.line-1:e.length-1,i=t?.line?t.column:0,v=e[s]||"",I=Math.max(1,t?.value?.length||1),_=String(s+1).length,m="Tokens(";return m+=this.#s.filter(A=>A.type!=="WHITESPACE").length+" consumed, ",m+=this.#i.filter(A=>A.type!=="WHITESPACE").length+" remaining",m+=", line "+(s+1)+`) `,m+=" "+String(s+1).padStart(_)+" | "+v+` `,m+=" ".repeat(_+5)+" ".repeat(i)+"^".repeat(I),t&&(m+=" "+t.type+" '"+t.value+"'"),m}currentToken(){return this.token(0)}token(t,e){var s,i=0;do{if(!e)for(;this.#i[i]&&this.#i[i].type==="WHITESPACE";)i++;s=this.#i[i],t--,i++}while(t>-1);return s||{type:"EOF",value:"<<<EOF>>>"}}hasMore(){return this.#i.length>0}lastMatch(){return this.#f}matchToken(t,e){if(!this.#a.includes(t)&&(e=e||"IDENTIFIER",this.currentToken()&&this.currentToken().value===t&&this.currentToken().type===e))return this.consumeToken()}matchOpToken(t){if(this.currentToken()&&this.currentToken().op&&this.currentToken().value===t)return this.consumeToken()}matchTokenType(...t){if(this.currentToken()&&this.currentToken().type&&t.includes(this.currentToken().type))return this.consumeToken()}matchAnyToken(...t){for(var e=0;e<t.length;e++){var s=this.matchToken(t[e]);if(s)return s}}matchAnyOpToken(...t){for(var e=0;e<t.length;e++){var s=this.matchOpToken(t[e]);if(s)return s}}consumeToken(){var t=this.#i.shift();return this.#s.push(t),this.#f=t,this.consumeWhitespace(),t}consumeWhitespace(){for(;this.token(0,!0).type==="WHITESPACE";)this.#s.push(this.#i.shift())}consumeUntil(t,e){for(var s=[],i=this.token(0,!0);(e==null||i.type!==e)&&(t==null||i.value!==t)&&i.type!=="EOF";){var v=this.#i.shift();this.#s.push(v),s.push(i),i=this.token(0,!0)}return this.consumeWhitespace(),s}consumeUntilWhitespace(){return this.consumeUntil(null,"WHITESPACE")}peekToken(t,e,s){e=e||0,s=s||"IDENTIFIER";let i=0;for(;e>0;)i++,this.#i[i]?.type!=="WHITESPACE"&&e--;if(this.#i[i]&&this.#i[i].value===t&&this.#i[i].type===s)return this.#i[i]}lastWhitespace(){var t=this.#s.at(-1);return t&&t.type==="WHITESPACE"?t.value:""}pushFollow(t){this.#a.push(t)}popFollow(){this.#a.pop()}pushFollows(...t){for(var e=0;e<t.length;e++)this.#a.push(t[e]);return t.length}popFollows(t){for(var e=0;e<t;e++)this.#a.pop()}clearFollows(){var t=this.#a;return this.#a=[],t}restoreFollows(t){this.#a=t}},y={"+":"PLUS","-":"MINUS","*":"MULTIPLY","/":"DIVIDE",".":"PERIOD","..":"ELLIPSIS","\\":"BACKSLASH",":":"COLON","%":"PERCENT","|":"PIPE","!":"EXCLAMATION","?":"QUESTION","#":"POUND","&":"AMPERSAND",$:"DOLLAR",";":"SEMI",",":"COMMA","(":"L_PAREN",")":"R_PAREN","<":"L_ANG",">":"R_ANG","<=":"LTE_ANG",">=":"GTE_ANG","==":"EQ","===":"EQQ","!=":"NEQ","!==":"NEQQ","{":"L_BRACE","}":"R_BRACE","[":"L_BRACKET","]":"R_BRACKET","=":"EQUALS","~":"TILDE","^":"CARET"},S=class E{#i="";#s=0;#f=0;#a=1;#n="<START>";#m=0;#h=[];#p=!1;#c;#u(t){return t>="a"&&t<="z"||t>="A"&&t<="Z"}#l(t){return t>="0"&&t<="9"}#v(t){return t===" "||t===" "||t==="\r"||t===` `}#T(t){return t==="\r"||t===` `}#E(t){return this.#u(t)||this.#l(t)||t==="-"||t==="_"||t===":"}#d(t){return t==="_"||t==="$"}#w(t){return t==="`"}static tokenize(t,e){return new E().tokenize(t,e)}tokenize(t,e){return this.#i=t,this.#s=0,this.#f=0,this.#a=1,this.#n="<START>",this.#m=0,this.#h=[],this.#p=e||!1,this.#c="indeterminant",this.#B()}#t(){return this.#i.charAt(this.#s)}#o(){return this.#i.charAt(this.#s+1)}#S(t=1){return this.#i.charAt(this.#s+t)}#e(){return this.#n=this.#t(),this.#s++,this.#n===` `?(this.#a++,this.#f=0):this.#f++,this.#n}#k(){return this.#p&&this.#m===0}#_(){return!this.#k()||this.#c==="command"}#C(){return this.#u(this.#n)||this.#l(this.#n)||this.#n===")"||this.#n==='"'||this.#n==="'"||this.#n==="`"||this.#n==="}"||this.#n==="]"}#R(){if(this.#h.length>0){var t=this.#h.at(-1);if(t.type==="IDENTIFIER"||t.type==="CLASS_REF"||t.type==="ID_REF"||t.op&&(t.value===">"||t.value===")"))return!1}return!0}#r(t,e){return{type:t,value:e||"",start:this.#s,end:this.#s+1,column:this.#f,line:this.#a}}#y(t,e){var s=this.#r(t,e);return s.op=!0,s}#L(){for(;this.#t()&&!this.#T(this.#t());)this.#e();this.#e()}#N(){for(var t=this.#r("WHITESPACE"),e="";this.#t()&&this.#v(this.#t());)this.#T(this.#t())&&(this.#c="indeterminant"),e+=this.#e();return t.value=e,t.end=this.#s,t}#x(){var t=this.#r("CLASS_REF"),e=this.#e();if(this.#t()==="{"){for(t.template=!0,e+=this.#e();this.#t()&&this.#t()!=="}";)e+=this.#e();if(this.#t()!=="}")throw new Error("Unterminated class reference");e+=this.#e()}else for(;this.#E(this.#t())||this.#t()==="\\";)this.#t()==="\\"&&this.#e(),e+=this.#e();return t.value=e,t.end=this.#s,t}#b(){var t=this.#r("ID_REF"),e=this.#e();if(this.#t()==="{"){for(t.template=!0,e+=this.#e();this.#t()&&this.#t()!=="}";)e+=this.#e();if(this.#t()!=="}")throw new Error("Unterminated id reference");this.#e()}else for(;this.#E(this.#t());)e+=this.#e();return t.value=e,t.end=this.#s,t}#P(){for(var t=this.#r("ATTRIBUTE_REF"),e=this.#e();this.#s<this.#i.length&&this.#t()!=="]";)e+=this.#e();return this.#t()==="]"&&(e+=this.#e()),t.value=e,t.end=this.#s,t}#M(){for(var t=this.#r("ATTRIBUTE_REF"),e=this.#e();this.#E(this.#t());)e+=this.#e();return this.#t()==="="&&(e+=this.#e(),this.#t()==='"'||this.#t()==="'"?e+=this.#A().value:(this.#u(this.#t())||this.#l(this.#t())||this.#d(this.#t()))&&(e+=this.#I().value)),t.value=e,t.end=this.#s,t}#F(){for(var t=this.#r("STYLE_REF"),e=this.#e();this.#u(this.#t())||this.#t()==="-";)e+=this.#e();return t.value=e,t.end=this.#s,t}#D(){var t=this.#r("IDENTIFIER");this.#e();for(var e="";this.#u(this.#t());)e+=this.#e();return t.value=e,t.end=this.#s,t}#H(){var t=this.#r("TEMPLATE_LINE");t.value="TEMPLATE_LINE";for(var e="";this.#t()&&!this.#T(this.#t());)e+=this.#e();return this.#t()&&this.#T(this.#t())&&(this.#e(),e+=` `,this.#c="indeterminant"),t.content=e,t.end=this.#s,t}#W(){var t=this.#r("IDENTIFIER"),e=this.#e(),s=e==="\\";for(s&&(e="");(this.#u(this.#t())||this.#l(this.#t())||this.#d(this.#t())||this.#t()==="\\"||this.#t()==="{"||this.#t()==="}")&&!(this.#t()==="$"&&!s);)this.#t()==="\\"?(s=!0,this.#e()):(s=!1,e+=this.#e());return this.#t()==="!"&&e==="beep"&&(e+=this.#e()),t.value=e,t.end=this.#s,t}#I(){for(var t=this.#r("IDENTIFIER"),e=this.#e();this.#u(this.#t())||this.#l(this.#t())||this.#d(this.#t());)e+=this.#e();return this.#t()==="!"&&e==="beep"&&(e+=this.#e()),t.value=e,t.end=this.#s,t}#O(){for(var t=this.#r("NUMBER"),e=this.#e();this.#l(this.#t());)e+=this.#e();for(this.#t()==="."&&this.#l(this.#o())&&(e+=this.#e());this.#l(this.#t());)e+=this.#e();for((this.#t()==="e"||this.#t()==="E")&&(this.#l(this.#o())?e+=this.#e():this.#o()==="-"&&(e+=this.#e(),e+=this.#e()));this.#l(this.#t());)e+=this.#e();return t.value=e,t.end=this.#s,t}#g(){for(var t=this.#y(),e=this.#e();this.#t()&&y[e+this.#t()];)e+=this.#e();return t.type=y[e],t.value=e,t.end=this.#s,t}#A(){var t=this.#r("STRING"),e=this.#e();t.template=e==="`";for(var s="";this.#t()&&this.#t()!==e;)if(this.#t()==="\\"){this.#e();let i=this.#e();if(i==="b")s+="\b";else if(i==="f")s+="\f";else if(i==="n")s+=` `;else if(i==="r")s+="\r";else if(i==="t")s+=" ";else if(i==="v")s+="\v";else if(t.template&&i==="$")s+="\\$";else if(i==="x"){let v=this.#U();if(Number.isNaN(v))throw new Error("Invalid hexadecimal escape at [Line: "+t.line+", Column: "+t.column+"]");s+=String.fromCharCode(v)}else s+=i}else s+=this.#e();if(this.#t()!==e)throw new Error("Unterminated string at [Line: "+t.line+", Column: "+t.column+"]");return this.#e(),t.value=s,t.end=this.#s,t}#U(){if(!this.#t())return NaN;let t=16*Number.parseInt(this.#e(),16);return this.#t()?(t+=Number.parseInt(this.#e(),16),t):NaN}#z(){var t=this.#t(),e=this.#o(),s=this.#S(2);return t==="-"&&e==="-"&&(this.#v(s)||s===""||s==="-")||t==="/"&&e==="/"&&(this.#v(s)||s===""||s==="/")}#B(){for(;this.#s<this.#i.length;)if(this.#z())this.#L();else if(this.#v(this.#t()))this.#h.push(this.#N());else if(!this.#C()&&this.#t()==="."&&(this.#u(this.#o())||this.#o()==="{"||this.#o()==="-"))this.#h.push(this.#x());else if(!this.#C()&&this.#t()==="#"&&(this.#u(this.#o())||this.#o()==="{"))this.#p==="lines"&&this.#c==="indeterminant"?(this.#c="command",this.#h.push(this.#D())):this.#h.push(this.#b());else if(this.#p==="lines"&&this.#c==="indeterminant"&&this.#m===0)this.#c="template",this.#h.push(this.#H());else if(this.#t()==="["&&this.#o()==="@")this.#h.push(this.#P());else if(this.#t()==="@")this.#h.push(this.#M());else if(this.#t()==="*"&&this.#u(this.#o()))this.#h.push(this.#F());else if(this.#k()&&(this.#u(this.#t())||this.#t()==="\\")&&this.#c!=="command")this.#h.push(this.#W());else if(this.#_()&&(this.#u(this.#t())||this.#d(this.#t())))this.#h.push(this.#I());else if(this.#l(this.#t()))this.#h.push(this.#O());else if(this.#_()&&(this.#t()==='"'||this.#t()==="`"))this.#h.push(this.#A());else if(this.#_()&&this.#t()==="'")this.#R()?this.#h.push(this.#A()):this.#h.push(this.#g());else if(y[this.#t()])this.#n==="$"&&this.#t()==="{"&&this.#m++,this.#t()==="}"&&this.#m--,this.#h.push(this.#g());else if(this.#k()||this.#w(this.#t()))this.#h.push(this.#r("RESERVED",this.#e()));else if(this.#s<this.#i.length)throw new Error("Unknown token: "+this.#t()+" ");return new L(this.#h,this.#i)}};function P(E){let{runtime:t,createParser:e,reactivity:s}=E.internals,i=new S;function v(h,r,n){if(!r)return h;var l=document.createElement("div");l.innerHTML=r;var a={},T=[];for(var o of Array.from(l.childNodes)){o.nodeType===1&&n&&!o.hasAttribute("dom-scope")&&o.setAttribute("dom-scope","parent of "+n);var f=o.nodeType===1&&o.getAttribute("slot");f?(o.removeAttribute("slot"),a[f]||(a[f]=""),a[f]+=o.outerHTML):T.push(o.nodeType===1?o.outerHTML:o.nodeType===3?o.textContent:"")}var d=T.join(""),C=h.replace(/<slot\s+name\s*=\s*["']([^"']+)["']\s*\/?\s*>(\s*<\/slot>)?/g,function(c,u){return a[u]||""});return C=C.replace(/<slot\s*\/?\s*>(\s*<\/slot>)?/g,d),C}function I(h,r){if(typeof r!="string")return null;var n=h._attrsCache||(h._attrsCache={});if(!n[r]){var l=h.getAttribute(r);if(l==null)return null;try{n[r]=e(i.tokenize(l)).requireElement("expression")}catch(a){return console.error("component: failed to parse attrs."+r+":",a.message),null}}return n[r]}function _(h){var r=h.parentElement;return r?t.makeContext(r,null,r,null):null}function m(h){return new Proxy({_hsSkipTracking:!0},{get:function(r,n){if(n==="_hsSkipTracking")return!0;if(!(typeof n!="string"||n.startsWith("_"))){var l=I(h,n);if(l){var a=_(h);return a?l.evaluate(a):void 0}}},set:function(r,n,l){var a=I(h,n);if(!a||!a.set)return!1;var T=_(h);if(!T)return!1;var o={};if(a.lhs)for(var f in a.lhs){var d=a.lhs[f];o[f]=d&&d.evaluate?d.evaluate(T):d}return a.set(T,o,l),!0}})}function A(h,r){let n=h.getAttribute("component");if(!n.includes("-")){console.error("component name must contain a dash: '"+n+"'");return}for(var l=h.textContent,a="",T=/<style[^>]*>([\s\S]*?)<\/style>/gi,o;(o=T.exec(l))!==null;)a+=o[1]+` `;if(a&&(l=l.replace(/<style[^>]*>[\s\S]*?<\/style>/gi,""),h.textContent=l),a){var f=document.createElement("style");f.textContent="@scope ("+n+`) { `+a+"}",h.insertAdjacentElement("afterend",f)}let d=h.textContent,C=class extends HTMLElement{connectedCallback(){if(this._hypercomp_initialized)return;this._hypercomp_initialized=!0,this.setAttribute("dom-scope","isolated"),this._slotContent=this.innerHTML,this.innerHTML="";var c=t.getInternalData(this);c.elementScope||(c.elementScope={}),c.elementScope.attrs=m(this),r&&(this.setAttribute("_",r),E.process(this));let u=this;var p=v(d,u._slotContent,n);queueMicrotask(function(){var k=u._renderTemplate(p);k&&k.then?k.then(function(g){u._stampTemplate(g),u._setupReactiveEffect(p)}):(u._stampTemplate(k),u._setupReactiveEffect(p))})}disconnectedCallback(){s.stopElementEffects(this),t.cleanup(this),this._hypercomp_initialized=!1,this._hypercomp_stamped=!1}_setupReactiveEffect(c){var u=this;s.createEffect(function(){return u._renderTemplate(c)},function(p){u._stampTemplate(p)},{element:u})}_renderTemplate(c){var u=t.makeContext(this,null,this,null),p=[];u.meta.__ht_template_result=p;var k=i.tokenize(c,"lines"),g=e(k),R;try{R=g.parseElement("commandList"),g.ensureTerminated(R)}catch(w){return console.error("hypercomp template parse error:",w.message||w),""}var x,b,M=new Promise(function(w,F){x=w,b=F});return R.execute(u),this.__hs_scopes=u.meta.__ht_scopes||null,u.meta.returned||!u.meta.resolve?p.join(""):(u.meta.resolve=x,u.meta.reject=b,M.then(function(){return p.join("")}))}_stampTemplate(c){this._hypercomp_stamped?t.morph(this,c):(this.innerHTML=c,E.process(this),this._hypercomp_stamped=!0)}};customElements.define(n,C)}var N=new Set;E.addBeforeProcessHook(function(h){!h||!h.querySelectorAll||h.querySelectorAll('script[type="text/hyperscript-template"][component]').forEach(function(r){var n=r.getAttribute("_")||"";r.removeAttribute("_");var l=r.getAttribute("component");!N.has(l)&&!customElements.get(l)&&(N.add(l),A(r,n))})})}typeof self<"u"&&self._hyperscript&&self._hyperscript.use(P);})();