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