hyperscript.org
Version:
a small scripting language for the web
12 lines (11 loc) • 14.5 kB
JavaScript
var x=class{#s;#i=[];#f=null;#a=[];source;constructor(t,e){this.#s=t,this.source=e,this.consumeWhitespace()}get list(){return this.#s}get consumed(){return this.#i}toString(){var t=this.currentToken(),e=this.source.split(`
`),i=t?.line?t.line-1:e.length-1,n=t?.line?t.column:0,d=e[i]||"",I=Math.max(1,t?.value?.length||1),_=String(i+1).length,p="Tokens(";return p+=this.#i.filter(k=>k.type!=="WHITESPACE").length+" consumed, ",p+=this.#s.filter(k=>k.type!=="WHITESPACE").length+" remaining",p+=", line "+(i+1)+`)
`,p+=" "+String(i+1).padStart(_)+" | "+d+`
`,p+=" ".repeat(_+5)+" ".repeat(n)+"^".repeat(I),t&&(p+=" "+t.type+" '"+t.value+"'"),p}currentToken(){return this.token(0)}token(t,e){var i,n=0;do{if(!e)for(;this.#s[n]&&this.#s[n].type==="WHITESPACE";)n++;i=this.#s[n],t--,n++}while(t>-1);return i||{type:"EOF",value:"<<<EOF>>>"}}hasMore(){return this.#s.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 i=this.matchToken(t[e]);if(i)return i}}matchAnyOpToken(...t){for(var e=0;e<t.length;e++){var i=this.matchOpToken(t[e]);if(i)return i}}consumeToken(){var t=this.#s.shift();return this.#i.push(t),this.#f=t,this.consumeWhitespace(),t}consumeWhitespace(){for(;this.token(0,!0).type==="WHITESPACE";)this.#i.push(this.#s.shift())}consumeUntil(t,e){for(var i=[],n=this.token(0,!0);(e==null||n.type!==e)&&(t==null||n.value!==t)&&n.type!=="EOF";){var d=this.#s.shift();this.#i.push(d),i.push(n),n=this.token(0,!0)}return this.consumeWhitespace(),i}consumeUntilWhitespace(){return this.consumeUntil(null,"WHITESPACE")}peekToken(t,e,i){e=e||0,i=i||"IDENTIFIER";let n=0;for(;e>0;)n++,this.#s[n]?.type!=="WHITESPACE"&&e--;if(this.#s[n]&&this.#s[n].value===t&&this.#s[n].type===i)return this.#s[n]}lastWhitespace(){var t=this.#i.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}},L={"+":"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"},w=class v{#s="";#i=0;#f=0;#a=1;#h="<START>";#p=0;#r=[];#m=!1;#c;#o(t){return t>="a"&&t<="z"||t>="A"&&t<="Z"}#u(t){return t>="0"&&t<="9"}#v(t){return t===" "||t===" "||t==="\r"||t===`
`}#d(t){return t==="\r"||t===`
`}#E(t){return this.#o(t)||this.#u(t)||t==="-"||t==="_"||t===":"}#T(t){return t==="_"||t==="$"}#S(t){return t==="`"}static tokenize(t,e){return new v().tokenize(t,e)}tokenize(t,e){return this.#s=t,this.#i=0,this.#f=0,this.#a=1,this.#h="<START>",this.#p=0,this.#r=[],this.#m=e||!1,this.#c="indeterminant",this.#z()}#t(){return this.#s.charAt(this.#i)}#l(){return this.#s.charAt(this.#i+1)}#w(t=1){return this.#s.charAt(this.#i+t)}#e(){return this.#h=this.#t(),this.#i++,this.#h===`
`?(this.#a++,this.#f=0):this.#f++,this.#h}#A(){return this.#m&&this.#p===0}#_(){return!this.#A()||this.#c==="command"}#y(){return this.#o(this.#h)||this.#u(this.#h)||this.#h===")"||this.#h==='"'||this.#h==="'"||this.#h==="`"||this.#h==="}"||this.#h==="]"}#I(){if(this.#r.length>0){var t=this.#r.at(-1);if(t.type==="IDENTIFIER"||t.type==="CLASS_REF"||t.type==="ID_REF"||t.op&&(t.value===">"||t.value===")"))return!1}return!0}#n(t,e){return{type:t,value:e||"",start:this.#i,end:this.#i+1,column:this.#f,line:this.#a}}#R(t,e){var i=this.#n(t,e);return i.op=!0,i}#L(){for(;this.#t()&&!this.#d(this.#t());)this.#e();this.#e()}#x(){for(var t=this.#n("WHITESPACE"),e="";this.#t()&&this.#v(this.#t());)this.#d(this.#t())&&(this.#c="indeterminant"),e+=this.#e();return t.value=e,t.end=this.#i,t}#N(){var t=this.#n("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.#i,t}#b(){var t=this.#n("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.#i,t}#P(){for(var t=this.#n("ATTRIBUTE_REF"),e=this.#e();this.#i<this.#s.length&&this.#t()!=="]";)e+=this.#e();return this.#t()==="]"&&(e+=this.#e()),t.value=e,t.end=this.#i,t}#M(){for(var t=this.#n("ATTRIBUTE_REF"),e=this.#e();this.#E(this.#t());)e+=this.#e();return this.#t()==="="&&(e+=this.#e(),this.#t()==='"'||this.#t()==="'"?e+=this.#k().value:(this.#o(this.#t())||this.#u(this.#t())||this.#T(this.#t()))&&(e+=this.#g().value)),t.value=e,t.end=this.#i,t}#F(){for(var t=this.#n("STYLE_REF"),e=this.#e();this.#o(this.#t())||this.#t()==="-";)e+=this.#e();return t.value=e,t.end=this.#i,t}#H(){var t=this.#n("IDENTIFIER");this.#e();for(var e="";this.#o(this.#t());)e+=this.#e();return t.value=e,t.end=this.#i,t}#D(){var t=this.#n("TEMPLATE_LINE");t.value="TEMPLATE_LINE";for(var e="";this.#t()&&!this.#d(this.#t());)e+=this.#e();return this.#t()&&this.#d(this.#t())&&(this.#e(),e+=`
`,this.#c="indeterminant"),t.content=e,t.end=this.#i,t}#O(){var t=this.#n("IDENTIFIER"),e=this.#e(),i=e==="\\";for(i&&(e="");(this.#o(this.#t())||this.#u(this.#t())||this.#T(this.#t())||this.#t()==="\\"||this.#t()==="{"||this.#t()==="}")&&!(this.#t()==="$"&&!i);)this.#t()==="\\"?(i=!0,this.#e()):(i=!1,e+=this.#e());return this.#t()==="!"&&e==="beep"&&(e+=this.#e()),t.value=e,t.end=this.#i,t}#g(){for(var t=this.#n("IDENTIFIER"),e=this.#e();this.#o(this.#t())||this.#u(this.#t())||this.#T(this.#t());)e+=this.#e();return this.#t()==="!"&&e==="beep"&&(e+=this.#e()),t.value=e,t.end=this.#i,t}#W(){for(var t=this.#n("NUMBER"),e=this.#e();this.#u(this.#t());)e+=this.#e();for(this.#t()==="."&&this.#u(this.#l())&&(e+=this.#e());this.#u(this.#t());)e+=this.#e();for((this.#t()==="e"||this.#t()==="E")&&(this.#u(this.#l())?e+=this.#e():this.#l()==="-"&&(e+=this.#e(),e+=this.#e()));this.#u(this.#t());)e+=this.#e();return t.value=e,t.end=this.#i,t}#C(){for(var t=this.#R(),e=this.#e();this.#t()&&L[e+this.#t()];)e+=this.#e();return t.type=L[e],t.value=e,t.end=this.#i,t}#k(){var t=this.#n("STRING"),e=this.#e();t.template=e==="`";for(var i="";this.#t()&&this.#t()!==e;)if(this.#t()==="\\"){this.#e();let n=this.#e();if(n==="b")i+="\b";else if(n==="f")i+="\f";else if(n==="n")i+=`
`;else if(n==="r")i+="\r";else if(n==="t")i+=" ";else if(n==="v")i+="\v";else if(t.template&&n==="$")i+="\\$";else if(n==="x"){let d=this.#U();if(Number.isNaN(d))throw new Error("Invalid hexadecimal escape at [Line: "+t.line+", Column: "+t.column+"]");i+=String.fromCharCode(d)}else i+=n}else i+=this.#e();if(this.#t()!==e)throw new Error("Unterminated string at [Line: "+t.line+", Column: "+t.column+"]");return this.#e(),t.value=i,t.end=this.#i,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}#B(){var t=this.#t(),e=this.#l(),i=this.#w(2);return t==="-"&&e==="-"&&(this.#v(i)||i===""||i==="-")||t==="/"&&e==="/"&&(this.#v(i)||i===""||i==="/")}#z(){for(;this.#i<this.#s.length;)if(this.#B())this.#L();else if(this.#v(this.#t()))this.#r.push(this.#x());else if(!this.#y()&&this.#t()==="."&&(this.#o(this.#l())||this.#l()==="{"||this.#l()==="-"))this.#r.push(this.#N());else if(!this.#y()&&this.#t()==="#"&&(this.#o(this.#l())||this.#l()==="{"))this.#m==="lines"&&this.#c==="indeterminant"?(this.#c="command",this.#r.push(this.#H())):this.#r.push(this.#b());else if(this.#m==="lines"&&this.#c==="indeterminant"&&this.#p===0)this.#c="template",this.#r.push(this.#D());else if(this.#t()==="["&&this.#l()==="@")this.#r.push(this.#P());else if(this.#t()==="@")this.#r.push(this.#M());else if(this.#t()==="*"&&this.#o(this.#l()))this.#r.push(this.#F());else if(this.#A()&&(this.#o(this.#t())||this.#t()==="\\")&&this.#c!=="command")this.#r.push(this.#O());else if(this.#_()&&(this.#o(this.#t())||this.#T(this.#t())))this.#r.push(this.#g());else if(this.#u(this.#t()))this.#r.push(this.#W());else if(this.#_()&&(this.#t()==='"'||this.#t()==="`"))this.#r.push(this.#k());else if(this.#_()&&this.#t()==="'")this.#I()?this.#r.push(this.#k()):this.#r.push(this.#C());else if(L[this.#t()])this.#h==="$"&&this.#t()==="{"&&this.#p++,this.#t()==="}"&&this.#p--,this.#r.push(this.#C());else if(this.#A()||this.#S(this.#t()))this.#r.push(this.#n("RESERVED",this.#e()));else if(this.#i<this.#s.length)throw new Error("Unknown token: "+this.#t()+" ");return new x(this.#r,this.#s)}};function F(v){let{runtime:t,createParser:e,reactivity:i}=v.internals,n=new w;function d(){if(!(typeof document>"u"||!document.head)&&!document.head.querySelector('style[data-hyperscript-component="fouce-guard"]')){var s=document.createElement("style");s.setAttribute("data-hyperscript-component","fouce-guard"),s.textContent=":not(:defined) { visibility: hidden; }",document.head.appendChild(s)}}function I(s,h,r){if(!h)return s;var u=document.createElement("div");u.innerHTML=h;var a={},T=[];for(var l of Array.from(u.childNodes)){l.nodeType===1&&r&&!l.hasAttribute("dom-scope")&&l.setAttribute("dom-scope","parent of "+r);var c=l.nodeType===1&&l.getAttribute("slot");c?(l.removeAttribute("slot"),a[c]||(a[c]=""),a[c]+=l.outerHTML):T.push(l.nodeType===1?l.outerHTML:l.nodeType===3?l.textContent:"")}var E=T.join(""),y=s.replace(/<slot\s+name\s*=\s*["']([^"']+)["']\s*\/?\s*>(\s*<\/slot>)?/g,function(f,o){return a[o]||""});return y=y.replace(/<slot\s*\/?\s*>(\s*<\/slot>)?/g,E),y}function _(s,h){if(typeof h!="string")return null;var r=s._attrsCache||(s._attrsCache={});if(!r[h]){var u=s.getAttribute(h);if(u==null)return null;try{r[h]=e(n.tokenize(u)).requireElement("expression")}catch(a){return console.error("component: failed to parse attrs."+h+":",a.message),null}}return r[h]}function p(s){var h=s.parentElement;return h?t.makeContext(h,null,h,null):null}function k(s){return new Proxy({_hsSkipTracking:!0},{get:function(h,r){if(r==="_hsSkipTracking")return!0;if(!(typeof r!="string"||r.startsWith("_"))){var u=_(s,r);if(u){var a=p(s);return a?u.evaluate(a):void 0}}},set:function(h,r,u){var a=_(s,r);if(!a||!a.set)return!1;var T=p(s);if(!T)return!1;var l={};if(a.lhs)for(var c in a.lhs){var E=a.lhs[c];l[c]=E&&E.evaluate?E.evaluate(T):E}return a.set(T,l,u),!0}})}function H(s,h){let r=s.getAttribute("component");if(!r.includes("-")){console.error("component name must contain a dash: '"+r+"'");return}for(var u=s.textContent,a="",T=/<style[^>]*>([\s\S]*?)<\/style>/gi,l;(l=T.exec(u))!==null;)a+=l[1]+`
`;if(a){u=u.replace(/<style[^>]*>[\s\S]*?<\/style>/gi,""),s.textContent=u;var c=document.createElement("style");c.setAttribute("data-hyperscript-component",r),c.textContent="@scope ("+r+`) {
`+a+"}",document.head.appendChild(c)}let E=s.textContent,y=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 f=t.getInternalData(this);f.elementScope||(f.elementScope={}),f.elementScope.attrs=k(this),h&&(this.setAttribute("_",h),v.process(this));let o=this;var m=I(E,o._slotContent,r);queueMicrotask(function(){var A=o._renderTemplate(m);A&&A.then?A.then(function(C){o._stampTemplate(C),o._setupReactiveEffect(m)}):(o._stampTemplate(A),o._setupReactiveEffect(m))})}disconnectedCallback(){i.stopElementEffects(this),t.cleanup(this),this._hypercomp_initialized=!1,this._hypercomp_stamped=!1}_setupReactiveEffect(f){var o=this;i.createEffect(function(){return o._renderTemplate(f)},function(m){o._stampTemplate(m)},{element:o})}_renderTemplate(f){var o=t.makeContext(this,null,this,null),m=[];o.meta.__ht_template_result=m;var A=n.tokenize(f,"lines"),C=e(A),R;try{R=C.parseElement("commandList"),C.ensureTerminated(R)}catch(S){return console.error("hypercomp template parse error:",S.message||S),""}var P,M,O=new Promise(function(S,W){P=S,M=W});return R.execute(o),this.__hs_scopes=o.meta.__ht_scopes||null,o.meta.returned||!o.meta.resolve?m.join(""):(o.meta.resolve=P,o.meta.reject=M,O.then(function(){return m.join("")}))}_stampTemplate(f){this._hypercomp_stamped?t.morph(this,f):(this.innerHTML=f,v.process(this),this._hypercomp_stamped=!0)}};customElements.define(r,y)}var N=new Set,g=new Map;function b(s){var h=s.getAttribute("component");if(!(!h||N.has(h)||customElements.get(h))){N.add(h);var r=s.hasOwnProperty("_componentScript")?s._componentScript:s.getAttribute("_")||"";s.hasAttribute("_")&&s.removeAttribute("_"),H(s,r||"")}}function D(s){if(g.has(s))return g.get(s);var h=fetch(s).then(function(r){if(!r.ok)throw new Error("HTTP "+r.status+" fetching "+s);return r.text()}).then(function(r){var u=new DOMParser().parseFromString(r,"text/html");for(let a of u.querySelectorAll('script[type="text/hyperscript-template"][component]'))b(a)}).catch(function(r){console.error("hyperscript component bundle '"+s+"': "+r.message),g.delete(s)});return g.set(s,h),h}v.addBeforeProcessHook(function(s){d(),!(!s||!s.querySelectorAll)&&s.querySelectorAll('script[type="text/hyperscript-template"][component]').forEach(function(h){"_componentScript"in h||(h._componentScript=h.getAttribute("_")||"",h.removeAttribute("_"))})}),v.addAfterProcessHook(function(s){if(!(!s||!s.querySelectorAll)){for(var h of s.querySelectorAll('script[type="text/hyperscript-template"][component]'))b(h);for(var r of s.querySelectorAll('script[type="text/hyperscript-component"][src]'))r._componentBundleLoaded||(r._componentBundleLoaded=!0,D(r.getAttribute("src")))}})}typeof self<"u"&&self._hyperscript&&self._hyperscript.use(F);export{F as default};