@morphql/core 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- 'use strict';var chevrotain=require('chevrotain'),fastXmlParser=require('fast-xml-parser'),gt=require('papaparse'),Ct=require('js-beautify');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var gt__default=/*#__PURE__*/_interopDefault(gt);var Ct__default=/*#__PURE__*/_interopDefault(Ct);var At=chevrotain.createToken({name:"WhiteSpace",pattern:/\s+/,group:chevrotain.Lexer.SKIPPED}),Ut=chevrotain.createToken({name:"LineComment",pattern:/\/\/[^\n\r]*/,group:chevrotain.Lexer.SKIPPED}),Tt=chevrotain.createToken({name:"BlockComment",pattern:/\/\*[\s\S]*?\*\//,group:chevrotain.Lexer.SKIPPED}),h=chevrotain.createToken({name:"Identifier",pattern:/[a-zA-Z_$][a-zA-Z0-9_.[\]]*/}),C=chevrotain.createToken({name:"QuotedIdentifier",pattern:/`([^`\\]|\\.)*`/}),L=chevrotain.createToken({name:"From",pattern:/from/i,longer_alt:h}),T=chevrotain.createToken({name:"To",pattern:/to/i,longer_alt:h}),N=chevrotain.createToken({name:"Transform",pattern:/transform/i,longer_alt:h}),B=chevrotain.createToken({name:"Set",pattern:/set/i,longer_alt:h}),M=chevrotain.createToken({name:"Section",pattern:/section/i,longer_alt:h}),I=chevrotain.createToken({name:"Multiple",pattern:/multiple/i,longer_alt:h}),q=chevrotain.createToken({name:"Clone",pattern:/clone/i,longer_alt:h}),P=chevrotain.createToken({name:"Delete",pattern:/delete/i,longer_alt:h}),w=chevrotain.createToken({name:"Define",pattern:/define/i,longer_alt:h}),k=chevrotain.createToken({name:"Modify",pattern:/modify/i,longer_alt:h}),R=chevrotain.createToken({name:"If",pattern:/if/i,longer_alt:h}),z=chevrotain.createToken({name:"Else",pattern:/else/i,longer_alt:h}),_=chevrotain.createToken({name:"True",pattern:/true/i,longer_alt:h}),j=chevrotain.createToken({name:"False",pattern:/false/i,longer_alt:h}),F=chevrotain.createToken({name:"Null",pattern:/null/i,longer_alt:h}),x=chevrotain.createToken({name:"Return",pattern:/return/i,longer_alt:h}),D=chevrotain.createToken({name:"Unsafe",pattern:/unsafe/i,longer_alt:h}),W=chevrotain.createToken({name:"EqualsEquals",pattern:/==/}),Y=chevrotain.createToken({name:"EqualsEqualsEquals",pattern:/===/}),G=chevrotain.createToken({name:"NotEquals",pattern:/!=/}),Q=chevrotain.createToken({name:"NotEqualsEquals",pattern:/!==/}),J=chevrotain.createToken({name:"LessThanOrEqual",pattern:/<=/}),V=chevrotain.createToken({name:"GreaterThanOrEqual",pattern:/>=/}),K=chevrotain.createToken({name:"LessThan",pattern:/</}),Z=chevrotain.createToken({name:"GreaterThan",pattern:/>/}),H=chevrotain.createToken({name:"And",pattern:/&&/}),X=chevrotain.createToken({name:"Or",pattern:/\|\|/}),tt=chevrotain.createToken({name:"Not",pattern:/!/}),y=chevrotain.createToken({name:"Equals",pattern:/=/}),et=chevrotain.createToken({name:"Plus",pattern:/\+/}),O=chevrotain.createToken({name:"Minus",pattern:/-/}),rt=chevrotain.createToken({name:"Times",pattern:/\*/}),st=chevrotain.createToken({name:"Divide",pattern:/\//}),c=chevrotain.createToken({name:"LParen",pattern:/\(/}),f=chevrotain.createToken({name:"RParen",pattern:/\)/}),S=chevrotain.createToken({name:"Comma",pattern:/,/}),it=chevrotain.createToken({name:"StringLiteral",pattern:/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/}),nt=chevrotain.createToken({name:"NumericLiteral",pattern:/-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/}),at=[At,Ut,Tt,L,T,N,B,M,I,q,P,w,k,R,z,_,j,F,x,D,Y,W,Q,G,J,V,H,X,y,K,Z,tt,et,O,rt,st,c,f,S,it,nt,C,h],mt=new chevrotain.Lexer(at);var ot=class extends chevrotain.CstParser{constructor(){super(at),this.performSelfAnalysis();}query=this.RULE("query",()=>{this.CONSUME(L),this.SUBRULE(this.typeFormat,{LABEL:"sourceType"}),this.CONSUME(T),this.SUBRULE1(this.typeFormat,{LABEL:"targetType"}),this.OPTION(()=>{this.CONSUME(N),this.OPTION1(()=>{this.CONSUME(D);}),this.MANY(()=>{this.SUBRULE(this.action);});});});typeFormat=this.RULE("typeFormat",()=>{this.SUBRULE(this.anyIdentifier,{LABEL:"name"}),this.OPTION(()=>{this.CONSUME(c),this.MANY_SEP({SEP:S,DEF:()=>{this.SUBRULE(this.typeFormatParameter,{LABEL:"params"});}}),this.CONSUME(f);});});typeFormatParameter=this.RULE("typeFormatParameter",()=>{this.OR([{GATE:()=>this.LA(2).tokenType===y,ALT:()=>this.SUBRULE(this.namedParameter)},{ALT:()=>this.SUBRULE(this.literal)}]);});namedParameter=this.RULE("namedParameter",()=>{this.SUBRULE(this.anyIdentifier,{LABEL:"key"}),this.CONSUME(y),this.SUBRULE(this.literal,{LABEL:"value"});});anyIdentifier=this.RULE("anyIdentifier",()=>{this.OR([{ALT:()=>this.CONSUME(h)},{ALT:()=>this.CONSUME(C)}]);});literal=this.RULE("literal",()=>{this.OR([{ALT:()=>this.CONSUME(it)},{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(j)},{ALT:()=>this.CONSUME(F)}]);});action=this.RULE("action",()=>{this.OR([{ALT:()=>this.SUBRULE(this.setRule)},{ALT:()=>this.SUBRULE(this.modifyRule)},{ALT:()=>this.SUBRULE(this.sectionRule)},{ALT:()=>this.SUBRULE(this.cloneRule)},{ALT:()=>this.SUBRULE(this.deleteRule)},{ALT:()=>this.SUBRULE(this.ifAction)},{ALT:()=>this.SUBRULE(this.defineRule)},{ALT:()=>this.SUBRULE(this.returnRule)}]);});returnRule=this.RULE("returnRule",()=>{this.CONSUME(x),this.SUBRULE(this.expression,{LABEL:"expr"});});deleteRule=this.RULE("deleteRule",()=>{this.CONSUME(P),this.SUBRULE(this.anyIdentifier,{LABEL:"field"});});ifAction=this.RULE("ifAction",()=>{this.CONSUME(R),this.CONSUME(c),this.SUBRULE(this.expression,{LABEL:"condition"}),this.CONSUME(f),this.CONSUME2(c),this.MANY(()=>{this.SUBRULE(this.action,{LABEL:"thenActions"});}),this.CONSUME2(f),this.OPTION(()=>{this.CONSUME(z),this.CONSUME3(c),this.MANY2(()=>{this.SUBRULE2(this.action,{LABEL:"elseActions"});}),this.CONSUME3(f);});});cloneRule=this.RULE("cloneRule",()=>{this.CONSUME(q),this.OPTION(()=>{this.CONSUME(c),this.MANY_SEP({SEP:S,DEF:()=>{this.SUBRULE(this.anyIdentifier,{LABEL:"fields"});}}),this.CONSUME(f);});});setRule=this.RULE("setRule",()=>{this.CONSUME(B),this.SUBRULE(this.anyIdentifier,{LABEL:"left"}),this.CONSUME(y),this.SUBRULE(this.expression,{LABEL:"right"});});modifyRule=this.RULE("modifyRule",()=>{this.CONSUME(k),this.SUBRULE(this.anyIdentifier,{LABEL:"left"}),this.CONSUME(y),this.SUBRULE(this.expression,{LABEL:"right"});});expression=this.RULE("expression",()=>{this.SUBRULE(this.logicalOr);});logicalOr=this.RULE("logicalOr",()=>{this.SUBRULE(this.logicalAnd,{LABEL:"lhs"}),this.MANY(()=>{this.CONSUME(X),this.SUBRULE1(this.logicalAnd,{LABEL:"rhs"});});});logicalAnd=this.RULE("logicalAnd",()=>{this.SUBRULE(this.comparison,{LABEL:"lhs"}),this.MANY(()=>{this.CONSUME(H),this.SUBRULE1(this.comparison,{LABEL:"rhs"});});});comparison=this.RULE("comparison",()=>{this.SUBRULE(this.addition,{LABEL:"lhs"}),this.OPTION(()=>{this.OR([{ALT:()=>this.CONSUME(W,{LABEL:"ops"})},{ALT:()=>this.CONSUME(Y,{LABEL:"ops"})},{ALT:()=>this.CONSUME(G,{LABEL:"ops"})},{ALT:()=>this.CONSUME(Q,{LABEL:"ops"})},{ALT:()=>this.CONSUME(J,{LABEL:"ops"})},{ALT:()=>this.CONSUME(V,{LABEL:"ops"})},{ALT:()=>this.CONSUME(K,{LABEL:"ops"})},{ALT:()=>this.CONSUME(Z,{LABEL:"ops"})}]),this.SUBRULE1(this.addition,{LABEL:"rhs"});});});addition=this.RULE("addition",()=>{this.SUBRULE(this.multiplication,{LABEL:"lhs"}),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(et,{LABEL:"ops"})},{ALT:()=>this.CONSUME(O,{LABEL:"ops"})}]),this.SUBRULE1(this.multiplication,{LABEL:"rhs"});});});multiplication=this.RULE("multiplication",()=>{this.SUBRULE(this.unaryExpression,{LABEL:"lhs"}),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(rt,{LABEL:"ops"})},{ALT:()=>this.CONSUME(st,{LABEL:"ops"})}]),this.SUBRULE1(this.unaryExpression,{LABEL:"rhs"});});});unaryExpression=this.RULE("unaryExpression",()=>{this.OPTION(()=>{this.OR([{ALT:()=>this.CONSUME(O,{LABEL:"sign"})},{ALT:()=>this.CONSUME(tt,{LABEL:"sign"})}]);}),this.SUBRULE(this.atomic);});atomic=this.RULE("atomic",()=>{this.OR([{ALT:()=>this.SUBRULE(this.literal)},{GATE:()=>this.LA(2).tokenType===c,ALT:()=>this.SUBRULE(this.functionCall)},{ALT:()=>this.SUBRULE(this.anyIdentifier)},{ALT:()=>{this.CONSUME(c),this.SUBRULE(this.expression),this.CONSUME(f);}}]);});functionCall=this.RULE("functionCall",()=>{this.OR([{ALT:()=>this.CONSUME(h,{LABEL:"name"})},{ALT:()=>this.CONSUME(R,{LABEL:"name"})}]),this.CONSUME(c),this.MANY_SEP({SEP:S,DEF:()=>{this.SUBRULE(this.expression,{LABEL:"args"});}}),this.CONSUME(f);});sectionRule=this.RULE("sectionRule",()=>{this.CONSUME(M),this.OPTION(()=>{this.CONSUME(I);}),this.SUBRULE(this.anyIdentifier,{LABEL:"sectionName"}),this.CONSUME(c),this.OPTION1(()=>{this.CONSUME(L,{LABEL:"subqueryFrom"}),this.SUBRULE(this.typeFormat,{LABEL:"subquerySourceType"}),this.CONSUME(T,{LABEL:"subqueryTo"}),this.SUBRULE1(this.typeFormat,{LABEL:"subqueryTargetType"}),this.OPTION2(()=>{this.CONSUME(N,{LABEL:"subqueryTransform"});});}),this.MANY(()=>{this.SUBRULE(this.action);}),this.CONSUME(f),this.OPTION3(()=>{this.CONSUME1(L,{LABEL:"followFrom"}),this.SUBRULE2(this.anyIdentifier,{LABEL:"followPath"});});});defineRule=this.RULE("defineRule",()=>{this.CONSUME(w),this.SUBRULE(this.anyIdentifier,{LABEL:"left"}),this.CONSUME(y),this.SUBRULE(this.expression,{LABEL:"right"});})},d=new ot;var ft={substring:s=>{if(s.length<2)throw new Error("substring() requires at least 2 arguments (string, start, [length])");let[t,e,r]=s;return r!==void 0?`String(${t}).slice(${e}, (${e}) + (${r}))`:`String(${t}).slice(${e})`},if:s=>{if(s.length!==3)throw new Error("if() requires exactly 3 arguments (condition, trueValue, falseValue)");let[t,e,r]=s;return `((${t}) ? (${e}) : (${r}))`},text:s=>{if(s.length!==1)throw new Error("text() requires exactly 1 argument (string or number)");let[t]=s;return `String(${t})`},replace:s=>{if(s.length!==3)throw new Error("replace() requires exactly 3 arguments (string, search, replacement)");let[t,e,r]=s;return `String(${t}).replace(${e}, ${r})`},number:s=>{if(s.length!==1)throw new Error("number() requires exactly 1 argument (string)");let[t]=s;return `Number(${t})`},extractnumber:s=>{if(s.length!==1)throw new Error("extractNumber() requires exactly 1 argument (string)");let[t]=s;return `(() => { const match = String(${t}).match(/\\d+(\\.\\d+)?/); return match ? Number(match[0]) : null; })()`},uppercase:s=>{if(s.length!==1)throw new Error("uppercase() requires exactly 1 argument (string)");let[t]=s;return `String(${t}).toUpperCase()`},lowercase:s=>{if(s.length!==1)throw new Error("lowercase() requires exactly 1 argument (string)");let[t]=s;return `String(${t}).toLowerCase()`},xmlnode:s=>{if(s.length<1)throw new Error("xmlnode() requires at least 1 argument (string)");let t=s[0],e=[...s.slice(1)],r="";if(e.length>0){let i=[...e];i=[...Array(Math.ceil(i.length/2))].map(()=>i.splice(0,2)).map(([a,o])=>{let p=a;return a.startsWith('"')||a.startsWith("'")?p=`"$${a.slice(1,-1)}"`:p=`["$"+${a}]`,`${p}:${o||"null"}`}),r=","+i.join(",");}else return t;return `{_:${t}${r}}`},split:s=>{if(s.length<1)throw new Error("split() requires at least 1 argument (string)");let[t,e,r]=s,n=e!==void 0?e:'""',i=r!==void 0?`, ${r}`:"";return `String(${t}).split(${n}${i})`},to_base64:s=>{if(s.length!==1)throw new Error("to_base64() requires exactly 1 argument (string)");let[t]=s;return `(typeof btoa === 'function' ? btoa(unescape(encodeURIComponent(String(${t})))) : Buffer.from(String(${t}), 'utf-8').toString('base64'))`},from_base64:s=>{if(s.length!==1)throw new Error("from_base64() requires exactly 1 argument (string)");let[t]=s;return `(typeof atob === 'function' ? decodeURIComponent(escape(atob(String(${t})))) : Buffer.from(String(${t}), 'base64').toString('utf-8'))`},aslist:s=>{if(s.length!==1)throw new Error("aslist() requires exactly 1 argument");let[t]=s;return `(Array.isArray(${t}) ? ${t} : (${t} == null ? [] : [${t}]))`},spreadsheet:s=>{if(s.length!==1)throw new Error("spreadsheet() requires exactly 1 argument");let[t]=s;return `((data)=>{
1
+ 'use strict';var chevrotain=require('chevrotain'),fastXmlParser=require('fast-xml-parser'),ft=require('papaparse'),vt=require('js-beautify');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var ft__default=/*#__PURE__*/_interopDefault(ft);var vt__default=/*#__PURE__*/_interopDefault(vt);var Et=chevrotain.createToken({name:"WhiteSpace",pattern:/\s+/,group:chevrotain.Lexer.SKIPPED}),Lt=chevrotain.createToken({name:"LineComment",pattern:/\/\/[^\n\r]*/,group:chevrotain.Lexer.SKIPPED}),St=chevrotain.createToken({name:"BlockComment",pattern:/\/\*[\s\S]*?\*\//,group:chevrotain.Lexer.SKIPPED}),c=chevrotain.createToken({name:"Identifier",pattern:/[a-zA-Z_$][a-zA-Z0-9_.[\]]*/}),O=chevrotain.createToken({name:"QuotedIdentifier",pattern:/`([^`\\]|\\.)*`/}),U=chevrotain.createToken({name:"From",pattern:/from/i,longer_alt:c}),R=chevrotain.createToken({name:"To",pattern:/to/i,longer_alt:c}),$=chevrotain.createToken({name:"Transform",pattern:/transform/i,longer_alt:c}),C=chevrotain.createToken({name:"Set",pattern:/set/i,longer_alt:c}),B=chevrotain.createToken({name:"Section",pattern:/section/i,longer_alt:c}),M=chevrotain.createToken({name:"Multiple",pattern:/multiple/i,longer_alt:c}),I=chevrotain.createToken({name:"Clone",pattern:/clone/i,longer_alt:c}),w=chevrotain.createToken({name:"Delete",pattern:/delete/i,longer_alt:c}),k=chevrotain.createToken({name:"Define",pattern:/define/i,longer_alt:c}),q=chevrotain.createToken({name:"Modify",pattern:/modify/i,longer_alt:c}),v=chevrotain.createToken({name:"If",pattern:/if/i,longer_alt:c}),P=chevrotain.createToken({name:"Else",pattern:/else/i,longer_alt:c}),z=chevrotain.createToken({name:"True",pattern:/true/i,longer_alt:c}),_=chevrotain.createToken({name:"False",pattern:/false/i,longer_alt:c}),j=chevrotain.createToken({name:"Null",pattern:/null/i,longer_alt:c}),F=chevrotain.createToken({name:"Return",pattern:/return/i,longer_alt:c}),x=chevrotain.createToken({name:"Unsafe",pattern:/unsafe/i,longer_alt:c}),D=chevrotain.createToken({name:"EqualsEquals",pattern:/==/}),W=chevrotain.createToken({name:"EqualsEqualsEquals",pattern:/===/}),Y=chevrotain.createToken({name:"NotEquals",pattern:/!=/}),G=chevrotain.createToken({name:"NotEqualsEquals",pattern:/!==/}),J=chevrotain.createToken({name:"LessThanOrEqual",pattern:/<=/}),Q=chevrotain.createToken({name:"GreaterThanOrEqual",pattern:/>=/}),V=chevrotain.createToken({name:"LessThan",pattern:/</}),Z=chevrotain.createToken({name:"GreaterThan",pattern:/>/}),K=chevrotain.createToken({name:"And",pattern:/&&/}),H=chevrotain.createToken({name:"Or",pattern:/\|\|/}),X=chevrotain.createToken({name:"Not",pattern:/!/}),E=chevrotain.createToken({name:"Equals",pattern:/=/}),tt=chevrotain.createToken({name:"Plus",pattern:/\+/}),b=chevrotain.createToken({name:"Minus",pattern:/-/}),et=chevrotain.createToken({name:"Times",pattern:/\*/}),rt=chevrotain.createToken({name:"Divide",pattern:/\//}),m=chevrotain.createToken({name:"LParen",pattern:/\(/}),g=chevrotain.createToken({name:"RParen",pattern:/\)/}),N=chevrotain.createToken({name:"Comma",pattern:/,/}),st=chevrotain.createToken({name:"StringLiteral",pattern:/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/}),it=chevrotain.createToken({name:"NumericLiteral",pattern:/-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/}),nt=[Et,Lt,St,U,R,$,C,B,M,I,w,k,q,v,P,z,_,j,F,x,W,D,G,Y,J,Q,K,H,E,V,Z,X,tt,b,et,rt,m,g,N,st,it,O,c],ct=new chevrotain.Lexer(nt);var at=class extends chevrotain.CstParser{constructor(){super(nt),this.performSelfAnalysis();}query=this.RULE("query",()=>{this.CONSUME(U),this.SUBRULE(this.typeFormat,{LABEL:"sourceType"}),this.CONSUME(R),this.SUBRULE1(this.typeFormat,{LABEL:"targetType"}),this.OPTION(()=>{this.CONSUME($),this.OPTION1(()=>{this.CONSUME(x);}),this.MANY(()=>{this.SUBRULE(this.action);});});});typeFormat=this.RULE("typeFormat",()=>{this.SUBRULE(this.anyIdentifier,{LABEL:"name"}),this.OPTION(()=>{this.CONSUME(m),this.MANY_SEP({SEP:N,DEF:()=>{this.SUBRULE(this.typeFormatParameter,{LABEL:"params"});}}),this.CONSUME(g);});});typeFormatParameter=this.RULE("typeFormatParameter",()=>{this.OR([{GATE:()=>this.LA(2).tokenType===E,ALT:()=>this.SUBRULE(this.namedParameter)},{ALT:()=>this.SUBRULE(this.literal)}]);});namedParameter=this.RULE("namedParameter",()=>{this.SUBRULE(this.anyIdentifier,{LABEL:"key"}),this.CONSUME(E),this.SUBRULE(this.literal,{LABEL:"value"});});anyIdentifier=this.RULE("anyIdentifier",()=>{this.OR([{ALT:()=>this.CONSUME(c)},{ALT:()=>this.CONSUME(O)}]);});literal=this.RULE("literal",()=>{this.OR([{ALT:()=>this.CONSUME(st)},{ALT:()=>this.CONSUME(it)},{ALT:()=>this.CONSUME(z)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(j)}]);});action=this.RULE("action",()=>{this.OR([{ALT:()=>this.SUBRULE(this.setRule)},{ALT:()=>this.SUBRULE(this.modifyRule)},{ALT:()=>this.SUBRULE(this.sectionRule)},{ALT:()=>this.SUBRULE(this.cloneRule)},{ALT:()=>this.SUBRULE(this.deleteRule)},{ALT:()=>this.SUBRULE(this.ifAction)},{ALT:()=>this.SUBRULE(this.defineRule)},{ALT:()=>this.SUBRULE(this.returnRule)}]);});returnRule=this.RULE("returnRule",()=>{this.CONSUME(F),this.SUBRULE(this.expression,{LABEL:"expr"});});deleteRule=this.RULE("deleteRule",()=>{this.CONSUME(w),this.SUBRULE(this.anyIdentifier,{LABEL:"field"});});ifAction=this.RULE("ifAction",()=>{this.CONSUME(v),this.CONSUME(m),this.SUBRULE(this.expression,{LABEL:"condition"}),this.CONSUME(g),this.CONSUME2(m),this.MANY(()=>{this.SUBRULE(this.action,{LABEL:"thenActions"});}),this.CONSUME2(g),this.OPTION(()=>{this.CONSUME(P),this.CONSUME3(m),this.MANY2(()=>{this.SUBRULE2(this.action,{LABEL:"elseActions"});}),this.CONSUME3(g);});});cloneRule=this.RULE("cloneRule",()=>{this.CONSUME(I),this.OPTION(()=>{this.CONSUME(m),this.MANY_SEP({SEP:N,DEF:()=>{this.SUBRULE(this.anyIdentifier,{LABEL:"fields"});}}),this.CONSUME(g);});});setRule=this.RULE("setRule",()=>{this.CONSUME(C),this.SUBRULE(this.anyIdentifier,{LABEL:"left"}),this.CONSUME(E),this.SUBRULE(this.expression,{LABEL:"right"});});modifyRule=this.RULE("modifyRule",()=>{this.CONSUME(q),this.SUBRULE(this.anyIdentifier,{LABEL:"left"}),this.CONSUME(E),this.SUBRULE(this.expression,{LABEL:"right"});});expression=this.RULE("expression",()=>{this.SUBRULE(this.logicalOr);});logicalOr=this.RULE("logicalOr",()=>{this.SUBRULE(this.logicalAnd,{LABEL:"lhs"}),this.MANY(()=>{this.CONSUME(H),this.SUBRULE1(this.logicalAnd,{LABEL:"rhs"});});});logicalAnd=this.RULE("logicalAnd",()=>{this.SUBRULE(this.comparison,{LABEL:"lhs"}),this.MANY(()=>{this.CONSUME(K),this.SUBRULE1(this.comparison,{LABEL:"rhs"});});});comparison=this.RULE("comparison",()=>{this.SUBRULE(this.addition,{LABEL:"lhs"}),this.OPTION(()=>{this.OR([{ALT:()=>this.CONSUME(D,{LABEL:"ops"})},{ALT:()=>this.CONSUME(W,{LABEL:"ops"})},{ALT:()=>this.CONSUME(Y,{LABEL:"ops"})},{ALT:()=>this.CONSUME(G,{LABEL:"ops"})},{ALT:()=>this.CONSUME(J,{LABEL:"ops"})},{ALT:()=>this.CONSUME(Q,{LABEL:"ops"})},{ALT:()=>this.CONSUME(V,{LABEL:"ops"})},{ALT:()=>this.CONSUME(Z,{LABEL:"ops"})}]),this.SUBRULE1(this.addition,{LABEL:"rhs"});});});addition=this.RULE("addition",()=>{this.SUBRULE(this.multiplication,{LABEL:"lhs"}),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(tt,{LABEL:"ops"})},{ALT:()=>this.CONSUME(b,{LABEL:"ops"})}]),this.SUBRULE1(this.multiplication,{LABEL:"rhs"});});});multiplication=this.RULE("multiplication",()=>{this.SUBRULE(this.unaryExpression,{LABEL:"lhs"}),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(et,{LABEL:"ops"})},{ALT:()=>this.CONSUME(rt,{LABEL:"ops"})}]),this.SUBRULE1(this.unaryExpression,{LABEL:"rhs"});});});unaryExpression=this.RULE("unaryExpression",()=>{this.OPTION(()=>{this.OR([{ALT:()=>this.CONSUME(b,{LABEL:"sign"})},{ALT:()=>this.CONSUME(X,{LABEL:"sign"})}]);}),this.SUBRULE(this.atomic);});atomic=this.RULE("atomic",()=>{this.OR([{ALT:()=>this.SUBRULE(this.literal)},{GATE:()=>this.LA(2).tokenType===m,ALT:()=>this.SUBRULE(this.functionCall)},{ALT:()=>this.SUBRULE(this.anyIdentifier)},{ALT:()=>{this.CONSUME(m),this.SUBRULE(this.expression),this.CONSUME(g);}}]);});functionCall=this.RULE("functionCall",()=>{this.OR([{ALT:()=>this.CONSUME(c,{LABEL:"name"})},{ALT:()=>this.CONSUME(v,{LABEL:"name"})}]),this.CONSUME(m),this.MANY_SEP({SEP:N,DEF:()=>{this.SUBRULE(this.expression,{LABEL:"args"});}}),this.CONSUME(g);});sectionRule=this.RULE("sectionRule",()=>{this.CONSUME(B),this.OPTION(()=>{this.CONSUME(M);}),this.SUBRULE(this.anyIdentifier,{LABEL:"sectionName"}),this.CONSUME(m),this.OPTION1(()=>{this.CONSUME(U,{LABEL:"subqueryFrom"}),this.SUBRULE(this.typeFormat,{LABEL:"subquerySourceType"}),this.CONSUME(R,{LABEL:"subqueryTo"}),this.SUBRULE1(this.typeFormat,{LABEL:"subqueryTargetType"}),this.OPTION2(()=>{this.CONSUME($,{LABEL:"subqueryTransform"});});}),this.MANY(()=>{this.SUBRULE(this.action);}),this.CONSUME(g),this.OPTION3(()=>{this.CONSUME1(U,{LABEL:"followFrom"}),this.SUBRULE2(this.anyIdentifier,{LABEL:"followPath"});});});defineRule=this.RULE("defineRule",()=>{this.CONSUME(k),this.SUBRULE(this.anyIdentifier,{LABEL:"left"}),this.CONSUME(E),this.SUBRULE(this.expression,{LABEL:"right"});})},L=new at;var ut={substring:i=>{if(i.length<2)throw new Error("substring() requires at least 2 arguments (string, start, [length])");let[t,e,r]=i;return r!==void 0?`String(${t}).slice(${e}, (${e}) + (${r}))`:`String(${t}).slice(${e})`},if:i=>{if(i.length!==3)throw new Error("if() requires exactly 3 arguments (condition, trueValue, falseValue)");let[t,e,r]=i;return `((${t}) ? (${e}) : (${r}))`},text:i=>{if(i.length!==1)throw new Error("text() requires exactly 1 argument (string or number)");let[t]=i;return `String(${t})`},replace:i=>{if(i.length!==3)throw new Error("replace() requires exactly 3 arguments (string, search, replacement)");let[t,e,r]=i;return `String(${t}).replace(${e}, ${r})`},number:i=>{if(i.length!==1)throw new Error("number() requires exactly 1 argument (string)");let[t]=i;return `Number(${t})`},extractnumber:i=>{if(i.length!==1)throw new Error("extractNumber() requires exactly 1 argument (string)");let[t]=i;return `(() => { const match = String(${t}).match(/\\d+(\\.\\d+)?/); return match ? Number(match[0]) : null; })()`},uppercase:i=>{if(i.length!==1)throw new Error("uppercase() requires exactly 1 argument (string)");let[t]=i;return `String(${t}).toUpperCase()`},lowercase:i=>{if(i.length!==1)throw new Error("lowercase() requires exactly 1 argument (string)");let[t]=i;return `String(${t}).toLowerCase()`},xmlnode:i=>{if(i.length<1)throw new Error("xmlnode() requires at least 1 argument (string)");let t=i[0],e=[...i.slice(1)],r="";if(e.length>0){let s=[...e];s=[...Array(Math.ceil(s.length/2))].map(()=>s.splice(0,2)).map(([a,o])=>{let p=a;return a.startsWith('"')||a.startsWith("'")?p=`"$${a.slice(1,-1)}"`:p=`["$"+${a}]`,`${p}:${o||"null"}`}),r=","+s.join(",");}else return t;return `{_:${t}${r}}`},split:i=>{if(i.length<1)throw new Error("split() requires at least 1 argument (string)");let[t,e,r]=i,n=e!==void 0?e:'""',s=r!==void 0?`, ${r}`:"";return `String(${t}).split(${n}${s})`},to_base64:i=>{if(i.length!==1)throw new Error("to_base64() requires exactly 1 argument (string)");let[t]=i;return `(typeof btoa === 'function' ? btoa(unescape(encodeURIComponent(String(${t})))) : Buffer.from(String(${t}), 'utf-8').toString('base64'))`},from_base64:i=>{if(i.length!==1)throw new Error("from_base64() requires exactly 1 argument (string)");let[t]=i;return `(typeof atob === 'function' ? decodeURIComponent(escape(atob(String(${t})))) : Buffer.from(String(${t}), 'base64').toString('utf-8'))`},aslist:i=>{if(i.length!==1)throw new Error("aslist() requires exactly 1 argument");let[t]=i;return `(Array.isArray(${t}) ? ${t} : (${t} == null ? [] : [${t}]))`},spreadsheet:i=>{if(i.length!==1)throw new Error("spreadsheet() requires exactly 1 argument");let[t]=i;return `((data)=>{
2
2
  const spreadsheet = Array.isArray(data) ? data : (data == null ? [] : [data]);
3
3
  const out = [];
4
4
  const titles = [];
@@ -18,7 +18,14 @@
18
18
  }
19
19
  }
20
20
  return out;
21
- })(${t})`}};var A=class{sourceRoot={type:"object",properties:{}};targetRoot={type:"object",properties:{}};sourceStack=[this.sourceRoot];targetStack=[this.targetRoot];getCurrentSource(){return this.sourceStack[this.sourceStack.length-1]}getCurrentTarget(){return this.targetStack[this.targetStack.length-1]}recordAccess(t,e="any",r=false){let n=r?this.getCurrentTarget():this.getCurrentSource();this.setInNode(n,t,e);}recordAssignment(t,e="any"){this.setInNode(this.getCurrentTarget(),t,e);}recordClone(t){let e=this.getCurrentTarget();t?t.forEach(r=>{this.setInNode(this.getCurrentSource(),r,"any"),this.setInNode(e,r,"any");}):e.isOpen=true;}recordDelete(t){let e=this.getCurrentTarget();this.deleteInNode(e,t);}pushSection(t,e,r){e!=="parent"&&this.setInNode(this.getCurrentSource(),e,r?"array":"object");let n={type:r?"array":"object"};r?n.items={type:"object",properties:{}}:n.properties={};let i={type:"object",properties:{}};this.setInNodeExplicit(this.getCurrentTarget(),t,n),this.targetStack.push(r?n.items:n),this.sourceStack.push(i);}popSection(t,e){let r=this.sourceStack.pop();this.targetStack.pop();let n=this.getCurrentSource();if(t!=="parent"){let i=this.getOrSetNode(n,t,e?"array":"object"),a=e?i.items:i;a&&(a.properties||(a.properties={}),Object.assign(a.properties,r.properties||{}));}else n.properties||(n.properties={}),Object.assign(n.properties,r.properties||{});}setInNode(t,e,r){let n=e.split("."),i=t;for(let a=0;a<n.length;a++){let o=n[a];i.properties||(i.properties={}),a===n.length-1?i.properties[o]||(i.properties[o]={type:r},r==="object"?i.properties[o].properties={}:r==="array"&&(i.properties[o].items={type:"object",properties:{}})):(i.properties[o]||(i.properties[o]={type:"object",properties:{}}),i=i.properties[o]);}}setInNodeExplicit(t,e,r){let n=e.split("."),i=t;for(let a=0;a<n.length;a++){let o=n[a];i.properties||(i.properties={}),a===n.length-1?i.properties[o]=r:(i.properties[o]||(i.properties[o]={type:"object",properties:{}}),i=i.properties[o]);}}deleteInNode(t,e){let r=e.split("."),n=t;for(let i=0;i<r.length;i++){if(!n.properties)return;if(i===r.length-1)delete n.properties[r[i]];else if(n=n.properties[r[i]],!n)return}}getOrSetNode(t,e,r){let n=e.split("."),i=t;for(let a=0;a<n.length;a++){let o=n[a],u=a===n.length-1?r:"object";i.properties||(i.properties={}),i.properties[o]||(i.properties[o]={type:u},u==="object"?i.properties[o].properties={}:u==="array"&&(i.properties[o].items={type:"object",properties:{}})),i=i.properties[o];}return i}getResult(){return {source:this.sourceRoot,target:this.targetRoot}}};var Ot=d.getBaseCstVisitorConstructor(),lt=class extends Ot{readFrom="source";scopeStack=[];safeMode=true;isAnalyzing=false;tracker=new A;lastInferredType="any";constructor(){super(),this.validateVisitor();}visitWithContext(t,e){let r=this.readFrom;this.readFrom=e.readFrom;let n=this.visit(t);return this.readFrom=r,n}query(t){this.isAnalyzing&&(this.tracker=new A);let e=this.visit(t.sourceType),r=this.visit(t.targetType),n=!!t.Unsafe;this.safeMode=!n,this.scopeStack.push({format:r.name,options:r.options,isSerializationScope:true});let i=t.action?t.action.map(m=>this.visit(m)):[];this.scopeStack.pop(),t.Transform||i.push("Object.assign(target, source);");let a=e.name,o=r.name,p=JSON.stringify(e.options),u=JSON.stringify(r.options),g=i.some(m=>typeof m=="string"&&m.trim().startsWith("return "));return {code:`
21
+ })(${t})`},unpack:i=>{if(i.length<2)throw new Error("unpack() requires at least 2 arguments (string, fieldSpec1, [fieldSpec2, ...])");let[t,...e]=i;return `({ ${e.map(n=>{let s=n.replace(/^["']|["']$/g,""),a=s.split(":");if(a.length<3)throw new Error(`Invalid field spec for unpack(): ${s}. Expected "name:start:length[:modifier]"`);let[o,p,h,u]=a;if(!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(o))throw new Error(`Invalid field name in unpack() spec: "${o}". Must be a valid JavaScript identifier.`);let y=parseInt(p,10),f=parseInt(h,10);if(isNaN(y)||isNaN(f))throw new Error(`Invalid character positions in unpack() spec: ${s}`);let d=`String(${t} || "").substring(${y}, ${y} + ${f})`;return u!=="raw"&&(d=`(${d}).trim()`),`"${o}": ${d}`}).join(", ")} })`},pack:i=>{if(i.length<2)throw new Error("pack() requires at least 2 arguments (object, fieldSpec1, [fieldSpec2, ...])");let[t,...e]=i,r=e.map(s=>{let a=s.replace(/^["']|["']$/g,""),o=a.split(":");if(o.length<3)throw new Error(`Invalid field spec for pack(): ${a}. Expected "name:start:length[:modifier]"`);let[p,h,u,y]=o;if(!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(p))throw new Error(`Invalid field name in pack() spec: "${p}". Must be a valid JavaScript identifier.`);let f=parseInt(h,10),d=parseInt(u,10);if(isNaN(f)||isNaN(d))throw new Error(`Invalid character positions in pack() spec: ${a}`);return {name:p,start:f,length:d,left:y==="left"}}),n=r.reduce((s,a)=>Math.max(s,a.start+a.length),0);return `((val) => {
22
+ if (!val) return ' '.repeat(${n});
23
+ let line = ' '.repeat(${n});
24
+ ${r.map((s,a)=>{let o=`String(val["${s.name}"] ?? "")`,p=s.left?`(${o}).padStart(${s.length})`:`(${o}).padEnd(${s.length})`;return `
25
+ const v${a} = (${p}).substring(0, ${s.length});
26
+ line = line.substring(0, ${s.start}) + v${a} + line.substring(${s.start} + ${s.length});`}).join("")}
27
+ return line;
28
+ })(${t})`}};var S=class{sourceRoot={type:"object",properties:{}};targetRoot={type:"object",properties:{}};sourceStack=[this.sourceRoot];targetStack=[this.targetRoot];getCurrentSource(){return this.sourceStack[this.sourceStack.length-1]}getCurrentTarget(){return this.targetStack[this.targetStack.length-1]}recordAccess(t,e="any",r=false){let n=r?this.getCurrentTarget():this.getCurrentSource();this.setInNode(n,t,e);}recordAssignment(t,e="any"){this.setInNode(this.getCurrentTarget(),t,e);}recordClone(t){let e=this.getCurrentTarget();t?t.forEach(r=>{this.setInNode(this.getCurrentSource(),r,"any"),this.setInNode(e,r,"any");}):e.isOpen=true;}recordDelete(t){let e=this.getCurrentTarget();this.deleteInNode(e,t);}pushSection(t,e,r){e!=="parent"&&this.setInNode(this.getCurrentSource(),e,r?"array":"object");let n={type:r?"array":"object"};r?n.items={type:"object",properties:{}}:n.properties={};let s={type:"object",properties:{}};this.setInNodeExplicit(this.getCurrentTarget(),t,n),this.targetStack.push(r?n.items:n),this.sourceStack.push(s);}popSection(t,e){let r=this.sourceStack.pop();this.targetStack.pop();let n=this.getCurrentSource();if(t!=="parent"){let s=this.getOrSetNode(n,t,e?"array":"object"),a=e?s.items:s;a&&(a.properties||(a.properties={}),Object.assign(a.properties,r.properties||{}));}else n.properties||(n.properties={}),Object.assign(n.properties,r.properties||{});}setInNode(t,e,r){let n=e.split("."),s=t;for(let a=0;a<n.length;a++){let o=n[a];s.properties||(s.properties={}),a===n.length-1?s.properties[o]||(s.properties[o]={type:r},r==="object"?s.properties[o].properties={}:r==="array"&&(s.properties[o].items={type:"object",properties:{}})):(s.properties[o]||(s.properties[o]={type:"object",properties:{}}),s=s.properties[o]);}}setInNodeExplicit(t,e,r){let n=e.split("."),s=t;for(let a=0;a<n.length;a++){let o=n[a];s.properties||(s.properties={}),a===n.length-1?s.properties[o]=r:(s.properties[o]||(s.properties[o]={type:"object",properties:{}}),s=s.properties[o]);}}deleteInNode(t,e){let r=e.split("."),n=t;for(let s=0;s<r.length;s++){if(!n.properties)return;if(s===r.length-1)delete n.properties[r[s]];else if(n=n.properties[r[s]],!n)return}}getOrSetNode(t,e,r){let n=e.split("."),s=t;for(let a=0;a<n.length;a++){let o=n[a],h=a===n.length-1?r:"object";s.properties||(s.properties={}),s.properties[o]||(s.properties[o]={type:h},h==="object"?s.properties[o].properties={}:h==="array"&&(s.properties[o].items={type:"object",properties:{}})),s=s.properties[o];}return s}getResult(){return {source:this.sourceRoot,target:this.targetRoot}}};var Nt=L.getBaseCstVisitorConstructor(),ot=class extends Nt{readFrom="source";scopeStack=[];safeMode=true;isAnalyzing=false;tracker=new S;lastInferredType="any";constructor(){super(),this.validateVisitor();}reset(){this.readFrom="source",this.scopeStack=[],this.tracker=new S,this.lastInferredType="any";}visitWithContext(t,e){let r=this.readFrom;this.readFrom=e.readFrom;try{return this.visit(t)}finally{this.readFrom=r;}}query(t){this.reset(),this.isAnalyzing&&(this.tracker=new S);let e=this.visit(t.sourceType),r=this.visit(t.targetType),n=!!t.Unsafe;this.safeMode=!n,this.scopeStack.push({format:r.name,options:r.options,isSerializationScope:true});try{let s=t.action?t.action.map(f=>this.visit(f)):[];t.Transform||s.push("Object.assign(target, source);");let a=e.name,o=r.name,p=JSON.stringify(e.options),h=JSON.stringify(r.options),u=s.some(f=>typeof f=="string"&&f.trim().startsWith("return "));return {code:`
22
29
  return function(input, env) {
23
30
  // 1. Parse Input
24
31
  const source = env.parse('${a}', input, ${p});
@@ -27,58 +34,59 @@
27
34
  // 2. Transform
28
35
  const target = {};
29
36
  const _rootTarget = target;
30
- ${i.join(`
37
+ ${s.join(`
31
38
  `)}
32
39
 
33
40
  // 3. Serialize Output
34
- ${g?"":`return env.serialize('${o}', target, ${u});`}
41
+ ${u?"":`return env.serialize('${o}', target, ${h});`}
35
42
  }
36
- `,sourceType:e,targetType:r,analysis:this.isAnalyzing?this.tracker.getResult():void 0}}typeFormat(t){let e=this.visit(t.name),r={params:[]};return t.params&&t.params.forEach(n=>{let i=this.visit(n);typeof i=="object"&&"key"in i?r[i.key]=this.parseLiteral(i.value):r.params.push(this.parseLiteral(i));}),{name:e.name,options:r}}typeFormatParameter(t){if(t.namedParameter)return this.visit(t.namedParameter);if(t.literal)return this.visit(t.literal)}namedParameter(t){let e=this.visit(t.key).name,r=this.visit(t.value);return {key:e,value:r}}parseLiteral(t){if(t==="true")return true;if(t==="false")return false;if(t==="null")return null;if(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))return t.slice(1,-1);let e=Number(t);return isNaN(e)?t:e}genAccess(t,e,r=false){let n=this.safeMode&&!r?"?.":".";return e.quoted||e.name.includes("-")&&!e.name.includes(".")&&!e.name.includes("[")?`${t}${this.safeMode&&!r?"?.":""}["${e.name}"]`:`${t}${n}${e.name}`}anyIdentifier(t){if(t.Identifier)return {name:t.Identifier[0].image,quoted:false};if(t.QuotedIdentifier)return {name:t.QuotedIdentifier[0].image.slice(1,-1).replace(/\\(.)/g,"$1"),quoted:true}}literal(t){if(t.StringLiteral)return this.lastInferredType="string",t.StringLiteral[0].image;if(t.NumericLiteral)return this.lastInferredType="number",t.NumericLiteral[0].image;if(t.True)return this.lastInferredType="boolean","true";if(t.False)return this.lastInferredType="boolean","false";if(t.Null)return this.lastInferredType="null","null"}action(t){if(t.setRule)return this.visit(t.setRule);if(t.modifyRule)return this.visit(t.modifyRule);if(t.sectionRule)return this.visit(t.sectionRule);if(t.cloneRule)return this.visit(t.cloneRule);if(t.ifAction)return this.visit(t.ifAction);if(t.deleteRule)return this.visit(t.deleteRule);if(t.defineRule)return this.visit(t.defineRule);if(t.returnRule)return this.visit(t.returnRule)}deleteRule(t){let e=this.visit(t.field);return this.isAnalyzing&&this.tracker.recordDelete(e.name),`delete ${this.genAccess("target",e,true)};`}ifAction(t){let e=this.visit(t.condition),r=t.thenActions?t.thenActions.map(i=>this.visit(i)).join(`
37
- `):"",n=t.elseActions?`else { ${t.elseActions.map(i=>this.visit(i)).join(`
43
+ `,sourceType:e,targetType:r,analysis:this.isAnalyzing?this.tracker.getResult():void 0}}finally{this.scopeStack.pop();}}typeFormat(t){let e=this.visit(t.name),r={params:[]};return t.params&&t.params.forEach(n=>{let s=this.visit(n);typeof s=="object"&&"key"in s?r[s.key]=this.parseLiteral(s.value):r.params.push(this.parseLiteral(s));}),{name:e.name,options:r}}typeFormatParameter(t){if(t.namedParameter)return this.visit(t.namedParameter);if(t.literal)return this.visit(t.literal)}namedParameter(t){let e=this.visit(t.key).name,r=this.visit(t.value);return {key:e,value:r}}parseLiteral(t){if(t==="true")return true;if(t==="false")return false;if(t==="null")return null;if(t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'"))return t.slice(1,-1);let e=Number(t);return isNaN(e)?t:e}genAccess(t,e,r=false){let n=this.safeMode&&!r?"?.":".";return e.quoted||e.name.includes("-")&&!e.name.includes(".")&&!e.name.includes("[")?`${t}${this.safeMode&&!r?"?.":""}["${e.name}"]`:`${t}${n}${e.name}`}anyIdentifier(t){if(t.Identifier)return {name:t.Identifier[0].image,quoted:false};if(t.QuotedIdentifier)return {name:t.QuotedIdentifier[0].image.slice(1,-1).replace(/\\(.)/g,"$1"),quoted:true}}literal(t){if(t.StringLiteral)return this.lastInferredType="string",t.StringLiteral[0].image;if(t.NumericLiteral)return this.lastInferredType="number",t.NumericLiteral[0].image;if(t.True)return this.lastInferredType="boolean","true";if(t.False)return this.lastInferredType="boolean","false";if(t.Null)return this.lastInferredType="null","null"}action(t){if(t.setRule)return this.visit(t.setRule);if(t.modifyRule)return this.visit(t.modifyRule);if(t.sectionRule)return this.visit(t.sectionRule);if(t.cloneRule)return this.visit(t.cloneRule);if(t.ifAction)return this.visit(t.ifAction);if(t.deleteRule)return this.visit(t.deleteRule);if(t.defineRule)return this.visit(t.defineRule);if(t.returnRule)return this.visit(t.returnRule)}deleteRule(t){let e=this.visit(t.field);return this.isAnalyzing&&this.tracker.recordDelete(e.name),`delete ${this.genAccess("target",e,true)};`}ifAction(t){let e=this.visit(t.condition),r=t.thenActions?t.thenActions.map(s=>this.visit(s)).join(`
44
+ `):"",n=t.elseActions?`else { ${t.elseActions.map(s=>this.visit(s)).join(`
38
45
  `)} }`:"";return `if (${e}) {
39
46
  ${r}
40
47
  } ${n}`}cloneRule(t){if(t.fields){let e=t.fields.map(r=>this.visit(r));return this.isAnalyzing&&this.tracker.recordClone(e.map(r=>r.name)),e.map(r=>`${this.genAccess("target",r,true)} = ${this.genAccess("source",r)};`).join(`
41
- `)}return this.isAnalyzing&&this.tracker.recordClone(),"Object.assign(target, source);"}setRule(t){let e=this.visit(t.left);this.lastInferredType="any";let r=this.visit(t.right);return this.isAnalyzing&&this.tracker.recordAssignment(e.name,this.lastInferredType),`${this.genAccess("target",e,true)} = ${r};`}modifyRule(t){let e=this.visit(t.left);this.lastInferredType="any";let r=this.visitWithContext(t.right,{readFrom:"target"});return this.isAnalyzing&&this.tracker.recordAssignment(e.name,this.lastInferredType),`${this.genAccess("target",e,true)} = ${r};`}defineRule(t){let e=this.visit(t.left);this.lastInferredType="any";let r=this.visit(t.right);return this.isAnalyzing&&this.tracker.recordAssignment(e.name,this.lastInferredType),`${this.genAccess("source",e,true)} = ${r};`}returnRule(t){let e=this.visitWithContext(t.expr,{readFrom:"target"}),r=this.scopeStack[this.scopeStack.length-1];if(r&&r.isSerializationScope){let n=JSON.stringify(r.options);return `return env.serialize('${r.format}', ${e}, ${n});`}return `return ${e};`}expression(t){return this.visit(t.logicalOr)}logicalOr(t){let e=this.visit(t.lhs);if(t.rhs&&t.rhs.length>0){this.lastInferredType="boolean";for(let r=0;r<t.rhs.length;r++){let n=this.visit(t.rhs[r]);e=`${e} || ${n}`;}}return e}logicalAnd(t){let e=this.visit(t.lhs);if(t.rhs&&t.rhs.length>0){this.lastInferredType="boolean";for(let r=0;r<t.rhs.length;r++){let n=this.visit(t.rhs[r]);e=`${e} && ${n}`;}}return e}comparison(t){let e=this.visit(t.lhs);if(t.rhs){this.lastInferredType="boolean";let r=t.ops[0].image,n=this.visit(t.rhs[0]);e=`${e} ${r} ${n}`;}return e}addition(t){let e=this.visit(t.lhs),r=this.lastInferredType,n=e;if(t.rhs&&t.rhs.length>0){let i=r==="string",a=r==="number";for(let o=0;o<t.rhs.length;o++){let p=t.ops[o].image,u=this.visit(t.rhs[o]);this.lastInferredType==="string"&&(i=true),this.lastInferredType!=="number"&&(a=false),n=`${n} ${p} ${u}`;}i?this.lastInferredType="string":a?this.lastInferredType="number":this.lastInferredType="any";}else this.lastInferredType=r;return n}multiplication(t){let e=this.visit(t.lhs);if(t.rhs&&t.rhs.length>0){this.lastInferredType="number";for(let r=0;r<t.rhs.length;r++){let n=t.ops[r].image,i=this.visit(t.rhs[r]);e=`${e} ${n} ${i}`;}}return e}unaryExpression(t){let e=this.visit(t.atomic);if(t.sign){let r=t.sign[0].image;return r==="!"&&(this.lastInferredType="boolean"),r==="-"&&(this.lastInferredType="number"),`${r}${e}`}return e}atomic(t){if(t.literal)return this.visit(t.literal);if(t.functionCall)return this.visit(t.functionCall);if(t.anyIdentifier){this.lastInferredType="any";let e=this.visit(t.anyIdentifier);if(["true","false","null"].includes(e.name)&&!e.quoted)return e.name;if(!e.quoted){if(e.name.startsWith("source.")||e.name.startsWith("source["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7),"any",false),e.name;if(e.name.startsWith("target.")||e.name.startsWith("target["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7),"any",true),e.name;if(e.name==="target")return "target";if(e.name==="source")return "source";if(e.name==="_source"||e.name.startsWith("_source.")||e.name.startsWith("_source["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7)||"","any",false),`_rootSource${e.name.substring(7)}`;if(e.name==="_target"||e.name.startsWith("_target.")||e.name.startsWith("_target["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7)||"","any",true),`_rootTarget${e.name.substring(7)}`}return this.isAnalyzing&&this.tracker.recordAccess(e.name,"any",this.readFrom==="target"),this.genAccess(this.readFrom,e)}if(t.expression)return `(${this.visit(t.expression)})`}functionCall(t){let e=t.name[0].image,r=(e.startsWith("`")?e.slice(1,-1):e).toLowerCase(),n=t.args?t.args.map(a=>this.visit(a)):[],i=ft[r];if(i)return ["substring","text","replace","uppercase","lowercase","to_base64","from_base64"].includes(r)?this.lastInferredType="string":["number","extractnumber"].includes(r)?this.lastInferredType="number":["aslist"].includes(r)?this.lastInferredType="array":r==="asobject"&&(this.lastInferredType="object"),i(n);throw new Error(`Unknown function: ${e}`)}sectionRule(t){let e=this.visit(t.sectionName),r=e.name,n=this.genAccess("target",e,true),i=t.followPath?this.visit(t.followPath):e,a=i.name==="parent"?"source":this.genAccess("source",i),o=!!t.Multiple;if(!!t.subqueryFrom){let E=this.visit(t.subquerySourceType),m=this.visit(t.subqueryTargetType);this.scopeStack.push({format:m.name,options:m.options,isSerializationScope:true});let Lt=!!t.subqueryTransform,$=t.action?t.action.map(St=>this.visit(St)):[];Lt||$.push("Object.assign(target, source);");let ut=JSON.stringify(E.options),ct=JSON.stringify(m.options),v="";return o?v=`
48
+ `)}return this.isAnalyzing&&this.tracker.recordClone(),"Object.assign(target, source);"}setRule(t){let e=this.visit(t.left);this.lastInferredType="any";let r=this.visit(t.right);return this.isAnalyzing&&this.tracker.recordAssignment(e.name,this.lastInferredType),`${this.genAccess("target",e,true)} = ${r};`}modifyRule(t){let e=this.visit(t.left);this.lastInferredType="any";let r=this.visitWithContext(t.right,{readFrom:"target"});return this.isAnalyzing&&this.tracker.recordAssignment(e.name,this.lastInferredType),`${this.genAccess("target",e,true)} = ${r};`}defineRule(t){let e=this.visit(t.left);this.lastInferredType="any";let r=this.visit(t.right);return this.isAnalyzing&&this.tracker.recordAssignment(e.name,this.lastInferredType),`${this.genAccess("source",e,true)} = ${r};`}returnRule(t){let e=this.visitWithContext(t.expr,{readFrom:"target"}),r=this.scopeStack[this.scopeStack.length-1];if(r&&r.isSerializationScope){let n=JSON.stringify(r.options);return `return env.serialize('${r.format}', ${e}, ${n});`}return `return ${e};`}expression(t){return this.visit(t.logicalOr)}logicalOr(t){let e=this.visit(t.lhs);if(t.rhs&&t.rhs.length>0){this.lastInferredType="boolean";for(let r=0;r<t.rhs.length;r++){let n=this.visit(t.rhs[r]);e=`${e} || ${n}`;}}return e}logicalAnd(t){let e=this.visit(t.lhs);if(t.rhs&&t.rhs.length>0){this.lastInferredType="boolean";for(let r=0;r<t.rhs.length;r++){let n=this.visit(t.rhs[r]);e=`${e} && ${n}`;}}return e}comparison(t){let e=this.visit(t.lhs);if(t.rhs){this.lastInferredType="boolean";let r=t.ops[0].image,n=this.visit(t.rhs[0]);e=`${e} ${r} ${n}`;}return e}addition(t){let e=this.visit(t.lhs),r=this.lastInferredType,n=e;if(t.rhs&&t.rhs.length>0){let s=r==="string",a=r==="number";for(let o=0;o<t.rhs.length;o++){let p=t.ops[o].image,h=this.visit(t.rhs[o]);this.lastInferredType==="string"&&(s=true),this.lastInferredType!=="number"&&(a=false),n=`${n} ${p} ${h}`;}s?this.lastInferredType="string":a?this.lastInferredType="number":this.lastInferredType="any";}else this.lastInferredType=r;return n}multiplication(t){let e=this.visit(t.lhs);if(t.rhs&&t.rhs.length>0){this.lastInferredType="number";for(let r=0;r<t.rhs.length;r++){let n=t.ops[r].image,s=this.visit(t.rhs[r]);e=`${e} ${n} ${s}`;}}return e}unaryExpression(t){let e=this.visit(t.atomic);if(t.sign){let r=t.sign[0].image;return r==="!"&&(this.lastInferredType="boolean"),r==="-"&&(this.lastInferredType="number"),`${r}${e}`}return e}atomic(t){if(t.literal)return this.visit(t.literal);if(t.functionCall)return this.visit(t.functionCall);if(t.anyIdentifier){this.lastInferredType="any";let e=this.visit(t.anyIdentifier);if(["true","false","null"].includes(e.name)&&!e.quoted)return e.name;if(!e.quoted){if(e.name.startsWith("source.")||e.name.startsWith("source["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7),"any",false),e.name;if(e.name.startsWith("target.")||e.name.startsWith("target["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7),"any",true),e.name;if(e.name==="target")return "target";if(e.name==="source")return "source";if(e.name==="_source"||e.name.startsWith("_source.")||e.name.startsWith("_source["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7)||"","any",false),`_rootSource${e.name.substring(7)}`;if(e.name==="_target"||e.name.startsWith("_target.")||e.name.startsWith("_target["))return this.isAnalyzing&&this.tracker.recordAccess(e.name.substring(7)||"","any",true),`_rootTarget${e.name.substring(7)}`}return this.isAnalyzing&&this.tracker.recordAccess(e.name,"any",this.readFrom==="target"),this.genAccess(this.readFrom,e)}if(t.expression)return `(${this.visit(t.expression)})`}functionCall(t){let e=t.name[0].image,r=(e.startsWith("`")?e.slice(1,-1):e).toLowerCase(),n=t.args?t.args.map(a=>this.visit(a)):[],s=ut[r];if(s)return ["substring","text","replace","uppercase","lowercase","to_base64","from_base64"].includes(r)?this.lastInferredType="string":["number","extractnumber"].includes(r)?this.lastInferredType="number":["aslist"].includes(r)?this.lastInferredType="array":r==="asobject"&&(this.lastInferredType="object"),s(n);throw new Error(`Unknown function: ${e}`)}sectionRule(t){let e=this.visit(t.sectionName),r=e.name,n=this.genAccess("target",e,true),s=t.followPath?this.visit(t.followPath):e,a=s.name==="parent"?"source":this.genAccess("source",s),o=!!t.Multiple;if(!!t.subqueryFrom){let h=this.visit(t.subquerySourceType),u=this.visit(t.subqueryTargetType);this.scopeStack.push({format:u.name,options:u.options,isSerializationScope:true});try{let y=!!t.subqueryTransform,f=t.action?t.action.map(yt=>this.visit(yt)):[];y||f.push("Object.assign(target, source);");let d=JSON.stringify(h.options),ht=JSON.stringify(u.options);return o?`
42
49
  if (${a} && Array.isArray(${a})) {
43
50
  ${n} = ${a}.map(item => {
44
- const subSource = env.parse('${E.name}', item, ${ut});
51
+ const subSource = env.parse('${h.name}', item, ${d});
45
52
  const source = subSource;
46
53
  const target = {};
47
- ${$.join(`
54
+ ${f.join(`
48
55
  `)}
49
- return env.serialize('${m.name}', target, ${ct});
56
+ return env.serialize('${u.name}', target, ${ht});
50
57
  });
51
58
  }
52
- `:v=`
59
+ `:`
53
60
  if (${a}) {
54
61
  ${n} = (function(innerSource) {
55
- const subSource = env.parse('${E.name}', innerSource, ${ut});
62
+ const subSource = env.parse('${h.name}', innerSource, ${d});
56
63
  const source = subSource;
57
64
  const target = {};
58
- ${$.join(`
65
+ ${f.join(`
59
66
  `)}
60
- return env.serialize('${m.name}', target, ${ct});
67
+ return env.serialize('${u.name}', target, ${ht});
61
68
  })(${a});
62
69
  }
63
- `,this.scopeStack.pop(),v}this.scopeStack.push({format:"object",options:{},isSerializationScope:false}),this.isAnalyzing&&this.tracker.pushSection(r,i.name,o);let u=t.action?t.action.map(E=>this.visit(E)):[];this.isAnalyzing&&this.tracker.popSection(i.name,o);let g="";return o?g=`
70
+ `}finally{this.scopeStack.pop();}}this.scopeStack.push({format:"object",options:{},isSerializationScope:false});try{this.isAnalyzing&&this.tracker.pushSection(r,s.name,o);try{let h=t.action?t.action.map(u=>this.visit(u)):[];return o?`
64
71
  if (${a} && Array.isArray(${a})) {
65
72
  ${n} = ${a}.map(item => {
66
73
  const source = item;
67
74
  const target = {};
68
- ${u.join(`
75
+ ${h.join(`
69
76
  `)}
70
77
  return target;
71
78
  });
72
79
  }
73
- `:g=`
80
+ `:`
74
81
  if (${a}) {
75
82
  ${n} = (function(innerSource) {
76
83
  const source = innerSource;
77
84
  const target = {};
78
- ${u.join(`
85
+ ${h.join(`
79
86
  `)}
80
87
  return target;
81
88
  })(${a});
82
89
  }
83
- `,this.scopeStack.pop(),g}},ht=new lt;var dt={};function b(s,t){dt[s.toLowerCase()]=t;}function pt(s){let t=dt[s.toLowerCase()];if(!t)throw new Error(`No adapter found for format: ${s}`);return t}function $t(s){let t="";for(;s>=0;)t=String.fromCharCode(s%26+65)+t,s=Math.floor(s/26)-1;return t}b("json",{parse:s=>typeof s!="string"?s:JSON.parse(s),serialize:s=>JSON.stringify(s,null,2)});var vt=new fastXmlParser.XMLBuilder({ignoreAttributes:false,attributeNamePrefix:"$",textNodeName:"_",format:true});b("xml",{parse:(s,t)=>typeof s!="string"?s:new fastXmlParser.XMLParser({ignoreAttributes:false,removeNSPrefix:true,...t}).parse(s),serialize:(s,t)=>{let e=t?.rootGenerated??t?.params?.[0]??"root";return (t?new fastXmlParser.XMLBuilder({ignoreAttributes:false,attributeNamePrefix:"$",textNodeName:"_",format:true,...t}):vt).build({[e]:s})}});b("csv",{parse:(s,t)=>{if(typeof s!="string")return s;let e=t?.delimiter??t?.params?.[0]??",";return {rows:gt__default.default.parse(s,{delimiter:e,skipEmptyLines:true,...t}).data.map(i=>{let a={};return Array.isArray(i)&&i.forEach((o,p)=>{a[$t(p)]=o;}),a})}},serialize:(s,t)=>{if(!s||!Array.isArray(s.rows))return "";let e=t?.delimiter??t?.params?.[0]??",",r=s.rows.map(n=>Object.keys(n).filter(a=>/^[A-Z]+$/.test(a)).sort((a,o)=>a.length!==o.length?a.length-o.length:a.localeCompare(o)).map(a=>n[a]));return gt__default.default.unparse(r,{delimiter:e,...t})}});b("object",{parse:s=>s,serialize:s=>s});async function Xt(s,t){if(t?.cache){let g=await t.cache.retrieve(s);if(g)return Et(g)}let e=mt.tokenize(s);if(e.errors.length>0)throw new Error(`Lexing errors: ${e.errors[0].message}`);d.input=e.tokens;let r=d.query();if(d.errors.length>0)throw new Error(`Parsing errors: ${d.errors[0].message}`);ht.isAnalyzing=!!t?.analyze;let{code:n,analysis:i,sourceType:a,targetType:o}=ht.visit(r),p=Ct__default.default.js(n,{indent_size:2,space_in_empty_paren:true,end_with_newline:true});t?.cache&&await t.cache.save(s,p);let u=Et(p);return i&&(i.sourceFormat=a.name,i.targetFormat=o.name,u.analysis=i),u}function Et(s){let e=new Function(s)(),r={parse:(i,a,o)=>pt(i).parse(a,o),serialize:(i,a,o)=>pt(i).serialize(a,o)},n=(i=>e(i,r));return n.code=s,n}function te(s,...t){return s.reduce((e,r,n)=>e+r+(t[n]??""),"")}function ee(s){return `Hello, ${s}! The Morph engine is ready.`}exports.compile=Xt;exports.greet=ee;exports.morphQL=te;//# sourceMappingURL=index.cjs.map
90
+ `}finally{this.isAnalyzing&&this.tracker.popSection(s.name,o);}}finally{this.scopeStack.pop();}}},lt=new ot;var gt={};function A(i,t){gt[i.toLowerCase()]=t;}function pt(i){let t=gt[i.toLowerCase()];if(!t)throw new Error(`No adapter found for format: ${i}`);return t}function Rt(i){let t="";for(;i>=0;)t=String.fromCharCode(i%26+65)+t,i=Math.floor(i/26)-1;return t}A("json",{parse:i=>typeof i!="string"?i:JSON.parse(i),serialize:i=>JSON.stringify(i,null,2)});var $t=new fastXmlParser.XMLBuilder({ignoreAttributes:false,attributeNamePrefix:"$",textNodeName:"_",format:true});A("xml",{parse:(i,t)=>typeof i!="string"?i:new fastXmlParser.XMLParser({ignoreAttributes:false,removeNSPrefix:true,...t}).parse(i),serialize:(i,t)=>{let e=t?.rootGenerated??t?.params?.[0]??"root";return (t?new fastXmlParser.XMLBuilder({ignoreAttributes:false,attributeNamePrefix:"$",textNodeName:"_",format:true,...t}):$t).build({[e]:i})}});A("csv",{parse:(i,t)=>{if(typeof i!="string")return i;let e=t?.delimiter??t?.params?.[0]??",";return {rows:ft__default.default.parse(i,{delimiter:e,skipEmptyLines:true,...t}).data.map(s=>{let a={};return Array.isArray(s)&&s.forEach((o,p)=>{a[Rt(p)]=o;}),a})}},serialize:(i,t)=>{if(!i||!Array.isArray(i.rows))return "";let e=t?.delimiter??t?.params?.[0]??",",r=i.rows.map(n=>Object.keys(n).filter(a=>/^[A-Z]+$/.test(a)).sort((a,o)=>a.length!==o.length?a.length-o.length:a.localeCompare(o)).map(a=>n[a]));return ft__default.default.unparse(r,{delimiter:e,...t})}});A("object",{parse:i=>i,serialize:i=>i});A("plaintext",{parse:(i,t)=>{if(typeof i!="string")return i;let e=t?.separator??t?.params?.[0]??/\r?\n/;return {rows:i.split(e).filter(r=>r.length>0)}},serialize:(i,t)=>{if(!i||!Array.isArray(i.rows))return "";let e=t?.separator??t?.params?.[0]??`
91
+ `;return i.rows.join(e)}});async function Kt(i,t){if(t?.cache){let u=await t.cache.retrieve(i);if(u)return dt(u)}let e=ct.tokenize(i);if(e.errors.length>0)throw new Error(`Lexing errors: ${e.errors[0].message}`);L.input=e.tokens;let r=L.query();if(L.errors.length>0)throw new Error(`Parsing errors: ${L.errors[0].message}`);lt.isAnalyzing=!!t?.analyze;let{code:n,analysis:s,sourceType:a,targetType:o}=lt.visit(r),p=vt__default.default.js(n,{indent_size:2,space_in_empty_paren:true,end_with_newline:true});t?.cache&&await t.cache.save(i,p);let h=dt(p);return s&&(s.sourceFormat=a.name,s.targetFormat=o.name,h.analysis=s),h}function dt(i){let e=new Function(i)(),r={parse:(s,a,o)=>pt(s).parse(a,o),serialize:(s,a,o)=>pt(s).serialize(a,o)},n=(s=>e(s,r));return n.code=i,n}function Ht(i,...t){return i.reduce((e,r,n)=>e+r+(t[n]??""),"")}function Xt(i){return `Hello, ${i}! The Morph engine is ready.`}exports.compile=Kt;exports.getAdapter=pt;exports.greet=Xt;exports.morphQL=Ht;exports.registerAdapter=A;//# sourceMappingURL=index.cjs.map
84
92
  //# sourceMappingURL=index.cjs.map