@tslite/parser 1.0.0 → 1.1.1

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