@tslite/parser 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +48 -4
- package/dist/index.d.ts +48 -4
- package/dist/index.js +5 -5
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
'use strict';var core=require('@tslite/core');var
|
|
2
|
-
`&&
|
|
3
|
-
`||
|
|
4
|
-
`||l==="\r"||l==="\u2028"||l==="\u2029",y=()=>{for(;;){let l=u();if(l===" "||l===" "||l==="\f"||l==="\v"||w(l))s();else if(l==="/"&&u(1)==="/")for(;p<f.length&&!w(u());)s();else if(l==="/"&&u(1)==="*")for(s(),s();;){if(p>=f.length&&a("Unterminated block comment"),u()==="*"&&u(1)==="/"){s(),s();break}s();}else break}},h=(l,m,N,k)=>{x.push({type:l,value:m,start:N,end:p,loc:{start:k,end:b()}});},c=l=>{for(s();p<f.length;){let m=u();if(m===l)return s(),f.slice(S,p);if(w(m)&&a("Unterminated string literal"),m==="\\"){s(),p>=f.length&&a("Unterminated string literal"),s();continue}s();}return a("Unterminated string literal")},L=()=>{if(u()==="0"&&(u(1)==="x"||u(1)==="X")){for(s(),s(),H(u())||a("Invalid hexadecimal literal");H(u());)s();return}if(u()==="0"&&(u(1)==="b"||u(1)==="B")){for(s(),s(),u()!=="0"&&u()!=="1"&&a("Invalid binary literal");u()==="0"||u()==="1";)s();return}if(u()==="0"&&(u(1)==="o"||u(1)==="O")){for(s(),s(),u()>="0"&&u()<="7"||a("Invalid octal literal");u()>="0"&&u()<="7";)s();return}for(;F(u());)s();if(u()===".")for(s();F(u());)s();if(u()==="e"||u()==="E")for(s(),(u()==="+"||u()==="-")&&s(),F(u())||a("Invalid exponent");F(u());)s();},g=()=>{let l=p;for(;p<f.length;){let m=u();if(m==="`"){let N=f.slice(l,p);return s(),{raw:N,interp:false}}if(m==="$"&&u(1)==="{"){let N=f.slice(l,p);return s(),s(),{raw:N,interp:true}}if(m==="\\"){if(s(),p>=f.length)break;s();continue}s();}return a("Unterminated template literal")},S=0,v=0,d=[];for(;;){y(),S=p;let l=b();if(p>=f.length){h("EOF","",S,l);break}let m=u();if(_(m)){for(;be(u());)s();let k=f.slice(S,p);h(G.has(k)?"Keyword":"Identifier",k,S,l);continue}if(F(m)||m==="."&&F(u(1))){L(),h("Number",f.slice(S,p),S,l);continue}if(m==='"'||m==="'"){let k=c(m);h("String",k,S,l);continue}if(m==="`"){s();let{raw:k,interp:O}=g();O?(h("TemplateHead",k,S,l),d.push(v)):h("Template",k,S,l);continue}if(m==="{"){s(),h("Punctuator","{",S,l),v++;continue}if(m==="}"){if(d.length>0&&v===d[d.length-1]){d.pop(),s();let{raw:k,interp:O}=g();h(O?"TemplateMiddle":"TemplateTail",k,S,l),O&&d.push(v);continue}s(),h("Punctuator","}",S,l),v--;continue}if(m==="?"&&u(1)==="."&&F(u(2))){s(),h("Punctuator","?",S,l);continue}let N=ve.find(k=>f.startsWith(k,p));if(N){for(let k=0;k<N.length;k++)s();h("Punctuator",N,S,l);continue}a(`Unexpected character '${m}'`);}return x}var Y=f=>{let{peek:x,next:p,isP:i,isK:r,expectP:b,fail:u,unescape:a}=f,s=()=>{let v=0;for(let d=f.pos();d<f.tokens.length;d++){let l=f.tokens[d];if(l.type==="EOF")return false;if(l.type==="Punctuator"){if(["(","[","{","<"].includes(l.value))v++;else if([")","]","}",">"].includes(l.value)&&(v--,v===0)){let m=f.tokens[d+1];return !!m&&m.type==="Punctuator"&&m.value==="=>"}}}return false},w=()=>{b("<");let v=[];for(;!i(">");){let d=x();d.type!=="Identifier"&&u("Expected type parameter name"),p(),v.push(d.value),i(">")||b(",");}return b(">"),v},y=v=>{b("<");let d=[];for(;!i(">");)d.push(h(v)),i(">")||b(",");return b(">"),d},h=v=>{let d=c(v);if(!i("|"))return d;let l=[d];for(;i("|");)p(),l.push(c(v));return {t:"union",of:l}},c=v=>{let d=L(v);for(;i("[")&&x(1).type==="Punctuator"&&x(1).value==="]";)p(),p(),d={t:"array",items:d};return d},L=v=>{if(i("<")){let l=w(),m=new Set(v);return l.forEach(N=>m.add(N)),g(m)}if(i("(")){if(s())return g(v);p();let l=h(v);return b(")"),l}if(i("{"))return S(v);let d=x();if(d.type==="String")return p(),{t:"literal",value:a(d.value.slice(1,-1))};if(d.type==="Number")return p(),{t:"literal",value:Number(d.value)};if(r("true"))return p(),{t:"literal",value:true};if(r("false"))return p(),{t:"literal",value:false};if(r("null"))return p(),{t:"null"};if(d.type==="Identifier"){switch(p(),d.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"}}let l=i("<")?y(v):[];return v.has(d.value)&&l.length===0?{t:"var",name:d.value}:{t:"apply",name:d.value,args:l}}return u(`Expected a type but got '${d.value||d.type}'`)},g=v=>{b("(");let d=[];for(;!i(")");){let m=x();m.type!=="Identifier"&&u("Expected parameter name in function type"),p();let N=i("?");N&&p(),b(":");let k=h(v);d.push({name:m.value,type:k,...N?{optional:true}:{}}),i(")")||b(",");}b(")"),b("=>");let l=h(v);return {t:"fn",params:d,returns:l}},S=v=>{b("{");let d={};for(;!i("}");){let l=x(),m;if(l.type==="Identifier"||l.type==="Keyword")p(),m=l.value;else if(l.type==="String")p(),m=a(l.value.slice(1,-1));else return u("Expected property name in object type");let N=i("?");N&&p(),b(":");let k=h(v);if(d[m]={type:k,...N?{optional:true}:{}},i(";")||i(","))p();else break}return b("}"),{t:"object",props:d}};return {parseType:h,parseTypeParamList:w}};var M=class extends core.TSLiteError{constructor(p,i,r){super("parse-error",`${p} (${i.line}:${i.column})`,{position:i,index:r});this.position=i;this.index=r;this.name="ParseError";}},xe={"??":1,"||":2,"&&":3,"==":4,"===":4,"!=":4,"!==":4,"<":5,"<=":5,">":5,">=":5,"+":6,"-":6,"*":7,"/":7,"%":7},ke=new Set(["??","||","&&"]),Te=new Set(["class","import","export","debugger","with"]),Se=new Set(["=","+=","-=","*=","/=","%="]);function X(f){let x=z(f),p=0,i=(e=0)=>x[Math.min(p+e,x.length-1)],r=()=>x[p++],b=()=>x[p-1],u=()=>i().type==="EOF",a=e=>i().type==="Punctuator"&&i().value===e,s=e=>i().type==="Keyword"&&i().value===e,w=(e,n=i())=>{throw new M(e,n.loc.start,n.start)},y=e=>(a(e)||w(`Expected '${e}' but got '${i().value||i().type}'`),r()),h=(e,n)=>{let t=b()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},c=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),L=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let o=e[++t];switch(o){case "n":n+=`
|
|
5
|
-
`;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 T=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,T),16)),t=T;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=o;}}return n},g=()=>S(),S=()=>{let e=v(),n=i();if(n.type==="Punctuator"&&Se.has(n.value)){r();let t=S();return c({type:"AssignmentExpression",operator:n.value,left:e,right:t},e,t)}return e},v=()=>{let e=d();for(;a("|>");){r();let n=d();e=c({type:"CallExpression",callee:n,arguments:[e],optional:false},e,n);}return e},d=()=>{let e=l(0);if(!a("?"))return e;r();let n=g();y(":");let t=g();return c({type:"ConditionalExpression",test:e,consequent:n,alternate:t},e,t)},l=e=>{let n=m();for(;;){let t=i();if(t.type!=="Punctuator")break;let o=xe[t.value];if(!o||o<=e)break;r();let T=l(o);n=c({type:ke.has(t.value)?"LogicalExpression":"BinaryExpression",operator:t.value,left:n,right:T},n,T);}return n},m=()=>{let e=i();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(r(),h({type:"UpdateExpression",operator:e.value,prefix:true,argument:m()},e)):s("await")?(r(),h({type:"AwaitExpression",argument:m()},e)):s("typeof")||s("void")||s("delete")?(r(),h({type:"UnaryExpression",operator:e.value,prefix:true,argument:m()},e)):e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+")?(r(),h({type:"UnaryExpression",operator:e.value,prefix:true,argument:m()},e)):N()},N=()=>{let e=k();for(;i().type==="Identifier"&&i().value==="as";){r();let n=D(new Set);e=c({type:"TSAsExpression",expression:e,typeAnnotation:n},e,b());}return e},k=()=>{let e=W(),n=false;for(;;)if(a("?."))if(r(),n=true,a("(")){let{args:t,close:o}=O();e=c({type:"CallExpression",callee:e,arguments:t,optional:true},e,o);}else if(a("[")){r();let t=g(),o=y("]");e=c({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,o);}else {let t=i();t.type!=="Identifier"&&t.type!=="Keyword"&&w("Expected property name after '?.'"),r();let o=h({type:"Identifier",name:t.value},t);e=c({type:"MemberExpression",object:e,property:o,computed:false,optional:true},e,o);}else if(a(".")){r();let t=i();t.type!=="Identifier"&&t.type!=="Keyword"&&w("Expected property name"),r();let o=h({type:"Identifier",name:t.value},t);e=c({type:"MemberExpression",object:e,property:o,computed:false,optional:false},e,o);}else if(a("[")){r();let t=g(),o=y("]");e=c({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,o);}else if(a("(")){let{args:t,close:o}=O();e=c({type:"CallExpression",callee:e,arguments:t,optional:false},e,o);}else if(a("++")||a("--")){let t=r();e=c({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?c({type:"ChainExpression",expression:e},e,e):e},O=()=>{y("(");let e=[];for(;!a(")");)e.push(V()),a(")")||y(",");return {args:e,close:y(")")}},V=()=>{if(a("...")){let e=r(),n=g();return c({type:"SpreadElement",argument:n},e,n)}return g()},j=()=>{let e=i();return e.type!=="Identifier"&&w("Expected identifier"),r(),h({type:"Identifier",name:e.value},e)},Z=()=>{let e=0;for(let n=p;n<x.length;n++){let t=x[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 o=x[n+1];return !o||o.type!=="Punctuator"?false:o.value==="=>"?true:o.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<x.length;t++){let o=x[t];if(o.type==="EOF")return false;if(o.type==="Punctuator")if(["(","[","{","<"].includes(o.value))n++;else if([")","]","}",">"].includes(o.value)){if(n===0)return false;n--;}else {if(n===0&&o.value==="=>")return true;if(n===0&&(o.value===";"||o.value===","))return false}}return false},Q=(e=new Set)=>{y("(");let n=[];for(;!a(")");){if(a("...")){let t=r(),o=j();a(":")&&(r(),o.typeAnnotation=D(e)),n.push(c({type:"RestElement",argument:o},t,o));}else {let t=j();a(":")&&(r(),t.typeAnnotation=D(e)),n.push(t);}a(")")||y(",");}return y(")"),n},R=(e,n)=>{let t=a("(")?Q():[j()],o;a(":")&&(r(),o=D(new Set)),y("=>");let T=a("{")?K():g();return h({type:"ArrowFunctionExpression",params:t,body:T,async:e,expression:T.type!=="BlockStatement",generator:false,...o?{returnType:o}:{}},n)},ee=()=>{let e=y("["),n=[];for(;!a("]");){if(a(",")){r(),n.push(null);continue}n.push(V()),a("]")||y(",");}return c({type:"ArrayExpression",elements:n},e,y("]"))},te=()=>{let e=y("{"),n=[];for(;!a("}");){if(a("...")){let t=r(),o=g();n.push(c({type:"SpreadElement",argument:o},t,o));}else {let t=i(),o,T=false;if(a("["))T=true,r(),o=g(),y("]");else if(t.type==="String")r(),o=h({type:"Literal",value:L(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")r(),o=h({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")r(),o=h({type:"Identifier",name:t.value},t);else return w("Invalid property key");if(a(":")){r();let P=g();n.push(c({type:"Property",key:o,value:P,kind:"init",method:false,shorthand:false,computed:T},t,P));}else n.push(c({type:"Property",key:o,value:o,kind:"init",method:false,shorthand:true,computed:false},t,o));}a("}")||y(",");}return c({type:"ObjectExpression",properties:n},e,y("}"))},U=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:L(e)},tail:n}),ne=e=>{r();let n=[U(e.value,false)],t=[];for(;;){t.push(g());let o=i();if(o.type==="TemplateMiddle")r(),n.push(U(o.value,false));else if(o.type==="TemplateTail"){r(),n.push(U(o.value,true));break}else w("Expected `}` to close template interpolation");}return h({type:"TemplateLiteral",quasis:n,expressions:t},e)},W=()=>{let e=i();if(s("async")){let n=i(1);return n.value==="("||n.type==="Identifier"?(r(),R(true,e)):w("Unexpected 'async'")}if(a("(")&&Z()||e.type==="Identifier"&&i(1).type==="Punctuator"&&i(1).value==="=>")return R(false,e);if(e.type==="Number")return r(),h({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return r(),h({type:"Literal",value:L(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return r(),h({type:"TemplateLiteral",expressions:[],quasis:[U(e.value,true)]},e);if(e.type==="TemplateHead")return ne(e);if(e.type==="Keyword")return e.value==="true"?(r(),h({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(r(),h({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(r(),h({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?re(e):e.value==="function"?w("Anonymous functions are not supported \u2014 use an arrow function"):w(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return r(),h({type:"Identifier",name:e.value},e);if(a("(")){r();let n=g();return y(")"),n}return a("[")?ee():a("{")?te():w(`Unexpected token '${e.value||e.type}'`)},re=e=>{r();let n=W();for(;;)if(a(".")){r();let t=i();t.type!=="Identifier"&&t.type!=="Keyword"&&w("Expected property name"),r();let o=h({type:"Identifier",name:t.value},t);n=c({type:"MemberExpression",object:n,property:o,computed:false,optional:false},n,o);}else if(a("[")){r();let t=g(),o=y("]");n=c({type:"MemberExpression",object:n,property:t,computed:true,optional:false},n,o);}else break;if(a("(")){let{args:t,close:o}=O();return c({type:"NewExpression",callee:n,arguments:t},e,o)}return c({type:"NewExpression",callee:n,arguments:[]},e,n)},{parseType:D,parseTypeParamList:oe}=Y({tokens:x,pos:()=>p,peek:i,next:r,isP:a,isK:s,expectP:y,fail:w,unescape:L}),se=()=>{let e=r(),n=j(),t=new Set,o=[];a("<")&&(o=oe(),o.forEach(E=>t.add(E))),y("=");let T=D(t),P=c({type:"TSTypeAliasDeclaration",id:n,typeParameters:o,typeAnnotation:T},e,b());return a(";")&&r(),P},C=()=>{let e=i();if(e.type==="Identifier"&&e.value==="type"&&i(1).type==="Identifier"&&i(2).type==="Punctuator"&&(i(2).value==="="||i(2).value==="<"))return se();if(s("const")||s("let")||s("var")){let o=ae();return a(";")&&r(),o}if(s("if"))return ce();if(s("for"))return ie();if(s("while"))return pe();if(s("do"))return le();if(s("break")||s("continue"))return ue();if(s("return"))return fe();if(s("throw"))return de();if(s("try"))return ye();if(s("switch"))return me();if(a("{"))return K();if((s("function")||s("async")&&i(1).type==="Keyword"&&i(1).value==="function")&&w("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return r(),h({type:"EmptyStatement"},e);e.type==="Keyword"&&Te.has(e.value)&&w(`'${e.value}' is not supported in this language`);let n=g(),t=a(";")?r():b();return c({type:"ExpressionStatement",expression:n},n,t)},ae=()=>{let e=r(),n=e.value,t=j();a(":")&&(r(),t.typeAnnotation=D(new Set));let o=null;a("=")?(r(),o=g()):n==="const"&&w("'const' declarations must be initialized");let T=c({type:"VariableDeclarator",id:t,init:o},t,o??t);return c({type:"VariableDeclaration",kind:n,declarations:[T]},e,o??t)},q=()=>i().type==="Identifier"&&i().value==="of",B=()=>s("in"),ie=()=>{let e=r();y("(");let n=null,t=null;if(!a(";"))if(s("const")||s("let")||s("var")){let E=r(),I=j();if(a(":")&&(r(),I.typeAnnotation=D(new Set)),q()||B()){let A=c({type:"VariableDeclarator",id:I,init:null},I,I);t=c({type:"VariableDeclaration",kind:E.value,declarations:[A]},E,I);}else {let A=null;a("=")?(r(),A=g()):E.value==="const"&&w("'const' declarations must be initialized");let he=c({type:"VariableDeclarator",id:I,init:A},I,A??I);n=c({type:"VariableDeclaration",kind:E.value,declarations:[he]},E,A??I);}}else {let E=g();q()||B()?t=E:n=E;}if(t){let E=B();r();let I=g();y(")");let A=C();return c(E?{type:"ForInStatement",left:t,right:I,body:A}:{type:"ForOfStatement",left:t,right:I,body:A,await:false},e,A)}y(";");let o=a(";")?null:g();y(";");let T=a(")")?null:g();y(")");let P=C();return c({type:"ForStatement",init:n,test:o,update:T,body:P},e,P)},le=()=>{let e=r(),n=C();s("while")||w("Expected 'while' after the 'do' body"),r(),y("(");let t=g(),o=y(")");return a(";")&&r(),c({type:"DoWhileStatement",body:n,test:t},e,o)},ue=()=>{let e=r(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?r():b();return c({type:n,label:null},e,t)},pe=()=>{let e=r();y("(");let n=g();y(")");let t=C();return c({type:"WhileStatement",test:n,body:t},e,t)},ce=()=>{let e=r();y("(");let n=g();y(")");let t=C(),o=null;return s("else")&&(r(),o=C()),c({type:"IfStatement",test:n,consequent:t,alternate:o},e,o??t)},fe=()=>{let e=r(),n=null;!a(";")&&!a("}")&&!u()&&(n=g());let t=a(";")?r():b();return c({type:"ReturnStatement",argument:n},e,t)},de=()=>{let e=r(),n=g(),t=a(";")?r():b();return c({type:"ThrowStatement",argument:n},e,t)},ye=()=>{let e=r(),n=K(),t=null,o=null;if(s("catch")){let T=r(),P=null;a("(")&&(r(),P=j(),y(")"));let E=K();t=c({type:"CatchClause",param:P,body:E},T,E);}return s("finally")&&(r(),o=K()),!t&&!o&&w("'try' requires a 'catch' or 'finally' clause"),c({type:"TryStatement",block:n,handler:t,finalizer:o},e,o??t??n)},me=()=>{let e=r();y("(");let n=g();y(")"),y("{");let t=[];for(;!a("}")&&!u();){let T=i(),P=null;s("case")?(r(),P=g()):s("default")?r():w("Expected 'case' or 'default' in switch"),y(":");let E=[];for(;!a("}")&&!s("case")&&!s("default")&&!u();)E.push(C());t.push(c({type:"SwitchCase",test:P,consequent:E},T,b()??T));}let o=y("}");return c({type:"SwitchStatement",discriminant:n,cases:t},e,o)},K=()=>{let e=y("{"),n=[];for(;!a("}")&&!u();)n.push(C());return c({type:"BlockStatement",body:n},e,y("}"))};return {parseExpr:g,parseProgram:()=>{let e=i(),n=[];for(;!u();)n.push(C());return c({type:"Program",sourceType:"module",body:n},e,b()??e)},eof:u,fail:w}}function Ne(f){return X(f).parseProgram()}function Ee(f){let{parseExpr:x,eof:p,fail:i}=X(f),r=x();return p()||i("Unexpected token after expression"),r}exports.KEYWORDS=G;exports.LexError=$;exports.ParseError=M;exports.parse=Ne;exports.parseExpression=Ee;exports.tokenize=z;
|
|
1
|
+
'use strict';var core=require('@tslite/core');var Z=class extends core.TSLiteError{constructor(l,h,d,r,f){super("lex-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="LexError";}},ye=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"]),ze=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],se=c=>c>="a"&&c<="z"||c>="A"&&c<="Z"||c==="_"||c==="$",me=c=>se(c)||c>="0"&&c<="9",B=c=>c>="0"&&c<="9",de=c=>B(c)||c>="a"&&c<="f"||c>="A"&&c<="F",qe=c=>me(c)||c==="-",Ve=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),We=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(c){let S=[],l=0,h=1,d=0,r=()=>({line:h,column:d}),f=(v=0)=>c[l+v]??"",L=v=>{let C=r();throw new Z(v,C,l,{start:C,end:{line:h,column:d+1}},l+1)},y=()=>{let v=c[l++];return v==="\r"?(c[l]===`
|
|
2
|
+
`&&l++,h++,d=0):v===`
|
|
3
|
+
`||v==="\u2028"||v==="\u2029"?(h++,d=0):d++,v},s=v=>v===`
|
|
4
|
+
`||v==="\r"||v==="\u2028"||v==="\u2029",x=()=>{for(;;){let v=f();if(v===" "||v===" "||v==="\f"||v==="\v"||s(v))y();else if(v==="/"&&f(1)==="/")for(;l<c.length&&!s(f());)y();else if(v==="/"&&f(1)==="*")for(y(),y();;){if(l>=c.length&&L("Unterminated block comment"),f()==="*"&&f(1)==="/"){y(),y();break}y();}else break}},I=(v,C,R,N)=>{S.push({type:v,value:C,start:R,end:l,loc:{start:N,end:r()}}),$=v==="Punctuator"?Ve.has(C):v==="Keyword"?We.has(C):v==="TemplateHead"||v==="TemplateMiddle";},E=v=>{for(y();l<c.length;){let C=f();if(C===v)return y(),c.slice(a,l);if(s(C)&&L("Unterminated string literal"),C==="\\"){y(),l>=c.length&&L("Unterminated string literal"),y();continue}y();}return L("Unterminated string literal")},m=()=>{if(f()==="0"&&(f(1)==="x"||f(1)==="X")){for(y(),y(),de(f())||L("Invalid hexadecimal literal");de(f());)y();return}if(f()==="0"&&(f(1)==="b"||f(1)==="B")){for(y(),y(),f()!=="0"&&f()!=="1"&&L("Invalid binary literal");f()==="0"||f()==="1";)y();return}if(f()==="0"&&(f(1)==="o"||f(1)==="O")){for(y(),y(),f()>="0"&&f()<="7"||L("Invalid octal literal");f()>="0"&&f()<="7";)y();return}for(;B(f());)y();if(f()===".")for(y();B(f());)y();if(f()==="e"||f()==="E")for(y(),(f()==="+"||f()==="-")&&y(),B(f())||L("Invalid exponent");B(f());)y();},T=()=>{let v=l;for(;l<c.length;){let C=f();if(C==="`"){let R=c.slice(v,l);return y(),{raw:R,interp:false}}if(C==="$"&&f(1)==="{"){let R=c.slice(v,l);return y(),y(),{raw:R,interp:true}}if(C==="\\"){if(y(),l>=c.length)break;y();continue}y();}return L("Unterminated template literal")},a=0,J=0,k=[],$=true,X="js",K=[],F=[];for(;;){if(X==="jsxChildren"){a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(i==="<"){f(1)==="/"?(y(),y(),I("Punctuator","</",a,N),X="jsxCloseTag"):(y(),I("Punctuator","<",a,N),K.push("jsxChildren"),X="jsxOpenTag");continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:"jsxChildren"}),X="js";continue}for(;l<c.length&&f()!=="<"&&f()!=="{";)y();I("JSXText",c.slice(a,l),a,N);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){x(),a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(se(i)){for(y();qe(f());)y();I("Identifier",c.slice(a,l),a,N);continue}if(i==="/"&&f(1)===">"){y(),y(),I("Punctuator","/>",a,N),X=K.pop()??"js";continue}if(i===">"){y(),I("Punctuator",">",a,N),X=X==="jsxCloseTag"?K.pop()??"js":"jsxChildren";continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:X}),X="js";continue}if(i==='"'||i==="'"){I("String",E(i),a,N);continue}if(i==="="||i==="."||i===":"){y(),I("Punctuator",i,a,N);continue}L(`Unexpected character '${i}' in JSX tag`);}x(),a=l;let v=r();if(l>=c.length){I("EOF","",a,v);break}let C=f();if(se(C)){for(;me(f());)y();let N=c.slice(a,l);I(ye.has(N)?"Keyword":"Identifier",N,a,v);continue}if(B(C)||C==="."&&B(f(1))){m(),I("Number",c.slice(a,l),a,v);continue}if(C==='"'||C==="'"){let N=E(C);I("String",N,a,v);continue}if(C==="`"){y();let{raw:N,interp:i}=T();i?(I("TemplateHead",N,a,v),k.push(J)):I("Template",N,a,v);continue}if(C==="<"&&$){y(),I("Punctuator","<",a,v),K.push("js"),X="jsxOpenTag";continue}if(C==="{"){y(),I("Punctuator","{",a,v),J++;continue}if(C==="}"){if(k.length>0&&J===k[k.length-1]){k.pop(),y();let{raw:N,interp:i}=T();I(i?"TemplateMiddle":"TemplateTail",N,a,v),i&&k.push(J);continue}if(F.length>0&&J===F[F.length-1].level){let{restore:N}=F.pop();y(),I("Punctuator","}",a,v),X=N;continue}y(),I("Punctuator","}",a,v),J--;continue}if(C==="?"&&f(1)==="."&&B(f(2))){y(),I("Punctuator","?",a,v);continue}let R=ze.find(N=>c.startsWith(N,l));if(R){for(let N=0;N<R.length;N++)y();I("Punctuator",R,a,v);continue}L(`Unexpected character '${C}'`);}return S}var ie=(c=[])=>({vars:new Set(c),bounds:new Map,noCond:false,inferSink:null}),H=(c,S)=>{let l=new Set(c.vars),h=new Map(c.bounds);for(let d of S)l.add(d.name),d.bound||d.isConst?h.set(d.name,{bound:d.bound,isConst:d.isConst}):h.delete(d.name);return {vars:l,bounds:h,noCond:c.noCond,inferSink:c.inferSink}},He=(c,S)=>{let l=c.bounds.get(S);return l&&(l.bound||l.isConst)?{t:"var",name:S,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:S}},he=c=>{let{peek:S,next:l,isP:h,isK:d,expectP:r,fail:f,unescape:L}=c,y=()=>{let i=c.pos(),p=c.tokens[i],u=c.tokens[i-1];return !!u&&!!p&&p.loc.start.line>u.loc.end.line},s=()=>{let i=0;for(let p=c.pos();p<c.tokens.length;p++){let u=c.tokens[p];if(u.type==="EOF")return false;if(u.type==="Punctuator"){if(["(","[","{","<"].includes(u.value))i++;else if([")","]","}",">"].includes(u.value)&&(i--,i===0)){let b=c.tokens[p+1];return !!b&&b.type==="Punctuator"&&b.value==="=>"}}}return false},x=()=>{let i=c.tokens,p=[],u=0,b=false;for(let w=c.pos();w<i.length;w++){let j=i[w];if(j.type==="EOF")break;if(j.type==="Punctuator"){if(j.value==="<")u++,u===1&&(b=true);else if(j.value===">"){if(u--,u===0)break}else j.value==="("||j.value==="["||j.value==="{"?(u++,b=false):j.value===")"||j.value==="]"||j.value==="}"?(u--,b=false):j.value===","&&u===1?b=true:b=false;continue}if(u===1&&b){if(j.type==="Keyword"&&j.value==="const")continue;if(j.type==="Identifier"){p.push(j.value),b=false;continue}}b=false;}return p},I=i=>{let p=H(i,x().map(b=>({name:b})));r("<");let u=[];for(;!h(">");){let b=d("const");b&&l();let w=S();w.type!=="Identifier"&&f("Expected type parameter name"),l();let j;d("extends")&&(l(),j=T(p)),h("=")&&(l(),T(p)),u.push({name:w.value,...j?{bound:j}:{},...b?{isConst:true}:{}}),h(">")||r(",");}return r(">"),u},E=i=>{r("<");let p=[];for(;!h(">");)p.push(T(i)),h(">")||r(",");return r(">"),p},m=i=>{let p=S();return p.type==="Identifier"&&p.value===i},T=i=>{let p=a(i);if(i.noCond||!d("extends")||y())return p;l();let u=[],b=a({...i,noCond:true,inferSink:u});r("?");let w=H(i,u.map(z=>({name:z}))),j=T(w);r(":");let _=T(w);return {t:"conditional",check:p,extends:b,then:j,else:_,...u.length?{infer:u}:{}}},a=i=>{h("|")&&l();let p=J(i);if(!h("|"))return p;let u=[p];for(;h("|");)l(),u.push(J(i));return {t:"union",of:u}},J=i=>{h("&")&&l();let p=k(i);if(!h("&"))return p;let u=[p];for(;h("&");)l(),u.push(k(i));return {t:"intersection",of:u}},k=i=>{if(m("keyof"))return l(),{t:"keyof",of:k(i)};if(m("readonly")){l();let p=k(i);return p.t!=="array"&&p.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{...p,readonly:true}}if(m("infer")){l();let p=S();p.type!=="Identifier"&&f("Expected 'infer' variable name"),l();let u=i.inferSink;return u?(u.push(p.value),{t:"var",name:p.value}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return $(i)},$=i=>{let p=X(i);for(;h("[")&&!y();)if(l(),h("]"))l(),p={t:"array",items:p};else {let u=T(i);r("]"),p={t:"index",obj:p,key:u};}return p},X=i=>{if(d("typeof")){l();let u=S();u.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),l();let b=[u.value];for(;h(".");){l();let w=S();w.type!=="Identifier"&&w.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),l(),b.push(w.value);}return {t:"typeQuery",path:b}}if(h("<")){let u=I(i),b=H(i,u);return F(b,u.map(w=>w.name))}if(h("(")){if(s())return F(i);l();let u=T(i.noCond?{...i,noCond:false}:i);return r(")"),u}if(h("{"))return C()?R(i):N(i);if(h("["))return v(i);let p=S();if(p.type==="String")return l(),{t:"literal",value:L(p.value.slice(1,-1))};if(p.type==="Number")return l(),{t:"literal",value:Number(p.value)};if(d("true"))return l(),{t:"literal",value:true};if(d("false"))return l(),{t:"literal",value:false};if(d("null"))return l(),{t:"null"};if(d("void"))return l(),{t:"void"};if(p.type==="Identifier"){switch(l(),p.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"};case "never":return {t:"union",of:[]}}let u=h("<")?E(i):[];return i.vars.has(p.value)&&u.length===0?He(i,p.value):p.value==="ReturnType"&&u.length===1?{t:"returnType",fn:u[0]}:p.value==="Parameters"&&u.length===1?{t:"parameters",fn:u[0]}:(c.typeRefs?.push({name:p.value,loc:p.loc}),{t:"apply",name:p.value,args:u})}return f(`Expected a type but got '${p.value||p.type}'`)},K=i=>{r("(");let p=[];for(;!h(")");){let u=S();u.type!=="Identifier"&&f("Expected parameter name in function type"),l();let b=h("?");b&&l(),r(":");let w=T(i);p.push({name:u.value,type:w,...b?{optional:true}:{}}),h(")")||r(",");}return r(")"),p},F=(i,p)=>{let u=K(i);r("=>");let b=T(i);return {t:"fn",params:u,returns:b,...p?.length?{typeParams:p}:{}}},v=i=>{r("[");let p=[],u=[];for(;!h("]");){if(h("...")){l();let b=T(i);b.t==="array"?(p.push(b.items),u.push("rest")):(p.push(b),u.push("variadic"));}else {let b=T(i),w=h("?");w&&l(),p.push(b),u.push(w?"optional":"req");}h("]")||r(",");}return r("]"),u.some(b=>b!=="req")?{t:"tuple",elements:p,flags:u}:{t:"tuple",elements:p}},C=()=>{let i=c.tokens,p=c.pos()+1;return i[p]?.type==="Punctuator"&&(i[p]?.value==="+"||i[p]?.value==="-")&&p++,i[p]?.type==="Identifier"&&i[p]?.value==="readonly"&&p++,i[p]?.type==="Punctuator"&&i[p]?.value==="["&&i[p+1]?.type==="Identifier"&&i[p+2]?.type==="Keyword"&&i[p+2]?.value==="in"},R=i=>{r("{"),(h("+")||h("-"))&&l(),m("readonly")&&l(),r("[");let p=S();p.type!=="Identifier"&&f("Expected mapped type key name"),l(),d("in")||f("Expected 'in' in mapped type"),l();let u=T(i);m("as")&&f("Key remapping ('as') in mapped types is not supported"),r("]");let b=false,w=h("+")||h("-")?l().value:null;h("?")&&(l(),b=w!=="-"),r(":");let j=T(H(i,[{name:p.value}]));return (h(";")||h(","))&&l(),r("}"),{t:"mapped",key:p.value,over:u,body:j,optional:b}},N=i=>{r("{");let p={},u,b=[];for(;!h("}");){if(h("(")){let w=K(i);r(":");let j=T(i);b.push({t:"fn",params:w,returns:j});}else if(h("[")){l(),S().type!=="Identifier"&&f("Expected index signature parameter name"),l(),r(":");let w=S();(w.type!=="Identifier"||w.value!=="string"&&w.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),l(),r("]"),r(":"),u={key:w.value,value:T(i)};}else {let w=S(),j;if(w.type==="Identifier"||w.type==="Keyword")l(),j=w.value;else if(w.type==="String")l(),j=L(w.value.slice(1,-1));else if(w.type==="Number")l(),j=w.value;else return f("Expected property name in object type");let _=h("?");_&&l(),r(":");let z=T(i);p[j]={type:z,..._?{optional:true}:{}};}if(h(";")||h(","))l();else break}return r("}"),b.length>0?Object.keys(p).length>0||u?f("Callable object types (call signatures mixed with members) are not supported"):b.length===1?b[0]:{t:"overload",signatures:b}:{t:"object",props:p,...u?{index:u}:{}}};return {parseType:i=>T(ie(i)),parseTypeParamList:()=>I(ie()),parseTypeInScope:i=>T(H(ie(),i))}};var ee=class extends core.TSLiteError{constructor(l,h,d,r,f){super("parse-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="ParseError";}},Ye={"??":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},Qe=new Set(["??","||","&&"]),Ze=new Set(["class","debugger","with","enum","interface"]),et={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"},tt={namespace:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",module:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",declare:c=>c.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(c.value)||c.type==="Identifier"&&["type","namespace","module"].includes(c.value),abstract:c=>c.type==="Keyword"&&c.value==="class"},nt=new Set(["=","+=","-=","*=","/=","%="]);function ge(c){let S=ae(c),l=0,h=[],d=(e=0)=>S[Math.min(l+e,S.length-1)],r=()=>S[l++],f=()=>S[l-1],L=()=>{let e=S[l],t=S[l-1];return !!e&&!!t&&e.loc.start.line>t.loc.end.line},y=()=>d().type==="EOF",s=e=>d().type==="Punctuator"&&d().value===e,x=e=>d().type==="Keyword"&&d().value===e,I=e=>d().type==="Identifier"&&d().value===e,E=(e,t=d())=>{throw new ee(e,t.loc.start,t.start,t.loc,t.end)},m=e=>(s(e)||E(`Expected '${e}' but got '${d().value||d().type}'`),r()),T=(e,t)=>{let n=f()??t;return Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}})},a=(e,t,n)=>Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}}),J=e=>{let t="";for(let n=0;n<e.length;n++){if(e[n]!=="\\"){t+=e[n];continue}let o=e[++n];switch(o){case "n":t+=`
|
|
5
|
+
`;break;case "t":t+=" ";break;case "r":t+="\r";break;case "b":t+="\b";break;case "f":t+="\f";break;case "v":t+="\v";break;case "0":t+="\0";break;case "x":t+=String.fromCharCode(parseInt(e.slice(n+1,n+3),16)),n+=2;break;case "u":if(e[n+1]==="{"){let g=e.indexOf("}",n);t+=String.fromCodePoint(parseInt(e.slice(n+2,g),16)),n=g;}else t+=String.fromCharCode(parseInt(e.slice(n+1,n+5),16)),n+=4;break;default:t+=o;}}return t},k=(e=false)=>$(e),$=(e=false)=>{let t=X(e),n=d();if(n.type==="Punctuator"&&nt.has(n.value)){r();let o=$();return a({type:"AssignmentExpression",operator:n.value,left:t,right:o},t,o)}return t},X=(e=false)=>{let t=F(0,e);if(!s("?"))return t;r();let n=k();m(":");let o=k();return a({type:"ConditionalExpression",test:t,consequent:n,alternate:o},t,o)},K=()=>{let e=d();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 t=d(1);if(t.type==="Punctuator"&&t.value===">"&&e.end===t.start){let n=d(2);return n.type==="Punctuator"&&n.value===">"&&t.end===n.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},F=(e,t=false)=>{let n=v();for(;;){let o=K();if(!o)break;let{op:g,consume:P}=o;if(g==="in"&&t)break;let A=Ye[g];if(!A||A<=e)break;if(g==="satisfies"){if(L())break;E("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(g==="as"){if(L())break;r();let D=V(new Set);n=a({type:"TSAsExpression",expression:n,typeAnnotation:D},n,f());continue}for(let D=0;D<P;D++)r();let O=F(g==="**"?A-1:A,t);n=a({type:Qe.has(g)?"LogicalExpression":"BinaryExpression",operator:g,left:n,right:O},n,O);}return n},v=()=>{let e=d();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(r(),T({type:"UpdateExpression",operator:e.value,prefix:true,argument:v()},e)):x("await")?(r(),T({type:"AwaitExpression",argument:v()},e)):x("typeof")||x("void")||x("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?C(e):N()},C=e=>{r();let t=v();return s("**")&&E(`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.`),T({type:"UnaryExpression",operator:e.value,prefix:true,argument:t},e)},R=()=>{let e=l,t=()=>(l=e,void 0);try{if(r(),s(">"))return t();let n=[];for(;n.push(V(new Set)),!!s(",");)r();return s(">")?(r(),s("(")?n:t()):t()}catch{return t()}},N=()=>{let e=we(),t=false;for(;;)if(s("?."))if(r(),t=true,s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:true},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:true},e,o);}else {let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name after '?.'"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:true},e,o);}else if(s(".")){r();let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:false},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:false},e,o);}else if(s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:false},e,o);}else if(s("<")){let n=R();if(!n)break;let{args:o,close:g}=i();e=a({type:"CallExpression",callee:e,arguments:o,optional:false,typeArguments:n},e,g);}else if(s("++")||s("--")){let n=r();e=a({type:"UpdateExpression",operator:n.value,prefix:false,argument:e},e,n);}else break;return t?a({type:"ChainExpression",expression:e},e,e):e},i=()=>{m("(");let e=[];for(;!s(")");)e.push(p()),s(")")||m(",");return {args:e,close:m(")")}},p=()=>{if(s("...")){let e=r(),t=k();return a({type:"SpreadElement",argument:t},e,t)}return k()},u=()=>{let e=d();return e.type!=="Identifier"&&E("Expected identifier"),r(),T({type:"Identifier",name:e.value},e)},b=e=>{let t=s("{")?w(e):s("[")?j(e):u();return s(":")&&(r(),t.typeAnnotation=V(e)),t},w=e=>{let t=m("{"),n=[];for(;!s("}");){if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=u(),P,A=false;if(s(":")?(r(),P=b(e)):(P={...g},A=true),s("=")){r();let O=k();P=a({type:"AssignmentPattern",left:P,right:O},P,O);}n.push(a({type:"Property",key:g,value:P,kind:"init",shorthand:A,computed:false,method:false},g,P));}s("}")||m(",");}let o=m("}");return a({type:"ObjectPattern",properties:n},t,o)},j=e=>{let t=m("["),n=[];for(;!s("]");){if(s(",")){n.push(null),r();continue}if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=b(e);if(s("=")){r();let P=k();g=a({type:"AssignmentPattern",left:g,right:P},g,P);}n.push(g);}s("]")||m(",");}let o=m("]");return a({type:"ArrayPattern",elements:n},t,o)},_=()=>{let e=0;for(let t=l;t<S.length;t++){let n=S[t];if(n.type==="EOF")return false;if(n.type==="Punctuator"){if(n.value==="("||n.value==="["||n.value==="{")e++;else if((n.value===")"||n.value==="]"||n.value==="}")&&(e--,e===0)){let o=S[t+1];return !o||o.type!=="Punctuator"?false:o.value==="=>"?true:o.value===":"?z(t+2):false}}}return false},z=e=>{let t=0;for(let n=e;n<S.length;n++){let o=S[n];if(o.type==="EOF")return false;if(o.type==="Punctuator")if(["(","[","{","<"].includes(o.value))t++;else if([")","]","}",">"].includes(o.value)){if(t===0)return false;t--;}else {if(t===0&&o.value==="=>")return true;if(t===0&&(o.value===";"||o.value===","))return false}}return false},ve=(e=new Set)=>{m("(");let t=[];for(;!s(")");){if(s("...")){let n=r(),o=b(e);t.push(a({type:"RestElement",argument:o},n,o));}else {let n=b(e);if(s("=")){r();let o=k();n=a({type:"AssignmentPattern",left:n,right:o},n,o);}t.push(n);}s(")")||m(",");}return m(")"),t},te=(e,t)=>{let n=s("(")?ve():[u()],o;s(":")&&(r(),o=V(new Set)),m("=>");let g=s("{")?W():k();return T({type:"ArrowFunctionExpression",params:n,body:g,async:e,expression:g.type!=="BlockStatement",generator:false,...o?{returnType:o}:{}},t)},be=()=>{let e=m("["),t=[];for(;!s("]");){if(s(",")){r(),t.push(null);continue}t.push(p()),s("]")||m(",");}return a({type:"ArrayExpression",elements:t},e,m("]"))},Te=()=>{let e=m("{"),t=[];for(;!s("}");){if(s("...")){let n=r(),o=k();t.push(a({type:"SpreadElement",argument:o},n,o));}else {let n=d(),o,g=false;if(s("["))g=true,r(),o=k(),m("]");else if(n.type==="String")r(),o=T({type:"Literal",value:J(n.value.slice(1,-1)),raw:n.value},n);else if(n.type==="Number")r(),o=T({type:"Literal",value:Number(n.value),raw:n.value},n);else if(n.type==="Identifier"||n.type==="Keyword")r(),o=T({type:"Identifier",name:n.value},n);else return E("Invalid property key");if(s(":")){r();let P=k();t.push(a({type:"Property",key:o,value:P,kind:"init",method:false,shorthand:false,computed:g},n,P));}else t.push(a({type:"Property",key:o,value:o,kind:"init",method:false,shorthand:true,computed:false},n,o));}s("}")||m(",");}return a({type:"ObjectExpression",properties:t},e,m("}"))},G=(e,t)=>({type:"TemplateElement",value:{raw:e,cooked:J(e)},tail:t}),xe=e=>{r();let t=[G(e.value,false)],n=[];for(;;){n.push(k());let o=d();if(o.type==="TemplateMiddle")r(),t.push(G(o.value,false));else if(o.type==="TemplateTail"){r(),t.push(G(o.value,true));break}else E("Expected `}` to close template interpolation");}return T({type:"TemplateLiteral",quasis:t,expressions:n},e)},q=()=>{let e=d();return e.type!=="Identifier"&&E(`Expected JSX identifier but got '${e.value||e.type}'`),T({type:"JSXIdentifier",name:r().value},e)},le=()=>{let e=q();if(s(":")){r();let t=q();return a({type:"JSXNamespacedName",namespace:e,name:t},e,t)}for(;s(".");){r();let t=q();e=a({type:"JSXMemberExpression",object:e,property:t},e,t);}return e},M=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${M(e.namespace)}:${M(e.name)}`:`${M(e.object)}.${M(e.property)}`,Se=()=>{let e=d();if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(s("{")){let t=r(),n=$(),o=m("}");return a({type:"JSXExpressionContainer",expression:n},t,o)}return s("<")?ne():E(`Invalid JSX attribute value '${e.value||e.type}'`)},ke=()=>{let e=[];for(;!s(">")&&!s("/>")&&!y();){if(s("{")){let o=r();m("...");let g=$(),P=m("}");e.push(a({type:"JSXSpreadAttribute",argument:g},o,P));continue}let t=q();if(s(":")){r();let o=q();t=a({type:"JSXNamespacedName",namespace:t,name:o},t,o);}let n=null;s("=")&&(r(),n=Se()),e.push(a({type:"JSXAttribute",name:t,value:n},t,n??t));}return e},pe=()=>{let e=[];for(;!(s("</")||y());){let t=d();if(t.type==="JSXText")r(),e.push(T({type:"JSXText",value:t.value,raw:t.value},t));else if(s("{")){let n=r();if(s("}")){let o=r(),g=T({type:"JSXEmptyExpression"},n);e.push(a({type:"JSXExpressionContainer",expression:g},n,o));}else {let o=$(),g=m("}");e.push(a({type:"JSXExpressionContainer",expression:o},n,g));}}else s("<")?e.push(ne()):E(`Unexpected token in JSX children '${t.value||t.type}'`);}return e},ne=()=>{let e=m("<");if(s(">")){let Q=r(),oe=a({type:"JSXOpeningFragment"},e,Q),Ue=pe(),Me=m("</"),fe=m(">"),Be=a({type:"JSXClosingFragment"},Me,fe);return a({type:"JSXFragment",openingFragment:oe,children:Ue,closingFragment:Be},e,fe)}let t=le(),n=ke();if(s("/>")){let Q=r(),oe=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:true},e,Q);return a({type:"JSXElement",openingElement:oe,children:[],closingElement:null},e,Q)}let o=m(">"),g=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:false},e,o),P=pe(),A=m("</"),O=d(),D=le();M(D)!==M(t)&&E(`JSX closing tag </${M(D)}> does not match opening <${M(t)}>`,O);let Y=m(">"),Ke=a({type:"JSXClosingElement",name:D},A,Y);return a({type:"JSXElement",openingElement:g,children:P,closingElement:Ke},e,Y)},we=()=>{let e=d();if(x("async")){let t=d(1);return t.value==="("||t.type==="Identifier"?(r(),te(true,e)):E("Unexpected 'async'")}if(s("(")&&_()||e.type==="Identifier"&&d(1).type==="Punctuator"&&d(1).value==="=>")return te(false,e);if(e.type==="Number")return r(),T({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return r(),T({type:"TemplateLiteral",expressions:[],quasis:[G(e.value,true)]},e);if(e.type==="TemplateHead")return xe(e);if(e.type==="Keyword")return e.value==="true"?(r(),T({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(r(),T({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(r(),T({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?E("'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"?E("Anonymous functions are not supported \u2014 use an arrow function"):E(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return r(),T({type:"Identifier",name:e.value},e);if(s("(")){r();let t=k();return m(")"),t}return s("[")?be():s("{")?Te():s("<")?ne():E(`Unexpected token '${e.value||e.type}'`)},{parseType:V,parseTypeParamList:Ne,parseTypeInScope:Pe}=he({tokens:S,pos:()=>l,peek:d,next:r,isP:s,isK:x,expectP:m,fail:E,unescape:J,typeRefs:h}),Ee=()=>{let e=r(),t=u(),n=[],o=[];s("<")&&(o=Ne(),n=o.map(A=>A.name)),m("=");let g=Pe(o),P=a({type:"TSTypeAliasDeclaration",id:t,typeParameters:n,typeAnnotation:g},e,f());return s(";")&&r(),P},U=()=>{let e=d();if(e.type==="Identifier"&&e.value==="type"&&d(1).type==="Identifier"&&d(2).type==="Punctuator"&&(d(2).value==="="||d(2).value==="<"))return Ee();if(x("const")||x("let")||x("var")){let g=ce();return s(";")&&r(),g}if(x("if"))return Le();if(x("for"))return Ae();if(x("while"))return Je();if(x("do"))return Oe();if(x("break")||x("continue"))return De();if(x("return"))return Xe();if(x("throw"))return Re();if(x("try"))return Fe();if(x("switch"))return $e();if(x("import"))return Ce();if(x("export"))return je();if(s("{"))return W();if((x("function")||x("async")&&d(1).type==="Keyword"&&d(1).value==="function")&&E("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),s(";"))return r(),T({type:"EmptyStatement"},e);if(e.type==="Keyword"&&Ze.has(e.value)||e.type==="Identifier"&&tt[e.value]?.(d(1),d(2))===true){let g=et[e.value];E(`'${e.value}' is not supported in this language`+(g?` \u2014 ${g}`:""));}let n=k(),o=s(";")?r():f();return a({type:"ExpressionStatement",expression:n},n,o)},ce=()=>{let e=r(),t=e.value,n=b(new Set),o=null;s("=")?(r(),o=k()):t==="const"&&E("'const' declarations must be initialized");let g=a({type:"VariableDeclarator",id:n,init:o},n,o??n);return a({type:"VariableDeclaration",kind:t,declarations:[g]},e,o??n)},Ie=()=>{let e=d();return e.type!=="String"&&E("Expected module specifier string"),r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e)},Ce=()=>{let e=r(),t=[];if(d().type!=="String"){if(d().type==="Identifier"){let o=u();t.push(a({type:"ImportDefaultSpecifier",local:o},o,o)),s(",")&&r();}if(s("*")){let o=r();I("as")||E("Expected 'as' after '*' in import"),r();let g=u();t.push(a({type:"ImportNamespaceSpecifier",local:g},o,g));}else if(s("{")){for(r();!s("}")&&!y();){let o=u(),g=o;if(I("as")&&(r(),g=u()),t.push(a({type:"ImportSpecifier",imported:o,local:g},o,g)),s(","))r();else break}m("}");}I("from")||E("Expected 'from' in import declaration"),r();}let n=Ie();return s(";")&&r(),T({type:"ImportDeclaration",specifiers:t,source:n},e)},je=()=>{let e=r();if(x("default")){r();let t=k();return s(";")&&r(),T({type:"ExportDefaultDeclaration",declaration:t},e)}if(x("const")||x("let")||x("var")){let t=ce();return s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:t,specifiers:[],source:null},e)}if(s("{")){r();let t=[];for(;!s("}")&&!y();){let n=u(),o=n;if(I("as")&&(r(),o=u()),t.push(a({type:"ExportSpecifier",local:n,exported:o},n,o)),s(","))r();else break}return m("}"),s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:null,specifiers:t,source:null},e)}return E("Expected 'const', '{', or 'default' after 'export'")},ue=()=>d().type==="Identifier"&&d().value==="of",re=()=>x("in"),Ae=()=>{let e=r();m("(");let t=null,n=null;if(!s(";"))if(x("const")||x("let")||x("var")){let A=r(),O=u();if(s(":")&&(r(),O.typeAnnotation=V(new Set)),ue()||re()){let D=a({type:"VariableDeclarator",id:O,init:null},O,O);n=a({type:"VariableDeclaration",kind:A.value,declarations:[D]},A,O);}else {let D=null;s("=")?(r(),D=k(true)):A.value==="const"&&E("'const' declarations must be initialized");let Y=a({type:"VariableDeclarator",id:O,init:D},O,D??O);t=a({type:"VariableDeclaration",kind:A.value,declarations:[Y]},A,D??O);}}else {let A=k(true);ue()||re()?n=A:t=A;}if(n){let A=re();r();let O=k();m(")");let D=U();return a(A?{type:"ForInStatement",left:n,right:O,body:D}:{type:"ForOfStatement",left:n,right:O,body:D,await:false},e,D)}m(";");let o=s(";")?null:k();m(";");let g=s(")")?null:k();m(")");let P=U();return a({type:"ForStatement",init:t,test:o,update:g,body:P},e,P)},Oe=()=>{let e=r(),t=U();x("while")||E("Expected 'while' after the 'do' body"),r(),m("(");let n=k(),o=m(")");return s(";")&&r(),a({type:"DoWhileStatement",body:t,test:n},e,o)},De=()=>{let e=r(),t=e.value==="break"?"BreakStatement":"ContinueStatement",n=s(";")?r():f();return a({type:t,label:null},e,n)},Je=()=>{let e=r();m("(");let t=k();m(")");let n=U();return a({type:"WhileStatement",test:t,body:n},e,n)},Le=()=>{let e=r();m("(");let t=k();m(")");let n=U(),o=null;return x("else")&&(r(),o=U()),a({type:"IfStatement",test:t,consequent:n,alternate:o},e,o??n)},Xe=()=>{let e=r(),t=null;!s(";")&&!s("}")&&!y()&&(t=k());let n=s(";")?r():f();return a({type:"ReturnStatement",argument:t},e,n)},Re=()=>{let e=r(),t=k(),n=s(";")?r():f();return a({type:"ThrowStatement",argument:t},e,n)},Fe=()=>{let e=r(),t=W(),n=null,o=null;if(x("catch")){let g=r(),P=null;s("(")&&(r(),P=u(),m(")"));let A=W();n=a({type:"CatchClause",param:P,body:A},g,A);}return x("finally")&&(r(),o=W()),!n&&!o&&E("'try' requires a 'catch' or 'finally' clause"),a({type:"TryStatement",block:t,handler:n,finalizer:o},e,o??n??t)},$e=()=>{let e=r();m("(");let t=k();m(")"),m("{");let n=[];for(;!s("}")&&!y();){let g=d(),P=null;x("case")?(r(),P=k()):x("default")?r():E("Expected 'case' or 'default' in switch"),m(":");let A=[];for(;!s("}")&&!x("case")&&!x("default")&&!y();)A.push(U());n.push(a({type:"SwitchCase",test:P,consequent:A},g,f()??g));}let o=m("}");return a({type:"SwitchStatement",discriminant:t,cases:n},e,o)},W=()=>{let e=m("{"),t=[];for(;!s("}")&&!y();)t.push(U());return a({type:"BlockStatement",body:t},e,m("}"))};return {parseExpr:k,parseProgram:()=>{let e=d(),t=[];for(;!y();)t.push(U());return a({type:"Program",sourceType:"module",body:t,...h.length?{typeRefs:h}:{}},e,f()??e)},eof:y,fail:E}}function rt(c){return ge(c).parseProgram()}function ot(c){let{parseExpr:S,eof:l,fail:h}=ge(c),d=S();return l()||h("Unexpected token after expression"),d}exports.KEYWORDS=ye;exports.LexError=Z;exports.ParseError=ee;exports.parse=rt;exports.parseExpression=ot;exports.tokenize=ae;
|
package/dist/index.d.cts
CHANGED
|
@@ -12,7 +12,7 @@ interface SourceLocation {
|
|
|
12
12
|
readonly start: Position;
|
|
13
13
|
readonly end: Position;
|
|
14
14
|
}
|
|
15
|
-
type TokenType = "Identifier" | "Keyword" | "Number" | "String" | "Template" | "TemplateHead" | "TemplateMiddle" | "TemplateTail" | "Punctuator" | "EOF";
|
|
15
|
+
type TokenType = "Identifier" | "Keyword" | "Number" | "String" | "Template" | "TemplateHead" | "TemplateMiddle" | "TemplateTail" | "Punctuator" | "JSXText" | "EOF";
|
|
16
16
|
interface Token {
|
|
17
17
|
readonly type: TokenType;
|
|
18
18
|
/** O lexema cru, como aparece na fonte (sem decodificar escapes). */
|
|
@@ -26,7 +26,21 @@ interface Token {
|
|
|
26
26
|
declare class LexError extends TSLiteError<"lex-error"> {
|
|
27
27
|
readonly position: Position;
|
|
28
28
|
readonly index: number;
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
|
|
31
|
+
* vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
|
|
32
|
+
*/
|
|
33
|
+
readonly loc?: SourceLocation | undefined;
|
|
34
|
+
/** Offset absoluto do fim (exclusivo). */
|
|
35
|
+
readonly end?: number | undefined;
|
|
36
|
+
constructor(message: string, position: Position, index: number,
|
|
37
|
+
/**
|
|
38
|
+
* RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
|
|
39
|
+
* vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
|
|
40
|
+
*/
|
|
41
|
+
loc?: SourceLocation | undefined,
|
|
42
|
+
/** Offset absoluto do fim (exclusivo). */
|
|
43
|
+
end?: number | undefined);
|
|
30
44
|
}
|
|
31
45
|
/** Palavras reservadas. As permitidas e as proibidas — o parser decide. */
|
|
32
46
|
declare const KEYWORDS: Set<string>;
|
|
@@ -37,7 +51,23 @@ declare function tokenize(source: string): Token[];
|
|
|
37
51
|
declare class ParseError extends TSLiteError<"parse-error"> {
|
|
38
52
|
readonly position: Position;
|
|
39
53
|
readonly index: number;
|
|
40
|
-
|
|
54
|
+
/**
|
|
55
|
+
* O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
|
|
56
|
+
* para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
|
|
57
|
+
* seguem sendo o início (compat).
|
|
58
|
+
*/
|
|
59
|
+
readonly loc?: SourceLocation | undefined;
|
|
60
|
+
/** Offset absoluto do fim (exclusivo) do token culpado. */
|
|
61
|
+
readonly end?: number | undefined;
|
|
62
|
+
constructor(message: string, position: Position, index: number,
|
|
63
|
+
/**
|
|
64
|
+
* O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
|
|
65
|
+
* para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
|
|
66
|
+
* seguem sendo o início (compat).
|
|
67
|
+
*/
|
|
68
|
+
loc?: SourceLocation | undefined,
|
|
69
|
+
/** Offset absoluto do fim (exclusivo) do token culpado. */
|
|
70
|
+
end?: number | undefined);
|
|
41
71
|
}
|
|
42
72
|
/** Faz o parse de uma fonte completa num `Program` (ESTree, com `loc`). */
|
|
43
73
|
declare function parse(source: string): types.Program;
|
|
@@ -47,4 +77,18 @@ declare function parse(source: string): types.Program;
|
|
|
47
77
|
*/
|
|
48
78
|
declare function parseExpression(source: string): types.Expression;
|
|
49
79
|
|
|
50
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Uma REFERÊNCIA a tipo nomeado, com posição — a tabela lateral que permite
|
|
82
|
+
* "ir para a definição" e hover sobre um nome de tipo.
|
|
83
|
+
*
|
|
84
|
+
* Por que lateral e não `loc` no `Schema`: o `Schema` é dado imutável e os caches
|
|
85
|
+
* do type-core são **por referência** (TC17). Pendurar posição faria dois tipos
|
|
86
|
+
* estruturalmente iguais passarem a diferir, quebrando dedup e cache em silêncio.
|
|
87
|
+
* Mesmo movimento do overlay: informação AO LADO, não dentro.
|
|
88
|
+
*/
|
|
89
|
+
type TypeRef = {
|
|
90
|
+
readonly name: string;
|
|
91
|
+
readonly loc: SourceLocation;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export { KEYWORDS, LexError, ParseError, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, parse, parseExpression, tokenize };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ interface SourceLocation {
|
|
|
12
12
|
readonly start: Position;
|
|
13
13
|
readonly end: Position;
|
|
14
14
|
}
|
|
15
|
-
type TokenType = "Identifier" | "Keyword" | "Number" | "String" | "Template" | "TemplateHead" | "TemplateMiddle" | "TemplateTail" | "Punctuator" | "EOF";
|
|
15
|
+
type TokenType = "Identifier" | "Keyword" | "Number" | "String" | "Template" | "TemplateHead" | "TemplateMiddle" | "TemplateTail" | "Punctuator" | "JSXText" | "EOF";
|
|
16
16
|
interface Token {
|
|
17
17
|
readonly type: TokenType;
|
|
18
18
|
/** O lexema cru, como aparece na fonte (sem decodificar escapes). */
|
|
@@ -26,7 +26,21 @@ interface Token {
|
|
|
26
26
|
declare class LexError extends TSLiteError<"lex-error"> {
|
|
27
27
|
readonly position: Position;
|
|
28
28
|
readonly index: number;
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
|
|
31
|
+
* vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
|
|
32
|
+
*/
|
|
33
|
+
readonly loc?: SourceLocation | undefined;
|
|
34
|
+
/** Offset absoluto do fim (exclusivo). */
|
|
35
|
+
readonly end?: number | undefined;
|
|
36
|
+
constructor(message: string, position: Position, index: number,
|
|
37
|
+
/**
|
|
38
|
+
* RANGE do caractere culpado (mínimo: 1 char) — para o editor sublinhar em
|
|
39
|
+
* vez de marcar um ponto. `position`/`index` seguem sendo o início (compat).
|
|
40
|
+
*/
|
|
41
|
+
loc?: SourceLocation | undefined,
|
|
42
|
+
/** Offset absoluto do fim (exclusivo). */
|
|
43
|
+
end?: number | undefined);
|
|
30
44
|
}
|
|
31
45
|
/** Palavras reservadas. As permitidas e as proibidas — o parser decide. */
|
|
32
46
|
declare const KEYWORDS: Set<string>;
|
|
@@ -37,7 +51,23 @@ declare function tokenize(source: string): Token[];
|
|
|
37
51
|
declare class ParseError extends TSLiteError<"parse-error"> {
|
|
38
52
|
readonly position: Position;
|
|
39
53
|
readonly index: number;
|
|
40
|
-
|
|
54
|
+
/**
|
|
55
|
+
* O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
|
|
56
|
+
* para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
|
|
57
|
+
* seguem sendo o início (compat).
|
|
58
|
+
*/
|
|
59
|
+
readonly loc?: SourceLocation | undefined;
|
|
60
|
+
/** Offset absoluto do fim (exclusivo) do token culpado. */
|
|
61
|
+
readonly end?: number | undefined;
|
|
62
|
+
constructor(message: string, position: Position, index: number,
|
|
63
|
+
/**
|
|
64
|
+
* O RANGE completo do token culpado (`start`+`end`) — o que um editor precisa
|
|
65
|
+
* para SUBLINHAR o trecho, em vez de marcar um ponto. `position`/`index`
|
|
66
|
+
* seguem sendo o início (compat).
|
|
67
|
+
*/
|
|
68
|
+
loc?: SourceLocation | undefined,
|
|
69
|
+
/** Offset absoluto do fim (exclusivo) do token culpado. */
|
|
70
|
+
end?: number | undefined);
|
|
41
71
|
}
|
|
42
72
|
/** Faz o parse de uma fonte completa num `Program` (ESTree, com `loc`). */
|
|
43
73
|
declare function parse(source: string): types.Program;
|
|
@@ -47,4 +77,18 @@ declare function parse(source: string): types.Program;
|
|
|
47
77
|
*/
|
|
48
78
|
declare function parseExpression(source: string): types.Expression;
|
|
49
79
|
|
|
50
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Uma REFERÊNCIA a tipo nomeado, com posição — a tabela lateral que permite
|
|
82
|
+
* "ir para a definição" e hover sobre um nome de tipo.
|
|
83
|
+
*
|
|
84
|
+
* Por que lateral e não `loc` no `Schema`: o `Schema` é dado imutável e os caches
|
|
85
|
+
* do type-core são **por referência** (TC17). Pendurar posição faria dois tipos
|
|
86
|
+
* estruturalmente iguais passarem a diferir, quebrando dedup e cache em silêncio.
|
|
87
|
+
* Mesmo movimento do overlay: informação AO LADO, não dentro.
|
|
88
|
+
*/
|
|
89
|
+
type TypeRef = {
|
|
90
|
+
readonly name: string;
|
|
91
|
+
readonly loc: SourceLocation;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export { KEYWORDS, LexError, ParseError, type ParserErrorCode, type Position, type SourceLocation, type Token, type TokenType, type TypeRef, parse, parseExpression, tokenize };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {TSLiteError}from'@tslite/core';var
|
|
2
|
-
`&&
|
|
3
|
-
`||
|
|
4
|
-
`||l==="\r"||l==="\u2028"||l==="\u2029",y=()=>{for(;;){let l=u();if(l===" "||l===" "||l==="\f"||l==="\v"||w(l))s();else if(l==="/"&&u(1)==="/")for(;p<f.length&&!w(u());)s();else if(l==="/"&&u(1)==="*")for(s(),s();;){if(p>=f.length&&a("Unterminated block comment"),u()==="*"&&u(1)==="/"){s(),s();break}s();}else break}},h=(l,m,N,k)=>{x.push({type:l,value:m,start:N,end:p,loc:{start:k,end:b()}});},c=l=>{for(s();p<f.length;){let m=u();if(m===l)return s(),f.slice(S,p);if(w(m)&&a("Unterminated string literal"),m==="\\"){s(),p>=f.length&&a("Unterminated string literal"),s();continue}s();}return a("Unterminated string literal")},L=()=>{if(u()==="0"&&(u(1)==="x"||u(1)==="X")){for(s(),s(),H(u())||a("Invalid hexadecimal literal");H(u());)s();return}if(u()==="0"&&(u(1)==="b"||u(1)==="B")){for(s(),s(),u()!=="0"&&u()!=="1"&&a("Invalid binary literal");u()==="0"||u()==="1";)s();return}if(u()==="0"&&(u(1)==="o"||u(1)==="O")){for(s(),s(),u()>="0"&&u()<="7"||a("Invalid octal literal");u()>="0"&&u()<="7";)s();return}for(;F(u());)s();if(u()===".")for(s();F(u());)s();if(u()==="e"||u()==="E")for(s(),(u()==="+"||u()==="-")&&s(),F(u())||a("Invalid exponent");F(u());)s();},g=()=>{let l=p;for(;p<f.length;){let m=u();if(m==="`"){let N=f.slice(l,p);return s(),{raw:N,interp:false}}if(m==="$"&&u(1)==="{"){let N=f.slice(l,p);return s(),s(),{raw:N,interp:true}}if(m==="\\"){if(s(),p>=f.length)break;s();continue}s();}return a("Unterminated template literal")},S=0,v=0,d=[];for(;;){y(),S=p;let l=b();if(p>=f.length){h("EOF","",S,l);break}let m=u();if(_(m)){for(;be(u());)s();let k=f.slice(S,p);h(G.has(k)?"Keyword":"Identifier",k,S,l);continue}if(F(m)||m==="."&&F(u(1))){L(),h("Number",f.slice(S,p),S,l);continue}if(m==='"'||m==="'"){let k=c(m);h("String",k,S,l);continue}if(m==="`"){s();let{raw:k,interp:O}=g();O?(h("TemplateHead",k,S,l),d.push(v)):h("Template",k,S,l);continue}if(m==="{"){s(),h("Punctuator","{",S,l),v++;continue}if(m==="}"){if(d.length>0&&v===d[d.length-1]){d.pop(),s();let{raw:k,interp:O}=g();h(O?"TemplateMiddle":"TemplateTail",k,S,l),O&&d.push(v);continue}s(),h("Punctuator","}",S,l),v--;continue}if(m==="?"&&u(1)==="."&&F(u(2))){s(),h("Punctuator","?",S,l);continue}let N=ve.find(k=>f.startsWith(k,p));if(N){for(let k=0;k<N.length;k++)s();h("Punctuator",N,S,l);continue}a(`Unexpected character '${m}'`);}return x}var Y=f=>{let{peek:x,next:p,isP:i,isK:r,expectP:b,fail:u,unescape:a}=f,s=()=>{let v=0;for(let d=f.pos();d<f.tokens.length;d++){let l=f.tokens[d];if(l.type==="EOF")return false;if(l.type==="Punctuator"){if(["(","[","{","<"].includes(l.value))v++;else if([")","]","}",">"].includes(l.value)&&(v--,v===0)){let m=f.tokens[d+1];return !!m&&m.type==="Punctuator"&&m.value==="=>"}}}return false},w=()=>{b("<");let v=[];for(;!i(">");){let d=x();d.type!=="Identifier"&&u("Expected type parameter name"),p(),v.push(d.value),i(">")||b(",");}return b(">"),v},y=v=>{b("<");let d=[];for(;!i(">");)d.push(h(v)),i(">")||b(",");return b(">"),d},h=v=>{let d=c(v);if(!i("|"))return d;let l=[d];for(;i("|");)p(),l.push(c(v));return {t:"union",of:l}},c=v=>{let d=L(v);for(;i("[")&&x(1).type==="Punctuator"&&x(1).value==="]";)p(),p(),d={t:"array",items:d};return d},L=v=>{if(i("<")){let l=w(),m=new Set(v);return l.forEach(N=>m.add(N)),g(m)}if(i("(")){if(s())return g(v);p();let l=h(v);return b(")"),l}if(i("{"))return S(v);let d=x();if(d.type==="String")return p(),{t:"literal",value:a(d.value.slice(1,-1))};if(d.type==="Number")return p(),{t:"literal",value:Number(d.value)};if(r("true"))return p(),{t:"literal",value:true};if(r("false"))return p(),{t:"literal",value:false};if(r("null"))return p(),{t:"null"};if(d.type==="Identifier"){switch(p(),d.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"}}let l=i("<")?y(v):[];return v.has(d.value)&&l.length===0?{t:"var",name:d.value}:{t:"apply",name:d.value,args:l}}return u(`Expected a type but got '${d.value||d.type}'`)},g=v=>{b("(");let d=[];for(;!i(")");){let m=x();m.type!=="Identifier"&&u("Expected parameter name in function type"),p();let N=i("?");N&&p(),b(":");let k=h(v);d.push({name:m.value,type:k,...N?{optional:true}:{}}),i(")")||b(",");}b(")"),b("=>");let l=h(v);return {t:"fn",params:d,returns:l}},S=v=>{b("{");let d={};for(;!i("}");){let l=x(),m;if(l.type==="Identifier"||l.type==="Keyword")p(),m=l.value;else if(l.type==="String")p(),m=a(l.value.slice(1,-1));else return u("Expected property name in object type");let N=i("?");N&&p(),b(":");let k=h(v);if(d[m]={type:k,...N?{optional:true}:{}},i(";")||i(","))p();else break}return b("}"),{t:"object",props:d}};return {parseType:h,parseTypeParamList:w}};var M=class extends TSLiteError{constructor(p,i,r){super("parse-error",`${p} (${i.line}:${i.column})`,{position:i,index:r});this.position=i;this.index=r;this.name="ParseError";}},xe={"??":1,"||":2,"&&":3,"==":4,"===":4,"!=":4,"!==":4,"<":5,"<=":5,">":5,">=":5,"+":6,"-":6,"*":7,"/":7,"%":7},ke=new Set(["??","||","&&"]),Te=new Set(["class","import","export","debugger","with"]),Se=new Set(["=","+=","-=","*=","/=","%="]);function X(f){let x=z(f),p=0,i=(e=0)=>x[Math.min(p+e,x.length-1)],r=()=>x[p++],b=()=>x[p-1],u=()=>i().type==="EOF",a=e=>i().type==="Punctuator"&&i().value===e,s=e=>i().type==="Keyword"&&i().value===e,w=(e,n=i())=>{throw new M(e,n.loc.start,n.start)},y=e=>(a(e)||w(`Expected '${e}' but got '${i().value||i().type}'`),r()),h=(e,n)=>{let t=b()??n;return Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}})},c=(e,n,t)=>Object.assign(e,{start:n.start,end:t.end,loc:{start:n.loc.start,end:t.loc.end}}),L=e=>{let n="";for(let t=0;t<e.length;t++){if(e[t]!=="\\"){n+=e[t];continue}let o=e[++t];switch(o){case "n":n+=`
|
|
5
|
-
`;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 T=e.indexOf("}",t);n+=String.fromCodePoint(parseInt(e.slice(t+2,T),16)),t=T;}else n+=String.fromCharCode(parseInt(e.slice(t+1,t+5),16)),t+=4;break;default:n+=o;}}return n},g=()=>S(),S=()=>{let e=v(),n=i();if(n.type==="Punctuator"&&Se.has(n.value)){r();let t=S();return c({type:"AssignmentExpression",operator:n.value,left:e,right:t},e,t)}return e},v=()=>{let e=d();for(;a("|>");){r();let n=d();e=c({type:"CallExpression",callee:n,arguments:[e],optional:false},e,n);}return e},d=()=>{let e=l(0);if(!a("?"))return e;r();let n=g();y(":");let t=g();return c({type:"ConditionalExpression",test:e,consequent:n,alternate:t},e,t)},l=e=>{let n=m();for(;;){let t=i();if(t.type!=="Punctuator")break;let o=xe[t.value];if(!o||o<=e)break;r();let T=l(o);n=c({type:ke.has(t.value)?"LogicalExpression":"BinaryExpression",operator:t.value,left:n,right:T},n,T);}return n},m=()=>{let e=i();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(r(),h({type:"UpdateExpression",operator:e.value,prefix:true,argument:m()},e)):s("await")?(r(),h({type:"AwaitExpression",argument:m()},e)):s("typeof")||s("void")||s("delete")?(r(),h({type:"UnaryExpression",operator:e.value,prefix:true,argument:m()},e)):e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+")?(r(),h({type:"UnaryExpression",operator:e.value,prefix:true,argument:m()},e)):N()},N=()=>{let e=k();for(;i().type==="Identifier"&&i().value==="as";){r();let n=D(new Set);e=c({type:"TSAsExpression",expression:e,typeAnnotation:n},e,b());}return e},k=()=>{let e=W(),n=false;for(;;)if(a("?."))if(r(),n=true,a("(")){let{args:t,close:o}=O();e=c({type:"CallExpression",callee:e,arguments:t,optional:true},e,o);}else if(a("[")){r();let t=g(),o=y("]");e=c({type:"MemberExpression",object:e,property:t,computed:true,optional:true},e,o);}else {let t=i();t.type!=="Identifier"&&t.type!=="Keyword"&&w("Expected property name after '?.'"),r();let o=h({type:"Identifier",name:t.value},t);e=c({type:"MemberExpression",object:e,property:o,computed:false,optional:true},e,o);}else if(a(".")){r();let t=i();t.type!=="Identifier"&&t.type!=="Keyword"&&w("Expected property name"),r();let o=h({type:"Identifier",name:t.value},t);e=c({type:"MemberExpression",object:e,property:o,computed:false,optional:false},e,o);}else if(a("[")){r();let t=g(),o=y("]");e=c({type:"MemberExpression",object:e,property:t,computed:true,optional:false},e,o);}else if(a("(")){let{args:t,close:o}=O();e=c({type:"CallExpression",callee:e,arguments:t,optional:false},e,o);}else if(a("++")||a("--")){let t=r();e=c({type:"UpdateExpression",operator:t.value,prefix:false,argument:e},e,t);}else break;return n?c({type:"ChainExpression",expression:e},e,e):e},O=()=>{y("(");let e=[];for(;!a(")");)e.push(V()),a(")")||y(",");return {args:e,close:y(")")}},V=()=>{if(a("...")){let e=r(),n=g();return c({type:"SpreadElement",argument:n},e,n)}return g()},j=()=>{let e=i();return e.type!=="Identifier"&&w("Expected identifier"),r(),h({type:"Identifier",name:e.value},e)},Z=()=>{let e=0;for(let n=p;n<x.length;n++){let t=x[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 o=x[n+1];return !o||o.type!=="Punctuator"?false:o.value==="=>"?true:o.value===":"?J(n+2):false}}}return false},J=e=>{let n=0;for(let t=e;t<x.length;t++){let o=x[t];if(o.type==="EOF")return false;if(o.type==="Punctuator")if(["(","[","{","<"].includes(o.value))n++;else if([")","]","}",">"].includes(o.value)){if(n===0)return false;n--;}else {if(n===0&&o.value==="=>")return true;if(n===0&&(o.value===";"||o.value===","))return false}}return false},Q=(e=new Set)=>{y("(");let n=[];for(;!a(")");){if(a("...")){let t=r(),o=j();a(":")&&(r(),o.typeAnnotation=D(e)),n.push(c({type:"RestElement",argument:o},t,o));}else {let t=j();a(":")&&(r(),t.typeAnnotation=D(e)),n.push(t);}a(")")||y(",");}return y(")"),n},R=(e,n)=>{let t=a("(")?Q():[j()],o;a(":")&&(r(),o=D(new Set)),y("=>");let T=a("{")?K():g();return h({type:"ArrowFunctionExpression",params:t,body:T,async:e,expression:T.type!=="BlockStatement",generator:false,...o?{returnType:o}:{}},n)},ee=()=>{let e=y("["),n=[];for(;!a("]");){if(a(",")){r(),n.push(null);continue}n.push(V()),a("]")||y(",");}return c({type:"ArrayExpression",elements:n},e,y("]"))},te=()=>{let e=y("{"),n=[];for(;!a("}");){if(a("...")){let t=r(),o=g();n.push(c({type:"SpreadElement",argument:o},t,o));}else {let t=i(),o,T=false;if(a("["))T=true,r(),o=g(),y("]");else if(t.type==="String")r(),o=h({type:"Literal",value:L(t.value.slice(1,-1)),raw:t.value},t);else if(t.type==="Number")r(),o=h({type:"Literal",value:Number(t.value),raw:t.value},t);else if(t.type==="Identifier"||t.type==="Keyword")r(),o=h({type:"Identifier",name:t.value},t);else return w("Invalid property key");if(a(":")){r();let P=g();n.push(c({type:"Property",key:o,value:P,kind:"init",method:false,shorthand:false,computed:T},t,P));}else n.push(c({type:"Property",key:o,value:o,kind:"init",method:false,shorthand:true,computed:false},t,o));}a("}")||y(",");}return c({type:"ObjectExpression",properties:n},e,y("}"))},U=(e,n)=>({type:"TemplateElement",value:{raw:e,cooked:L(e)},tail:n}),ne=e=>{r();let n=[U(e.value,false)],t=[];for(;;){t.push(g());let o=i();if(o.type==="TemplateMiddle")r(),n.push(U(o.value,false));else if(o.type==="TemplateTail"){r(),n.push(U(o.value,true));break}else w("Expected `}` to close template interpolation");}return h({type:"TemplateLiteral",quasis:n,expressions:t},e)},W=()=>{let e=i();if(s("async")){let n=i(1);return n.value==="("||n.type==="Identifier"?(r(),R(true,e)):w("Unexpected 'async'")}if(a("(")&&Z()||e.type==="Identifier"&&i(1).type==="Punctuator"&&i(1).value==="=>")return R(false,e);if(e.type==="Number")return r(),h({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return r(),h({type:"Literal",value:L(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return r(),h({type:"TemplateLiteral",expressions:[],quasis:[U(e.value,true)]},e);if(e.type==="TemplateHead")return ne(e);if(e.type==="Keyword")return e.value==="true"?(r(),h({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(r(),h({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(r(),h({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?re(e):e.value==="function"?w("Anonymous functions are not supported \u2014 use an arrow function"):w(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return r(),h({type:"Identifier",name:e.value},e);if(a("(")){r();let n=g();return y(")"),n}return a("[")?ee():a("{")?te():w(`Unexpected token '${e.value||e.type}'`)},re=e=>{r();let n=W();for(;;)if(a(".")){r();let t=i();t.type!=="Identifier"&&t.type!=="Keyword"&&w("Expected property name"),r();let o=h({type:"Identifier",name:t.value},t);n=c({type:"MemberExpression",object:n,property:o,computed:false,optional:false},n,o);}else if(a("[")){r();let t=g(),o=y("]");n=c({type:"MemberExpression",object:n,property:t,computed:true,optional:false},n,o);}else break;if(a("(")){let{args:t,close:o}=O();return c({type:"NewExpression",callee:n,arguments:t},e,o)}return c({type:"NewExpression",callee:n,arguments:[]},e,n)},{parseType:D,parseTypeParamList:oe}=Y({tokens:x,pos:()=>p,peek:i,next:r,isP:a,isK:s,expectP:y,fail:w,unescape:L}),se=()=>{let e=r(),n=j(),t=new Set,o=[];a("<")&&(o=oe(),o.forEach(E=>t.add(E))),y("=");let T=D(t),P=c({type:"TSTypeAliasDeclaration",id:n,typeParameters:o,typeAnnotation:T},e,b());return a(";")&&r(),P},C=()=>{let e=i();if(e.type==="Identifier"&&e.value==="type"&&i(1).type==="Identifier"&&i(2).type==="Punctuator"&&(i(2).value==="="||i(2).value==="<"))return se();if(s("const")||s("let")||s("var")){let o=ae();return a(";")&&r(),o}if(s("if"))return ce();if(s("for"))return ie();if(s("while"))return pe();if(s("do"))return le();if(s("break")||s("continue"))return ue();if(s("return"))return fe();if(s("throw"))return de();if(s("try"))return ye();if(s("switch"))return me();if(a("{"))return K();if((s("function")||s("async")&&i(1).type==="Keyword"&&i(1).value==="function")&&w("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),a(";"))return r(),h({type:"EmptyStatement"},e);e.type==="Keyword"&&Te.has(e.value)&&w(`'${e.value}' is not supported in this language`);let n=g(),t=a(";")?r():b();return c({type:"ExpressionStatement",expression:n},n,t)},ae=()=>{let e=r(),n=e.value,t=j();a(":")&&(r(),t.typeAnnotation=D(new Set));let o=null;a("=")?(r(),o=g()):n==="const"&&w("'const' declarations must be initialized");let T=c({type:"VariableDeclarator",id:t,init:o},t,o??t);return c({type:"VariableDeclaration",kind:n,declarations:[T]},e,o??t)},q=()=>i().type==="Identifier"&&i().value==="of",B=()=>s("in"),ie=()=>{let e=r();y("(");let n=null,t=null;if(!a(";"))if(s("const")||s("let")||s("var")){let E=r(),I=j();if(a(":")&&(r(),I.typeAnnotation=D(new Set)),q()||B()){let A=c({type:"VariableDeclarator",id:I,init:null},I,I);t=c({type:"VariableDeclaration",kind:E.value,declarations:[A]},E,I);}else {let A=null;a("=")?(r(),A=g()):E.value==="const"&&w("'const' declarations must be initialized");let he=c({type:"VariableDeclarator",id:I,init:A},I,A??I);n=c({type:"VariableDeclaration",kind:E.value,declarations:[he]},E,A??I);}}else {let E=g();q()||B()?t=E:n=E;}if(t){let E=B();r();let I=g();y(")");let A=C();return c(E?{type:"ForInStatement",left:t,right:I,body:A}:{type:"ForOfStatement",left:t,right:I,body:A,await:false},e,A)}y(";");let o=a(";")?null:g();y(";");let T=a(")")?null:g();y(")");let P=C();return c({type:"ForStatement",init:n,test:o,update:T,body:P},e,P)},le=()=>{let e=r(),n=C();s("while")||w("Expected 'while' after the 'do' body"),r(),y("(");let t=g(),o=y(")");return a(";")&&r(),c({type:"DoWhileStatement",body:n,test:t},e,o)},ue=()=>{let e=r(),n=e.value==="break"?"BreakStatement":"ContinueStatement",t=a(";")?r():b();return c({type:n,label:null},e,t)},pe=()=>{let e=r();y("(");let n=g();y(")");let t=C();return c({type:"WhileStatement",test:n,body:t},e,t)},ce=()=>{let e=r();y("(");let n=g();y(")");let t=C(),o=null;return s("else")&&(r(),o=C()),c({type:"IfStatement",test:n,consequent:t,alternate:o},e,o??t)},fe=()=>{let e=r(),n=null;!a(";")&&!a("}")&&!u()&&(n=g());let t=a(";")?r():b();return c({type:"ReturnStatement",argument:n},e,t)},de=()=>{let e=r(),n=g(),t=a(";")?r():b();return c({type:"ThrowStatement",argument:n},e,t)},ye=()=>{let e=r(),n=K(),t=null,o=null;if(s("catch")){let T=r(),P=null;a("(")&&(r(),P=j(),y(")"));let E=K();t=c({type:"CatchClause",param:P,body:E},T,E);}return s("finally")&&(r(),o=K()),!t&&!o&&w("'try' requires a 'catch' or 'finally' clause"),c({type:"TryStatement",block:n,handler:t,finalizer:o},e,o??t??n)},me=()=>{let e=r();y("(");let n=g();y(")"),y("{");let t=[];for(;!a("}")&&!u();){let T=i(),P=null;s("case")?(r(),P=g()):s("default")?r():w("Expected 'case' or 'default' in switch"),y(":");let E=[];for(;!a("}")&&!s("case")&&!s("default")&&!u();)E.push(C());t.push(c({type:"SwitchCase",test:P,consequent:E},T,b()??T));}let o=y("}");return c({type:"SwitchStatement",discriminant:n,cases:t},e,o)},K=()=>{let e=y("{"),n=[];for(;!a("}")&&!u();)n.push(C());return c({type:"BlockStatement",body:n},e,y("}"))};return {parseExpr:g,parseProgram:()=>{let e=i(),n=[];for(;!u();)n.push(C());return c({type:"Program",sourceType:"module",body:n},e,b()??e)},eof:u,fail:w}}function Ne(f){return X(f).parseProgram()}function Ee(f){let{parseExpr:x,eof:p,fail:i}=X(f),r=x();return p()||i("Unexpected token after expression"),r}export{G as KEYWORDS,$ as LexError,M as ParseError,Ne as parse,Ee as parseExpression,z as tokenize};
|
|
1
|
+
import {TSLiteError}from'@tslite/core';var Z=class extends TSLiteError{constructor(l,h,d,r,f){super("lex-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="LexError";}},ye=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"]),ze=["...","===","!==","=>","?.","??","==","!=","<=",">=","&&","||","|","&","++","--","+=","-=","*=","/=","%=","**","<<","+","-","*","/","%","<",">","!","?",":",".",",",";","=","^","~","(",")","[","]","{","}"],se=c=>c>="a"&&c<="z"||c>="A"&&c<="Z"||c==="_"||c==="$",me=c=>se(c)||c>="0"&&c<="9",B=c=>c>="0"&&c<="9",de=c=>B(c)||c>="a"&&c<="f"||c>="A"&&c<="F",qe=c=>me(c)||c==="-",Ve=new Set(["(","[","{",",",";",":","?","=>","...","=","+","-","*","/","%","<","<=",">",">=","==","===","!=","!==","&&","||","??","!","+=","-=","*=","/=","%="]),We=new Set(["return","typeof","await","delete","void","in","instanceof","else","case","yield","throw"]);function ae(c){let S=[],l=0,h=1,d=0,r=()=>({line:h,column:d}),f=(v=0)=>c[l+v]??"",L=v=>{let C=r();throw new Z(v,C,l,{start:C,end:{line:h,column:d+1}},l+1)},y=()=>{let v=c[l++];return v==="\r"?(c[l]===`
|
|
2
|
+
`&&l++,h++,d=0):v===`
|
|
3
|
+
`||v==="\u2028"||v==="\u2029"?(h++,d=0):d++,v},s=v=>v===`
|
|
4
|
+
`||v==="\r"||v==="\u2028"||v==="\u2029",x=()=>{for(;;){let v=f();if(v===" "||v===" "||v==="\f"||v==="\v"||s(v))y();else if(v==="/"&&f(1)==="/")for(;l<c.length&&!s(f());)y();else if(v==="/"&&f(1)==="*")for(y(),y();;){if(l>=c.length&&L("Unterminated block comment"),f()==="*"&&f(1)==="/"){y(),y();break}y();}else break}},I=(v,C,R,N)=>{S.push({type:v,value:C,start:R,end:l,loc:{start:N,end:r()}}),$=v==="Punctuator"?Ve.has(C):v==="Keyword"?We.has(C):v==="TemplateHead"||v==="TemplateMiddle";},E=v=>{for(y();l<c.length;){let C=f();if(C===v)return y(),c.slice(a,l);if(s(C)&&L("Unterminated string literal"),C==="\\"){y(),l>=c.length&&L("Unterminated string literal"),y();continue}y();}return L("Unterminated string literal")},m=()=>{if(f()==="0"&&(f(1)==="x"||f(1)==="X")){for(y(),y(),de(f())||L("Invalid hexadecimal literal");de(f());)y();return}if(f()==="0"&&(f(1)==="b"||f(1)==="B")){for(y(),y(),f()!=="0"&&f()!=="1"&&L("Invalid binary literal");f()==="0"||f()==="1";)y();return}if(f()==="0"&&(f(1)==="o"||f(1)==="O")){for(y(),y(),f()>="0"&&f()<="7"||L("Invalid octal literal");f()>="0"&&f()<="7";)y();return}for(;B(f());)y();if(f()===".")for(y();B(f());)y();if(f()==="e"||f()==="E")for(y(),(f()==="+"||f()==="-")&&y(),B(f())||L("Invalid exponent");B(f());)y();},T=()=>{let v=l;for(;l<c.length;){let C=f();if(C==="`"){let R=c.slice(v,l);return y(),{raw:R,interp:false}}if(C==="$"&&f(1)==="{"){let R=c.slice(v,l);return y(),y(),{raw:R,interp:true}}if(C==="\\"){if(y(),l>=c.length)break;y();continue}y();}return L("Unterminated template literal")},a=0,J=0,k=[],$=true,X="js",K=[],F=[];for(;;){if(X==="jsxChildren"){a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(i==="<"){f(1)==="/"?(y(),y(),I("Punctuator","</",a,N),X="jsxCloseTag"):(y(),I("Punctuator","<",a,N),K.push("jsxChildren"),X="jsxOpenTag");continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:"jsxChildren"}),X="js";continue}for(;l<c.length&&f()!=="<"&&f()!=="{";)y();I("JSXText",c.slice(a,l),a,N);continue}if(X==="jsxOpenTag"||X==="jsxCloseTag"){x(),a=l;let N=r();if(l>=c.length){I("EOF","",a,N);break}let i=f();if(se(i)){for(y();qe(f());)y();I("Identifier",c.slice(a,l),a,N);continue}if(i==="/"&&f(1)===">"){y(),y(),I("Punctuator","/>",a,N),X=K.pop()??"js";continue}if(i===">"){y(),I("Punctuator",">",a,N),X=X==="jsxCloseTag"?K.pop()??"js":"jsxChildren";continue}if(i==="{"){y(),I("Punctuator","{",a,N),F.push({level:J,restore:X}),X="js";continue}if(i==='"'||i==="'"){I("String",E(i),a,N);continue}if(i==="="||i==="."||i===":"){y(),I("Punctuator",i,a,N);continue}L(`Unexpected character '${i}' in JSX tag`);}x(),a=l;let v=r();if(l>=c.length){I("EOF","",a,v);break}let C=f();if(se(C)){for(;me(f());)y();let N=c.slice(a,l);I(ye.has(N)?"Keyword":"Identifier",N,a,v);continue}if(B(C)||C==="."&&B(f(1))){m(),I("Number",c.slice(a,l),a,v);continue}if(C==='"'||C==="'"){let N=E(C);I("String",N,a,v);continue}if(C==="`"){y();let{raw:N,interp:i}=T();i?(I("TemplateHead",N,a,v),k.push(J)):I("Template",N,a,v);continue}if(C==="<"&&$){y(),I("Punctuator","<",a,v),K.push("js"),X="jsxOpenTag";continue}if(C==="{"){y(),I("Punctuator","{",a,v),J++;continue}if(C==="}"){if(k.length>0&&J===k[k.length-1]){k.pop(),y();let{raw:N,interp:i}=T();I(i?"TemplateMiddle":"TemplateTail",N,a,v),i&&k.push(J);continue}if(F.length>0&&J===F[F.length-1].level){let{restore:N}=F.pop();y(),I("Punctuator","}",a,v),X=N;continue}y(),I("Punctuator","}",a,v),J--;continue}if(C==="?"&&f(1)==="."&&B(f(2))){y(),I("Punctuator","?",a,v);continue}let R=ze.find(N=>c.startsWith(N,l));if(R){for(let N=0;N<R.length;N++)y();I("Punctuator",R,a,v);continue}L(`Unexpected character '${C}'`);}return S}var ie=(c=[])=>({vars:new Set(c),bounds:new Map,noCond:false,inferSink:null}),H=(c,S)=>{let l=new Set(c.vars),h=new Map(c.bounds);for(let d of S)l.add(d.name),d.bound||d.isConst?h.set(d.name,{bound:d.bound,isConst:d.isConst}):h.delete(d.name);return {vars:l,bounds:h,noCond:c.noCond,inferSink:c.inferSink}},He=(c,S)=>{let l=c.bounds.get(S);return l&&(l.bound||l.isConst)?{t:"var",name:S,...l.bound?{bound:l.bound}:{},...l.isConst?{isConst:true}:{}}:{t:"var",name:S}},he=c=>{let{peek:S,next:l,isP:h,isK:d,expectP:r,fail:f,unescape:L}=c,y=()=>{let i=c.pos(),p=c.tokens[i],u=c.tokens[i-1];return !!u&&!!p&&p.loc.start.line>u.loc.end.line},s=()=>{let i=0;for(let p=c.pos();p<c.tokens.length;p++){let u=c.tokens[p];if(u.type==="EOF")return false;if(u.type==="Punctuator"){if(["(","[","{","<"].includes(u.value))i++;else if([")","]","}",">"].includes(u.value)&&(i--,i===0)){let b=c.tokens[p+1];return !!b&&b.type==="Punctuator"&&b.value==="=>"}}}return false},x=()=>{let i=c.tokens,p=[],u=0,b=false;for(let w=c.pos();w<i.length;w++){let j=i[w];if(j.type==="EOF")break;if(j.type==="Punctuator"){if(j.value==="<")u++,u===1&&(b=true);else if(j.value===">"){if(u--,u===0)break}else j.value==="("||j.value==="["||j.value==="{"?(u++,b=false):j.value===")"||j.value==="]"||j.value==="}"?(u--,b=false):j.value===","&&u===1?b=true:b=false;continue}if(u===1&&b){if(j.type==="Keyword"&&j.value==="const")continue;if(j.type==="Identifier"){p.push(j.value),b=false;continue}}b=false;}return p},I=i=>{let p=H(i,x().map(b=>({name:b})));r("<");let u=[];for(;!h(">");){let b=d("const");b&&l();let w=S();w.type!=="Identifier"&&f("Expected type parameter name"),l();let j;d("extends")&&(l(),j=T(p)),h("=")&&(l(),T(p)),u.push({name:w.value,...j?{bound:j}:{},...b?{isConst:true}:{}}),h(">")||r(",");}return r(">"),u},E=i=>{r("<");let p=[];for(;!h(">");)p.push(T(i)),h(">")||r(",");return r(">"),p},m=i=>{let p=S();return p.type==="Identifier"&&p.value===i},T=i=>{let p=a(i);if(i.noCond||!d("extends")||y())return p;l();let u=[],b=a({...i,noCond:true,inferSink:u});r("?");let w=H(i,u.map(z=>({name:z}))),j=T(w);r(":");let _=T(w);return {t:"conditional",check:p,extends:b,then:j,else:_,...u.length?{infer:u}:{}}},a=i=>{h("|")&&l();let p=J(i);if(!h("|"))return p;let u=[p];for(;h("|");)l(),u.push(J(i));return {t:"union",of:u}},J=i=>{h("&")&&l();let p=k(i);if(!h("&"))return p;let u=[p];for(;h("&");)l(),u.push(k(i));return {t:"intersection",of:u}},k=i=>{if(m("keyof"))return l(),{t:"keyof",of:k(i)};if(m("readonly")){l();let p=k(i);return p.t!=="array"&&p.t!=="tuple"&&f("'readonly' only applies to array and tuple types"),{...p,readonly:true}}if(m("infer")){l();let p=S();p.type!=="Identifier"&&f("Expected 'infer' variable name"),l();let u=i.inferSink;return u?(u.push(p.value),{t:"var",name:p.value}):f("'infer' is only allowed in the 'extends' clause of a conditional")}return $(i)},$=i=>{let p=X(i);for(;h("[")&&!y();)if(l(),h("]"))l(),p={t:"array",items:p};else {let u=T(i);r("]"),p={t:"index",obj:p,key:u};}return p},X=i=>{if(d("typeof")){l();let u=S();u.type!=="Identifier"&&f("Expected a name after 'typeof' in type position"),l();let b=[u.value];for(;h(".");){l();let w=S();w.type!=="Identifier"&&w.type!=="Keyword"&&f("Expected a property name after '.' in 'typeof'"),l(),b.push(w.value);}return {t:"typeQuery",path:b}}if(h("<")){let u=I(i),b=H(i,u);return F(b,u.map(w=>w.name))}if(h("(")){if(s())return F(i);l();let u=T(i.noCond?{...i,noCond:false}:i);return r(")"),u}if(h("{"))return C()?R(i):N(i);if(h("["))return v(i);let p=S();if(p.type==="String")return l(),{t:"literal",value:L(p.value.slice(1,-1))};if(p.type==="Number")return l(),{t:"literal",value:Number(p.value)};if(d("true"))return l(),{t:"literal",value:true};if(d("false"))return l(),{t:"literal",value:false};if(d("null"))return l(),{t:"null"};if(d("void"))return l(),{t:"void"};if(p.type==="Identifier"){switch(l(),p.value){case "string":return {t:"string"};case "number":return {t:"number"};case "boolean":return {t:"boolean"};case "any":return {t:"any"};case "unknown":return {t:"unknown"};case "undefined":return {t:"undefined"};case "never":return {t:"union",of:[]}}let u=h("<")?E(i):[];return i.vars.has(p.value)&&u.length===0?He(i,p.value):p.value==="ReturnType"&&u.length===1?{t:"returnType",fn:u[0]}:p.value==="Parameters"&&u.length===1?{t:"parameters",fn:u[0]}:(c.typeRefs?.push({name:p.value,loc:p.loc}),{t:"apply",name:p.value,args:u})}return f(`Expected a type but got '${p.value||p.type}'`)},K=i=>{r("(");let p=[];for(;!h(")");){let u=S();u.type!=="Identifier"&&f("Expected parameter name in function type"),l();let b=h("?");b&&l(),r(":");let w=T(i);p.push({name:u.value,type:w,...b?{optional:true}:{}}),h(")")||r(",");}return r(")"),p},F=(i,p)=>{let u=K(i);r("=>");let b=T(i);return {t:"fn",params:u,returns:b,...p?.length?{typeParams:p}:{}}},v=i=>{r("[");let p=[],u=[];for(;!h("]");){if(h("...")){l();let b=T(i);b.t==="array"?(p.push(b.items),u.push("rest")):(p.push(b),u.push("variadic"));}else {let b=T(i),w=h("?");w&&l(),p.push(b),u.push(w?"optional":"req");}h("]")||r(",");}return r("]"),u.some(b=>b!=="req")?{t:"tuple",elements:p,flags:u}:{t:"tuple",elements:p}},C=()=>{let i=c.tokens,p=c.pos()+1;return i[p]?.type==="Punctuator"&&(i[p]?.value==="+"||i[p]?.value==="-")&&p++,i[p]?.type==="Identifier"&&i[p]?.value==="readonly"&&p++,i[p]?.type==="Punctuator"&&i[p]?.value==="["&&i[p+1]?.type==="Identifier"&&i[p+2]?.type==="Keyword"&&i[p+2]?.value==="in"},R=i=>{r("{"),(h("+")||h("-"))&&l(),m("readonly")&&l(),r("[");let p=S();p.type!=="Identifier"&&f("Expected mapped type key name"),l(),d("in")||f("Expected 'in' in mapped type"),l();let u=T(i);m("as")&&f("Key remapping ('as') in mapped types is not supported"),r("]");let b=false,w=h("+")||h("-")?l().value:null;h("?")&&(l(),b=w!=="-"),r(":");let j=T(H(i,[{name:p.value}]));return (h(";")||h(","))&&l(),r("}"),{t:"mapped",key:p.value,over:u,body:j,optional:b}},N=i=>{r("{");let p={},u,b=[];for(;!h("}");){if(h("(")){let w=K(i);r(":");let j=T(i);b.push({t:"fn",params:w,returns:j});}else if(h("[")){l(),S().type!=="Identifier"&&f("Expected index signature parameter name"),l(),r(":");let w=S();(w.type!=="Identifier"||w.value!=="string"&&w.value!=="number")&&f("Index signature parameter type must be 'string' or 'number'"),l(),r("]"),r(":"),u={key:w.value,value:T(i)};}else {let w=S(),j;if(w.type==="Identifier"||w.type==="Keyword")l(),j=w.value;else if(w.type==="String")l(),j=L(w.value.slice(1,-1));else if(w.type==="Number")l(),j=w.value;else return f("Expected property name in object type");let _=h("?");_&&l(),r(":");let z=T(i);p[j]={type:z,..._?{optional:true}:{}};}if(h(";")||h(","))l();else break}return r("}"),b.length>0?Object.keys(p).length>0||u?f("Callable object types (call signatures mixed with members) are not supported"):b.length===1?b[0]:{t:"overload",signatures:b}:{t:"object",props:p,...u?{index:u}:{}}};return {parseType:i=>T(ie(i)),parseTypeParamList:()=>I(ie()),parseTypeInScope:i=>T(H(ie(),i))}};var ee=class extends TSLiteError{constructor(l,h,d,r,f){super("parse-error",`${l} (${h.line}:${h.column})`,{position:h,index:d,loc:r,end:f});this.position=h;this.index=d;this.loc=r;this.end=f;this.name="ParseError";}},Ye={"??":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},Qe=new Set(["??","||","&&"]),Ze=new Set(["class","debugger","with","enum","interface"]),et={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"},tt={namespace:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",module:(c,S)=>c.type==="Identifier"&&S.type==="Punctuator"&&S.value==="{",declare:c=>c.type==="Keyword"&&["const","let","var","function","class","enum","interface"].includes(c.value)||c.type==="Identifier"&&["type","namespace","module"].includes(c.value),abstract:c=>c.type==="Keyword"&&c.value==="class"},nt=new Set(["=","+=","-=","*=","/=","%="]);function ge(c){let S=ae(c),l=0,h=[],d=(e=0)=>S[Math.min(l+e,S.length-1)],r=()=>S[l++],f=()=>S[l-1],L=()=>{let e=S[l],t=S[l-1];return !!e&&!!t&&e.loc.start.line>t.loc.end.line},y=()=>d().type==="EOF",s=e=>d().type==="Punctuator"&&d().value===e,x=e=>d().type==="Keyword"&&d().value===e,I=e=>d().type==="Identifier"&&d().value===e,E=(e,t=d())=>{throw new ee(e,t.loc.start,t.start,t.loc,t.end)},m=e=>(s(e)||E(`Expected '${e}' but got '${d().value||d().type}'`),r()),T=(e,t)=>{let n=f()??t;return Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}})},a=(e,t,n)=>Object.assign(e,{start:t.start,end:n.end,loc:{start:t.loc.start,end:n.loc.end}}),J=e=>{let t="";for(let n=0;n<e.length;n++){if(e[n]!=="\\"){t+=e[n];continue}let o=e[++n];switch(o){case "n":t+=`
|
|
5
|
+
`;break;case "t":t+=" ";break;case "r":t+="\r";break;case "b":t+="\b";break;case "f":t+="\f";break;case "v":t+="\v";break;case "0":t+="\0";break;case "x":t+=String.fromCharCode(parseInt(e.slice(n+1,n+3),16)),n+=2;break;case "u":if(e[n+1]==="{"){let g=e.indexOf("}",n);t+=String.fromCodePoint(parseInt(e.slice(n+2,g),16)),n=g;}else t+=String.fromCharCode(parseInt(e.slice(n+1,n+5),16)),n+=4;break;default:t+=o;}}return t},k=(e=false)=>$(e),$=(e=false)=>{let t=X(e),n=d();if(n.type==="Punctuator"&&nt.has(n.value)){r();let o=$();return a({type:"AssignmentExpression",operator:n.value,left:t,right:o},t,o)}return t},X=(e=false)=>{let t=F(0,e);if(!s("?"))return t;r();let n=k();m(":");let o=k();return a({type:"ConditionalExpression",test:t,consequent:n,alternate:o},t,o)},K=()=>{let e=d();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 t=d(1);if(t.type==="Punctuator"&&t.value===">"&&e.end===t.start){let n=d(2);return n.type==="Punctuator"&&n.value===">"&&t.end===n.start?{op:">>>",consume:3}:{op:">>",consume:2}}return {op:">",consume:1}}return {op:e.value,consume:1}},F=(e,t=false)=>{let n=v();for(;;){let o=K();if(!o)break;let{op:g,consume:P}=o;if(g==="in"&&t)break;let A=Ye[g];if(!A||A<=e)break;if(g==="satisfies"){if(L())break;E("'satisfies' is not supported in this language \u2014 a type annotation (`const x: T = \u2026`) also checks compatibility, but it declares the type as T, while 'satisfies' keeps the inferred one");}if(g==="as"){if(L())break;r();let D=V(new Set);n=a({type:"TSAsExpression",expression:n,typeAnnotation:D},n,f());continue}for(let D=0;D<P;D++)r();let O=F(g==="**"?A-1:A,t);n=a({type:Qe.has(g)?"LogicalExpression":"BinaryExpression",operator:g,left:n,right:O},n,O);}return n},v=()=>{let e=d();return e.type==="Punctuator"&&(e.value==="++"||e.value==="--")?(r(),T({type:"UpdateExpression",operator:e.value,prefix:true,argument:v()},e)):x("await")?(r(),T({type:"AwaitExpression",argument:v()},e)):x("typeof")||x("void")||x("delete")||e.type==="Punctuator"&&(e.value==="!"||e.value==="-"||e.value==="+"||e.value==="~")?C(e):N()},C=e=>{r();let t=v();return s("**")&&E(`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.`),T({type:"UnaryExpression",operator:e.value,prefix:true,argument:t},e)},R=()=>{let e=l,t=()=>(l=e,void 0);try{if(r(),s(">"))return t();let n=[];for(;n.push(V(new Set)),!!s(",");)r();return s(">")?(r(),s("(")?n:t()):t()}catch{return t()}},N=()=>{let e=we(),t=false;for(;;)if(s("?."))if(r(),t=true,s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:true},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:true},e,o);}else {let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name after '?.'"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:true},e,o);}else if(s(".")){r();let n=d();n.type!=="Identifier"&&n.type!=="Keyword"&&E("Expected property name"),r();let o=T({type:"Identifier",name:n.value},n);e=a({type:"MemberExpression",object:e,property:o,computed:false,optional:false},e,o);}else if(s("[")){r();let n=k(),o=m("]");e=a({type:"MemberExpression",object:e,property:n,computed:true,optional:false},e,o);}else if(s("(")){let{args:n,close:o}=i();e=a({type:"CallExpression",callee:e,arguments:n,optional:false},e,o);}else if(s("<")){let n=R();if(!n)break;let{args:o,close:g}=i();e=a({type:"CallExpression",callee:e,arguments:o,optional:false,typeArguments:n},e,g);}else if(s("++")||s("--")){let n=r();e=a({type:"UpdateExpression",operator:n.value,prefix:false,argument:e},e,n);}else break;return t?a({type:"ChainExpression",expression:e},e,e):e},i=()=>{m("(");let e=[];for(;!s(")");)e.push(p()),s(")")||m(",");return {args:e,close:m(")")}},p=()=>{if(s("...")){let e=r(),t=k();return a({type:"SpreadElement",argument:t},e,t)}return k()},u=()=>{let e=d();return e.type!=="Identifier"&&E("Expected identifier"),r(),T({type:"Identifier",name:e.value},e)},b=e=>{let t=s("{")?w(e):s("[")?j(e):u();return s(":")&&(r(),t.typeAnnotation=V(e)),t},w=e=>{let t=m("{"),n=[];for(;!s("}");){if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=u(),P,A=false;if(s(":")?(r(),P=b(e)):(P={...g},A=true),s("=")){r();let O=k();P=a({type:"AssignmentPattern",left:P,right:O},P,O);}n.push(a({type:"Property",key:g,value:P,kind:"init",shorthand:A,computed:false,method:false},g,P));}s("}")||m(",");}let o=m("}");return a({type:"ObjectPattern",properties:n},t,o)},j=e=>{let t=m("["),n=[];for(;!s("]");){if(s(",")){n.push(null),r();continue}if(s("...")){let g=r(),P=b(e);n.push(a({type:"RestElement",argument:P},g,P));}else {let g=b(e);if(s("=")){r();let P=k();g=a({type:"AssignmentPattern",left:g,right:P},g,P);}n.push(g);}s("]")||m(",");}let o=m("]");return a({type:"ArrayPattern",elements:n},t,o)},_=()=>{let e=0;for(let t=l;t<S.length;t++){let n=S[t];if(n.type==="EOF")return false;if(n.type==="Punctuator"){if(n.value==="("||n.value==="["||n.value==="{")e++;else if((n.value===")"||n.value==="]"||n.value==="}")&&(e--,e===0)){let o=S[t+1];return !o||o.type!=="Punctuator"?false:o.value==="=>"?true:o.value===":"?z(t+2):false}}}return false},z=e=>{let t=0;for(let n=e;n<S.length;n++){let o=S[n];if(o.type==="EOF")return false;if(o.type==="Punctuator")if(["(","[","{","<"].includes(o.value))t++;else if([")","]","}",">"].includes(o.value)){if(t===0)return false;t--;}else {if(t===0&&o.value==="=>")return true;if(t===0&&(o.value===";"||o.value===","))return false}}return false},ve=(e=new Set)=>{m("(");let t=[];for(;!s(")");){if(s("...")){let n=r(),o=b(e);t.push(a({type:"RestElement",argument:o},n,o));}else {let n=b(e);if(s("=")){r();let o=k();n=a({type:"AssignmentPattern",left:n,right:o},n,o);}t.push(n);}s(")")||m(",");}return m(")"),t},te=(e,t)=>{let n=s("(")?ve():[u()],o;s(":")&&(r(),o=V(new Set)),m("=>");let g=s("{")?W():k();return T({type:"ArrowFunctionExpression",params:n,body:g,async:e,expression:g.type!=="BlockStatement",generator:false,...o?{returnType:o}:{}},t)},be=()=>{let e=m("["),t=[];for(;!s("]");){if(s(",")){r(),t.push(null);continue}t.push(p()),s("]")||m(",");}return a({type:"ArrayExpression",elements:t},e,m("]"))},Te=()=>{let e=m("{"),t=[];for(;!s("}");){if(s("...")){let n=r(),o=k();t.push(a({type:"SpreadElement",argument:o},n,o));}else {let n=d(),o,g=false;if(s("["))g=true,r(),o=k(),m("]");else if(n.type==="String")r(),o=T({type:"Literal",value:J(n.value.slice(1,-1)),raw:n.value},n);else if(n.type==="Number")r(),o=T({type:"Literal",value:Number(n.value),raw:n.value},n);else if(n.type==="Identifier"||n.type==="Keyword")r(),o=T({type:"Identifier",name:n.value},n);else return E("Invalid property key");if(s(":")){r();let P=k();t.push(a({type:"Property",key:o,value:P,kind:"init",method:false,shorthand:false,computed:g},n,P));}else t.push(a({type:"Property",key:o,value:o,kind:"init",method:false,shorthand:true,computed:false},n,o));}s("}")||m(",");}return a({type:"ObjectExpression",properties:t},e,m("}"))},G=(e,t)=>({type:"TemplateElement",value:{raw:e,cooked:J(e)},tail:t}),xe=e=>{r();let t=[G(e.value,false)],n=[];for(;;){n.push(k());let o=d();if(o.type==="TemplateMiddle")r(),t.push(G(o.value,false));else if(o.type==="TemplateTail"){r(),t.push(G(o.value,true));break}else E("Expected `}` to close template interpolation");}return T({type:"TemplateLiteral",quasis:t,expressions:n},e)},q=()=>{let e=d();return e.type!=="Identifier"&&E(`Expected JSX identifier but got '${e.value||e.type}'`),T({type:"JSXIdentifier",name:r().value},e)},le=()=>{let e=q();if(s(":")){r();let t=q();return a({type:"JSXNamespacedName",namespace:e,name:t},e,t)}for(;s(".");){r();let t=q();e=a({type:"JSXMemberExpression",object:e,property:t},e,t);}return e},M=e=>e.type==="JSXIdentifier"?e.name:e.type==="JSXNamespacedName"?`${M(e.namespace)}:${M(e.name)}`:`${M(e.object)}.${M(e.property)}`,Se=()=>{let e=d();if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(s("{")){let t=r(),n=$(),o=m("}");return a({type:"JSXExpressionContainer",expression:n},t,o)}return s("<")?ne():E(`Invalid JSX attribute value '${e.value||e.type}'`)},ke=()=>{let e=[];for(;!s(">")&&!s("/>")&&!y();){if(s("{")){let o=r();m("...");let g=$(),P=m("}");e.push(a({type:"JSXSpreadAttribute",argument:g},o,P));continue}let t=q();if(s(":")){r();let o=q();t=a({type:"JSXNamespacedName",namespace:t,name:o},t,o);}let n=null;s("=")&&(r(),n=Se()),e.push(a({type:"JSXAttribute",name:t,value:n},t,n??t));}return e},pe=()=>{let e=[];for(;!(s("</")||y());){let t=d();if(t.type==="JSXText")r(),e.push(T({type:"JSXText",value:t.value,raw:t.value},t));else if(s("{")){let n=r();if(s("}")){let o=r(),g=T({type:"JSXEmptyExpression"},n);e.push(a({type:"JSXExpressionContainer",expression:g},n,o));}else {let o=$(),g=m("}");e.push(a({type:"JSXExpressionContainer",expression:o},n,g));}}else s("<")?e.push(ne()):E(`Unexpected token in JSX children '${t.value||t.type}'`);}return e},ne=()=>{let e=m("<");if(s(">")){let Q=r(),oe=a({type:"JSXOpeningFragment"},e,Q),Ue=pe(),Me=m("</"),fe=m(">"),Be=a({type:"JSXClosingFragment"},Me,fe);return a({type:"JSXFragment",openingFragment:oe,children:Ue,closingFragment:Be},e,fe)}let t=le(),n=ke();if(s("/>")){let Q=r(),oe=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:true},e,Q);return a({type:"JSXElement",openingElement:oe,children:[],closingElement:null},e,Q)}let o=m(">"),g=a({type:"JSXOpeningElement",name:t,attributes:n,selfClosing:false},e,o),P=pe(),A=m("</"),O=d(),D=le();M(D)!==M(t)&&E(`JSX closing tag </${M(D)}> does not match opening <${M(t)}>`,O);let Y=m(">"),Ke=a({type:"JSXClosingElement",name:D},A,Y);return a({type:"JSXElement",openingElement:g,children:P,closingElement:Ke},e,Y)},we=()=>{let e=d();if(x("async")){let t=d(1);return t.value==="("||t.type==="Identifier"?(r(),te(true,e)):E("Unexpected 'async'")}if(s("(")&&_()||e.type==="Identifier"&&d(1).type==="Punctuator"&&d(1).value==="=>")return te(false,e);if(e.type==="Number")return r(),T({type:"Literal",value:Number(e.value),raw:e.value},e);if(e.type==="String")return r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e);if(e.type==="Template")return r(),T({type:"TemplateLiteral",expressions:[],quasis:[G(e.value,true)]},e);if(e.type==="TemplateHead")return xe(e);if(e.type==="Keyword")return e.value==="true"?(r(),T({type:"Literal",value:true,raw:"true"},e)):e.value==="false"?(r(),T({type:"Literal",value:false,raw:"false"},e)):e.value==="null"?(r(),T({type:"Literal",value:null,raw:"null"},e)):e.value==="new"?E("'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"?E("Anonymous functions are not supported \u2014 use an arrow function"):E(`'${e.value}' is not supported in this language`);if(e.type==="Identifier")return r(),T({type:"Identifier",name:e.value},e);if(s("(")){r();let t=k();return m(")"),t}return s("[")?be():s("{")?Te():s("<")?ne():E(`Unexpected token '${e.value||e.type}'`)},{parseType:V,parseTypeParamList:Ne,parseTypeInScope:Pe}=he({tokens:S,pos:()=>l,peek:d,next:r,isP:s,isK:x,expectP:m,fail:E,unescape:J,typeRefs:h}),Ee=()=>{let e=r(),t=u(),n=[],o=[];s("<")&&(o=Ne(),n=o.map(A=>A.name)),m("=");let g=Pe(o),P=a({type:"TSTypeAliasDeclaration",id:t,typeParameters:n,typeAnnotation:g},e,f());return s(";")&&r(),P},U=()=>{let e=d();if(e.type==="Identifier"&&e.value==="type"&&d(1).type==="Identifier"&&d(2).type==="Punctuator"&&(d(2).value==="="||d(2).value==="<"))return Ee();if(x("const")||x("let")||x("var")){let g=ce();return s(";")&&r(),g}if(x("if"))return Le();if(x("for"))return Ae();if(x("while"))return Je();if(x("do"))return Oe();if(x("break")||x("continue"))return De();if(x("return"))return Xe();if(x("throw"))return Re();if(x("try"))return Fe();if(x("switch"))return $e();if(x("import"))return Ce();if(x("export"))return je();if(s("{"))return W();if((x("function")||x("async")&&d(1).type==="Keyword"&&d(1).value==="function")&&E("Function declarations are not supported \u2014 use 'const fn = () => \u2026'"),s(";"))return r(),T({type:"EmptyStatement"},e);if(e.type==="Keyword"&&Ze.has(e.value)||e.type==="Identifier"&&tt[e.value]?.(d(1),d(2))===true){let g=et[e.value];E(`'${e.value}' is not supported in this language`+(g?` \u2014 ${g}`:""));}let n=k(),o=s(";")?r():f();return a({type:"ExpressionStatement",expression:n},n,o)},ce=()=>{let e=r(),t=e.value,n=b(new Set),o=null;s("=")?(r(),o=k()):t==="const"&&E("'const' declarations must be initialized");let g=a({type:"VariableDeclarator",id:n,init:o},n,o??n);return a({type:"VariableDeclaration",kind:t,declarations:[g]},e,o??n)},Ie=()=>{let e=d();return e.type!=="String"&&E("Expected module specifier string"),r(),T({type:"Literal",value:J(e.value.slice(1,-1)),raw:e.value},e)},Ce=()=>{let e=r(),t=[];if(d().type!=="String"){if(d().type==="Identifier"){let o=u();t.push(a({type:"ImportDefaultSpecifier",local:o},o,o)),s(",")&&r();}if(s("*")){let o=r();I("as")||E("Expected 'as' after '*' in import"),r();let g=u();t.push(a({type:"ImportNamespaceSpecifier",local:g},o,g));}else if(s("{")){for(r();!s("}")&&!y();){let o=u(),g=o;if(I("as")&&(r(),g=u()),t.push(a({type:"ImportSpecifier",imported:o,local:g},o,g)),s(","))r();else break}m("}");}I("from")||E("Expected 'from' in import declaration"),r();}let n=Ie();return s(";")&&r(),T({type:"ImportDeclaration",specifiers:t,source:n},e)},je=()=>{let e=r();if(x("default")){r();let t=k();return s(";")&&r(),T({type:"ExportDefaultDeclaration",declaration:t},e)}if(x("const")||x("let")||x("var")){let t=ce();return s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:t,specifiers:[],source:null},e)}if(s("{")){r();let t=[];for(;!s("}")&&!y();){let n=u(),o=n;if(I("as")&&(r(),o=u()),t.push(a({type:"ExportSpecifier",local:n,exported:o},n,o)),s(","))r();else break}return m("}"),s(";")&&r(),T({type:"ExportNamedDeclaration",declaration:null,specifiers:t,source:null},e)}return E("Expected 'const', '{', or 'default' after 'export'")},ue=()=>d().type==="Identifier"&&d().value==="of",re=()=>x("in"),Ae=()=>{let e=r();m("(");let t=null,n=null;if(!s(";"))if(x("const")||x("let")||x("var")){let A=r(),O=u();if(s(":")&&(r(),O.typeAnnotation=V(new Set)),ue()||re()){let D=a({type:"VariableDeclarator",id:O,init:null},O,O);n=a({type:"VariableDeclaration",kind:A.value,declarations:[D]},A,O);}else {let D=null;s("=")?(r(),D=k(true)):A.value==="const"&&E("'const' declarations must be initialized");let Y=a({type:"VariableDeclarator",id:O,init:D},O,D??O);t=a({type:"VariableDeclaration",kind:A.value,declarations:[Y]},A,D??O);}}else {let A=k(true);ue()||re()?n=A:t=A;}if(n){let A=re();r();let O=k();m(")");let D=U();return a(A?{type:"ForInStatement",left:n,right:O,body:D}:{type:"ForOfStatement",left:n,right:O,body:D,await:false},e,D)}m(";");let o=s(";")?null:k();m(";");let g=s(")")?null:k();m(")");let P=U();return a({type:"ForStatement",init:t,test:o,update:g,body:P},e,P)},Oe=()=>{let e=r(),t=U();x("while")||E("Expected 'while' after the 'do' body"),r(),m("(");let n=k(),o=m(")");return s(";")&&r(),a({type:"DoWhileStatement",body:t,test:n},e,o)},De=()=>{let e=r(),t=e.value==="break"?"BreakStatement":"ContinueStatement",n=s(";")?r():f();return a({type:t,label:null},e,n)},Je=()=>{let e=r();m("(");let t=k();m(")");let n=U();return a({type:"WhileStatement",test:t,body:n},e,n)},Le=()=>{let e=r();m("(");let t=k();m(")");let n=U(),o=null;return x("else")&&(r(),o=U()),a({type:"IfStatement",test:t,consequent:n,alternate:o},e,o??n)},Xe=()=>{let e=r(),t=null;!s(";")&&!s("}")&&!y()&&(t=k());let n=s(";")?r():f();return a({type:"ReturnStatement",argument:t},e,n)},Re=()=>{let e=r(),t=k(),n=s(";")?r():f();return a({type:"ThrowStatement",argument:t},e,n)},Fe=()=>{let e=r(),t=W(),n=null,o=null;if(x("catch")){let g=r(),P=null;s("(")&&(r(),P=u(),m(")"));let A=W();n=a({type:"CatchClause",param:P,body:A},g,A);}return x("finally")&&(r(),o=W()),!n&&!o&&E("'try' requires a 'catch' or 'finally' clause"),a({type:"TryStatement",block:t,handler:n,finalizer:o},e,o??n??t)},$e=()=>{let e=r();m("(");let t=k();m(")"),m("{");let n=[];for(;!s("}")&&!y();){let g=d(),P=null;x("case")?(r(),P=k()):x("default")?r():E("Expected 'case' or 'default' in switch"),m(":");let A=[];for(;!s("}")&&!x("case")&&!x("default")&&!y();)A.push(U());n.push(a({type:"SwitchCase",test:P,consequent:A},g,f()??g));}let o=m("}");return a({type:"SwitchStatement",discriminant:t,cases:n},e,o)},W=()=>{let e=m("{"),t=[];for(;!s("}")&&!y();)t.push(U());return a({type:"BlockStatement",body:t},e,m("}"))};return {parseExpr:k,parseProgram:()=>{let e=d(),t=[];for(;!y();)t.push(U());return a({type:"Program",sourceType:"module",body:t,...h.length?{typeRefs:h}:{}},e,f()??e)},eof:y,fail:E}}function rt(c){return ge(c).parseProgram()}function ot(c){let{parseExpr:S,eof:l,fail:h}=ge(c),d=S();return l()||h("Unexpected token after expression"),d}export{ye as KEYWORDS,Z as LexError,ee as ParseError,rt as parse,ot as parseExpression,ae as tokenize};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tslite/parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "TSLite parser — tokenizer + Pratt parser for the TSLite language (JS subset + pipe) into ESTree",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -40,13 +40,13 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@tslite/core": "0.
|
|
43
|
+
"@tslite/core": "0.2.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@tslite/
|
|
47
|
-
"@tslite/capability": "0.
|
|
48
|
-
"@tslite/
|
|
49
|
-
"@tslite/
|
|
46
|
+
"@tslite/printer": "0.2.0",
|
|
47
|
+
"@tslite/capability": "0.2.0",
|
|
48
|
+
"@tslite/interpret": "0.2.0",
|
|
49
|
+
"@tslite/checker": "0.3.0"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsup",
|