@tslite/parser 0.6.0 → 0.6.2

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,7 +1,7 @@
1
- 'use strict';var core=require('@tslite/core');var ee=class extends core.TSLiteError{constructor(i,o,p,g,f){super("lex-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="LexError";}},Le=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"]),it=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],me=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Re=r=>me(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",Ie=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",lt=r=>Re(r)||r==="-",ct=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),pt=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(r,c,i){let o=[],p=0,g=1,f=0,s=()=>({line:g,column:f}),y=(N=0)=>r[p+N]??"",F=N=>{let R=s(),U={start:R,end:{line:g,column:f+1}};if(i){i({message:N,position:R,index:p,loc:U,end:p+1});return}throw new ee(N,R,p,U,p+1)},T=()=>{let N=r[p++];return N==="\r"?(r[p]===`
1
+ 'use strict';var core=require('@tslite/core');var ee=class extends core.TSLiteError{constructor(l,o,p,g,f){super("lex-error",`${l} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="LexError";}},Le=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"]),lt=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],me=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Re=r=>me(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",Ie=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",ct=r=>Re(r)||r==="-",pt=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),ut=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(r,c,l){let o=[],p=0,g=1,f=0,s=()=>({line:g,column:f}),y=(N=0)=>r[p+N]??"",F=N=>{let L=s(),U={start:L,end:{line:g,column:f+1}};if(l){l({message:N,position:L,index:p,loc:U,end:p+1});return}throw new ee(N,L,p,U,p+1)},T=()=>{let N=r[p++];return N==="\r"?(r[p]===`
2
2
  `&&p++,g++,f=0):N===`
3
3
  `||N==="\u2028"||N==="\u2029"?(g++,f=0):f++,N},a=N=>N===`
4
- `||N==="\r"||N==="\u2028"||N==="\u2029",P=()=>{for(;;){let N=y();if(N===" "||N===" "||N==="\f"||N==="\v"||a(N))T();else if(N==="/"&&y(1)==="/"){let R=p,U=s();T(),T();let I=p;for(;p<r.length&&!a(y());)T();c?.({type:"Line",value:r.slice(I,p),start:R,end:p,loc:{start:U,end:s()}});}else if(N==="/"&&y(1)==="*"){let R=p,U=s();T(),T();let I=p,u=p;for(;;){if(p>=r.length){F("Unterminated block comment"),u=p;break}if(y()==="*"&&y(1)==="/"){u=p,T(),T();break}T();}c?.({type:"Block",value:r.slice(I,u),start:R,end:p,loc:{start:U,end:s()}});}else break}},A=(N,R,U,I)=>{o.push({type:N,value:R,start:U,end:p,loc:{start:I,end:s()}}),E=N==="Punctuator"?ct.has(R):N==="Keyword"?pt.has(R):N==="TemplateHead"||N==="TemplateMiddle";},L=N=>{for(T();p<r.length;){let R=y();if(R===N)return T(),r.slice(b,p);if(a(R))return F("Unterminated string literal"),r.slice(b,p);if(R==="\\"){if(T(),p>=r.length)return F("Unterminated string literal"),r.slice(b,p);T();continue}T();}return F("Unterminated string literal"),r.slice(b,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!Ie(y()))return F("Invalid hexadecimal literal");for(;Ie(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return F("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return F("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return F("Invalid exponent");for(;Z(y());)T();}},v=()=>{let N=p;for(;p<r.length;){let R=y();if(R==="`"){let U=r.slice(N,p);return T(),{raw:U,interp:false}}if(R==="$"&&y(1)==="{"){let U=r.slice(N,p);return T(),T(),{raw:U,interp:true}}if(R==="\\"){if(T(),p>=r.length)break;T();continue}T();}return F("Unterminated template literal"),{raw:r.slice(N,p),interp:false}},b=0,m=0,M=[],E=true,X="js",V=[],z=[];for(;;){if(X==="jsxChildren"){b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(u==="<"){y(1)==="/"?(T(),T(),A("Punctuator","</",b,I),X="jsxCloseTag"):(T(),A("Punctuator","<",b,I),V.push("jsxChildren"),X="jsxOpenTag");continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:"jsxChildren"}),X="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();A("JSXText",r.slice(b,p),b,I);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){P(),b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(me(u)){for(T();lt(y());)T();A("Identifier",r.slice(b,p),b,I);continue}if(u==="/"&&y(1)===">"){T(),T(),A("Punctuator","/>",b,I),X=V.pop()??"js";continue}if(u===">"){T(),A("Punctuator",">",b,I),X=X==="jsxCloseTag"?V.pop()??"js":"jsxChildren";continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:X}),X="js";continue}if(u==='"'||u==="'"){A("String",L(u),b,I);continue}if(u==="="||u==="."||u===":"){T(),A("Punctuator",u,b,I);continue}F(`Unexpected character '${u}' in JSX tag`),T(),A("Unknown",u,b,I);continue}P(),b=p;let N=s();if(p>=r.length){A("EOF","",b,N);break}let R=y();if(me(R)){for(;Re(y());)T();let I=r.slice(b,p);A(Le.has(I)?"Keyword":"Identifier",I,b,N);continue}if(Z(R)||R==="."&&Z(y(1))){D(),A("Number",r.slice(b,p),b,N);continue}if(R==='"'||R==="'"){let I=L(R);A("String",I,b,N);continue}if(R==="`"){T();let{raw:I,interp:u}=v();u?(A("TemplateHead",I,b,N),M.push(m)):A("Template",I,b,N);continue}if(R==="<"&&E){T(),A("Punctuator","<",b,N),V.push("js"),X="jsxOpenTag";continue}if(R==="{"){T(),A("Punctuator","{",b,N),m++;continue}if(R==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:u}=v();A(u?"TemplateMiddle":"TemplateTail",I,b,N),u&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),A("Punctuator","}",b,N),X=I;continue}T(),A("Punctuator","}",b,N),m--;continue}if(R==="?"&&y(1)==="."&&Z(y(2))){T(),A("Punctuator","?",b,N);continue}let U=it.find(I=>r.startsWith(I,p));if(U){for(let I=0;I<U.length;I++)T();A("Punctuator",U,b,N);continue}F(`Unexpected character '${R}'`),T(),A("Unknown",R,b,N);}return o}var ut=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ae=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Oe=new WeakMap;function dt(r){let c=Oe.get(r);if(c)return c;let i=[];for(let o of Object.keys(r)){if(ut.has(o))continue;let p=r[o];if(Ae(p))i.push(p);else if(Array.isArray(p))for(let g of p)Ae(g)&&i.push(g);}return i.sort((o,p)=>o.start-p.start),Oe.set(r,i),i}function ft(r,c){let i=r,o,p;for(;;){let g=dt(i);o=void 0,p=void 0;let f;for(let s of g){if(s.start<=c.start&&c.end<=s.end){f=s;break}if(s.end<=c.start)o=s;else if(c.end<=s.start){p=s;break}}if(!f)break;i=f;}return {enclosing:i,preceding:o,following:p}}var he=(r,c)=>{(r.leadingComments??(r.leadingComments=[])).push(c);},ue=(r,c)=>{(r.trailingComments??(r.trailingComments=[])).push(c);},ge=(r,c)=>{(r.innerComments??(r.innerComments=[])).push(c);};function yt(r,c){for(let i=c;i<r.length;i++){let o=r[i];if(o!==" "&&o!==" "&&o!=="\r"&&o!==`
5
- `&&o!=="\f")return o}return ""}var mt=new Set([")","]","}",","]);function Te(r,c,i=""){for(let o of c){let{enclosing:p,preceding:g,following:f}=ft(r,o),s=g==null||g.loc.end.line<o.loc.start.line,y=!s&&(f==null||o.loc.end.line<f.loc.start.line);if(s)f?he(f,o):g?ue(g,o):ge(p,o);else if(y)g?ue(g,o):f?he(f,o):ge(p,o);else {let F=mt.has(yt(i,o.end));g&&F?ue(g,o):f?he(f,o):g?ue(g,o):ge(p,o);}}}var be=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ie=(r,c)=>{let i=new Set(r.vars),o=new Map(r.bounds);for(let p of c)i.add(p.name),p.bound||p.isConst?o.set(p.name,{bound:p.bound,isConst:p.isConst}):o.delete(p.name);return {vars:i,bounds:o,noCond:r.noCond,inferSink:r.inferSink}},ht=(r,c)=>{let i=r.bounds.get(c);return i&&(i.bound||i.isConst)?{t:"var",name:c,...i.bound?{bound:i.bound}:{},...i.isConst?{isConst:true}:{}}:{t:"var",name:c}},je=r=>{let{peek:c,next:i,isP:o,isK:p,expectP:g,fail:f,unescape:s}=r,y=(u,h)=>{let d=r.tokens[r.pos()-1]??h;return Object.assign(u,{start:h.start,end:d.end,loc:{start:h.loc.start,end:d.loc.end}})},F=()=>{let u=r.pos(),h=r.tokens[u],d=r.tokens[u-1];return !!d&&!!h&&h.loc.start.line>d.loc.end.line},T=()=>{let u=0;for(let h=r.pos();h<r.tokens.length;h++){let d=r.tokens[h];if(d.type==="EOF")return false;if(d.type==="Punctuator"){if(["(","[","{","<"].includes(d.value))u++;else if([")","]","}",">"].includes(d.value)&&(u--,u===0)){let x=r.tokens[h+1];return !!x&&x.type==="Punctuator"&&x.value==="=>"}}}return false},a=()=>{let u=r.tokens,h=[],d=0,x=false;for(let k=r.pos();k<u.length;k++){let w=u[k];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")d++,d===1&&(x=true);else if(w.value===">"){if(d--,d===0)break}else w.value==="("||w.value==="["||w.value==="{"?(d++,x=false):w.value===")"||w.value==="]"||w.value==="}"?(d--,x=false):w.value===","&&d===1?x=true:x=false;continue}if(d===1&&x){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),x=false;continue}}x=false;}return h},P=u=>{let h=ie(u,a().map(x=>({name:x})));g("<");let d=[];for(;!o(">");){let x=p("const");x&&i();let k=c();k.type!=="Identifier"&&f("Expected type parameter name"),i();let w;p("extends")&&(i(),w=D(h).schema),o("=")&&(i(),D(h)),d.push({name:k.value,...w?{bound:w}:{},...x?{isConst:true}:{}}),o(">")||g(",");}return g(">"),d},A=u=>{g("<");let h=[];for(;!o(">");)h.push(D(u)),o(">")||g(",");return g(">"),h},L=u=>{let h=c();return h.type==="Identifier"&&h.value===u},D=u=>{let h=c(),d=v(u);if(u.noCond||!p("extends")||F())return d;i();let x=[],k=v({...u,noCond:true,inferSink:x});g("?");let w=ie(u,x.map(J=>({name:J}))),j=D(w);g(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:d.node,extendsType:k.node,trueType:j.node,falseType:$.node},h),schema:{t:"conditional",check:d.schema,extends:k.schema,then:j.schema,else:$.schema,...x.length?{infer:x}:{}}}},v=u=>{let h=c();o("|")&&i();let d=b(u);if(!o("|"))return d;let x=[d.node],k=[d.schema];for(;o("|");){i();let w=b(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSUnionType",types:x},h),schema:{t:"union",of:k}}},b=u=>{let h=c();o("&")&&i();let d=m(u);if(!o("&"))return d;let x=[d.node],k=[d.schema];for(;o("&");){i();let w=m(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSIntersectionType",types:x},h),schema:{t:"intersection",of:k}}},m=u=>{let h=c();if(L("keyof")){i();let d=m(u);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:d.node},h),schema:{t:"keyof",of:d.schema}}}if(L("readonly")){i();let d=m(u);return d.schema.t!=="array"&&d.schema.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{node:y({...d.node,readonly:true},h),schema:{...d.schema,readonly:true}}}if(L("infer")){i();let d=c();d.type!=="Identifier"&&f("Expected 'infer' variable name"),i();let x=u.inferSink;return x?(x.push(d.value),{node:y({type:"TSInferType",name:d.value},h),schema:{t:"var",name:d.value}}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return M(u)},M=u=>{let h=c(),d=X(u);for(;o("[")&&!F();)if(i(),o("]"))i(),d={node:y({type:"TSArrayType",elementType:d.node},h),schema:{t:"array",items:d.schema}};else {let x=D(u);g("]"),d={node:y({type:"TSIndexedAccessType",objectType:d.node,indexType:x.node},h),schema:{t:"index",obj:d.schema,key:x.schema}};}return d},E=(u,h,d)=>({node:y({type:"TSKeywordType",kind:h},u),schema:d}),X=u=>{let h=c();if(p("typeof")){i();let k=c();k.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),i();let w=[k.value];for(;o(".");){i();let j=c();j.type!=="Identifier"&&j.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),i(),w.push(j.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(o("<")){let k=P(u),w=ie(u,k);return z(w,h,k.map(j=>j.name))}if(o("(")){if(T())return z(u,h);i();let k=D(u.noCond?{...u,noCond:false}:u);return g(")"),k}if(o("{"))return R()?U(u):I(u);if(o("["))return N(u);let d=c();if(d.type==="String"){i();let k=s(d.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(d.type==="Number"){i();let k=Number(d.value);return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(p("true"))return i(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return i(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return i(),E(h,"null",{t:"null"});if(p("void"))return i(),E(h,"void",{t:"void"});if(d.type==="Identifier"){switch(i(),d.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let k=o("<")?A(u):[],w=k.map(J=>J.schema),j=k.length?k.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:d.value,...j?{typeArguments:j}:{}},h);return u.vars.has(d.value)&&w.length===0?{node:$,schema:ht(u,d.value)}:d.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:d.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:d.value,loc:d.loc}),{node:$,schema:{t:"apply",name:d.value,args:w}})}r.recordError(`Expected a type but got '${d.value||d.type}'`,d);let x=r.createMissingNode({type:"TSTypeReference",typeName:""},d);return r.typeRefs?.push({name:"",loc:x.loc}),{node:x,schema:{t:"unknown"}}},V=u=>{g("(");let h=[];for(;!o(")");){let d=c();d.type!=="Identifier"&&f("Expected parameter name in function type"),i();let x=o("?");x&&i(),g(":");let k=D(u);h.push({node:y({type:"TSTypeParameter",name:d.value,typeAnnotation:k.node,...x?{optional:true}:{}},d),schema:{name:d.value,type:k.schema,...x?{optional:true}:{}}}),o(")")||g(",");}return g(")"),h},z=(u,h,d)=>{let x=V(u);g("=>");let k=D(u);return {node:y({type:"TSFunctionType",params:x.map(w=>w.node),returnType:k.node,...d?.length?{typeParams:[...d]}:{}},h),schema:{t:"fn",params:x.map(w=>w.schema),returns:k.schema,...d?.length?{typeParams:d}:{}}}},N=u=>{let h=c();g("[");let d=[],x=[],k=[];for(;!o("]");){if(o("...")){i();let j=D(u);j.schema.t==="array"?(x.push(j.schema.items),d.push(j.node.elementType),k.push("rest")):(x.push(j.schema),d.push(j.node),k.push("variadic"));}else {let j=D(u),$=o("?");$&&i(),x.push(j.schema),d.push(j.node),k.push($?"optional":"req");}o("]")||g(",");}g("]");let w=k.some(j=>j!=="req");return {node:y({type:"TSTupleType",elementTypes:d,...w?{flags:k}:{}},h),schema:w?{t:"tuple",elements:x,flags:k}:{t:"tuple",elements:x}}},R=()=>{let u=r.tokens,h=r.pos()+1;return u[h]?.type==="Punctuator"&&(u[h]?.value==="+"||u[h]?.value==="-")&&h++,u[h]?.type==="Identifier"&&u[h]?.value==="readonly"&&h++,u[h]?.type==="Punctuator"&&u[h]?.value==="["&&u[h+1]?.type==="Identifier"&&u[h+2]?.type==="Keyword"&&u[h+2]?.value==="in"},U=u=>{let h=c();g("{"),(o("+")||o("-"))&&i(),L("readonly")&&i(),g("[");let d=c();d.type!=="Identifier"&&f("Expected mapped type key name"),i(),p("in")||f("Expected 'in' in mapped type"),i();let x=D(u);L("as")&&f("Key remapping ('as') in mapped types is not supported"),g("]");let k=false,w=o("+")||o("-")?i().value:null;o("?")&&(i(),k=w!=="-"),g(":");let j=D(ie(u,[{name:d.value}]));return (o(";")||o(","))&&i(),g("}"),{node:y({type:"TSMappedType",key:d.value,constraint:x.node,typeAnnotation:j.node,optional:k},h),schema:{t:"mapped",key:d.value,over:x.schema,body:j.schema,optional:k}}},I=u=>{let h=c();g("{");let d=[],x={},k,w=[];for(;!o("}");){let $=c();if(o("(")){let J=V(u);g(":");let B=D(u);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),d.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(o("[")){i(),c().type!=="Identifier"&&f("Expected index signature parameter name"),i(),g(":");let J=c();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),i(),g("]"),g(":");let B=D(u);k={key:J.value,value:B.schema},d.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=c(),B;if(J.type==="Identifier"||J.type==="Keyword")i(),B=J.value;else if(J.type==="String")i(),B=s(J.value.slice(1,-1));else if(J.type==="Number")i(),B=J.value;else return f("Expected property name in object type");let W=o("?");W&&i(),g(":");let ce=D(u);x[B]={type:ce.schema,...W?{optional:true}:{}},d.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ce.node,...W?{optional:true}:{}},$));}if(o(";")||o(","))i();else break}g("}");let j=y({type:"TSTypeLiteral",members:d},h);return w.length>0?Object.keys(x).length>0||k?f("Callable object types (call signatures mixed with members) are not supported"):{node:j,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:j,schema:{t:"object",props:x,...k?{index:k}:{}}}};return {parseType:u=>D(be(u)),parseTypeParamList:()=>P(be()),parseTypeInScope:u=>D(ie(be(),u))}};var te=class extends core.TSLiteError{constructor(i,o,p,g,f){super("parse-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="ParseError";}},Tt=r=>{let c=r;return c!=null&&typeof c.start=="number"&&c.start===c.end&&c.start>=0},bt=(r,c)=>Object.assign(r,{start:c.start,end:c.start,loc:{start:c.loc.start,end:c.loc.start}}),xt={"??":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},St=new Set(["??","||","&&"]),vt=new Set(["class","debugger","with","enum","interface"]),kt={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"},wt={namespace:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",module:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},Pt=new Set(["=","+=","-=","*=","/=","%="]);function xe(r){let c=[],i=[],o=ae(r,e=>i.push(e),e=>c.push({code:"lex-error",...e})),p=0,g=[],f=(e=0)=>o[Math.min(p+e,o.length-1)],s=()=>o[p++],y=()=>o[p-1],F=()=>{let e=o[p],n=o[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",a=e=>f().type==="Punctuator"&&f().value===e,P=e=>f().type==="Keyword"&&f().value===e,A=e=>f().type==="Identifier"&&f().value===e,L=(e,n=f())=>{throw D(e,n),new te(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=c[c.length-1];t&&t.index===n.start||c.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(a(e)||L(`Expected '${e}' but got '${f().value||f().type}'`),s()),b=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let l=e[++t];switch(l){case "n":n+=`
6
- `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=l;}}return n},E=(e=false)=>X(e),X=(e=false)=>{let n=V(e),t=f();if(t.type==="Punctuator"&&Pt.has(t.value)){s();let l=X();return m({type:"AssignmentExpression",operator:t.value,left:n,right:l},n,l)}return n},V=(e=false)=>{let n=N(0,e);if(!a("?"))return n;s();let t=E();v(":");let l=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:l},n,l)},z=()=>{let e=f();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 n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},N=(e,n=false)=>{let t=R();for(;;){let l=z();if(!l)break;let{op:S,consume:C}=l;if(S==="in"&&n)break;let O=xt[S];if(!O||O<=e)break;if(S==="satisfies"){if(F())break;L("'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(S==="as"){if(F())break;s();let K=oe(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:K.schema,typeAnnotationNode:K.node},t,y());continue}for(let K=0;K<C;K++)s();let _=N(S==="**"?O-1:O,n);t=m({type:St.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},R=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(s(),b({type:"UpdateExpression",operator:e.value,prefix:true,argument:R()},e)):P("await")?(s(),b({type:"AwaitExpression",argument:R()},e)):P("typeof")||P("void")||P("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?U(e):u()},U=e=>{s();let n=R();return a("**")&&L(`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.`),b({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=c.length,t=()=>(p=e,c.length=n,void 0);try{if(s(),a(">"))return t();let l=[];for(;l.push(oe(new Set).schema),!!a(",");)s();return a(">")?(s(),a("(")?l:t()):t()}catch{return t()}},u=()=>{let e=$e(),n=false;for(;;)if(a("?."))if(s(),n=true,a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,l);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name after '?.'"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:true},e,l);}else if(a(".")){s();let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:false},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,l);}else if(a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,l);}else if(a("<")){let t=I();if(!t)break;let{args:l,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:l,optional:false,typeArguments:t},e,S);}else if(a("++")||a("--")){let t=s();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!a(")");)e.push(d()),a(")")||v(",");return {args:e,close:v(")")}},d=()=>{if(a("...")){let e=s(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},x=()=>{let e=f();return e.type!=="Identifier"&&L("Expected identifier"),s(),b({type:"Identifier",name:e.value},e)},k=e=>{let n=a("{")?w(e):a("[")?j(e):x();if(a(":")){s();let t=oe(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!a("}");){if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(),C,O=false;if(a(":")?(s(),C=k(e)):(C={...S},O=true),a("=")){s();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:O,computed:false,method:false},S,C));}a("}")||v(",");}let l=v("}");return m({type:"ObjectPattern",properties:t},n,l)},j=e=>{let n=v("["),t=[];for(;!a("]");){if(a(",")){t.push(null),s();continue}if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=k(e);if(a("=")){s();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}a("]")||v(",");}let l=v("]");return m({type:"ArrayPattern",elements:t},n,l)},$=()=>{let e=0;for(let n=p;n<o.length;n++){let t=o[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let l=o[n+1];return !l||l.type!=="Punctuator"?false:l.value==="=>"?true:l.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<o.length;t++){let l=o[t];if(l.type==="EOF")return false;if(l.type==="Punctuator")if(["(","[","{","<"].includes(l.value))n++;else if([")","]","}",">"].includes(l.value)){if(n===0)return false;n--;}else {if(n===0&&l.value==="=>")return true;if(n===0&&(l.value===";"||l.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!a(")");){if(a("...")){let t=s(),l=k(e);n.push(m({type:"RestElement",argument:l},t,l));}else {let t=k(e);if(a("=")){s();let l=E();t=m({type:"AssignmentPattern",left:t,right:l},t,l);}n.push(t);}a(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=a("(")?B():[x()],l,S;if(a(":")){s();let O=oe(new Set);l=O.schema,S=O.node;}v("=>");let C=a("{")?se():E();return b({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...l?{returnType:l,returnTypeNode:S}:{}},n)},ce=()=>{let e=v("["),n=[];for(;!a("]");){if(a(",")){s(),n.push(null);continue}n.push(d()),a("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Fe=()=>{let e=v("{"),n=[];for(;!a("}");){if(a("...")){let t=s(),l=E();n.push(m({type:"SpreadElement",argument:l},t,l));}else {let t=f(),l,S=false;if(a("["))S=true,s(),l=E(),v("]");else if(t.type==="String")s(),l=b({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")s(),l=b({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")s(),l=b({type:"Identifier",name:t.value},t);else return L("Invalid property key");if(a(":")){s();let C=E();n.push(m({type:"Property",key:l,value:C,kind:"init",method:false,shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:l,value:l,kind:"init",method:false,shorthand:true,computed:false},t,l));}a("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},pe=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),Xe=e=>{s();let n=[pe(e.value,false)],t=[];for(;;){t.push(E());let l=f();if(l.type==="TemplateMiddle")s(),n.push(pe(l.value,false));else if(l.type==="TemplateTail"){s(),n.push(pe(l.value,true));break}else L("Expected `}` to close template interpolation");}return b({type:"TemplateLiteral",quasis:n,expressions:t},e)},re=()=>{let e=f();return e.type!=="Identifier"&&L(`Expected JSX identifier but got '${e.value||e.type}'`),b({type:"JSXIdentifier",name:s().value},e)},ke=()=>{let e=re();if(a(":")){s();let n=re();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;a(".");){s();let n=re();e=m({type:"JSXMemberExpression",object:e,property:n},e,n);}return e},Q=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${Q(e.namespace)}:${Q(e.name)}`:`${Q(e.object)}.${Q(e.property)}`,Ue=()=>{let e=f();if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(a("{")){let n=s(),t=X(),l=v("}");return m({type:"JSXExpressionContainer",expression:t},n,l)}return a("<")?fe():L(`Invalid JSX attribute value '${e.value||e.type}'`)},Ke=()=>{let e=[];for(;!a(">")&&!a("/>")&&!T();){if(a("{")){let l=s();v("...");let S=X(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},l,C));continue}let n=re();if(a(":")){s();let l=re();n=m({type:"JSXNamespacedName",namespace:n,name:l},n,l);}let t=null;a("=")&&(s(),t=Ue()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},we=()=>{let e=[];for(;!(a("</")||T());){let n=f();if(n.type==="JSXText")s(),e.push(b({type:"JSXText",value:n.value,raw:n.value},n));else if(a("{")){let t=s();if(a("}")){let l=s(),S=b({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,l));}else {let l=X(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:l},t,S));}}else a("<")?e.push(fe()):L(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},fe=()=>{let e=v("<");if(a(">")){let Y=s(),ne=m({type:"JSXOpeningFragment"},e,Y),rt=we(),ot=v("</"),Ce=v(">"),st=m({type:"JSXClosingFragment"},ot,Ce);return m({type:"JSXFragment",openingFragment:ne,children:rt,closingFragment:st},e,Ce)}let n=ke(),t=Ke();if(a("/>")){let Y=s(),ne=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Y);return m({type:"JSXElement",openingElement:ne,children:[],closingElement:null},e,Y)}let l=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,l),C=we(),O=v("</"),_=f(),K=ke();Q(K)!==Q(n)&&L(`JSX closing tag </${Q(K)}> does not match opening <${Q(n)}>`,_);let H=v(">"),G=m({type:"JSXClosingElement",name:K},O,H);return m({type:"JSXElement",openingElement:S,children:C,closingElement:G},e,H)},$e=()=>{let e=f();if(P("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(s(),W(true,e)):L("Unexpected 'async'")}if(a("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return s(),b({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return s(),b({type:"TemplateLiteral",expressions:[],quasis:[pe(e.value,true)]},e);if(e.type==="TemplateHead")return Xe(e);if(e.type==="Keyword")return e.value==="true"?(s(),b({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(s(),b({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(s(),b({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?L("'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"?L("Anonymous functions are not supported \u2014 use an arrow function"):L(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return s(),b({type:"Identifier",name:e.value},e);if(a("(")){s();let n=E();return v(")"),n}return a("[")?ce():a("{")?Fe():a("<")?fe():L(`Unexpected token '${e.value||e.type}'`)},{parseType:oe,parseTypeParamList:Me,parseTypeInScope:Be}=je({tokens:o,pos:()=>p,peek:f,next:s,isP:a,isK:P,expectP:v,fail:L,recordError:D,createMissingNode:bt,unescape:M,typeRefs:g}),_e=()=>{let e=s(),n=x(),t=[],l=[];a("<")&&(l=Me(),t=l.map(O=>O.name)),v("=");let S=Be(l),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return a(";")&&s(),C},q=()=>{let e=f();if(e.type==="Identifier"&&e.value==="type"&&f(1).type==="Identifier"&&f(2).type==="Punctuator"&&(f(2).value==="="||f(2).value==="<"))return _e();if(P("const")||P("let")||P("var")){let S=Pe();return a(";")&&s(),S}if(P("if"))return Ye();if(P("for"))return Ve();if(P("while"))return qe();if(P("do"))return Ge();if(P("break")||P("continue"))return Qe();if(P("return"))return Ze();if(P("throw"))return et();if(P("try"))return tt();if(P("switch"))return nt();if(P("import"))return We();if(P("export"))return He();if(a("{"))return se();if((P("function")||P("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&L("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return s(),b({type:"EmptyStatement"},e);if(e.type==="Keyword"&&vt.has(e.value)||e.type==="Identifier"&&wt[e.value]?.(f(1),f(2))===true){let S=kt[e.value];L(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),l=a(";")?s():y();return m({type:"ExpressionStatement",expression:t},t,l)},Pe=()=>{let e=s(),n=e.value,t=()=>{let S=k(new Set),C=null;return a("=")?(s(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},l=[t()];for(;a(",");)s(),l.push(t());return m({type:"VariableDeclaration",kind:n,declarations:l},e,l[l.length-1])},ze=()=>{let e=f();return e.type!=="String"&&L("Expected module specifier string"),s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},We=()=>{let e=s(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let l=x();n.push(m({type:"ImportDefaultSpecifier",local:l},l,l)),a(",")&&s();}if(a("*")){let l=s();A("as")||L("Expected 'as' after '*' in import"),s();let S=x();n.push(m({type:"ImportNamespaceSpecifier",local:S},l,S));}else if(a("{")){for(s();!a("}")&&!T();){let l=x(),S=l;if(A("as")&&(s(),S=x()),n.push(m({type:"ImportSpecifier",imported:l,local:S},l,S)),a(","))s();else break}v("}");}A("from")||L("Expected 'from' in import declaration"),s();}let t=ze();return a(";")&&s(),b({type:"ImportDeclaration",specifiers:n,source:t},e)},He=()=>{let e=s();if(P("default")){s();let n=E();return a(";")&&s(),b({type:"ExportDefaultDeclaration",declaration:n},e)}if(P("const")||P("let")||P("var")){let n=Pe();return a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(a("{")){s();let n=[];for(;!a("}")&&!T();){let t=x(),l=t;if(A("as")&&(s(),l=x()),n.push(m({type:"ExportSpecifier",local:t,exported:l},t,l)),a(","))s();else break}return v("}"),a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return L("Expected 'const', '{', or 'default' after 'export'")},Ne=()=>f().type==="Identifier"&&f().value==="of",ye=()=>P("in"),Ve=()=>{let e=s();v("(");let n=null,t=null;if(!a(";"))if(P("const")||P("let")||P("var")){let O=s(),_=()=>{let H=x();if(a(":")){s();let G=oe(new Set);H.typeAnnotation=G.schema,H.typeAnnotationNode=G.node;}return H},K=_();if(Ne()||ye()){let H=m({type:"VariableDeclarator",id:K,init:null},K,K);t=m({type:"VariableDeclaration",kind:O.value,declarations:[H]},O,K);}else {let H=Y=>{let ne=null;return a("=")?(s(),ne=E(true)):O.value==="const"&&L("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Y,init:ne},Y,ne??Y)},G=[H(K)];for(;a(",");)s(),G.push(H(_()));n=m({type:"VariableDeclaration",kind:O.value,declarations:G},O,G[G.length-1]);}}else {let O=E(true);Ne()||ye()?t=O:n=O;}if(t){let O=ye();s();let _=E();v(")");let K=q();return m(O?{type:"ForInStatement",left:t,right:_,body:K}:{type:"ForOfStatement",left:t,right:_,body:K,await:false},e,K)}v(";");let l=a(";")?null:E();v(";");let S=a(")")?null:E();v(")");let C=q();return m({type:"ForStatement",init:n,test:l,update:S,body:C},e,C)},Ge=()=>{let e=s(),n=q();P("while")||L("Expected 'while' after the 'do' body"),s(),v("(");let t=E(),l=v(")");return a(";")&&s(),m({type:"DoWhileStatement",body:n,test:t},e,l)},Qe=()=>{let e=s(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?s():y();return m({type:n,label:null},e,t)},qe=()=>{let e=s();v("(");let n=E();v(")");let t=q();return m({type:"WhileStatement",test:n,body:t},e,t)},Ye=()=>{let e=s();v("(");let n=E();v(")");let t=q(),l=null;return P("else")&&(s(),l=q()),m({type:"IfStatement",test:n,consequent:t,alternate:l},e,l??t)},Ze=()=>{let e=s(),n=null;!a(";")&&!a("}")&&!T()&&(n=E());let t=a(";")?s():y();return m({type:"ReturnStatement",argument:n},e,t)},et=()=>{let e=s(),n=E(),t=a(";")?s():y();return m({type:"ThrowStatement",argument:n},e,t)},tt=()=>{let e=s(),n=se(),t=null,l=null;if(P("catch")){let S=s(),C=null;a("(")&&(s(),C=x(),v(")"));let O=se();t=m({type:"CatchClause",param:C,body:O},S,O);}return P("finally")&&(s(),l=se()),!t&&!l&&L("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:l},e,l??t??n)},nt=()=>{let e=s();v("(");let n=E();v(")"),v("{");let t=[];for(;!a("}")&&!T();){let S=f(),C=null;P("case")?(s(),C=E()):P("default")?s():L("Expected 'case' or 'default' in switch"),v(":");let O=[];for(;!a("}")&&!P("case")&&!P("default")&&!T();)O.push(q());t.push(m({type:"SwitchCase",test:C,consequent:O},S,y()??S));}let l=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,l)},se=()=>{let e=v("{"),n=Ee(()=>a("}"));return m({type:"BlockStatement",body:n},e,v("}"))},Ee=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){s();continue}try{n.push(q());}catch(t){if(!(t instanceof te))throw t;T()||s();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=Ee(()=>false);return m({type:"Program",sourceType:"module",body:n,...g.length?{typeRefs:g}:{},...i.length?{comments:i}:{},...c.length?{parseDiagnostics:c}:{}},e,y()??e)},eof:T,fail:L,recordError:D,diagnostics:c}}function De(r){let c=xe(r),i=c.parseProgram(),o=i.comments;return o?.length&&Te(i,o,r),{ast:i,diagnostics:c.diagnostics}}function Nt(r){let{ast:c,diagnostics:i}=De(r),o=i[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return c}function Et(r){let c=xe(r),i=c.parseExpr();c.eof()||c.fail("Unexpected token after expression");let o=c.diagnostics[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return i}function Se(r){let c=xe(r),i;try{i=c.parseExpr(),c.eof()||c.recordError("Unexpected token after expression");}catch(o){if(!(o instanceof te)&&!(o instanceof ee))throw o}return {ast:i,diagnostics:c.diagnostics}}var Je="{{";function Ct(r){let c=[0];for(let i=0;i<r.length;i++)r[i]===`
7
- `&&c.push(i+1);return c}function de(r,c){let i=0,o=r.length-1;for(;i<o;){let p=i+o+1>>1;r[p]<=c?i=p:o=p-1;}return {line:i+1,column:c-r[i]}}var le=(r,c,i)=>({start:de(r,c),end:de(r,i)});function ve(r,c,i){if(Array.isArray(r)){for(let p of r)ve(p,c,i);return}if(!r||typeof r!="object")return;let o=r;typeof o.start=="number"&&typeof o.end=="number"&&(o.start+=c,o.end+=c,o.loc=le(i,o.start,o.end));for(let p in o)p!=="loc"&&ve(o[p],c,i);}function It(r,c,i){let o=r.index+c,p=r.end+c;return {...r,index:o,end:p,position:de(i,o),loc:le(i,o,p)}}var Lt=(r,c,i,o)=>({code:"parse-error",message:r,index:c,end:i,position:de(o,c),loc:le(o,c,i)}),Rt=new Set(["{","(","["]),At=new Set(["}",")","]"]);function Ot(r,c){let i=ae(r.slice(c),void 0,()=>{}),o=0;for(let p=0;p<i.length;p++){let g=i[p];if(g.type==="Punctuator"){if(Rt.has(g.value))o++;else if(At.has(g.value)){if(o>0)o--;else if(g.value==="}"){let f=i[p+1];if(f?.type==="Punctuator"&&f.value==="}"&&f.start===g.end)return c+g.start}}}}return -1}function jt(r){let c=Ct(r),i=[],o=[],p=[],g=0,f=0;for(;f<r.length;)if(r[f]==="{"&&r[f+1]==="{"){let a=f+Je.length,P=Ot(r,a);if(P===-1){p.push(Lt("Unterminated '{{' \u2014 expected '}}'.",f,r.length,c));break}i.push({text:r.slice(g,f),start:g}),o.push({inner:r.slice(a,P),start:a}),g=P+Je.length,f=g;}else f++;i.push({text:r.slice(g),start:g});let s=o.map(a=>{let{ast:P,diagnostics:A}=Se(a.inner);ve(P,a.start,c);for(let L of A)p.push(It(L,a.start,c));return P});if(p.length)return {kind:"error",diagnostics:p};if(o.length===0)return {kind:"literal"};let y=s;if(o.length===1&&i.every(a=>a.text===""))return {kind:"expression",node:y[0]};let F=(a,P)=>({type:"TemplateElement",value:{raw:a.text,cooked:a.text},tail:P,start:a.start,end:a.start+a.text.length,loc:le(c,a.start,a.start+a.text.length)});return {kind:"template",node:{type:"TemplateLiteral",quasis:i.map((a,P)=>F(a,P===i.length-1)),expressions:y,start:0,end:r.length,loc:le(c,0,r.length)}}}exports.KEYWORDS=Le;exports.LexError=ee;exports.ParseError=te;exports.attachComments=Te;exports.nodeIsMissing=Tt;exports.parse=Nt;exports.parseExpression=Et;exports.parseExpressionTolerant=Se;exports.parseInterpolated=jt;exports.parseTolerant=De;exports.tokenize=ae;
4
+ `||N==="\r"||N==="\u2028"||N==="\u2029",P=()=>{for(;;){let N=y();if(N===" "||N===" "||N==="\f"||N==="\v"||a(N))T();else if(N==="/"&&y(1)==="/"){let L=p,U=s();T(),T();let I=p;for(;p<r.length&&!a(y());)T();c?.({type:"Line",value:r.slice(I,p),start:L,end:p,loc:{start:U,end:s()}});}else if(N==="/"&&y(1)==="*"){let L=p,U=s();T(),T();let I=p,u=p;for(;;){if(p>=r.length){F("Unterminated block comment"),u=p;break}if(y()==="*"&&y(1)==="/"){u=p,T(),T();break}T();}c?.({type:"Block",value:r.slice(I,u),start:L,end:p,loc:{start:U,end:s()}});}else break}},A=(N,L,U,I)=>{o.push({type:N,value:L,start:U,end:p,loc:{start:I,end:s()}}),E=N==="Punctuator"?pt.has(L):N==="Keyword"?ut.has(L):N==="TemplateHead"||N==="TemplateMiddle";},R=N=>{for(T();p<r.length;){let L=y();if(L===N)return T(),r.slice(b,p);if(a(L))return F("Unterminated string literal"),r.slice(b,p);if(L==="\\"){if(T(),p>=r.length)return F("Unterminated string literal"),r.slice(b,p);T();continue}T();}return F("Unterminated string literal"),r.slice(b,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!Ie(y()))return F("Invalid hexadecimal literal");for(;Ie(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return F("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return F("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return F("Invalid exponent");for(;Z(y());)T();}},v=()=>{let N=p;for(;p<r.length;){let L=y();if(L==="`"){let U=r.slice(N,p);return T(),{raw:U,interp:false}}if(L==="$"&&y(1)==="{"){let U=r.slice(N,p);return T(),T(),{raw:U,interp:true}}if(L==="\\"){if(T(),p>=r.length)break;T();continue}T();}return F("Unterminated template literal"),{raw:r.slice(N,p),interp:false}},b=0,m=0,M=[],E=true,X="js",V=[],z=[];for(;;){if(X==="jsxChildren"){b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(u==="<"){y(1)==="/"?(T(),T(),A("Punctuator","</",b,I),X="jsxCloseTag"):(T(),A("Punctuator","<",b,I),V.push("jsxChildren"),X="jsxOpenTag");continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:"jsxChildren"}),X="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();A("JSXText",r.slice(b,p),b,I);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){P(),b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(me(u)){for(T();ct(y());)T();A("Identifier",r.slice(b,p),b,I);continue}if(u==="/"&&y(1)===">"){T(),T(),A("Punctuator","/>",b,I),X=V.pop()??"js";continue}if(u===">"){T(),A("Punctuator",">",b,I),X=X==="jsxCloseTag"?V.pop()??"js":"jsxChildren";continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:X}),X="js";continue}if(u==='"'||u==="'"){A("String",R(u),b,I);continue}if(u==="="||u==="."||u===":"){T(),A("Punctuator",u,b,I);continue}F(`Unexpected character '${u}' in JSX tag`),T(),A("Unknown",u,b,I);continue}P(),b=p;let N=s();if(p>=r.length){A("EOF","",b,N);break}let L=y();if(me(L)){for(;Re(y());)T();let I=r.slice(b,p);A(Le.has(I)?"Keyword":"Identifier",I,b,N);continue}if(Z(L)||L==="."&&Z(y(1))){D(),A("Number",r.slice(b,p),b,N);continue}if(L==='"'||L==="'"){let I=R(L);A("String",I,b,N);continue}if(L==="`"){T();let{raw:I,interp:u}=v();u?(A("TemplateHead",I,b,N),M.push(m)):A("Template",I,b,N);continue}if(L==="<"&&E){T(),A("Punctuator","<",b,N),V.push("js"),X="jsxOpenTag";continue}if(L==="{"){T(),A("Punctuator","{",b,N),m++;continue}if(L==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:u}=v();A(u?"TemplateMiddle":"TemplateTail",I,b,N),u&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),A("Punctuator","}",b,N),X=I;continue}T(),A("Punctuator","}",b,N),m--;continue}if(L==="?"&&y(1)==="."&&Z(y(2))){T(),A("Punctuator","?",b,N);continue}let U=lt.find(I=>r.startsWith(I,p));if(U){for(let I=0;I<U.length;I++)T();A("Punctuator",U,b,N);continue}F(`Unexpected character '${L}'`),T(),A("Unknown",L,b,N);}return o}var dt=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ae=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Oe=new WeakMap;function ft(r){let c=Oe.get(r);if(c)return c;let l=[];for(let o of Object.keys(r)){if(dt.has(o))continue;let p=r[o];if(Ae(p))l.push(p);else if(Array.isArray(p))for(let g of p)Ae(g)&&l.push(g);}return l.sort((o,p)=>o.start-p.start),Oe.set(r,l),l}function yt(r,c){let l=r,o,p;for(;;){let g=ft(l);o=void 0,p=void 0;let f;for(let s of g){if(s.start<=c.start&&c.end<=s.end){f=s;break}if(s.end<=c.start)o=s;else if(c.end<=s.start){p=s;break}}if(!f)break;l=f;}return {enclosing:l,preceding:o,following:p}}var he=(r,c)=>{(r.leadingComments??(r.leadingComments=[])).push(c);},ue=(r,c)=>{(r.trailingComments??(r.trailingComments=[])).push(c);},ge=(r,c)=>{(r.innerComments??(r.innerComments=[])).push(c);};function mt(r,c){for(let l=c;l<r.length;l++){let o=r[l];if(o!==" "&&o!==" "&&o!=="\r"&&o!==`
5
+ `&&o!=="\f")return o}return ""}var ht=new Set([")","]","}",","]);function Te(r,c,l=""){for(let o of c){let{enclosing:p,preceding:g,following:f}=yt(r,o),s=g==null||g.loc.end.line<o.loc.start.line,y=!s&&(f==null||o.loc.end.line<f.loc.start.line);if(s)f?he(f,o):g?ue(g,o):ge(p,o);else if(y)g?ue(g,o):f?he(f,o):ge(p,o);else {let F=ht.has(mt(l,o.end));g&&F?ue(g,o):f?he(f,o):g?ue(g,o):ge(p,o);}}}var be=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ie=(r,c)=>{let l=new Set(r.vars),o=new Map(r.bounds);for(let p of c)l.add(p.name),p.bound||p.isConst?o.set(p.name,{bound:p.bound,isConst:p.isConst}):o.delete(p.name);return {vars:l,bounds:o,noCond:r.noCond,inferSink:r.inferSink}},gt=(r,c)=>{let l=r.bounds.get(c);return l&&(l.bound||l.isConst)?{t:"var",name:c,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:c}},je=r=>{let{peek:c,next:l,isP:o,isK:p,expectP:g,fail:f,unescape:s}=r,y=(u,h)=>{let d=r.tokens[r.pos()-1]??h;return Object.assign(u,{start:h.start,end:d.end,loc:{start:h.loc.start,end:d.loc.end}})},F=()=>{let u=r.pos(),h=r.tokens[u],d=r.tokens[u-1];return !!d&&!!h&&h.loc.start.line>d.loc.end.line},T=()=>{let u=0;for(let h=r.pos();h<r.tokens.length;h++){let d=r.tokens[h];if(d.type==="EOF")return false;if(d.type==="Punctuator"){if(["(","[","{","<"].includes(d.value))u++;else if([")","]","}",">"].includes(d.value)&&(u--,u===0)){let x=r.tokens[h+1];return !!x&&x.type==="Punctuator"&&x.value==="=>"}}}return false},a=()=>{let u=r.tokens,h=[],d=0,x=false;for(let k=r.pos();k<u.length;k++){let w=u[k];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")d++,d===1&&(x=true);else if(w.value===">"){if(d--,d===0)break}else w.value==="("||w.value==="["||w.value==="{"?(d++,x=false):w.value===")"||w.value==="]"||w.value==="}"?(d--,x=false):w.value===","&&d===1?x=true:x=false;continue}if(d===1&&x){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),x=false;continue}}x=false;}return h},P=u=>{let h=ie(u,a().map(x=>({name:x})));g("<");let d=[];for(;!o(">");){let x=p("const");x&&l();let k=c();k.type!=="Identifier"&&f("Expected type parameter name"),l();let w;p("extends")&&(l(),w=D(h).schema),o("=")&&(l(),D(h)),d.push({name:k.value,...w?{bound:w}:{},...x?{isConst:true}:{}}),o(">")||g(",");}return g(">"),d},A=u=>{g("<");let h=[];for(;!o(">");)h.push(D(u)),o(">")||g(",");return g(">"),h},R=u=>{let h=c();return h.type==="Identifier"&&h.value===u},D=u=>{let h=c(),d=v(u);if(u.noCond||!p("extends")||F())return d;l();let x=[],k=v({...u,noCond:true,inferSink:x});g("?");let w=ie(u,x.map(J=>({name:J}))),j=D(w);g(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:d.node,extendsType:k.node,trueType:j.node,falseType:$.node},h),schema:{t:"conditional",check:d.schema,extends:k.schema,then:j.schema,else:$.schema,...x.length?{infer:x}:{}}}},v=u=>{let h=c();o("|")&&l();let d=b(u);if(!o("|"))return d;let x=[d.node],k=[d.schema];for(;o("|");){l();let w=b(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSUnionType",types:x},h),schema:{t:"union",of:k}}},b=u=>{let h=c();o("&")&&l();let d=m(u);if(!o("&"))return d;let x=[d.node],k=[d.schema];for(;o("&");){l();let w=m(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSIntersectionType",types:x},h),schema:{t:"intersection",of:k}}},m=u=>{let h=c();if(R("keyof")){l();let d=m(u);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:d.node},h),schema:{t:"keyof",of:d.schema}}}if(R("readonly")){l();let d=m(u);return d.schema.t!=="array"&&d.schema.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{node:y({...d.node,readonly:true},h),schema:{...d.schema,readonly:true}}}if(R("infer")){l();let d=c();d.type!=="Identifier"&&f("Expected 'infer' variable name"),l();let x=u.inferSink;return x?(x.push(d.value),{node:y({type:"TSInferType",name:d.value},h),schema:{t:"var",name:d.value}}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return M(u)},M=u=>{let h=c(),d=X(u);for(;o("[")&&!F();)if(l(),o("]"))l(),d={node:y({type:"TSArrayType",elementType:d.node},h),schema:{t:"array",items:d.schema}};else {let x=D(u);g("]"),d={node:y({type:"TSIndexedAccessType",objectType:d.node,indexType:x.node},h),schema:{t:"index",obj:d.schema,key:x.schema}};}return d},E=(u,h,d)=>({node:y({type:"TSKeywordType",kind:h},u),schema:d}),X=u=>{let h=c();if(p("typeof")){l();let k=c();k.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),l();let w=[k.value];for(;o(".");){l();let j=c();j.type!=="Identifier"&&j.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),l(),w.push(j.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(o("<")){let k=P(u),w=ie(u,k);return z(w,h,k.map(j=>j.name))}if(o("(")){if(T())return z(u,h);l();let k=D(u.noCond?{...u,noCond:false}:u);return g(")"),k}if(o("{"))return L()?U(u):I(u);if(o("["))return N(u);let d=c();if(d.type==="String"){l();let k=s(d.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(d.type==="Number"){l();let k=Number(d.value);return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(p("true"))return l(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return l(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return l(),E(h,"null",{t:"null"});if(p("void"))return l(),E(h,"void",{t:"void"});if(d.type==="Identifier"){switch(l(),d.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let k=o("<")?A(u):[],w=k.map(J=>J.schema),j=k.length?k.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:d.value,...j?{typeArguments:j}:{}},h);return u.vars.has(d.value)&&w.length===0?{node:$,schema:gt(u,d.value)}:d.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:d.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:d.value,loc:d.loc}),{node:$,schema:{t:"apply",name:d.value,args:w}})}r.recordError(`Expected a type but got '${d.value||d.type}'`,d);let x=r.createMissingNode({type:"TSTypeReference",typeName:""},d);return r.typeRefs?.push({name:"",loc:x.loc}),{node:x,schema:{t:"unknown"}}},V=u=>{g("(");let h=[];for(;!o(")");){let d=c();d.type!=="Identifier"&&f("Expected parameter name in function type"),l();let x=o("?");x&&l(),g(":");let k=D(u);h.push({node:y({type:"TSTypeParameter",name:d.value,typeAnnotation:k.node,...x?{optional:true}:{}},d),schema:{name:d.value,type:k.schema,...x?{optional:true}:{}}}),o(")")||g(",");}return g(")"),h},z=(u,h,d)=>{let x=V(u);g("=>");let k=D(u);return {node:y({type:"TSFunctionType",params:x.map(w=>w.node),returnType:k.node,...d?.length?{typeParams:[...d]}:{}},h),schema:{t:"fn",params:x.map(w=>w.schema),returns:k.schema,...d?.length?{typeParams:d}:{}}}},N=u=>{let h=c();g("[");let d=[],x=[],k=[];for(;!o("]");){if(o("...")){l();let j=D(u);j.schema.t==="array"?(x.push(j.schema.items),d.push(j.node.elementType),k.push("rest")):(x.push(j.schema),d.push(j.node),k.push("variadic"));}else {let j=D(u),$=o("?");$&&l(),x.push(j.schema),d.push(j.node),k.push($?"optional":"req");}o("]")||g(",");}g("]");let w=k.some(j=>j!=="req");return {node:y({type:"TSTupleType",elementTypes:d,...w?{flags:k}:{}},h),schema:w?{t:"tuple",elements:x,flags:k}:{t:"tuple",elements:x}}},L=()=>{let u=r.tokens,h=r.pos()+1;return u[h]?.type==="Punctuator"&&(u[h]?.value==="+"||u[h]?.value==="-")&&h++,u[h]?.type==="Identifier"&&u[h]?.value==="readonly"&&h++,u[h]?.type==="Punctuator"&&u[h]?.value==="["&&u[h+1]?.type==="Identifier"&&u[h+2]?.type==="Keyword"&&u[h+2]?.value==="in"},U=u=>{let h=c();g("{"),(o("+")||o("-"))&&l(),R("readonly")&&l(),g("[");let d=c();d.type!=="Identifier"&&f("Expected mapped type key name"),l(),p("in")||f("Expected 'in' in mapped type"),l();let x=D(u);R("as")&&f("Key remapping ('as') in mapped types is not supported"),g("]");let k=false,w=o("+")||o("-")?l().value:null;o("?")&&(l(),k=w!=="-"),g(":");let j=D(ie(u,[{name:d.value}]));return (o(";")||o(","))&&l(),g("}"),{node:y({type:"TSMappedType",key:d.value,constraint:x.node,typeAnnotation:j.node,optional:k},h),schema:{t:"mapped",key:d.value,over:x.schema,body:j.schema,optional:k}}},I=u=>{let h=c();g("{");let d=[],x={},k,w=[];for(;!o("}");){let $=c();if(o("(")){let J=V(u);g(":");let B=D(u);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),d.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(o("[")){l(),c().type!=="Identifier"&&f("Expected index signature parameter name"),l(),g(":");let J=c();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),l(),g("]"),g(":");let B=D(u);k={key:J.value,value:B.schema},d.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=c(),B;if(J.type==="Identifier"||J.type==="Keyword")l(),B=J.value;else if(J.type==="String")l(),B=s(J.value.slice(1,-1));else if(J.type==="Number")l(),B=J.value;else return f("Expected property name in object type");let W=o("?");W&&l(),g(":");let ce=D(u);x[B]={type:ce.schema,...W?{optional:true}:{}},d.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ce.node,...W?{optional:true}:{}},$));}if(o(";")||o(","))l();else break}g("}");let j=y({type:"TSTypeLiteral",members:d},h);return w.length>0?Object.keys(x).length>0||k?f("Callable object types (call signatures mixed with members) are not supported"):{node:j,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:j,schema:{t:"object",props:x,...k?{index:k}:{}}}};return {parseType:u=>D(be(u)),parseTypeParamList:()=>P(be()),parseTypeInScope:u=>D(ie(be(),u))}};var te=class extends core.TSLiteError{constructor(l,o,p,g,f){super("parse-error",`${l} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="ParseError";}},bt=r=>{let c=r;return c!=null&&typeof c.start=="number"&&c.start===c.end&&c.start>=0},De=(r,c)=>Object.assign(r,{start:c.start,end:c.start,loc:{start:c.loc.start,end:c.loc.start}}),xt={"??":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},St=new Set(["??","||","&&"]),vt=new Set(["class","debugger","with","enum","interface"]),kt={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"},wt={namespace:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",module:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},Pt=new Set(["=","+=","-=","*=","/=","%="]);function xe(r){let c=[],l=[],o=ae(r,e=>l.push(e),e=>c.push({code:"lex-error",...e})),p=0,g=[],f=(e=0)=>o[Math.min(p+e,o.length-1)],s=()=>o[p++],y=()=>o[p-1],F=()=>{let e=o[p],n=o[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",a=e=>f().type==="Punctuator"&&f().value===e,P=e=>f().type==="Keyword"&&f().value===e,A=e=>f().type==="Identifier"&&f().value===e,R=(e,n=f())=>{throw D(e,n),new te(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=c[c.length-1];t&&t.index===n.start||c.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(a(e)||R(`Expected '${e}' but got '${f().value||f().type}'`),s()),b=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let i=e[++t];switch(i){case "n":n+=`
6
+ `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=i;}}return n},E=(e=false)=>X(e),X=(e=false)=>{let n=V(e),t=f();if(t.type==="Punctuator"&&Pt.has(t.value)){s();let i=X();return m({type:"AssignmentExpression",operator:t.value,left:n,right:i},n,i)}return n},V=(e=false)=>{let n=N(0,e);if(!a("?"))return n;s();let t=E();v(":");let i=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:i},n,i)},z=()=>{let e=f();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 n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},N=(e,n=false)=>{let t=L();for(;;){let i=z();if(!i)break;let{op:S,consume:C}=i;if(S==="in"&&n)break;let O=xt[S];if(!O||O<=e)break;if(S==="satisfies"){if(F())break;R("'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(S==="as"){if(F())break;s();let K=oe(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:K.schema,typeAnnotationNode:K.node},t,y());continue}for(let K=0;K<C;K++)s();let _=N(S==="**"?O-1:O,n);t=m({type:St.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},L=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(s(),b({type:"UpdateExpression",operator:e.value,prefix:true,argument:L()},e)):P("await")?(s(),b({type:"AwaitExpression",argument:L()},e)):P("typeof")||P("void")||P("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?U(e):u()},U=e=>{s();let n=L();return a("**")&&R(`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.`),b({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=c.length,t=()=>(p=e,c.length=n,void 0);try{if(s(),a(">"))return t();let i=[];for(;i.push(oe(new Set).schema),!!a(",");)s();return a(">")?(s(),a("(")?i:t()):t()}catch{return t()}},u=()=>{let e=Me(),n=false;for(;;)if(a("?."))if(s(),n=true,a("(")){let{args:t,close:i}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,i);}else if(a("[")){s();let t=E(),i=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,i);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&R("Expected property name after '?.'"),s();let i=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:i,computed:false,optional:true},e,i);}else if(a(".")){s();let t=f(),i;t.type==="Identifier"||t.type==="Keyword"?(s(),i=b({type:"Identifier",name:t.value},t)):(D("Expected property name"),i=De({type:"Identifier",name:""},t)),e=m({type:"MemberExpression",object:e,property:i,computed:false,optional:false},e,i);}else if(a("[")){s();let t=E(),i=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,i);}else if(a("(")){let{args:t,close:i}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,i);}else if(a("<")){let t=I();if(!t)break;let{args:i,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:i,optional:false,typeArguments:t},e,S);}else if(a("++")||a("--")){let t=s();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!a(")");)e.push(d()),a(")")||v(",");return {args:e,close:v(")")}},d=()=>{if(a("...")){let e=s(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},x=()=>{let e=f();return e.type!=="Identifier"&&R("Expected identifier"),s(),b({type:"Identifier",name:e.value},e)},k=e=>{let n=a("{")?w(e):a("[")?j(e):x();if(a(":")){s();let t=oe(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!a("}");){if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(),C,O=false;if(a(":")?(s(),C=k(e)):(C={...S},O=true),a("=")){s();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:O,computed:false},S,C));}a("}")||v(",");}let i=v("}");return m({type:"ObjectPattern",properties:t},n,i)},j=e=>{let n=v("["),t=[];for(;!a("]");){if(a(",")){t.push(null),s();continue}if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=k(e);if(a("=")){s();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}a("]")||v(",");}let i=v("]");return m({type:"ArrayPattern",elements:t},n,i)},$=()=>{let e=0;for(let n=p;n<o.length;n++){let t=o[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let i=o[n+1];return !i||i.type!=="Punctuator"?false:i.value==="=>"?true:i.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<o.length;t++){let i=o[t];if(i.type==="EOF")return false;if(i.type==="Punctuator")if(["(","[","{","<"].includes(i.value))n++;else if([")","]","}",">"].includes(i.value)){if(n===0)return false;n--;}else {if(n===0&&i.value==="=>")return true;if(n===0&&(i.value===";"||i.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!a(")");){if(a("...")){let t=s(),i=k(e);n.push(m({type:"RestElement",argument:i},t,i));}else {let t=k(e);if(a("=")){s();let i=E();t=m({type:"AssignmentPattern",left:t,right:i},t,i);}n.push(t);}a(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=a("(")?B():[x()],i,S;if(a(":")){s();let O=oe(new Set);i=O.schema,S=O.node;}v("=>");let C=a("{")?se():E();return b({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...i?{returnType:i,returnTypeNode:S}:{}},n)},ce=()=>{let e=v("["),n=[];for(;!a("]");){if(a(",")){s(),n.push(null);continue}n.push(d()),a("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Xe=()=>{let e=v("{"),n=[];for(;!a("}");){if(a("...")){let t=s(),i=E();n.push(m({type:"SpreadElement",argument:i},t,i));}else {let t=f(),i,S=false;if(a("["))S=true,s(),i=E(),v("]");else if(t.type==="String")s(),i=b({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")s(),i=b({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")s(),i=b({type:"Identifier",name:t.value},t);else return R("Invalid property key");if(a(":")){s();let C=E();n.push(m({type:"Property",key:i,value:C,kind:"init",shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:i,value:i,kind:"init",shorthand:true,computed:false},t,i));}a("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},pe=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),Ue=e=>{s();let n=[pe(e.value,false)],t=[];for(;;){t.push(E());let i=f();if(i.type==="TemplateMiddle")s(),n.push(pe(i.value,false));else if(i.type==="TemplateTail"){s(),n.push(pe(i.value,true));break}else R("Expected `}` to close template interpolation");}return b({type:"TemplateLiteral",quasis:n,expressions:t},e)},re=()=>{let e=f();return e.type!=="Identifier"&&R(`Expected JSX identifier but got '${e.value||e.type}'`),b({type:"JSXIdentifier",name:s().value},e)},ke=()=>{let e=re();if(a(":")){s();let n=re();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;a(".");){s();let n=re();e=m({type:"JSXMemberExpression",object:e,property:n},e,n);}return e},Q=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${Q(e.namespace)}:${Q(e.name)}`:`${Q(e.object)}.${Q(e.property)}`,Ke=()=>{let e=f();if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(a("{")){let n=s(),t=X(),i=v("}");return m({type:"JSXExpressionContainer",expression:t},n,i)}return a("<")?fe():R(`Invalid JSX attribute value '${e.value||e.type}'`)},$e=()=>{let e=[];for(;!a(">")&&!a("/>")&&!T();){if(a("{")){let i=s();v("...");let S=X(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},i,C));continue}let n=re();if(a(":")){s();let i=re();n=m({type:"JSXNamespacedName",namespace:n,name:i},n,i);}let t=null;a("=")&&(s(),t=Ke()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},we=()=>{let e=[];for(;!(a("</")||T());){let n=f();if(n.type==="JSXText")s(),e.push(b({type:"JSXText",value:n.value,raw:n.value},n));else if(a("{")){let t=s();if(a("}")){let i=s(),S=b({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,i));}else {let i=X(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:i},t,S));}}else a("<")?e.push(fe()):R(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},fe=()=>{let e=v("<");if(a(">")){let Y=s(),ne=m({type:"JSXOpeningFragment"},e,Y),ot=we(),st=v("</"),Ce=v(">"),at=m({type:"JSXClosingFragment"},st,Ce);return m({type:"JSXFragment",openingFragment:ne,children:ot,closingFragment:at},e,Ce)}let n=ke(),t=$e();if(a("/>")){let Y=s(),ne=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Y);return m({type:"JSXElement",openingElement:ne,children:[],closingElement:null},e,Y)}let i=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,i),C=we(),O=v("</"),_=f(),K=ke();Q(K)!==Q(n)&&R(`JSX closing tag </${Q(K)}> does not match opening <${Q(n)}>`,_);let H=v(">"),G=m({type:"JSXClosingElement",name:K},O,H);return m({type:"JSXElement",openingElement:S,children:C,closingElement:G},e,H)},Me=()=>{let e=f();if(P("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(s(),W(true,e)):R("Unexpected 'async'")}if(a("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return s(),b({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return s(),b({type:"TemplateLiteral",expressions:[],quasis:[pe(e.value,true)]},e);if(e.type==="TemplateHead")return Ue(e);if(e.type==="Keyword")return e.value==="true"?(s(),b({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(s(),b({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(s(),b({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?R("'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"?R("Anonymous functions are not supported \u2014 use an arrow function"):R(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return s(),b({type:"Identifier",name:e.value},e);if(a("(")){s();let n=E();return v(")"),n}return a("[")?ce():a("{")?Xe():a("<")?fe():R(`Unexpected token '${e.value||e.type}'`)},{parseType:oe,parseTypeParamList:Be,parseTypeInScope:_e}=je({tokens:o,pos:()=>p,peek:f,next:s,isP:a,isK:P,expectP:v,fail:R,recordError:D,createMissingNode:De,unescape:M,typeRefs:g}),ze=()=>{let e=s(),n=x(),t=[],i=[];a("<")&&(i=Be(),t=i.map(O=>O.name)),v("=");let S=_e(i),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return a(";")&&s(),C},q=()=>{let e=f();if(e.type==="Identifier"&&e.value==="type"&&f(1).type==="Identifier"&&f(2).type==="Punctuator"&&(f(2).value==="="||f(2).value==="<"))return ze();if(P("const")||P("let")||P("var")){let S=Pe();return a(";")&&s(),S}if(P("if"))return Ze();if(P("for"))return Ge();if(P("while"))return Ye();if(P("do"))return Qe();if(P("break")||P("continue"))return qe();if(P("return"))return et();if(P("throw"))return tt();if(P("try"))return nt();if(P("switch"))return rt();if(P("import"))return He();if(P("export"))return Ve();if(a("{"))return se();if((P("function")||P("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&R("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return s(),b({type:"EmptyStatement"},e);if(e.type==="Keyword"&&vt.has(e.value)||e.type==="Identifier"&&wt[e.value]?.(f(1),f(2))===true){let S=kt[e.value];R(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),i=a(";")?s():y();return m({type:"ExpressionStatement",expression:t},t,i)},Pe=()=>{let e=s(),n=e.value,t=()=>{let S=k(new Set),C=null;return a("=")?(s(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},i=[t()];for(;a(",");)s(),i.push(t());return m({type:"VariableDeclaration",kind:n,declarations:i},e,i[i.length-1])},We=()=>{let e=f();return e.type!=="String"&&R("Expected module specifier string"),s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},He=()=>{let e=s(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let i=x();n.push(m({type:"ImportDefaultSpecifier",local:i},i,i)),a(",")&&s();}if(a("*")){let i=s();A("as")||R("Expected 'as' after '*' in import"),s();let S=x();n.push(m({type:"ImportNamespaceSpecifier",local:S},i,S));}else if(a("{")){for(s();!a("}")&&!T();){let i=x(),S=i;if(A("as")&&(s(),S=x()),n.push(m({type:"ImportSpecifier",imported:i,local:S},i,S)),a(","))s();else break}v("}");}A("from")||R("Expected 'from' in import declaration"),s();}let t=We();return a(";")&&s(),b({type:"ImportDeclaration",specifiers:n,source:t},e)},Ve=()=>{let e=s();if(P("default")){s();let n=E();return a(";")&&s(),b({type:"ExportDefaultDeclaration",declaration:n},e)}if(P("const")||P("let")||P("var")){let n=Pe();return a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(a("{")){s();let n=[];for(;!a("}")&&!T();){let t=x(),i=t;if(A("as")&&(s(),i=x()),n.push(m({type:"ExportSpecifier",local:t,exported:i},t,i)),a(","))s();else break}return v("}"),a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return R("Expected 'const', '{', or 'default' after 'export'")},Ne=()=>f().type==="Identifier"&&f().value==="of",ye=()=>P("in"),Ge=()=>{let e=s();v("(");let n=null,t=null;if(!a(";"))if(P("const")||P("let")||P("var")){let O=s(),_=()=>{let H=x();if(a(":")){s();let G=oe(new Set);H.typeAnnotation=G.schema,H.typeAnnotationNode=G.node;}return H},K=_();if(Ne()||ye()){let H=m({type:"VariableDeclarator",id:K,init:null},K,K);t=m({type:"VariableDeclaration",kind:O.value,declarations:[H]},O,K);}else {let H=Y=>{let ne=null;return a("=")?(s(),ne=E(true)):O.value==="const"&&R("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Y,init:ne},Y,ne??Y)},G=[H(K)];for(;a(",");)s(),G.push(H(_()));n=m({type:"VariableDeclaration",kind:O.value,declarations:G},O,G[G.length-1]);}}else {let O=E(true);Ne()||ye()?t=O:n=O;}if(t){let O=ye();s();let _=E();v(")");let K=q();return m(O?{type:"ForInStatement",left:t,right:_,body:K}:{type:"ForOfStatement",left:t,right:_,body:K,await:false},e,K)}v(";");let i=a(";")?null:E();v(";");let S=a(")")?null:E();v(")");let C=q();return m({type:"ForStatement",init:n,test:i,update:S,body:C},e,C)},Qe=()=>{let e=s(),n=q();P("while")||R("Expected 'while' after the 'do' body"),s(),v("(");let t=E(),i=v(")");return a(";")&&s(),m({type:"DoWhileStatement",body:n,test:t},e,i)},qe=()=>{let e=s(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?s():y();return m({type:n,label:null},e,t)},Ye=()=>{let e=s();v("(");let n=E();v(")");let t=q();return m({type:"WhileStatement",test:n,body:t},e,t)},Ze=()=>{let e=s();v("(");let n=E();v(")");let t=q(),i=null;return P("else")&&(s(),i=q()),m({type:"IfStatement",test:n,consequent:t,alternate:i},e,i??t)},et=()=>{let e=s(),n=null;!a(";")&&!a("}")&&!T()&&(n=E());let t=a(";")?s():y();return m({type:"ReturnStatement",argument:n},e,t)},tt=()=>{let e=s(),n=E(),t=a(";")?s():y();return m({type:"ThrowStatement",argument:n},e,t)},nt=()=>{let e=s(),n=se(),t=null,i=null;if(P("catch")){let S=s(),C=null;a("(")&&(s(),C=x(),v(")"));let O=se();t=m({type:"CatchClause",param:C,body:O},S,O);}return P("finally")&&(s(),i=se()),!t&&!i&&R("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:i},e,i??t??n)},rt=()=>{let e=s();v("(");let n=E();v(")"),v("{");let t=[];for(;!a("}")&&!T();){let S=f(),C=null;P("case")?(s(),C=E()):P("default")?s():R("Expected 'case' or 'default' in switch"),v(":");let O=[];for(;!a("}")&&!P("case")&&!P("default")&&!T();)O.push(q());t.push(m({type:"SwitchCase",test:C,consequent:O},S,y()??S));}let i=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,i)},se=()=>{let e=v("{"),n=Ee(()=>a("}"));return m({type:"BlockStatement",body:n},e,v("}"))},Ee=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){s();continue}try{n.push(q());}catch(t){if(!(t instanceof te))throw t;T()||s();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=Ee(()=>false);return m({type:"Program",sourceType:"module",body:n,...g.length?{typeRefs:g}:{},...l.length?{comments:l}:{},...c.length?{parseDiagnostics:c}:{}},e,y()??e)},eof:T,fail:R,recordError:D,diagnostics:c}}function Je(r){let c=xe(r),l=c.parseProgram(),o=l.comments;return o?.length&&Te(l,o,r),{ast:l,diagnostics:c.diagnostics}}function Nt(r){let{ast:c,diagnostics:l}=Je(r),o=l[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return c}function Et(r){let c=xe(r),l=c.parseExpr();c.eof()||c.fail("Unexpected token after expression");let o=c.diagnostics[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return l}function Se(r){let c=xe(r),l;try{l=c.parseExpr(),c.eof()||c.recordError("Unexpected token after expression");}catch(o){if(!(o instanceof te)&&!(o instanceof ee))throw o}return {ast:l,diagnostics:c.diagnostics}}var Fe="{{";function Ct(r){let c=[0];for(let l=0;l<r.length;l++)r[l]===`
7
+ `&&c.push(l+1);return c}function de(r,c){let l=0,o=r.length-1;for(;l<o;){let p=l+o+1>>1;r[p]<=c?l=p:o=p-1;}return {line:l+1,column:c-r[l]}}var le=(r,c,l)=>({start:de(r,c),end:de(r,l)});function ve(r,c,l){if(Array.isArray(r)){for(let p of r)ve(p,c,l);return}if(!r||typeof r!="object")return;let o=r;typeof o.start=="number"&&typeof o.end=="number"&&(o.start+=c,o.end+=c,o.loc=le(l,o.start,o.end));for(let p in o)p!=="loc"&&ve(o[p],c,l);}function It(r,c,l){let o=r.index+c,p=r.end+c;return {...r,index:o,end:p,position:de(l,o),loc:le(l,o,p)}}var Lt=(r,c,l,o)=>({code:"parse-error",message:r,index:c,end:l,position:de(o,c),loc:le(o,c,l)}),Rt=new Set(["{","(","["]),At=new Set(["}",")","]"]);function Ot(r,c){let l=ae(r.slice(c),void 0,()=>{}),o=0;for(let p=0;p<l.length;p++){let g=l[p];if(g.type==="Punctuator"){if(Rt.has(g.value))o++;else if(At.has(g.value)){if(o>0)o--;else if(g.value==="}"){let f=l[p+1];if(f?.type==="Punctuator"&&f.value==="}"&&f.start===g.end)return c+g.start}}}}return -1}function jt(r){let c=Ct(r),l=[],o=[],p=[],g=0,f=0;for(;f<r.length;)if(r[f]==="{"&&r[f+1]==="{"){let a=f+Fe.length,P=Ot(r,a);if(P===-1){p.push(Lt("Unterminated '{{' \u2014 expected '}}'.",f,r.length,c));break}l.push({text:r.slice(g,f),start:g}),o.push({inner:r.slice(a,P),start:a}),g=P+Fe.length,f=g;}else f++;l.push({text:r.slice(g),start:g});let s=o.map(a=>{let{ast:P,diagnostics:A}=Se(a.inner);ve(P,a.start,c);for(let R of A)p.push(It(R,a.start,c));return P});if(p.length)return {kind:"error",diagnostics:p};if(o.length===0)return {kind:"literal"};let y=s;if(o.length===1&&l.every(a=>a.text===""))return {kind:"expression",node:y[0]};let F=(a,P)=>({type:"TemplateElement",value:{raw:a.text,cooked:a.text},tail:P,start:a.start,end:a.start+a.text.length,loc:le(c,a.start,a.start+a.text.length)});return {kind:"template",node:{type:"TemplateLiteral",quasis:l.map((a,P)=>F(a,P===l.length-1)),expressions:y,start:0,end:r.length,loc:le(c,0,r.length)}}}exports.KEYWORDS=Le;exports.LexError=ee;exports.ParseError=te;exports.attachComments=Te;exports.nodeIsMissing=bt;exports.parse=Nt;exports.parseExpression=Et;exports.parseExpressionTolerant=Se;exports.parseInterpolated=jt;exports.parseTolerant=Je;exports.tokenize=ae;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import {TSLiteError}from'@tslite/core';var ee=class extends TSLiteError{constructor(i,o,p,g,f){super("lex-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="LexError";}},Le=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"]),it=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],me=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Re=r=>me(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",Ie=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",lt=r=>Re(r)||r==="-",ct=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),pt=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(r,c,i){let o=[],p=0,g=1,f=0,s=()=>({line:g,column:f}),y=(N=0)=>r[p+N]??"",F=N=>{let R=s(),U={start:R,end:{line:g,column:f+1}};if(i){i({message:N,position:R,index:p,loc:U,end:p+1});return}throw new ee(N,R,p,U,p+1)},T=()=>{let N=r[p++];return N==="\r"?(r[p]===`
1
+ import {TSLiteError}from'@tslite/core';var ee=class extends TSLiteError{constructor(l,o,p,g,f){super("lex-error",`${l} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="LexError";}},Le=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"]),lt=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],me=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Re=r=>me(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",Ie=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",ct=r=>Re(r)||r==="-",pt=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),ut=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(r,c,l){let o=[],p=0,g=1,f=0,s=()=>({line:g,column:f}),y=(N=0)=>r[p+N]??"",F=N=>{let L=s(),U={start:L,end:{line:g,column:f+1}};if(l){l({message:N,position:L,index:p,loc:U,end:p+1});return}throw new ee(N,L,p,U,p+1)},T=()=>{let N=r[p++];return N==="\r"?(r[p]===`
2
2
  `&&p++,g++,f=0):N===`
3
3
  `||N==="\u2028"||N==="\u2029"?(g++,f=0):f++,N},a=N=>N===`
4
- `||N==="\r"||N==="\u2028"||N==="\u2029",P=()=>{for(;;){let N=y();if(N===" "||N===" "||N==="\f"||N==="\v"||a(N))T();else if(N==="/"&&y(1)==="/"){let R=p,U=s();T(),T();let I=p;for(;p<r.length&&!a(y());)T();c?.({type:"Line",value:r.slice(I,p),start:R,end:p,loc:{start:U,end:s()}});}else if(N==="/"&&y(1)==="*"){let R=p,U=s();T(),T();let I=p,u=p;for(;;){if(p>=r.length){F("Unterminated block comment"),u=p;break}if(y()==="*"&&y(1)==="/"){u=p,T(),T();break}T();}c?.({type:"Block",value:r.slice(I,u),start:R,end:p,loc:{start:U,end:s()}});}else break}},A=(N,R,U,I)=>{o.push({type:N,value:R,start:U,end:p,loc:{start:I,end:s()}}),E=N==="Punctuator"?ct.has(R):N==="Keyword"?pt.has(R):N==="TemplateHead"||N==="TemplateMiddle";},L=N=>{for(T();p<r.length;){let R=y();if(R===N)return T(),r.slice(b,p);if(a(R))return F("Unterminated string literal"),r.slice(b,p);if(R==="\\"){if(T(),p>=r.length)return F("Unterminated string literal"),r.slice(b,p);T();continue}T();}return F("Unterminated string literal"),r.slice(b,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!Ie(y()))return F("Invalid hexadecimal literal");for(;Ie(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return F("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return F("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return F("Invalid exponent");for(;Z(y());)T();}},v=()=>{let N=p;for(;p<r.length;){let R=y();if(R==="`"){let U=r.slice(N,p);return T(),{raw:U,interp:false}}if(R==="$"&&y(1)==="{"){let U=r.slice(N,p);return T(),T(),{raw:U,interp:true}}if(R==="\\"){if(T(),p>=r.length)break;T();continue}T();}return F("Unterminated template literal"),{raw:r.slice(N,p),interp:false}},b=0,m=0,M=[],E=true,X="js",V=[],z=[];for(;;){if(X==="jsxChildren"){b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(u==="<"){y(1)==="/"?(T(),T(),A("Punctuator","</",b,I),X="jsxCloseTag"):(T(),A("Punctuator","<",b,I),V.push("jsxChildren"),X="jsxOpenTag");continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:"jsxChildren"}),X="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();A("JSXText",r.slice(b,p),b,I);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){P(),b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(me(u)){for(T();lt(y());)T();A("Identifier",r.slice(b,p),b,I);continue}if(u==="/"&&y(1)===">"){T(),T(),A("Punctuator","/>",b,I),X=V.pop()??"js";continue}if(u===">"){T(),A("Punctuator",">",b,I),X=X==="jsxCloseTag"?V.pop()??"js":"jsxChildren";continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:X}),X="js";continue}if(u==='"'||u==="'"){A("String",L(u),b,I);continue}if(u==="="||u==="."||u===":"){T(),A("Punctuator",u,b,I);continue}F(`Unexpected character '${u}' in JSX tag`),T(),A("Unknown",u,b,I);continue}P(),b=p;let N=s();if(p>=r.length){A("EOF","",b,N);break}let R=y();if(me(R)){for(;Re(y());)T();let I=r.slice(b,p);A(Le.has(I)?"Keyword":"Identifier",I,b,N);continue}if(Z(R)||R==="."&&Z(y(1))){D(),A("Number",r.slice(b,p),b,N);continue}if(R==='"'||R==="'"){let I=L(R);A("String",I,b,N);continue}if(R==="`"){T();let{raw:I,interp:u}=v();u?(A("TemplateHead",I,b,N),M.push(m)):A("Template",I,b,N);continue}if(R==="<"&&E){T(),A("Punctuator","<",b,N),V.push("js"),X="jsxOpenTag";continue}if(R==="{"){T(),A("Punctuator","{",b,N),m++;continue}if(R==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:u}=v();A(u?"TemplateMiddle":"TemplateTail",I,b,N),u&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),A("Punctuator","}",b,N),X=I;continue}T(),A("Punctuator","}",b,N),m--;continue}if(R==="?"&&y(1)==="."&&Z(y(2))){T(),A("Punctuator","?",b,N);continue}let U=it.find(I=>r.startsWith(I,p));if(U){for(let I=0;I<U.length;I++)T();A("Punctuator",U,b,N);continue}F(`Unexpected character '${R}'`),T(),A("Unknown",R,b,N);}return o}var ut=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ae=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Oe=new WeakMap;function dt(r){let c=Oe.get(r);if(c)return c;let i=[];for(let o of Object.keys(r)){if(ut.has(o))continue;let p=r[o];if(Ae(p))i.push(p);else if(Array.isArray(p))for(let g of p)Ae(g)&&i.push(g);}return i.sort((o,p)=>o.start-p.start),Oe.set(r,i),i}function ft(r,c){let i=r,o,p;for(;;){let g=dt(i);o=void 0,p=void 0;let f;for(let s of g){if(s.start<=c.start&&c.end<=s.end){f=s;break}if(s.end<=c.start)o=s;else if(c.end<=s.start){p=s;break}}if(!f)break;i=f;}return {enclosing:i,preceding:o,following:p}}var he=(r,c)=>{(r.leadingComments??(r.leadingComments=[])).push(c);},ue=(r,c)=>{(r.trailingComments??(r.trailingComments=[])).push(c);},ge=(r,c)=>{(r.innerComments??(r.innerComments=[])).push(c);};function yt(r,c){for(let i=c;i<r.length;i++){let o=r[i];if(o!==" "&&o!==" "&&o!=="\r"&&o!==`
5
- `&&o!=="\f")return o}return ""}var mt=new Set([")","]","}",","]);function Te(r,c,i=""){for(let o of c){let{enclosing:p,preceding:g,following:f}=ft(r,o),s=g==null||g.loc.end.line<o.loc.start.line,y=!s&&(f==null||o.loc.end.line<f.loc.start.line);if(s)f?he(f,o):g?ue(g,o):ge(p,o);else if(y)g?ue(g,o):f?he(f,o):ge(p,o);else {let F=mt.has(yt(i,o.end));g&&F?ue(g,o):f?he(f,o):g?ue(g,o):ge(p,o);}}}var be=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ie=(r,c)=>{let i=new Set(r.vars),o=new Map(r.bounds);for(let p of c)i.add(p.name),p.bound||p.isConst?o.set(p.name,{bound:p.bound,isConst:p.isConst}):o.delete(p.name);return {vars:i,bounds:o,noCond:r.noCond,inferSink:r.inferSink}},ht=(r,c)=>{let i=r.bounds.get(c);return i&&(i.bound||i.isConst)?{t:"var",name:c,...i.bound?{bound:i.bound}:{},...i.isConst?{isConst:true}:{}}:{t:"var",name:c}},je=r=>{let{peek:c,next:i,isP:o,isK:p,expectP:g,fail:f,unescape:s}=r,y=(u,h)=>{let d=r.tokens[r.pos()-1]??h;return Object.assign(u,{start:h.start,end:d.end,loc:{start:h.loc.start,end:d.loc.end}})},F=()=>{let u=r.pos(),h=r.tokens[u],d=r.tokens[u-1];return !!d&&!!h&&h.loc.start.line>d.loc.end.line},T=()=>{let u=0;for(let h=r.pos();h<r.tokens.length;h++){let d=r.tokens[h];if(d.type==="EOF")return false;if(d.type==="Punctuator"){if(["(","[","{","<"].includes(d.value))u++;else if([")","]","}",">"].includes(d.value)&&(u--,u===0)){let x=r.tokens[h+1];return !!x&&x.type==="Punctuator"&&x.value==="=>"}}}return false},a=()=>{let u=r.tokens,h=[],d=0,x=false;for(let k=r.pos();k<u.length;k++){let w=u[k];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")d++,d===1&&(x=true);else if(w.value===">"){if(d--,d===0)break}else w.value==="("||w.value==="["||w.value==="{"?(d++,x=false):w.value===")"||w.value==="]"||w.value==="}"?(d--,x=false):w.value===","&&d===1?x=true:x=false;continue}if(d===1&&x){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),x=false;continue}}x=false;}return h},P=u=>{let h=ie(u,a().map(x=>({name:x})));g("<");let d=[];for(;!o(">");){let x=p("const");x&&i();let k=c();k.type!=="Identifier"&&f("Expected type parameter name"),i();let w;p("extends")&&(i(),w=D(h).schema),o("=")&&(i(),D(h)),d.push({name:k.value,...w?{bound:w}:{},...x?{isConst:true}:{}}),o(">")||g(",");}return g(">"),d},A=u=>{g("<");let h=[];for(;!o(">");)h.push(D(u)),o(">")||g(",");return g(">"),h},L=u=>{let h=c();return h.type==="Identifier"&&h.value===u},D=u=>{let h=c(),d=v(u);if(u.noCond||!p("extends")||F())return d;i();let x=[],k=v({...u,noCond:true,inferSink:x});g("?");let w=ie(u,x.map(J=>({name:J}))),j=D(w);g(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:d.node,extendsType:k.node,trueType:j.node,falseType:$.node},h),schema:{t:"conditional",check:d.schema,extends:k.schema,then:j.schema,else:$.schema,...x.length?{infer:x}:{}}}},v=u=>{let h=c();o("|")&&i();let d=b(u);if(!o("|"))return d;let x=[d.node],k=[d.schema];for(;o("|");){i();let w=b(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSUnionType",types:x},h),schema:{t:"union",of:k}}},b=u=>{let h=c();o("&")&&i();let d=m(u);if(!o("&"))return d;let x=[d.node],k=[d.schema];for(;o("&");){i();let w=m(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSIntersectionType",types:x},h),schema:{t:"intersection",of:k}}},m=u=>{let h=c();if(L("keyof")){i();let d=m(u);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:d.node},h),schema:{t:"keyof",of:d.schema}}}if(L("readonly")){i();let d=m(u);return d.schema.t!=="array"&&d.schema.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{node:y({...d.node,readonly:true},h),schema:{...d.schema,readonly:true}}}if(L("infer")){i();let d=c();d.type!=="Identifier"&&f("Expected 'infer' variable name"),i();let x=u.inferSink;return x?(x.push(d.value),{node:y({type:"TSInferType",name:d.value},h),schema:{t:"var",name:d.value}}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return M(u)},M=u=>{let h=c(),d=X(u);for(;o("[")&&!F();)if(i(),o("]"))i(),d={node:y({type:"TSArrayType",elementType:d.node},h),schema:{t:"array",items:d.schema}};else {let x=D(u);g("]"),d={node:y({type:"TSIndexedAccessType",objectType:d.node,indexType:x.node},h),schema:{t:"index",obj:d.schema,key:x.schema}};}return d},E=(u,h,d)=>({node:y({type:"TSKeywordType",kind:h},u),schema:d}),X=u=>{let h=c();if(p("typeof")){i();let k=c();k.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),i();let w=[k.value];for(;o(".");){i();let j=c();j.type!=="Identifier"&&j.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),i(),w.push(j.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(o("<")){let k=P(u),w=ie(u,k);return z(w,h,k.map(j=>j.name))}if(o("(")){if(T())return z(u,h);i();let k=D(u.noCond?{...u,noCond:false}:u);return g(")"),k}if(o("{"))return R()?U(u):I(u);if(o("["))return N(u);let d=c();if(d.type==="String"){i();let k=s(d.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(d.type==="Number"){i();let k=Number(d.value);return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(p("true"))return i(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return i(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return i(),E(h,"null",{t:"null"});if(p("void"))return i(),E(h,"void",{t:"void"});if(d.type==="Identifier"){switch(i(),d.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let k=o("<")?A(u):[],w=k.map(J=>J.schema),j=k.length?k.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:d.value,...j?{typeArguments:j}:{}},h);return u.vars.has(d.value)&&w.length===0?{node:$,schema:ht(u,d.value)}:d.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:d.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:d.value,loc:d.loc}),{node:$,schema:{t:"apply",name:d.value,args:w}})}r.recordError(`Expected a type but got '${d.value||d.type}'`,d);let x=r.createMissingNode({type:"TSTypeReference",typeName:""},d);return r.typeRefs?.push({name:"",loc:x.loc}),{node:x,schema:{t:"unknown"}}},V=u=>{g("(");let h=[];for(;!o(")");){let d=c();d.type!=="Identifier"&&f("Expected parameter name in function type"),i();let x=o("?");x&&i(),g(":");let k=D(u);h.push({node:y({type:"TSTypeParameter",name:d.value,typeAnnotation:k.node,...x?{optional:true}:{}},d),schema:{name:d.value,type:k.schema,...x?{optional:true}:{}}}),o(")")||g(",");}return g(")"),h},z=(u,h,d)=>{let x=V(u);g("=>");let k=D(u);return {node:y({type:"TSFunctionType",params:x.map(w=>w.node),returnType:k.node,...d?.length?{typeParams:[...d]}:{}},h),schema:{t:"fn",params:x.map(w=>w.schema),returns:k.schema,...d?.length?{typeParams:d}:{}}}},N=u=>{let h=c();g("[");let d=[],x=[],k=[];for(;!o("]");){if(o("...")){i();let j=D(u);j.schema.t==="array"?(x.push(j.schema.items),d.push(j.node.elementType),k.push("rest")):(x.push(j.schema),d.push(j.node),k.push("variadic"));}else {let j=D(u),$=o("?");$&&i(),x.push(j.schema),d.push(j.node),k.push($?"optional":"req");}o("]")||g(",");}g("]");let w=k.some(j=>j!=="req");return {node:y({type:"TSTupleType",elementTypes:d,...w?{flags:k}:{}},h),schema:w?{t:"tuple",elements:x,flags:k}:{t:"tuple",elements:x}}},R=()=>{let u=r.tokens,h=r.pos()+1;return u[h]?.type==="Punctuator"&&(u[h]?.value==="+"||u[h]?.value==="-")&&h++,u[h]?.type==="Identifier"&&u[h]?.value==="readonly"&&h++,u[h]?.type==="Punctuator"&&u[h]?.value==="["&&u[h+1]?.type==="Identifier"&&u[h+2]?.type==="Keyword"&&u[h+2]?.value==="in"},U=u=>{let h=c();g("{"),(o("+")||o("-"))&&i(),L("readonly")&&i(),g("[");let d=c();d.type!=="Identifier"&&f("Expected mapped type key name"),i(),p("in")||f("Expected 'in' in mapped type"),i();let x=D(u);L("as")&&f("Key remapping ('as') in mapped types is not supported"),g("]");let k=false,w=o("+")||o("-")?i().value:null;o("?")&&(i(),k=w!=="-"),g(":");let j=D(ie(u,[{name:d.value}]));return (o(";")||o(","))&&i(),g("}"),{node:y({type:"TSMappedType",key:d.value,constraint:x.node,typeAnnotation:j.node,optional:k},h),schema:{t:"mapped",key:d.value,over:x.schema,body:j.schema,optional:k}}},I=u=>{let h=c();g("{");let d=[],x={},k,w=[];for(;!o("}");){let $=c();if(o("(")){let J=V(u);g(":");let B=D(u);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),d.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(o("[")){i(),c().type!=="Identifier"&&f("Expected index signature parameter name"),i(),g(":");let J=c();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),i(),g("]"),g(":");let B=D(u);k={key:J.value,value:B.schema},d.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=c(),B;if(J.type==="Identifier"||J.type==="Keyword")i(),B=J.value;else if(J.type==="String")i(),B=s(J.value.slice(1,-1));else if(J.type==="Number")i(),B=J.value;else return f("Expected property name in object type");let W=o("?");W&&i(),g(":");let ce=D(u);x[B]={type:ce.schema,...W?{optional:true}:{}},d.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ce.node,...W?{optional:true}:{}},$));}if(o(";")||o(","))i();else break}g("}");let j=y({type:"TSTypeLiteral",members:d},h);return w.length>0?Object.keys(x).length>0||k?f("Callable object types (call signatures mixed with members) are not supported"):{node:j,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:j,schema:{t:"object",props:x,...k?{index:k}:{}}}};return {parseType:u=>D(be(u)),parseTypeParamList:()=>P(be()),parseTypeInScope:u=>D(ie(be(),u))}};var te=class extends TSLiteError{constructor(i,o,p,g,f){super("parse-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="ParseError";}},Tt=r=>{let c=r;return c!=null&&typeof c.start=="number"&&c.start===c.end&&c.start>=0},bt=(r,c)=>Object.assign(r,{start:c.start,end:c.start,loc:{start:c.loc.start,end:c.loc.start}}),xt={"??":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},St=new Set(["??","||","&&"]),vt=new Set(["class","debugger","with","enum","interface"]),kt={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"},wt={namespace:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",module:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},Pt=new Set(["=","+=","-=","*=","/=","%="]);function xe(r){let c=[],i=[],o=ae(r,e=>i.push(e),e=>c.push({code:"lex-error",...e})),p=0,g=[],f=(e=0)=>o[Math.min(p+e,o.length-1)],s=()=>o[p++],y=()=>o[p-1],F=()=>{let e=o[p],n=o[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",a=e=>f().type==="Punctuator"&&f().value===e,P=e=>f().type==="Keyword"&&f().value===e,A=e=>f().type==="Identifier"&&f().value===e,L=(e,n=f())=>{throw D(e,n),new te(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=c[c.length-1];t&&t.index===n.start||c.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(a(e)||L(`Expected '${e}' but got '${f().value||f().type}'`),s()),b=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let l=e[++t];switch(l){case "n":n+=`
6
- `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=l;}}return n},E=(e=false)=>X(e),X=(e=false)=>{let n=V(e),t=f();if(t.type==="Punctuator"&&Pt.has(t.value)){s();let l=X();return m({type:"AssignmentExpression",operator:t.value,left:n,right:l},n,l)}return n},V=(e=false)=>{let n=N(0,e);if(!a("?"))return n;s();let t=E();v(":");let l=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:l},n,l)},z=()=>{let e=f();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 n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},N=(e,n=false)=>{let t=R();for(;;){let l=z();if(!l)break;let{op:S,consume:C}=l;if(S==="in"&&n)break;let O=xt[S];if(!O||O<=e)break;if(S==="satisfies"){if(F())break;L("'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(S==="as"){if(F())break;s();let K=oe(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:K.schema,typeAnnotationNode:K.node},t,y());continue}for(let K=0;K<C;K++)s();let _=N(S==="**"?O-1:O,n);t=m({type:St.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},R=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(s(),b({type:"UpdateExpression",operator:e.value,prefix:true,argument:R()},e)):P("await")?(s(),b({type:"AwaitExpression",argument:R()},e)):P("typeof")||P("void")||P("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?U(e):u()},U=e=>{s();let n=R();return a("**")&&L(`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.`),b({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=c.length,t=()=>(p=e,c.length=n,void 0);try{if(s(),a(">"))return t();let l=[];for(;l.push(oe(new Set).schema),!!a(",");)s();return a(">")?(s(),a("(")?l:t()):t()}catch{return t()}},u=()=>{let e=$e(),n=false;for(;;)if(a("?."))if(s(),n=true,a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,l);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name after '?.'"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:true},e,l);}else if(a(".")){s();let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:false},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,l);}else if(a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,l);}else if(a("<")){let t=I();if(!t)break;let{args:l,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:l,optional:false,typeArguments:t},e,S);}else if(a("++")||a("--")){let t=s();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!a(")");)e.push(d()),a(")")||v(",");return {args:e,close:v(")")}},d=()=>{if(a("...")){let e=s(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},x=()=>{let e=f();return e.type!=="Identifier"&&L("Expected identifier"),s(),b({type:"Identifier",name:e.value},e)},k=e=>{let n=a("{")?w(e):a("[")?j(e):x();if(a(":")){s();let t=oe(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!a("}");){if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(),C,O=false;if(a(":")?(s(),C=k(e)):(C={...S},O=true),a("=")){s();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:O,computed:false,method:false},S,C));}a("}")||v(",");}let l=v("}");return m({type:"ObjectPattern",properties:t},n,l)},j=e=>{let n=v("["),t=[];for(;!a("]");){if(a(",")){t.push(null),s();continue}if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=k(e);if(a("=")){s();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}a("]")||v(",");}let l=v("]");return m({type:"ArrayPattern",elements:t},n,l)},$=()=>{let e=0;for(let n=p;n<o.length;n++){let t=o[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let l=o[n+1];return !l||l.type!=="Punctuator"?false:l.value==="=>"?true:l.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<o.length;t++){let l=o[t];if(l.type==="EOF")return false;if(l.type==="Punctuator")if(["(","[","{","<"].includes(l.value))n++;else if([")","]","}",">"].includes(l.value)){if(n===0)return false;n--;}else {if(n===0&&l.value==="=>")return true;if(n===0&&(l.value===";"||l.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!a(")");){if(a("...")){let t=s(),l=k(e);n.push(m({type:"RestElement",argument:l},t,l));}else {let t=k(e);if(a("=")){s();let l=E();t=m({type:"AssignmentPattern",left:t,right:l},t,l);}n.push(t);}a(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=a("(")?B():[x()],l,S;if(a(":")){s();let O=oe(new Set);l=O.schema,S=O.node;}v("=>");let C=a("{")?se():E();return b({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...l?{returnType:l,returnTypeNode:S}:{}},n)},ce=()=>{let e=v("["),n=[];for(;!a("]");){if(a(",")){s(),n.push(null);continue}n.push(d()),a("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Fe=()=>{let e=v("{"),n=[];for(;!a("}");){if(a("...")){let t=s(),l=E();n.push(m({type:"SpreadElement",argument:l},t,l));}else {let t=f(),l,S=false;if(a("["))S=true,s(),l=E(),v("]");else if(t.type==="String")s(),l=b({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")s(),l=b({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")s(),l=b({type:"Identifier",name:t.value},t);else return L("Invalid property key");if(a(":")){s();let C=E();n.push(m({type:"Property",key:l,value:C,kind:"init",method:false,shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:l,value:l,kind:"init",method:false,shorthand:true,computed:false},t,l));}a("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},pe=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),Xe=e=>{s();let n=[pe(e.value,false)],t=[];for(;;){t.push(E());let l=f();if(l.type==="TemplateMiddle")s(),n.push(pe(l.value,false));else if(l.type==="TemplateTail"){s(),n.push(pe(l.value,true));break}else L("Expected `}` to close template interpolation");}return b({type:"TemplateLiteral",quasis:n,expressions:t},e)},re=()=>{let e=f();return e.type!=="Identifier"&&L(`Expected JSX identifier but got '${e.value||e.type}'`),b({type:"JSXIdentifier",name:s().value},e)},ke=()=>{let e=re();if(a(":")){s();let n=re();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;a(".");){s();let n=re();e=m({type:"JSXMemberExpression",object:e,property:n},e,n);}return e},Q=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${Q(e.namespace)}:${Q(e.name)}`:`${Q(e.object)}.${Q(e.property)}`,Ue=()=>{let e=f();if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(a("{")){let n=s(),t=X(),l=v("}");return m({type:"JSXExpressionContainer",expression:t},n,l)}return a("<")?fe():L(`Invalid JSX attribute value '${e.value||e.type}'`)},Ke=()=>{let e=[];for(;!a(">")&&!a("/>")&&!T();){if(a("{")){let l=s();v("...");let S=X(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},l,C));continue}let n=re();if(a(":")){s();let l=re();n=m({type:"JSXNamespacedName",namespace:n,name:l},n,l);}let t=null;a("=")&&(s(),t=Ue()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},we=()=>{let e=[];for(;!(a("</")||T());){let n=f();if(n.type==="JSXText")s(),e.push(b({type:"JSXText",value:n.value,raw:n.value},n));else if(a("{")){let t=s();if(a("}")){let l=s(),S=b({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,l));}else {let l=X(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:l},t,S));}}else a("<")?e.push(fe()):L(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},fe=()=>{let e=v("<");if(a(">")){let Y=s(),ne=m({type:"JSXOpeningFragment"},e,Y),rt=we(),ot=v("</"),Ce=v(">"),st=m({type:"JSXClosingFragment"},ot,Ce);return m({type:"JSXFragment",openingFragment:ne,children:rt,closingFragment:st},e,Ce)}let n=ke(),t=Ke();if(a("/>")){let Y=s(),ne=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Y);return m({type:"JSXElement",openingElement:ne,children:[],closingElement:null},e,Y)}let l=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,l),C=we(),O=v("</"),_=f(),K=ke();Q(K)!==Q(n)&&L(`JSX closing tag </${Q(K)}> does not match opening <${Q(n)}>`,_);let H=v(">"),G=m({type:"JSXClosingElement",name:K},O,H);return m({type:"JSXElement",openingElement:S,children:C,closingElement:G},e,H)},$e=()=>{let e=f();if(P("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(s(),W(true,e)):L("Unexpected 'async'")}if(a("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return s(),b({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return s(),b({type:"TemplateLiteral",expressions:[],quasis:[pe(e.value,true)]},e);if(e.type==="TemplateHead")return Xe(e);if(e.type==="Keyword")return e.value==="true"?(s(),b({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(s(),b({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(s(),b({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?L("'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"?L("Anonymous functions are not supported \u2014 use an arrow function"):L(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return s(),b({type:"Identifier",name:e.value},e);if(a("(")){s();let n=E();return v(")"),n}return a("[")?ce():a("{")?Fe():a("<")?fe():L(`Unexpected token '${e.value||e.type}'`)},{parseType:oe,parseTypeParamList:Me,parseTypeInScope:Be}=je({tokens:o,pos:()=>p,peek:f,next:s,isP:a,isK:P,expectP:v,fail:L,recordError:D,createMissingNode:bt,unescape:M,typeRefs:g}),_e=()=>{let e=s(),n=x(),t=[],l=[];a("<")&&(l=Me(),t=l.map(O=>O.name)),v("=");let S=Be(l),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return a(";")&&s(),C},q=()=>{let e=f();if(e.type==="Identifier"&&e.value==="type"&&f(1).type==="Identifier"&&f(2).type==="Punctuator"&&(f(2).value==="="||f(2).value==="<"))return _e();if(P("const")||P("let")||P("var")){let S=Pe();return a(";")&&s(),S}if(P("if"))return Ye();if(P("for"))return Ve();if(P("while"))return qe();if(P("do"))return Ge();if(P("break")||P("continue"))return Qe();if(P("return"))return Ze();if(P("throw"))return et();if(P("try"))return tt();if(P("switch"))return nt();if(P("import"))return We();if(P("export"))return He();if(a("{"))return se();if((P("function")||P("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&L("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return s(),b({type:"EmptyStatement"},e);if(e.type==="Keyword"&&vt.has(e.value)||e.type==="Identifier"&&wt[e.value]?.(f(1),f(2))===true){let S=kt[e.value];L(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),l=a(";")?s():y();return m({type:"ExpressionStatement",expression:t},t,l)},Pe=()=>{let e=s(),n=e.value,t=()=>{let S=k(new Set),C=null;return a("=")?(s(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},l=[t()];for(;a(",");)s(),l.push(t());return m({type:"VariableDeclaration",kind:n,declarations:l},e,l[l.length-1])},ze=()=>{let e=f();return e.type!=="String"&&L("Expected module specifier string"),s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},We=()=>{let e=s(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let l=x();n.push(m({type:"ImportDefaultSpecifier",local:l},l,l)),a(",")&&s();}if(a("*")){let l=s();A("as")||L("Expected 'as' after '*' in import"),s();let S=x();n.push(m({type:"ImportNamespaceSpecifier",local:S},l,S));}else if(a("{")){for(s();!a("}")&&!T();){let l=x(),S=l;if(A("as")&&(s(),S=x()),n.push(m({type:"ImportSpecifier",imported:l,local:S},l,S)),a(","))s();else break}v("}");}A("from")||L("Expected 'from' in import declaration"),s();}let t=ze();return a(";")&&s(),b({type:"ImportDeclaration",specifiers:n,source:t},e)},He=()=>{let e=s();if(P("default")){s();let n=E();return a(";")&&s(),b({type:"ExportDefaultDeclaration",declaration:n},e)}if(P("const")||P("let")||P("var")){let n=Pe();return a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(a("{")){s();let n=[];for(;!a("}")&&!T();){let t=x(),l=t;if(A("as")&&(s(),l=x()),n.push(m({type:"ExportSpecifier",local:t,exported:l},t,l)),a(","))s();else break}return v("}"),a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return L("Expected 'const', '{', or 'default' after 'export'")},Ne=()=>f().type==="Identifier"&&f().value==="of",ye=()=>P("in"),Ve=()=>{let e=s();v("(");let n=null,t=null;if(!a(";"))if(P("const")||P("let")||P("var")){let O=s(),_=()=>{let H=x();if(a(":")){s();let G=oe(new Set);H.typeAnnotation=G.schema,H.typeAnnotationNode=G.node;}return H},K=_();if(Ne()||ye()){let H=m({type:"VariableDeclarator",id:K,init:null},K,K);t=m({type:"VariableDeclaration",kind:O.value,declarations:[H]},O,K);}else {let H=Y=>{let ne=null;return a("=")?(s(),ne=E(true)):O.value==="const"&&L("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Y,init:ne},Y,ne??Y)},G=[H(K)];for(;a(",");)s(),G.push(H(_()));n=m({type:"VariableDeclaration",kind:O.value,declarations:G},O,G[G.length-1]);}}else {let O=E(true);Ne()||ye()?t=O:n=O;}if(t){let O=ye();s();let _=E();v(")");let K=q();return m(O?{type:"ForInStatement",left:t,right:_,body:K}:{type:"ForOfStatement",left:t,right:_,body:K,await:false},e,K)}v(";");let l=a(";")?null:E();v(";");let S=a(")")?null:E();v(")");let C=q();return m({type:"ForStatement",init:n,test:l,update:S,body:C},e,C)},Ge=()=>{let e=s(),n=q();P("while")||L("Expected 'while' after the 'do' body"),s(),v("(");let t=E(),l=v(")");return a(";")&&s(),m({type:"DoWhileStatement",body:n,test:t},e,l)},Qe=()=>{let e=s(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?s():y();return m({type:n,label:null},e,t)},qe=()=>{let e=s();v("(");let n=E();v(")");let t=q();return m({type:"WhileStatement",test:n,body:t},e,t)},Ye=()=>{let e=s();v("(");let n=E();v(")");let t=q(),l=null;return P("else")&&(s(),l=q()),m({type:"IfStatement",test:n,consequent:t,alternate:l},e,l??t)},Ze=()=>{let e=s(),n=null;!a(";")&&!a("}")&&!T()&&(n=E());let t=a(";")?s():y();return m({type:"ReturnStatement",argument:n},e,t)},et=()=>{let e=s(),n=E(),t=a(";")?s():y();return m({type:"ThrowStatement",argument:n},e,t)},tt=()=>{let e=s(),n=se(),t=null,l=null;if(P("catch")){let S=s(),C=null;a("(")&&(s(),C=x(),v(")"));let O=se();t=m({type:"CatchClause",param:C,body:O},S,O);}return P("finally")&&(s(),l=se()),!t&&!l&&L("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:l},e,l??t??n)},nt=()=>{let e=s();v("(");let n=E();v(")"),v("{");let t=[];for(;!a("}")&&!T();){let S=f(),C=null;P("case")?(s(),C=E()):P("default")?s():L("Expected 'case' or 'default' in switch"),v(":");let O=[];for(;!a("}")&&!P("case")&&!P("default")&&!T();)O.push(q());t.push(m({type:"SwitchCase",test:C,consequent:O},S,y()??S));}let l=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,l)},se=()=>{let e=v("{"),n=Ee(()=>a("}"));return m({type:"BlockStatement",body:n},e,v("}"))},Ee=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){s();continue}try{n.push(q());}catch(t){if(!(t instanceof te))throw t;T()||s();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=Ee(()=>false);return m({type:"Program",sourceType:"module",body:n,...g.length?{typeRefs:g}:{},...i.length?{comments:i}:{},...c.length?{parseDiagnostics:c}:{}},e,y()??e)},eof:T,fail:L,recordError:D,diagnostics:c}}function De(r){let c=xe(r),i=c.parseProgram(),o=i.comments;return o?.length&&Te(i,o,r),{ast:i,diagnostics:c.diagnostics}}function Nt(r){let{ast:c,diagnostics:i}=De(r),o=i[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return c}function Et(r){let c=xe(r),i=c.parseExpr();c.eof()||c.fail("Unexpected token after expression");let o=c.diagnostics[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return i}function Se(r){let c=xe(r),i;try{i=c.parseExpr(),c.eof()||c.recordError("Unexpected token after expression");}catch(o){if(!(o instanceof te)&&!(o instanceof ee))throw o}return {ast:i,diagnostics:c.diagnostics}}var Je="{{";function Ct(r){let c=[0];for(let i=0;i<r.length;i++)r[i]===`
7
- `&&c.push(i+1);return c}function de(r,c){let i=0,o=r.length-1;for(;i<o;){let p=i+o+1>>1;r[p]<=c?i=p:o=p-1;}return {line:i+1,column:c-r[i]}}var le=(r,c,i)=>({start:de(r,c),end:de(r,i)});function ve(r,c,i){if(Array.isArray(r)){for(let p of r)ve(p,c,i);return}if(!r||typeof r!="object")return;let o=r;typeof o.start=="number"&&typeof o.end=="number"&&(o.start+=c,o.end+=c,o.loc=le(i,o.start,o.end));for(let p in o)p!=="loc"&&ve(o[p],c,i);}function It(r,c,i){let o=r.index+c,p=r.end+c;return {...r,index:o,end:p,position:de(i,o),loc:le(i,o,p)}}var Lt=(r,c,i,o)=>({code:"parse-error",message:r,index:c,end:i,position:de(o,c),loc:le(o,c,i)}),Rt=new Set(["{","(","["]),At=new Set(["}",")","]"]);function Ot(r,c){let i=ae(r.slice(c),void 0,()=>{}),o=0;for(let p=0;p<i.length;p++){let g=i[p];if(g.type==="Punctuator"){if(Rt.has(g.value))o++;else if(At.has(g.value)){if(o>0)o--;else if(g.value==="}"){let f=i[p+1];if(f?.type==="Punctuator"&&f.value==="}"&&f.start===g.end)return c+g.start}}}}return -1}function jt(r){let c=Ct(r),i=[],o=[],p=[],g=0,f=0;for(;f<r.length;)if(r[f]==="{"&&r[f+1]==="{"){let a=f+Je.length,P=Ot(r,a);if(P===-1){p.push(Lt("Unterminated '{{' \u2014 expected '}}'.",f,r.length,c));break}i.push({text:r.slice(g,f),start:g}),o.push({inner:r.slice(a,P),start:a}),g=P+Je.length,f=g;}else f++;i.push({text:r.slice(g),start:g});let s=o.map(a=>{let{ast:P,diagnostics:A}=Se(a.inner);ve(P,a.start,c);for(let L of A)p.push(It(L,a.start,c));return P});if(p.length)return {kind:"error",diagnostics:p};if(o.length===0)return {kind:"literal"};let y=s;if(o.length===1&&i.every(a=>a.text===""))return {kind:"expression",node:y[0]};let F=(a,P)=>({type:"TemplateElement",value:{raw:a.text,cooked:a.text},tail:P,start:a.start,end:a.start+a.text.length,loc:le(c,a.start,a.start+a.text.length)});return {kind:"template",node:{type:"TemplateLiteral",quasis:i.map((a,P)=>F(a,P===i.length-1)),expressions:y,start:0,end:r.length,loc:le(c,0,r.length)}}}export{Le as KEYWORDS,ee as LexError,te as ParseError,Te as attachComments,Tt as nodeIsMissing,Nt as parse,Et as parseExpression,Se as parseExpressionTolerant,jt as parseInterpolated,De as parseTolerant,ae as tokenize};
4
+ `||N==="\r"||N==="\u2028"||N==="\u2029",P=()=>{for(;;){let N=y();if(N===" "||N===" "||N==="\f"||N==="\v"||a(N))T();else if(N==="/"&&y(1)==="/"){let L=p,U=s();T(),T();let I=p;for(;p<r.length&&!a(y());)T();c?.({type:"Line",value:r.slice(I,p),start:L,end:p,loc:{start:U,end:s()}});}else if(N==="/"&&y(1)==="*"){let L=p,U=s();T(),T();let I=p,u=p;for(;;){if(p>=r.length){F("Unterminated block comment"),u=p;break}if(y()==="*"&&y(1)==="/"){u=p,T(),T();break}T();}c?.({type:"Block",value:r.slice(I,u),start:L,end:p,loc:{start:U,end:s()}});}else break}},A=(N,L,U,I)=>{o.push({type:N,value:L,start:U,end:p,loc:{start:I,end:s()}}),E=N==="Punctuator"?pt.has(L):N==="Keyword"?ut.has(L):N==="TemplateHead"||N==="TemplateMiddle";},R=N=>{for(T();p<r.length;){let L=y();if(L===N)return T(),r.slice(b,p);if(a(L))return F("Unterminated string literal"),r.slice(b,p);if(L==="\\"){if(T(),p>=r.length)return F("Unterminated string literal"),r.slice(b,p);T();continue}T();}return F("Unterminated string literal"),r.slice(b,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!Ie(y()))return F("Invalid hexadecimal literal");for(;Ie(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return F("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return F("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return F("Invalid exponent");for(;Z(y());)T();}},v=()=>{let N=p;for(;p<r.length;){let L=y();if(L==="`"){let U=r.slice(N,p);return T(),{raw:U,interp:false}}if(L==="$"&&y(1)==="{"){let U=r.slice(N,p);return T(),T(),{raw:U,interp:true}}if(L==="\\"){if(T(),p>=r.length)break;T();continue}T();}return F("Unterminated template literal"),{raw:r.slice(N,p),interp:false}},b=0,m=0,M=[],E=true,X="js",V=[],z=[];for(;;){if(X==="jsxChildren"){b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(u==="<"){y(1)==="/"?(T(),T(),A("Punctuator","</",b,I),X="jsxCloseTag"):(T(),A("Punctuator","<",b,I),V.push("jsxChildren"),X="jsxOpenTag");continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:"jsxChildren"}),X="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();A("JSXText",r.slice(b,p),b,I);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){P(),b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(me(u)){for(T();ct(y());)T();A("Identifier",r.slice(b,p),b,I);continue}if(u==="/"&&y(1)===">"){T(),T(),A("Punctuator","/>",b,I),X=V.pop()??"js";continue}if(u===">"){T(),A("Punctuator",">",b,I),X=X==="jsxCloseTag"?V.pop()??"js":"jsxChildren";continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:X}),X="js";continue}if(u==='"'||u==="'"){A("String",R(u),b,I);continue}if(u==="="||u==="."||u===":"){T(),A("Punctuator",u,b,I);continue}F(`Unexpected character '${u}' in JSX tag`),T(),A("Unknown",u,b,I);continue}P(),b=p;let N=s();if(p>=r.length){A("EOF","",b,N);break}let L=y();if(me(L)){for(;Re(y());)T();let I=r.slice(b,p);A(Le.has(I)?"Keyword":"Identifier",I,b,N);continue}if(Z(L)||L==="."&&Z(y(1))){D(),A("Number",r.slice(b,p),b,N);continue}if(L==='"'||L==="'"){let I=R(L);A("String",I,b,N);continue}if(L==="`"){T();let{raw:I,interp:u}=v();u?(A("TemplateHead",I,b,N),M.push(m)):A("Template",I,b,N);continue}if(L==="<"&&E){T(),A("Punctuator","<",b,N),V.push("js"),X="jsxOpenTag";continue}if(L==="{"){T(),A("Punctuator","{",b,N),m++;continue}if(L==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:u}=v();A(u?"TemplateMiddle":"TemplateTail",I,b,N),u&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),A("Punctuator","}",b,N),X=I;continue}T(),A("Punctuator","}",b,N),m--;continue}if(L==="?"&&y(1)==="."&&Z(y(2))){T(),A("Punctuator","?",b,N);continue}let U=lt.find(I=>r.startsWith(I,p));if(U){for(let I=0;I<U.length;I++)T();A("Punctuator",U,b,N);continue}F(`Unexpected character '${L}'`),T(),A("Unknown",L,b,N);}return o}var dt=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ae=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Oe=new WeakMap;function ft(r){let c=Oe.get(r);if(c)return c;let l=[];for(let o of Object.keys(r)){if(dt.has(o))continue;let p=r[o];if(Ae(p))l.push(p);else if(Array.isArray(p))for(let g of p)Ae(g)&&l.push(g);}return l.sort((o,p)=>o.start-p.start),Oe.set(r,l),l}function yt(r,c){let l=r,o,p;for(;;){let g=ft(l);o=void 0,p=void 0;let f;for(let s of g){if(s.start<=c.start&&c.end<=s.end){f=s;break}if(s.end<=c.start)o=s;else if(c.end<=s.start){p=s;break}}if(!f)break;l=f;}return {enclosing:l,preceding:o,following:p}}var he=(r,c)=>{(r.leadingComments??(r.leadingComments=[])).push(c);},ue=(r,c)=>{(r.trailingComments??(r.trailingComments=[])).push(c);},ge=(r,c)=>{(r.innerComments??(r.innerComments=[])).push(c);};function mt(r,c){for(let l=c;l<r.length;l++){let o=r[l];if(o!==" "&&o!==" "&&o!=="\r"&&o!==`
5
+ `&&o!=="\f")return o}return ""}var ht=new Set([")","]","}",","]);function Te(r,c,l=""){for(let o of c){let{enclosing:p,preceding:g,following:f}=yt(r,o),s=g==null||g.loc.end.line<o.loc.start.line,y=!s&&(f==null||o.loc.end.line<f.loc.start.line);if(s)f?he(f,o):g?ue(g,o):ge(p,o);else if(y)g?ue(g,o):f?he(f,o):ge(p,o);else {let F=ht.has(mt(l,o.end));g&&F?ue(g,o):f?he(f,o):g?ue(g,o):ge(p,o);}}}var be=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ie=(r,c)=>{let l=new Set(r.vars),o=new Map(r.bounds);for(let p of c)l.add(p.name),p.bound||p.isConst?o.set(p.name,{bound:p.bound,isConst:p.isConst}):o.delete(p.name);return {vars:l,bounds:o,noCond:r.noCond,inferSink:r.inferSink}},gt=(r,c)=>{let l=r.bounds.get(c);return l&&(l.bound||l.isConst)?{t:"var",name:c,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:c}},je=r=>{let{peek:c,next:l,isP:o,isK:p,expectP:g,fail:f,unescape:s}=r,y=(u,h)=>{let d=r.tokens[r.pos()-1]??h;return Object.assign(u,{start:h.start,end:d.end,loc:{start:h.loc.start,end:d.loc.end}})},F=()=>{let u=r.pos(),h=r.tokens[u],d=r.tokens[u-1];return !!d&&!!h&&h.loc.start.line>d.loc.end.line},T=()=>{let u=0;for(let h=r.pos();h<r.tokens.length;h++){let d=r.tokens[h];if(d.type==="EOF")return false;if(d.type==="Punctuator"){if(["(","[","{","<"].includes(d.value))u++;else if([")","]","}",">"].includes(d.value)&&(u--,u===0)){let x=r.tokens[h+1];return !!x&&x.type==="Punctuator"&&x.value==="=>"}}}return false},a=()=>{let u=r.tokens,h=[],d=0,x=false;for(let k=r.pos();k<u.length;k++){let w=u[k];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")d++,d===1&&(x=true);else if(w.value===">"){if(d--,d===0)break}else w.value==="("||w.value==="["||w.value==="{"?(d++,x=false):w.value===")"||w.value==="]"||w.value==="}"?(d--,x=false):w.value===","&&d===1?x=true:x=false;continue}if(d===1&&x){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),x=false;continue}}x=false;}return h},P=u=>{let h=ie(u,a().map(x=>({name:x})));g("<");let d=[];for(;!o(">");){let x=p("const");x&&l();let k=c();k.type!=="Identifier"&&f("Expected type parameter name"),l();let w;p("extends")&&(l(),w=D(h).schema),o("=")&&(l(),D(h)),d.push({name:k.value,...w?{bound:w}:{},...x?{isConst:true}:{}}),o(">")||g(",");}return g(">"),d},A=u=>{g("<");let h=[];for(;!o(">");)h.push(D(u)),o(">")||g(",");return g(">"),h},R=u=>{let h=c();return h.type==="Identifier"&&h.value===u},D=u=>{let h=c(),d=v(u);if(u.noCond||!p("extends")||F())return d;l();let x=[],k=v({...u,noCond:true,inferSink:x});g("?");let w=ie(u,x.map(J=>({name:J}))),j=D(w);g(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:d.node,extendsType:k.node,trueType:j.node,falseType:$.node},h),schema:{t:"conditional",check:d.schema,extends:k.schema,then:j.schema,else:$.schema,...x.length?{infer:x}:{}}}},v=u=>{let h=c();o("|")&&l();let d=b(u);if(!o("|"))return d;let x=[d.node],k=[d.schema];for(;o("|");){l();let w=b(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSUnionType",types:x},h),schema:{t:"union",of:k}}},b=u=>{let h=c();o("&")&&l();let d=m(u);if(!o("&"))return d;let x=[d.node],k=[d.schema];for(;o("&");){l();let w=m(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSIntersectionType",types:x},h),schema:{t:"intersection",of:k}}},m=u=>{let h=c();if(R("keyof")){l();let d=m(u);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:d.node},h),schema:{t:"keyof",of:d.schema}}}if(R("readonly")){l();let d=m(u);return d.schema.t!=="array"&&d.schema.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{node:y({...d.node,readonly:true},h),schema:{...d.schema,readonly:true}}}if(R("infer")){l();let d=c();d.type!=="Identifier"&&f("Expected 'infer' variable name"),l();let x=u.inferSink;return x?(x.push(d.value),{node:y({type:"TSInferType",name:d.value},h),schema:{t:"var",name:d.value}}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return M(u)},M=u=>{let h=c(),d=X(u);for(;o("[")&&!F();)if(l(),o("]"))l(),d={node:y({type:"TSArrayType",elementType:d.node},h),schema:{t:"array",items:d.schema}};else {let x=D(u);g("]"),d={node:y({type:"TSIndexedAccessType",objectType:d.node,indexType:x.node},h),schema:{t:"index",obj:d.schema,key:x.schema}};}return d},E=(u,h,d)=>({node:y({type:"TSKeywordType",kind:h},u),schema:d}),X=u=>{let h=c();if(p("typeof")){l();let k=c();k.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),l();let w=[k.value];for(;o(".");){l();let j=c();j.type!=="Identifier"&&j.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),l(),w.push(j.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(o("<")){let k=P(u),w=ie(u,k);return z(w,h,k.map(j=>j.name))}if(o("(")){if(T())return z(u,h);l();let k=D(u.noCond?{...u,noCond:false}:u);return g(")"),k}if(o("{"))return L()?U(u):I(u);if(o("["))return N(u);let d=c();if(d.type==="String"){l();let k=s(d.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(d.type==="Number"){l();let k=Number(d.value);return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(p("true"))return l(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return l(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return l(),E(h,"null",{t:"null"});if(p("void"))return l(),E(h,"void",{t:"void"});if(d.type==="Identifier"){switch(l(),d.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let k=o("<")?A(u):[],w=k.map(J=>J.schema),j=k.length?k.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:d.value,...j?{typeArguments:j}:{}},h);return u.vars.has(d.value)&&w.length===0?{node:$,schema:gt(u,d.value)}:d.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:d.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:d.value,loc:d.loc}),{node:$,schema:{t:"apply",name:d.value,args:w}})}r.recordError(`Expected a type but got '${d.value||d.type}'`,d);let x=r.createMissingNode({type:"TSTypeReference",typeName:""},d);return r.typeRefs?.push({name:"",loc:x.loc}),{node:x,schema:{t:"unknown"}}},V=u=>{g("(");let h=[];for(;!o(")");){let d=c();d.type!=="Identifier"&&f("Expected parameter name in function type"),l();let x=o("?");x&&l(),g(":");let k=D(u);h.push({node:y({type:"TSTypeParameter",name:d.value,typeAnnotation:k.node,...x?{optional:true}:{}},d),schema:{name:d.value,type:k.schema,...x?{optional:true}:{}}}),o(")")||g(",");}return g(")"),h},z=(u,h,d)=>{let x=V(u);g("=>");let k=D(u);return {node:y({type:"TSFunctionType",params:x.map(w=>w.node),returnType:k.node,...d?.length?{typeParams:[...d]}:{}},h),schema:{t:"fn",params:x.map(w=>w.schema),returns:k.schema,...d?.length?{typeParams:d}:{}}}},N=u=>{let h=c();g("[");let d=[],x=[],k=[];for(;!o("]");){if(o("...")){l();let j=D(u);j.schema.t==="array"?(x.push(j.schema.items),d.push(j.node.elementType),k.push("rest")):(x.push(j.schema),d.push(j.node),k.push("variadic"));}else {let j=D(u),$=o("?");$&&l(),x.push(j.schema),d.push(j.node),k.push($?"optional":"req");}o("]")||g(",");}g("]");let w=k.some(j=>j!=="req");return {node:y({type:"TSTupleType",elementTypes:d,...w?{flags:k}:{}},h),schema:w?{t:"tuple",elements:x,flags:k}:{t:"tuple",elements:x}}},L=()=>{let u=r.tokens,h=r.pos()+1;return u[h]?.type==="Punctuator"&&(u[h]?.value==="+"||u[h]?.value==="-")&&h++,u[h]?.type==="Identifier"&&u[h]?.value==="readonly"&&h++,u[h]?.type==="Punctuator"&&u[h]?.value==="["&&u[h+1]?.type==="Identifier"&&u[h+2]?.type==="Keyword"&&u[h+2]?.value==="in"},U=u=>{let h=c();g("{"),(o("+")||o("-"))&&l(),R("readonly")&&l(),g("[");let d=c();d.type!=="Identifier"&&f("Expected mapped type key name"),l(),p("in")||f("Expected 'in' in mapped type"),l();let x=D(u);R("as")&&f("Key remapping ('as') in mapped types is not supported"),g("]");let k=false,w=o("+")||o("-")?l().value:null;o("?")&&(l(),k=w!=="-"),g(":");let j=D(ie(u,[{name:d.value}]));return (o(";")||o(","))&&l(),g("}"),{node:y({type:"TSMappedType",key:d.value,constraint:x.node,typeAnnotation:j.node,optional:k},h),schema:{t:"mapped",key:d.value,over:x.schema,body:j.schema,optional:k}}},I=u=>{let h=c();g("{");let d=[],x={},k,w=[];for(;!o("}");){let $=c();if(o("(")){let J=V(u);g(":");let B=D(u);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),d.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(o("[")){l(),c().type!=="Identifier"&&f("Expected index signature parameter name"),l(),g(":");let J=c();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),l(),g("]"),g(":");let B=D(u);k={key:J.value,value:B.schema},d.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=c(),B;if(J.type==="Identifier"||J.type==="Keyword")l(),B=J.value;else if(J.type==="String")l(),B=s(J.value.slice(1,-1));else if(J.type==="Number")l(),B=J.value;else return f("Expected property name in object type");let W=o("?");W&&l(),g(":");let ce=D(u);x[B]={type:ce.schema,...W?{optional:true}:{}},d.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ce.node,...W?{optional:true}:{}},$));}if(o(";")||o(","))l();else break}g("}");let j=y({type:"TSTypeLiteral",members:d},h);return w.length>0?Object.keys(x).length>0||k?f("Callable object types (call signatures mixed with members) are not supported"):{node:j,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:j,schema:{t:"object",props:x,...k?{index:k}:{}}}};return {parseType:u=>D(be(u)),parseTypeParamList:()=>P(be()),parseTypeInScope:u=>D(ie(be(),u))}};var te=class extends TSLiteError{constructor(l,o,p,g,f){super("parse-error",`${l} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="ParseError";}},bt=r=>{let c=r;return c!=null&&typeof c.start=="number"&&c.start===c.end&&c.start>=0},De=(r,c)=>Object.assign(r,{start:c.start,end:c.start,loc:{start:c.loc.start,end:c.loc.start}}),xt={"??":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},St=new Set(["??","||","&&"]),vt=new Set(["class","debugger","with","enum","interface"]),kt={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"},wt={namespace:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",module:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},Pt=new Set(["=","+=","-=","*=","/=","%="]);function xe(r){let c=[],l=[],o=ae(r,e=>l.push(e),e=>c.push({code:"lex-error",...e})),p=0,g=[],f=(e=0)=>o[Math.min(p+e,o.length-1)],s=()=>o[p++],y=()=>o[p-1],F=()=>{let e=o[p],n=o[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",a=e=>f().type==="Punctuator"&&f().value===e,P=e=>f().type==="Keyword"&&f().value===e,A=e=>f().type==="Identifier"&&f().value===e,R=(e,n=f())=>{throw D(e,n),new te(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=c[c.length-1];t&&t.index===n.start||c.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(a(e)||R(`Expected '${e}' but got '${f().value||f().type}'`),s()),b=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let i=e[++t];switch(i){case "n":n+=`
6
+ `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=i;}}return n},E=(e=false)=>X(e),X=(e=false)=>{let n=V(e),t=f();if(t.type==="Punctuator"&&Pt.has(t.value)){s();let i=X();return m({type:"AssignmentExpression",operator:t.value,left:n,right:i},n,i)}return n},V=(e=false)=>{let n=N(0,e);if(!a("?"))return n;s();let t=E();v(":");let i=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:i},n,i)},z=()=>{let e=f();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 n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},N=(e,n=false)=>{let t=L();for(;;){let i=z();if(!i)break;let{op:S,consume:C}=i;if(S==="in"&&n)break;let O=xt[S];if(!O||O<=e)break;if(S==="satisfies"){if(F())break;R("'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(S==="as"){if(F())break;s();let K=oe(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:K.schema,typeAnnotationNode:K.node},t,y());continue}for(let K=0;K<C;K++)s();let _=N(S==="**"?O-1:O,n);t=m({type:St.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},L=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(s(),b({type:"UpdateExpression",operator:e.value,prefix:true,argument:L()},e)):P("await")?(s(),b({type:"AwaitExpression",argument:L()},e)):P("typeof")||P("void")||P("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?U(e):u()},U=e=>{s();let n=L();return a("**")&&R(`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.`),b({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=c.length,t=()=>(p=e,c.length=n,void 0);try{if(s(),a(">"))return t();let i=[];for(;i.push(oe(new Set).schema),!!a(",");)s();return a(">")?(s(),a("(")?i:t()):t()}catch{return t()}},u=()=>{let e=Me(),n=false;for(;;)if(a("?."))if(s(),n=true,a("(")){let{args:t,close:i}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,i);}else if(a("[")){s();let t=E(),i=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,i);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&R("Expected property name after '?.'"),s();let i=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:i,computed:false,optional:true},e,i);}else if(a(".")){s();let t=f(),i;t.type==="Identifier"||t.type==="Keyword"?(s(),i=b({type:"Identifier",name:t.value},t)):(D("Expected property name"),i=De({type:"Identifier",name:""},t)),e=m({type:"MemberExpression",object:e,property:i,computed:false,optional:false},e,i);}else if(a("[")){s();let t=E(),i=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,i);}else if(a("(")){let{args:t,close:i}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,i);}else if(a("<")){let t=I();if(!t)break;let{args:i,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:i,optional:false,typeArguments:t},e,S);}else if(a("++")||a("--")){let t=s();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!a(")");)e.push(d()),a(")")||v(",");return {args:e,close:v(")")}},d=()=>{if(a("...")){let e=s(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},x=()=>{let e=f();return e.type!=="Identifier"&&R("Expected identifier"),s(),b({type:"Identifier",name:e.value},e)},k=e=>{let n=a("{")?w(e):a("[")?j(e):x();if(a(":")){s();let t=oe(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!a("}");){if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(),C,O=false;if(a(":")?(s(),C=k(e)):(C={...S},O=true),a("=")){s();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:O,computed:false},S,C));}a("}")||v(",");}let i=v("}");return m({type:"ObjectPattern",properties:t},n,i)},j=e=>{let n=v("["),t=[];for(;!a("]");){if(a(",")){t.push(null),s();continue}if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=k(e);if(a("=")){s();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}a("]")||v(",");}let i=v("]");return m({type:"ArrayPattern",elements:t},n,i)},$=()=>{let e=0;for(let n=p;n<o.length;n++){let t=o[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let i=o[n+1];return !i||i.type!=="Punctuator"?false:i.value==="=>"?true:i.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<o.length;t++){let i=o[t];if(i.type==="EOF")return false;if(i.type==="Punctuator")if(["(","[","{","<"].includes(i.value))n++;else if([")","]","}",">"].includes(i.value)){if(n===0)return false;n--;}else {if(n===0&&i.value==="=>")return true;if(n===0&&(i.value===";"||i.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!a(")");){if(a("...")){let t=s(),i=k(e);n.push(m({type:"RestElement",argument:i},t,i));}else {let t=k(e);if(a("=")){s();let i=E();t=m({type:"AssignmentPattern",left:t,right:i},t,i);}n.push(t);}a(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=a("(")?B():[x()],i,S;if(a(":")){s();let O=oe(new Set);i=O.schema,S=O.node;}v("=>");let C=a("{")?se():E();return b({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...i?{returnType:i,returnTypeNode:S}:{}},n)},ce=()=>{let e=v("["),n=[];for(;!a("]");){if(a(",")){s(),n.push(null);continue}n.push(d()),a("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Xe=()=>{let e=v("{"),n=[];for(;!a("}");){if(a("...")){let t=s(),i=E();n.push(m({type:"SpreadElement",argument:i},t,i));}else {let t=f(),i,S=false;if(a("["))S=true,s(),i=E(),v("]");else if(t.type==="String")s(),i=b({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")s(),i=b({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")s(),i=b({type:"Identifier",name:t.value},t);else return R("Invalid property key");if(a(":")){s();let C=E();n.push(m({type:"Property",key:i,value:C,kind:"init",shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:i,value:i,kind:"init",shorthand:true,computed:false},t,i));}a("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},pe=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),Ue=e=>{s();let n=[pe(e.value,false)],t=[];for(;;){t.push(E());let i=f();if(i.type==="TemplateMiddle")s(),n.push(pe(i.value,false));else if(i.type==="TemplateTail"){s(),n.push(pe(i.value,true));break}else R("Expected `}` to close template interpolation");}return b({type:"TemplateLiteral",quasis:n,expressions:t},e)},re=()=>{let e=f();return e.type!=="Identifier"&&R(`Expected JSX identifier but got '${e.value||e.type}'`),b({type:"JSXIdentifier",name:s().value},e)},ke=()=>{let e=re();if(a(":")){s();let n=re();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;a(".");){s();let n=re();e=m({type:"JSXMemberExpression",object:e,property:n},e,n);}return e},Q=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${Q(e.namespace)}:${Q(e.name)}`:`${Q(e.object)}.${Q(e.property)}`,Ke=()=>{let e=f();if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(a("{")){let n=s(),t=X(),i=v("}");return m({type:"JSXExpressionContainer",expression:t},n,i)}return a("<")?fe():R(`Invalid JSX attribute value '${e.value||e.type}'`)},$e=()=>{let e=[];for(;!a(">")&&!a("/>")&&!T();){if(a("{")){let i=s();v("...");let S=X(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},i,C));continue}let n=re();if(a(":")){s();let i=re();n=m({type:"JSXNamespacedName",namespace:n,name:i},n,i);}let t=null;a("=")&&(s(),t=Ke()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},we=()=>{let e=[];for(;!(a("</")||T());){let n=f();if(n.type==="JSXText")s(),e.push(b({type:"JSXText",value:n.value,raw:n.value},n));else if(a("{")){let t=s();if(a("}")){let i=s(),S=b({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,i));}else {let i=X(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:i},t,S));}}else a("<")?e.push(fe()):R(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},fe=()=>{let e=v("<");if(a(">")){let Y=s(),ne=m({type:"JSXOpeningFragment"},e,Y),ot=we(),st=v("</"),Ce=v(">"),at=m({type:"JSXClosingFragment"},st,Ce);return m({type:"JSXFragment",openingFragment:ne,children:ot,closingFragment:at},e,Ce)}let n=ke(),t=$e();if(a("/>")){let Y=s(),ne=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Y);return m({type:"JSXElement",openingElement:ne,children:[],closingElement:null},e,Y)}let i=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,i),C=we(),O=v("</"),_=f(),K=ke();Q(K)!==Q(n)&&R(`JSX closing tag </${Q(K)}> does not match opening <${Q(n)}>`,_);let H=v(">"),G=m({type:"JSXClosingElement",name:K},O,H);return m({type:"JSXElement",openingElement:S,children:C,closingElement:G},e,H)},Me=()=>{let e=f();if(P("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(s(),W(true,e)):R("Unexpected 'async'")}if(a("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return s(),b({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return s(),b({type:"TemplateLiteral",expressions:[],quasis:[pe(e.value,true)]},e);if(e.type==="TemplateHead")return Ue(e);if(e.type==="Keyword")return e.value==="true"?(s(),b({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(s(),b({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(s(),b({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?R("'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"?R("Anonymous functions are not supported \u2014 use an arrow function"):R(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return s(),b({type:"Identifier",name:e.value},e);if(a("(")){s();let n=E();return v(")"),n}return a("[")?ce():a("{")?Xe():a("<")?fe():R(`Unexpected token '${e.value||e.type}'`)},{parseType:oe,parseTypeParamList:Be,parseTypeInScope:_e}=je({tokens:o,pos:()=>p,peek:f,next:s,isP:a,isK:P,expectP:v,fail:R,recordError:D,createMissingNode:De,unescape:M,typeRefs:g}),ze=()=>{let e=s(),n=x(),t=[],i=[];a("<")&&(i=Be(),t=i.map(O=>O.name)),v("=");let S=_e(i),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return a(";")&&s(),C},q=()=>{let e=f();if(e.type==="Identifier"&&e.value==="type"&&f(1).type==="Identifier"&&f(2).type==="Punctuator"&&(f(2).value==="="||f(2).value==="<"))return ze();if(P("const")||P("let")||P("var")){let S=Pe();return a(";")&&s(),S}if(P("if"))return Ze();if(P("for"))return Ge();if(P("while"))return Ye();if(P("do"))return Qe();if(P("break")||P("continue"))return qe();if(P("return"))return et();if(P("throw"))return tt();if(P("try"))return nt();if(P("switch"))return rt();if(P("import"))return He();if(P("export"))return Ve();if(a("{"))return se();if((P("function")||P("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&R("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return s(),b({type:"EmptyStatement"},e);if(e.type==="Keyword"&&vt.has(e.value)||e.type==="Identifier"&&wt[e.value]?.(f(1),f(2))===true){let S=kt[e.value];R(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),i=a(";")?s():y();return m({type:"ExpressionStatement",expression:t},t,i)},Pe=()=>{let e=s(),n=e.value,t=()=>{let S=k(new Set),C=null;return a("=")?(s(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},i=[t()];for(;a(",");)s(),i.push(t());return m({type:"VariableDeclaration",kind:n,declarations:i},e,i[i.length-1])},We=()=>{let e=f();return e.type!=="String"&&R("Expected module specifier string"),s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},He=()=>{let e=s(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let i=x();n.push(m({type:"ImportDefaultSpecifier",local:i},i,i)),a(",")&&s();}if(a("*")){let i=s();A("as")||R("Expected 'as' after '*' in import"),s();let S=x();n.push(m({type:"ImportNamespaceSpecifier",local:S},i,S));}else if(a("{")){for(s();!a("}")&&!T();){let i=x(),S=i;if(A("as")&&(s(),S=x()),n.push(m({type:"ImportSpecifier",imported:i,local:S},i,S)),a(","))s();else break}v("}");}A("from")||R("Expected 'from' in import declaration"),s();}let t=We();return a(";")&&s(),b({type:"ImportDeclaration",specifiers:n,source:t},e)},Ve=()=>{let e=s();if(P("default")){s();let n=E();return a(";")&&s(),b({type:"ExportDefaultDeclaration",declaration:n},e)}if(P("const")||P("let")||P("var")){let n=Pe();return a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(a("{")){s();let n=[];for(;!a("}")&&!T();){let t=x(),i=t;if(A("as")&&(s(),i=x()),n.push(m({type:"ExportSpecifier",local:t,exported:i},t,i)),a(","))s();else break}return v("}"),a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return R("Expected 'const', '{', or 'default' after 'export'")},Ne=()=>f().type==="Identifier"&&f().value==="of",ye=()=>P("in"),Ge=()=>{let e=s();v("(");let n=null,t=null;if(!a(";"))if(P("const")||P("let")||P("var")){let O=s(),_=()=>{let H=x();if(a(":")){s();let G=oe(new Set);H.typeAnnotation=G.schema,H.typeAnnotationNode=G.node;}return H},K=_();if(Ne()||ye()){let H=m({type:"VariableDeclarator",id:K,init:null},K,K);t=m({type:"VariableDeclaration",kind:O.value,declarations:[H]},O,K);}else {let H=Y=>{let ne=null;return a("=")?(s(),ne=E(true)):O.value==="const"&&R("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Y,init:ne},Y,ne??Y)},G=[H(K)];for(;a(",");)s(),G.push(H(_()));n=m({type:"VariableDeclaration",kind:O.value,declarations:G},O,G[G.length-1]);}}else {let O=E(true);Ne()||ye()?t=O:n=O;}if(t){let O=ye();s();let _=E();v(")");let K=q();return m(O?{type:"ForInStatement",left:t,right:_,body:K}:{type:"ForOfStatement",left:t,right:_,body:K,await:false},e,K)}v(";");let i=a(";")?null:E();v(";");let S=a(")")?null:E();v(")");let C=q();return m({type:"ForStatement",init:n,test:i,update:S,body:C},e,C)},Qe=()=>{let e=s(),n=q();P("while")||R("Expected 'while' after the 'do' body"),s(),v("(");let t=E(),i=v(")");return a(";")&&s(),m({type:"DoWhileStatement",body:n,test:t},e,i)},qe=()=>{let e=s(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?s():y();return m({type:n,label:null},e,t)},Ye=()=>{let e=s();v("(");let n=E();v(")");let t=q();return m({type:"WhileStatement",test:n,body:t},e,t)},Ze=()=>{let e=s();v("(");let n=E();v(")");let t=q(),i=null;return P("else")&&(s(),i=q()),m({type:"IfStatement",test:n,consequent:t,alternate:i},e,i??t)},et=()=>{let e=s(),n=null;!a(";")&&!a("}")&&!T()&&(n=E());let t=a(";")?s():y();return m({type:"ReturnStatement",argument:n},e,t)},tt=()=>{let e=s(),n=E(),t=a(";")?s():y();return m({type:"ThrowStatement",argument:n},e,t)},nt=()=>{let e=s(),n=se(),t=null,i=null;if(P("catch")){let S=s(),C=null;a("(")&&(s(),C=x(),v(")"));let O=se();t=m({type:"CatchClause",param:C,body:O},S,O);}return P("finally")&&(s(),i=se()),!t&&!i&&R("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:i},e,i??t??n)},rt=()=>{let e=s();v("(");let n=E();v(")"),v("{");let t=[];for(;!a("}")&&!T();){let S=f(),C=null;P("case")?(s(),C=E()):P("default")?s():R("Expected 'case' or 'default' in switch"),v(":");let O=[];for(;!a("}")&&!P("case")&&!P("default")&&!T();)O.push(q());t.push(m({type:"SwitchCase",test:C,consequent:O},S,y()??S));}let i=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,i)},se=()=>{let e=v("{"),n=Ee(()=>a("}"));return m({type:"BlockStatement",body:n},e,v("}"))},Ee=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){s();continue}try{n.push(q());}catch(t){if(!(t instanceof te))throw t;T()||s();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=Ee(()=>false);return m({type:"Program",sourceType:"module",body:n,...g.length?{typeRefs:g}:{},...l.length?{comments:l}:{},...c.length?{parseDiagnostics:c}:{}},e,y()??e)},eof:T,fail:R,recordError:D,diagnostics:c}}function Je(r){let c=xe(r),l=c.parseProgram(),o=l.comments;return o?.length&&Te(l,o,r),{ast:l,diagnostics:c.diagnostics}}function Nt(r){let{ast:c,diagnostics:l}=Je(r),o=l[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return c}function Et(r){let c=xe(r),l=c.parseExpr();c.eof()||c.fail("Unexpected token after expression");let o=c.diagnostics[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return l}function Se(r){let c=xe(r),l;try{l=c.parseExpr(),c.eof()||c.recordError("Unexpected token after expression");}catch(o){if(!(o instanceof te)&&!(o instanceof ee))throw o}return {ast:l,diagnostics:c.diagnostics}}var Fe="{{";function Ct(r){let c=[0];for(let l=0;l<r.length;l++)r[l]===`
7
+ `&&c.push(l+1);return c}function de(r,c){let l=0,o=r.length-1;for(;l<o;){let p=l+o+1>>1;r[p]<=c?l=p:o=p-1;}return {line:l+1,column:c-r[l]}}var le=(r,c,l)=>({start:de(r,c),end:de(r,l)});function ve(r,c,l){if(Array.isArray(r)){for(let p of r)ve(p,c,l);return}if(!r||typeof r!="object")return;let o=r;typeof o.start=="number"&&typeof o.end=="number"&&(o.start+=c,o.end+=c,o.loc=le(l,o.start,o.end));for(let p in o)p!=="loc"&&ve(o[p],c,l);}function It(r,c,l){let o=r.index+c,p=r.end+c;return {...r,index:o,end:p,position:de(l,o),loc:le(l,o,p)}}var Lt=(r,c,l,o)=>({code:"parse-error",message:r,index:c,end:l,position:de(o,c),loc:le(o,c,l)}),Rt=new Set(["{","(","["]),At=new Set(["}",")","]"]);function Ot(r,c){let l=ae(r.slice(c),void 0,()=>{}),o=0;for(let p=0;p<l.length;p++){let g=l[p];if(g.type==="Punctuator"){if(Rt.has(g.value))o++;else if(At.has(g.value)){if(o>0)o--;else if(g.value==="}"){let f=l[p+1];if(f?.type==="Punctuator"&&f.value==="}"&&f.start===g.end)return c+g.start}}}}return -1}function jt(r){let c=Ct(r),l=[],o=[],p=[],g=0,f=0;for(;f<r.length;)if(r[f]==="{"&&r[f+1]==="{"){let a=f+Fe.length,P=Ot(r,a);if(P===-1){p.push(Lt("Unterminated '{{' \u2014 expected '}}'.",f,r.length,c));break}l.push({text:r.slice(g,f),start:g}),o.push({inner:r.slice(a,P),start:a}),g=P+Fe.length,f=g;}else f++;l.push({text:r.slice(g),start:g});let s=o.map(a=>{let{ast:P,diagnostics:A}=Se(a.inner);ve(P,a.start,c);for(let R of A)p.push(It(R,a.start,c));return P});if(p.length)return {kind:"error",diagnostics:p};if(o.length===0)return {kind:"literal"};let y=s;if(o.length===1&&l.every(a=>a.text===""))return {kind:"expression",node:y[0]};let F=(a,P)=>({type:"TemplateElement",value:{raw:a.text,cooked:a.text},tail:P,start:a.start,end:a.start+a.text.length,loc:le(c,a.start,a.start+a.text.length)});return {kind:"template",node:{type:"TemplateLiteral",quasis:l.map((a,P)=>F(a,P===l.length-1)),expressions:y,start:0,end:r.length,loc:le(c,0,r.length)}}}export{Le as KEYWORDS,ee as LexError,te as ParseError,Te as attachComments,bt as nodeIsMissing,Nt as parse,Et as parseExpression,Se as parseExpressionTolerant,jt as parseInterpolated,Je as parseTolerant,ae as tokenize};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tslite/parser",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
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",
@@ -40,13 +40,13 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@tslite/core": "0.5.0"
43
+ "@tslite/core": "0.6.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@tslite/capability": "0.2.0",
47
- "@tslite/printer": "0.3.1",
48
- "@tslite/interpret": "0.3.1",
49
- "@tslite/checker": "0.5.1"
47
+ "@tslite/printer": "0.3.2",
48
+ "@tslite/checker": "0.7.0",
49
+ "@tslite/interpret": "0.3.2"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup",