@tslite/parser 1.2.0 → 1.3.0

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