@tslite/parser 0.2.0 → 0.3.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/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
- 'use strict';var core=require('@tslite/core');var Q=class extends core.TSLiteError{constructor(l,i,o){super("lex-error",`${l} (${i.line}:${i.column})`,{position:i,index:o});this.position=i;this.index=o;this.name="LexError";}},de=new Set(["const","if","else","return","async","await","true","false","null","typeof","let","var","function","class","new","for","while","do","this","throw","try","catch","finally","switch","case","break","continue","default","void","delete","instanceof","in","yield","import","export","extends","super","debugger","with"]),Be=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],oe=u=>u>="a"&&u<="z"||u>="A"&&u<="Z"||u==="_"||u==="$",ye=u=>oe(u)||u>="0"&&u<="9",M=u=>u>="0"&&u<="9",fe=u=>M(u)||u>="a"&&u<="f"||u>="A"&&u<="F",qe=u=>ye(u)||u==="-",ze=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),Ve=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function se(u){let N=[],l=0,i=1,o=0,v=()=>({line:i,column:o}),d=(g=0)=>u[l+g]??"",A=g=>{throw new Q(g,v(),l)},r=()=>{let g=u[l++];return g==="\r"?(u[l]===`
2
- `&&l++,i++,o=0):g===`
3
- `||g==="\u2028"||g==="\u2029"?(i++,o=0):o++,g},b=g=>g===`
4
- `||g==="\r"||g==="\u2028"||g==="\u2029",K=()=>{for(;;){let g=d();if(g===" "||g===" "||g==="\f"||g==="\v"||b(g))r();else if(g==="/"&&d(1)==="/")for(;l<u.length&&!b(d());)r();else if(g==="/"&&d(1)==="*")for(r(),r();;){if(l>=u.length&&A("Unterminated block comment"),d()==="*"&&d(1)==="/"){r(),r();break}r();}else break}},m=(g,C,L,k)=>{N.push({type:g,value:C,start:L,end:l,loc:{start:k,end:v()}}),q=g==="Punctuator"?ze.has(C):g==="Keyword"?Ve.has(C):g==="TemplateHead"||g==="TemplateMiddle";},y=g=>{for(r();l<u.length;){let C=d();if(C===g)return r(),u.slice(S,l);if(b(C)&&A("Unterminated string literal"),C==="\\"){r(),l>=u.length&&A("Unterminated string literal"),r();continue}r();}return A("Unterminated string literal")},P=()=>{if(d()==="0"&&(d(1)==="x"||d(1)==="X")){for(r(),r(),fe(d())||A("Invalid hexadecimal literal");fe(d());)r();return}if(d()==="0"&&(d(1)==="b"||d(1)==="B")){for(r(),r(),d()!=="0"&&d()!=="1"&&A("Invalid binary literal");d()==="0"||d()==="1";)r();return}if(d()==="0"&&(d(1)==="o"||d(1)==="O")){for(r(),r(),d()>="0"&&d()<="7"||A("Invalid octal literal");d()>="0"&&d()<="7";)r();return}for(;M(d());)r();if(d()===".")for(r();M(d());)r();if(d()==="e"||d()==="E")for(r(),(d()==="+"||d()==="-")&&r(),M(d())||A("Invalid exponent");M(d());)r();},c=()=>{let g=l;for(;l<u.length;){let C=d();if(C==="`"){let L=u.slice(g,l);return r(),{raw:L,interp:false}}if(C==="$"&&d(1)==="{"){let L=u.slice(g,l);return r(),r(),{raw:L,interp:true}}if(C==="\\"){if(r(),l>=u.length)break;r();continue}r();}return A("Unterminated template literal")},S=0,x=0,X=[],q=true,D="js",$=[],F=[];for(;;){if(D==="jsxChildren"){S=l;let k=v();if(l>=u.length){m("EOF","",S,k);break}let a=d();if(a==="<"){d(1)==="/"?(r(),r(),m("Punctuator","</",S,k),D="jsxCloseTag"):(r(),m("Punctuator","<",S,k),$.push("jsxChildren"),D="jsxOpenTag");continue}if(a==="{"){r(),m("Punctuator","{",S,k),F.push({level:x,restore:"jsxChildren"}),D="js";continue}for(;l<u.length&&d()!=="<"&&d()!=="{";)r();m("JSXText",u.slice(S,l),S,k);continue}if(D==="jsxOpenTag"||D==="jsxCloseTag"){K(),S=l;let k=v();if(l>=u.length){m("EOF","",S,k);break}let a=d();if(oe(a)){for(r();qe(d());)r();m("Identifier",u.slice(S,l),S,k);continue}if(a==="/"&&d(1)===">"){r(),r(),m("Punctuator","/>",S,k),D=$.pop()??"js";continue}if(a===">"){r(),m("Punctuator",">",S,k),D=D==="jsxCloseTag"?$.pop()??"js":"jsxChildren";continue}if(a==="{"){r(),m("Punctuator","{",S,k),F.push({level:x,restore:D}),D="js";continue}if(a==='"'||a==="'"){m("String",y(a),S,k);continue}if(a==="="||a==="."||a===":"){r(),m("Punctuator",a,S,k);continue}A(`Unexpected character '${a}' in JSX tag`);}K(),S=l;let g=v();if(l>=u.length){m("EOF","",S,g);break}let C=d();if(oe(C)){for(;ye(d());)r();let k=u.slice(S,l);m(de.has(k)?"Keyword":"Identifier",k,S,g);continue}if(M(C)||C==="."&&M(d(1))){P(),m("Number",u.slice(S,l),S,g);continue}if(C==='"'||C==="'"){let k=y(C);m("String",k,S,g);continue}if(C==="`"){r();let{raw:k,interp:a}=c();a?(m("TemplateHead",k,S,g),X.push(x)):m("Template",k,S,g);continue}if(C==="<"&&q){r(),m("Punctuator","<",S,g),$.push("js"),D="jsxOpenTag";continue}if(C==="{"){r(),m("Punctuator","{",S,g),x++;continue}if(C==="}"){if(X.length>0&&x===X[X.length-1]){X.pop(),r();let{raw:k,interp:a}=c();m(a?"TemplateMiddle":"TemplateTail",k,S,g),a&&X.push(x);continue}if(F.length>0&&x===F[F.length-1].level){let{restore:k}=F.pop();r(),m("Punctuator","}",S,g),D=k;continue}r(),m("Punctuator","}",S,g),x--;continue}if(C==="?"&&d(1)==="."&&M(d(2))){r(),m("Punctuator","?",S,g);continue}let L=Be.find(k=>u.startsWith(k,l));if(L){for(let k=0;k<L.length;k++)r();m("Punctuator",L,S,g);continue}A(`Unexpected character '${C}'`);}return N}var ae=(u=[])=>({vars:new Set(u),bounds:new Map,noCond:false,inferSink:null}),_=(u,N)=>{let l=new Set(u.vars),i=new Map(u.bounds);for(let o of N)l.add(o.name),o.bound||o.isConst?i.set(o.name,{bound:o.bound,isConst:o.isConst}):i.delete(o.name);return {vars:l,bounds:i,noCond:u.noCond,inferSink:u.inferSink}},We=(u,N)=>{let l=u.bounds.get(N);return l&&(l.bound||l.isConst)?{t:"var",name:N,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:N}},me=u=>{let{peek:N,next:l,isP:i,isK:o,expectP:v,fail:d,unescape:A}=u,r=()=>{let a=u.pos(),p=u.tokens[a],f=u.tokens[a-1];return !!f&&!!p&&p.loc.start.line>f.loc.end.line},b=()=>{let a=0;for(let p=u.pos();p<u.tokens.length;p++){let f=u.tokens[p];if(f.type==="EOF")return false;if(f.type==="Punctuator"){if(["(","[","{","<"].includes(f.value))a++;else if([")","]","}",">"].includes(f.value)&&(a--,a===0)){let T=u.tokens[p+1];return !!T&&T.type==="Punctuator"&&T.value==="=>"}}}return false},K=()=>{let a=u.tokens,p=[],f=0,T=false;for(let E=u.pos();E<a.length;E++){let I=a[E];if(I.type==="EOF")break;if(I.type==="Punctuator"){if(I.value==="<")f++,f===1&&(T=true);else if(I.value===">"){if(f--,f===0)break}else I.value==="("||I.value==="["||I.value==="{"?(f++,T=false):I.value===")"||I.value==="]"||I.value==="}"?(f--,T=false):I.value===","&&f===1?T=true:T=false;continue}if(f===1&&T){if(I.type==="Keyword"&&I.value==="const")continue;if(I.type==="Identifier"){p.push(I.value),T=false;continue}}T=false;}return p},m=a=>{let p=_(a,K().map(T=>({name:T})));v("<");let f=[];for(;!i(">");){let T=o("const");T&&l();let E=N();E.type!=="Identifier"&&d("Expected type parameter name"),l();let I;o("extends")&&(l(),I=c(p)),i("=")&&(l(),c(p)),f.push({name:E.value,...I?{bound:I}:{},...T?{isConst:true}:{}}),i(">")||v(",");}return v(">"),f},y=a=>{v("<");let p=[];for(;!i(">");)p.push(c(a)),i(">")||v(",");return v(">"),p},P=a=>{let p=N();return p.type==="Identifier"&&p.value===a},c=a=>{let p=S(a);if(a.noCond||!o("extends")||r())return p;l();let f=[],T=S({...a,noCond:true,inferSink:f});v("?");let E=_(a,f.map(B=>({name:B}))),I=c(E);v(":");let z=c(E);return {t:"conditional",check:p,extends:T,then:I,else:z,...f.length?{infer:f}:{}}},S=a=>{i("|")&&l();let p=x(a);if(!i("|"))return p;let f=[p];for(;i("|");)l(),f.push(x(a));return {t:"union",of:f}},x=a=>{i("&")&&l();let p=X(a);if(!i("&"))return p;let f=[p];for(;i("&");)l(),f.push(X(a));return {t:"intersection",of:f}},X=a=>{if(P("keyof"))return l(),{t:"keyof",of:X(a)};if(P("infer")){l();let p=N();p.type!=="Identifier"&&d("Expected 'infer' variable name"),l();let f=a.inferSink;return f?(f.push(p.value),{t:"var",name:p.value}):d("'infer' is only allowed in the 'extends' clause of a conditional")}return q(a)},q=a=>{let p=D(a);for(;i("[")&&!r();)if(l(),i("]"))l(),p={t:"array",items:p};else {let f=c(a);v("]"),p={t:"index",obj:p,key:f};}return p},D=a=>{if(i("<")){let f=m(a),T=_(a,f);return F(T)}if(i("(")){if(b())return F(a);l();let f=c(a.noCond?{...a,noCond:false}:a);return v(")"),f}if(i("{"))return C()?L(a):k(a);if(i("["))return g(a);let p=N();if(p.type==="String")return l(),{t:"literal",value:A(p.value.slice(1,-1))};if(p.type==="Number")return l(),{t:"literal",value:Number(p.value)};if(o("true"))return l(),{t:"literal",value:true};if(o("false"))return l(),{t:"literal",value:false};if(o("null"))return l(),{t:"null"};if(o("void"))return l(),{t:"void"};if(p.type==="Identifier"){switch(l(),p.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"};case "never":return {t:"union",of:[]}}let f=i("<")?y(a):[];return a.vars.has(p.value)&&f.length===0?We(a,p.value):p.value==="ReturnType"&&f.length===1?{t:"returnType",fn:f[0]}:p.value==="Parameters"&&f.length===1?{t:"parameters",fn:f[0]}:{t:"apply",name:p.value,args:f}}return d(`Expected a type but got '${p.value||p.type}'`)},$=a=>{v("(");let p=[];for(;!i(")");){let f=N();f.type!=="Identifier"&&d("Expected parameter name in function type"),l();let T=i("?");T&&l(),v(":");let E=c(a);p.push({name:f.value,type:E,...T?{optional:true}:{}}),i(")")||v(",");}return v(")"),p},F=a=>{let p=$(a);v("=>");let f=c(a);return {t:"fn",params:p,returns:f}},g=a=>{v("[");let p=[],f=[];for(;!i("]");){if(i("...")){l();let T=c(a);T.t==="array"?(p.push(T.items),f.push("rest")):(p.push(T),f.push("variadic"));}else {let T=c(a),E=i("?");E&&l(),p.push(T),f.push(E?"optional":"req");}i("]")||v(",");}return v("]"),f.some(T=>T!=="req")?{t:"tuple",elements:p,flags:f}:{t:"tuple",elements:p}},C=()=>{let a=u.tokens,p=u.pos()+1;return a[p]?.type==="Punctuator"&&(a[p]?.value==="+"||a[p]?.value==="-")&&p++,a[p]?.type==="Identifier"&&a[p]?.value==="readonly"&&p++,a[p]?.type==="Punctuator"&&a[p]?.value==="["&&a[p+1]?.type==="Identifier"&&a[p+2]?.type==="Keyword"&&a[p+2]?.value==="in"},L=a=>{v("{"),(i("+")||i("-"))&&l(),P("readonly")&&l(),v("[");let p=N();p.type!=="Identifier"&&d("Expected mapped type key name"),l(),o("in")||d("Expected 'in' in mapped type"),l();let f=c(a);P("as")&&d("Key remapping ('as') in mapped types is not supported"),v("]");let T=false,E=i("+")||i("-")?l().value:null;i("?")&&(l(),T=E!=="-"),v(":");let I=c(_(a,[{name:p.value}]));return (i(";")||i(","))&&l(),v("}"),{t:"mapped",key:p.value,over:f,body:I,optional:T}},k=a=>{v("{");let p={},f,T=[];for(;!i("}");){if(i("(")){let E=$(a);v(":");let I=c(a);T.push({t:"fn",params:E,returns:I});}else if(i("[")){l(),N().type!=="Identifier"&&d("Expected index signature parameter name"),l(),v(":");let E=N();(E.type!=="Identifier"||E.value!=="string"&&E.value!=="number")&&d("Index signature parameter type must be 'string' or 'number'"),l(),v("]"),v(":"),f={key:E.value,value:c(a)};}else {let E=N(),I;if(E.type==="Identifier"||E.type==="Keyword")l(),I=E.value;else if(E.type==="String")l(),I=A(E.value.slice(1,-1));else if(E.type==="Number")l(),I=E.value;else return d("Expected property name in object type");let z=i("?");z&&l(),v(":");let B=c(a);p[I]={type:B,...z?{optional:true}:{}};}if(i(";")||i(","))l();else break}return v("}"),T.length>0?Object.keys(p).length>0||f?d("Callable object types (call signatures mixed with members) are not supported"):T.length===1?T[0]:{t:"overload",signatures:T}:{t:"object",props:p,...f?{index:f}:{}}};return {parseType:a=>c(ae(a)),parseTypeParamList:()=>m(ae()),parseTypeInScope:a=>c(_(ae(),a))}};var ee=class extends core.TSLiteError{constructor(l,i,o){super("parse-error",`${l} (${i.line}:${i.column})`,{position:i,index:o});this.position=i;this.index=o;this.name="ParseError";}},Ge={"??":1,"||":2,"&&":3,"|":4,"^":5,"&":6,"==":7,"===":7,"!=":7,"!==":7,"<":8,"<=":8,">":8,">=":8,in:8,as:8,"<<":9,">>":9,">>>":9,"+":10,"-":10,"*":11,"/":11,"%":11,"**":12},He=new Set(["??","||","&&"]),Ye=new Set(["class","debugger","with"]),Ze=new Set(["=","+=","-=","*=","/=","%="]);function he(u){let N=se(u),l=0,i=(e=0)=>N[Math.min(l+e,N.length-1)],o=()=>N[l++],v=()=>N[l-1],d=()=>{let e=N[l],t=N[l-1];return !!e&&!!t&&e.loc.start.line>t.loc.end.line},A=()=>i().type==="EOF",r=e=>i().type==="Punctuator"&&i().value===e,b=e=>i().type==="Keyword"&&i().value===e,K=e=>i().type==="Identifier"&&i().value===e,m=(e,t=i())=>{throw new ee(e,t.loc.start,t.start)},y=e=>(r(e)||m(`Expected '${e}' but got '${i().value||i().type}'`),o()),P=(e,t)=>{let n=v()??t;return Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}})},c=(e,t,n)=>Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}}),S=e=>{let t="";for(let n=0;n<e.length;n++){if(e[n]!=="\\"){t+=e[n];continue}let s=e[++n];switch(s){case "n":t+=`
5
- `;break;case "t":t+=" ";break;case "r":t+="\r";break;case "b":t+="\b";break;case "f":t+="\f";break;case "v":t+="\v";break;case "0":t+="\0";break;case "x":t+=String.fromCharCode(parseInt(e.slice(n+1,n+3),16)),n+=2;break;case "u":if(e[n+1]==="{"){let h=e.indexOf("}",n);t+=String.fromCodePoint(parseInt(e.slice(n+2,h),16)),n=h;}else t+=String.fromCharCode(parseInt(e.slice(n+1,n+5),16)),n+=4;break;default:t+=s;}}return t},x=(e=false)=>X(e),X=(e=false)=>{let t=q(e),n=i();if(n.type==="Punctuator"&&Ze.has(n.value)){o();let s=X();return c({type:"AssignmentExpression",operator:n.value,left:t,right:s},t,s)}return t},q=(e=false)=>{let t=$(0,e);if(!r("?"))return t;o();let n=x();y(":");let s=x();return c({type:"ConditionalExpression",test:t,consequent:n,alternate:s},t,s)},D=()=>{let e=i();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let t=i(1);if(t.type==="Punctuator"&&t.value===">"&&e.end===t.start){let n=i(2);return n.type==="Punctuator"&&n.value===">"&&t.end===n.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},$=(e,t=false)=>{let n=F();for(;;){let s=D();if(!s)break;let{op:h,consume:w}=s;if(h==="in"&&t)break;let j=Ge[h];if(!j||j<=e)break;if(h==="as"){if(d())break;o();let J=H(new Set);n=c({type:"TSAsExpression",expression:n,typeAnnotation:J},n,v());continue}for(let J=0;J<w;J++)o();let O=$(h==="**"?j-1:j,t);n=c({type:He.has(h)?"LogicalExpression":"BinaryExpression",operator:h,left:n,right:O},n,O);}return n},F=()=>{let e=i();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(o(),P({type:"UpdateExpression",operator:e.value,prefix:true,argument:F()},e)):b("await")?(o(),P({type:"AwaitExpression",argument:F()},e)):b("typeof")||b("void")||b("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?g(e):C()},g=e=>{o();let t=F();return r("**")&&m(`An unary expression with the '${e.value}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.`),P({type:"UnaryExpression",operator:e.value,prefix:true,argument:t},e)},C=()=>{let e=Te(),t=false;for(;;)if(r("?."))if(o(),t=true,r("(")){let{args:n,close:s}=L();e=c({type:"CallExpression",callee:e,arguments:n,optional:true},e,s);}else if(r("[")){o();let n=x(),s=y("]");e=c({type:"MemberExpression",object:e,property:n,computed:true,optional:true},e,s);}else {let n=i();n.type!=="Identifier"&&n.type!=="Keyword"&&m("Expected property name after '?.'"),o();let s=P({type:"Identifier",name:n.value},n);e=c({type:"MemberExpression",object:e,property:s,computed:false,optional:true},e,s);}else if(r(".")){o();let n=i();n.type!=="Identifier"&&n.type!=="Keyword"&&m("Expected property name"),o();let s=P({type:"Identifier",name:n.value},n);e=c({type:"MemberExpression",object:e,property:s,computed:false,optional:false},e,s);}else if(r("[")){o();let n=x(),s=y("]");e=c({type:"MemberExpression",object:e,property:n,computed:true,optional:false},e,s);}else if(r("(")){let{args:n,close:s}=L();e=c({type:"CallExpression",callee:e,arguments:n,optional:false},e,s);}else if(r("++")||r("--")){let n=o();e=c({type:"UpdateExpression",operator:n.value,prefix:false,argument:e},e,n);}else break;return t?c({type:"ChainExpression",expression:e},e,e):e},L=()=>{y("(");let e=[];for(;!r(")");)e.push(k()),r(")")||y(",");return {args:e,close:y(")")}},k=()=>{if(r("...")){let e=o(),t=x();return c({type:"SpreadElement",argument:t},e,t)}return x()},a=()=>{let e=i();return e.type!=="Identifier"&&m("Expected identifier"),o(),P({type:"Identifier",name:e.value},e)},p=e=>{let t=r("{")?f(e):r("[")?T(e):a();return r(":")&&(o(),t.typeAnnotation=H(e)),t},f=e=>{let t=y("{"),n=[];for(;!r("}");){if(r("...")){let h=o(),w=p(e);n.push(c({type:"RestElement",argument:w},h,w));}else {let h=a(),w,j=false;if(r(":")?(o(),w=p(e)):(w={...h},j=true),r("=")){o();let O=x();w=c({type:"AssignmentPattern",left:w,right:O},w,O);}n.push(c({type:"Property",key:h,value:w,kind:"init",shorthand:j,computed:false,method:false},h,w));}r("}")||y(",");}let s=y("}");return c({type:"ObjectPattern",properties:n},t,s)},T=e=>{let t=y("["),n=[];for(;!r("]");){if(r(",")){n.push(null),o();continue}if(r("...")){let h=o(),w=p(e);n.push(c({type:"RestElement",argument:w},h,w));}else {let h=p(e);if(r("=")){o();let w=x();h=c({type:"AssignmentPattern",left:h,right:w},h,w);}n.push(h);}r("]")||y(",");}let s=y("]");return c({type:"ArrayPattern",elements:n},t,s)},E=()=>{let e=0;for(let t=l;t<N.length;t++){let n=N[t];if(n.type==="EOF")return false;if(n.type==="Punctuator"){if(n.value==="("||n.value==="["||n.value==="{")e++;else if((n.value===")"||n.value==="]"||n.value==="}")&&(e--,e===0)){let s=N[t+1];return !s||s.type!=="Punctuator"?false:s.value==="=>"?true:s.value===":"?I(t+2):false}}}return false},I=e=>{let t=0;for(let n=e;n<N.length;n++){let s=N[n];if(s.type==="EOF")return false;if(s.type==="Punctuator")if(["(","[","{","<"].includes(s.value))t++;else if([")","]","}",">"].includes(s.value)){if(t===0)return false;t--;}else {if(t===0&&s.value==="=>")return true;if(t===0&&(s.value===";"||s.value===","))return false}}return false},z=(e=new Set)=>{y("(");let t=[];for(;!r(")");){if(r("...")){let n=o(),s=p(e);t.push(c({type:"RestElement",argument:s},n,s));}else {let n=p(e);if(r("=")){o();let s=x();n=c({type:"AssignmentPattern",left:n,right:s},n,s);}t.push(n);}r(")")||y(",");}return y(")"),t},B=(e,t)=>{let n=r("(")?z():[a()],s;r(":")&&(o(),s=H(new Set)),y("=>");let h=r("{")?W():x();return P({type:"ArrowFunctionExpression",params:n,body:h,async:e,expression:h.type!=="BlockStatement",generator:false,...s?{returnType:s}:{}},t)},ge=()=>{let e=y("["),t=[];for(;!r("]");){if(r(",")){o(),t.push(null);continue}t.push(k()),r("]")||y(",");}return c({type:"ArrayExpression",elements:t},e,y("]"))},ve=()=>{let e=y("{"),t=[];for(;!r("}");){if(r("...")){let n=o(),s=x();t.push(c({type:"SpreadElement",argument:s},n,s));}else {let n=i(),s,h=false;if(r("["))h=true,o(),s=x(),y("]");else if(n.type==="String")o(),s=P({type:"Literal",value:S(n.value.slice(1,-1)),raw:n.value},n);else if(n.type==="Number")o(),s=P({type:"Literal",value:Number(n.value),raw:n.value},n);else if(n.type==="Identifier"||n.type==="Keyword")o(),s=P({type:"Identifier",name:n.value},n);else return m("Invalid property key");if(r(":")){o();let w=x();t.push(c({type:"Property",key:s,value:w,kind:"init",method:false,shorthand:false,computed:h},n,w));}else t.push(c({type:"Property",key:s,value:s,kind:"init",method:false,shorthand:true,computed:false},n,s));}r("}")||y(",");}return c({type:"ObjectExpression",properties:t},e,y("}"))},G=(e,t)=>({type:"TemplateElement",value:{raw:e,cooked:S(e)},tail:t}),be=e=>{o();let t=[G(e.value,false)],n=[];for(;;){n.push(x());let s=i();if(s.type==="TemplateMiddle")o(),t.push(G(s.value,false));else if(s.type==="TemplateTail"){o(),t.push(G(s.value,true));break}else m("Expected `}` to close template interpolation");}return P({type:"TemplateLiteral",quasis:t,expressions:n},e)},V=()=>{let e=i();return e.type!=="Identifier"&&m(`Expected JSX identifier but got '${e.value||e.type}'`),P({type:"JSXIdentifier",name:o().value},e)},ie=()=>{let e=V();if(r(":")){o();let t=V();return c({type:"JSXNamespacedName",namespace:e,name:t},e,t)}for(;r(".");){o();let t=V();e=c({type:"JSXMemberExpression",object:e,property:t},e,t);}return e},U=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${U(e.namespace)}:${U(e.name)}`:`${U(e.object)}.${U(e.property)}`,Se=()=>{let e=i();if(e.type==="String")return o(),P({type:"Literal",value:S(e.value.slice(1,-1)),raw:e.value},e);if(r("{")){let t=o(),n=X(),s=y("}");return c({type:"JSXExpressionContainer",expression:n},t,s)}return r("<")?te():m(`Invalid JSX attribute value '${e.value||e.type}'`)},xe=()=>{let e=[];for(;!r(">")&&!r("/>")&&!A();){if(r("{")){let s=o();y("...");let h=X(),w=y("}");e.push(c({type:"JSXSpreadAttribute",argument:h},s,w));continue}let t=V();if(r(":")){o();let s=V();t=c({type:"JSXNamespacedName",namespace:t,name:s},t,s);}let n=null;r("=")&&(o(),n=Se()),e.push(c({type:"JSXAttribute",name:t,value:n},t,n??t));}return e},le=()=>{let e=[];for(;!(r("</")||A());){let t=i();if(t.type==="JSXText")o(),e.push(P({type:"JSXText",value:t.value,raw:t.value},t));else if(r("{")){let n=o();if(r("}")){let s=o(),h=P({type:"JSXEmptyExpression"},n);e.push(c({type:"JSXExpressionContainer",expression:h},n,s));}else {let s=X(),h=y("}");e.push(c({type:"JSXExpressionContainer",expression:s},n,h));}}else r("<")?e.push(te()):m(`Unexpected token in JSX children '${t.value||t.type}'`);}return e},te=()=>{let e=y("<");if(r(">")){let Z=o(),re=c({type:"JSXOpeningFragment"},e,Z),Ke=le(),Re=y("</"),ue=y(">"),Ue=c({type:"JSXClosingFragment"},Re,ue);return c({type:"JSXFragment",openingFragment:re,children:Ke,closingFragment:Ue},e,ue)}let t=ie(),n=xe();if(r("/>")){let Z=o(),re=c({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:true},e,Z);return c({type:"JSXElement",openingElement:re,children:[],closingElement:null},e,Z)}let s=y(">"),h=c({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:false},e,s),w=le(),j=y("</"),O=i(),J=ie();U(J)!==U(t)&&m(`JSX closing tag </${U(J)}> does not match opening <${U(t)}>`,O);let Y=y(">"),$e=c({type:"JSXClosingElement",name:J},j,Y);return c({type:"JSXElement",openingElement:h,children:w,closingElement:$e},e,Y)},Te=()=>{let e=i();if(b("async")){let t=i(1);return t.value==="("||t.type==="Identifier"?(o(),B(true,e)):m("Unexpected 'async'")}if(r("(")&&E()||e.type==="Identifier"&&i(1).type==="Punctuator"&&i(1).value==="=>")return B(false,e);if(e.type==="Number")return o(),P({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return o(),P({type:"Literal",value:S(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return o(),P({type:"TemplateLiteral",expressions:[],quasis:[G(e.value,true)]},e);if(e.type==="TemplateHead")return be(e);if(e.type==="Keyword")return e.value==="true"?(o(),P({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(o(),P({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(o(),P({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?m("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?m("Anonymous functions are not supported \u2014 use an arrow function"):m(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return o(),P({type:"Identifier",name:e.value},e);if(r("(")){o();let t=x();return y(")"),t}return r("[")?ge():r("{")?ve():r("<")?te():m(`Unexpected token '${e.value||e.type}'`)},{parseType:H,parseTypeParamList:ke,parseTypeInScope:we}=me({tokens:N,pos:()=>l,peek:i,next:o,isP:r,isK:b,expectP:y,fail:m,unescape:S}),Ne=()=>{let e=o(),t=a(),n=[],s=[];r("<")&&(s=ke(),n=s.map(j=>j.name)),y("=");let h=we(s),w=c({type:"TSTypeAliasDeclaration",id:t,typeParameters:n,typeAnnotation:h},e,v());return r(";")&&o(),w},R=()=>{let e=i();if(e.type==="Identifier"&&e.value==="type"&&i(1).type==="Identifier"&&i(2).type==="Punctuator"&&(i(2).value==="="||i(2).value==="<"))return Ne();if(b("const")||b("let")||b("var")){let s=pe();return r(";")&&o(),s}if(b("if"))return Je();if(b("for"))return Ce();if(b("while"))return Oe();if(b("do"))return je();if(b("break")||b("continue"))return Ae();if(b("return"))return De();if(b("throw"))return Xe();if(b("try"))return Fe();if(b("switch"))return Le();if(b("import"))return Ee();if(b("export"))return Ie();if(r("{"))return W();if((b("function")||b("async")&&i(1).type==="Keyword"&&i(1).value==="function")&&m("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),r(";"))return o(),P({type:"EmptyStatement"},e);e.type==="Keyword"&&Ye.has(e.value)&&m(`'${e.value}' is not supported in this language`);let t=x(),n=r(";")?o():v();return c({type:"ExpressionStatement",expression:t},t,n)},pe=()=>{let e=o(),t=e.value,n=p(new Set),s=null;r("=")?(o(),s=x()):t==="const"&&m("'const' declarations must be initialized");let h=c({type:"VariableDeclarator",id:n,init:s},n,s??n);return c({type:"VariableDeclaration",kind:t,declarations:[h]},e,s??n)},Pe=()=>{let e=i();return e.type!=="String"&&m("Expected module specifier string"),o(),P({type:"Literal",value:S(e.value.slice(1,-1)),raw:e.value},e)},Ee=()=>{let e=o(),t=[];if(i().type!=="String"){if(i().type==="Identifier"){let s=a();t.push(c({type:"ImportDefaultSpecifier",local:s},s,s)),r(",")&&o();}if(r("*")){let s=o();K("as")||m("Expected 'as' after '*' in import"),o();let h=a();t.push(c({type:"ImportNamespaceSpecifier",local:h},s,h));}else if(r("{")){for(o();!r("}")&&!A();){let s=a(),h=s;if(K("as")&&(o(),h=a()),t.push(c({type:"ImportSpecifier",imported:s,local:h},s,h)),r(","))o();else break}y("}");}K("from")||m("Expected 'from' in import declaration"),o();}let n=Pe();return r(";")&&o(),P({type:"ImportDeclaration",specifiers:t,source:n},e)},Ie=()=>{let e=o();if(b("default")){o();let t=x();return r(";")&&o(),P({type:"ExportDefaultDeclaration",declaration:t},e)}if(b("const")||b("let")||b("var")){let t=pe();return r(";")&&o(),P({type:"ExportNamedDeclaration",declaration:t,specifiers:[],source:null},e)}if(r("{")){o();let t=[];for(;!r("}")&&!A();){let n=a(),s=n;if(K("as")&&(o(),s=a()),t.push(c({type:"ExportSpecifier",local:n,exported:s},n,s)),r(","))o();else break}return y("}"),r(";")&&o(),P({type:"ExportNamedDeclaration",declaration:null,specifiers:t,source:null},e)}return m("Expected 'const', '{', or 'default' after 'export'")},ce=()=>i().type==="Identifier"&&i().value==="of",ne=()=>b("in"),Ce=()=>{let e=o();y("(");let t=null,n=null;if(!r(";"))if(b("const")||b("let")||b("var")){let j=o(),O=a();if(r(":")&&(o(),O.typeAnnotation=H(new Set)),ce()||ne()){let J=c({type:"VariableDeclarator",id:O,init:null},O,O);n=c({type:"VariableDeclaration",kind:j.value,declarations:[J]},j,O);}else {let J=null;r("=")?(o(),J=x(true)):j.value==="const"&&m("'const' declarations must be initialized");let Y=c({type:"VariableDeclarator",id:O,init:J},O,J??O);t=c({type:"VariableDeclaration",kind:j.value,declarations:[Y]},j,J??O);}}else {let j=x(true);ce()||ne()?n=j:t=j;}if(n){let j=ne();o();let O=x();y(")");let J=R();return c(j?{type:"ForInStatement",left:n,right:O,body:J}:{type:"ForOfStatement",left:n,right:O,body:J,await:false},e,J)}y(";");let s=r(";")?null:x();y(";");let h=r(")")?null:x();y(")");let w=R();return c({type:"ForStatement",init:t,test:s,update:h,body:w},e,w)},je=()=>{let e=o(),t=R();b("while")||m("Expected 'while' after the 'do' body"),o(),y("(");let n=x(),s=y(")");return r(";")&&o(),c({type:"DoWhileStatement",body:t,test:n},e,s)},Ae=()=>{let e=o(),t=e.value==="break"?"BreakStatement":"ContinueStatement",n=r(";")?o():v();return c({type:t,label:null},e,n)},Oe=()=>{let e=o();y("(");let t=x();y(")");let n=R();return c({type:"WhileStatement",test:t,body:n},e,n)},Je=()=>{let e=o();y("(");let t=x();y(")");let n=R(),s=null;return b("else")&&(o(),s=R()),c({type:"IfStatement",test:t,consequent:n,alternate:s},e,s??n)},De=()=>{let e=o(),t=null;!r(";")&&!r("}")&&!A()&&(t=x());let n=r(";")?o():v();return c({type:"ReturnStatement",argument:t},e,n)},Xe=()=>{let e=o(),t=x(),n=r(";")?o():v();return c({type:"ThrowStatement",argument:t},e,n)},Fe=()=>{let e=o(),t=W(),n=null,s=null;if(b("catch")){let h=o(),w=null;r("(")&&(o(),w=a(),y(")"));let j=W();n=c({type:"CatchClause",param:w,body:j},h,j);}return b("finally")&&(o(),s=W()),!n&&!s&&m("'try' requires a 'catch' or 'finally' clause"),c({type:"TryStatement",block:t,handler:n,finalizer:s},e,s??n??t)},Le=()=>{let e=o();y("(");let t=x();y(")"),y("{");let n=[];for(;!r("}")&&!A();){let h=i(),w=null;b("case")?(o(),w=x()):b("default")?o():m("Expected 'case' or 'default' in switch"),y(":");let j=[];for(;!r("}")&&!b("case")&&!b("default")&&!A();)j.push(R());n.push(c({type:"SwitchCase",test:w,consequent:j},h,v()??h));}let s=y("}");return c({type:"SwitchStatement",discriminant:t,cases:n},e,s)},W=()=>{let e=y("{"),t=[];for(;!r("}")&&!A();)t.push(R());return c({type:"BlockStatement",body:t},e,y("}"))};return {parseExpr:x,parseProgram:()=>{let e=i(),t=[];for(;!A();)t.push(R());return c({type:"Program",sourceType:"module",body:t},e,v()??e)},eof:A,fail:m}}function Qe(u){return he(u).parseProgram()}function et(u){let{parseExpr:N,eof:l,fail:i}=he(u),o=N();return l()||i("Unexpected token after expression"),o}exports.KEYWORDS=de;exports.LexError=Q;exports.ParseError=ee;exports.parse=Qe;exports.parseExpression=et;exports.tokenize=se;
1
+ 'use strict';var core=require('@tslite/core');var Z=class extends core.TSLiteError{constructor(l,h,d,r,f){super("lex-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="LexError";}},ye=new Set(["const","if","else","return","async","await","true","false","null","typeof","let","var","function","class","new","for","while","do","this","throw","try","catch","finally","switch","case","break","continue","default","void","delete","instanceof","in","yield","import","export","extends","super","debugger","with","enum","interface"]),ze=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],se=c=>c>="a"&&c<="z"||c>="A"&&c<="Z"||c==="_"||c==="$",me=c=>se(c)||c>="0"&&c<="9",B=c=>c>="0"&&c<="9",de=c=>B(c)||c>="a"&&c<="f"||c>="A"&&c<="F",qe=c=>me(c)||c==="-",Ve=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),We=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(c){let S=[],l=0,h=1,d=0,r=()=>({line:h,column:d}),f=(v=0)=>c[l+v]??"",L=v=>{let C=r();throw new Z(v,C,l,{start:C,end:{line:h,column:d+1}},l+1)},y=()=>{let v=c[l++];return v==="\r"?(c[l]===`
2
+ `&&l++,h++,d=0):v===`
3
+ `||v==="\u2028"||v==="\u2029"?(h++,d=0):d++,v},s=v=>v===`
4
+ `||v==="\r"||v==="\u2028"||v==="\u2029",x=()=>{for(;;){let v=f();if(v===" "||v===" "||v==="\f"||v==="\v"||s(v))y();else if(v==="/"&&f(1)==="/")for(;l<c.length&&!s(f());)y();else if(v==="/"&&f(1)==="*")for(y(),y();;){if(l>=c.length&&L("Unterminated block comment"),f()==="*"&&f(1)==="/"){y(),y();break}y();}else break}},I=(v,C,R,N)=>{S.push({type:v,value:C,start:R,end:l,loc:{start:N,end:r()}}),$=v==="Punctuator"?Ve.has(C):v==="Keyword"?We.has(C):v==="TemplateHead"||v==="TemplateMiddle";},E=v=>{for(y();l<c.length;){let C=f();if(C===v)return y(),c.slice(a,l);if(s(C)&&L("Unterminated string literal"),C==="\\"){y(),l>=c.length&&L("Unterminated string literal"),y();continue}y();}return L("Unterminated string literal")},m=()=>{if(f()==="0"&&(f(1)==="x"||f(1)==="X")){for(y(),y(),de(f())||L("Invalid hexadecimal literal");de(f());)y();return}if(f()==="0"&&(f(1)==="b"||f(1)==="B")){for(y(),y(),f()!=="0"&&f()!=="1"&&L("Invalid binary literal");f()==="0"||f()==="1";)y();return}if(f()==="0"&&(f(1)==="o"||f(1)==="O")){for(y(),y(),f()>="0"&&f()<="7"||L("Invalid octal literal");f()>="0"&&f()<="7";)y();return}for(;B(f());)y();if(f()===".")for(y();B(f());)y();if(f()==="e"||f()==="E")for(y(),(f()==="+"||f()==="-")&&y(),B(f())||L("Invalid exponent");B(f());)y();},T=()=>{let v=l;for(;l<c.length;){let C=f();if(C==="`"){let R=c.slice(v,l);return y(),{raw:R,interp:false}}if(C==="$"&&f(1)==="{"){let R=c.slice(v,l);return y(),y(),{raw:R,interp:true}}if(C==="\\"){if(y(),l>=c.length)break;y();continue}y();}return L("Unterminated template literal")},a=0,J=0,k=[],$=true,X="js",K=[],F=[];for(;;){if(X==="jsxChildren"){a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(i==="<"){f(1)==="/"?(y(),y(),I("Punctuator","</",a,N),X="jsxCloseTag"):(y(),I("Punctuator","<",a,N),K.push("jsxChildren"),X="jsxOpenTag");continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:"jsxChildren"}),X="js";continue}for(;l<c.length&&f()!=="<"&&f()!=="{";)y();I("JSXText",c.slice(a,l),a,N);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){x(),a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(se(i)){for(y();qe(f());)y();I("Identifier",c.slice(a,l),a,N);continue}if(i==="/"&&f(1)===">"){y(),y(),I("Punctuator","/>",a,N),X=K.pop()??"js";continue}if(i===">"){y(),I("Punctuator",">",a,N),X=X==="jsxCloseTag"?K.pop()??"js":"jsxChildren";continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:X}),X="js";continue}if(i==='"'||i==="'"){I("String",E(i),a,N);continue}if(i==="="||i==="."||i===":"){y(),I("Punctuator",i,a,N);continue}L(`Unexpected character '${i}' in JSX tag`);}x(),a=l;let v=r();if(l>=c.length){I("EOF","",a,v);break}let C=f();if(se(C)){for(;me(f());)y();let N=c.slice(a,l);I(ye.has(N)?"Keyword":"Identifier",N,a,v);continue}if(B(C)||C==="."&&B(f(1))){m(),I("Number",c.slice(a,l),a,v);continue}if(C==='"'||C==="'"){let N=E(C);I("String",N,a,v);continue}if(C==="`"){y();let{raw:N,interp:i}=T();i?(I("TemplateHead",N,a,v),k.push(J)):I("Template",N,a,v);continue}if(C==="<"&&$){y(),I("Punctuator","<",a,v),K.push("js"),X="jsxOpenTag";continue}if(C==="{"){y(),I("Punctuator","{",a,v),J++;continue}if(C==="}"){if(k.length>0&&J===k[k.length-1]){k.pop(),y();let{raw:N,interp:i}=T();I(i?"TemplateMiddle":"TemplateTail",N,a,v),i&&k.push(J);continue}if(F.length>0&&J===F[F.length-1].level){let{restore:N}=F.pop();y(),I("Punctuator","}",a,v),X=N;continue}y(),I("Punctuator","}",a,v),J--;continue}if(C==="?"&&f(1)==="."&&B(f(2))){y(),I("Punctuator","?",a,v);continue}let R=ze.find(N=>c.startsWith(N,l));if(R){for(let N=0;N<R.length;N++)y();I("Punctuator",R,a,v);continue}L(`Unexpected character '${C}'`);}return S}var ie=(c=[])=>({vars:new Set(c),bounds:new Map,noCond:false,inferSink:null}),H=(c,S)=>{let l=new Set(c.vars),h=new Map(c.bounds);for(let d of S)l.add(d.name),d.bound||d.isConst?h.set(d.name,{bound:d.bound,isConst:d.isConst}):h.delete(d.name);return {vars:l,bounds:h,noCond:c.noCond,inferSink:c.inferSink}},He=(c,S)=>{let l=c.bounds.get(S);return l&&(l.bound||l.isConst)?{t:"var",name:S,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:S}},he=c=>{let{peek:S,next:l,isP:h,isK:d,expectP:r,fail:f,unescape:L}=c,y=()=>{let i=c.pos(),p=c.tokens[i],u=c.tokens[i-1];return !!u&&!!p&&p.loc.start.line>u.loc.end.line},s=()=>{let i=0;for(let p=c.pos();p<c.tokens.length;p++){let u=c.tokens[p];if(u.type==="EOF")return false;if(u.type==="Punctuator"){if(["(","[","{","<"].includes(u.value))i++;else if([")","]","}",">"].includes(u.value)&&(i--,i===0)){let b=c.tokens[p+1];return !!b&&b.type==="Punctuator"&&b.value==="=>"}}}return false},x=()=>{let i=c.tokens,p=[],u=0,b=false;for(let w=c.pos();w<i.length;w++){let j=i[w];if(j.type==="EOF")break;if(j.type==="Punctuator"){if(j.value==="<")u++,u===1&&(b=true);else if(j.value===">"){if(u--,u===0)break}else j.value==="("||j.value==="["||j.value==="{"?(u++,b=false):j.value===")"||j.value==="]"||j.value==="}"?(u--,b=false):j.value===","&&u===1?b=true:b=false;continue}if(u===1&&b){if(j.type==="Keyword"&&j.value==="const")continue;if(j.type==="Identifier"){p.push(j.value),b=false;continue}}b=false;}return p},I=i=>{let p=H(i,x().map(b=>({name:b})));r("<");let u=[];for(;!h(">");){let b=d("const");b&&l();let w=S();w.type!=="Identifier"&&f("Expected type parameter name"),l();let j;d("extends")&&(l(),j=T(p)),h("=")&&(l(),T(p)),u.push({name:w.value,...j?{bound:j}:{},...b?{isConst:true}:{}}),h(">")||r(",");}return r(">"),u},E=i=>{r("<");let p=[];for(;!h(">");)p.push(T(i)),h(">")||r(",");return r(">"),p},m=i=>{let p=S();return p.type==="Identifier"&&p.value===i},T=i=>{let p=a(i);if(i.noCond||!d("extends")||y())return p;l();let u=[],b=a({...i,noCond:true,inferSink:u});r("?");let w=H(i,u.map(z=>({name:z}))),j=T(w);r(":");let _=T(w);return {t:"conditional",check:p,extends:b,then:j,else:_,...u.length?{infer:u}:{}}},a=i=>{h("|")&&l();let p=J(i);if(!h("|"))return p;let u=[p];for(;h("|");)l(),u.push(J(i));return {t:"union",of:u}},J=i=>{h("&")&&l();let p=k(i);if(!h("&"))return p;let u=[p];for(;h("&");)l(),u.push(k(i));return {t:"intersection",of:u}},k=i=>{if(m("keyof"))return l(),{t:"keyof",of:k(i)};if(m("readonly")){l();let p=k(i);return p.t!=="array"&&p.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{...p,readonly:true}}if(m("infer")){l();let p=S();p.type!=="Identifier"&&f("Expected 'infer' variable name"),l();let u=i.inferSink;return u?(u.push(p.value),{t:"var",name:p.value}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return $(i)},$=i=>{let p=X(i);for(;h("[")&&!y();)if(l(),h("]"))l(),p={t:"array",items:p};else {let u=T(i);r("]"),p={t:"index",obj:p,key:u};}return p},X=i=>{if(d("typeof")){l();let u=S();u.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),l();let b=[u.value];for(;h(".");){l();let w=S();w.type!=="Identifier"&&w.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),l(),b.push(w.value);}return {t:"typeQuery",path:b}}if(h("<")){let u=I(i),b=H(i,u);return F(b,u.map(w=>w.name))}if(h("(")){if(s())return F(i);l();let u=T(i.noCond?{...i,noCond:false}:i);return r(")"),u}if(h("{"))return C()?R(i):N(i);if(h("["))return v(i);let p=S();if(p.type==="String")return l(),{t:"literal",value:L(p.value.slice(1,-1))};if(p.type==="Number")return l(),{t:"literal",value:Number(p.value)};if(d("true"))return l(),{t:"literal",value:true};if(d("false"))return l(),{t:"literal",value:false};if(d("null"))return l(),{t:"null"};if(d("void"))return l(),{t:"void"};if(p.type==="Identifier"){switch(l(),p.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"};case "never":return {t:"union",of:[]}}let u=h("<")?E(i):[];return i.vars.has(p.value)&&u.length===0?He(i,p.value):p.value==="ReturnType"&&u.length===1?{t:"returnType",fn:u[0]}:p.value==="Parameters"&&u.length===1?{t:"parameters",fn:u[0]}:(c.typeRefs?.push({name:p.value,loc:p.loc}),{t:"apply",name:p.value,args:u})}return f(`Expected a type but got '${p.value||p.type}'`)},K=i=>{r("(");let p=[];for(;!h(")");){let u=S();u.type!=="Identifier"&&f("Expected parameter name in function type"),l();let b=h("?");b&&l(),r(":");let w=T(i);p.push({name:u.value,type:w,...b?{optional:true}:{}}),h(")")||r(",");}return r(")"),p},F=(i,p)=>{let u=K(i);r("=>");let b=T(i);return {t:"fn",params:u,returns:b,...p?.length?{typeParams:p}:{}}},v=i=>{r("[");let p=[],u=[];for(;!h("]");){if(h("...")){l();let b=T(i);b.t==="array"?(p.push(b.items),u.push("rest")):(p.push(b),u.push("variadic"));}else {let b=T(i),w=h("?");w&&l(),p.push(b),u.push(w?"optional":"req");}h("]")||r(",");}return r("]"),u.some(b=>b!=="req")?{t:"tuple",elements:p,flags:u}:{t:"tuple",elements:p}},C=()=>{let i=c.tokens,p=c.pos()+1;return i[p]?.type==="Punctuator"&&(i[p]?.value==="+"||i[p]?.value==="-")&&p++,i[p]?.type==="Identifier"&&i[p]?.value==="readonly"&&p++,i[p]?.type==="Punctuator"&&i[p]?.value==="["&&i[p+1]?.type==="Identifier"&&i[p+2]?.type==="Keyword"&&i[p+2]?.value==="in"},R=i=>{r("{"),(h("+")||h("-"))&&l(),m("readonly")&&l(),r("[");let p=S();p.type!=="Identifier"&&f("Expected mapped type key name"),l(),d("in")||f("Expected 'in' in mapped type"),l();let u=T(i);m("as")&&f("Key remapping ('as') in mapped types is not supported"),r("]");let b=false,w=h("+")||h("-")?l().value:null;h("?")&&(l(),b=w!=="-"),r(":");let j=T(H(i,[{name:p.value}]));return (h(";")||h(","))&&l(),r("}"),{t:"mapped",key:p.value,over:u,body:j,optional:b}},N=i=>{r("{");let p={},u,b=[];for(;!h("}");){if(h("(")){let w=K(i);r(":");let j=T(i);b.push({t:"fn",params:w,returns:j});}else if(h("[")){l(),S().type!=="Identifier"&&f("Expected index signature parameter name"),l(),r(":");let w=S();(w.type!=="Identifier"||w.value!=="string"&&w.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),l(),r("]"),r(":"),u={key:w.value,value:T(i)};}else {let w=S(),j;if(w.type==="Identifier"||w.type==="Keyword")l(),j=w.value;else if(w.type==="String")l(),j=L(w.value.slice(1,-1));else if(w.type==="Number")l(),j=w.value;else return f("Expected property name in object type");let _=h("?");_&&l(),r(":");let z=T(i);p[j]={type:z,..._?{optional:true}:{}};}if(h(";")||h(","))l();else break}return r("}"),b.length>0?Object.keys(p).length>0||u?f("Callable object types (call signatures mixed with members) are not supported"):b.length===1?b[0]:{t:"overload",signatures:b}:{t:"object",props:p,...u?{index:u}:{}}};return {parseType:i=>T(ie(i)),parseTypeParamList:()=>I(ie()),parseTypeInScope:i=>T(H(ie(),i))}};var ee=class extends core.TSLiteError{constructor(l,h,d,r,f){super("parse-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="ParseError";}},Ye={"??":1,"||":2,"&&":3,"|":4,"^":5,"&":6,"==":7,"===":7,"!=":7,"!==":7,"<":8,"<=":8,">":8,">=":8,in:8,as:8,satisfies:8,"<<":9,">>":9,">>>":9,"+":10,"-":10,"*":11,"/":11,"%":11,"**":12},Qe=new Set(["??","||","&&"]),Ze=new Set(["class","debugger","with","enum","interface"]),et={enum:'use a union of literals (`type T = "a" | "b"`) \u2014 it is serializable',interface:"use a type alias (`type X = { \u2026 }`)",namespace:"use modules (`import`/`export`)",module:"use modules (`import`/`export`)",declare:"declare the shape via the DI env, or with a `type` alias",abstract:"classes are not part of the language \u2014 model with objects + functions"},tt={namespace:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",module:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",declare:c=>c.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(c.value)||c.type==="Identifier"&&["type","namespace","module"].includes(c.value),abstract:c=>c.type==="Keyword"&&c.value==="class"},nt=new Set(["=","+=","-=","*=","/=","%="]);function ge(c){let S=ae(c),l=0,h=[],d=(e=0)=>S[Math.min(l+e,S.length-1)],r=()=>S[l++],f=()=>S[l-1],L=()=>{let e=S[l],t=S[l-1];return !!e&&!!t&&e.loc.start.line>t.loc.end.line},y=()=>d().type==="EOF",s=e=>d().type==="Punctuator"&&d().value===e,x=e=>d().type==="Keyword"&&d().value===e,I=e=>d().type==="Identifier"&&d().value===e,E=(e,t=d())=>{throw new ee(e,t.loc.start,t.start,t.loc,t.end)},m=e=>(s(e)||E(`Expected '${e}' but got '${d().value||d().type}'`),r()),T=(e,t)=>{let n=f()??t;return Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}})},a=(e,t,n)=>Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}}),J=e=>{let t="";for(let n=0;n<e.length;n++){if(e[n]!=="\\"){t+=e[n];continue}let o=e[++n];switch(o){case "n":t+=`
5
+ `;break;case "t":t+=" ";break;case "r":t+="\r";break;case "b":t+="\b";break;case "f":t+="\f";break;case "v":t+="\v";break;case "0":t+="\0";break;case "x":t+=String.fromCharCode(parseInt(e.slice(n+1,n+3),16)),n+=2;break;case "u":if(e[n+1]==="{"){let g=e.indexOf("}",n);t+=String.fromCodePoint(parseInt(e.slice(n+2,g),16)),n=g;}else t+=String.fromCharCode(parseInt(e.slice(n+1,n+5),16)),n+=4;break;default:t+=o;}}return t},k=(e=false)=>$(e),$=(e=false)=>{let t=X(e),n=d();if(n.type==="Punctuator"&&nt.has(n.value)){r();let o=$();return a({type:"AssignmentExpression",operator:n.value,left:t,right:o},t,o)}return t},X=(e=false)=>{let t=F(0,e);if(!s("?"))return t;r();let n=k();m(":");let o=k();return a({type:"ConditionalExpression",test:t,consequent:n,alternate:o},t,o)},K=()=>{let e=d();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:e.value==="satisfies"?{op:"satisfies",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let t=d(1);if(t.type==="Punctuator"&&t.value===">"&&e.end===t.start){let n=d(2);return n.type==="Punctuator"&&n.value===">"&&t.end===n.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},F=(e,t=false)=>{let n=v();for(;;){let o=K();if(!o)break;let{op:g,consume:P}=o;if(g==="in"&&t)break;let A=Ye[g];if(!A||A<=e)break;if(g==="satisfies"){if(L())break;E("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(g==="as"){if(L())break;r();let D=V(new Set);n=a({type:"TSAsExpression",expression:n,typeAnnotation:D},n,f());continue}for(let D=0;D<P;D++)r();let O=F(g==="**"?A-1:A,t);n=a({type:Qe.has(g)?"LogicalExpression":"BinaryExpression",operator:g,left:n,right:O},n,O);}return n},v=()=>{let e=d();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(r(),T({type:"UpdateExpression",operator:e.value,prefix:true,argument:v()},e)):x("await")?(r(),T({type:"AwaitExpression",argument:v()},e)):x("typeof")||x("void")||x("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?C(e):N()},C=e=>{r();let t=v();return s("**")&&E(`An unary expression with the '${e.value}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.`),T({type:"UnaryExpression",operator:e.value,prefix:true,argument:t},e)},R=()=>{let e=l,t=()=>(l=e,void 0);try{if(r(),s(">"))return t();let n=[];for(;n.push(V(new Set)),!!s(",");)r();return s(">")?(r(),s("(")?n:t()):t()}catch{return t()}},N=()=>{let e=we(),t=false;for(;;)if(s("?."))if(r(),t=true,s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:true},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:true},e,o);}else {let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name after '?.'"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:true},e,o);}else if(s(".")){r();let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:false},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:false},e,o);}else if(s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:false},e,o);}else if(s("<")){let n=R();if(!n)break;let{args:o,close:g}=i();e=a({type:"CallExpression",callee:e,arguments:o,optional:false,typeArguments:n},e,g);}else if(s("++")||s("--")){let n=r();e=a({type:"UpdateExpression",operator:n.value,prefix:false,argument:e},e,n);}else break;return t?a({type:"ChainExpression",expression:e},e,e):e},i=()=>{m("(");let e=[];for(;!s(")");)e.push(p()),s(")")||m(",");return {args:e,close:m(")")}},p=()=>{if(s("...")){let e=r(),t=k();return a({type:"SpreadElement",argument:t},e,t)}return k()},u=()=>{let e=d();return e.type!=="Identifier"&&E("Expected identifier"),r(),T({type:"Identifier",name:e.value},e)},b=e=>{let t=s("{")?w(e):s("[")?j(e):u();return s(":")&&(r(),t.typeAnnotation=V(e)),t},w=e=>{let t=m("{"),n=[];for(;!s("}");){if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=u(),P,A=false;if(s(":")?(r(),P=b(e)):(P={...g},A=true),s("=")){r();let O=k();P=a({type:"AssignmentPattern",left:P,right:O},P,O);}n.push(a({type:"Property",key:g,value:P,kind:"init",shorthand:A,computed:false,method:false},g,P));}s("}")||m(",");}let o=m("}");return a({type:"ObjectPattern",properties:n},t,o)},j=e=>{let t=m("["),n=[];for(;!s("]");){if(s(",")){n.push(null),r();continue}if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=b(e);if(s("=")){r();let P=k();g=a({type:"AssignmentPattern",left:g,right:P},g,P);}n.push(g);}s("]")||m(",");}let o=m("]");return a({type:"ArrayPattern",elements:n},t,o)},_=()=>{let e=0;for(let t=l;t<S.length;t++){let n=S[t];if(n.type==="EOF")return false;if(n.type==="Punctuator"){if(n.value==="("||n.value==="["||n.value==="{")e++;else if((n.value===")"||n.value==="]"||n.value==="}")&&(e--,e===0)){let o=S[t+1];return !o||o.type!=="Punctuator"?false:o.value==="=>"?true:o.value===":"?z(t+2):false}}}return false},z=e=>{let t=0;for(let n=e;n<S.length;n++){let o=S[n];if(o.type==="EOF")return false;if(o.type==="Punctuator")if(["(","[","{","<"].includes(o.value))t++;else if([")","]","}",">"].includes(o.value)){if(t===0)return false;t--;}else {if(t===0&&o.value==="=>")return true;if(t===0&&(o.value===";"||o.value===","))return false}}return false},ve=(e=new Set)=>{m("(");let t=[];for(;!s(")");){if(s("...")){let n=r(),o=b(e);t.push(a({type:"RestElement",argument:o},n,o));}else {let n=b(e);if(s("=")){r();let o=k();n=a({type:"AssignmentPattern",left:n,right:o},n,o);}t.push(n);}s(")")||m(",");}return m(")"),t},te=(e,t)=>{let n=s("(")?ve():[u()],o;s(":")&&(r(),o=V(new Set)),m("=>");let g=s("{")?W():k();return T({type:"ArrowFunctionExpression",params:n,body:g,async:e,expression:g.type!=="BlockStatement",generator:false,...o?{returnType:o}:{}},t)},be=()=>{let e=m("["),t=[];for(;!s("]");){if(s(",")){r(),t.push(null);continue}t.push(p()),s("]")||m(",");}return a({type:"ArrayExpression",elements:t},e,m("]"))},Te=()=>{let e=m("{"),t=[];for(;!s("}");){if(s("...")){let n=r(),o=k();t.push(a({type:"SpreadElement",argument:o},n,o));}else {let n=d(),o,g=false;if(s("["))g=true,r(),o=k(),m("]");else if(n.type==="String")r(),o=T({type:"Literal",value:J(n.value.slice(1,-1)),raw:n.value},n);else if(n.type==="Number")r(),o=T({type:"Literal",value:Number(n.value),raw:n.value},n);else if(n.type==="Identifier"||n.type==="Keyword")r(),o=T({type:"Identifier",name:n.value},n);else return E("Invalid property key");if(s(":")){r();let P=k();t.push(a({type:"Property",key:o,value:P,kind:"init",method:false,shorthand:false,computed:g},n,P));}else t.push(a({type:"Property",key:o,value:o,kind:"init",method:false,shorthand:true,computed:false},n,o));}s("}")||m(",");}return a({type:"ObjectExpression",properties:t},e,m("}"))},G=(e,t)=>({type:"TemplateElement",value:{raw:e,cooked:J(e)},tail:t}),xe=e=>{r();let t=[G(e.value,false)],n=[];for(;;){n.push(k());let o=d();if(o.type==="TemplateMiddle")r(),t.push(G(o.value,false));else if(o.type==="TemplateTail"){r(),t.push(G(o.value,true));break}else E("Expected `}` to close template interpolation");}return T({type:"TemplateLiteral",quasis:t,expressions:n},e)},q=()=>{let e=d();return e.type!=="Identifier"&&E(`Expected JSX identifier but got '${e.value||e.type}'`),T({type:"JSXIdentifier",name:r().value},e)},le=()=>{let e=q();if(s(":")){r();let t=q();return a({type:"JSXNamespacedName",namespace:e,name:t},e,t)}for(;s(".");){r();let t=q();e=a({type:"JSXMemberExpression",object:e,property:t},e,t);}return e},M=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${M(e.namespace)}:${M(e.name)}`:`${M(e.object)}.${M(e.property)}`,Se=()=>{let e=d();if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(s("{")){let t=r(),n=$(),o=m("}");return a({type:"JSXExpressionContainer",expression:n},t,o)}return s("<")?ne():E(`Invalid JSX attribute value '${e.value||e.type}'`)},ke=()=>{let e=[];for(;!s(">")&&!s("/>")&&!y();){if(s("{")){let o=r();m("...");let g=$(),P=m("}");e.push(a({type:"JSXSpreadAttribute",argument:g},o,P));continue}let t=q();if(s(":")){r();let o=q();t=a({type:"JSXNamespacedName",namespace:t,name:o},t,o);}let n=null;s("=")&&(r(),n=Se()),e.push(a({type:"JSXAttribute",name:t,value:n},t,n??t));}return e},pe=()=>{let e=[];for(;!(s("</")||y());){let t=d();if(t.type==="JSXText")r(),e.push(T({type:"JSXText",value:t.value,raw:t.value},t));else if(s("{")){let n=r();if(s("}")){let o=r(),g=T({type:"JSXEmptyExpression"},n);e.push(a({type:"JSXExpressionContainer",expression:g},n,o));}else {let o=$(),g=m("}");e.push(a({type:"JSXExpressionContainer",expression:o},n,g));}}else s("<")?e.push(ne()):E(`Unexpected token in JSX children '${t.value||t.type}'`);}return e},ne=()=>{let e=m("<");if(s(">")){let Q=r(),oe=a({type:"JSXOpeningFragment"},e,Q),Ue=pe(),Me=m("</"),fe=m(">"),Be=a({type:"JSXClosingFragment"},Me,fe);return a({type:"JSXFragment",openingFragment:oe,children:Ue,closingFragment:Be},e,fe)}let t=le(),n=ke();if(s("/>")){let Q=r(),oe=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:true},e,Q);return a({type:"JSXElement",openingElement:oe,children:[],closingElement:null},e,Q)}let o=m(">"),g=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:false},e,o),P=pe(),A=m("</"),O=d(),D=le();M(D)!==M(t)&&E(`JSX closing tag </${M(D)}> does not match opening <${M(t)}>`,O);let Y=m(">"),Ke=a({type:"JSXClosingElement",name:D},A,Y);return a({type:"JSXElement",openingElement:g,children:P,closingElement:Ke},e,Y)},we=()=>{let e=d();if(x("async")){let t=d(1);return t.value==="("||t.type==="Identifier"?(r(),te(true,e)):E("Unexpected 'async'")}if(s("(")&&_()||e.type==="Identifier"&&d(1).type==="Punctuator"&&d(1).value==="=>")return te(false,e);if(e.type==="Number")return r(),T({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return r(),T({type:"TemplateLiteral",expressions:[],quasis:[G(e.value,true)]},e);if(e.type==="TemplateHead")return xe(e);if(e.type==="Keyword")return e.value==="true"?(r(),T({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(r(),T({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(r(),T({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?E("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?E("Anonymous functions are not supported \u2014 use an arrow function"):E(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return r(),T({type:"Identifier",name:e.value},e);if(s("(")){r();let t=k();return m(")"),t}return s("[")?be():s("{")?Te():s("<")?ne():E(`Unexpected token '${e.value||e.type}'`)},{parseType:V,parseTypeParamList:Ne,parseTypeInScope:Pe}=he({tokens:S,pos:()=>l,peek:d,next:r,isP:s,isK:x,expectP:m,fail:E,unescape:J,typeRefs:h}),Ee=()=>{let e=r(),t=u(),n=[],o=[];s("<")&&(o=Ne(),n=o.map(A=>A.name)),m("=");let g=Pe(o),P=a({type:"TSTypeAliasDeclaration",id:t,typeParameters:n,typeAnnotation:g},e,f());return s(";")&&r(),P},U=()=>{let e=d();if(e.type==="Identifier"&&e.value==="type"&&d(1).type==="Identifier"&&d(2).type==="Punctuator"&&(d(2).value==="="||d(2).value==="<"))return Ee();if(x("const")||x("let")||x("var")){let g=ce();return s(";")&&r(),g}if(x("if"))return Le();if(x("for"))return Ae();if(x("while"))return Je();if(x("do"))return Oe();if(x("break")||x("continue"))return De();if(x("return"))return Xe();if(x("throw"))return Re();if(x("try"))return Fe();if(x("switch"))return $e();if(x("import"))return Ce();if(x("export"))return je();if(s("{"))return W();if((x("function")||x("async")&&d(1).type==="Keyword"&&d(1).value==="function")&&E("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),s(";"))return r(),T({type:"EmptyStatement"},e);if(e.type==="Keyword"&&Ze.has(e.value)||e.type==="Identifier"&&tt[e.value]?.(d(1),d(2))===true){let g=et[e.value];E(`'${e.value}' is not supported in this language`+(g?` \u2014 ${g}`:""));}let n=k(),o=s(";")?r():f();return a({type:"ExpressionStatement",expression:n},n,o)},ce=()=>{let e=r(),t=e.value,n=b(new Set),o=null;s("=")?(r(),o=k()):t==="const"&&E("'const' declarations must be initialized");let g=a({type:"VariableDeclarator",id:n,init:o},n,o??n);return a({type:"VariableDeclaration",kind:t,declarations:[g]},e,o??n)},Ie=()=>{let e=d();return e.type!=="String"&&E("Expected module specifier string"),r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e)},Ce=()=>{let e=r(),t=[];if(d().type!=="String"){if(d().type==="Identifier"){let o=u();t.push(a({type:"ImportDefaultSpecifier",local:o},o,o)),s(",")&&r();}if(s("*")){let o=r();I("as")||E("Expected 'as' after '*' in import"),r();let g=u();t.push(a({type:"ImportNamespaceSpecifier",local:g},o,g));}else if(s("{")){for(r();!s("}")&&!y();){let o=u(),g=o;if(I("as")&&(r(),g=u()),t.push(a({type:"ImportSpecifier",imported:o,local:g},o,g)),s(","))r();else break}m("}");}I("from")||E("Expected 'from' in import declaration"),r();}let n=Ie();return s(";")&&r(),T({type:"ImportDeclaration",specifiers:t,source:n},e)},je=()=>{let e=r();if(x("default")){r();let t=k();return s(";")&&r(),T({type:"ExportDefaultDeclaration",declaration:t},e)}if(x("const")||x("let")||x("var")){let t=ce();return s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:t,specifiers:[],source:null},e)}if(s("{")){r();let t=[];for(;!s("}")&&!y();){let n=u(),o=n;if(I("as")&&(r(),o=u()),t.push(a({type:"ExportSpecifier",local:n,exported:o},n,o)),s(","))r();else break}return m("}"),s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:null,specifiers:t,source:null},e)}return E("Expected 'const', '{', or 'default' after 'export'")},ue=()=>d().type==="Identifier"&&d().value==="of",re=()=>x("in"),Ae=()=>{let e=r();m("(");let t=null,n=null;if(!s(";"))if(x("const")||x("let")||x("var")){let A=r(),O=u();if(s(":")&&(r(),O.typeAnnotation=V(new Set)),ue()||re()){let D=a({type:"VariableDeclarator",id:O,init:null},O,O);n=a({type:"VariableDeclaration",kind:A.value,declarations:[D]},A,O);}else {let D=null;s("=")?(r(),D=k(true)):A.value==="const"&&E("'const' declarations must be initialized");let Y=a({type:"VariableDeclarator",id:O,init:D},O,D??O);t=a({type:"VariableDeclaration",kind:A.value,declarations:[Y]},A,D??O);}}else {let A=k(true);ue()||re()?n=A:t=A;}if(n){let A=re();r();let O=k();m(")");let D=U();return a(A?{type:"ForInStatement",left:n,right:O,body:D}:{type:"ForOfStatement",left:n,right:O,body:D,await:false},e,D)}m(";");let o=s(";")?null:k();m(";");let g=s(")")?null:k();m(")");let P=U();return a({type:"ForStatement",init:t,test:o,update:g,body:P},e,P)},Oe=()=>{let e=r(),t=U();x("while")||E("Expected 'while' after the 'do' body"),r(),m("(");let n=k(),o=m(")");return s(";")&&r(),a({type:"DoWhileStatement",body:t,test:n},e,o)},De=()=>{let e=r(),t=e.value==="break"?"BreakStatement":"ContinueStatement",n=s(";")?r():f();return a({type:t,label:null},e,n)},Je=()=>{let e=r();m("(");let t=k();m(")");let n=U();return a({type:"WhileStatement",test:t,body:n},e,n)},Le=()=>{let e=r();m("(");let t=k();m(")");let n=U(),o=null;return x("else")&&(r(),o=U()),a({type:"IfStatement",test:t,consequent:n,alternate:o},e,o??n)},Xe=()=>{let e=r(),t=null;!s(";")&&!s("}")&&!y()&&(t=k());let n=s(";")?r():f();return a({type:"ReturnStatement",argument:t},e,n)},Re=()=>{let e=r(),t=k(),n=s(";")?r():f();return a({type:"ThrowStatement",argument:t},e,n)},Fe=()=>{let e=r(),t=W(),n=null,o=null;if(x("catch")){let g=r(),P=null;s("(")&&(r(),P=u(),m(")"));let A=W();n=a({type:"CatchClause",param:P,body:A},g,A);}return x("finally")&&(r(),o=W()),!n&&!o&&E("'try' requires a 'catch' or 'finally' clause"),a({type:"TryStatement",block:t,handler:n,finalizer:o},e,o??n??t)},$e=()=>{let e=r();m("(");let t=k();m(")"),m("{");let n=[];for(;!s("}")&&!y();){let g=d(),P=null;x("case")?(r(),P=k()):x("default")?r():E("Expected 'case' or 'default' in switch"),m(":");let A=[];for(;!s("}")&&!x("case")&&!x("default")&&!y();)A.push(U());n.push(a({type:"SwitchCase",test:P,consequent:A},g,f()??g));}let o=m("}");return a({type:"SwitchStatement",discriminant:t,cases:n},e,o)},W=()=>{let e=m("{"),t=[];for(;!s("}")&&!y();)t.push(U());return a({type:"BlockStatement",body:t},e,m("}"))};return {parseExpr:k,parseProgram:()=>{let e=d(),t=[];for(;!y();)t.push(U());return a({type:"Program",sourceType:"module",body:t,...h.length?{typeRefs:h}:{}},e,f()??e)},eof:y,fail:E}}function rt(c){return ge(c).parseProgram()}function ot(c){let{parseExpr:S,eof:l,fail:h}=ge(c),d=S();return l()||h("Unexpected token after expression"),d}exports.KEYWORDS=ye;exports.LexError=Z;exports.ParseError=ee;exports.parse=rt;exports.parseExpression=ot;exports.tokenize=ae;
package/dist/index.d.cts CHANGED
@@ -26,7 +26,21 @@ interface Token {
26
26
  declare class LexError extends TSLiteError<"lex-error"> {
27
27
  readonly position: Position;
28
28
  readonly index: number;
29
- constructor(message: string, position: Position, index: number);
29
+ /**
30
+ * RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
31
+ * vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
32
+ */
33
+ readonly loc?: SourceLocation | undefined;
34
+ /** Offset absoluto do fim (exclusivo). */
35
+ readonly end?: number | undefined;
36
+ constructor(message: string, position: Position, index: number,
37
+ /**
38
+ * RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
39
+ * vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
40
+ */
41
+ loc?: SourceLocation | undefined,
42
+ /** Offset absoluto do fim (exclusivo). */
43
+ end?: number | undefined);
30
44
  }
31
45
  /** Palavras reservadas. As permitidas e as proibidas — o parser decide. */
32
46
  declare const KEYWORDS: Set<string>;
@@ -37,7 +51,23 @@ declare function tokenize(source: string): Token[];
37
51
  declare class ParseError extends TSLiteError<"parse-error"> {
38
52
  readonly position: Position;
39
53
  readonly index: number;
40
- constructor(message: string, position: Position, index: number);
54
+ /**
55
+ * O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
56
+ * para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
57
+ * seguem sendo o início (compat).
58
+ */
59
+ readonly loc?: SourceLocation | undefined;
60
+ /** Offset absoluto do fim (exclusivo) do token culpado. */
61
+ readonly end?: number | undefined;
62
+ constructor(message: string, position: Position, index: number,
63
+ /**
64
+ * O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
65
+ * para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
66
+ * seguem sendo o início (compat).
67
+ */
68
+ loc?: SourceLocation | undefined,
69
+ /** Offset absoluto do fim (exclusivo) do token culpado. */
70
+ end?: number | undefined);
41
71
  }
42
72
  /** Faz o parse de uma fonte completa num `Program` (ESTree, com `loc`). */
43
73
  declare function parse(source: string): types.Program;
@@ -47,4 +77,18 @@ declare function parse(source: string): types.Program;
47
77
  */
48
78
  declare function parseExpression(source: string): types.Expression;
49
79
 
50
- export { KEYWORDS, LexError, ParseError, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, parse, parseExpression, tokenize };
80
+ /**
81
+ * Uma REFERÊNCIA a tipo nomeado, com posição — a tabela lateral que permite
82
+ * "ir para a definição" e hover sobre um nome de tipo.
83
+ *
84
+ * Por que lateral e não `loc` no `Schema`: o `Schema` é dado imutável e os caches
85
+ * do type-core são **por referência** (TC17). Pendurar posição faria dois tipos
86
+ * estruturalmente iguais passarem a diferir, quebrando dedup e cache em silêncio.
87
+ * Mesmo movimento do overlay: informação AO LADO, não dentro.
88
+ */
89
+ type TypeRef = {
90
+ readonly name: string;
91
+ readonly loc: SourceLocation;
92
+ };
93
+
94
+ export { KEYWORDS, LexError, ParseError, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, parse, parseExpression, tokenize };
package/dist/index.d.ts CHANGED
@@ -26,7 +26,21 @@ interface Token {
26
26
  declare class LexError extends TSLiteError<"lex-error"> {
27
27
  readonly position: Position;
28
28
  readonly index: number;
29
- constructor(message: string, position: Position, index: number);
29
+ /**
30
+ * RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
31
+ * vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
32
+ */
33
+ readonly loc?: SourceLocation | undefined;
34
+ /** Offset absoluto do fim (exclusivo). */
35
+ readonly end?: number | undefined;
36
+ constructor(message: string, position: Position, index: number,
37
+ /**
38
+ * RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
39
+ * vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
40
+ */
41
+ loc?: SourceLocation | undefined,
42
+ /** Offset absoluto do fim (exclusivo). */
43
+ end?: number | undefined);
30
44
  }
31
45
  /** Palavras reservadas. As permitidas e as proibidas — o parser decide. */
32
46
  declare const KEYWORDS: Set<string>;
@@ -37,7 +51,23 @@ declare function tokenize(source: string): Token[];
37
51
  declare class ParseError extends TSLiteError<"parse-error"> {
38
52
  readonly position: Position;
39
53
  readonly index: number;
40
- constructor(message: string, position: Position, index: number);
54
+ /**
55
+ * O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
56
+ * para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
57
+ * seguem sendo o início (compat).
58
+ */
59
+ readonly loc?: SourceLocation | undefined;
60
+ /** Offset absoluto do fim (exclusivo) do token culpado. */
61
+ readonly end?: number | undefined;
62
+ constructor(message: string, position: Position, index: number,
63
+ /**
64
+ * O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
65
+ * para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
66
+ * seguem sendo o início (compat).
67
+ */
68
+ loc?: SourceLocation | undefined,
69
+ /** Offset absoluto do fim (exclusivo) do token culpado. */
70
+ end?: number | undefined);
41
71
  }
42
72
  /** Faz o parse de uma fonte completa num `Program` (ESTree, com `loc`). */
43
73
  declare function parse(source: string): types.Program;
@@ -47,4 +77,18 @@ declare function parse(source: string): types.Program;
47
77
  */
48
78
  declare function parseExpression(source: string): types.Expression;
49
79
 
50
- export { KEYWORDS, LexError, ParseError, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, parse, parseExpression, tokenize };
80
+ /**
81
+ * Uma REFERÊNCIA a tipo nomeado, com posição — a tabela lateral que permite
82
+ * "ir para a definição" e hover sobre um nome de tipo.
83
+ *
84
+ * Por que lateral e não `loc` no `Schema`: o `Schema` é dado imutável e os caches
85
+ * do type-core são **por referência** (TC17). Pendurar posição faria dois tipos
86
+ * estruturalmente iguais passarem a diferir, quebrando dedup e cache em silêncio.
87
+ * Mesmo movimento do overlay: informação AO LADO, não dentro.
88
+ */
89
+ type TypeRef = {
90
+ readonly name: string;
91
+ readonly loc: SourceLocation;
92
+ };
93
+
94
+ export { KEYWORDS, LexError, ParseError, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, parse, parseExpression, tokenize };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import {TSLiteError}from'@tslite/core';var Q=class extends TSLiteError{constructor(l,i,o){super("lex-error",`${l} (${i.line}:${i.column})`,{position:i,index:o});this.position=i;this.index=o;this.name="LexError";}},de=new Set(["const","if","else","return","async","await","true","false","null","typeof","let","var","function","class","new","for","while","do","this","throw","try","catch","finally","switch","case","break","continue","default","void","delete","instanceof","in","yield","import","export","extends","super","debugger","with"]),Be=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],oe=u=>u>="a"&&u<="z"||u>="A"&&u<="Z"||u==="_"||u==="$",ye=u=>oe(u)||u>="0"&&u<="9",M=u=>u>="0"&&u<="9",fe=u=>M(u)||u>="a"&&u<="f"||u>="A"&&u<="F",qe=u=>ye(u)||u==="-",ze=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),Ve=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function se(u){let N=[],l=0,i=1,o=0,v=()=>({line:i,column:o}),d=(g=0)=>u[l+g]??"",A=g=>{throw new Q(g,v(),l)},r=()=>{let g=u[l++];return g==="\r"?(u[l]===`
2
- `&&l++,i++,o=0):g===`
3
- `||g==="\u2028"||g==="\u2029"?(i++,o=0):o++,g},b=g=>g===`
4
- `||g==="\r"||g==="\u2028"||g==="\u2029",K=()=>{for(;;){let g=d();if(g===" "||g===" "||g==="\f"||g==="\v"||b(g))r();else if(g==="/"&&d(1)==="/")for(;l<u.length&&!b(d());)r();else if(g==="/"&&d(1)==="*")for(r(),r();;){if(l>=u.length&&A("Unterminated block comment"),d()==="*"&&d(1)==="/"){r(),r();break}r();}else break}},m=(g,C,L,k)=>{N.push({type:g,value:C,start:L,end:l,loc:{start:k,end:v()}}),q=g==="Punctuator"?ze.has(C):g==="Keyword"?Ve.has(C):g==="TemplateHead"||g==="TemplateMiddle";},y=g=>{for(r();l<u.length;){let C=d();if(C===g)return r(),u.slice(S,l);if(b(C)&&A("Unterminated string literal"),C==="\\"){r(),l>=u.length&&A("Unterminated string literal"),r();continue}r();}return A("Unterminated string literal")},P=()=>{if(d()==="0"&&(d(1)==="x"||d(1)==="X")){for(r(),r(),fe(d())||A("Invalid hexadecimal literal");fe(d());)r();return}if(d()==="0"&&(d(1)==="b"||d(1)==="B")){for(r(),r(),d()!=="0"&&d()!=="1"&&A("Invalid binary literal");d()==="0"||d()==="1";)r();return}if(d()==="0"&&(d(1)==="o"||d(1)==="O")){for(r(),r(),d()>="0"&&d()<="7"||A("Invalid octal literal");d()>="0"&&d()<="7";)r();return}for(;M(d());)r();if(d()===".")for(r();M(d());)r();if(d()==="e"||d()==="E")for(r(),(d()==="+"||d()==="-")&&r(),M(d())||A("Invalid exponent");M(d());)r();},c=()=>{let g=l;for(;l<u.length;){let C=d();if(C==="`"){let L=u.slice(g,l);return r(),{raw:L,interp:false}}if(C==="$"&&d(1)==="{"){let L=u.slice(g,l);return r(),r(),{raw:L,interp:true}}if(C==="\\"){if(r(),l>=u.length)break;r();continue}r();}return A("Unterminated template literal")},S=0,x=0,X=[],q=true,D="js",$=[],F=[];for(;;){if(D==="jsxChildren"){S=l;let k=v();if(l>=u.length){m("EOF","",S,k);break}let a=d();if(a==="<"){d(1)==="/"?(r(),r(),m("Punctuator","</",S,k),D="jsxCloseTag"):(r(),m("Punctuator","<",S,k),$.push("jsxChildren"),D="jsxOpenTag");continue}if(a==="{"){r(),m("Punctuator","{",S,k),F.push({level:x,restore:"jsxChildren"}),D="js";continue}for(;l<u.length&&d()!=="<"&&d()!=="{";)r();m("JSXText",u.slice(S,l),S,k);continue}if(D==="jsxOpenTag"||D==="jsxCloseTag"){K(),S=l;let k=v();if(l>=u.length){m("EOF","",S,k);break}let a=d();if(oe(a)){for(r();qe(d());)r();m("Identifier",u.slice(S,l),S,k);continue}if(a==="/"&&d(1)===">"){r(),r(),m("Punctuator","/>",S,k),D=$.pop()??"js";continue}if(a===">"){r(),m("Punctuator",">",S,k),D=D==="jsxCloseTag"?$.pop()??"js":"jsxChildren";continue}if(a==="{"){r(),m("Punctuator","{",S,k),F.push({level:x,restore:D}),D="js";continue}if(a==='"'||a==="'"){m("String",y(a),S,k);continue}if(a==="="||a==="."||a===":"){r(),m("Punctuator",a,S,k);continue}A(`Unexpected character '${a}' in JSX tag`);}K(),S=l;let g=v();if(l>=u.length){m("EOF","",S,g);break}let C=d();if(oe(C)){for(;ye(d());)r();let k=u.slice(S,l);m(de.has(k)?"Keyword":"Identifier",k,S,g);continue}if(M(C)||C==="."&&M(d(1))){P(),m("Number",u.slice(S,l),S,g);continue}if(C==='"'||C==="'"){let k=y(C);m("String",k,S,g);continue}if(C==="`"){r();let{raw:k,interp:a}=c();a?(m("TemplateHead",k,S,g),X.push(x)):m("Template",k,S,g);continue}if(C==="<"&&q){r(),m("Punctuator","<",S,g),$.push("js"),D="jsxOpenTag";continue}if(C==="{"){r(),m("Punctuator","{",S,g),x++;continue}if(C==="}"){if(X.length>0&&x===X[X.length-1]){X.pop(),r();let{raw:k,interp:a}=c();m(a?"TemplateMiddle":"TemplateTail",k,S,g),a&&X.push(x);continue}if(F.length>0&&x===F[F.length-1].level){let{restore:k}=F.pop();r(),m("Punctuator","}",S,g),D=k;continue}r(),m("Punctuator","}",S,g),x--;continue}if(C==="?"&&d(1)==="."&&M(d(2))){r(),m("Punctuator","?",S,g);continue}let L=Be.find(k=>u.startsWith(k,l));if(L){for(let k=0;k<L.length;k++)r();m("Punctuator",L,S,g);continue}A(`Unexpected character '${C}'`);}return N}var ae=(u=[])=>({vars:new Set(u),bounds:new Map,noCond:false,inferSink:null}),_=(u,N)=>{let l=new Set(u.vars),i=new Map(u.bounds);for(let o of N)l.add(o.name),o.bound||o.isConst?i.set(o.name,{bound:o.bound,isConst:o.isConst}):i.delete(o.name);return {vars:l,bounds:i,noCond:u.noCond,inferSink:u.inferSink}},We=(u,N)=>{let l=u.bounds.get(N);return l&&(l.bound||l.isConst)?{t:"var",name:N,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:N}},me=u=>{let{peek:N,next:l,isP:i,isK:o,expectP:v,fail:d,unescape:A}=u,r=()=>{let a=u.pos(),p=u.tokens[a],f=u.tokens[a-1];return !!f&&!!p&&p.loc.start.line>f.loc.end.line},b=()=>{let a=0;for(let p=u.pos();p<u.tokens.length;p++){let f=u.tokens[p];if(f.type==="EOF")return false;if(f.type==="Punctuator"){if(["(","[","{","<"].includes(f.value))a++;else if([")","]","}",">"].includes(f.value)&&(a--,a===0)){let T=u.tokens[p+1];return !!T&&T.type==="Punctuator"&&T.value==="=>"}}}return false},K=()=>{let a=u.tokens,p=[],f=0,T=false;for(let E=u.pos();E<a.length;E++){let I=a[E];if(I.type==="EOF")break;if(I.type==="Punctuator"){if(I.value==="<")f++,f===1&&(T=true);else if(I.value===">"){if(f--,f===0)break}else I.value==="("||I.value==="["||I.value==="{"?(f++,T=false):I.value===")"||I.value==="]"||I.value==="}"?(f--,T=false):I.value===","&&f===1?T=true:T=false;continue}if(f===1&&T){if(I.type==="Keyword"&&I.value==="const")continue;if(I.type==="Identifier"){p.push(I.value),T=false;continue}}T=false;}return p},m=a=>{let p=_(a,K().map(T=>({name:T})));v("<");let f=[];for(;!i(">");){let T=o("const");T&&l();let E=N();E.type!=="Identifier"&&d("Expected type parameter name"),l();let I;o("extends")&&(l(),I=c(p)),i("=")&&(l(),c(p)),f.push({name:E.value,...I?{bound:I}:{},...T?{isConst:true}:{}}),i(">")||v(",");}return v(">"),f},y=a=>{v("<");let p=[];for(;!i(">");)p.push(c(a)),i(">")||v(",");return v(">"),p},P=a=>{let p=N();return p.type==="Identifier"&&p.value===a},c=a=>{let p=S(a);if(a.noCond||!o("extends")||r())return p;l();let f=[],T=S({...a,noCond:true,inferSink:f});v("?");let E=_(a,f.map(B=>({name:B}))),I=c(E);v(":");let z=c(E);return {t:"conditional",check:p,extends:T,then:I,else:z,...f.length?{infer:f}:{}}},S=a=>{i("|")&&l();let p=x(a);if(!i("|"))return p;let f=[p];for(;i("|");)l(),f.push(x(a));return {t:"union",of:f}},x=a=>{i("&")&&l();let p=X(a);if(!i("&"))return p;let f=[p];for(;i("&");)l(),f.push(X(a));return {t:"intersection",of:f}},X=a=>{if(P("keyof"))return l(),{t:"keyof",of:X(a)};if(P("infer")){l();let p=N();p.type!=="Identifier"&&d("Expected 'infer' variable name"),l();let f=a.inferSink;return f?(f.push(p.value),{t:"var",name:p.value}):d("'infer' is only allowed in the 'extends' clause of a conditional")}return q(a)},q=a=>{let p=D(a);for(;i("[")&&!r();)if(l(),i("]"))l(),p={t:"array",items:p};else {let f=c(a);v("]"),p={t:"index",obj:p,key:f};}return p},D=a=>{if(i("<")){let f=m(a),T=_(a,f);return F(T)}if(i("(")){if(b())return F(a);l();let f=c(a.noCond?{...a,noCond:false}:a);return v(")"),f}if(i("{"))return C()?L(a):k(a);if(i("["))return g(a);let p=N();if(p.type==="String")return l(),{t:"literal",value:A(p.value.slice(1,-1))};if(p.type==="Number")return l(),{t:"literal",value:Number(p.value)};if(o("true"))return l(),{t:"literal",value:true};if(o("false"))return l(),{t:"literal",value:false};if(o("null"))return l(),{t:"null"};if(o("void"))return l(),{t:"void"};if(p.type==="Identifier"){switch(l(),p.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"};case "never":return {t:"union",of:[]}}let f=i("<")?y(a):[];return a.vars.has(p.value)&&f.length===0?We(a,p.value):p.value==="ReturnType"&&f.length===1?{t:"returnType",fn:f[0]}:p.value==="Parameters"&&f.length===1?{t:"parameters",fn:f[0]}:{t:"apply",name:p.value,args:f}}return d(`Expected a type but got '${p.value||p.type}'`)},$=a=>{v("(");let p=[];for(;!i(")");){let f=N();f.type!=="Identifier"&&d("Expected parameter name in function type"),l();let T=i("?");T&&l(),v(":");let E=c(a);p.push({name:f.value,type:E,...T?{optional:true}:{}}),i(")")||v(",");}return v(")"),p},F=a=>{let p=$(a);v("=>");let f=c(a);return {t:"fn",params:p,returns:f}},g=a=>{v("[");let p=[],f=[];for(;!i("]");){if(i("...")){l();let T=c(a);T.t==="array"?(p.push(T.items),f.push("rest")):(p.push(T),f.push("variadic"));}else {let T=c(a),E=i("?");E&&l(),p.push(T),f.push(E?"optional":"req");}i("]")||v(",");}return v("]"),f.some(T=>T!=="req")?{t:"tuple",elements:p,flags:f}:{t:"tuple",elements:p}},C=()=>{let a=u.tokens,p=u.pos()+1;return a[p]?.type==="Punctuator"&&(a[p]?.value==="+"||a[p]?.value==="-")&&p++,a[p]?.type==="Identifier"&&a[p]?.value==="readonly"&&p++,a[p]?.type==="Punctuator"&&a[p]?.value==="["&&a[p+1]?.type==="Identifier"&&a[p+2]?.type==="Keyword"&&a[p+2]?.value==="in"},L=a=>{v("{"),(i("+")||i("-"))&&l(),P("readonly")&&l(),v("[");let p=N();p.type!=="Identifier"&&d("Expected mapped type key name"),l(),o("in")||d("Expected 'in' in mapped type"),l();let f=c(a);P("as")&&d("Key remapping ('as') in mapped types is not supported"),v("]");let T=false,E=i("+")||i("-")?l().value:null;i("?")&&(l(),T=E!=="-"),v(":");let I=c(_(a,[{name:p.value}]));return (i(";")||i(","))&&l(),v("}"),{t:"mapped",key:p.value,over:f,body:I,optional:T}},k=a=>{v("{");let p={},f,T=[];for(;!i("}");){if(i("(")){let E=$(a);v(":");let I=c(a);T.push({t:"fn",params:E,returns:I});}else if(i("[")){l(),N().type!=="Identifier"&&d("Expected index signature parameter name"),l(),v(":");let E=N();(E.type!=="Identifier"||E.value!=="string"&&E.value!=="number")&&d("Index signature parameter type must be 'string' or 'number'"),l(),v("]"),v(":"),f={key:E.value,value:c(a)};}else {let E=N(),I;if(E.type==="Identifier"||E.type==="Keyword")l(),I=E.value;else if(E.type==="String")l(),I=A(E.value.slice(1,-1));else if(E.type==="Number")l(),I=E.value;else return d("Expected property name in object type");let z=i("?");z&&l(),v(":");let B=c(a);p[I]={type:B,...z?{optional:true}:{}};}if(i(";")||i(","))l();else break}return v("}"),T.length>0?Object.keys(p).length>0||f?d("Callable object types (call signatures mixed with members) are not supported"):T.length===1?T[0]:{t:"overload",signatures:T}:{t:"object",props:p,...f?{index:f}:{}}};return {parseType:a=>c(ae(a)),parseTypeParamList:()=>m(ae()),parseTypeInScope:a=>c(_(ae(),a))}};var ee=class extends TSLiteError{constructor(l,i,o){super("parse-error",`${l} (${i.line}:${i.column})`,{position:i,index:o});this.position=i;this.index=o;this.name="ParseError";}},Ge={"??":1,"||":2,"&&":3,"|":4,"^":5,"&":6,"==":7,"===":7,"!=":7,"!==":7,"<":8,"<=":8,">":8,">=":8,in:8,as:8,"<<":9,">>":9,">>>":9,"+":10,"-":10,"*":11,"/":11,"%":11,"**":12},He=new Set(["??","||","&&"]),Ye=new Set(["class","debugger","with"]),Ze=new Set(["=","+=","-=","*=","/=","%="]);function he(u){let N=se(u),l=0,i=(e=0)=>N[Math.min(l+e,N.length-1)],o=()=>N[l++],v=()=>N[l-1],d=()=>{let e=N[l],t=N[l-1];return !!e&&!!t&&e.loc.start.line>t.loc.end.line},A=()=>i().type==="EOF",r=e=>i().type==="Punctuator"&&i().value===e,b=e=>i().type==="Keyword"&&i().value===e,K=e=>i().type==="Identifier"&&i().value===e,m=(e,t=i())=>{throw new ee(e,t.loc.start,t.start)},y=e=>(r(e)||m(`Expected '${e}' but got '${i().value||i().type}'`),o()),P=(e,t)=>{let n=v()??t;return Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}})},c=(e,t,n)=>Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}}),S=e=>{let t="";for(let n=0;n<e.length;n++){if(e[n]!=="\\"){t+=e[n];continue}let s=e[++n];switch(s){case "n":t+=`
5
- `;break;case "t":t+=" ";break;case "r":t+="\r";break;case "b":t+="\b";break;case "f":t+="\f";break;case "v":t+="\v";break;case "0":t+="\0";break;case "x":t+=String.fromCharCode(parseInt(e.slice(n+1,n+3),16)),n+=2;break;case "u":if(e[n+1]==="{"){let h=e.indexOf("}",n);t+=String.fromCodePoint(parseInt(e.slice(n+2,h),16)),n=h;}else t+=String.fromCharCode(parseInt(e.slice(n+1,n+5),16)),n+=4;break;default:t+=s;}}return t},x=(e=false)=>X(e),X=(e=false)=>{let t=q(e),n=i();if(n.type==="Punctuator"&&Ze.has(n.value)){o();let s=X();return c({type:"AssignmentExpression",operator:n.value,left:t,right:s},t,s)}return t},q=(e=false)=>{let t=$(0,e);if(!r("?"))return t;o();let n=x();y(":");let s=x();return c({type:"ConditionalExpression",test:t,consequent:n,alternate:s},t,s)},D=()=>{let e=i();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let t=i(1);if(t.type==="Punctuator"&&t.value===">"&&e.end===t.start){let n=i(2);return n.type==="Punctuator"&&n.value===">"&&t.end===n.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},$=(e,t=false)=>{let n=F();for(;;){let s=D();if(!s)break;let{op:h,consume:w}=s;if(h==="in"&&t)break;let j=Ge[h];if(!j||j<=e)break;if(h==="as"){if(d())break;o();let J=H(new Set);n=c({type:"TSAsExpression",expression:n,typeAnnotation:J},n,v());continue}for(let J=0;J<w;J++)o();let O=$(h==="**"?j-1:j,t);n=c({type:He.has(h)?"LogicalExpression":"BinaryExpression",operator:h,left:n,right:O},n,O);}return n},F=()=>{let e=i();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(o(),P({type:"UpdateExpression",operator:e.value,prefix:true,argument:F()},e)):b("await")?(o(),P({type:"AwaitExpression",argument:F()},e)):b("typeof")||b("void")||b("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?g(e):C()},g=e=>{o();let t=F();return r("**")&&m(`An unary expression with the '${e.value}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.`),P({type:"UnaryExpression",operator:e.value,prefix:true,argument:t},e)},C=()=>{let e=Te(),t=false;for(;;)if(r("?."))if(o(),t=true,r("(")){let{args:n,close:s}=L();e=c({type:"CallExpression",callee:e,arguments:n,optional:true},e,s);}else if(r("[")){o();let n=x(),s=y("]");e=c({type:"MemberExpression",object:e,property:n,computed:true,optional:true},e,s);}else {let n=i();n.type!=="Identifier"&&n.type!=="Keyword"&&m("Expected property name after '?.'"),o();let s=P({type:"Identifier",name:n.value},n);e=c({type:"MemberExpression",object:e,property:s,computed:false,optional:true},e,s);}else if(r(".")){o();let n=i();n.type!=="Identifier"&&n.type!=="Keyword"&&m("Expected property name"),o();let s=P({type:"Identifier",name:n.value},n);e=c({type:"MemberExpression",object:e,property:s,computed:false,optional:false},e,s);}else if(r("[")){o();let n=x(),s=y("]");e=c({type:"MemberExpression",object:e,property:n,computed:true,optional:false},e,s);}else if(r("(")){let{args:n,close:s}=L();e=c({type:"CallExpression",callee:e,arguments:n,optional:false},e,s);}else if(r("++")||r("--")){let n=o();e=c({type:"UpdateExpression",operator:n.value,prefix:false,argument:e},e,n);}else break;return t?c({type:"ChainExpression",expression:e},e,e):e},L=()=>{y("(");let e=[];for(;!r(")");)e.push(k()),r(")")||y(",");return {args:e,close:y(")")}},k=()=>{if(r("...")){let e=o(),t=x();return c({type:"SpreadElement",argument:t},e,t)}return x()},a=()=>{let e=i();return e.type!=="Identifier"&&m("Expected identifier"),o(),P({type:"Identifier",name:e.value},e)},p=e=>{let t=r("{")?f(e):r("[")?T(e):a();return r(":")&&(o(),t.typeAnnotation=H(e)),t},f=e=>{let t=y("{"),n=[];for(;!r("}");){if(r("...")){let h=o(),w=p(e);n.push(c({type:"RestElement",argument:w},h,w));}else {let h=a(),w,j=false;if(r(":")?(o(),w=p(e)):(w={...h},j=true),r("=")){o();let O=x();w=c({type:"AssignmentPattern",left:w,right:O},w,O);}n.push(c({type:"Property",key:h,value:w,kind:"init",shorthand:j,computed:false,method:false},h,w));}r("}")||y(",");}let s=y("}");return c({type:"ObjectPattern",properties:n},t,s)},T=e=>{let t=y("["),n=[];for(;!r("]");){if(r(",")){n.push(null),o();continue}if(r("...")){let h=o(),w=p(e);n.push(c({type:"RestElement",argument:w},h,w));}else {let h=p(e);if(r("=")){o();let w=x();h=c({type:"AssignmentPattern",left:h,right:w},h,w);}n.push(h);}r("]")||y(",");}let s=y("]");return c({type:"ArrayPattern",elements:n},t,s)},E=()=>{let e=0;for(let t=l;t<N.length;t++){let n=N[t];if(n.type==="EOF")return false;if(n.type==="Punctuator"){if(n.value==="("||n.value==="["||n.value==="{")e++;else if((n.value===")"||n.value==="]"||n.value==="}")&&(e--,e===0)){let s=N[t+1];return !s||s.type!=="Punctuator"?false:s.value==="=>"?true:s.value===":"?I(t+2):false}}}return false},I=e=>{let t=0;for(let n=e;n<N.length;n++){let s=N[n];if(s.type==="EOF")return false;if(s.type==="Punctuator")if(["(","[","{","<"].includes(s.value))t++;else if([")","]","}",">"].includes(s.value)){if(t===0)return false;t--;}else {if(t===0&&s.value==="=>")return true;if(t===0&&(s.value===";"||s.value===","))return false}}return false},z=(e=new Set)=>{y("(");let t=[];for(;!r(")");){if(r("...")){let n=o(),s=p(e);t.push(c({type:"RestElement",argument:s},n,s));}else {let n=p(e);if(r("=")){o();let s=x();n=c({type:"AssignmentPattern",left:n,right:s},n,s);}t.push(n);}r(")")||y(",");}return y(")"),t},B=(e,t)=>{let n=r("(")?z():[a()],s;r(":")&&(o(),s=H(new Set)),y("=>");let h=r("{")?W():x();return P({type:"ArrowFunctionExpression",params:n,body:h,async:e,expression:h.type!=="BlockStatement",generator:false,...s?{returnType:s}:{}},t)},ge=()=>{let e=y("["),t=[];for(;!r("]");){if(r(",")){o(),t.push(null);continue}t.push(k()),r("]")||y(",");}return c({type:"ArrayExpression",elements:t},e,y("]"))},ve=()=>{let e=y("{"),t=[];for(;!r("}");){if(r("...")){let n=o(),s=x();t.push(c({type:"SpreadElement",argument:s},n,s));}else {let n=i(),s,h=false;if(r("["))h=true,o(),s=x(),y("]");else if(n.type==="String")o(),s=P({type:"Literal",value:S(n.value.slice(1,-1)),raw:n.value},n);else if(n.type==="Number")o(),s=P({type:"Literal",value:Number(n.value),raw:n.value},n);else if(n.type==="Identifier"||n.type==="Keyword")o(),s=P({type:"Identifier",name:n.value},n);else return m("Invalid property key");if(r(":")){o();let w=x();t.push(c({type:"Property",key:s,value:w,kind:"init",method:false,shorthand:false,computed:h},n,w));}else t.push(c({type:"Property",key:s,value:s,kind:"init",method:false,shorthand:true,computed:false},n,s));}r("}")||y(",");}return c({type:"ObjectExpression",properties:t},e,y("}"))},G=(e,t)=>({type:"TemplateElement",value:{raw:e,cooked:S(e)},tail:t}),be=e=>{o();let t=[G(e.value,false)],n=[];for(;;){n.push(x());let s=i();if(s.type==="TemplateMiddle")o(),t.push(G(s.value,false));else if(s.type==="TemplateTail"){o(),t.push(G(s.value,true));break}else m("Expected `}` to close template interpolation");}return P({type:"TemplateLiteral",quasis:t,expressions:n},e)},V=()=>{let e=i();return e.type!=="Identifier"&&m(`Expected JSX identifier but got '${e.value||e.type}'`),P({type:"JSXIdentifier",name:o().value},e)},ie=()=>{let e=V();if(r(":")){o();let t=V();return c({type:"JSXNamespacedName",namespace:e,name:t},e,t)}for(;r(".");){o();let t=V();e=c({type:"JSXMemberExpression",object:e,property:t},e,t);}return e},U=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${U(e.namespace)}:${U(e.name)}`:`${U(e.object)}.${U(e.property)}`,Se=()=>{let e=i();if(e.type==="String")return o(),P({type:"Literal",value:S(e.value.slice(1,-1)),raw:e.value},e);if(r("{")){let t=o(),n=X(),s=y("}");return c({type:"JSXExpressionContainer",expression:n},t,s)}return r("<")?te():m(`Invalid JSX attribute value '${e.value||e.type}'`)},xe=()=>{let e=[];for(;!r(">")&&!r("/>")&&!A();){if(r("{")){let s=o();y("...");let h=X(),w=y("}");e.push(c({type:"JSXSpreadAttribute",argument:h},s,w));continue}let t=V();if(r(":")){o();let s=V();t=c({type:"JSXNamespacedName",namespace:t,name:s},t,s);}let n=null;r("=")&&(o(),n=Se()),e.push(c({type:"JSXAttribute",name:t,value:n},t,n??t));}return e},le=()=>{let e=[];for(;!(r("</")||A());){let t=i();if(t.type==="JSXText")o(),e.push(P({type:"JSXText",value:t.value,raw:t.value},t));else if(r("{")){let n=o();if(r("}")){let s=o(),h=P({type:"JSXEmptyExpression"},n);e.push(c({type:"JSXExpressionContainer",expression:h},n,s));}else {let s=X(),h=y("}");e.push(c({type:"JSXExpressionContainer",expression:s},n,h));}}else r("<")?e.push(te()):m(`Unexpected token in JSX children '${t.value||t.type}'`);}return e},te=()=>{let e=y("<");if(r(">")){let Z=o(),re=c({type:"JSXOpeningFragment"},e,Z),Ke=le(),Re=y("</"),ue=y(">"),Ue=c({type:"JSXClosingFragment"},Re,ue);return c({type:"JSXFragment",openingFragment:re,children:Ke,closingFragment:Ue},e,ue)}let t=ie(),n=xe();if(r("/>")){let Z=o(),re=c({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:true},e,Z);return c({type:"JSXElement",openingElement:re,children:[],closingElement:null},e,Z)}let s=y(">"),h=c({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:false},e,s),w=le(),j=y("</"),O=i(),J=ie();U(J)!==U(t)&&m(`JSX closing tag </${U(J)}> does not match opening <${U(t)}>`,O);let Y=y(">"),$e=c({type:"JSXClosingElement",name:J},j,Y);return c({type:"JSXElement",openingElement:h,children:w,closingElement:$e},e,Y)},Te=()=>{let e=i();if(b("async")){let t=i(1);return t.value==="("||t.type==="Identifier"?(o(),B(true,e)):m("Unexpected 'async'")}if(r("(")&&E()||e.type==="Identifier"&&i(1).type==="Punctuator"&&i(1).value==="=>")return B(false,e);if(e.type==="Number")return o(),P({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return o(),P({type:"Literal",value:S(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return o(),P({type:"TemplateLiteral",expressions:[],quasis:[G(e.value,true)]},e);if(e.type==="TemplateHead")return be(e);if(e.type==="Keyword")return e.value==="true"?(o(),P({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(o(),P({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(o(),P({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?m("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?m("Anonymous functions are not supported \u2014 use an arrow function"):m(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return o(),P({type:"Identifier",name:e.value},e);if(r("(")){o();let t=x();return y(")"),t}return r("[")?ge():r("{")?ve():r("<")?te():m(`Unexpected token '${e.value||e.type}'`)},{parseType:H,parseTypeParamList:ke,parseTypeInScope:we}=me({tokens:N,pos:()=>l,peek:i,next:o,isP:r,isK:b,expectP:y,fail:m,unescape:S}),Ne=()=>{let e=o(),t=a(),n=[],s=[];r("<")&&(s=ke(),n=s.map(j=>j.name)),y("=");let h=we(s),w=c({type:"TSTypeAliasDeclaration",id:t,typeParameters:n,typeAnnotation:h},e,v());return r(";")&&o(),w},R=()=>{let e=i();if(e.type==="Identifier"&&e.value==="type"&&i(1).type==="Identifier"&&i(2).type==="Punctuator"&&(i(2).value==="="||i(2).value==="<"))return Ne();if(b("const")||b("let")||b("var")){let s=pe();return r(";")&&o(),s}if(b("if"))return Je();if(b("for"))return Ce();if(b("while"))return Oe();if(b("do"))return je();if(b("break")||b("continue"))return Ae();if(b("return"))return De();if(b("throw"))return Xe();if(b("try"))return Fe();if(b("switch"))return Le();if(b("import"))return Ee();if(b("export"))return Ie();if(r("{"))return W();if((b("function")||b("async")&&i(1).type==="Keyword"&&i(1).value==="function")&&m("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),r(";"))return o(),P({type:"EmptyStatement"},e);e.type==="Keyword"&&Ye.has(e.value)&&m(`'${e.value}' is not supported in this language`);let t=x(),n=r(";")?o():v();return c({type:"ExpressionStatement",expression:t},t,n)},pe=()=>{let e=o(),t=e.value,n=p(new Set),s=null;r("=")?(o(),s=x()):t==="const"&&m("'const' declarations must be initialized");let h=c({type:"VariableDeclarator",id:n,init:s},n,s??n);return c({type:"VariableDeclaration",kind:t,declarations:[h]},e,s??n)},Pe=()=>{let e=i();return e.type!=="String"&&m("Expected module specifier string"),o(),P({type:"Literal",value:S(e.value.slice(1,-1)),raw:e.value},e)},Ee=()=>{let e=o(),t=[];if(i().type!=="String"){if(i().type==="Identifier"){let s=a();t.push(c({type:"ImportDefaultSpecifier",local:s},s,s)),r(",")&&o();}if(r("*")){let s=o();K("as")||m("Expected 'as' after '*' in import"),o();let h=a();t.push(c({type:"ImportNamespaceSpecifier",local:h},s,h));}else if(r("{")){for(o();!r("}")&&!A();){let s=a(),h=s;if(K("as")&&(o(),h=a()),t.push(c({type:"ImportSpecifier",imported:s,local:h},s,h)),r(","))o();else break}y("}");}K("from")||m("Expected 'from' in import declaration"),o();}let n=Pe();return r(";")&&o(),P({type:"ImportDeclaration",specifiers:t,source:n},e)},Ie=()=>{let e=o();if(b("default")){o();let t=x();return r(";")&&o(),P({type:"ExportDefaultDeclaration",declaration:t},e)}if(b("const")||b("let")||b("var")){let t=pe();return r(";")&&o(),P({type:"ExportNamedDeclaration",declaration:t,specifiers:[],source:null},e)}if(r("{")){o();let t=[];for(;!r("}")&&!A();){let n=a(),s=n;if(K("as")&&(o(),s=a()),t.push(c({type:"ExportSpecifier",local:n,exported:s},n,s)),r(","))o();else break}return y("}"),r(";")&&o(),P({type:"ExportNamedDeclaration",declaration:null,specifiers:t,source:null},e)}return m("Expected 'const', '{', or 'default' after 'export'")},ce=()=>i().type==="Identifier"&&i().value==="of",ne=()=>b("in"),Ce=()=>{let e=o();y("(");let t=null,n=null;if(!r(";"))if(b("const")||b("let")||b("var")){let j=o(),O=a();if(r(":")&&(o(),O.typeAnnotation=H(new Set)),ce()||ne()){let J=c({type:"VariableDeclarator",id:O,init:null},O,O);n=c({type:"VariableDeclaration",kind:j.value,declarations:[J]},j,O);}else {let J=null;r("=")?(o(),J=x(true)):j.value==="const"&&m("'const' declarations must be initialized");let Y=c({type:"VariableDeclarator",id:O,init:J},O,J??O);t=c({type:"VariableDeclaration",kind:j.value,declarations:[Y]},j,J??O);}}else {let j=x(true);ce()||ne()?n=j:t=j;}if(n){let j=ne();o();let O=x();y(")");let J=R();return c(j?{type:"ForInStatement",left:n,right:O,body:J}:{type:"ForOfStatement",left:n,right:O,body:J,await:false},e,J)}y(";");let s=r(";")?null:x();y(";");let h=r(")")?null:x();y(")");let w=R();return c({type:"ForStatement",init:t,test:s,update:h,body:w},e,w)},je=()=>{let e=o(),t=R();b("while")||m("Expected 'while' after the 'do' body"),o(),y("(");let n=x(),s=y(")");return r(";")&&o(),c({type:"DoWhileStatement",body:t,test:n},e,s)},Ae=()=>{let e=o(),t=e.value==="break"?"BreakStatement":"ContinueStatement",n=r(";")?o():v();return c({type:t,label:null},e,n)},Oe=()=>{let e=o();y("(");let t=x();y(")");let n=R();return c({type:"WhileStatement",test:t,body:n},e,n)},Je=()=>{let e=o();y("(");let t=x();y(")");let n=R(),s=null;return b("else")&&(o(),s=R()),c({type:"IfStatement",test:t,consequent:n,alternate:s},e,s??n)},De=()=>{let e=o(),t=null;!r(";")&&!r("}")&&!A()&&(t=x());let n=r(";")?o():v();return c({type:"ReturnStatement",argument:t},e,n)},Xe=()=>{let e=o(),t=x(),n=r(";")?o():v();return c({type:"ThrowStatement",argument:t},e,n)},Fe=()=>{let e=o(),t=W(),n=null,s=null;if(b("catch")){let h=o(),w=null;r("(")&&(o(),w=a(),y(")"));let j=W();n=c({type:"CatchClause",param:w,body:j},h,j);}return b("finally")&&(o(),s=W()),!n&&!s&&m("'try' requires a 'catch' or 'finally' clause"),c({type:"TryStatement",block:t,handler:n,finalizer:s},e,s??n??t)},Le=()=>{let e=o();y("(");let t=x();y(")"),y("{");let n=[];for(;!r("}")&&!A();){let h=i(),w=null;b("case")?(o(),w=x()):b("default")?o():m("Expected 'case' or 'default' in switch"),y(":");let j=[];for(;!r("}")&&!b("case")&&!b("default")&&!A();)j.push(R());n.push(c({type:"SwitchCase",test:w,consequent:j},h,v()??h));}let s=y("}");return c({type:"SwitchStatement",discriminant:t,cases:n},e,s)},W=()=>{let e=y("{"),t=[];for(;!r("}")&&!A();)t.push(R());return c({type:"BlockStatement",body:t},e,y("}"))};return {parseExpr:x,parseProgram:()=>{let e=i(),t=[];for(;!A();)t.push(R());return c({type:"Program",sourceType:"module",body:t},e,v()??e)},eof:A,fail:m}}function Qe(u){return he(u).parseProgram()}function et(u){let{parseExpr:N,eof:l,fail:i}=he(u),o=N();return l()||i("Unexpected token after expression"),o}export{de as KEYWORDS,Q as LexError,ee as ParseError,Qe as parse,et as parseExpression,se as tokenize};
1
+ import {TSLiteError}from'@tslite/core';var Z=class extends TSLiteError{constructor(l,h,d,r,f){super("lex-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="LexError";}},ye=new Set(["const","if","else","return","async","await","true","false","null","typeof","let","var","function","class","new","for","while","do","this","throw","try","catch","finally","switch","case","break","continue","default","void","delete","instanceof","in","yield","import","export","extends","super","debugger","with","enum","interface"]),ze=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],se=c=>c>="a"&&c<="z"||c>="A"&&c<="Z"||c==="_"||c==="$",me=c=>se(c)||c>="0"&&c<="9",B=c=>c>="0"&&c<="9",de=c=>B(c)||c>="a"&&c<="f"||c>="A"&&c<="F",qe=c=>me(c)||c==="-",Ve=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),We=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(c){let S=[],l=0,h=1,d=0,r=()=>({line:h,column:d}),f=(v=0)=>c[l+v]??"",L=v=>{let C=r();throw new Z(v,C,l,{start:C,end:{line:h,column:d+1}},l+1)},y=()=>{let v=c[l++];return v==="\r"?(c[l]===`
2
+ `&&l++,h++,d=0):v===`
3
+ `||v==="\u2028"||v==="\u2029"?(h++,d=0):d++,v},s=v=>v===`
4
+ `||v==="\r"||v==="\u2028"||v==="\u2029",x=()=>{for(;;){let v=f();if(v===" "||v===" "||v==="\f"||v==="\v"||s(v))y();else if(v==="/"&&f(1)==="/")for(;l<c.length&&!s(f());)y();else if(v==="/"&&f(1)==="*")for(y(),y();;){if(l>=c.length&&L("Unterminated block comment"),f()==="*"&&f(1)==="/"){y(),y();break}y();}else break}},I=(v,C,R,N)=>{S.push({type:v,value:C,start:R,end:l,loc:{start:N,end:r()}}),$=v==="Punctuator"?Ve.has(C):v==="Keyword"?We.has(C):v==="TemplateHead"||v==="TemplateMiddle";},E=v=>{for(y();l<c.length;){let C=f();if(C===v)return y(),c.slice(a,l);if(s(C)&&L("Unterminated string literal"),C==="\\"){y(),l>=c.length&&L("Unterminated string literal"),y();continue}y();}return L("Unterminated string literal")},m=()=>{if(f()==="0"&&(f(1)==="x"||f(1)==="X")){for(y(),y(),de(f())||L("Invalid hexadecimal literal");de(f());)y();return}if(f()==="0"&&(f(1)==="b"||f(1)==="B")){for(y(),y(),f()!=="0"&&f()!=="1"&&L("Invalid binary literal");f()==="0"||f()==="1";)y();return}if(f()==="0"&&(f(1)==="o"||f(1)==="O")){for(y(),y(),f()>="0"&&f()<="7"||L("Invalid octal literal");f()>="0"&&f()<="7";)y();return}for(;B(f());)y();if(f()===".")for(y();B(f());)y();if(f()==="e"||f()==="E")for(y(),(f()==="+"||f()==="-")&&y(),B(f())||L("Invalid exponent");B(f());)y();},T=()=>{let v=l;for(;l<c.length;){let C=f();if(C==="`"){let R=c.slice(v,l);return y(),{raw:R,interp:false}}if(C==="$"&&f(1)==="{"){let R=c.slice(v,l);return y(),y(),{raw:R,interp:true}}if(C==="\\"){if(y(),l>=c.length)break;y();continue}y();}return L("Unterminated template literal")},a=0,J=0,k=[],$=true,X="js",K=[],F=[];for(;;){if(X==="jsxChildren"){a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(i==="<"){f(1)==="/"?(y(),y(),I("Punctuator","</",a,N),X="jsxCloseTag"):(y(),I("Punctuator","<",a,N),K.push("jsxChildren"),X="jsxOpenTag");continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:"jsxChildren"}),X="js";continue}for(;l<c.length&&f()!=="<"&&f()!=="{";)y();I("JSXText",c.slice(a,l),a,N);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){x(),a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(se(i)){for(y();qe(f());)y();I("Identifier",c.slice(a,l),a,N);continue}if(i==="/"&&f(1)===">"){y(),y(),I("Punctuator","/>",a,N),X=K.pop()??"js";continue}if(i===">"){y(),I("Punctuator",">",a,N),X=X==="jsxCloseTag"?K.pop()??"js":"jsxChildren";continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:X}),X="js";continue}if(i==='"'||i==="'"){I("String",E(i),a,N);continue}if(i==="="||i==="."||i===":"){y(),I("Punctuator",i,a,N);continue}L(`Unexpected character '${i}' in JSX tag`);}x(),a=l;let v=r();if(l>=c.length){I("EOF","",a,v);break}let C=f();if(se(C)){for(;me(f());)y();let N=c.slice(a,l);I(ye.has(N)?"Keyword":"Identifier",N,a,v);continue}if(B(C)||C==="."&&B(f(1))){m(),I("Number",c.slice(a,l),a,v);continue}if(C==='"'||C==="'"){let N=E(C);I("String",N,a,v);continue}if(C==="`"){y();let{raw:N,interp:i}=T();i?(I("TemplateHead",N,a,v),k.push(J)):I("Template",N,a,v);continue}if(C==="<"&&$){y(),I("Punctuator","<",a,v),K.push("js"),X="jsxOpenTag";continue}if(C==="{"){y(),I("Punctuator","{",a,v),J++;continue}if(C==="}"){if(k.length>0&&J===k[k.length-1]){k.pop(),y();let{raw:N,interp:i}=T();I(i?"TemplateMiddle":"TemplateTail",N,a,v),i&&k.push(J);continue}if(F.length>0&&J===F[F.length-1].level){let{restore:N}=F.pop();y(),I("Punctuator","}",a,v),X=N;continue}y(),I("Punctuator","}",a,v),J--;continue}if(C==="?"&&f(1)==="."&&B(f(2))){y(),I("Punctuator","?",a,v);continue}let R=ze.find(N=>c.startsWith(N,l));if(R){for(let N=0;N<R.length;N++)y();I("Punctuator",R,a,v);continue}L(`Unexpected character '${C}'`);}return S}var ie=(c=[])=>({vars:new Set(c),bounds:new Map,noCond:false,inferSink:null}),H=(c,S)=>{let l=new Set(c.vars),h=new Map(c.bounds);for(let d of S)l.add(d.name),d.bound||d.isConst?h.set(d.name,{bound:d.bound,isConst:d.isConst}):h.delete(d.name);return {vars:l,bounds:h,noCond:c.noCond,inferSink:c.inferSink}},He=(c,S)=>{let l=c.bounds.get(S);return l&&(l.bound||l.isConst)?{t:"var",name:S,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:S}},he=c=>{let{peek:S,next:l,isP:h,isK:d,expectP:r,fail:f,unescape:L}=c,y=()=>{let i=c.pos(),p=c.tokens[i],u=c.tokens[i-1];return !!u&&!!p&&p.loc.start.line>u.loc.end.line},s=()=>{let i=0;for(let p=c.pos();p<c.tokens.length;p++){let u=c.tokens[p];if(u.type==="EOF")return false;if(u.type==="Punctuator"){if(["(","[","{","<"].includes(u.value))i++;else if([")","]","}",">"].includes(u.value)&&(i--,i===0)){let b=c.tokens[p+1];return !!b&&b.type==="Punctuator"&&b.value==="=>"}}}return false},x=()=>{let i=c.tokens,p=[],u=0,b=false;for(let w=c.pos();w<i.length;w++){let j=i[w];if(j.type==="EOF")break;if(j.type==="Punctuator"){if(j.value==="<")u++,u===1&&(b=true);else if(j.value===">"){if(u--,u===0)break}else j.value==="("||j.value==="["||j.value==="{"?(u++,b=false):j.value===")"||j.value==="]"||j.value==="}"?(u--,b=false):j.value===","&&u===1?b=true:b=false;continue}if(u===1&&b){if(j.type==="Keyword"&&j.value==="const")continue;if(j.type==="Identifier"){p.push(j.value),b=false;continue}}b=false;}return p},I=i=>{let p=H(i,x().map(b=>({name:b})));r("<");let u=[];for(;!h(">");){let b=d("const");b&&l();let w=S();w.type!=="Identifier"&&f("Expected type parameter name"),l();let j;d("extends")&&(l(),j=T(p)),h("=")&&(l(),T(p)),u.push({name:w.value,...j?{bound:j}:{},...b?{isConst:true}:{}}),h(">")||r(",");}return r(">"),u},E=i=>{r("<");let p=[];for(;!h(">");)p.push(T(i)),h(">")||r(",");return r(">"),p},m=i=>{let p=S();return p.type==="Identifier"&&p.value===i},T=i=>{let p=a(i);if(i.noCond||!d("extends")||y())return p;l();let u=[],b=a({...i,noCond:true,inferSink:u});r("?");let w=H(i,u.map(z=>({name:z}))),j=T(w);r(":");let _=T(w);return {t:"conditional",check:p,extends:b,then:j,else:_,...u.length?{infer:u}:{}}},a=i=>{h("|")&&l();let p=J(i);if(!h("|"))return p;let u=[p];for(;h("|");)l(),u.push(J(i));return {t:"union",of:u}},J=i=>{h("&")&&l();let p=k(i);if(!h("&"))return p;let u=[p];for(;h("&");)l(),u.push(k(i));return {t:"intersection",of:u}},k=i=>{if(m("keyof"))return l(),{t:"keyof",of:k(i)};if(m("readonly")){l();let p=k(i);return p.t!=="array"&&p.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{...p,readonly:true}}if(m("infer")){l();let p=S();p.type!=="Identifier"&&f("Expected 'infer' variable name"),l();let u=i.inferSink;return u?(u.push(p.value),{t:"var",name:p.value}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return $(i)},$=i=>{let p=X(i);for(;h("[")&&!y();)if(l(),h("]"))l(),p={t:"array",items:p};else {let u=T(i);r("]"),p={t:"index",obj:p,key:u};}return p},X=i=>{if(d("typeof")){l();let u=S();u.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),l();let b=[u.value];for(;h(".");){l();let w=S();w.type!=="Identifier"&&w.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),l(),b.push(w.value);}return {t:"typeQuery",path:b}}if(h("<")){let u=I(i),b=H(i,u);return F(b,u.map(w=>w.name))}if(h("(")){if(s())return F(i);l();let u=T(i.noCond?{...i,noCond:false}:i);return r(")"),u}if(h("{"))return C()?R(i):N(i);if(h("["))return v(i);let p=S();if(p.type==="String")return l(),{t:"literal",value:L(p.value.slice(1,-1))};if(p.type==="Number")return l(),{t:"literal",value:Number(p.value)};if(d("true"))return l(),{t:"literal",value:true};if(d("false"))return l(),{t:"literal",value:false};if(d("null"))return l(),{t:"null"};if(d("void"))return l(),{t:"void"};if(p.type==="Identifier"){switch(l(),p.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"};case "never":return {t:"union",of:[]}}let u=h("<")?E(i):[];return i.vars.has(p.value)&&u.length===0?He(i,p.value):p.value==="ReturnType"&&u.length===1?{t:"returnType",fn:u[0]}:p.value==="Parameters"&&u.length===1?{t:"parameters",fn:u[0]}:(c.typeRefs?.push({name:p.value,loc:p.loc}),{t:"apply",name:p.value,args:u})}return f(`Expected a type but got '${p.value||p.type}'`)},K=i=>{r("(");let p=[];for(;!h(")");){let u=S();u.type!=="Identifier"&&f("Expected parameter name in function type"),l();let b=h("?");b&&l(),r(":");let w=T(i);p.push({name:u.value,type:w,...b?{optional:true}:{}}),h(")")||r(",");}return r(")"),p},F=(i,p)=>{let u=K(i);r("=>");let b=T(i);return {t:"fn",params:u,returns:b,...p?.length?{typeParams:p}:{}}},v=i=>{r("[");let p=[],u=[];for(;!h("]");){if(h("...")){l();let b=T(i);b.t==="array"?(p.push(b.items),u.push("rest")):(p.push(b),u.push("variadic"));}else {let b=T(i),w=h("?");w&&l(),p.push(b),u.push(w?"optional":"req");}h("]")||r(",");}return r("]"),u.some(b=>b!=="req")?{t:"tuple",elements:p,flags:u}:{t:"tuple",elements:p}},C=()=>{let i=c.tokens,p=c.pos()+1;return i[p]?.type==="Punctuator"&&(i[p]?.value==="+"||i[p]?.value==="-")&&p++,i[p]?.type==="Identifier"&&i[p]?.value==="readonly"&&p++,i[p]?.type==="Punctuator"&&i[p]?.value==="["&&i[p+1]?.type==="Identifier"&&i[p+2]?.type==="Keyword"&&i[p+2]?.value==="in"},R=i=>{r("{"),(h("+")||h("-"))&&l(),m("readonly")&&l(),r("[");let p=S();p.type!=="Identifier"&&f("Expected mapped type key name"),l(),d("in")||f("Expected 'in' in mapped type"),l();let u=T(i);m("as")&&f("Key remapping ('as') in mapped types is not supported"),r("]");let b=false,w=h("+")||h("-")?l().value:null;h("?")&&(l(),b=w!=="-"),r(":");let j=T(H(i,[{name:p.value}]));return (h(";")||h(","))&&l(),r("}"),{t:"mapped",key:p.value,over:u,body:j,optional:b}},N=i=>{r("{");let p={},u,b=[];for(;!h("}");){if(h("(")){let w=K(i);r(":");let j=T(i);b.push({t:"fn",params:w,returns:j});}else if(h("[")){l(),S().type!=="Identifier"&&f("Expected index signature parameter name"),l(),r(":");let w=S();(w.type!=="Identifier"||w.value!=="string"&&w.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),l(),r("]"),r(":"),u={key:w.value,value:T(i)};}else {let w=S(),j;if(w.type==="Identifier"||w.type==="Keyword")l(),j=w.value;else if(w.type==="String")l(),j=L(w.value.slice(1,-1));else if(w.type==="Number")l(),j=w.value;else return f("Expected property name in object type");let _=h("?");_&&l(),r(":");let z=T(i);p[j]={type:z,..._?{optional:true}:{}};}if(h(";")||h(","))l();else break}return r("}"),b.length>0?Object.keys(p).length>0||u?f("Callable object types (call signatures mixed with members) are not supported"):b.length===1?b[0]:{t:"overload",signatures:b}:{t:"object",props:p,...u?{index:u}:{}}};return {parseType:i=>T(ie(i)),parseTypeParamList:()=>I(ie()),parseTypeInScope:i=>T(H(ie(),i))}};var ee=class extends TSLiteError{constructor(l,h,d,r,f){super("parse-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="ParseError";}},Ye={"??":1,"||":2,"&&":3,"|":4,"^":5,"&":6,"==":7,"===":7,"!=":7,"!==":7,"<":8,"<=":8,">":8,">=":8,in:8,as:8,satisfies:8,"<<":9,">>":9,">>>":9,"+":10,"-":10,"*":11,"/":11,"%":11,"**":12},Qe=new Set(["??","||","&&"]),Ze=new Set(["class","debugger","with","enum","interface"]),et={enum:'use a union of literals (`type T = "a" | "b"`) \u2014 it is serializable',interface:"use a type alias (`type X = { \u2026 }`)",namespace:"use modules (`import`/`export`)",module:"use modules (`import`/`export`)",declare:"declare the shape via the DI env, or with a `type` alias",abstract:"classes are not part of the language \u2014 model with objects + functions"},tt={namespace:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",module:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",declare:c=>c.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(c.value)||c.type==="Identifier"&&["type","namespace","module"].includes(c.value),abstract:c=>c.type==="Keyword"&&c.value==="class"},nt=new Set(["=","+=","-=","*=","/=","%="]);function ge(c){let S=ae(c),l=0,h=[],d=(e=0)=>S[Math.min(l+e,S.length-1)],r=()=>S[l++],f=()=>S[l-1],L=()=>{let e=S[l],t=S[l-1];return !!e&&!!t&&e.loc.start.line>t.loc.end.line},y=()=>d().type==="EOF",s=e=>d().type==="Punctuator"&&d().value===e,x=e=>d().type==="Keyword"&&d().value===e,I=e=>d().type==="Identifier"&&d().value===e,E=(e,t=d())=>{throw new ee(e,t.loc.start,t.start,t.loc,t.end)},m=e=>(s(e)||E(`Expected '${e}' but got '${d().value||d().type}'`),r()),T=(e,t)=>{let n=f()??t;return Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}})},a=(e,t,n)=>Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}}),J=e=>{let t="";for(let n=0;n<e.length;n++){if(e[n]!=="\\"){t+=e[n];continue}let o=e[++n];switch(o){case "n":t+=`
5
+ `;break;case "t":t+=" ";break;case "r":t+="\r";break;case "b":t+="\b";break;case "f":t+="\f";break;case "v":t+="\v";break;case "0":t+="\0";break;case "x":t+=String.fromCharCode(parseInt(e.slice(n+1,n+3),16)),n+=2;break;case "u":if(e[n+1]==="{"){let g=e.indexOf("}",n);t+=String.fromCodePoint(parseInt(e.slice(n+2,g),16)),n=g;}else t+=String.fromCharCode(parseInt(e.slice(n+1,n+5),16)),n+=4;break;default:t+=o;}}return t},k=(e=false)=>$(e),$=(e=false)=>{let t=X(e),n=d();if(n.type==="Punctuator"&&nt.has(n.value)){r();let o=$();return a({type:"AssignmentExpression",operator:n.value,left:t,right:o},t,o)}return t},X=(e=false)=>{let t=F(0,e);if(!s("?"))return t;r();let n=k();m(":");let o=k();return a({type:"ConditionalExpression",test:t,consequent:n,alternate:o},t,o)},K=()=>{let e=d();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:e.value==="satisfies"?{op:"satisfies",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let t=d(1);if(t.type==="Punctuator"&&t.value===">"&&e.end===t.start){let n=d(2);return n.type==="Punctuator"&&n.value===">"&&t.end===n.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},F=(e,t=false)=>{let n=v();for(;;){let o=K();if(!o)break;let{op:g,consume:P}=o;if(g==="in"&&t)break;let A=Ye[g];if(!A||A<=e)break;if(g==="satisfies"){if(L())break;E("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(g==="as"){if(L())break;r();let D=V(new Set);n=a({type:"TSAsExpression",expression:n,typeAnnotation:D},n,f());continue}for(let D=0;D<P;D++)r();let O=F(g==="**"?A-1:A,t);n=a({type:Qe.has(g)?"LogicalExpression":"BinaryExpression",operator:g,left:n,right:O},n,O);}return n},v=()=>{let e=d();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(r(),T({type:"UpdateExpression",operator:e.value,prefix:true,argument:v()},e)):x("await")?(r(),T({type:"AwaitExpression",argument:v()},e)):x("typeof")||x("void")||x("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?C(e):N()},C=e=>{r();let t=v();return s("**")&&E(`An unary expression with the '${e.value}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.`),T({type:"UnaryExpression",operator:e.value,prefix:true,argument:t},e)},R=()=>{let e=l,t=()=>(l=e,void 0);try{if(r(),s(">"))return t();let n=[];for(;n.push(V(new Set)),!!s(",");)r();return s(">")?(r(),s("(")?n:t()):t()}catch{return t()}},N=()=>{let e=we(),t=false;for(;;)if(s("?."))if(r(),t=true,s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:true},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:true},e,o);}else {let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name after '?.'"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:true},e,o);}else if(s(".")){r();let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:false},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:false},e,o);}else if(s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:false},e,o);}else if(s("<")){let n=R();if(!n)break;let{args:o,close:g}=i();e=a({type:"CallExpression",callee:e,arguments:o,optional:false,typeArguments:n},e,g);}else if(s("++")||s("--")){let n=r();e=a({type:"UpdateExpression",operator:n.value,prefix:false,argument:e},e,n);}else break;return t?a({type:"ChainExpression",expression:e},e,e):e},i=()=>{m("(");let e=[];for(;!s(")");)e.push(p()),s(")")||m(",");return {args:e,close:m(")")}},p=()=>{if(s("...")){let e=r(),t=k();return a({type:"SpreadElement",argument:t},e,t)}return k()},u=()=>{let e=d();return e.type!=="Identifier"&&E("Expected identifier"),r(),T({type:"Identifier",name:e.value},e)},b=e=>{let t=s("{")?w(e):s("[")?j(e):u();return s(":")&&(r(),t.typeAnnotation=V(e)),t},w=e=>{let t=m("{"),n=[];for(;!s("}");){if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=u(),P,A=false;if(s(":")?(r(),P=b(e)):(P={...g},A=true),s("=")){r();let O=k();P=a({type:"AssignmentPattern",left:P,right:O},P,O);}n.push(a({type:"Property",key:g,value:P,kind:"init",shorthand:A,computed:false,method:false},g,P));}s("}")||m(",");}let o=m("}");return a({type:"ObjectPattern",properties:n},t,o)},j=e=>{let t=m("["),n=[];for(;!s("]");){if(s(",")){n.push(null),r();continue}if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=b(e);if(s("=")){r();let P=k();g=a({type:"AssignmentPattern",left:g,right:P},g,P);}n.push(g);}s("]")||m(",");}let o=m("]");return a({type:"ArrayPattern",elements:n},t,o)},_=()=>{let e=0;for(let t=l;t<S.length;t++){let n=S[t];if(n.type==="EOF")return false;if(n.type==="Punctuator"){if(n.value==="("||n.value==="["||n.value==="{")e++;else if((n.value===")"||n.value==="]"||n.value==="}")&&(e--,e===0)){let o=S[t+1];return !o||o.type!=="Punctuator"?false:o.value==="=>"?true:o.value===":"?z(t+2):false}}}return false},z=e=>{let t=0;for(let n=e;n<S.length;n++){let o=S[n];if(o.type==="EOF")return false;if(o.type==="Punctuator")if(["(","[","{","<"].includes(o.value))t++;else if([")","]","}",">"].includes(o.value)){if(t===0)return false;t--;}else {if(t===0&&o.value==="=>")return true;if(t===0&&(o.value===";"||o.value===","))return false}}return false},ve=(e=new Set)=>{m("(");let t=[];for(;!s(")");){if(s("...")){let n=r(),o=b(e);t.push(a({type:"RestElement",argument:o},n,o));}else {let n=b(e);if(s("=")){r();let o=k();n=a({type:"AssignmentPattern",left:n,right:o},n,o);}t.push(n);}s(")")||m(",");}return m(")"),t},te=(e,t)=>{let n=s("(")?ve():[u()],o;s(":")&&(r(),o=V(new Set)),m("=>");let g=s("{")?W():k();return T({type:"ArrowFunctionExpression",params:n,body:g,async:e,expression:g.type!=="BlockStatement",generator:false,...o?{returnType:o}:{}},t)},be=()=>{let e=m("["),t=[];for(;!s("]");){if(s(",")){r(),t.push(null);continue}t.push(p()),s("]")||m(",");}return a({type:"ArrayExpression",elements:t},e,m("]"))},Te=()=>{let e=m("{"),t=[];for(;!s("}");){if(s("...")){let n=r(),o=k();t.push(a({type:"SpreadElement",argument:o},n,o));}else {let n=d(),o,g=false;if(s("["))g=true,r(),o=k(),m("]");else if(n.type==="String")r(),o=T({type:"Literal",value:J(n.value.slice(1,-1)),raw:n.value},n);else if(n.type==="Number")r(),o=T({type:"Literal",value:Number(n.value),raw:n.value},n);else if(n.type==="Identifier"||n.type==="Keyword")r(),o=T({type:"Identifier",name:n.value},n);else return E("Invalid property key");if(s(":")){r();let P=k();t.push(a({type:"Property",key:o,value:P,kind:"init",method:false,shorthand:false,computed:g},n,P));}else t.push(a({type:"Property",key:o,value:o,kind:"init",method:false,shorthand:true,computed:false},n,o));}s("}")||m(",");}return a({type:"ObjectExpression",properties:t},e,m("}"))},G=(e,t)=>({type:"TemplateElement",value:{raw:e,cooked:J(e)},tail:t}),xe=e=>{r();let t=[G(e.value,false)],n=[];for(;;){n.push(k());let o=d();if(o.type==="TemplateMiddle")r(),t.push(G(o.value,false));else if(o.type==="TemplateTail"){r(),t.push(G(o.value,true));break}else E("Expected `}` to close template interpolation");}return T({type:"TemplateLiteral",quasis:t,expressions:n},e)},q=()=>{let e=d();return e.type!=="Identifier"&&E(`Expected JSX identifier but got '${e.value||e.type}'`),T({type:"JSXIdentifier",name:r().value},e)},le=()=>{let e=q();if(s(":")){r();let t=q();return a({type:"JSXNamespacedName",namespace:e,name:t},e,t)}for(;s(".");){r();let t=q();e=a({type:"JSXMemberExpression",object:e,property:t},e,t);}return e},M=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${M(e.namespace)}:${M(e.name)}`:`${M(e.object)}.${M(e.property)}`,Se=()=>{let e=d();if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(s("{")){let t=r(),n=$(),o=m("}");return a({type:"JSXExpressionContainer",expression:n},t,o)}return s("<")?ne():E(`Invalid JSX attribute value '${e.value||e.type}'`)},ke=()=>{let e=[];for(;!s(">")&&!s("/>")&&!y();){if(s("{")){let o=r();m("...");let g=$(),P=m("}");e.push(a({type:"JSXSpreadAttribute",argument:g},o,P));continue}let t=q();if(s(":")){r();let o=q();t=a({type:"JSXNamespacedName",namespace:t,name:o},t,o);}let n=null;s("=")&&(r(),n=Se()),e.push(a({type:"JSXAttribute",name:t,value:n},t,n??t));}return e},pe=()=>{let e=[];for(;!(s("</")||y());){let t=d();if(t.type==="JSXText")r(),e.push(T({type:"JSXText",value:t.value,raw:t.value},t));else if(s("{")){let n=r();if(s("}")){let o=r(),g=T({type:"JSXEmptyExpression"},n);e.push(a({type:"JSXExpressionContainer",expression:g},n,o));}else {let o=$(),g=m("}");e.push(a({type:"JSXExpressionContainer",expression:o},n,g));}}else s("<")?e.push(ne()):E(`Unexpected token in JSX children '${t.value||t.type}'`);}return e},ne=()=>{let e=m("<");if(s(">")){let Q=r(),oe=a({type:"JSXOpeningFragment"},e,Q),Ue=pe(),Me=m("</"),fe=m(">"),Be=a({type:"JSXClosingFragment"},Me,fe);return a({type:"JSXFragment",openingFragment:oe,children:Ue,closingFragment:Be},e,fe)}let t=le(),n=ke();if(s("/>")){let Q=r(),oe=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:true},e,Q);return a({type:"JSXElement",openingElement:oe,children:[],closingElement:null},e,Q)}let o=m(">"),g=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:false},e,o),P=pe(),A=m("</"),O=d(),D=le();M(D)!==M(t)&&E(`JSX closing tag </${M(D)}> does not match opening <${M(t)}>`,O);let Y=m(">"),Ke=a({type:"JSXClosingElement",name:D},A,Y);return a({type:"JSXElement",openingElement:g,children:P,closingElement:Ke},e,Y)},we=()=>{let e=d();if(x("async")){let t=d(1);return t.value==="("||t.type==="Identifier"?(r(),te(true,e)):E("Unexpected 'async'")}if(s("(")&&_()||e.type==="Identifier"&&d(1).type==="Punctuator"&&d(1).value==="=>")return te(false,e);if(e.type==="Number")return r(),T({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return r(),T({type:"TemplateLiteral",expressions:[],quasis:[G(e.value,true)]},e);if(e.type==="TemplateHead")return xe(e);if(e.type==="Keyword")return e.value==="true"?(r(),T({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(r(),T({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(r(),T({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?E("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?E("Anonymous functions are not supported \u2014 use an arrow function"):E(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return r(),T({type:"Identifier",name:e.value},e);if(s("(")){r();let t=k();return m(")"),t}return s("[")?be():s("{")?Te():s("<")?ne():E(`Unexpected token '${e.value||e.type}'`)},{parseType:V,parseTypeParamList:Ne,parseTypeInScope:Pe}=he({tokens:S,pos:()=>l,peek:d,next:r,isP:s,isK:x,expectP:m,fail:E,unescape:J,typeRefs:h}),Ee=()=>{let e=r(),t=u(),n=[],o=[];s("<")&&(o=Ne(),n=o.map(A=>A.name)),m("=");let g=Pe(o),P=a({type:"TSTypeAliasDeclaration",id:t,typeParameters:n,typeAnnotation:g},e,f());return s(";")&&r(),P},U=()=>{let e=d();if(e.type==="Identifier"&&e.value==="type"&&d(1).type==="Identifier"&&d(2).type==="Punctuator"&&(d(2).value==="="||d(2).value==="<"))return Ee();if(x("const")||x("let")||x("var")){let g=ce();return s(";")&&r(),g}if(x("if"))return Le();if(x("for"))return Ae();if(x("while"))return Je();if(x("do"))return Oe();if(x("break")||x("continue"))return De();if(x("return"))return Xe();if(x("throw"))return Re();if(x("try"))return Fe();if(x("switch"))return $e();if(x("import"))return Ce();if(x("export"))return je();if(s("{"))return W();if((x("function")||x("async")&&d(1).type==="Keyword"&&d(1).value==="function")&&E("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),s(";"))return r(),T({type:"EmptyStatement"},e);if(e.type==="Keyword"&&Ze.has(e.value)||e.type==="Identifier"&&tt[e.value]?.(d(1),d(2))===true){let g=et[e.value];E(`'${e.value}' is not supported in this language`+(g?` \u2014 ${g}`:""));}let n=k(),o=s(";")?r():f();return a({type:"ExpressionStatement",expression:n},n,o)},ce=()=>{let e=r(),t=e.value,n=b(new Set),o=null;s("=")?(r(),o=k()):t==="const"&&E("'const' declarations must be initialized");let g=a({type:"VariableDeclarator",id:n,init:o},n,o??n);return a({type:"VariableDeclaration",kind:t,declarations:[g]},e,o??n)},Ie=()=>{let e=d();return e.type!=="String"&&E("Expected module specifier string"),r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e)},Ce=()=>{let e=r(),t=[];if(d().type!=="String"){if(d().type==="Identifier"){let o=u();t.push(a({type:"ImportDefaultSpecifier",local:o},o,o)),s(",")&&r();}if(s("*")){let o=r();I("as")||E("Expected 'as' after '*' in import"),r();let g=u();t.push(a({type:"ImportNamespaceSpecifier",local:g},o,g));}else if(s("{")){for(r();!s("}")&&!y();){let o=u(),g=o;if(I("as")&&(r(),g=u()),t.push(a({type:"ImportSpecifier",imported:o,local:g},o,g)),s(","))r();else break}m("}");}I("from")||E("Expected 'from' in import declaration"),r();}let n=Ie();return s(";")&&r(),T({type:"ImportDeclaration",specifiers:t,source:n},e)},je=()=>{let e=r();if(x("default")){r();let t=k();return s(";")&&r(),T({type:"ExportDefaultDeclaration",declaration:t},e)}if(x("const")||x("let")||x("var")){let t=ce();return s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:t,specifiers:[],source:null},e)}if(s("{")){r();let t=[];for(;!s("}")&&!y();){let n=u(),o=n;if(I("as")&&(r(),o=u()),t.push(a({type:"ExportSpecifier",local:n,exported:o},n,o)),s(","))r();else break}return m("}"),s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:null,specifiers:t,source:null},e)}return E("Expected 'const', '{', or 'default' after 'export'")},ue=()=>d().type==="Identifier"&&d().value==="of",re=()=>x("in"),Ae=()=>{let e=r();m("(");let t=null,n=null;if(!s(";"))if(x("const")||x("let")||x("var")){let A=r(),O=u();if(s(":")&&(r(),O.typeAnnotation=V(new Set)),ue()||re()){let D=a({type:"VariableDeclarator",id:O,init:null},O,O);n=a({type:"VariableDeclaration",kind:A.value,declarations:[D]},A,O);}else {let D=null;s("=")?(r(),D=k(true)):A.value==="const"&&E("'const' declarations must be initialized");let Y=a({type:"VariableDeclarator",id:O,init:D},O,D??O);t=a({type:"VariableDeclaration",kind:A.value,declarations:[Y]},A,D??O);}}else {let A=k(true);ue()||re()?n=A:t=A;}if(n){let A=re();r();let O=k();m(")");let D=U();return a(A?{type:"ForInStatement",left:n,right:O,body:D}:{type:"ForOfStatement",left:n,right:O,body:D,await:false},e,D)}m(";");let o=s(";")?null:k();m(";");let g=s(")")?null:k();m(")");let P=U();return a({type:"ForStatement",init:t,test:o,update:g,body:P},e,P)},Oe=()=>{let e=r(),t=U();x("while")||E("Expected 'while' after the 'do' body"),r(),m("(");let n=k(),o=m(")");return s(";")&&r(),a({type:"DoWhileStatement",body:t,test:n},e,o)},De=()=>{let e=r(),t=e.value==="break"?"BreakStatement":"ContinueStatement",n=s(";")?r():f();return a({type:t,label:null},e,n)},Je=()=>{let e=r();m("(");let t=k();m(")");let n=U();return a({type:"WhileStatement",test:t,body:n},e,n)},Le=()=>{let e=r();m("(");let t=k();m(")");let n=U(),o=null;return x("else")&&(r(),o=U()),a({type:"IfStatement",test:t,consequent:n,alternate:o},e,o??n)},Xe=()=>{let e=r(),t=null;!s(";")&&!s("}")&&!y()&&(t=k());let n=s(";")?r():f();return a({type:"ReturnStatement",argument:t},e,n)},Re=()=>{let e=r(),t=k(),n=s(";")?r():f();return a({type:"ThrowStatement",argument:t},e,n)},Fe=()=>{let e=r(),t=W(),n=null,o=null;if(x("catch")){let g=r(),P=null;s("(")&&(r(),P=u(),m(")"));let A=W();n=a({type:"CatchClause",param:P,body:A},g,A);}return x("finally")&&(r(),o=W()),!n&&!o&&E("'try' requires a 'catch' or 'finally' clause"),a({type:"TryStatement",block:t,handler:n,finalizer:o},e,o??n??t)},$e=()=>{let e=r();m("(");let t=k();m(")"),m("{");let n=[];for(;!s("}")&&!y();){let g=d(),P=null;x("case")?(r(),P=k()):x("default")?r():E("Expected 'case' or 'default' in switch"),m(":");let A=[];for(;!s("}")&&!x("case")&&!x("default")&&!y();)A.push(U());n.push(a({type:"SwitchCase",test:P,consequent:A},g,f()??g));}let o=m("}");return a({type:"SwitchStatement",discriminant:t,cases:n},e,o)},W=()=>{let e=m("{"),t=[];for(;!s("}")&&!y();)t.push(U());return a({type:"BlockStatement",body:t},e,m("}"))};return {parseExpr:k,parseProgram:()=>{let e=d(),t=[];for(;!y();)t.push(U());return a({type:"Program",sourceType:"module",body:t,...h.length?{typeRefs:h}:{}},e,f()??e)},eof:y,fail:E}}function rt(c){return ge(c).parseProgram()}function ot(c){let{parseExpr:S,eof:l,fail:h}=ge(c),d=S();return l()||h("Unexpected token after expression"),d}export{ye as KEYWORDS,Z as LexError,ee as ParseError,rt as parse,ot as parseExpression,ae as tokenize};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tslite/parser",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "TSLite parser — tokenizer + Pratt parser for the TSLite language (JS subset + pipe) into ESTree",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -43,10 +43,10 @@
43
43
  "@tslite/core": "0.2.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@tslite/checker": "0.2.1",
47
46
  "@tslite/printer": "0.2.0",
47
+ "@tslite/capability": "0.2.0",
48
48
  "@tslite/interpret": "0.2.0",
49
- "@tslite/capability": "0.2.0"
49
+ "@tslite/checker": "0.3.0"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup",