@tslite/parser 0.5.1 → 0.6.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,7 @@
1
- 'use strict';var core=require('@tslite/core');var te=class extends core.TSLiteError{constructor(u,a,p,k,f){super("lex-error",`${u} (${a.line}:${a.column})`,{position:a,index:p,loc:k,end:f});this.position=a;this.index=p;this.loc=k;this.end=f;this.name="LexError";}},Pe=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"]),nt=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],de=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Ne=r=>de(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",we=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",ot=r=>Ne(r)||r==="-",rt=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),st=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function fe(r,d,u){let a=[],p=0,k=1,f=0,o=()=>({line:k,column:f}),y=(P=0)=>r[p+P]??"",K=P=>{let A=o(),X={start:A,end:{line:k,column:f+1}};if(u){u({message:P,position:A,index:p,loc:X,end:p+1});return}throw new te(P,A,p,X,p+1)},T=()=>{let P=r[p++];return P==="\r"?(r[p]===`
2
- `&&p++,k++,f=0):P===`
3
- `||P==="\u2028"||P==="\u2029"?(k++,f=0):f++,P},i=P=>P===`
4
- `||P==="\r"||P==="\u2028"||P==="\u2029",N=()=>{for(;;){let P=y();if(P===" "||P===" "||P==="\f"||P==="\v"||i(P))T();else if(P==="/"&&y(1)==="/"){let A=p,X=o();T(),T();let I=p;for(;p<r.length&&!i(y());)T();d?.({type:"Line",value:r.slice(I,p),start:A,end:p,loc:{start:X,end:o()}});}else if(P==="/"&&y(1)==="*"){let A=p,X=o();T(),T();let I=p,l=p;for(;;){if(p>=r.length){K("Unterminated block comment"),l=p;break}if(y()==="*"&&y(1)==="/"){l=p,T(),T();break}T();}d?.({type:"Block",value:r.slice(I,l),start:A,end:p,loc:{start:X,end:o()}});}else break}},L=(P,A,X,I)=>{a.push({type:P,value:A,start:X,end:p,loc:{start:I,end:o()}}),E=P==="Punctuator"?rt.has(A):P==="Keyword"?st.has(A):P==="TemplateHead"||P==="TemplateMiddle";},R=P=>{for(T();p<r.length;){let A=y();if(A===P)return T(),r.slice(g,p);if(i(A))return K("Unterminated string literal"),r.slice(g,p);if(A==="\\"){if(T(),p>=r.length)return K("Unterminated string literal"),r.slice(g,p);T();continue}T();}return K("Unterminated string literal"),r.slice(g,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!we(y()))return K("Invalid hexadecimal literal");for(;we(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return K("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return K("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return K("Invalid exponent");for(;Z(y());)T();}},v=()=>{let P=p;for(;p<r.length;){let A=y();if(A==="`"){let X=r.slice(P,p);return T(),{raw:X,interp:false}}if(A==="$"&&y(1)==="{"){let X=r.slice(P,p);return T(),T(),{raw:X,interp:true}}if(A==="\\"){if(T(),p>=r.length)break;T();continue}T();}return K("Unterminated template literal"),{raw:r.slice(P,p),interp:false}},g=0,m=0,M=[],E=true,F="js",H=[],z=[];for(;;){if(F==="jsxChildren"){g=p;let I=o();if(p>=r.length){L("EOF","",g,I);break}let l=y();if(l==="<"){y(1)==="/"?(T(),T(),L("Punctuator","</",g,I),F="jsxCloseTag"):(T(),L("Punctuator","<",g,I),H.push("jsxChildren"),F="jsxOpenTag");continue}if(l==="{"){T(),L("Punctuator","{",g,I),z.push({level:m,restore:"jsxChildren"}),F="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();L("JSXText",r.slice(g,p),g,I);continue}if(F==="jsxOpenTag"||F==="jsxCloseTag"){N(),g=p;let I=o();if(p>=r.length){L("EOF","",g,I);break}let l=y();if(de(l)){for(T();ot(y());)T();L("Identifier",r.slice(g,p),g,I);continue}if(l==="/"&&y(1)===">"){T(),T(),L("Punctuator","/>",g,I),F=H.pop()??"js";continue}if(l===">"){T(),L("Punctuator",">",g,I),F=F==="jsxCloseTag"?H.pop()??"js":"jsxChildren";continue}if(l==="{"){T(),L("Punctuator","{",g,I),z.push({level:m,restore:F}),F="js";continue}if(l==='"'||l==="'"){L("String",R(l),g,I);continue}if(l==="="||l==="."||l===":"){T(),L("Punctuator",l,g,I);continue}K(`Unexpected character '${l}' in JSX tag`),T(),L("Unknown",l,g,I);continue}N(),g=p;let P=o();if(p>=r.length){L("EOF","",g,P);break}let A=y();if(de(A)){for(;Ne(y());)T();let I=r.slice(g,p);L(Pe.has(I)?"Keyword":"Identifier",I,g,P);continue}if(Z(A)||A==="."&&Z(y(1))){D(),L("Number",r.slice(g,p),g,P);continue}if(A==='"'||A==="'"){let I=R(A);L("String",I,g,P);continue}if(A==="`"){T();let{raw:I,interp:l}=v();l?(L("TemplateHead",I,g,P),M.push(m)):L("Template",I,g,P);continue}if(A==="<"&&E){T(),L("Punctuator","<",g,P),H.push("js"),F="jsxOpenTag";continue}if(A==="{"){T(),L("Punctuator","{",g,P),m++;continue}if(A==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:l}=v();L(l?"TemplateMiddle":"TemplateTail",I,g,P),l&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),L("Punctuator","}",g,P),F=I;continue}T(),L("Punctuator","}",g,P),m--;continue}if(A==="?"&&y(1)==="."&&Z(y(2))){T(),L("Punctuator","?",g,P);continue}let X=nt.find(I=>r.startsWith(I,p));if(X){for(let I=0;I<X.length;I++)T();L("Punctuator",X,g,P);continue}K(`Unexpected character '${A}'`),T(),L("Unknown",A,g,P);}return a}var at=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ee=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Ce=new WeakMap;function it(r){let d=Ce.get(r);if(d)return d;let u=[];for(let a of Object.keys(r)){if(at.has(a))continue;let p=r[a];if(Ee(p))u.push(p);else if(Array.isArray(p))for(let k of p)Ee(k)&&u.push(k);}return u.sort((a,p)=>a.start-p.start),Ce.set(r,u),u}function lt(r,d){let u=r,a,p;for(;;){let k=it(u);a=void 0,p=void 0;let f;for(let o of k){if(o.start<=d.start&&d.end<=o.end){f=o;break}if(o.end<=d.start)a=o;else if(d.end<=o.start){p=o;break}}if(!f)break;u=f;}return {enclosing:u,preceding:a,following:p}}var ye=(r,d)=>{(r.leadingComments??(r.leadingComments=[])).push(d);},ce=(r,d)=>{(r.trailingComments??(r.trailingComments=[])).push(d);},me=(r,d)=>{(r.innerComments??(r.innerComments=[])).push(d);};function ct(r,d){for(let u=d;u<r.length;u++){let a=r[u];if(a!==" "&&a!==" "&&a!=="\r"&&a!==`
5
- `&&a!=="\f")return a}return ""}var pt=new Set([")","]","}",","]);function he(r,d,u=""){for(let a of d){let{enclosing:p,preceding:k,following:f}=lt(r,a),o=k==null||k.loc.end.line<a.loc.start.line,y=!o&&(f==null||a.loc.end.line<f.loc.start.line);if(o)f?ye(f,a):k?ce(k,a):me(p,a);else if(y)k?ce(k,a):f?ye(f,a):me(p,a);else {let K=pt.has(ct(u,a.end));k&&K?ce(k,a):f?ye(f,a):k?ce(k,a):me(p,a);}}}var Te=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ae=(r,d)=>{let u=new Set(r.vars),a=new Map(r.bounds);for(let p of d)u.add(p.name),p.bound||p.isConst?a.set(p.name,{bound:p.bound,isConst:p.isConst}):a.delete(p.name);return {vars:u,bounds:a,noCond:r.noCond,inferSink:r.inferSink}},ut=(r,d)=>{let u=r.bounds.get(d);return u&&(u.bound||u.isConst)?{t:"var",name:d,...u.bound?{bound:u.bound}:{},...u.isConst?{isConst:true}:{}}:{t:"var",name:d}},Ie=r=>{let{peek:d,next:u,isP:a,isK:p,expectP:k,fail:f,unescape:o}=r,y=(l,h)=>{let c=r.tokens[r.pos()-1]??h;return Object.assign(l,{start:h.start,end:c.end,loc:{start:h.loc.start,end:c.loc.end}})},K=()=>{let l=r.pos(),h=r.tokens[l],c=r.tokens[l-1];return !!c&&!!h&&h.loc.start.line>c.loc.end.line},T=()=>{let l=0;for(let h=r.pos();h<r.tokens.length;h++){let c=r.tokens[h];if(c.type==="EOF")return false;if(c.type==="Punctuator"){if(["(","[","{","<"].includes(c.value))l++;else if([")","]","}",">"].includes(c.value)&&(l--,l===0)){let b=r.tokens[h+1];return !!b&&b.type==="Punctuator"&&b.value==="=>"}}}return false},i=()=>{let l=r.tokens,h=[],c=0,b=false;for(let x=r.pos();x<l.length;x++){let w=l[x];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")c++,c===1&&(b=true);else if(w.value===">"){if(c--,c===0)break}else w.value==="("||w.value==="["||w.value==="{"?(c++,b=false):w.value===")"||w.value==="]"||w.value==="}"?(c--,b=false):w.value===","&&c===1?b=true:b=false;continue}if(c===1&&b){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),b=false;continue}}b=false;}return h},N=l=>{let h=ae(l,i().map(b=>({name:b})));k("<");let c=[];for(;!a(">");){let b=p("const");b&&u();let x=d();x.type!=="Identifier"&&f("Expected type parameter name"),u();let w;p("extends")&&(u(),w=D(h).schema),a("=")&&(u(),D(h)),c.push({name:x.value,...w?{bound:w}:{},...b?{isConst:true}:{}}),a(">")||k(",");}return k(">"),c},L=l=>{k("<");let h=[];for(;!a(">");)h.push(D(l)),a(">")||k(",");return k(">"),h},R=l=>{let h=d();return h.type==="Identifier"&&h.value===l},D=l=>{let h=d(),c=v(l);if(l.noCond||!p("extends")||K())return c;u();let b=[],x=v({...l,noCond:true,inferSink:b});k("?");let w=ae(l,b.map(J=>({name:J}))),O=D(w);k(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:c.node,extendsType:x.node,trueType:O.node,falseType:$.node},h),schema:{t:"conditional",check:c.schema,extends:x.schema,then:O.schema,else:$.schema,...b.length?{infer:b}:{}}}},v=l=>{let h=d();a("|")&&u();let c=g(l);if(!a("|"))return c;let b=[c.node],x=[c.schema];for(;a("|");){u();let w=g(l);b.push(w.node),x.push(w.schema);}return {node:y({type:"TSUnionType",types:b},h),schema:{t:"union",of:x}}},g=l=>{let h=d();a("&")&&u();let c=m(l);if(!a("&"))return c;let b=[c.node],x=[c.schema];for(;a("&");){u();let w=m(l);b.push(w.node),x.push(w.schema);}return {node:y({type:"TSIntersectionType",types:b},h),schema:{t:"intersection",of:x}}},m=l=>{let h=d();if(R("keyof")){u();let c=m(l);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:c.node},h),schema:{t:"keyof",of:c.schema}}}if(R("readonly")){u();let c=m(l);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(R("infer")){u();let c=d();c.type!=="Identifier"&&f("Expected 'infer' variable name"),u();let b=l.inferSink;return b?(b.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 M(l)},M=l=>{let h=d(),c=F(l);for(;a("[")&&!K();)if(u(),a("]"))u(),c={node:y({type:"TSArrayType",elementType:c.node},h),schema:{t:"array",items:c.schema}};else {let b=D(l);k("]"),c={node:y({type:"TSIndexedAccessType",objectType:c.node,indexType:b.node},h),schema:{t:"index",obj:c.schema,key:b.schema}};}return c},E=(l,h,c)=>({node:y({type:"TSKeywordType",kind:h},l),schema:c}),F=l=>{let h=d();if(p("typeof")){u();let x=d();x.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),u();let w=[x.value];for(;a(".");){u();let O=d();O.type!=="Identifier"&&O.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),u(),w.push(O.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(a("<")){let x=N(l),w=ae(l,x);return z(w,h,x.map(O=>O.name))}if(a("(")){if(T())return z(l,h);u();let x=D(l.noCond?{...l,noCond:false}:l);return k(")"),x}if(a("{"))return A()?X(l):I(l);if(a("["))return P(l);let c=d();if(c.type==="String"){u();let x=o(c.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:x},h),schema:{t:"literal",value:x}}}if(c.type==="Number"){u();let x=Number(c.value);return {node:y({type:"TSLiteralType",literal:x},h),schema:{t:"literal",value:x}}}if(p("true"))return u(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return u(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return u(),E(h,"null",{t:"null"});if(p("void"))return u(),E(h,"void",{t:"void"});if(c.type==="Identifier"){switch(u(),c.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let x=a("<")?L(l):[],w=x.map(J=>J.schema),O=x.length?x.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:c.value,...O?{typeArguments:O}:{}},h);return l.vars.has(c.value)&&w.length===0?{node:$,schema:ut(l,c.value)}:c.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:c.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:c.value,loc:c.loc}),{node:$,schema:{t:"apply",name:c.value,args:w}})}r.recordError(`Expected a type but got '${c.value||c.type}'`,c);let b=r.createMissingNode({type:"TSTypeReference",typeName:""},c);return r.typeRefs?.push({name:"",loc:b.loc}),{node:b,schema:{t:"unknown"}}},H=l=>{k("(");let h=[];for(;!a(")");){let c=d();c.type!=="Identifier"&&f("Expected parameter name in function type"),u();let b=a("?");b&&u(),k(":");let x=D(l);h.push({node:y({type:"TSTypeParameter",name:c.value,typeAnnotation:x.node,...b?{optional:true}:{}},c),schema:{name:c.value,type:x.schema,...b?{optional:true}:{}}}),a(")")||k(",");}return k(")"),h},z=(l,h,c)=>{let b=H(l);k("=>");let x=D(l);return {node:y({type:"TSFunctionType",params:b.map(w=>w.node),returnType:x.node,...c?.length?{typeParams:[...c]}:{}},h),schema:{t:"fn",params:b.map(w=>w.schema),returns:x.schema,...c?.length?{typeParams:c}:{}}}},P=l=>{let h=d();k("[");let c=[],b=[],x=[];for(;!a("]");){if(a("...")){u();let O=D(l);O.schema.t==="array"?(b.push(O.schema.items),c.push(O.node.elementType),x.push("rest")):(b.push(O.schema),c.push(O.node),x.push("variadic"));}else {let O=D(l),$=a("?");$&&u(),b.push(O.schema),c.push(O.node),x.push($?"optional":"req");}a("]")||k(",");}k("]");let w=x.some(O=>O!=="req");return {node:y({type:"TSTupleType",elementTypes:c,...w?{flags:x}:{}},h),schema:w?{t:"tuple",elements:b,flags:x}:{t:"tuple",elements:b}}},A=()=>{let l=r.tokens,h=r.pos()+1;return l[h]?.type==="Punctuator"&&(l[h]?.value==="+"||l[h]?.value==="-")&&h++,l[h]?.type==="Identifier"&&l[h]?.value==="readonly"&&h++,l[h]?.type==="Punctuator"&&l[h]?.value==="["&&l[h+1]?.type==="Identifier"&&l[h+2]?.type==="Keyword"&&l[h+2]?.value==="in"},X=l=>{let h=d();k("{"),(a("+")||a("-"))&&u(),R("readonly")&&u(),k("[");let c=d();c.type!=="Identifier"&&f("Expected mapped type key name"),u(),p("in")||f("Expected 'in' in mapped type"),u();let b=D(l);R("as")&&f("Key remapping ('as') in mapped types is not supported"),k("]");let x=false,w=a("+")||a("-")?u().value:null;a("?")&&(u(),x=w!=="-"),k(":");let O=D(ae(l,[{name:c.value}]));return (a(";")||a(","))&&u(),k("}"),{node:y({type:"TSMappedType",key:c.value,constraint:b.node,typeAnnotation:O.node,optional:x},h),schema:{t:"mapped",key:c.value,over:b.schema,body:O.schema,optional:x}}},I=l=>{let h=d();k("{");let c=[],b={},x,w=[];for(;!a("}");){let $=d();if(a("(")){let J=H(l);k(":");let B=D(l);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),c.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(a("[")){u(),d().type!=="Identifier"&&f("Expected index signature parameter name"),u(),k(":");let J=d();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),u(),k("]"),k(":");let B=D(l);x={key:J.value,value:B.schema},c.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=d(),B;if(J.type==="Identifier"||J.type==="Keyword")u(),B=J.value;else if(J.type==="String")u(),B=o(J.value.slice(1,-1));else if(J.type==="Number")u(),B=J.value;else return f("Expected property name in object type");let W=a("?");W&&u(),k(":");let ie=D(l);b[B]={type:ie.schema,...W?{optional:true}:{}},c.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ie.node,...W?{optional:true}:{}},$));}if(a(";")||a(","))u();else break}k("}");let O=y({type:"TSTypeLiteral",members:c},h);return w.length>0?Object.keys(b).length>0||x?f("Callable object types (call signatures mixed with members) are not supported"):{node:O,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:O,schema:{t:"object",props:b,...x?{index:x}:{}}}};return {parseType:l=>D(Te(l)),parseTypeParamList:()=>N(Te()),parseTypeInScope:l=>D(ae(Te(),l))}};var ne=class extends core.TSLiteError{constructor(u,a,p,k,f){super("parse-error",`${u} (${a.line}:${a.column})`,{position:a,index:p,loc:k,end:f});this.position=a;this.index=p;this.loc=k;this.end=f;this.name="ParseError";}},ft=r=>{let d=r;return d!=null&&typeof d.start=="number"&&d.start===d.end&&d.start>=0},yt=(r,d)=>Object.assign(r,{start:d.start,end:d.start,loc:{start:d.loc.start,end:d.loc.start}}),mt={"??":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},ht=new Set(["??","||","&&"]),Tt=new Set(["class","debugger","with","enum","interface"]),gt={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"},bt={namespace:(r,d)=>r.type==="Identifier"&&d.type==="Punctuator"&&d.value==="{",module:(r,d)=>r.type==="Identifier"&&d.type==="Punctuator"&&d.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},St=new Set(["=","+=","-=","*=","/=","%="]);function Ae(r){let d=[],u=[],a=fe(r,e=>u.push(e),e=>d.push({code:"lex-error",...e})),p=0,k=[],f=(e=0)=>a[Math.min(p+e,a.length-1)],o=()=>a[p++],y=()=>a[p-1],K=()=>{let e=a[p],n=a[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",i=e=>f().type==="Punctuator"&&f().value===e,N=e=>f().type==="Keyword"&&f().value===e,L=e=>f().type==="Identifier"&&f().value===e,R=(e,n=f())=>{throw D(e,n),new ne(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=d[d.length-1];t&&t.index===n.start||d.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(i(e)||R(`Expected '${e}' but got '${f().value||f().type}'`),o()),g=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let s=e[++t];switch(s){case "n":n+=`
6
- `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=s;}}return n},E=(e=false)=>F(e),F=(e=false)=>{let n=H(e),t=f();if(t.type==="Punctuator"&&St.has(t.value)){o();let s=F();return m({type:"AssignmentExpression",operator:t.value,left:n,right:s},n,s)}return n},H=(e=false)=>{let n=P(0,e);if(!i("?"))return n;o();let t=E();v(":");let s=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:s},n,s)},z=()=>{let e=f();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:e.value==="satisfies"?{op:"satisfies",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},P=(e,n=false)=>{let t=A();for(;;){let s=z();if(!s)break;let{op:S,consume:C}=s;if(S==="in"&&n)break;let j=mt[S];if(!j||j<=e)break;if(S==="satisfies"){if(K())break;R("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(S==="as"){if(K())break;o();let U=re(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:U.schema,typeAnnotationNode:U.node},t,y());continue}for(let U=0;U<C;U++)o();let _=P(S==="**"?j-1:j,n);t=m({type:ht.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},A=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(o(),g({type:"UpdateExpression",operator:e.value,prefix:true,argument:A()},e)):N("await")?(o(),g({type:"AwaitExpression",argument:A()},e)):N("typeof")||N("void")||N("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?X(e):l()},X=e=>{o();let n=A();return i("**")&&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.`),g({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=d.length,t=()=>(p=e,d.length=n,void 0);try{if(o(),i(">"))return t();let s=[];for(;s.push(re(new Set).schema),!!i(",");)o();return i(">")?(o(),i("(")?s:t()):t()}catch{return t()}},l=()=>{let e=Je(),n=false;for(;;)if(i("?."))if(o(),n=true,i("(")){let{args:t,close:s}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,s);}else if(i("[")){o();let t=E(),s=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,s);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&R("Expected property name after '?.'"),o();let s=g({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:s,computed:false,optional:true},e,s);}else if(i(".")){o();let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&R("Expected property name"),o();let s=g({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:s,computed:false,optional:false},e,s);}else if(i("[")){o();let t=E(),s=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,s);}else if(i("(")){let{args:t,close:s}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,s);}else if(i("<")){let t=I();if(!t)break;let{args:s,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:s,optional:false,typeArguments:t},e,S);}else if(i("++")||i("--")){let t=o();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!i(")");)e.push(c()),i(")")||v(",");return {args:e,close:v(")")}},c=()=>{if(i("...")){let e=o(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},b=()=>{let e=f();return e.type!=="Identifier"&&R("Expected identifier"),o(),g({type:"Identifier",name:e.value},e)},x=e=>{let n=i("{")?w(e):i("[")?O(e):b();if(i(":")){o();let t=re(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!i("}");){if(i("...")){let S=o(),C=x(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=b(),C,j=false;if(i(":")?(o(),C=x(e)):(C={...S},j=true),i("=")){o();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:j,computed:false,method:false},S,C));}i("}")||v(",");}let s=v("}");return m({type:"ObjectPattern",properties:t},n,s)},O=e=>{let n=v("["),t=[];for(;!i("]");){if(i(",")){t.push(null),o();continue}if(i("...")){let S=o(),C=x(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(e);if(i("=")){o();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}i("]")||v(",");}let s=v("]");return m({type:"ArrayPattern",elements:t},n,s)},$=()=>{let e=0;for(let n=p;n<a.length;n++){let t=a[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let s=a[n+1];return !s||s.type!=="Punctuator"?false:s.value==="=>"?true:s.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<a.length;t++){let s=a[t];if(s.type==="EOF")return false;if(s.type==="Punctuator")if(["(","[","{","<"].includes(s.value))n++;else if([")","]","}",">"].includes(s.value)){if(n===0)return false;n--;}else {if(n===0&&s.value==="=>")return true;if(n===0&&(s.value===";"||s.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!i(")");){if(i("...")){let t=o(),s=x(e);n.push(m({type:"RestElement",argument:s},t,s));}else {let t=x(e);if(i("=")){o();let s=E();t=m({type:"AssignmentPattern",left:t,right:s},t,s);}n.push(t);}i(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=i("(")?B():[b()],s,S;if(i(":")){o();let j=re(new Set);s=j.schema,S=j.node;}v("=>");let C=i("{")?se():E();return g({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...s?{returnType:s,returnTypeNode:S}:{}},n)},ie=()=>{let e=v("["),n=[];for(;!i("]");){if(i(",")){o(),n.push(null);continue}n.push(c()),i("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Le=()=>{let e=v("{"),n=[];for(;!i("}");){if(i("...")){let t=o(),s=E();n.push(m({type:"SpreadElement",argument:s},t,s));}else {let t=f(),s,S=false;if(i("["))S=true,o(),s=E(),v("]");else if(t.type==="String")o(),s=g({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")o(),s=g({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")o(),s=g({type:"Identifier",name:t.value},t);else return R("Invalid property key");if(i(":")){o();let C=E();n.push(m({type:"Property",key:s,value:C,kind:"init",method:false,shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:s,value:s,kind:"init",method:false,shorthand:true,computed:false},t,s));}i("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},le=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),je=e=>{o();let n=[le(e.value,false)],t=[];for(;;){t.push(E());let s=f();if(s.type==="TemplateMiddle")o(),n.push(le(s.value,false));else if(s.type==="TemplateTail"){o(),n.push(le(s.value,true));break}else R("Expected `}` to close template interpolation");}return g({type:"TemplateLiteral",quasis:n,expressions:t},e)},oe=()=>{let e=f();return e.type!=="Identifier"&&R(`Expected JSX identifier but got '${e.value||e.type}'`),g({type:"JSXIdentifier",name:o().value},e)},ge=()=>{let e=oe();if(i(":")){o();let n=oe();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;i(".");){o();let n=oe();e=m({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)}`,Oe=()=>{let e=f();if(e.type==="String")return o(),g({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(i("{")){let n=o(),t=F(),s=v("}");return m({type:"JSXExpressionContainer",expression:t},n,s)}return i("<")?pe():R(`Invalid JSX attribute value '${e.value||e.type}'`)},De=()=>{let e=[];for(;!i(">")&&!i("/>")&&!T();){if(i("{")){let s=o();v("...");let S=F(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},s,C));continue}let n=oe();if(i(":")){o();let s=oe();n=m({type:"JSXNamespacedName",namespace:n,name:s},n,s);}let t=null;i("=")&&(o(),t=Oe()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},be=()=>{let e=[];for(;!(i("</")||T());){let n=f();if(n.type==="JSXText")o(),e.push(g({type:"JSXText",value:n.value,raw:n.value},n));else if(i("{")){let t=o();if(i("}")){let s=o(),S=g({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,s));}else {let s=F(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:s},t,S));}}else i("<")?e.push(pe()):R(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},pe=()=>{let e=v("<");if(i(">")){let Q=o(),ee=m({type:"JSXOpeningFragment"},e,Q),Qe=be(),Ze=v("</"),ke=v(">"),et=m({type:"JSXClosingFragment"},Ze,ke);return m({type:"JSXFragment",openingFragment:ee,children:Qe,closingFragment:et},e,ke)}let n=ge(),t=De();if(i("/>")){let Q=o(),ee=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Q);return m({type:"JSXElement",openingElement:ee,children:[],closingElement:null},e,Q)}let s=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,s),C=be(),j=v("</"),_=f(),U=ge();G(U)!==G(n)&&R(`JSX closing tag </${G(U)}> does not match opening <${G(n)}>`,_);let q=v(">"),V=m({type:"JSXClosingElement",name:U},j,q);return m({type:"JSXElement",openingElement:S,children:C,closingElement:V},e,q)},Je=()=>{let e=f();if(N("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(o(),W(true,e)):R("Unexpected 'async'")}if(i("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return o(),g({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return o(),g({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return o(),g({type:"TemplateLiteral",expressions:[],quasis:[le(e.value,true)]},e);if(e.type==="TemplateHead")return je(e);if(e.type==="Keyword")return e.value==="true"?(o(),g({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(o(),g({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(o(),g({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?R("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?R("Anonymous functions are not supported \u2014 use an arrow function"):R(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return o(),g({type:"Identifier",name:e.value},e);if(i("(")){o();let n=E();return v(")"),n}return i("[")?ie():i("{")?Le():i("<")?pe():R(`Unexpected token '${e.value||e.type}'`)},{parseType:re,parseTypeParamList:Fe,parseTypeInScope:Xe}=Ie({tokens:a,pos:()=>p,peek:f,next:o,isP:i,isK:N,expectP:v,fail:R,recordError:D,createMissingNode:yt,unescape:M,typeRefs:k}),Ke=()=>{let e=o(),n=b(),t=[],s=[];i("<")&&(s=Fe(),t=s.map(j=>j.name)),v("=");let S=Xe(s),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return i(";")&&o(),C},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 Ke();if(N("const")||N("let")||N("var")){let S=Se();return i(";")&&o(),S}if(N("if"))return qe();if(N("for"))return Be();if(N("while"))return We();if(N("do"))return _e();if(N("break")||N("continue"))return ze();if(N("return"))return He();if(N("throw"))return Ve();if(N("try"))return Ge();if(N("switch"))return Ye();if(N("import"))return $e();if(N("export"))return Me();if(i("{"))return se();if((N("function")||N("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&R("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),i(";"))return o(),g({type:"EmptyStatement"},e);if(e.type==="Keyword"&&Tt.has(e.value)||e.type==="Identifier"&&bt[e.value]?.(f(1),f(2))===true){let S=gt[e.value];R(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),s=i(";")?o():y();return m({type:"ExpressionStatement",expression:t},t,s)},Se=()=>{let e=o(),n=e.value,t=()=>{let S=x(new Set),C=null;return i("=")?(o(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},s=[t()];for(;i(",");)o(),s.push(t());return m({type:"VariableDeclaration",kind:n,declarations:s},e,s[s.length-1])},Ue=()=>{let e=f();return e.type!=="String"&&R("Expected module specifier string"),o(),g({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},$e=()=>{let e=o(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let s=b();n.push(m({type:"ImportDefaultSpecifier",local:s},s,s)),i(",")&&o();}if(i("*")){let s=o();L("as")||R("Expected 'as' after '*' in import"),o();let S=b();n.push(m({type:"ImportNamespaceSpecifier",local:S},s,S));}else if(i("{")){for(o();!i("}")&&!T();){let s=b(),S=s;if(L("as")&&(o(),S=b()),n.push(m({type:"ImportSpecifier",imported:s,local:S},s,S)),i(","))o();else break}v("}");}L("from")||R("Expected 'from' in import declaration"),o();}let t=Ue();return i(";")&&o(),g({type:"ImportDeclaration",specifiers:n,source:t},e)},Me=()=>{let e=o();if(N("default")){o();let n=E();return i(";")&&o(),g({type:"ExportDefaultDeclaration",declaration:n},e)}if(N("const")||N("let")||N("var")){let n=Se();return i(";")&&o(),g({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(i("{")){o();let n=[];for(;!i("}")&&!T();){let t=b(),s=t;if(L("as")&&(o(),s=b()),n.push(m({type:"ExportSpecifier",local:t,exported:s},t,s)),i(","))o();else break}return v("}"),i(";")&&o(),g({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return R("Expected 'const', '{', or 'default' after 'export'")},ve=()=>f().type==="Identifier"&&f().value==="of",ue=()=>N("in"),Be=()=>{let e=o();v("(");let n=null,t=null;if(!i(";"))if(N("const")||N("let")||N("var")){let j=o(),_=()=>{let q=b();if(i(":")){o();let V=re(new Set);q.typeAnnotation=V.schema,q.typeAnnotationNode=V.node;}return q},U=_();if(ve()||ue()){let q=m({type:"VariableDeclarator",id:U,init:null},U,U);t=m({type:"VariableDeclaration",kind:j.value,declarations:[q]},j,U);}else {let q=Q=>{let ee=null;return i("=")?(o(),ee=E(true)):j.value==="const"&&R("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Q,init:ee},Q,ee??Q)},V=[q(U)];for(;i(",");)o(),V.push(q(_()));n=m({type:"VariableDeclaration",kind:j.value,declarations:V},j,V[V.length-1]);}}else {let j=E(true);ve()||ue()?t=j:n=j;}if(t){let j=ue();o();let _=E();v(")");let U=Y();return m(j?{type:"ForInStatement",left:t,right:_,body:U}:{type:"ForOfStatement",left:t,right:_,body:U,await:false},e,U)}v(";");let s=i(";")?null:E();v(";");let S=i(")")?null:E();v(")");let C=Y();return m({type:"ForStatement",init:n,test:s,update:S,body:C},e,C)},_e=()=>{let e=o(),n=Y();N("while")||R("Expected 'while' after the 'do' body"),o(),v("(");let t=E(),s=v(")");return i(";")&&o(),m({type:"DoWhileStatement",body:n,test:t},e,s)},ze=()=>{let e=o(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=i(";")?o():y();return m({type:n,label:null},e,t)},We=()=>{let e=o();v("(");let n=E();v(")");let t=Y();return m({type:"WhileStatement",test:n,body:t},e,t)},qe=()=>{let e=o();v("(");let n=E();v(")");let t=Y(),s=null;return N("else")&&(o(),s=Y()),m({type:"IfStatement",test:n,consequent:t,alternate:s},e,s??t)},He=()=>{let e=o(),n=null;!i(";")&&!i("}")&&!T()&&(n=E());let t=i(";")?o():y();return m({type:"ReturnStatement",argument:n},e,t)},Ve=()=>{let e=o(),n=E(),t=i(";")?o():y();return m({type:"ThrowStatement",argument:n},e,t)},Ge=()=>{let e=o(),n=se(),t=null,s=null;if(N("catch")){let S=o(),C=null;i("(")&&(o(),C=b(),v(")"));let j=se();t=m({type:"CatchClause",param:C,body:j},S,j);}return N("finally")&&(o(),s=se()),!t&&!s&&R("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:s},e,s??t??n)},Ye=()=>{let e=o();v("(");let n=E();v(")"),v("{");let t=[];for(;!i("}")&&!T();){let S=f(),C=null;N("case")?(o(),C=E()):N("default")?o():R("Expected 'case' or 'default' in switch"),v(":");let j=[];for(;!i("}")&&!N("case")&&!N("default")&&!T();)j.push(Y());t.push(m({type:"SwitchCase",test:C,consequent:j},S,y()??S));}let s=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,s)},se=()=>{let e=v("{"),n=xe(()=>i("}"));return m({type:"BlockStatement",body:n},e,v("}"))},xe=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){o();continue}try{n.push(Y());}catch(t){if(!(t instanceof ne))throw t;T()||o();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=xe(()=>false);return m({type:"Program",sourceType:"module",body:n,...k.length?{typeRefs:k}:{},...u.length?{comments:u}:{},...d.length?{parseDiagnostics:d}:{}},e,y()??e)},eof:T,fail:R,diagnostics:d}}function Re(r){let d=Ae(r),u=d.parseProgram(),a=u.comments;return a?.length&&he(u,a,r),{ast:u,diagnostics:d.diagnostics}}function vt(r){let{ast:d,diagnostics:u}=Re(r),a=u[0];if(a){let p=a.code==="lex-error"?te:ne;throw new p(a.message,a.position,a.index,a.loc,a.end)}return d}function xt(r){let d=Ae(r),u=d.parseExpr();d.eof()||d.fail("Unexpected token after expression");let a=d.diagnostics[0];if(a){let p=a.code==="lex-error"?te:ne;throw new p(a.message,a.position,a.index,a.loc,a.end)}return u}exports.KEYWORDS=Pe;exports.LexError=te;exports.ParseError=ne;exports.attachComments=he;exports.nodeIsMissing=ft;exports.parse=vt;exports.parseExpression=xt;exports.parseTolerant=Re;exports.tokenize=fe;
1
+ 'use strict';var core=require('@tslite/core');var ee=class extends core.TSLiteError{constructor(i,o,p,g,f){super("lex-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="LexError";}},Le=new Set(["const","if","else","return","async","await","true","false","null","typeof","let","var","function","class","new","for","while","do","this","throw","try","catch","finally","switch","case","break","continue","default","void","delete","instanceof","in","yield","import","export","extends","super","debugger","with","enum","interface"]),it=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],me=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Re=r=>me(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",Ie=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",lt=r=>Re(r)||r==="-",ct=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),pt=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(r,c,i){let o=[],p=0,g=1,f=0,s=()=>({line:g,column:f}),y=(N=0)=>r[p+N]??"",F=N=>{let R=s(),U={start:R,end:{line:g,column:f+1}};if(i){i({message:N,position:R,index:p,loc:U,end:p+1});return}throw new ee(N,R,p,U,p+1)},T=()=>{let N=r[p++];return N==="\r"?(r[p]===`
2
+ `&&p++,g++,f=0):N===`
3
+ `||N==="\u2028"||N==="\u2029"?(g++,f=0):f++,N},a=N=>N===`
4
+ `||N==="\r"||N==="\u2028"||N==="\u2029",P=()=>{for(;;){let N=y();if(N===" "||N===" "||N==="\f"||N==="\v"||a(N))T();else if(N==="/"&&y(1)==="/"){let R=p,U=s();T(),T();let I=p;for(;p<r.length&&!a(y());)T();c?.({type:"Line",value:r.slice(I,p),start:R,end:p,loc:{start:U,end:s()}});}else if(N==="/"&&y(1)==="*"){let R=p,U=s();T(),T();let I=p,u=p;for(;;){if(p>=r.length){F("Unterminated block comment"),u=p;break}if(y()==="*"&&y(1)==="/"){u=p,T(),T();break}T();}c?.({type:"Block",value:r.slice(I,u),start:R,end:p,loc:{start:U,end:s()}});}else break}},A=(N,R,U,I)=>{o.push({type:N,value:R,start:U,end:p,loc:{start:I,end:s()}}),E=N==="Punctuator"?ct.has(R):N==="Keyword"?pt.has(R):N==="TemplateHead"||N==="TemplateMiddle";},L=N=>{for(T();p<r.length;){let R=y();if(R===N)return T(),r.slice(b,p);if(a(R))return F("Unterminated string literal"),r.slice(b,p);if(R==="\\"){if(T(),p>=r.length)return F("Unterminated string literal"),r.slice(b,p);T();continue}T();}return F("Unterminated string literal"),r.slice(b,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!Ie(y()))return F("Invalid hexadecimal literal");for(;Ie(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return F("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return F("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return F("Invalid exponent");for(;Z(y());)T();}},v=()=>{let N=p;for(;p<r.length;){let R=y();if(R==="`"){let U=r.slice(N,p);return T(),{raw:U,interp:false}}if(R==="$"&&y(1)==="{"){let U=r.slice(N,p);return T(),T(),{raw:U,interp:true}}if(R==="\\"){if(T(),p>=r.length)break;T();continue}T();}return F("Unterminated template literal"),{raw:r.slice(N,p),interp:false}},b=0,m=0,M=[],E=true,X="js",V=[],z=[];for(;;){if(X==="jsxChildren"){b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(u==="<"){y(1)==="/"?(T(),T(),A("Punctuator","</",b,I),X="jsxCloseTag"):(T(),A("Punctuator","<",b,I),V.push("jsxChildren"),X="jsxOpenTag");continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:"jsxChildren"}),X="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();A("JSXText",r.slice(b,p),b,I);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){P(),b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(me(u)){for(T();lt(y());)T();A("Identifier",r.slice(b,p),b,I);continue}if(u==="/"&&y(1)===">"){T(),T(),A("Punctuator","/>",b,I),X=V.pop()??"js";continue}if(u===">"){T(),A("Punctuator",">",b,I),X=X==="jsxCloseTag"?V.pop()??"js":"jsxChildren";continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:X}),X="js";continue}if(u==='"'||u==="'"){A("String",L(u),b,I);continue}if(u==="="||u==="."||u===":"){T(),A("Punctuator",u,b,I);continue}F(`Unexpected character '${u}' in JSX tag`),T(),A("Unknown",u,b,I);continue}P(),b=p;let N=s();if(p>=r.length){A("EOF","",b,N);break}let R=y();if(me(R)){for(;Re(y());)T();let I=r.slice(b,p);A(Le.has(I)?"Keyword":"Identifier",I,b,N);continue}if(Z(R)||R==="."&&Z(y(1))){D(),A("Number",r.slice(b,p),b,N);continue}if(R==='"'||R==="'"){let I=L(R);A("String",I,b,N);continue}if(R==="`"){T();let{raw:I,interp:u}=v();u?(A("TemplateHead",I,b,N),M.push(m)):A("Template",I,b,N);continue}if(R==="<"&&E){T(),A("Punctuator","<",b,N),V.push("js"),X="jsxOpenTag";continue}if(R==="{"){T(),A("Punctuator","{",b,N),m++;continue}if(R==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:u}=v();A(u?"TemplateMiddle":"TemplateTail",I,b,N),u&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),A("Punctuator","}",b,N),X=I;continue}T(),A("Punctuator","}",b,N),m--;continue}if(R==="?"&&y(1)==="."&&Z(y(2))){T(),A("Punctuator","?",b,N);continue}let U=it.find(I=>r.startsWith(I,p));if(U){for(let I=0;I<U.length;I++)T();A("Punctuator",U,b,N);continue}F(`Unexpected character '${R}'`),T(),A("Unknown",R,b,N);}return o}var ut=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ae=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Oe=new WeakMap;function dt(r){let c=Oe.get(r);if(c)return c;let i=[];for(let o of Object.keys(r)){if(ut.has(o))continue;let p=r[o];if(Ae(p))i.push(p);else if(Array.isArray(p))for(let g of p)Ae(g)&&i.push(g);}return i.sort((o,p)=>o.start-p.start),Oe.set(r,i),i}function ft(r,c){let i=r,o,p;for(;;){let g=dt(i);o=void 0,p=void 0;let f;for(let s of g){if(s.start<=c.start&&c.end<=s.end){f=s;break}if(s.end<=c.start)o=s;else if(c.end<=s.start){p=s;break}}if(!f)break;i=f;}return {enclosing:i,preceding:o,following:p}}var he=(r,c)=>{(r.leadingComments??(r.leadingComments=[])).push(c);},ue=(r,c)=>{(r.trailingComments??(r.trailingComments=[])).push(c);},ge=(r,c)=>{(r.innerComments??(r.innerComments=[])).push(c);};function yt(r,c){for(let i=c;i<r.length;i++){let o=r[i];if(o!==" "&&o!==" "&&o!=="\r"&&o!==`
5
+ `&&o!=="\f")return o}return ""}var mt=new Set([")","]","}",","]);function Te(r,c,i=""){for(let o of c){let{enclosing:p,preceding:g,following:f}=ft(r,o),s=g==null||g.loc.end.line<o.loc.start.line,y=!s&&(f==null||o.loc.end.line<f.loc.start.line);if(s)f?he(f,o):g?ue(g,o):ge(p,o);else if(y)g?ue(g,o):f?he(f,o):ge(p,o);else {let F=mt.has(yt(i,o.end));g&&F?ue(g,o):f?he(f,o):g?ue(g,o):ge(p,o);}}}var be=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ie=(r,c)=>{let i=new Set(r.vars),o=new Map(r.bounds);for(let p of c)i.add(p.name),p.bound||p.isConst?o.set(p.name,{bound:p.bound,isConst:p.isConst}):o.delete(p.name);return {vars:i,bounds:o,noCond:r.noCond,inferSink:r.inferSink}},ht=(r,c)=>{let i=r.bounds.get(c);return i&&(i.bound||i.isConst)?{t:"var",name:c,...i.bound?{bound:i.bound}:{},...i.isConst?{isConst:true}:{}}:{t:"var",name:c}},je=r=>{let{peek:c,next:i,isP:o,isK:p,expectP:g,fail:f,unescape:s}=r,y=(u,h)=>{let d=r.tokens[r.pos()-1]??h;return Object.assign(u,{start:h.start,end:d.end,loc:{start:h.loc.start,end:d.loc.end}})},F=()=>{let u=r.pos(),h=r.tokens[u],d=r.tokens[u-1];return !!d&&!!h&&h.loc.start.line>d.loc.end.line},T=()=>{let u=0;for(let h=r.pos();h<r.tokens.length;h++){let d=r.tokens[h];if(d.type==="EOF")return false;if(d.type==="Punctuator"){if(["(","[","{","<"].includes(d.value))u++;else if([")","]","}",">"].includes(d.value)&&(u--,u===0)){let x=r.tokens[h+1];return !!x&&x.type==="Punctuator"&&x.value==="=>"}}}return false},a=()=>{let u=r.tokens,h=[],d=0,x=false;for(let k=r.pos();k<u.length;k++){let w=u[k];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")d++,d===1&&(x=true);else if(w.value===">"){if(d--,d===0)break}else w.value==="("||w.value==="["||w.value==="{"?(d++,x=false):w.value===")"||w.value==="]"||w.value==="}"?(d--,x=false):w.value===","&&d===1?x=true:x=false;continue}if(d===1&&x){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),x=false;continue}}x=false;}return h},P=u=>{let h=ie(u,a().map(x=>({name:x})));g("<");let d=[];for(;!o(">");){let x=p("const");x&&i();let k=c();k.type!=="Identifier"&&f("Expected type parameter name"),i();let w;p("extends")&&(i(),w=D(h).schema),o("=")&&(i(),D(h)),d.push({name:k.value,...w?{bound:w}:{},...x?{isConst:true}:{}}),o(">")||g(",");}return g(">"),d},A=u=>{g("<");let h=[];for(;!o(">");)h.push(D(u)),o(">")||g(",");return g(">"),h},L=u=>{let h=c();return h.type==="Identifier"&&h.value===u},D=u=>{let h=c(),d=v(u);if(u.noCond||!p("extends")||F())return d;i();let x=[],k=v({...u,noCond:true,inferSink:x});g("?");let w=ie(u,x.map(J=>({name:J}))),j=D(w);g(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:d.node,extendsType:k.node,trueType:j.node,falseType:$.node},h),schema:{t:"conditional",check:d.schema,extends:k.schema,then:j.schema,else:$.schema,...x.length?{infer:x}:{}}}},v=u=>{let h=c();o("|")&&i();let d=b(u);if(!o("|"))return d;let x=[d.node],k=[d.schema];for(;o("|");){i();let w=b(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSUnionType",types:x},h),schema:{t:"union",of:k}}},b=u=>{let h=c();o("&")&&i();let d=m(u);if(!o("&"))return d;let x=[d.node],k=[d.schema];for(;o("&");){i();let w=m(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSIntersectionType",types:x},h),schema:{t:"intersection",of:k}}},m=u=>{let h=c();if(L("keyof")){i();let d=m(u);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:d.node},h),schema:{t:"keyof",of:d.schema}}}if(L("readonly")){i();let d=m(u);return d.schema.t!=="array"&&d.schema.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{node:y({...d.node,readonly:true},h),schema:{...d.schema,readonly:true}}}if(L("infer")){i();let d=c();d.type!=="Identifier"&&f("Expected 'infer' variable name"),i();let x=u.inferSink;return x?(x.push(d.value),{node:y({type:"TSInferType",name:d.value},h),schema:{t:"var",name:d.value}}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return M(u)},M=u=>{let h=c(),d=X(u);for(;o("[")&&!F();)if(i(),o("]"))i(),d={node:y({type:"TSArrayType",elementType:d.node},h),schema:{t:"array",items:d.schema}};else {let x=D(u);g("]"),d={node:y({type:"TSIndexedAccessType",objectType:d.node,indexType:x.node},h),schema:{t:"index",obj:d.schema,key:x.schema}};}return d},E=(u,h,d)=>({node:y({type:"TSKeywordType",kind:h},u),schema:d}),X=u=>{let h=c();if(p("typeof")){i();let k=c();k.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),i();let w=[k.value];for(;o(".");){i();let j=c();j.type!=="Identifier"&&j.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),i(),w.push(j.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(o("<")){let k=P(u),w=ie(u,k);return z(w,h,k.map(j=>j.name))}if(o("(")){if(T())return z(u,h);i();let k=D(u.noCond?{...u,noCond:false}:u);return g(")"),k}if(o("{"))return R()?U(u):I(u);if(o("["))return N(u);let d=c();if(d.type==="String"){i();let k=s(d.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(d.type==="Number"){i();let k=Number(d.value);return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(p("true"))return i(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return i(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return i(),E(h,"null",{t:"null"});if(p("void"))return i(),E(h,"void",{t:"void"});if(d.type==="Identifier"){switch(i(),d.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let k=o("<")?A(u):[],w=k.map(J=>J.schema),j=k.length?k.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:d.value,...j?{typeArguments:j}:{}},h);return u.vars.has(d.value)&&w.length===0?{node:$,schema:ht(u,d.value)}:d.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:d.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:d.value,loc:d.loc}),{node:$,schema:{t:"apply",name:d.value,args:w}})}r.recordError(`Expected a type but got '${d.value||d.type}'`,d);let x=r.createMissingNode({type:"TSTypeReference",typeName:""},d);return r.typeRefs?.push({name:"",loc:x.loc}),{node:x,schema:{t:"unknown"}}},V=u=>{g("(");let h=[];for(;!o(")");){let d=c();d.type!=="Identifier"&&f("Expected parameter name in function type"),i();let x=o("?");x&&i(),g(":");let k=D(u);h.push({node:y({type:"TSTypeParameter",name:d.value,typeAnnotation:k.node,...x?{optional:true}:{}},d),schema:{name:d.value,type:k.schema,...x?{optional:true}:{}}}),o(")")||g(",");}return g(")"),h},z=(u,h,d)=>{let x=V(u);g("=>");let k=D(u);return {node:y({type:"TSFunctionType",params:x.map(w=>w.node),returnType:k.node,...d?.length?{typeParams:[...d]}:{}},h),schema:{t:"fn",params:x.map(w=>w.schema),returns:k.schema,...d?.length?{typeParams:d}:{}}}},N=u=>{let h=c();g("[");let d=[],x=[],k=[];for(;!o("]");){if(o("...")){i();let j=D(u);j.schema.t==="array"?(x.push(j.schema.items),d.push(j.node.elementType),k.push("rest")):(x.push(j.schema),d.push(j.node),k.push("variadic"));}else {let j=D(u),$=o("?");$&&i(),x.push(j.schema),d.push(j.node),k.push($?"optional":"req");}o("]")||g(",");}g("]");let w=k.some(j=>j!=="req");return {node:y({type:"TSTupleType",elementTypes:d,...w?{flags:k}:{}},h),schema:w?{t:"tuple",elements:x,flags:k}:{t:"tuple",elements:x}}},R=()=>{let u=r.tokens,h=r.pos()+1;return u[h]?.type==="Punctuator"&&(u[h]?.value==="+"||u[h]?.value==="-")&&h++,u[h]?.type==="Identifier"&&u[h]?.value==="readonly"&&h++,u[h]?.type==="Punctuator"&&u[h]?.value==="["&&u[h+1]?.type==="Identifier"&&u[h+2]?.type==="Keyword"&&u[h+2]?.value==="in"},U=u=>{let h=c();g("{"),(o("+")||o("-"))&&i(),L("readonly")&&i(),g("[");let d=c();d.type!=="Identifier"&&f("Expected mapped type key name"),i(),p("in")||f("Expected 'in' in mapped type"),i();let x=D(u);L("as")&&f("Key remapping ('as') in mapped types is not supported"),g("]");let k=false,w=o("+")||o("-")?i().value:null;o("?")&&(i(),k=w!=="-"),g(":");let j=D(ie(u,[{name:d.value}]));return (o(";")||o(","))&&i(),g("}"),{node:y({type:"TSMappedType",key:d.value,constraint:x.node,typeAnnotation:j.node,optional:k},h),schema:{t:"mapped",key:d.value,over:x.schema,body:j.schema,optional:k}}},I=u=>{let h=c();g("{");let d=[],x={},k,w=[];for(;!o("}");){let $=c();if(o("(")){let J=V(u);g(":");let B=D(u);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),d.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(o("[")){i(),c().type!=="Identifier"&&f("Expected index signature parameter name"),i(),g(":");let J=c();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),i(),g("]"),g(":");let B=D(u);k={key:J.value,value:B.schema},d.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=c(),B;if(J.type==="Identifier"||J.type==="Keyword")i(),B=J.value;else if(J.type==="String")i(),B=s(J.value.slice(1,-1));else if(J.type==="Number")i(),B=J.value;else return f("Expected property name in object type");let W=o("?");W&&i(),g(":");let ce=D(u);x[B]={type:ce.schema,...W?{optional:true}:{}},d.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ce.node,...W?{optional:true}:{}},$));}if(o(";")||o(","))i();else break}g("}");let j=y({type:"TSTypeLiteral",members:d},h);return w.length>0?Object.keys(x).length>0||k?f("Callable object types (call signatures mixed with members) are not supported"):{node:j,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:j,schema:{t:"object",props:x,...k?{index:k}:{}}}};return {parseType:u=>D(be(u)),parseTypeParamList:()=>P(be()),parseTypeInScope:u=>D(ie(be(),u))}};var te=class extends core.TSLiteError{constructor(i,o,p,g,f){super("parse-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="ParseError";}},Tt=r=>{let c=r;return c!=null&&typeof c.start=="number"&&c.start===c.end&&c.start>=0},bt=(r,c)=>Object.assign(r,{start:c.start,end:c.start,loc:{start:c.loc.start,end:c.loc.start}}),xt={"??":1,"||":2,"&&":3,"|":4,"^":5,"&":6,"==":7,"===":7,"!=":7,"!==":7,"<":8,"<=":8,">":8,">=":8,in:8,as:8,satisfies:8,"<<":9,">>":9,">>>":9,"+":10,"-":10,"*":11,"/":11,"%":11,"**":12},St=new Set(["??","||","&&"]),vt=new Set(["class","debugger","with","enum","interface"]),kt={enum:'use a union of literals (`type T = "a" | "b"`) \u2014 it is serializable',interface:"use a type alias (`type X = { \u2026 }`)",namespace:"use modules (`import`/`export`)",module:"use modules (`import`/`export`)",declare:"declare the shape via the DI env, or with a `type` alias",abstract:"classes are not part of the language \u2014 model with objects + functions"},wt={namespace:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",module:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},Pt=new Set(["=","+=","-=","*=","/=","%="]);function xe(r){let c=[],i=[],o=ae(r,e=>i.push(e),e=>c.push({code:"lex-error",...e})),p=0,g=[],f=(e=0)=>o[Math.min(p+e,o.length-1)],s=()=>o[p++],y=()=>o[p-1],F=()=>{let e=o[p],n=o[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",a=e=>f().type==="Punctuator"&&f().value===e,P=e=>f().type==="Keyword"&&f().value===e,A=e=>f().type==="Identifier"&&f().value===e,L=(e,n=f())=>{throw D(e,n),new te(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=c[c.length-1];t&&t.index===n.start||c.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(a(e)||L(`Expected '${e}' but got '${f().value||f().type}'`),s()),b=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let l=e[++t];switch(l){case "n":n+=`
6
+ `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=l;}}return n},E=(e=false)=>X(e),X=(e=false)=>{let n=V(e),t=f();if(t.type==="Punctuator"&&Pt.has(t.value)){s();let l=X();return m({type:"AssignmentExpression",operator:t.value,left:n,right:l},n,l)}return n},V=(e=false)=>{let n=N(0,e);if(!a("?"))return n;s();let t=E();v(":");let l=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:l},n,l)},z=()=>{let e=f();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:e.value==="satisfies"?{op:"satisfies",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},N=(e,n=false)=>{let t=R();for(;;){let l=z();if(!l)break;let{op:S,consume:C}=l;if(S==="in"&&n)break;let O=xt[S];if(!O||O<=e)break;if(S==="satisfies"){if(F())break;L("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(S==="as"){if(F())break;s();let K=oe(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:K.schema,typeAnnotationNode:K.node},t,y());continue}for(let K=0;K<C;K++)s();let _=N(S==="**"?O-1:O,n);t=m({type:St.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},R=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(s(),b({type:"UpdateExpression",operator:e.value,prefix:true,argument:R()},e)):P("await")?(s(),b({type:"AwaitExpression",argument:R()},e)):P("typeof")||P("void")||P("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?U(e):u()},U=e=>{s();let n=R();return a("**")&&L(`An unary expression with the '${e.value}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.`),b({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=c.length,t=()=>(p=e,c.length=n,void 0);try{if(s(),a(">"))return t();let l=[];for(;l.push(oe(new Set).schema),!!a(",");)s();return a(">")?(s(),a("(")?l:t()):t()}catch{return t()}},u=()=>{let e=$e(),n=false;for(;;)if(a("?."))if(s(),n=true,a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,l);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name after '?.'"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:true},e,l);}else if(a(".")){s();let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:false},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,l);}else if(a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,l);}else if(a("<")){let t=I();if(!t)break;let{args:l,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:l,optional:false,typeArguments:t},e,S);}else if(a("++")||a("--")){let t=s();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!a(")");)e.push(d()),a(")")||v(",");return {args:e,close:v(")")}},d=()=>{if(a("...")){let e=s(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},x=()=>{let e=f();return e.type!=="Identifier"&&L("Expected identifier"),s(),b({type:"Identifier",name:e.value},e)},k=e=>{let n=a("{")?w(e):a("[")?j(e):x();if(a(":")){s();let t=oe(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!a("}");){if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(),C,O=false;if(a(":")?(s(),C=k(e)):(C={...S},O=true),a("=")){s();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:O,computed:false,method:false},S,C));}a("}")||v(",");}let l=v("}");return m({type:"ObjectPattern",properties:t},n,l)},j=e=>{let n=v("["),t=[];for(;!a("]");){if(a(",")){t.push(null),s();continue}if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=k(e);if(a("=")){s();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}a("]")||v(",");}let l=v("]");return m({type:"ArrayPattern",elements:t},n,l)},$=()=>{let e=0;for(let n=p;n<o.length;n++){let t=o[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let l=o[n+1];return !l||l.type!=="Punctuator"?false:l.value==="=>"?true:l.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<o.length;t++){let l=o[t];if(l.type==="EOF")return false;if(l.type==="Punctuator")if(["(","[","{","<"].includes(l.value))n++;else if([")","]","}",">"].includes(l.value)){if(n===0)return false;n--;}else {if(n===0&&l.value==="=>")return true;if(n===0&&(l.value===";"||l.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!a(")");){if(a("...")){let t=s(),l=k(e);n.push(m({type:"RestElement",argument:l},t,l));}else {let t=k(e);if(a("=")){s();let l=E();t=m({type:"AssignmentPattern",left:t,right:l},t,l);}n.push(t);}a(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=a("(")?B():[x()],l,S;if(a(":")){s();let O=oe(new Set);l=O.schema,S=O.node;}v("=>");let C=a("{")?se():E();return b({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...l?{returnType:l,returnTypeNode:S}:{}},n)},ce=()=>{let e=v("["),n=[];for(;!a("]");){if(a(",")){s(),n.push(null);continue}n.push(d()),a("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Fe=()=>{let e=v("{"),n=[];for(;!a("}");){if(a("...")){let t=s(),l=E();n.push(m({type:"SpreadElement",argument:l},t,l));}else {let t=f(),l,S=false;if(a("["))S=true,s(),l=E(),v("]");else if(t.type==="String")s(),l=b({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")s(),l=b({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")s(),l=b({type:"Identifier",name:t.value},t);else return L("Invalid property key");if(a(":")){s();let C=E();n.push(m({type:"Property",key:l,value:C,kind:"init",method:false,shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:l,value:l,kind:"init",method:false,shorthand:true,computed:false},t,l));}a("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},pe=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),Xe=e=>{s();let n=[pe(e.value,false)],t=[];for(;;){t.push(E());let l=f();if(l.type==="TemplateMiddle")s(),n.push(pe(l.value,false));else if(l.type==="TemplateTail"){s(),n.push(pe(l.value,true));break}else L("Expected `}` to close template interpolation");}return b({type:"TemplateLiteral",quasis:n,expressions:t},e)},re=()=>{let e=f();return e.type!=="Identifier"&&L(`Expected JSX identifier but got '${e.value||e.type}'`),b({type:"JSXIdentifier",name:s().value},e)},ke=()=>{let e=re();if(a(":")){s();let n=re();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;a(".");){s();let n=re();e=m({type:"JSXMemberExpression",object:e,property:n},e,n);}return e},Q=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${Q(e.namespace)}:${Q(e.name)}`:`${Q(e.object)}.${Q(e.property)}`,Ue=()=>{let e=f();if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(a("{")){let n=s(),t=X(),l=v("}");return m({type:"JSXExpressionContainer",expression:t},n,l)}return a("<")?fe():L(`Invalid JSX attribute value '${e.value||e.type}'`)},Ke=()=>{let e=[];for(;!a(">")&&!a("/>")&&!T();){if(a("{")){let l=s();v("...");let S=X(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},l,C));continue}let n=re();if(a(":")){s();let l=re();n=m({type:"JSXNamespacedName",namespace:n,name:l},n,l);}let t=null;a("=")&&(s(),t=Ue()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},we=()=>{let e=[];for(;!(a("</")||T());){let n=f();if(n.type==="JSXText")s(),e.push(b({type:"JSXText",value:n.value,raw:n.value},n));else if(a("{")){let t=s();if(a("}")){let l=s(),S=b({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,l));}else {let l=X(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:l},t,S));}}else a("<")?e.push(fe()):L(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},fe=()=>{let e=v("<");if(a(">")){let Y=s(),ne=m({type:"JSXOpeningFragment"},e,Y),rt=we(),ot=v("</"),Ce=v(">"),st=m({type:"JSXClosingFragment"},ot,Ce);return m({type:"JSXFragment",openingFragment:ne,children:rt,closingFragment:st},e,Ce)}let n=ke(),t=Ke();if(a("/>")){let Y=s(),ne=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Y);return m({type:"JSXElement",openingElement:ne,children:[],closingElement:null},e,Y)}let l=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,l),C=we(),O=v("</"),_=f(),K=ke();Q(K)!==Q(n)&&L(`JSX closing tag </${Q(K)}> does not match opening <${Q(n)}>`,_);let H=v(">"),G=m({type:"JSXClosingElement",name:K},O,H);return m({type:"JSXElement",openingElement:S,children:C,closingElement:G},e,H)},$e=()=>{let e=f();if(P("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(s(),W(true,e)):L("Unexpected 'async'")}if(a("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return s(),b({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return s(),b({type:"TemplateLiteral",expressions:[],quasis:[pe(e.value,true)]},e);if(e.type==="TemplateHead")return Xe(e);if(e.type==="Keyword")return e.value==="true"?(s(),b({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(s(),b({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(s(),b({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?L("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?L("Anonymous functions are not supported \u2014 use an arrow function"):L(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return s(),b({type:"Identifier",name:e.value},e);if(a("(")){s();let n=E();return v(")"),n}return a("[")?ce():a("{")?Fe():a("<")?fe():L(`Unexpected token '${e.value||e.type}'`)},{parseType:oe,parseTypeParamList:Me,parseTypeInScope:Be}=je({tokens:o,pos:()=>p,peek:f,next:s,isP:a,isK:P,expectP:v,fail:L,recordError:D,createMissingNode:bt,unescape:M,typeRefs:g}),_e=()=>{let e=s(),n=x(),t=[],l=[];a("<")&&(l=Me(),t=l.map(O=>O.name)),v("=");let S=Be(l),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return a(";")&&s(),C},q=()=>{let e=f();if(e.type==="Identifier"&&e.value==="type"&&f(1).type==="Identifier"&&f(2).type==="Punctuator"&&(f(2).value==="="||f(2).value==="<"))return _e();if(P("const")||P("let")||P("var")){let S=Pe();return a(";")&&s(),S}if(P("if"))return Ye();if(P("for"))return Ve();if(P("while"))return qe();if(P("do"))return Ge();if(P("break")||P("continue"))return Qe();if(P("return"))return Ze();if(P("throw"))return et();if(P("try"))return tt();if(P("switch"))return nt();if(P("import"))return We();if(P("export"))return He();if(a("{"))return se();if((P("function")||P("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&L("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return s(),b({type:"EmptyStatement"},e);if(e.type==="Keyword"&&vt.has(e.value)||e.type==="Identifier"&&wt[e.value]?.(f(1),f(2))===true){let S=kt[e.value];L(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),l=a(";")?s():y();return m({type:"ExpressionStatement",expression:t},t,l)},Pe=()=>{let e=s(),n=e.value,t=()=>{let S=k(new Set),C=null;return a("=")?(s(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},l=[t()];for(;a(",");)s(),l.push(t());return m({type:"VariableDeclaration",kind:n,declarations:l},e,l[l.length-1])},ze=()=>{let e=f();return e.type!=="String"&&L("Expected module specifier string"),s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},We=()=>{let e=s(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let l=x();n.push(m({type:"ImportDefaultSpecifier",local:l},l,l)),a(",")&&s();}if(a("*")){let l=s();A("as")||L("Expected 'as' after '*' in import"),s();let S=x();n.push(m({type:"ImportNamespaceSpecifier",local:S},l,S));}else if(a("{")){for(s();!a("}")&&!T();){let l=x(),S=l;if(A("as")&&(s(),S=x()),n.push(m({type:"ImportSpecifier",imported:l,local:S},l,S)),a(","))s();else break}v("}");}A("from")||L("Expected 'from' in import declaration"),s();}let t=ze();return a(";")&&s(),b({type:"ImportDeclaration",specifiers:n,source:t},e)},He=()=>{let e=s();if(P("default")){s();let n=E();return a(";")&&s(),b({type:"ExportDefaultDeclaration",declaration:n},e)}if(P("const")||P("let")||P("var")){let n=Pe();return a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(a("{")){s();let n=[];for(;!a("}")&&!T();){let t=x(),l=t;if(A("as")&&(s(),l=x()),n.push(m({type:"ExportSpecifier",local:t,exported:l},t,l)),a(","))s();else break}return v("}"),a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return L("Expected 'const', '{', or 'default' after 'export'")},Ne=()=>f().type==="Identifier"&&f().value==="of",ye=()=>P("in"),Ve=()=>{let e=s();v("(");let n=null,t=null;if(!a(";"))if(P("const")||P("let")||P("var")){let O=s(),_=()=>{let H=x();if(a(":")){s();let G=oe(new Set);H.typeAnnotation=G.schema,H.typeAnnotationNode=G.node;}return H},K=_();if(Ne()||ye()){let H=m({type:"VariableDeclarator",id:K,init:null},K,K);t=m({type:"VariableDeclaration",kind:O.value,declarations:[H]},O,K);}else {let H=Y=>{let ne=null;return a("=")?(s(),ne=E(true)):O.value==="const"&&L("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Y,init:ne},Y,ne??Y)},G=[H(K)];for(;a(",");)s(),G.push(H(_()));n=m({type:"VariableDeclaration",kind:O.value,declarations:G},O,G[G.length-1]);}}else {let O=E(true);Ne()||ye()?t=O:n=O;}if(t){let O=ye();s();let _=E();v(")");let K=q();return m(O?{type:"ForInStatement",left:t,right:_,body:K}:{type:"ForOfStatement",left:t,right:_,body:K,await:false},e,K)}v(";");let l=a(";")?null:E();v(";");let S=a(")")?null:E();v(")");let C=q();return m({type:"ForStatement",init:n,test:l,update:S,body:C},e,C)},Ge=()=>{let e=s(),n=q();P("while")||L("Expected 'while' after the 'do' body"),s(),v("(");let t=E(),l=v(")");return a(";")&&s(),m({type:"DoWhileStatement",body:n,test:t},e,l)},Qe=()=>{let e=s(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?s():y();return m({type:n,label:null},e,t)},qe=()=>{let e=s();v("(");let n=E();v(")");let t=q();return m({type:"WhileStatement",test:n,body:t},e,t)},Ye=()=>{let e=s();v("(");let n=E();v(")");let t=q(),l=null;return P("else")&&(s(),l=q()),m({type:"IfStatement",test:n,consequent:t,alternate:l},e,l??t)},Ze=()=>{let e=s(),n=null;!a(";")&&!a("}")&&!T()&&(n=E());let t=a(";")?s():y();return m({type:"ReturnStatement",argument:n},e,t)},et=()=>{let e=s(),n=E(),t=a(";")?s():y();return m({type:"ThrowStatement",argument:n},e,t)},tt=()=>{let e=s(),n=se(),t=null,l=null;if(P("catch")){let S=s(),C=null;a("(")&&(s(),C=x(),v(")"));let O=se();t=m({type:"CatchClause",param:C,body:O},S,O);}return P("finally")&&(s(),l=se()),!t&&!l&&L("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:l},e,l??t??n)},nt=()=>{let e=s();v("(");let n=E();v(")"),v("{");let t=[];for(;!a("}")&&!T();){let S=f(),C=null;P("case")?(s(),C=E()):P("default")?s():L("Expected 'case' or 'default' in switch"),v(":");let O=[];for(;!a("}")&&!P("case")&&!P("default")&&!T();)O.push(q());t.push(m({type:"SwitchCase",test:C,consequent:O},S,y()??S));}let l=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,l)},se=()=>{let e=v("{"),n=Ee(()=>a("}"));return m({type:"BlockStatement",body:n},e,v("}"))},Ee=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){s();continue}try{n.push(q());}catch(t){if(!(t instanceof te))throw t;T()||s();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=Ee(()=>false);return m({type:"Program",sourceType:"module",body:n,...g.length?{typeRefs:g}:{},...i.length?{comments:i}:{},...c.length?{parseDiagnostics:c}:{}},e,y()??e)},eof:T,fail:L,recordError:D,diagnostics:c}}function De(r){let c=xe(r),i=c.parseProgram(),o=i.comments;return o?.length&&Te(i,o,r),{ast:i,diagnostics:c.diagnostics}}function Nt(r){let{ast:c,diagnostics:i}=De(r),o=i[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return c}function Et(r){let c=xe(r),i=c.parseExpr();c.eof()||c.fail("Unexpected token after expression");let o=c.diagnostics[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return i}function Se(r){let c=xe(r),i;try{i=c.parseExpr(),c.eof()||c.recordError("Unexpected token after expression");}catch(o){if(!(o instanceof te)&&!(o instanceof ee))throw o}return {ast:i,diagnostics:c.diagnostics}}var Je="{{";function Ct(r){let c=[0];for(let i=0;i<r.length;i++)r[i]===`
7
+ `&&c.push(i+1);return c}function de(r,c){let i=0,o=r.length-1;for(;i<o;){let p=i+o+1>>1;r[p]<=c?i=p:o=p-1;}return {line:i+1,column:c-r[i]}}var le=(r,c,i)=>({start:de(r,c),end:de(r,i)});function ve(r,c,i){if(Array.isArray(r)){for(let p of r)ve(p,c,i);return}if(!r||typeof r!="object")return;let o=r;typeof o.start=="number"&&typeof o.end=="number"&&(o.start+=c,o.end+=c,o.loc=le(i,o.start,o.end));for(let p in o)p!=="loc"&&ve(o[p],c,i);}function It(r,c,i){let o=r.index+c,p=r.end+c;return {...r,index:o,end:p,position:de(i,o),loc:le(i,o,p)}}var Lt=(r,c,i,o)=>({code:"parse-error",message:r,index:c,end:i,position:de(o,c),loc:le(o,c,i)}),Rt=new Set(["{","(","["]),At=new Set(["}",")","]"]);function Ot(r,c){let i=ae(r.slice(c),void 0,()=>{}),o=0;for(let p=0;p<i.length;p++){let g=i[p];if(g.type==="Punctuator"){if(Rt.has(g.value))o++;else if(At.has(g.value)){if(o>0)o--;else if(g.value==="}"){let f=i[p+1];if(f?.type==="Punctuator"&&f.value==="}"&&f.start===g.end)return c+g.start}}}}return -1}function jt(r){let c=Ct(r),i=[],o=[],p=[],g=0,f=0;for(;f<r.length;)if(r[f]==="{"&&r[f+1]==="{"){let a=f+Je.length,P=Ot(r,a);if(P===-1){p.push(Lt("Unterminated '{{' \u2014 expected '}}'.",f,r.length,c));break}i.push({text:r.slice(g,f),start:g}),o.push({inner:r.slice(a,P),start:a}),g=P+Je.length,f=g;}else f++;i.push({text:r.slice(g),start:g});let s=o.map(a=>{let{ast:P,diagnostics:A}=Se(a.inner);ve(P,a.start,c);for(let L of A)p.push(It(L,a.start,c));return P});if(p.length)return {kind:"error",diagnostics:p};if(o.length===0)return {kind:"literal"};let y=s;if(o.length===1&&i.every(a=>a.text===""))return {kind:"expression",node:y[0]};let F=(a,P)=>({type:"TemplateElement",value:{raw:a.text,cooked:a.text},tail:P,start:a.start,end:a.start+a.text.length,loc:le(c,a.start,a.start+a.text.length)});return {kind:"template",node:{type:"TemplateLiteral",quasis:i.map((a,P)=>F(a,P===i.length-1)),expressions:y,start:0,end:r.length,loc:le(c,0,r.length)}}}exports.KEYWORDS=Le;exports.LexError=ee;exports.ParseError=te;exports.attachComments=Te;exports.nodeIsMissing=Tt;exports.parse=Nt;exports.parseExpression=Et;exports.parseExpressionTolerant=Se;exports.parseInterpolated=jt;exports.parseTolerant=De;exports.tokenize=ae;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { TSLiteError, types } from '@tslite/core';
1
+ import { TSLiteError, types, Expression, TemplateLiteral } from '@tslite/core';
2
2
 
3
3
  /** Code dos erros lançados pelo parser/tokenizer. */
4
4
  type ParserErrorCode = "lex-error" | "parse-error";
@@ -163,6 +163,50 @@ declare function parse(source: string): types.Program;
163
163
  * Entra direto na gramática de expressão — então `{ … }` é objeto, não bloco.
164
164
  */
165
165
  declare function parseExpression(source: string): types.Expression;
166
+ /**
167
+ * Parse **tolerante** de uma única expressão: o par de {@link parseExpression} que
168
+ * o {@link parseTolerant} é de `parse`. Nunca lança por sintaxe — devolve o nó (com
169
+ * buracos de largura zero onde faltava) + os diagnósticos ao lado. Um `!eof` vira
170
+ * diagnóstico (`Unexpected token after expression`), não exceção.
171
+ *
172
+ * Existe porque um consumidor de expressão (o editor, o `parseInterpolated`) precisa
173
+ * do mesmo contrato tolerante que o `parseTolerant` dá pra Program — sem ele, só o
174
+ * `parseExpression` estrito, que lança e perde o resto.
175
+ */
176
+ declare function parseExpressionTolerant(source: string): {
177
+ readonly ast: types.Expression | undefined;
178
+ readonly diagnostics: readonly ParseDiagnostic[];
179
+ };
180
+
181
+ /**
182
+ * O resultado — uma **união discriminada de quatro casos**. A discriminação é o
183
+ * produto principal: quem chama decide o que fazer com cada um (o `literal`, no
184
+ * nosso caso, continua sendo dado do host — por isso NÃO devolve um `Literal`).
185
+ */
186
+ type InterpolatedResult = {
187
+ readonly kind: "literal";
188
+ } | {
189
+ readonly kind: "expression";
190
+ readonly node: Expression;
191
+ } | {
192
+ readonly kind: "template";
193
+ readonly node: TemplateLiteral;
194
+ } | {
195
+ readonly kind: "error";
196
+ readonly diagnostics: readonly ParseDiagnostic[];
197
+ };
198
+ /**
199
+ * Faz o parse de uma string de HOST com interpolação `{{ … }}`. Ver
200
+ * {@link InterpolatedResult}. Tolerante por natureza (o editor é o consumidor
201
+ * principal): erro entra como o 4º braço, nunca lança.
202
+ *
203
+ * A regra de forma (e nada de tipo — o tipo cai no checker):
204
+ * - zero interpolações → `literal`;
205
+ * - **exatamente uma** interpolação e nada em volta → `expression` (o próprio nó);
206
+ * - qualquer outra combinação → `template` (`TemplateLiteral`, que É string);
207
+ * - qualquer diagnóstico (expressão inválida, `{{` sem `}}`) → `error`.
208
+ */
209
+ declare function parseInterpolated(source: string): InterpolatedResult;
166
210
 
167
211
  /** O mínimo que o algoritmo precisa de um nó: posição e os baldes de comentário. */
168
212
  interface Positioned {
@@ -207,4 +251,4 @@ type TypeRef = {
207
251
  readonly loc: SourceLocation;
208
252
  };
209
253
 
210
- export { type CommentToken, KEYWORDS, type LexDiagnostic, LexError, type OnComment, type OnLexError, type ParseDiagnostic, ParseError, type ParseResult, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, attachComments, nodeIsMissing, parse, parseExpression, parseTolerant, tokenize };
254
+ export { type CommentToken, type InterpolatedResult, KEYWORDS, type LexDiagnostic, LexError, type OnComment, type OnLexError, type ParseDiagnostic, ParseError, type ParseResult, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, attachComments, nodeIsMissing, parse, parseExpression, parseExpressionTolerant, parseInterpolated, parseTolerant, tokenize };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TSLiteError, types } from '@tslite/core';
1
+ import { TSLiteError, types, Expression, TemplateLiteral } from '@tslite/core';
2
2
 
3
3
  /** Code dos erros lançados pelo parser/tokenizer. */
4
4
  type ParserErrorCode = "lex-error" | "parse-error";
@@ -163,6 +163,50 @@ declare function parse(source: string): types.Program;
163
163
  * Entra direto na gramática de expressão — então `{ … }` é objeto, não bloco.
164
164
  */
165
165
  declare function parseExpression(source: string): types.Expression;
166
+ /**
167
+ * Parse **tolerante** de uma única expressão: o par de {@link parseExpression} que
168
+ * o {@link parseTolerant} é de `parse`. Nunca lança por sintaxe — devolve o nó (com
169
+ * buracos de largura zero onde faltava) + os diagnósticos ao lado. Um `!eof` vira
170
+ * diagnóstico (`Unexpected token after expression`), não exceção.
171
+ *
172
+ * Existe porque um consumidor de expressão (o editor, o `parseInterpolated`) precisa
173
+ * do mesmo contrato tolerante que o `parseTolerant` dá pra Program — sem ele, só o
174
+ * `parseExpression` estrito, que lança e perde o resto.
175
+ */
176
+ declare function parseExpressionTolerant(source: string): {
177
+ readonly ast: types.Expression | undefined;
178
+ readonly diagnostics: readonly ParseDiagnostic[];
179
+ };
180
+
181
+ /**
182
+ * O resultado — uma **união discriminada de quatro casos**. A discriminação é o
183
+ * produto principal: quem chama decide o que fazer com cada um (o `literal`, no
184
+ * nosso caso, continua sendo dado do host — por isso NÃO devolve um `Literal`).
185
+ */
186
+ type InterpolatedResult = {
187
+ readonly kind: "literal";
188
+ } | {
189
+ readonly kind: "expression";
190
+ readonly node: Expression;
191
+ } | {
192
+ readonly kind: "template";
193
+ readonly node: TemplateLiteral;
194
+ } | {
195
+ readonly kind: "error";
196
+ readonly diagnostics: readonly ParseDiagnostic[];
197
+ };
198
+ /**
199
+ * Faz o parse de uma string de HOST com interpolação `{{ … }}`. Ver
200
+ * {@link InterpolatedResult}. Tolerante por natureza (o editor é o consumidor
201
+ * principal): erro entra como o 4º braço, nunca lança.
202
+ *
203
+ * A regra de forma (e nada de tipo — o tipo cai no checker):
204
+ * - zero interpolações → `literal`;
205
+ * - **exatamente uma** interpolação e nada em volta → `expression` (o próprio nó);
206
+ * - qualquer outra combinação → `template` (`TemplateLiteral`, que É string);
207
+ * - qualquer diagnóstico (expressão inválida, `{{` sem `}}`) → `error`.
208
+ */
209
+ declare function parseInterpolated(source: string): InterpolatedResult;
166
210
 
167
211
  /** O mínimo que o algoritmo precisa de um nó: posição e os baldes de comentário. */
168
212
  interface Positioned {
@@ -207,4 +251,4 @@ type TypeRef = {
207
251
  readonly loc: SourceLocation;
208
252
  };
209
253
 
210
- export { type CommentToken, KEYWORDS, type LexDiagnostic, LexError, type OnComment, type OnLexError, type ParseDiagnostic, ParseError, type ParseResult, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, attachComments, nodeIsMissing, parse, parseExpression, parseTolerant, tokenize };
254
+ export { type CommentToken, type InterpolatedResult, KEYWORDS, type LexDiagnostic, LexError, type OnComment, type OnLexError, type ParseDiagnostic, ParseError, type ParseResult, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, attachComments, nodeIsMissing, parse, parseExpression, parseExpressionTolerant, parseInterpolated, parseTolerant, tokenize };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
- import {TSLiteError}from'@tslite/core';var te=class extends TSLiteError{constructor(u,a,p,k,f){super("lex-error",`${u} (${a.line}:${a.column})`,{position:a,index:p,loc:k,end:f});this.position=a;this.index=p;this.loc=k;this.end=f;this.name="LexError";}},Pe=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"]),nt=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],de=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Ne=r=>de(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",we=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",ot=r=>Ne(r)||r==="-",rt=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),st=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function fe(r,d,u){let a=[],p=0,k=1,f=0,o=()=>({line:k,column:f}),y=(P=0)=>r[p+P]??"",K=P=>{let A=o(),X={start:A,end:{line:k,column:f+1}};if(u){u({message:P,position:A,index:p,loc:X,end:p+1});return}throw new te(P,A,p,X,p+1)},T=()=>{let P=r[p++];return P==="\r"?(r[p]===`
2
- `&&p++,k++,f=0):P===`
3
- `||P==="\u2028"||P==="\u2029"?(k++,f=0):f++,P},i=P=>P===`
4
- `||P==="\r"||P==="\u2028"||P==="\u2029",N=()=>{for(;;){let P=y();if(P===" "||P===" "||P==="\f"||P==="\v"||i(P))T();else if(P==="/"&&y(1)==="/"){let A=p,X=o();T(),T();let I=p;for(;p<r.length&&!i(y());)T();d?.({type:"Line",value:r.slice(I,p),start:A,end:p,loc:{start:X,end:o()}});}else if(P==="/"&&y(1)==="*"){let A=p,X=o();T(),T();let I=p,l=p;for(;;){if(p>=r.length){K("Unterminated block comment"),l=p;break}if(y()==="*"&&y(1)==="/"){l=p,T(),T();break}T();}d?.({type:"Block",value:r.slice(I,l),start:A,end:p,loc:{start:X,end:o()}});}else break}},L=(P,A,X,I)=>{a.push({type:P,value:A,start:X,end:p,loc:{start:I,end:o()}}),E=P==="Punctuator"?rt.has(A):P==="Keyword"?st.has(A):P==="TemplateHead"||P==="TemplateMiddle";},R=P=>{for(T();p<r.length;){let A=y();if(A===P)return T(),r.slice(g,p);if(i(A))return K("Unterminated string literal"),r.slice(g,p);if(A==="\\"){if(T(),p>=r.length)return K("Unterminated string literal"),r.slice(g,p);T();continue}T();}return K("Unterminated string literal"),r.slice(g,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!we(y()))return K("Invalid hexadecimal literal");for(;we(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return K("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return K("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return K("Invalid exponent");for(;Z(y());)T();}},v=()=>{let P=p;for(;p<r.length;){let A=y();if(A==="`"){let X=r.slice(P,p);return T(),{raw:X,interp:false}}if(A==="$"&&y(1)==="{"){let X=r.slice(P,p);return T(),T(),{raw:X,interp:true}}if(A==="\\"){if(T(),p>=r.length)break;T();continue}T();}return K("Unterminated template literal"),{raw:r.slice(P,p),interp:false}},g=0,m=0,M=[],E=true,F="js",H=[],z=[];for(;;){if(F==="jsxChildren"){g=p;let I=o();if(p>=r.length){L("EOF","",g,I);break}let l=y();if(l==="<"){y(1)==="/"?(T(),T(),L("Punctuator","</",g,I),F="jsxCloseTag"):(T(),L("Punctuator","<",g,I),H.push("jsxChildren"),F="jsxOpenTag");continue}if(l==="{"){T(),L("Punctuator","{",g,I),z.push({level:m,restore:"jsxChildren"}),F="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();L("JSXText",r.slice(g,p),g,I);continue}if(F==="jsxOpenTag"||F==="jsxCloseTag"){N(),g=p;let I=o();if(p>=r.length){L("EOF","",g,I);break}let l=y();if(de(l)){for(T();ot(y());)T();L("Identifier",r.slice(g,p),g,I);continue}if(l==="/"&&y(1)===">"){T(),T(),L("Punctuator","/>",g,I),F=H.pop()??"js";continue}if(l===">"){T(),L("Punctuator",">",g,I),F=F==="jsxCloseTag"?H.pop()??"js":"jsxChildren";continue}if(l==="{"){T(),L("Punctuator","{",g,I),z.push({level:m,restore:F}),F="js";continue}if(l==='"'||l==="'"){L("String",R(l),g,I);continue}if(l==="="||l==="."||l===":"){T(),L("Punctuator",l,g,I);continue}K(`Unexpected character '${l}' in JSX tag`),T(),L("Unknown",l,g,I);continue}N(),g=p;let P=o();if(p>=r.length){L("EOF","",g,P);break}let A=y();if(de(A)){for(;Ne(y());)T();let I=r.slice(g,p);L(Pe.has(I)?"Keyword":"Identifier",I,g,P);continue}if(Z(A)||A==="."&&Z(y(1))){D(),L("Number",r.slice(g,p),g,P);continue}if(A==='"'||A==="'"){let I=R(A);L("String",I,g,P);continue}if(A==="`"){T();let{raw:I,interp:l}=v();l?(L("TemplateHead",I,g,P),M.push(m)):L("Template",I,g,P);continue}if(A==="<"&&E){T(),L("Punctuator","<",g,P),H.push("js"),F="jsxOpenTag";continue}if(A==="{"){T(),L("Punctuator","{",g,P),m++;continue}if(A==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:l}=v();L(l?"TemplateMiddle":"TemplateTail",I,g,P),l&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),L("Punctuator","}",g,P),F=I;continue}T(),L("Punctuator","}",g,P),m--;continue}if(A==="?"&&y(1)==="."&&Z(y(2))){T(),L("Punctuator","?",g,P);continue}let X=nt.find(I=>r.startsWith(I,p));if(X){for(let I=0;I<X.length;I++)T();L("Punctuator",X,g,P);continue}K(`Unexpected character '${A}'`),T(),L("Unknown",A,g,P);}return a}var at=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ee=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Ce=new WeakMap;function it(r){let d=Ce.get(r);if(d)return d;let u=[];for(let a of Object.keys(r)){if(at.has(a))continue;let p=r[a];if(Ee(p))u.push(p);else if(Array.isArray(p))for(let k of p)Ee(k)&&u.push(k);}return u.sort((a,p)=>a.start-p.start),Ce.set(r,u),u}function lt(r,d){let u=r,a,p;for(;;){let k=it(u);a=void 0,p=void 0;let f;for(let o of k){if(o.start<=d.start&&d.end<=o.end){f=o;break}if(o.end<=d.start)a=o;else if(d.end<=o.start){p=o;break}}if(!f)break;u=f;}return {enclosing:u,preceding:a,following:p}}var ye=(r,d)=>{(r.leadingComments??(r.leadingComments=[])).push(d);},ce=(r,d)=>{(r.trailingComments??(r.trailingComments=[])).push(d);},me=(r,d)=>{(r.innerComments??(r.innerComments=[])).push(d);};function ct(r,d){for(let u=d;u<r.length;u++){let a=r[u];if(a!==" "&&a!==" "&&a!=="\r"&&a!==`
5
- `&&a!=="\f")return a}return ""}var pt=new Set([")","]","}",","]);function he(r,d,u=""){for(let a of d){let{enclosing:p,preceding:k,following:f}=lt(r,a),o=k==null||k.loc.end.line<a.loc.start.line,y=!o&&(f==null||a.loc.end.line<f.loc.start.line);if(o)f?ye(f,a):k?ce(k,a):me(p,a);else if(y)k?ce(k,a):f?ye(f,a):me(p,a);else {let K=pt.has(ct(u,a.end));k&&K?ce(k,a):f?ye(f,a):k?ce(k,a):me(p,a);}}}var Te=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ae=(r,d)=>{let u=new Set(r.vars),a=new Map(r.bounds);for(let p of d)u.add(p.name),p.bound||p.isConst?a.set(p.name,{bound:p.bound,isConst:p.isConst}):a.delete(p.name);return {vars:u,bounds:a,noCond:r.noCond,inferSink:r.inferSink}},ut=(r,d)=>{let u=r.bounds.get(d);return u&&(u.bound||u.isConst)?{t:"var",name:d,...u.bound?{bound:u.bound}:{},...u.isConst?{isConst:true}:{}}:{t:"var",name:d}},Ie=r=>{let{peek:d,next:u,isP:a,isK:p,expectP:k,fail:f,unescape:o}=r,y=(l,h)=>{let c=r.tokens[r.pos()-1]??h;return Object.assign(l,{start:h.start,end:c.end,loc:{start:h.loc.start,end:c.loc.end}})},K=()=>{let l=r.pos(),h=r.tokens[l],c=r.tokens[l-1];return !!c&&!!h&&h.loc.start.line>c.loc.end.line},T=()=>{let l=0;for(let h=r.pos();h<r.tokens.length;h++){let c=r.tokens[h];if(c.type==="EOF")return false;if(c.type==="Punctuator"){if(["(","[","{","<"].includes(c.value))l++;else if([")","]","}",">"].includes(c.value)&&(l--,l===0)){let b=r.tokens[h+1];return !!b&&b.type==="Punctuator"&&b.value==="=>"}}}return false},i=()=>{let l=r.tokens,h=[],c=0,b=false;for(let x=r.pos();x<l.length;x++){let w=l[x];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")c++,c===1&&(b=true);else if(w.value===">"){if(c--,c===0)break}else w.value==="("||w.value==="["||w.value==="{"?(c++,b=false):w.value===")"||w.value==="]"||w.value==="}"?(c--,b=false):w.value===","&&c===1?b=true:b=false;continue}if(c===1&&b){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),b=false;continue}}b=false;}return h},N=l=>{let h=ae(l,i().map(b=>({name:b})));k("<");let c=[];for(;!a(">");){let b=p("const");b&&u();let x=d();x.type!=="Identifier"&&f("Expected type parameter name"),u();let w;p("extends")&&(u(),w=D(h).schema),a("=")&&(u(),D(h)),c.push({name:x.value,...w?{bound:w}:{},...b?{isConst:true}:{}}),a(">")||k(",");}return k(">"),c},L=l=>{k("<");let h=[];for(;!a(">");)h.push(D(l)),a(">")||k(",");return k(">"),h},R=l=>{let h=d();return h.type==="Identifier"&&h.value===l},D=l=>{let h=d(),c=v(l);if(l.noCond||!p("extends")||K())return c;u();let b=[],x=v({...l,noCond:true,inferSink:b});k("?");let w=ae(l,b.map(J=>({name:J}))),O=D(w);k(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:c.node,extendsType:x.node,trueType:O.node,falseType:$.node},h),schema:{t:"conditional",check:c.schema,extends:x.schema,then:O.schema,else:$.schema,...b.length?{infer:b}:{}}}},v=l=>{let h=d();a("|")&&u();let c=g(l);if(!a("|"))return c;let b=[c.node],x=[c.schema];for(;a("|");){u();let w=g(l);b.push(w.node),x.push(w.schema);}return {node:y({type:"TSUnionType",types:b},h),schema:{t:"union",of:x}}},g=l=>{let h=d();a("&")&&u();let c=m(l);if(!a("&"))return c;let b=[c.node],x=[c.schema];for(;a("&");){u();let w=m(l);b.push(w.node),x.push(w.schema);}return {node:y({type:"TSIntersectionType",types:b},h),schema:{t:"intersection",of:x}}},m=l=>{let h=d();if(R("keyof")){u();let c=m(l);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:c.node},h),schema:{t:"keyof",of:c.schema}}}if(R("readonly")){u();let c=m(l);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(R("infer")){u();let c=d();c.type!=="Identifier"&&f("Expected 'infer' variable name"),u();let b=l.inferSink;return b?(b.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 M(l)},M=l=>{let h=d(),c=F(l);for(;a("[")&&!K();)if(u(),a("]"))u(),c={node:y({type:"TSArrayType",elementType:c.node},h),schema:{t:"array",items:c.schema}};else {let b=D(l);k("]"),c={node:y({type:"TSIndexedAccessType",objectType:c.node,indexType:b.node},h),schema:{t:"index",obj:c.schema,key:b.schema}};}return c},E=(l,h,c)=>({node:y({type:"TSKeywordType",kind:h},l),schema:c}),F=l=>{let h=d();if(p("typeof")){u();let x=d();x.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),u();let w=[x.value];for(;a(".");){u();let O=d();O.type!=="Identifier"&&O.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),u(),w.push(O.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(a("<")){let x=N(l),w=ae(l,x);return z(w,h,x.map(O=>O.name))}if(a("(")){if(T())return z(l,h);u();let x=D(l.noCond?{...l,noCond:false}:l);return k(")"),x}if(a("{"))return A()?X(l):I(l);if(a("["))return P(l);let c=d();if(c.type==="String"){u();let x=o(c.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:x},h),schema:{t:"literal",value:x}}}if(c.type==="Number"){u();let x=Number(c.value);return {node:y({type:"TSLiteralType",literal:x},h),schema:{t:"literal",value:x}}}if(p("true"))return u(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return u(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return u(),E(h,"null",{t:"null"});if(p("void"))return u(),E(h,"void",{t:"void"});if(c.type==="Identifier"){switch(u(),c.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let x=a("<")?L(l):[],w=x.map(J=>J.schema),O=x.length?x.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:c.value,...O?{typeArguments:O}:{}},h);return l.vars.has(c.value)&&w.length===0?{node:$,schema:ut(l,c.value)}:c.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:c.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:c.value,loc:c.loc}),{node:$,schema:{t:"apply",name:c.value,args:w}})}r.recordError(`Expected a type but got '${c.value||c.type}'`,c);let b=r.createMissingNode({type:"TSTypeReference",typeName:""},c);return r.typeRefs?.push({name:"",loc:b.loc}),{node:b,schema:{t:"unknown"}}},H=l=>{k("(");let h=[];for(;!a(")");){let c=d();c.type!=="Identifier"&&f("Expected parameter name in function type"),u();let b=a("?");b&&u(),k(":");let x=D(l);h.push({node:y({type:"TSTypeParameter",name:c.value,typeAnnotation:x.node,...b?{optional:true}:{}},c),schema:{name:c.value,type:x.schema,...b?{optional:true}:{}}}),a(")")||k(",");}return k(")"),h},z=(l,h,c)=>{let b=H(l);k("=>");let x=D(l);return {node:y({type:"TSFunctionType",params:b.map(w=>w.node),returnType:x.node,...c?.length?{typeParams:[...c]}:{}},h),schema:{t:"fn",params:b.map(w=>w.schema),returns:x.schema,...c?.length?{typeParams:c}:{}}}},P=l=>{let h=d();k("[");let c=[],b=[],x=[];for(;!a("]");){if(a("...")){u();let O=D(l);O.schema.t==="array"?(b.push(O.schema.items),c.push(O.node.elementType),x.push("rest")):(b.push(O.schema),c.push(O.node),x.push("variadic"));}else {let O=D(l),$=a("?");$&&u(),b.push(O.schema),c.push(O.node),x.push($?"optional":"req");}a("]")||k(",");}k("]");let w=x.some(O=>O!=="req");return {node:y({type:"TSTupleType",elementTypes:c,...w?{flags:x}:{}},h),schema:w?{t:"tuple",elements:b,flags:x}:{t:"tuple",elements:b}}},A=()=>{let l=r.tokens,h=r.pos()+1;return l[h]?.type==="Punctuator"&&(l[h]?.value==="+"||l[h]?.value==="-")&&h++,l[h]?.type==="Identifier"&&l[h]?.value==="readonly"&&h++,l[h]?.type==="Punctuator"&&l[h]?.value==="["&&l[h+1]?.type==="Identifier"&&l[h+2]?.type==="Keyword"&&l[h+2]?.value==="in"},X=l=>{let h=d();k("{"),(a("+")||a("-"))&&u(),R("readonly")&&u(),k("[");let c=d();c.type!=="Identifier"&&f("Expected mapped type key name"),u(),p("in")||f("Expected 'in' in mapped type"),u();let b=D(l);R("as")&&f("Key remapping ('as') in mapped types is not supported"),k("]");let x=false,w=a("+")||a("-")?u().value:null;a("?")&&(u(),x=w!=="-"),k(":");let O=D(ae(l,[{name:c.value}]));return (a(";")||a(","))&&u(),k("}"),{node:y({type:"TSMappedType",key:c.value,constraint:b.node,typeAnnotation:O.node,optional:x},h),schema:{t:"mapped",key:c.value,over:b.schema,body:O.schema,optional:x}}},I=l=>{let h=d();k("{");let c=[],b={},x,w=[];for(;!a("}");){let $=d();if(a("(")){let J=H(l);k(":");let B=D(l);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),c.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(a("[")){u(),d().type!=="Identifier"&&f("Expected index signature parameter name"),u(),k(":");let J=d();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),u(),k("]"),k(":");let B=D(l);x={key:J.value,value:B.schema},c.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=d(),B;if(J.type==="Identifier"||J.type==="Keyword")u(),B=J.value;else if(J.type==="String")u(),B=o(J.value.slice(1,-1));else if(J.type==="Number")u(),B=J.value;else return f("Expected property name in object type");let W=a("?");W&&u(),k(":");let ie=D(l);b[B]={type:ie.schema,...W?{optional:true}:{}},c.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ie.node,...W?{optional:true}:{}},$));}if(a(";")||a(","))u();else break}k("}");let O=y({type:"TSTypeLiteral",members:c},h);return w.length>0?Object.keys(b).length>0||x?f("Callable object types (call signatures mixed with members) are not supported"):{node:O,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:O,schema:{t:"object",props:b,...x?{index:x}:{}}}};return {parseType:l=>D(Te(l)),parseTypeParamList:()=>N(Te()),parseTypeInScope:l=>D(ae(Te(),l))}};var ne=class extends TSLiteError{constructor(u,a,p,k,f){super("parse-error",`${u} (${a.line}:${a.column})`,{position:a,index:p,loc:k,end:f});this.position=a;this.index=p;this.loc=k;this.end=f;this.name="ParseError";}},ft=r=>{let d=r;return d!=null&&typeof d.start=="number"&&d.start===d.end&&d.start>=0},yt=(r,d)=>Object.assign(r,{start:d.start,end:d.start,loc:{start:d.loc.start,end:d.loc.start}}),mt={"??":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},ht=new Set(["??","||","&&"]),Tt=new Set(["class","debugger","with","enum","interface"]),gt={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"},bt={namespace:(r,d)=>r.type==="Identifier"&&d.type==="Punctuator"&&d.value==="{",module:(r,d)=>r.type==="Identifier"&&d.type==="Punctuator"&&d.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},St=new Set(["=","+=","-=","*=","/=","%="]);function Ae(r){let d=[],u=[],a=fe(r,e=>u.push(e),e=>d.push({code:"lex-error",...e})),p=0,k=[],f=(e=0)=>a[Math.min(p+e,a.length-1)],o=()=>a[p++],y=()=>a[p-1],K=()=>{let e=a[p],n=a[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",i=e=>f().type==="Punctuator"&&f().value===e,N=e=>f().type==="Keyword"&&f().value===e,L=e=>f().type==="Identifier"&&f().value===e,R=(e,n=f())=>{throw D(e,n),new ne(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=d[d.length-1];t&&t.index===n.start||d.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(i(e)||R(`Expected '${e}' but got '${f().value||f().type}'`),o()),g=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let s=e[++t];switch(s){case "n":n+=`
6
- `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=s;}}return n},E=(e=false)=>F(e),F=(e=false)=>{let n=H(e),t=f();if(t.type==="Punctuator"&&St.has(t.value)){o();let s=F();return m({type:"AssignmentExpression",operator:t.value,left:n,right:s},n,s)}return n},H=(e=false)=>{let n=P(0,e);if(!i("?"))return n;o();let t=E();v(":");let s=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:s},n,s)},z=()=>{let e=f();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:e.value==="satisfies"?{op:"satisfies",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},P=(e,n=false)=>{let t=A();for(;;){let s=z();if(!s)break;let{op:S,consume:C}=s;if(S==="in"&&n)break;let j=mt[S];if(!j||j<=e)break;if(S==="satisfies"){if(K())break;R("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(S==="as"){if(K())break;o();let U=re(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:U.schema,typeAnnotationNode:U.node},t,y());continue}for(let U=0;U<C;U++)o();let _=P(S==="**"?j-1:j,n);t=m({type:ht.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},A=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(o(),g({type:"UpdateExpression",operator:e.value,prefix:true,argument:A()},e)):N("await")?(o(),g({type:"AwaitExpression",argument:A()},e)):N("typeof")||N("void")||N("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?X(e):l()},X=e=>{o();let n=A();return i("**")&&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.`),g({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=d.length,t=()=>(p=e,d.length=n,void 0);try{if(o(),i(">"))return t();let s=[];for(;s.push(re(new Set).schema),!!i(",");)o();return i(">")?(o(),i("(")?s:t()):t()}catch{return t()}},l=()=>{let e=Je(),n=false;for(;;)if(i("?."))if(o(),n=true,i("(")){let{args:t,close:s}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,s);}else if(i("[")){o();let t=E(),s=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,s);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&R("Expected property name after '?.'"),o();let s=g({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:s,computed:false,optional:true},e,s);}else if(i(".")){o();let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&R("Expected property name"),o();let s=g({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:s,computed:false,optional:false},e,s);}else if(i("[")){o();let t=E(),s=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,s);}else if(i("(")){let{args:t,close:s}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,s);}else if(i("<")){let t=I();if(!t)break;let{args:s,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:s,optional:false,typeArguments:t},e,S);}else if(i("++")||i("--")){let t=o();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!i(")");)e.push(c()),i(")")||v(",");return {args:e,close:v(")")}},c=()=>{if(i("...")){let e=o(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},b=()=>{let e=f();return e.type!=="Identifier"&&R("Expected identifier"),o(),g({type:"Identifier",name:e.value},e)},x=e=>{let n=i("{")?w(e):i("[")?O(e):b();if(i(":")){o();let t=re(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!i("}");){if(i("...")){let S=o(),C=x(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=b(),C,j=false;if(i(":")?(o(),C=x(e)):(C={...S},j=true),i("=")){o();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:j,computed:false,method:false},S,C));}i("}")||v(",");}let s=v("}");return m({type:"ObjectPattern",properties:t},n,s)},O=e=>{let n=v("["),t=[];for(;!i("]");){if(i(",")){t.push(null),o();continue}if(i("...")){let S=o(),C=x(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(e);if(i("=")){o();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}i("]")||v(",");}let s=v("]");return m({type:"ArrayPattern",elements:t},n,s)},$=()=>{let e=0;for(let n=p;n<a.length;n++){let t=a[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let s=a[n+1];return !s||s.type!=="Punctuator"?false:s.value==="=>"?true:s.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<a.length;t++){let s=a[t];if(s.type==="EOF")return false;if(s.type==="Punctuator")if(["(","[","{","<"].includes(s.value))n++;else if([")","]","}",">"].includes(s.value)){if(n===0)return false;n--;}else {if(n===0&&s.value==="=>")return true;if(n===0&&(s.value===";"||s.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!i(")");){if(i("...")){let t=o(),s=x(e);n.push(m({type:"RestElement",argument:s},t,s));}else {let t=x(e);if(i("=")){o();let s=E();t=m({type:"AssignmentPattern",left:t,right:s},t,s);}n.push(t);}i(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=i("(")?B():[b()],s,S;if(i(":")){o();let j=re(new Set);s=j.schema,S=j.node;}v("=>");let C=i("{")?se():E();return g({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...s?{returnType:s,returnTypeNode:S}:{}},n)},ie=()=>{let e=v("["),n=[];for(;!i("]");){if(i(",")){o(),n.push(null);continue}n.push(c()),i("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Le=()=>{let e=v("{"),n=[];for(;!i("}");){if(i("...")){let t=o(),s=E();n.push(m({type:"SpreadElement",argument:s},t,s));}else {let t=f(),s,S=false;if(i("["))S=true,o(),s=E(),v("]");else if(t.type==="String")o(),s=g({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")o(),s=g({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")o(),s=g({type:"Identifier",name:t.value},t);else return R("Invalid property key");if(i(":")){o();let C=E();n.push(m({type:"Property",key:s,value:C,kind:"init",method:false,shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:s,value:s,kind:"init",method:false,shorthand:true,computed:false},t,s));}i("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},le=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),je=e=>{o();let n=[le(e.value,false)],t=[];for(;;){t.push(E());let s=f();if(s.type==="TemplateMiddle")o(),n.push(le(s.value,false));else if(s.type==="TemplateTail"){o(),n.push(le(s.value,true));break}else R("Expected `}` to close template interpolation");}return g({type:"TemplateLiteral",quasis:n,expressions:t},e)},oe=()=>{let e=f();return e.type!=="Identifier"&&R(`Expected JSX identifier but got '${e.value||e.type}'`),g({type:"JSXIdentifier",name:o().value},e)},ge=()=>{let e=oe();if(i(":")){o();let n=oe();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;i(".");){o();let n=oe();e=m({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)}`,Oe=()=>{let e=f();if(e.type==="String")return o(),g({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(i("{")){let n=o(),t=F(),s=v("}");return m({type:"JSXExpressionContainer",expression:t},n,s)}return i("<")?pe():R(`Invalid JSX attribute value '${e.value||e.type}'`)},De=()=>{let e=[];for(;!i(">")&&!i("/>")&&!T();){if(i("{")){let s=o();v("...");let S=F(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},s,C));continue}let n=oe();if(i(":")){o();let s=oe();n=m({type:"JSXNamespacedName",namespace:n,name:s},n,s);}let t=null;i("=")&&(o(),t=Oe()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},be=()=>{let e=[];for(;!(i("</")||T());){let n=f();if(n.type==="JSXText")o(),e.push(g({type:"JSXText",value:n.value,raw:n.value},n));else if(i("{")){let t=o();if(i("}")){let s=o(),S=g({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,s));}else {let s=F(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:s},t,S));}}else i("<")?e.push(pe()):R(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},pe=()=>{let e=v("<");if(i(">")){let Q=o(),ee=m({type:"JSXOpeningFragment"},e,Q),Qe=be(),Ze=v("</"),ke=v(">"),et=m({type:"JSXClosingFragment"},Ze,ke);return m({type:"JSXFragment",openingFragment:ee,children:Qe,closingFragment:et},e,ke)}let n=ge(),t=De();if(i("/>")){let Q=o(),ee=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Q);return m({type:"JSXElement",openingElement:ee,children:[],closingElement:null},e,Q)}let s=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,s),C=be(),j=v("</"),_=f(),U=ge();G(U)!==G(n)&&R(`JSX closing tag </${G(U)}> does not match opening <${G(n)}>`,_);let q=v(">"),V=m({type:"JSXClosingElement",name:U},j,q);return m({type:"JSXElement",openingElement:S,children:C,closingElement:V},e,q)},Je=()=>{let e=f();if(N("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(o(),W(true,e)):R("Unexpected 'async'")}if(i("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return o(),g({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return o(),g({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return o(),g({type:"TemplateLiteral",expressions:[],quasis:[le(e.value,true)]},e);if(e.type==="TemplateHead")return je(e);if(e.type==="Keyword")return e.value==="true"?(o(),g({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(o(),g({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(o(),g({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?R("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?R("Anonymous functions are not supported \u2014 use an arrow function"):R(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return o(),g({type:"Identifier",name:e.value},e);if(i("(")){o();let n=E();return v(")"),n}return i("[")?ie():i("{")?Le():i("<")?pe():R(`Unexpected token '${e.value||e.type}'`)},{parseType:re,parseTypeParamList:Fe,parseTypeInScope:Xe}=Ie({tokens:a,pos:()=>p,peek:f,next:o,isP:i,isK:N,expectP:v,fail:R,recordError:D,createMissingNode:yt,unescape:M,typeRefs:k}),Ke=()=>{let e=o(),n=b(),t=[],s=[];i("<")&&(s=Fe(),t=s.map(j=>j.name)),v("=");let S=Xe(s),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return i(";")&&o(),C},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 Ke();if(N("const")||N("let")||N("var")){let S=Se();return i(";")&&o(),S}if(N("if"))return qe();if(N("for"))return Be();if(N("while"))return We();if(N("do"))return _e();if(N("break")||N("continue"))return ze();if(N("return"))return He();if(N("throw"))return Ve();if(N("try"))return Ge();if(N("switch"))return Ye();if(N("import"))return $e();if(N("export"))return Me();if(i("{"))return se();if((N("function")||N("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&R("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),i(";"))return o(),g({type:"EmptyStatement"},e);if(e.type==="Keyword"&&Tt.has(e.value)||e.type==="Identifier"&&bt[e.value]?.(f(1),f(2))===true){let S=gt[e.value];R(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),s=i(";")?o():y();return m({type:"ExpressionStatement",expression:t},t,s)},Se=()=>{let e=o(),n=e.value,t=()=>{let S=x(new Set),C=null;return i("=")?(o(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},s=[t()];for(;i(",");)o(),s.push(t());return m({type:"VariableDeclaration",kind:n,declarations:s},e,s[s.length-1])},Ue=()=>{let e=f();return e.type!=="String"&&R("Expected module specifier string"),o(),g({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},$e=()=>{let e=o(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let s=b();n.push(m({type:"ImportDefaultSpecifier",local:s},s,s)),i(",")&&o();}if(i("*")){let s=o();L("as")||R("Expected 'as' after '*' in import"),o();let S=b();n.push(m({type:"ImportNamespaceSpecifier",local:S},s,S));}else if(i("{")){for(o();!i("}")&&!T();){let s=b(),S=s;if(L("as")&&(o(),S=b()),n.push(m({type:"ImportSpecifier",imported:s,local:S},s,S)),i(","))o();else break}v("}");}L("from")||R("Expected 'from' in import declaration"),o();}let t=Ue();return i(";")&&o(),g({type:"ImportDeclaration",specifiers:n,source:t},e)},Me=()=>{let e=o();if(N("default")){o();let n=E();return i(";")&&o(),g({type:"ExportDefaultDeclaration",declaration:n},e)}if(N("const")||N("let")||N("var")){let n=Se();return i(";")&&o(),g({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(i("{")){o();let n=[];for(;!i("}")&&!T();){let t=b(),s=t;if(L("as")&&(o(),s=b()),n.push(m({type:"ExportSpecifier",local:t,exported:s},t,s)),i(","))o();else break}return v("}"),i(";")&&o(),g({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return R("Expected 'const', '{', or 'default' after 'export'")},ve=()=>f().type==="Identifier"&&f().value==="of",ue=()=>N("in"),Be=()=>{let e=o();v("(");let n=null,t=null;if(!i(";"))if(N("const")||N("let")||N("var")){let j=o(),_=()=>{let q=b();if(i(":")){o();let V=re(new Set);q.typeAnnotation=V.schema,q.typeAnnotationNode=V.node;}return q},U=_();if(ve()||ue()){let q=m({type:"VariableDeclarator",id:U,init:null},U,U);t=m({type:"VariableDeclaration",kind:j.value,declarations:[q]},j,U);}else {let q=Q=>{let ee=null;return i("=")?(o(),ee=E(true)):j.value==="const"&&R("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Q,init:ee},Q,ee??Q)},V=[q(U)];for(;i(",");)o(),V.push(q(_()));n=m({type:"VariableDeclaration",kind:j.value,declarations:V},j,V[V.length-1]);}}else {let j=E(true);ve()||ue()?t=j:n=j;}if(t){let j=ue();o();let _=E();v(")");let U=Y();return m(j?{type:"ForInStatement",left:t,right:_,body:U}:{type:"ForOfStatement",left:t,right:_,body:U,await:false},e,U)}v(";");let s=i(";")?null:E();v(";");let S=i(")")?null:E();v(")");let C=Y();return m({type:"ForStatement",init:n,test:s,update:S,body:C},e,C)},_e=()=>{let e=o(),n=Y();N("while")||R("Expected 'while' after the 'do' body"),o(),v("(");let t=E(),s=v(")");return i(";")&&o(),m({type:"DoWhileStatement",body:n,test:t},e,s)},ze=()=>{let e=o(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=i(";")?o():y();return m({type:n,label:null},e,t)},We=()=>{let e=o();v("(");let n=E();v(")");let t=Y();return m({type:"WhileStatement",test:n,body:t},e,t)},qe=()=>{let e=o();v("(");let n=E();v(")");let t=Y(),s=null;return N("else")&&(o(),s=Y()),m({type:"IfStatement",test:n,consequent:t,alternate:s},e,s??t)},He=()=>{let e=o(),n=null;!i(";")&&!i("}")&&!T()&&(n=E());let t=i(";")?o():y();return m({type:"ReturnStatement",argument:n},e,t)},Ve=()=>{let e=o(),n=E(),t=i(";")?o():y();return m({type:"ThrowStatement",argument:n},e,t)},Ge=()=>{let e=o(),n=se(),t=null,s=null;if(N("catch")){let S=o(),C=null;i("(")&&(o(),C=b(),v(")"));let j=se();t=m({type:"CatchClause",param:C,body:j},S,j);}return N("finally")&&(o(),s=se()),!t&&!s&&R("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:s},e,s??t??n)},Ye=()=>{let e=o();v("(");let n=E();v(")"),v("{");let t=[];for(;!i("}")&&!T();){let S=f(),C=null;N("case")?(o(),C=E()):N("default")?o():R("Expected 'case' or 'default' in switch"),v(":");let j=[];for(;!i("}")&&!N("case")&&!N("default")&&!T();)j.push(Y());t.push(m({type:"SwitchCase",test:C,consequent:j},S,y()??S));}let s=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,s)},se=()=>{let e=v("{"),n=xe(()=>i("}"));return m({type:"BlockStatement",body:n},e,v("}"))},xe=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){o();continue}try{n.push(Y());}catch(t){if(!(t instanceof ne))throw t;T()||o();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=xe(()=>false);return m({type:"Program",sourceType:"module",body:n,...k.length?{typeRefs:k}:{},...u.length?{comments:u}:{},...d.length?{parseDiagnostics:d}:{}},e,y()??e)},eof:T,fail:R,diagnostics:d}}function Re(r){let d=Ae(r),u=d.parseProgram(),a=u.comments;return a?.length&&he(u,a,r),{ast:u,diagnostics:d.diagnostics}}function vt(r){let{ast:d,diagnostics:u}=Re(r),a=u[0];if(a){let p=a.code==="lex-error"?te:ne;throw new p(a.message,a.position,a.index,a.loc,a.end)}return d}function xt(r){let d=Ae(r),u=d.parseExpr();d.eof()||d.fail("Unexpected token after expression");let a=d.diagnostics[0];if(a){let p=a.code==="lex-error"?te:ne;throw new p(a.message,a.position,a.index,a.loc,a.end)}return u}export{Pe as KEYWORDS,te as LexError,ne as ParseError,he as attachComments,ft as nodeIsMissing,vt as parse,xt as parseExpression,Re as parseTolerant,fe as tokenize};
1
+ import {TSLiteError}from'@tslite/core';var ee=class extends TSLiteError{constructor(i,o,p,g,f){super("lex-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="LexError";}},Le=new Set(["const","if","else","return","async","await","true","false","null","typeof","let","var","function","class","new","for","while","do","this","throw","try","catch","finally","switch","case","break","continue","default","void","delete","instanceof","in","yield","import","export","extends","super","debugger","with","enum","interface"]),it=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],me=r=>r>="a"&&r<="z"||r>="A"&&r<="Z"||r==="_"||r==="$",Re=r=>me(r)||r>="0"&&r<="9",Z=r=>r>="0"&&r<="9",Ie=r=>Z(r)||r>="a"&&r<="f"||r>="A"&&r<="F",lt=r=>Re(r)||r==="-",ct=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),pt=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(r,c,i){let o=[],p=0,g=1,f=0,s=()=>({line:g,column:f}),y=(N=0)=>r[p+N]??"",F=N=>{let R=s(),U={start:R,end:{line:g,column:f+1}};if(i){i({message:N,position:R,index:p,loc:U,end:p+1});return}throw new ee(N,R,p,U,p+1)},T=()=>{let N=r[p++];return N==="\r"?(r[p]===`
2
+ `&&p++,g++,f=0):N===`
3
+ `||N==="\u2028"||N==="\u2029"?(g++,f=0):f++,N},a=N=>N===`
4
+ `||N==="\r"||N==="\u2028"||N==="\u2029",P=()=>{for(;;){let N=y();if(N===" "||N===" "||N==="\f"||N==="\v"||a(N))T();else if(N==="/"&&y(1)==="/"){let R=p,U=s();T(),T();let I=p;for(;p<r.length&&!a(y());)T();c?.({type:"Line",value:r.slice(I,p),start:R,end:p,loc:{start:U,end:s()}});}else if(N==="/"&&y(1)==="*"){let R=p,U=s();T(),T();let I=p,u=p;for(;;){if(p>=r.length){F("Unterminated block comment"),u=p;break}if(y()==="*"&&y(1)==="/"){u=p,T(),T();break}T();}c?.({type:"Block",value:r.slice(I,u),start:R,end:p,loc:{start:U,end:s()}});}else break}},A=(N,R,U,I)=>{o.push({type:N,value:R,start:U,end:p,loc:{start:I,end:s()}}),E=N==="Punctuator"?ct.has(R):N==="Keyword"?pt.has(R):N==="TemplateHead"||N==="TemplateMiddle";},L=N=>{for(T();p<r.length;){let R=y();if(R===N)return T(),r.slice(b,p);if(a(R))return F("Unterminated string literal"),r.slice(b,p);if(R==="\\"){if(T(),p>=r.length)return F("Unterminated string literal"),r.slice(b,p);T();continue}T();}return F("Unterminated string literal"),r.slice(b,p)},D=()=>{if(y()==="0"&&(y(1)==="x"||y(1)==="X")){if(T(),T(),!Ie(y()))return F("Invalid hexadecimal literal");for(;Ie(y());)T();return}if(y()==="0"&&(y(1)==="b"||y(1)==="B")){if(T(),T(),y()!=="0"&&y()!=="1")return F("Invalid binary literal");for(;y()==="0"||y()==="1";)T();return}if(y()==="0"&&(y(1)==="o"||y(1)==="O")){if(T(),T(),!(y()>="0"&&y()<="7"))return F("Invalid octal literal");for(;y()>="0"&&y()<="7";)T();return}for(;Z(y());)T();if(y()===".")for(T();Z(y());)T();if(y()==="e"||y()==="E"){if(T(),(y()==="+"||y()==="-")&&T(),!Z(y()))return F("Invalid exponent");for(;Z(y());)T();}},v=()=>{let N=p;for(;p<r.length;){let R=y();if(R==="`"){let U=r.slice(N,p);return T(),{raw:U,interp:false}}if(R==="$"&&y(1)==="{"){let U=r.slice(N,p);return T(),T(),{raw:U,interp:true}}if(R==="\\"){if(T(),p>=r.length)break;T();continue}T();}return F("Unterminated template literal"),{raw:r.slice(N,p),interp:false}},b=0,m=0,M=[],E=true,X="js",V=[],z=[];for(;;){if(X==="jsxChildren"){b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(u==="<"){y(1)==="/"?(T(),T(),A("Punctuator","</",b,I),X="jsxCloseTag"):(T(),A("Punctuator","<",b,I),V.push("jsxChildren"),X="jsxOpenTag");continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:"jsxChildren"}),X="js";continue}for(;p<r.length&&y()!=="<"&&y()!=="{";)T();A("JSXText",r.slice(b,p),b,I);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){P(),b=p;let I=s();if(p>=r.length){A("EOF","",b,I);break}let u=y();if(me(u)){for(T();lt(y());)T();A("Identifier",r.slice(b,p),b,I);continue}if(u==="/"&&y(1)===">"){T(),T(),A("Punctuator","/>",b,I),X=V.pop()??"js";continue}if(u===">"){T(),A("Punctuator",">",b,I),X=X==="jsxCloseTag"?V.pop()??"js":"jsxChildren";continue}if(u==="{"){T(),A("Punctuator","{",b,I),z.push({level:m,restore:X}),X="js";continue}if(u==='"'||u==="'"){A("String",L(u),b,I);continue}if(u==="="||u==="."||u===":"){T(),A("Punctuator",u,b,I);continue}F(`Unexpected character '${u}' in JSX tag`),T(),A("Unknown",u,b,I);continue}P(),b=p;let N=s();if(p>=r.length){A("EOF","",b,N);break}let R=y();if(me(R)){for(;Re(y());)T();let I=r.slice(b,p);A(Le.has(I)?"Keyword":"Identifier",I,b,N);continue}if(Z(R)||R==="."&&Z(y(1))){D(),A("Number",r.slice(b,p),b,N);continue}if(R==='"'||R==="'"){let I=L(R);A("String",I,b,N);continue}if(R==="`"){T();let{raw:I,interp:u}=v();u?(A("TemplateHead",I,b,N),M.push(m)):A("Template",I,b,N);continue}if(R==="<"&&E){T(),A("Punctuator","<",b,N),V.push("js"),X="jsxOpenTag";continue}if(R==="{"){T(),A("Punctuator","{",b,N),m++;continue}if(R==="}"){if(M.length>0&&m===M[M.length-1]){M.pop(),T();let{raw:I,interp:u}=v();A(u?"TemplateMiddle":"TemplateTail",I,b,N),u&&M.push(m);continue}if(z.length>0&&m===z[z.length-1].level){let{restore:I}=z.pop();T(),A("Punctuator","}",b,N),X=I;continue}T(),A("Punctuator","}",b,N),m--;continue}if(R==="?"&&y(1)==="."&&Z(y(2))){T(),A("Punctuator","?",b,N);continue}let U=it.find(I=>r.startsWith(I,p));if(U){for(let I=0;I<U.length;I++)T();A("Punctuator",U,b,N);continue}F(`Unexpected character '${R}'`),T(),A("Unknown",R,b,N);}return o}var ut=new Set(["leadingComments","trailingComments","innerComments","comments"]),Ae=r=>typeof r=="object"&&r!==null&&typeof r.type=="string"&&r.loc!=null&&typeof r.start=="number",Oe=new WeakMap;function dt(r){let c=Oe.get(r);if(c)return c;let i=[];for(let o of Object.keys(r)){if(ut.has(o))continue;let p=r[o];if(Ae(p))i.push(p);else if(Array.isArray(p))for(let g of p)Ae(g)&&i.push(g);}return i.sort((o,p)=>o.start-p.start),Oe.set(r,i),i}function ft(r,c){let i=r,o,p;for(;;){let g=dt(i);o=void 0,p=void 0;let f;for(let s of g){if(s.start<=c.start&&c.end<=s.end){f=s;break}if(s.end<=c.start)o=s;else if(c.end<=s.start){p=s;break}}if(!f)break;i=f;}return {enclosing:i,preceding:o,following:p}}var he=(r,c)=>{(r.leadingComments??(r.leadingComments=[])).push(c);},ue=(r,c)=>{(r.trailingComments??(r.trailingComments=[])).push(c);},ge=(r,c)=>{(r.innerComments??(r.innerComments=[])).push(c);};function yt(r,c){for(let i=c;i<r.length;i++){let o=r[i];if(o!==" "&&o!==" "&&o!=="\r"&&o!==`
5
+ `&&o!=="\f")return o}return ""}var mt=new Set([")","]","}",","]);function Te(r,c,i=""){for(let o of c){let{enclosing:p,preceding:g,following:f}=ft(r,o),s=g==null||g.loc.end.line<o.loc.start.line,y=!s&&(f==null||o.loc.end.line<f.loc.start.line);if(s)f?he(f,o):g?ue(g,o):ge(p,o);else if(y)g?ue(g,o):f?he(f,o):ge(p,o);else {let F=mt.has(yt(i,o.end));g&&F?ue(g,o):f?he(f,o):g?ue(g,o):ge(p,o);}}}var be=(r=[])=>({vars:new Set(r),bounds:new Map,noCond:false,inferSink:null}),ie=(r,c)=>{let i=new Set(r.vars),o=new Map(r.bounds);for(let p of c)i.add(p.name),p.bound||p.isConst?o.set(p.name,{bound:p.bound,isConst:p.isConst}):o.delete(p.name);return {vars:i,bounds:o,noCond:r.noCond,inferSink:r.inferSink}},ht=(r,c)=>{let i=r.bounds.get(c);return i&&(i.bound||i.isConst)?{t:"var",name:c,...i.bound?{bound:i.bound}:{},...i.isConst?{isConst:true}:{}}:{t:"var",name:c}},je=r=>{let{peek:c,next:i,isP:o,isK:p,expectP:g,fail:f,unescape:s}=r,y=(u,h)=>{let d=r.tokens[r.pos()-1]??h;return Object.assign(u,{start:h.start,end:d.end,loc:{start:h.loc.start,end:d.loc.end}})},F=()=>{let u=r.pos(),h=r.tokens[u],d=r.tokens[u-1];return !!d&&!!h&&h.loc.start.line>d.loc.end.line},T=()=>{let u=0;for(let h=r.pos();h<r.tokens.length;h++){let d=r.tokens[h];if(d.type==="EOF")return false;if(d.type==="Punctuator"){if(["(","[","{","<"].includes(d.value))u++;else if([")","]","}",">"].includes(d.value)&&(u--,u===0)){let x=r.tokens[h+1];return !!x&&x.type==="Punctuator"&&x.value==="=>"}}}return false},a=()=>{let u=r.tokens,h=[],d=0,x=false;for(let k=r.pos();k<u.length;k++){let w=u[k];if(w.type==="EOF")break;if(w.type==="Punctuator"){if(w.value==="<")d++,d===1&&(x=true);else if(w.value===">"){if(d--,d===0)break}else w.value==="("||w.value==="["||w.value==="{"?(d++,x=false):w.value===")"||w.value==="]"||w.value==="}"?(d--,x=false):w.value===","&&d===1?x=true:x=false;continue}if(d===1&&x){if(w.type==="Keyword"&&w.value==="const")continue;if(w.type==="Identifier"){h.push(w.value),x=false;continue}}x=false;}return h},P=u=>{let h=ie(u,a().map(x=>({name:x})));g("<");let d=[];for(;!o(">");){let x=p("const");x&&i();let k=c();k.type!=="Identifier"&&f("Expected type parameter name"),i();let w;p("extends")&&(i(),w=D(h).schema),o("=")&&(i(),D(h)),d.push({name:k.value,...w?{bound:w}:{},...x?{isConst:true}:{}}),o(">")||g(",");}return g(">"),d},A=u=>{g("<");let h=[];for(;!o(">");)h.push(D(u)),o(">")||g(",");return g(">"),h},L=u=>{let h=c();return h.type==="Identifier"&&h.value===u},D=u=>{let h=c(),d=v(u);if(u.noCond||!p("extends")||F())return d;i();let x=[],k=v({...u,noCond:true,inferSink:x});g("?");let w=ie(u,x.map(J=>({name:J}))),j=D(w);g(":");let $=D(w);return {node:y({type:"TSConditionalType",checkType:d.node,extendsType:k.node,trueType:j.node,falseType:$.node},h),schema:{t:"conditional",check:d.schema,extends:k.schema,then:j.schema,else:$.schema,...x.length?{infer:x}:{}}}},v=u=>{let h=c();o("|")&&i();let d=b(u);if(!o("|"))return d;let x=[d.node],k=[d.schema];for(;o("|");){i();let w=b(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSUnionType",types:x},h),schema:{t:"union",of:k}}},b=u=>{let h=c();o("&")&&i();let d=m(u);if(!o("&"))return d;let x=[d.node],k=[d.schema];for(;o("&");){i();let w=m(u);x.push(w.node),k.push(w.schema);}return {node:y({type:"TSIntersectionType",types:x},h),schema:{t:"intersection",of:k}}},m=u=>{let h=c();if(L("keyof")){i();let d=m(u);return {node:y({type:"TSTypeOperator",operator:"keyof",typeAnnotation:d.node},h),schema:{t:"keyof",of:d.schema}}}if(L("readonly")){i();let d=m(u);return d.schema.t!=="array"&&d.schema.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{node:y({...d.node,readonly:true},h),schema:{...d.schema,readonly:true}}}if(L("infer")){i();let d=c();d.type!=="Identifier"&&f("Expected 'infer' variable name"),i();let x=u.inferSink;return x?(x.push(d.value),{node:y({type:"TSInferType",name:d.value},h),schema:{t:"var",name:d.value}}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return M(u)},M=u=>{let h=c(),d=X(u);for(;o("[")&&!F();)if(i(),o("]"))i(),d={node:y({type:"TSArrayType",elementType:d.node},h),schema:{t:"array",items:d.schema}};else {let x=D(u);g("]"),d={node:y({type:"TSIndexedAccessType",objectType:d.node,indexType:x.node},h),schema:{t:"index",obj:d.schema,key:x.schema}};}return d},E=(u,h,d)=>({node:y({type:"TSKeywordType",kind:h},u),schema:d}),X=u=>{let h=c();if(p("typeof")){i();let k=c();k.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),i();let w=[k.value];for(;o(".");){i();let j=c();j.type!=="Identifier"&&j.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),i(),w.push(j.value);}return {node:y({type:"TSTypeQuery",exprName:w},h),schema:{t:"typeQuery",path:w}}}if(o("<")){let k=P(u),w=ie(u,k);return z(w,h,k.map(j=>j.name))}if(o("(")){if(T())return z(u,h);i();let k=D(u.noCond?{...u,noCond:false}:u);return g(")"),k}if(o("{"))return R()?U(u):I(u);if(o("["))return N(u);let d=c();if(d.type==="String"){i();let k=s(d.value.slice(1,-1));return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(d.type==="Number"){i();let k=Number(d.value);return {node:y({type:"TSLiteralType",literal:k},h),schema:{t:"literal",value:k}}}if(p("true"))return i(),{node:y({type:"TSLiteralType",literal:true},h),schema:{t:"literal",value:true}};if(p("false"))return i(),{node:y({type:"TSLiteralType",literal:false},h),schema:{t:"literal",value:false}};if(p("null"))return i(),E(h,"null",{t:"null"});if(p("void"))return i(),E(h,"void",{t:"void"});if(d.type==="Identifier"){switch(i(),d.value){case "string":return E(h,"string",{t:"string"});case "number":return E(h,"number",{t:"number"});case "boolean":return E(h,"boolean",{t:"boolean"});case "any":return E(h,"any",{t:"any"});case "unknown":return E(h,"unknown",{t:"unknown"});case "undefined":return E(h,"undefined",{t:"undefined"});case "never":return E(h,"never",{t:"union",of:[]})}let k=o("<")?A(u):[],w=k.map(J=>J.schema),j=k.length?k.map(J=>J.node):void 0,$=y({type:"TSTypeReference",typeName:d.value,...j?{typeArguments:j}:{}},h);return u.vars.has(d.value)&&w.length===0?{node:$,schema:ht(u,d.value)}:d.value==="ReturnType"&&w.length===1?{node:$,schema:{t:"returnType",fn:w[0]}}:d.value==="Parameters"&&w.length===1?{node:$,schema:{t:"parameters",fn:w[0]}}:(r.typeRefs?.push({name:d.value,loc:d.loc}),{node:$,schema:{t:"apply",name:d.value,args:w}})}r.recordError(`Expected a type but got '${d.value||d.type}'`,d);let x=r.createMissingNode({type:"TSTypeReference",typeName:""},d);return r.typeRefs?.push({name:"",loc:x.loc}),{node:x,schema:{t:"unknown"}}},V=u=>{g("(");let h=[];for(;!o(")");){let d=c();d.type!=="Identifier"&&f("Expected parameter name in function type"),i();let x=o("?");x&&i(),g(":");let k=D(u);h.push({node:y({type:"TSTypeParameter",name:d.value,typeAnnotation:k.node,...x?{optional:true}:{}},d),schema:{name:d.value,type:k.schema,...x?{optional:true}:{}}}),o(")")||g(",");}return g(")"),h},z=(u,h,d)=>{let x=V(u);g("=>");let k=D(u);return {node:y({type:"TSFunctionType",params:x.map(w=>w.node),returnType:k.node,...d?.length?{typeParams:[...d]}:{}},h),schema:{t:"fn",params:x.map(w=>w.schema),returns:k.schema,...d?.length?{typeParams:d}:{}}}},N=u=>{let h=c();g("[");let d=[],x=[],k=[];for(;!o("]");){if(o("...")){i();let j=D(u);j.schema.t==="array"?(x.push(j.schema.items),d.push(j.node.elementType),k.push("rest")):(x.push(j.schema),d.push(j.node),k.push("variadic"));}else {let j=D(u),$=o("?");$&&i(),x.push(j.schema),d.push(j.node),k.push($?"optional":"req");}o("]")||g(",");}g("]");let w=k.some(j=>j!=="req");return {node:y({type:"TSTupleType",elementTypes:d,...w?{flags:k}:{}},h),schema:w?{t:"tuple",elements:x,flags:k}:{t:"tuple",elements:x}}},R=()=>{let u=r.tokens,h=r.pos()+1;return u[h]?.type==="Punctuator"&&(u[h]?.value==="+"||u[h]?.value==="-")&&h++,u[h]?.type==="Identifier"&&u[h]?.value==="readonly"&&h++,u[h]?.type==="Punctuator"&&u[h]?.value==="["&&u[h+1]?.type==="Identifier"&&u[h+2]?.type==="Keyword"&&u[h+2]?.value==="in"},U=u=>{let h=c();g("{"),(o("+")||o("-"))&&i(),L("readonly")&&i(),g("[");let d=c();d.type!=="Identifier"&&f("Expected mapped type key name"),i(),p("in")||f("Expected 'in' in mapped type"),i();let x=D(u);L("as")&&f("Key remapping ('as') in mapped types is not supported"),g("]");let k=false,w=o("+")||o("-")?i().value:null;o("?")&&(i(),k=w!=="-"),g(":");let j=D(ie(u,[{name:d.value}]));return (o(";")||o(","))&&i(),g("}"),{node:y({type:"TSMappedType",key:d.value,constraint:x.node,typeAnnotation:j.node,optional:k},h),schema:{t:"mapped",key:d.value,over:x.schema,body:j.schema,optional:k}}},I=u=>{let h=c();g("{");let d=[],x={},k,w=[];for(;!o("}");){let $=c();if(o("(")){let J=V(u);g(":");let B=D(u);w.push({t:"fn",params:J.map(W=>W.schema),returns:B.schema}),d.push(y({type:"TSCallSignatureDeclaration",params:J.map(W=>W.node),returnType:B.node},$));}else if(o("[")){i(),c().type!=="Identifier"&&f("Expected index signature parameter name"),i(),g(":");let J=c();(J.type!=="Identifier"||J.value!=="string"&&J.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),i(),g("]"),g(":");let B=D(u);k={key:J.value,value:B.schema},d.push(y({type:"TSIndexSignature",keyType:J.value,typeAnnotation:B.node},$));}else {let J=c(),B;if(J.type==="Identifier"||J.type==="Keyword")i(),B=J.value;else if(J.type==="String")i(),B=s(J.value.slice(1,-1));else if(J.type==="Number")i(),B=J.value;else return f("Expected property name in object type");let W=o("?");W&&i(),g(":");let ce=D(u);x[B]={type:ce.schema,...W?{optional:true}:{}},d.push(y({type:"TSPropertySignature",key:B,typeAnnotation:ce.node,...W?{optional:true}:{}},$));}if(o(";")||o(","))i();else break}g("}");let j=y({type:"TSTypeLiteral",members:d},h);return w.length>0?Object.keys(x).length>0||k?f("Callable object types (call signatures mixed with members) are not supported"):{node:j,schema:w.length===1?w[0]:{t:"overload",signatures:w}}:{node:j,schema:{t:"object",props:x,...k?{index:k}:{}}}};return {parseType:u=>D(be(u)),parseTypeParamList:()=>P(be()),parseTypeInScope:u=>D(ie(be(),u))}};var te=class extends TSLiteError{constructor(i,o,p,g,f){super("parse-error",`${i} (${o.line}:${o.column})`,{position:o,index:p,loc:g,end:f});this.position=o;this.index=p;this.loc=g;this.end=f;this.name="ParseError";}},Tt=r=>{let c=r;return c!=null&&typeof c.start=="number"&&c.start===c.end&&c.start>=0},bt=(r,c)=>Object.assign(r,{start:c.start,end:c.start,loc:{start:c.loc.start,end:c.loc.start}}),xt={"??":1,"||":2,"&&":3,"|":4,"^":5,"&":6,"==":7,"===":7,"!=":7,"!==":7,"<":8,"<=":8,">":8,">=":8,in:8,as:8,satisfies:8,"<<":9,">>":9,">>>":9,"+":10,"-":10,"*":11,"/":11,"%":11,"**":12},St=new Set(["??","||","&&"]),vt=new Set(["class","debugger","with","enum","interface"]),kt={enum:'use a union of literals (`type T = "a" | "b"`) \u2014 it is serializable',interface:"use a type alias (`type X = { \u2026 }`)",namespace:"use modules (`import`/`export`)",module:"use modules (`import`/`export`)",declare:"declare the shape via the DI env, or with a `type` alias",abstract:"classes are not part of the language \u2014 model with objects + functions"},wt={namespace:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",module:(r,c)=>r.type==="Identifier"&&c.type==="Punctuator"&&c.value==="{",declare:r=>r.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(r.value)||r.type==="Identifier"&&["type","namespace","module"].includes(r.value),abstract:r=>r.type==="Keyword"&&r.value==="class"},Pt=new Set(["=","+=","-=","*=","/=","%="]);function xe(r){let c=[],i=[],o=ae(r,e=>i.push(e),e=>c.push({code:"lex-error",...e})),p=0,g=[],f=(e=0)=>o[Math.min(p+e,o.length-1)],s=()=>o[p++],y=()=>o[p-1],F=()=>{let e=o[p],n=o[p-1];return !!e&&!!n&&e.loc.start.line>n.loc.end.line},T=()=>f().type==="EOF",a=e=>f().type==="Punctuator"&&f().value===e,P=e=>f().type==="Keyword"&&f().value===e,A=e=>f().type==="Identifier"&&f().value===e,L=(e,n=f())=>{throw D(e,n),new te(e,n.loc.start,n.start,n.loc,n.end)},D=(e,n=f())=>{let t=c[c.length-1];t&&t.index===n.start||c.push({code:"parse-error",message:e,position:n.loc.start,index:n.start,loc:n.loc,end:n.end});},v=e=>(a(e)||L(`Expected '${e}' but got '${f().value||f().type}'`),s()),b=(e,n)=>{let t=y()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},m=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),M=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let l=e[++t];switch(l){case "n":n+=`
6
+ `;break;case "t":n+=" ";break;case "r":n+="\r";break;case "b":n+="\b";break;case "f":n+="\f";break;case "v":n+="\v";break;case "0":n+="\0";break;case "x":n+=String.fromCharCode(parseInt(e.slice(t+1,t+3),16)),t+=2;break;case "u":if(e[t+1]==="{"){let S=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,S),16)),t=S;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=l;}}return n},E=(e=false)=>X(e),X=(e=false)=>{let n=V(e),t=f();if(t.type==="Punctuator"&&Pt.has(t.value)){s();let l=X();return m({type:"AssignmentExpression",operator:t.value,left:n,right:l},n,l)}return n},V=(e=false)=>{let n=N(0,e);if(!a("?"))return n;s();let t=E();v(":");let l=E();return m({type:"ConditionalExpression",test:n,consequent:t,alternate:l},n,l)},z=()=>{let e=f();if(e.type==="Keyword")return e.value==="in"?{op:"in",consume:1}:null;if(e.type==="Identifier")return e.value==="as"?{op:"as",consume:1}:e.value==="satisfies"?{op:"satisfies",consume:1}:null;if(e.type!=="Punctuator")return null;if(e.value===">"){let n=f(1);if(n.type==="Punctuator"&&n.value===">"&&e.end===n.start){let t=f(2);return t.type==="Punctuator"&&t.value===">"&&n.end===t.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},N=(e,n=false)=>{let t=R();for(;;){let l=z();if(!l)break;let{op:S,consume:C}=l;if(S==="in"&&n)break;let O=xt[S];if(!O||O<=e)break;if(S==="satisfies"){if(F())break;L("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(S==="as"){if(F())break;s();let K=oe(new Set);t=m({type:"TSAsExpression",expression:t,typeAnnotation:K.schema,typeAnnotationNode:K.node},t,y());continue}for(let K=0;K<C;K++)s();let _=N(S==="**"?O-1:O,n);t=m({type:St.has(S)?"LogicalExpression":"BinaryExpression",operator:S,left:t,right:_},t,_);}return t},R=()=>{let e=f();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(s(),b({type:"UpdateExpression",operator:e.value,prefix:true,argument:R()},e)):P("await")?(s(),b({type:"AwaitExpression",argument:R()},e)):P("typeof")||P("void")||P("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?U(e):u()},U=e=>{s();let n=R();return a("**")&&L(`An unary expression with the '${e.value}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.`),b({type:"UnaryExpression",operator:e.value,prefix:true,argument:n},e)},I=()=>{let e=p,n=c.length,t=()=>(p=e,c.length=n,void 0);try{if(s(),a(">"))return t();let l=[];for(;l.push(oe(new Set).schema),!!a(",");)s();return a(">")?(s(),a("(")?l:t()):t()}catch{return t()}},u=()=>{let e=$e(),n=false;for(;;)if(a("?."))if(s(),n=true,a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:true},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,l);}else {let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name after '?.'"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:true},e,l);}else if(a(".")){s();let t=f();t.type!=="Identifier"&&t.type!=="Keyword"&&L("Expected property name"),s();let l=b({type:"Identifier",name:t.value},t);e=m({type:"MemberExpression",object:e,property:l,computed:false,optional:false},e,l);}else if(a("[")){s();let t=E(),l=v("]");e=m({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,l);}else if(a("(")){let{args:t,close:l}=h();e=m({type:"CallExpression",callee:e,arguments:t,optional:false},e,l);}else if(a("<")){let t=I();if(!t)break;let{args:l,close:S}=h();e=m({type:"CallExpression",callee:e,arguments:l,optional:false,typeArguments:t},e,S);}else if(a("++")||a("--")){let t=s();e=m({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?m({type:"ChainExpression",expression:e},e,e):e},h=()=>{v("(");let e=[];for(;!a(")");)e.push(d()),a(")")||v(",");return {args:e,close:v(")")}},d=()=>{if(a("...")){let e=s(),n=E();return m({type:"SpreadElement",argument:n},e,n)}return E()},x=()=>{let e=f();return e.type!=="Identifier"&&L("Expected identifier"),s(),b({type:"Identifier",name:e.value},e)},k=e=>{let n=a("{")?w(e):a("[")?j(e):x();if(a(":")){s();let t=oe(e);n.typeAnnotation=t.schema,n.typeAnnotationNode=t.node;}return n},w=e=>{let n=v("{"),t=[];for(;!a("}");){if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=x(),C,O=false;if(a(":")?(s(),C=k(e)):(C={...S},O=true),a("=")){s();let _=E();C=m({type:"AssignmentPattern",left:C,right:_},C,_);}t.push(m({type:"Property",key:S,value:C,kind:"init",shorthand:O,computed:false,method:false},S,C));}a("}")||v(",");}let l=v("}");return m({type:"ObjectPattern",properties:t},n,l)},j=e=>{let n=v("["),t=[];for(;!a("]");){if(a(",")){t.push(null),s();continue}if(a("...")){let S=s(),C=k(e);t.push(m({type:"RestElement",argument:C},S,C));}else {let S=k(e);if(a("=")){s();let C=E();S=m({type:"AssignmentPattern",left:S,right:C},S,C);}t.push(S);}a("]")||v(",");}let l=v("]");return m({type:"ArrayPattern",elements:t},n,l)},$=()=>{let e=0;for(let n=p;n<o.length;n++){let t=o[n];if(t.type==="EOF")return false;if(t.type==="Punctuator"){if(t.value==="("||t.value==="["||t.value==="{")e++;else if((t.value===")"||t.value==="]"||t.value==="}")&&(e--,e===0)){let l=o[n+1];return !l||l.type!=="Punctuator"?false:l.value==="=>"?true:l.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<o.length;t++){let l=o[t];if(l.type==="EOF")return false;if(l.type==="Punctuator")if(["(","[","{","<"].includes(l.value))n++;else if([")","]","}",">"].includes(l.value)){if(n===0)return false;n--;}else {if(n===0&&l.value==="=>")return true;if(n===0&&(l.value===";"||l.value===","))return false}}return false},B=(e=new Set)=>{v("(");let n=[];for(;!a(")");){if(a("...")){let t=s(),l=k(e);n.push(m({type:"RestElement",argument:l},t,l));}else {let t=k(e);if(a("=")){s();let l=E();t=m({type:"AssignmentPattern",left:t,right:l},t,l);}n.push(t);}a(")")||v(",");}return v(")"),n},W=(e,n)=>{let t=a("(")?B():[x()],l,S;if(a(":")){s();let O=oe(new Set);l=O.schema,S=O.node;}v("=>");let C=a("{")?se():E();return b({type:"ArrowFunctionExpression",params:t,body:C,async:e,expression:C.type!=="BlockStatement",generator:false,...l?{returnType:l,returnTypeNode:S}:{}},n)},ce=()=>{let e=v("["),n=[];for(;!a("]");){if(a(",")){s(),n.push(null);continue}n.push(d()),a("]")||v(",");}return m({type:"ArrayExpression",elements:n},e,v("]"))},Fe=()=>{let e=v("{"),n=[];for(;!a("}");){if(a("...")){let t=s(),l=E();n.push(m({type:"SpreadElement",argument:l},t,l));}else {let t=f(),l,S=false;if(a("["))S=true,s(),l=E(),v("]");else if(t.type==="String")s(),l=b({type:"Literal",value:M(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")s(),l=b({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")s(),l=b({type:"Identifier",name:t.value},t);else return L("Invalid property key");if(a(":")){s();let C=E();n.push(m({type:"Property",key:l,value:C,kind:"init",method:false,shorthand:false,computed:S},t,C));}else n.push(m({type:"Property",key:l,value:l,kind:"init",method:false,shorthand:true,computed:false},t,l));}a("}")||v(",");}return m({type:"ObjectExpression",properties:n},e,v("}"))},pe=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:M(e)},tail:n}),Xe=e=>{s();let n=[pe(e.value,false)],t=[];for(;;){t.push(E());let l=f();if(l.type==="TemplateMiddle")s(),n.push(pe(l.value,false));else if(l.type==="TemplateTail"){s(),n.push(pe(l.value,true));break}else L("Expected `}` to close template interpolation");}return b({type:"TemplateLiteral",quasis:n,expressions:t},e)},re=()=>{let e=f();return e.type!=="Identifier"&&L(`Expected JSX identifier but got '${e.value||e.type}'`),b({type:"JSXIdentifier",name:s().value},e)},ke=()=>{let e=re();if(a(":")){s();let n=re();return m({type:"JSXNamespacedName",namespace:e,name:n},e,n)}for(;a(".");){s();let n=re();e=m({type:"JSXMemberExpression",object:e,property:n},e,n);}return e},Q=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${Q(e.namespace)}:${Q(e.name)}`:`${Q(e.object)}.${Q(e.property)}`,Ue=()=>{let e=f();if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(a("{")){let n=s(),t=X(),l=v("}");return m({type:"JSXExpressionContainer",expression:t},n,l)}return a("<")?fe():L(`Invalid JSX attribute value '${e.value||e.type}'`)},Ke=()=>{let e=[];for(;!a(">")&&!a("/>")&&!T();){if(a("{")){let l=s();v("...");let S=X(),C=v("}");e.push(m({type:"JSXSpreadAttribute",argument:S},l,C));continue}let n=re();if(a(":")){s();let l=re();n=m({type:"JSXNamespacedName",namespace:n,name:l},n,l);}let t=null;a("=")&&(s(),t=Ue()),e.push(m({type:"JSXAttribute",name:n,value:t},n,t??n));}return e},we=()=>{let e=[];for(;!(a("</")||T());){let n=f();if(n.type==="JSXText")s(),e.push(b({type:"JSXText",value:n.value,raw:n.value},n));else if(a("{")){let t=s();if(a("}")){let l=s(),S=b({type:"JSXEmptyExpression"},t);e.push(m({type:"JSXExpressionContainer",expression:S},t,l));}else {let l=X(),S=v("}");e.push(m({type:"JSXExpressionContainer",expression:l},t,S));}}else a("<")?e.push(fe()):L(`Unexpected token in JSX children '${n.value||n.type}'`);}return e},fe=()=>{let e=v("<");if(a(">")){let Y=s(),ne=m({type:"JSXOpeningFragment"},e,Y),rt=we(),ot=v("</"),Ce=v(">"),st=m({type:"JSXClosingFragment"},ot,Ce);return m({type:"JSXFragment",openingFragment:ne,children:rt,closingFragment:st},e,Ce)}let n=ke(),t=Ke();if(a("/>")){let Y=s(),ne=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:true},e,Y);return m({type:"JSXElement",openingElement:ne,children:[],closingElement:null},e,Y)}let l=v(">"),S=m({type:"JSXOpeningElement",name:n,attributes:t,selfClosing:false},e,l),C=we(),O=v("</"),_=f(),K=ke();Q(K)!==Q(n)&&L(`JSX closing tag </${Q(K)}> does not match opening <${Q(n)}>`,_);let H=v(">"),G=m({type:"JSXClosingElement",name:K},O,H);return m({type:"JSXElement",openingElement:S,children:C,closingElement:G},e,H)},$e=()=>{let e=f();if(P("async")){let n=f(1);return n.value==="("||n.type==="Identifier"?(s(),W(true,e)):L("Unexpected 'async'")}if(a("(")&&$()||e.type==="Identifier"&&f(1).type==="Punctuator"&&f(1).value==="=>")return W(false,e);if(e.type==="Number")return s(),b({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return s(),b({type:"TemplateLiteral",expressions:[],quasis:[pe(e.value,true)]},e);if(e.type==="TemplateHead")return Xe(e);if(e.type==="Keyword")return e.value==="true"?(s(),b({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(s(),b({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(s(),b({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?L("'new' is not supported \u2014 the language has no constructors (FP / portable IR). Use a factory function provided via DI (e.g. `now()` instead of `new Date()`)."):e.value==="function"?L("Anonymous functions are not supported \u2014 use an arrow function"):L(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return s(),b({type:"Identifier",name:e.value},e);if(a("(")){s();let n=E();return v(")"),n}return a("[")?ce():a("{")?Fe():a("<")?fe():L(`Unexpected token '${e.value||e.type}'`)},{parseType:oe,parseTypeParamList:Me,parseTypeInScope:Be}=je({tokens:o,pos:()=>p,peek:f,next:s,isP:a,isK:P,expectP:v,fail:L,recordError:D,createMissingNode:bt,unescape:M,typeRefs:g}),_e=()=>{let e=s(),n=x(),t=[],l=[];a("<")&&(l=Me(),t=l.map(O=>O.name)),v("=");let S=Be(l),C=m({type:"TSTypeAliasDeclaration",id:n,typeParameters:t,typeAnnotation:S.schema,typeNode:S.node},e,y());return a(";")&&s(),C},q=()=>{let e=f();if(e.type==="Identifier"&&e.value==="type"&&f(1).type==="Identifier"&&f(2).type==="Punctuator"&&(f(2).value==="="||f(2).value==="<"))return _e();if(P("const")||P("let")||P("var")){let S=Pe();return a(";")&&s(),S}if(P("if"))return Ye();if(P("for"))return Ve();if(P("while"))return qe();if(P("do"))return Ge();if(P("break")||P("continue"))return Qe();if(P("return"))return Ze();if(P("throw"))return et();if(P("try"))return tt();if(P("switch"))return nt();if(P("import"))return We();if(P("export"))return He();if(a("{"))return se();if((P("function")||P("async")&&f(1).type==="Keyword"&&f(1).value==="function")&&L("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return s(),b({type:"EmptyStatement"},e);if(e.type==="Keyword"&&vt.has(e.value)||e.type==="Identifier"&&wt[e.value]?.(f(1),f(2))===true){let S=kt[e.value];L(`'${e.value}' is not supported in this language`+(S?` \u2014 ${S}`:""));}let t=E(),l=a(";")?s():y();return m({type:"ExpressionStatement",expression:t},t,l)},Pe=()=>{let e=s(),n=e.value,t=()=>{let S=k(new Set),C=null;return a("=")?(s(),C=E()):n==="const"&&D("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:S,init:C},S,C??S)},l=[t()];for(;a(",");)s(),l.push(t());return m({type:"VariableDeclaration",kind:n,declarations:l},e,l[l.length-1])},ze=()=>{let e=f();return e.type!=="String"&&L("Expected module specifier string"),s(),b({type:"Literal",value:M(e.value.slice(1,-1)),raw:e.value},e)},We=()=>{let e=s(),n=[];if(f().type!=="String"){if(f().type==="Identifier"){let l=x();n.push(m({type:"ImportDefaultSpecifier",local:l},l,l)),a(",")&&s();}if(a("*")){let l=s();A("as")||L("Expected 'as' after '*' in import"),s();let S=x();n.push(m({type:"ImportNamespaceSpecifier",local:S},l,S));}else if(a("{")){for(s();!a("}")&&!T();){let l=x(),S=l;if(A("as")&&(s(),S=x()),n.push(m({type:"ImportSpecifier",imported:l,local:S},l,S)),a(","))s();else break}v("}");}A("from")||L("Expected 'from' in import declaration"),s();}let t=ze();return a(";")&&s(),b({type:"ImportDeclaration",specifiers:n,source:t},e)},He=()=>{let e=s();if(P("default")){s();let n=E();return a(";")&&s(),b({type:"ExportDefaultDeclaration",declaration:n},e)}if(P("const")||P("let")||P("var")){let n=Pe();return a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:n,specifiers:[],source:null},e)}if(a("{")){s();let n=[];for(;!a("}")&&!T();){let t=x(),l=t;if(A("as")&&(s(),l=x()),n.push(m({type:"ExportSpecifier",local:t,exported:l},t,l)),a(","))s();else break}return v("}"),a(";")&&s(),b({type:"ExportNamedDeclaration",declaration:null,specifiers:n,source:null},e)}return L("Expected 'const', '{', or 'default' after 'export'")},Ne=()=>f().type==="Identifier"&&f().value==="of",ye=()=>P("in"),Ve=()=>{let e=s();v("(");let n=null,t=null;if(!a(";"))if(P("const")||P("let")||P("var")){let O=s(),_=()=>{let H=x();if(a(":")){s();let G=oe(new Set);H.typeAnnotation=G.schema,H.typeAnnotationNode=G.node;}return H},K=_();if(Ne()||ye()){let H=m({type:"VariableDeclarator",id:K,init:null},K,K);t=m({type:"VariableDeclaration",kind:O.value,declarations:[H]},O,K);}else {let H=Y=>{let ne=null;return a("=")?(s(),ne=E(true)):O.value==="const"&&L("'const' declarations must be initialized"),m({type:"VariableDeclarator",id:Y,init:ne},Y,ne??Y)},G=[H(K)];for(;a(",");)s(),G.push(H(_()));n=m({type:"VariableDeclaration",kind:O.value,declarations:G},O,G[G.length-1]);}}else {let O=E(true);Ne()||ye()?t=O:n=O;}if(t){let O=ye();s();let _=E();v(")");let K=q();return m(O?{type:"ForInStatement",left:t,right:_,body:K}:{type:"ForOfStatement",left:t,right:_,body:K,await:false},e,K)}v(";");let l=a(";")?null:E();v(";");let S=a(")")?null:E();v(")");let C=q();return m({type:"ForStatement",init:n,test:l,update:S,body:C},e,C)},Ge=()=>{let e=s(),n=q();P("while")||L("Expected 'while' after the 'do' body"),s(),v("(");let t=E(),l=v(")");return a(";")&&s(),m({type:"DoWhileStatement",body:n,test:t},e,l)},Qe=()=>{let e=s(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?s():y();return m({type:n,label:null},e,t)},qe=()=>{let e=s();v("(");let n=E();v(")");let t=q();return m({type:"WhileStatement",test:n,body:t},e,t)},Ye=()=>{let e=s();v("(");let n=E();v(")");let t=q(),l=null;return P("else")&&(s(),l=q()),m({type:"IfStatement",test:n,consequent:t,alternate:l},e,l??t)},Ze=()=>{let e=s(),n=null;!a(";")&&!a("}")&&!T()&&(n=E());let t=a(";")?s():y();return m({type:"ReturnStatement",argument:n},e,t)},et=()=>{let e=s(),n=E(),t=a(";")?s():y();return m({type:"ThrowStatement",argument:n},e,t)},tt=()=>{let e=s(),n=se(),t=null,l=null;if(P("catch")){let S=s(),C=null;a("(")&&(s(),C=x(),v(")"));let O=se();t=m({type:"CatchClause",param:C,body:O},S,O);}return P("finally")&&(s(),l=se()),!t&&!l&&L("'try' requires a 'catch' or 'finally' clause"),m({type:"TryStatement",block:n,handler:t,finalizer:l},e,l??t??n)},nt=()=>{let e=s();v("(");let n=E();v(")"),v("{");let t=[];for(;!a("}")&&!T();){let S=f(),C=null;P("case")?(s(),C=E()):P("default")?s():L("Expected 'case' or 'default' in switch"),v(":");let O=[];for(;!a("}")&&!P("case")&&!P("default")&&!T();)O.push(q());t.push(m({type:"SwitchCase",test:C,consequent:O},S,y()??S));}let l=v("}");return m({type:"SwitchStatement",discriminant:n,cases:t},e,l)},se=()=>{let e=v("{"),n=Ee(()=>a("}"));return m({type:"BlockStatement",body:n},e,v("}"))},Ee=e=>{let n=[];for(;!e()&&!T();){if(f().type==="Unknown"){s();continue}try{n.push(q());}catch(t){if(!(t instanceof te))throw t;T()||s();}}return n};return {parseExpr:E,parseProgram:()=>{let e=f(),n=Ee(()=>false);return m({type:"Program",sourceType:"module",body:n,...g.length?{typeRefs:g}:{},...i.length?{comments:i}:{},...c.length?{parseDiagnostics:c}:{}},e,y()??e)},eof:T,fail:L,recordError:D,diagnostics:c}}function De(r){let c=xe(r),i=c.parseProgram(),o=i.comments;return o?.length&&Te(i,o,r),{ast:i,diagnostics:c.diagnostics}}function Nt(r){let{ast:c,diagnostics:i}=De(r),o=i[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return c}function Et(r){let c=xe(r),i=c.parseExpr();c.eof()||c.fail("Unexpected token after expression");let o=c.diagnostics[0];if(o){let p=o.code==="lex-error"?ee:te;throw new p(o.message,o.position,o.index,o.loc,o.end)}return i}function Se(r){let c=xe(r),i;try{i=c.parseExpr(),c.eof()||c.recordError("Unexpected token after expression");}catch(o){if(!(o instanceof te)&&!(o instanceof ee))throw o}return {ast:i,diagnostics:c.diagnostics}}var Je="{{";function Ct(r){let c=[0];for(let i=0;i<r.length;i++)r[i]===`
7
+ `&&c.push(i+1);return c}function de(r,c){let i=0,o=r.length-1;for(;i<o;){let p=i+o+1>>1;r[p]<=c?i=p:o=p-1;}return {line:i+1,column:c-r[i]}}var le=(r,c,i)=>({start:de(r,c),end:de(r,i)});function ve(r,c,i){if(Array.isArray(r)){for(let p of r)ve(p,c,i);return}if(!r||typeof r!="object")return;let o=r;typeof o.start=="number"&&typeof o.end=="number"&&(o.start+=c,o.end+=c,o.loc=le(i,o.start,o.end));for(let p in o)p!=="loc"&&ve(o[p],c,i);}function It(r,c,i){let o=r.index+c,p=r.end+c;return {...r,index:o,end:p,position:de(i,o),loc:le(i,o,p)}}var Lt=(r,c,i,o)=>({code:"parse-error",message:r,index:c,end:i,position:de(o,c),loc:le(o,c,i)}),Rt=new Set(["{","(","["]),At=new Set(["}",")","]"]);function Ot(r,c){let i=ae(r.slice(c),void 0,()=>{}),o=0;for(let p=0;p<i.length;p++){let g=i[p];if(g.type==="Punctuator"){if(Rt.has(g.value))o++;else if(At.has(g.value)){if(o>0)o--;else if(g.value==="}"){let f=i[p+1];if(f?.type==="Punctuator"&&f.value==="}"&&f.start===g.end)return c+g.start}}}}return -1}function jt(r){let c=Ct(r),i=[],o=[],p=[],g=0,f=0;for(;f<r.length;)if(r[f]==="{"&&r[f+1]==="{"){let a=f+Je.length,P=Ot(r,a);if(P===-1){p.push(Lt("Unterminated '{{' \u2014 expected '}}'.",f,r.length,c));break}i.push({text:r.slice(g,f),start:g}),o.push({inner:r.slice(a,P),start:a}),g=P+Je.length,f=g;}else f++;i.push({text:r.slice(g),start:g});let s=o.map(a=>{let{ast:P,diagnostics:A}=Se(a.inner);ve(P,a.start,c);for(let L of A)p.push(It(L,a.start,c));return P});if(p.length)return {kind:"error",diagnostics:p};if(o.length===0)return {kind:"literal"};let y=s;if(o.length===1&&i.every(a=>a.text===""))return {kind:"expression",node:y[0]};let F=(a,P)=>({type:"TemplateElement",value:{raw:a.text,cooked:a.text},tail:P,start:a.start,end:a.start+a.text.length,loc:le(c,a.start,a.start+a.text.length)});return {kind:"template",node:{type:"TemplateLiteral",quasis:i.map((a,P)=>F(a,P===i.length-1)),expressions:y,start:0,end:r.length,loc:le(c,0,r.length)}}}export{Le as KEYWORDS,ee as LexError,te as ParseError,Te as attachComments,Tt as nodeIsMissing,Nt as parse,Et as parseExpression,Se as parseExpressionTolerant,jt as parseInterpolated,De as parseTolerant,ae as tokenize};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tslite/parser",
3
- "version": "0.5.1",
3
+ "version": "0.6.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.4.0"
43
+ "@tslite/core": "0.5.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@tslite/capability": "0.2.0",
47
- "@tslite/printer": "0.3.0",
48
- "@tslite/interpret": "0.3.0",
49
- "@tslite/checker": "0.5.0"
47
+ "@tslite/printer": "0.3.1",
48
+ "@tslite/interpret": "0.3.1",
49
+ "@tslite/checker": "0.5.1"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup",