@jetstreamapp/soql-parser-js 7.0.0 → 7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 7.1.1
4
+
5
+ Apr 25, 2026
6
+
7
+ - Upgrade dependencies
8
+
9
+ ## 7.1.0
10
+
11
+ Mar 21, 2026
12
+
13
+ ### Bug Fixes
14
+
15
+ - **Fixed error recovery dropping subsequent clauses** - When using `ignoreParseErrors: true`, if a clause failed to parse, all subsequent clauses (ORDER BY, LIMIT, OFFSET, etc.) were silently dropped. The parser now correctly synchronizes forward and continues parsing remaining clauses.
16
+ - **Standardized `NULL` value casing in array expressions** - `NULL` values inside `IN` / `NOT IN` / `INCLUDES` / `EXCLUDES` arrays now use uppercase `'NULL'` in the parsed `value` field, consistent with scalar `NULL` comparisons. The composer still outputs lowercase `null` in SOQL strings. If you were comparing `condition.value` items with a case-sensitive `=== 'null'` check, update to `=== 'NULL'`.
17
+ - **Removed incorrect `DOT` token name alias** - `tokenTypeName(TokenKind.DOT)` now returns `'DOT'` instead of the incorrect `'DECIMAL'`. This only affects consumers using the low-level `tokenTypeName` API.
18
+
19
+ ### Internal
20
+
21
+ - Removed singleton parser pattern — `parseQuery` now creates a fresh parser instance per call, eliminating shared mutable state and making the parser safe for concurrent use in worker threads.
22
+
3
23
  ## 7.0.0
4
24
 
5
25
  Mar 14, 2026
package/dist/cjs/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var H=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var Oe=Object.getOwnPropertyNames;var Fe=Object.prototype.hasOwnProperty;var ge=(i,e)=>{for(var s in e)H(i,s,{get:e[s],enumerable:!0})},me=(i,e,s,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Oe(e))!Fe.call(i,n)&&n!==s&&H(i,n,{get:()=>e[n],enumerable:!(t=Le(e,n))||t.enumerable});return i};var ye=i=>me(H({},"__esModule",{value:!0}),i);var we={};ge(we,{Compose:()=>B,composeQuery:()=>Ie,formatQuery:()=>Ye,getComposedField:()=>Pe,getField:()=>Se,getFlattenedFields:()=>ve,hasAlias:()=>U,isFieldSubquery:()=>J,isGroupByField:()=>k,isGroupByFn:()=>Ce,isHavingClauseWithRightCondition:()=>Ae,isNegationCondition:()=>y,isOrderByField:()=>$,isOrderByFn:()=>Te,isQueryValid:()=>ne,isString:()=>f,isSubquery:()=>F,isValueCondition:()=>de,isValueFunctionCondition:()=>Y,isValueQueryCondition:()=>w,isValueWithDateLiteralCondition:()=>Ne,isValueWithDateNLiteralCondition:()=>Re,isWhereClauseWithRightCondition:()=>fe,isWhereOrHavingClauseWithRightCondition:()=>v,parseQuery:()=>b});module.exports=ye(we);var T=(r=>(r[r.AND=0]="AND",r[r.AS=1]="AS",r[r.ASC=2]="ASC",r[r.DESC=3]="DESC",r[r.CUBE=4]="CUBE",r[r.ELSE=5]="ELSE",r[r.EXCLUDES=6]="EXCLUDES",r[r.FALSE=7]="FALSE",r[r.FIRST=8]="FIRST",r[r.FROM=9]="FROM",r[r.GROUP_BY=10]="GROUP_BY",r[r.HAVING=11]="HAVING",r[r.IN=12]="IN",r[r.INCLUDES=13]="INCLUDES",r[r.LAST=14]="LAST",r[r.LIKE=15]="LIKE",r[r.LIMIT=16]="LIMIT",r[r.NOT=17]="NOT",r[r.NULL=18]="NULL",r[r.NULLS=19]="NULLS",r[r.OR=20]="OR",r[r.ORDER_BY=21]="ORDER_BY",r[r.ROLLUP=22]="ROLLUP",r[r.SELECT=23]="SELECT",r[r.TRUE=24]="TRUE",r[r.USING=25]="USING",r[r.WHERE=26]="WHERE",r[r.WITH=27]="WITH",r[r.FOR=28]="FOR",r[r.UPDATE=29]="UPDATE",r[r.ABOVE=30]="ABOVE",r[r.ABOVE_OR_BELOW=31]="ABOVE_OR_BELOW",r[r.APEX_NEW=32]="APEX_NEW",r[r.AT=33]="AT",r[r.BELOW=34]="BELOW",r[r.DATA_CATEGORY=35]="DATA_CATEGORY",r[r.END=36]="END",r[r.OFFSET=37]="OFFSET",r[r.REFERENCE=38]="REFERENCE",r[r.SCOPE=39]="SCOPE",r[r.TRACKING=40]="TRACKING",r[r.THEN=41]="THEN",r[r.TYPEOF=42]="TYPEOF",r[r.VIEW=43]="VIEW",r[r.VIEWSTAT=44]="VIEWSTAT",r[r.WHEN=45]="WHEN",r[r.SECURITY_ENFORCED=46]="SECURITY_ENFORCED",r[r.SYSTEM_MODE=47]="SYSTEM_MODE",r[r.USER_MODE=48]="USER_MODE",r[r.CALENDAR_MONTH=49]="CALENDAR_MONTH",r[r.CALENDAR_QUARTER=50]="CALENDAR_QUARTER",r[r.CALENDAR_YEAR=51]="CALENDAR_YEAR",r[r.DAY_IN_MONTH=52]="DAY_IN_MONTH",r[r.DAY_IN_WEEK=53]="DAY_IN_WEEK",r[r.DAY_IN_YEAR=54]="DAY_IN_YEAR",r[r.DAY_ONLY=55]="DAY_ONLY",r[r.FISCAL_MONTH=56]="FISCAL_MONTH",r[r.FISCAL_QUARTER=57]="FISCAL_QUARTER",r[r.FISCAL_YEAR=58]="FISCAL_YEAR",r[r.HOUR_IN_DAY=59]="HOUR_IN_DAY",r[r.WEEK_IN_MONTH=60]="WEEK_IN_MONTH",r[r.WEEK_IN_YEAR=61]="WEEK_IN_YEAR",r[r.AVG=62]="AVG",r[r.COUNT=63]="COUNT",r[r.COUNT_DISTINCT=64]="COUNT_DISTINCT",r[r.MIN=65]="MIN",r[r.MAX=66]="MAX",r[r.SUM=67]="SUM",r[r.DISTANCE=68]="DISTANCE",r[r.GEOLOCATION=69]="GEOLOCATION",r[r.FIELDS=70]="FIELDS",r[r.FORMAT=71]="FORMAT",r[r.TOLABEL=72]="TOLABEL",r[r.CONVERT_TIMEZONE=73]="CONVERT_TIMEZONE",r[r.CONVERT_CURRENCY=74]="CONVERT_CURRENCY",r[r.GROUPING=75]="GROUPING",r[r.ALL=76]="ALL",r[r.CUSTOM=77]="CUSTOM",r[r.STANDARD=78]="STANDARD",r[r.YESTERDAY=79]="YESTERDAY",r[r.TODAY=80]="TODAY",r[r.TOMORROW=81]="TOMORROW",r[r.LAST_WEEK=82]="LAST_WEEK",r[r.THIS_WEEK=83]="THIS_WEEK",r[r.NEXT_WEEK=84]="NEXT_WEEK",r[r.LAST_MONTH=85]="LAST_MONTH",r[r.THIS_MONTH=86]="THIS_MONTH",r[r.NEXT_MONTH=87]="NEXT_MONTH",r[r.LAST_90_DAYS=88]="LAST_90_DAYS",r[r.NEXT_90_DAYS=89]="NEXT_90_DAYS",r[r.THIS_QUARTER=90]="THIS_QUARTER",r[r.LAST_QUARTER=91]="LAST_QUARTER",r[r.NEXT_QUARTER=92]="NEXT_QUARTER",r[r.THIS_YEAR=93]="THIS_YEAR",r[r.LAST_YEAR=94]="LAST_YEAR",r[r.NEXT_YEAR=95]="NEXT_YEAR",r[r.THIS_FISCAL_QUARTER=96]="THIS_FISCAL_QUARTER",r[r.LAST_FISCAL_QUARTER=97]="LAST_FISCAL_QUARTER",r[r.NEXT_FISCAL_QUARTER=98]="NEXT_FISCAL_QUARTER",r[r.THIS_FISCAL_YEAR=99]="THIS_FISCAL_YEAR",r[r.LAST_FISCAL_YEAR=100]="LAST_FISCAL_YEAR",r[r.NEXT_FISCAL_YEAR=101]="NEXT_FISCAL_YEAR",r[r.NEXT_N_DAYS=102]="NEXT_N_DAYS",r[r.LAST_N_DAYS=103]="LAST_N_DAYS",r[r.N_DAYS_AGO=104]="N_DAYS_AGO",r[r.NEXT_N_WEEKS=105]="NEXT_N_WEEKS",r[r.LAST_N_WEEKS=106]="LAST_N_WEEKS",r[r.N_WEEKS_AGO=107]="N_WEEKS_AGO",r[r.NEXT_N_MONTHS=108]="NEXT_N_MONTHS",r[r.LAST_N_MONTHS=109]="LAST_N_MONTHS",r[r.N_MONTHS_AGO=110]="N_MONTHS_AGO",r[r.NEXT_N_QUARTERS=111]="NEXT_N_QUARTERS",r[r.LAST_N_QUARTERS=112]="LAST_N_QUARTERS",r[r.N_QUARTERS_AGO=113]="N_QUARTERS_AGO",r[r.NEXT_N_YEARS=114]="NEXT_N_YEARS",r[r.LAST_N_YEARS=115]="LAST_N_YEARS",r[r.N_YEARS_AGO=116]="N_YEARS_AGO",r[r.NEXT_N_FISCAL_QUARTERS=117]="NEXT_N_FISCAL_QUARTERS",r[r.LAST_N_FISCAL_QUARTERS=118]="LAST_N_FISCAL_QUARTERS",r[r.N_FISCAL_QUARTERS_AGO=119]="N_FISCAL_QUARTERS_AGO",r[r.NEXT_N_FISCAL_YEARS=120]="NEXT_N_FISCAL_YEARS",r[r.LAST_N_FISCAL_YEARS=121]="LAST_N_FISCAL_YEARS",r[r.N_FISCAL_YEARS_AGO=122]="N_FISCAL_YEARS_AGO",r[r.DELEGATED=123]="DELEGATED",r[r.EVERYTHING=124]="EVERYTHING",r[r.MINE_AND_MY_GROUPS=125]="MINE_AND_MY_GROUPS",r[r.MINE=126]="MINE",r[r.MY_TERRITORY=127]="MY_TERRITORY",r[r.MY_TEAM_TERRITORY=128]="MY_TEAM_TERRITORY",r[r.TEAM=129]="TEAM",r[r.ALL_PRIVATE=130]="ALL_PRIVATE",r[r.MRU=131]="MRU",r[r.EQUAL=132]="EQUAL",r[r.NOT_EQUAL=133]="NOT_EQUAL",r[r.LESS_THAN=134]="LESS_THAN",r[r.LESS_THAN_OR_EQUAL=135]="LESS_THAN_OR_EQUAL",r[r.GREATER_THAN=136]="GREATER_THAN",r[r.GREATER_THAN_OR_EQUAL=137]="GREATER_THAN_OR_EQUAL",r[r.L_PAREN=138]="L_PAREN",r[r.R_PAREN=139]="R_PAREN",r[r.L_SQUARE_BRACKET=140]="L_SQUARE_BRACKET",r[r.R_SQUARE_BRACKET=141]="R_SQUARE_BRACKET",r[r.DOT=142]="DOT",r[r.COMMA=143]="COMMA",r[r.COLON=144]="COLON",r[r.SEMICOLON=145]="SEMICOLON",r[r.ASTERISK=146]="ASTERISK",r[r.PLUS=147]="PLUS",r[r.MINUS=148]="MINUS",r[r.DATETIME=149]="DATETIME",r[r.DATE_TOKEN=150]="DATE_TOKEN",r[r.CURRENCY_PREFIXED_DECIMAL=151]="CURRENCY_PREFIXED_DECIMAL",r[r.CURRENCY_PREFIXED_INTEGER=152]="CURRENCY_PREFIXED_INTEGER",r[r.SIGNED_DECIMAL=153]="SIGNED_DECIMAL",r[r.UNSIGNED_DECIMAL=154]="UNSIGNED_DECIMAL",r[r.SIGNED_INTEGER=155]="SIGNED_INTEGER",r[r.UNSIGNED_INTEGER=156]="UNSIGNED_INTEGER",r[r.STRING_LITERAL=157]="STRING_LITERAL",r[r.GEOLOCATION_UNIT=158]="GEOLOCATION_UNIT",r[r.IDENTIFIER=159]="IDENTIFIER",r[r.EOF=160]="EOF",r))(T||{});function g(i){return i>=49&&i<=61}function m(i){return i>=62&&i<=67}function X(i){return i>=68&&i<=69}function j(i){return i===70}function x(i){return i>=71&&i<=75}function te(i){return i>=76&&i<=78}function M(i){return i>=79&&i<=101}function P(i){return i>=102&&i<=122}function ie(i){return i>=123&&i<=131}function I(i){return i===159||i>=30&&i<=48||i>=49&&i<=61||i>=62&&i<=67||i>=68&&i<=69||i===70||i>=71&&i<=75||i>=76&&i<=78||i>=79&&i<=101||i>=102&&i<=122||i>=123&&i<=131||i===158||i===152}function q(i){return i===153||i===154||i===155||i===156}function re(i){return i>=132&&i<=137}var xe=new Map([["AND",0],["AS",1],["ASC",2],["DESC",3],["CUBE",4],["ELSE",5],["EXCLUDES",6],["FALSE",7],["FIRST",8],["FROM",9],["HAVING",11],["IN",12],["INCLUDES",13],["LAST",14],["LIKE",15],["LIMIT",16],["NOT",17],["NULL",18],["NULLS",19],["OR",20],["ROLLUP",22],["SELECT",23],["TRUE",24],["USING",25],["WHERE",26],["WITH",27],["FOR",28],["UPDATE",29],["ABOVE_OR_BELOW",31],["ABOVE",30],["NEW",32],["AT",33],["BELOW",34],["END",36],["OFFSET",37],["REFERENCE",38],["SCOPE",39],["TRACKING",40],["THEN",41],["TYPEOF",42],["VIEW",43],["VIEWSTAT",44],["WHEN",45],["SECURITY_ENFORCED",46],["SYSTEM_MODE",47],["USER_MODE",48],["CALENDAR_MONTH",49],["CALENDAR_QUARTER",50],["CALENDAR_YEAR",51],["DAY_IN_MONTH",52],["DAY_IN_WEEK",53],["DAY_IN_YEAR",54],["DAY_ONLY",55],["FISCAL_MONTH",56],["FISCAL_QUARTER",57],["FISCAL_YEAR",58],["HOUR_IN_DAY",59],["WEEK_IN_MONTH",60],["WEEK_IN_YEAR",61],["AVG",62],["COUNT_DISTINCT",64],["COUNT",63],["MIN",65],["MAX",66],["SUM",67],["DISTANCE",68],["GEOLOCATION",69],["FIELDS",70],["FORMAT",71],["TOLABEL",72],["CONVERTTIMEZONE",73],["CONVERTCURRENCY",74],["GROUPING",75],["ALL",76],["CUSTOM",77],["STANDARD",78],["YESTERDAY",79],["TODAY",80],["TOMORROW",81],["LAST_WEEK",82],["THIS_WEEK",83],["NEXT_WEEK",84],["LAST_MONTH",85],["THIS_MONTH",86],["NEXT_MONTH",87],["LAST_90_DAYS",88],["NEXT_90_DAYS",89],["THIS_QUARTER",90],["LAST_QUARTER",91],["NEXT_QUARTER",92],["THIS_YEAR",93],["LAST_YEAR",94],["NEXT_YEAR",95],["THIS_FISCAL_QUARTER",96],["LAST_FISCAL_QUARTER",97],["NEXT_FISCAL_QUARTER",98],["THIS_FISCAL_YEAR",99],["LAST_FISCAL_YEAR",100],["NEXT_FISCAL_YEAR",101],["NEXT_N_DAYS",102],["LAST_N_DAYS",103],["N_DAYS_AGO",104],["NEXT_N_WEEKS",105],["LAST_N_WEEKS",106],["N_WEEKS_AGO",107],["NEXT_N_MONTHS",108],["LAST_N_MONTHS",109],["N_MONTHS_AGO",110],["NEXT_N_QUARTERS",111],["LAST_N_QUARTERS",112],["N_QUARTERS_AGO",113],["NEXT_N_YEARS",114],["LAST_N_YEARS",115],["N_YEARS_AGO",116],["NEXT_N_FISCAL_QUARTERS",117],["LAST_N_FISCAL_QUARTERS",118],["N_FISCAL_QUARTERS_AGO",119],["NEXT_N_FISCAL_YEARS",120],["LAST_N_FISCAL_YEARS",121],["N_FISCAL_YEARS_AGO",122],["DELEGATED",123],["EVERYTHING",124],["MINEANDMYGROUPS",125],["MINE",126],["MY_TERRITORY",127],["MY_TEAM_TERRITORY",128],["TEAM",129],["ALLPRIVATE",130],["MRU",131]]),p=[];(function(){for(let e of Object.keys(T)){let s=T[e];typeof s=="number"&&(p[s]=e)}p[72]="toLabel",p[73]="convertTimezone",p[74]="convertCurrency",p[157]="StringIdentifier",p[150]="DATE",p[32]="new",p[125]="MineAndMyGroups",p[127]="MyTerritory",p[128]="MyTeamTerritory",p[130]="AllPrivate",p[123]="Delegated",p[124]="Everything",p[126]="Mine",p[129]="Team",p[131]="Mru",p[159]="Identifier",p[142]="DECIMAL",p[132]="EQUAL",p[156]="UNSIGNED_INTEGER",p[155]="SIGNED_INTEGER",p[154]="UNSIGNED_DECIMAL",p[153]="SIGNED_DECIMAL",p[151]="CURRENCY_PREFIXED_DECIMAL",p[152]="CURRENCY_PREFIXED_INTEGER"})();function d(i){return p[i]||T[i]}function K(i){return i>=65&&i<=90||i>=97&&i<=122}function c(i){return i>=48&&i<=57}function Q(i){return K(i)||c(i)||i===95}function G(i){return i===32||i===9||i===10||i===13||i===12}function se(i){let e=[],s=i.length,t=0;for(;t<s;){let n=i.charCodeAt(t);if(G(n)){for(t++;t<s&&G(i.charCodeAt(t));)t++;continue}if(n===39){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(E===92){t+=2;continue}if(E===39){t++;break}t++}let o=i.substring(a,t),l=o.substring(1,o.length-1).toUpperCase();l==="MI"||l==="KM"?e.push({kind:158,text:o,start:a}):e.push({kind:157,text:o,start:a});continue}if(c(n)){if(t+9<s&&i.charCodeAt(t+4)===45&&i.charCodeAt(t+7)===45){let o=!0;for(let h of[0,1,2,3,5,6,8,9])if(!c(i.charCodeAt(t+h))){o=!1;break}if(o)if(t+10<s&&(i.charCodeAt(t+10)===84||i.charCodeAt(t+10)===116)){let h=t;for(t+=11;t<s;){let l=i.charCodeAt(t);if(c(l)||l===58)t++;else break}if(t<s&&i.charCodeAt(t)===46)for(t++;t<s&&c(i.charCodeAt(t));)t++;if(t<s){let l=i.charCodeAt(t);if(l===90||l===122)t++;else if(l===46){for(t++;t<s&&c(i.charCodeAt(t));)t++;t<s&&(i.charCodeAt(t)===90||i.charCodeAt(t)===122)&&t++}else if(l===43||l===45)for(t++;t<s;){let E=i.charCodeAt(t);if(c(E)||E===58)t++;else break}}e.push({kind:149,text:i.substring(h,t),start:h});continue}else{let h=t;t+=10,e.push({kind:150,text:i.substring(h,t),start:h});continue}}let a=t;for(;t<s&&c(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&c(i.charCodeAt(t+1))){for(t++;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:156,text:i.substring(a,t),start:a});continue}if(n===33){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:133,text:"!=",start:t}),t+=2;continue}throw new Error(`Unexpected character '!' at position ${t}`)}if(n===60){if(t+1<s){let a=i.charCodeAt(t+1);if(a===61){e.push({kind:135,text:"<=",start:t}),t+=2;continue}if(a===62){e.push({kind:133,text:"<>",start:t}),t+=2;continue}}e.push({kind:134,text:"<",start:t}),t++;continue}if(n===62){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:137,text:">=",start:t}),t+=2;continue}e.push({kind:136,text:">",start:t}),t++;continue}if(n===61){e.push({kind:132,text:"=",start:t}),t++;continue}if(n===40){e.push({kind:138,text:"(",start:t}),t++;continue}if(n===41){e.push({kind:139,text:")",start:t}),t++;continue}if(n===91){e.push({kind:140,text:"[",start:t}),t++;continue}if(n===93){e.push({kind:141,text:"]",start:t}),t++;continue}if(n===44){e.push({kind:143,text:",",start:t}),t++;continue}if(n===58){e.push({kind:144,text:":",start:t}),t++;continue}if(n===59){e.push({kind:145,text:";",start:t}),t++;continue}if(n===42){e.push({kind:146,text:"*",start:t}),t++;continue}if(n===43||n===45){if(t+1<s){let a=i.charCodeAt(t+1);if(c(a)){let o=t;for(t++;t<s&&c(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&c(i.charCodeAt(t+1))){for(t++;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}e.push({kind:155,text:i.substring(o,t),start:o});continue}if(a===46&&t+2<s&&c(i.charCodeAt(t+2))){let o=t;for(t+=2;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}}e.push({kind:n===43?147:148,text:i[t],start:t}),t++;continue}if(n===46){if(t+1<s&&c(i.charCodeAt(t+1))){let a=t;for(t++;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:142,text:".",start:t}),t++;continue}if(K(n)){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(Q(E)||E===46)t++;else break}let o=i.substring(a,t);if(o.includes(".."))throw new Error(`Invalid identifier with consecutive dots at position ${a}: "${o}"`);let h=o.toUpperCase();if(h==="ORDER"&&!o.includes(".")){let E=V(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:21,text:u,start:a}),t=E;continue}}if(h==="GROUP"&&!o.includes(".")){let E=V(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:10,text:u,start:a}),t=E;continue}}if(h==="DATA"&&!o.includes(".")){let E=V(i,t,"CATEGORY");if(E!==-1){let u=i.substring(a,E);e.push({kind:35,text:u,start:a}),t=E;continue}}if(o.length>=4){let E=o.substring(0,3);if(ee(E)){let u=o.substring(3);if(De(u)){u.includes(".")?e.push({kind:151,text:o,start:a}):e.push({kind:152,text:o,start:a});continue}}}if(o.length===3&&ee(o)){let E=t,u=t,R=!1;if(u<s&&i.charCodeAt(u)===45&&(R=!0,u++),R&&u<s&&c(i.charCodeAt(u))){for(;u<s&&c(i.charCodeAt(u));)u++;if(u<s&&i.charCodeAt(u)===46&&u+1<s&&c(i.charCodeAt(u+1))){for(u++;u<s&&c(i.charCodeAt(u));)u++;let A=i.substring(a,u);e.push({kind:151,text:A,start:a}),t=u;continue}if(u>=s||!Q(i.charCodeAt(u))&&i.charCodeAt(u)!==46){let A=i.substring(a,u);e.push({kind:152,text:A,start:a}),t=u;continue}t=E}}if(o.includes(".")){e.push({kind:159,text:o,start:a});continue}let l=xe.get(h);if(l!==void 0){e.push({kind:l,text:o,start:a});continue}e.push({kind:159,text:o,start:a});continue}throw new Error(`Unexpected character '${i[t]}' (code ${n}) at position ${t}`)}return e.push({kind:160,text:"",start:t}),e}function V(i,e,s){let t=i.length,n=e;if(n>=t||!G(i.charCodeAt(n)))return-1;for(;n<t&&G(i.charCodeAt(n));)n++;let a=s.length;if(n+a>t)return-1;for(let h=0;h<a;h++){let l=i.charCodeAt(n+h),E=s.charCodeAt(h);if((l>=97&&l<=122?l-32:l)!==E)return-1}let o=n+a;if(o<t){let h=i.charCodeAt(o);if(Q(h)||h===46)return-1}return o}function ee(i){for(let e=0;e<i.length;e++)if(!K(i.charCodeAt(e)))return!1;return!0}function De(i){if(i.length===0)return!1;let e=0;if(i.charCodeAt(e)===45&&e++,e>=i.length||!c(i.charCodeAt(e)))return!1;for(;e<i.length&&c(i.charCodeAt(e));)e++;if(e<i.length&&i.charCodeAt(e)===46){if(e++,e>=i.length||!c(i.charCodeAt(e)))return!1;for(;e<i.length&&c(i.charCodeAt(e));)e++}return e===i.length}var be=new Set([23,9,26,21,10,11,16,27,28,29,25,19,2,3,0,20,17,45,41,5,36,42,143,139,160]),z=class i{constructor(){this.tokens=[];this.pos=0;this.config={}}init(e,s){this.tokens=e,this.pos=0,this.config=s}getPos(){return this.pos}peek(){return this.tokens[this.pos]}peekAt(e){let s=this.pos+e;return s>=this.tokens.length?this.tokens[this.tokens.length-1]:this.tokens[s]}advance(){let e=this.tokens[this.pos];return e.kind!==160&&this.pos++,e}expect(e){let s=this.peek();if(s.kind!==e)throw this.error(`Expected ${T[e]} but found ${T[s.kind]} ("${s.text}") at position ${s.start}`);return this.advance()}match(e){return this.peek().kind===e?(this.advance(),!0):!1}check(e){return this.peek().kind===e}isAtEnd(){return this.peek().kind===160}error(e){return this.config.logErrors&&console.log(e),new Error(e)}static{this.CLAUSE_BOUNDARY_KINDS=new Set([26,21,10,11,16,37,27,28,29,25,160])}isAtClauseBoundary(){let e=this.peek().kind;return i.CLAUSE_BOUNDARY_KINDS.has(e)||e===139}synchronize(){for(;!this.isAtEnd();){if(this.isAtClauseBoundary())return;this.advance()}}consumeIdentifier(){let e=this.peek();if(I(e.kind))return this.advance();throw this.error(`Expected identifier but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}checkIdentifier(){return I(this.peek().kind)}isFunctionCall(){let e=this.peek(),s=this.peekAt(1);return(g(e.kind)||m(e.kind)||X(e.kind)||j(e.kind)||x(e.kind))&&s.kind===138}canBeAlias(){let e=this.peek();return!(!I(e.kind)||be.has(e.kind)||e.kind===37&&this.peekAt(1).kind===156)}getLiteralType(e){let s=d(e);return s==="UNSIGNED_INTEGER"||s==="SIGNED_INTEGER"?"INTEGER":s==="UNSIGNED_DECIMAL"||s==="SIGNED_DECIMAL"?"DECIMAL":s==="CURRENCY_PREFIXED_DECIMAL"?"DECIMAL_WITH_CURRENCY_PREFIX":s==="CURRENCY_PREFIXED_INTEGER"?"INTEGER_WITH_CURRENCY_PREFIX":s==="DATETIME"?"DATETIME":s==="DATE"?"DATE":s==="NULL"?"NULL":s==="StringIdentifier"||s==="Identifier"?"STRING":s==="TRUE"||s==="FALSE"?"BOOLEAN":M(e)?"DATE_LITERAL":P(e)?"DATE_N_LITERAL":"STRING"}parseSelectStatement(e=!1){let s=this.parseSelectClause(),t=this.parseFromClause(),n;e?(n={fields:s,relationshipName:t.sObject},t.sObjectPrefix&&(n.sObjectPrefix=t.sObjectPrefix)):n={fields:s,sObject:t.sObject},t.alias&&(n.sObjectAlias=t.alias),this.applySObjectAlias(n);let a=this.parseClauseStatements();return Object.assign(n,a),n}parseSelectStatementPartial(){let e,s;this.check(23)&&(e=this.parseSelectClause()),this.check(9)&&(s=this.parseFromClause());let t={};e&&(t.fields=e),s&&(t.sObject=s.sObject,s.alias&&(t.sObjectAlias=s.alias)),t.fields&&this.applySObjectAlias(t);let n=this.parseClauseStatements();return Object.assign(t,n),t}applySObjectAlias(e){if(!e.sObjectAlias||!Array.isArray(e.fields))return;let s=e.sObjectAlias;e.fields.forEach(t=>{t.relationships&&t.relationships[0]===s&&(t.relationships=t.relationships.slice(1),t.objectPrefix=s,t.relationships.length===0&&(delete t.relationships,t.type="Field"))})}parseSelectClause(){this.expect(23);let e=[];for(e.push(this.parseSelectField());this.match(143);)e.push(this.parseSelectField());return e}parseSelectField(){let e=this.peek(),s=this.peekAt(1);return e.kind===138&&s.kind===23?this.parseSelectClauseSubqueryIdentifier():e.kind===42?this.parseSelectClauseTypeOf():this.isFunctionCall()?this.parseSelectClauseFunctionIdentifier():this.parseSelectClauseIdentifier()}parseSelectClauseFunctionIdentifier(){let e=this.parseAnyFunction(!0);return this.canBeAlias()&&(e.alias=this.consumeIdentifier().text),e}parseSelectClauseSubqueryIdentifier(){this.expect(138);let e=this.parseSelectStatement(!0);return this.expect(139),{type:"FieldSubquery",subquery:e}}parseSelectClauseTypeOf(){this.expect(42);let e=this.consumeIdentifier().text,s=[];for(;this.check(45);){this.advance();let t=this.consumeIdentifier().text;this.expect(41);let n=[];for(n.push(this.consumeIdentifier().text);this.match(143);)n.push(this.consumeIdentifier().text);s.push({type:"WHEN",objectType:t,fieldList:n})}if(this.check(5)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);s.push({type:"ELSE",fieldList:t})}return this.expect(36),{type:"FieldTypeof",field:e,conditions:s}}parseSelectClauseIdentifier(){let s=this.consumeIdentifier().text,t;if(!s.includes("."))t={type:"Field",field:s};else{let n=s.split(".");t={type:"FieldRelationship",field:n[n.length-1],relationships:n.slice(0,n.length-1),rawValue:s}}return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseFromClause(){this.expect(9);let s=this.consumeIdentifier().text,t;if(s.includes(".")){let n=s.split(".");t={sObjectPrefix:n.slice(0,n.length-1),sObject:n[n.length-1]}}else t={sObject:s};return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseClauseStatements(){let e={},s=(t,n)=>{if(this.config.ignoreParseErrors){let a=this.pos;try{t(),this.isAtClauseBoundary()||(n&&n(),this.synchronize())}catch(o){this.config.logErrors&&console.log(o),n&&n(),this.pos=a,this.synchronize()}}else t()};for(this.check(25)&&s(()=>{e.usingScope=this.parseUsingScope()}),this.check(26)&&s(()=>{e.where=this.parseWhereClause()},()=>{delete e.where});this.check(27);)s(()=>{let t=this.parseWithClause();t.withSecurityEnforced&&(e.withSecurityEnforced=!0),t.withAccessLevel&&(e.withAccessLevel=t.withAccessLevel),t.withDataCategory&&(e.withDataCategory=t.withDataCategory)});return this.check(10)&&(s(()=>{e.groupBy=this.parseGroupByClause()}),this.check(11)&&s(()=>{e.having=this.parseHavingClause()})),this.check(21)&&s(()=>{e.orderBy=this.parseOrderByClause()}),this.check(16)&&s(()=>{this.advance(),e.limit=Number(this.expect(156).text)}),this.check(37)&&s(()=>{this.advance(),e.offset=Number(this.expect(156).text)}),this.check(28)&&s(()=>{this.advance();let t=this.peek();if(t.kind===43||t.kind===38||t.kind===29)e.for=d(this.advance().kind);else throw this.error(`Expected VIEW, REFERENCE, or UPDATE after FOR at position ${t.start}`)}),this.check(29)&&s(()=>{this.advance();let t=this.peek();if(t.kind===40||t.kind===44)e.update=d(this.advance().kind);else throw this.error(`Expected TRACKING or VIEWSTAT after UPDATE at position ${t.start}`)}),e}parseUsingScope(){this.expect(25),this.expect(39);let e=this.peek();if(ie(e.kind))return this.advance().text;if(I(e.kind))return this.advance().text;throw this.error(`Expected scope enumeration value at position ${e.start}`)}parseWhereClause(){return this.expect(26),this.parseConditionExpressions(!0,!0)}parseConditionExpressions(e,s){let t,n,a={left:0,right:0};for(;;){let o;if(t)if(this.check(0))o="AND",this.advance();else if(this.check(20))o="OR",this.advance();else break;let h={},l=h,E=null;for(;this.hasNegationPrefix();){let A=0;for(;this.check(138)&&this.isNegationAhead();)this.advance(),A++,a.left++;if(this.check(17)){this.advance();let _={left:A>0?{openParen:A}:null,operator:"NOT",right:{left:{}}};E?E.right=_:h=_,E=_,l=_.right}else break}l.left=this.parseExpression(e,s,a),t?o&&(n.operator=o,n.right=h):(t=h,n=h);let u=h;for(;u.right&&u.right.left!==void 0;){let A=u.right;if(A.right&&A.right.left!==void 0)u=A;else{u=A;break}}if(n=u,this.isAtEnd())break;let R=this.peek().kind;if(R!==0&&R!==20)break}if(a.left!==a.right)throw this.error(`Mismatched parentheses in WHERE/HAVING clause: ${a.left} open vs ${a.right} close`);return t}hasNegationPrefix(){let e=0;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}isNegationAhead(){let e=1;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}parseExpression(e,s,t){let n=0;for(;this.check(138)&&!this.isSubqueryStart()&&!this.isArrayExpressionStart();)this.advance(),n++,t&&t.left++;let a,o=!1;e&&m(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseAggregateFunction(!1),o=!0):s&&X(this.peek().kind)&&this.peek().kind===68&&this.peekAt(1).kind===138?(a=this.parseLocationFunction(!1),o=!0):g(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseDateFunction(!1),o=!0):x(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseOtherFunction(!1),o=!0):a=this.consumeIdentifier().text;let h=this.parseOperator(),l=h.operator,E=h.isSet,u,R,A;if(E)if(this.check(138)&&this.peekAt(1).kind===23)this.expect(138),u=this.parseSelectStatement(!1),this.expect(139),R="SUBQUERY";else if(this.config.allowApexBindVariables&&this.check(144))u=this.parseApexBindVariable().value,R="APEX_BIND_VARIABLE";else{let S=this.parseArrayExpression();u=S.values,R=S.literalType,A=S.dateLiteralVariable}else{let S=this.parseAtomicValue();u=S.value,R=S.literalType,A=S.dateLiteralVariable}let _=0;for(;this.check(139)&&(!t||t.left>t.right);)this.advance(),_++,t&&t.right++;let C={};return o?C.fn=a:C.field=a,C.operator=l,R==="SUBQUERY"?C.valueQuery=u:C.value=u,C.literalType=R,A!==void 0&&(C.dateLiteralVariable=A),n>0&&(C.openParen=n),_>0&&(C.closeParen=_),C}isSubqueryStart(){return this.check(138)&&this.peekAt(1).kind===23}isArrayExpressionStart(){return!1}parseOperator(){let e=this.peek();if(re(e.kind))return{operator:this.advance().text,isSet:!1};if(e.kind===15)return this.advance(),{operator:"LIKE",isSet:!1};if(e.kind===12)return this.advance(),{operator:"IN",isSet:!0};if(e.kind===17&&this.peekAt(1).kind===12)return this.advance(),this.advance(),{operator:"NOT IN",isSet:!0};if(e.kind===13)return this.advance(),{operator:"INCLUDES",isSet:!0};if(e.kind===6)return this.advance(),{operator:"EXCLUDES",isSet:!0};throw this.error(`Expected operator but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseAtomicValue(){let e=this.peek();if(this.config.allowApexBindVariables&&e.kind===144)return{value:this.parseApexBindVariable().value,literalType:"APEX_BIND_VARIABLE"};if(e.kind===157)return{value:this.advance().text,literalType:"STRING"};if(e.kind===158)return{value:this.advance().text,literalType:"STRING"};if(e.kind===156)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===155)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===154)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===153)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===151)return{value:this.advance().text,literalType:"DECIMAL_WITH_CURRENCY_PREFIX"};if(e.kind===152)return{value:this.advance().text,literalType:"INTEGER_WITH_CURRENCY_PREFIX"};if(e.kind===149)return{value:this.advance().text,literalType:"DATETIME"};if(e.kind===150)return{value:this.advance().text,literalType:"DATE"};if(e.kind===18)return this.advance(),{value:"NULL",literalType:"NULL"};if(e.kind===24||e.kind===7)return{value:d(this.advance().kind),literalType:"BOOLEAN"};if(M(e.kind))return{value:this.advance().text,literalType:"DATE_LITERAL"};if(P(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,literalType:"DATE_N_LITERAL",dateLiteralVariable:n}}throw this.error(`Expected value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseArrayExpression(){this.expect(138);let e=[];for(e.push(this.parseArrayItem());this.match(143);)e.push(this.parseArrayItem());this.expect(139);let s=e.map(l=>l.value),t=e.map(l=>l.type),n=e.map(l=>l.variable!==void 0?l.variable:null),a=n.some(l=>l!==null),o;new Set(t).size===1?o=this.getLiteralType(this.tokenKindFromTypeName(t[0])):o=t.map(l=>this.getLiteralType(this.tokenKindFromTypeName(l)));let h={values:s,literalType:o};return a&&(h.dateLiteralVariable=n),h}tokenKindFromTypeName(e){for(let s=0;s<=160;s++)if(d(s)===e)return s;return 159}parseArrayItem(){let e=this.peek();if(e.kind===157)return{value:this.advance().text,type:d(157)};if(e.kind===158)return{value:this.advance().text,type:d(158)};if(e.kind===151)return{value:this.advance().text,type:d(151)};if(e.kind===152)return{value:this.advance().text,type:d(152)};if(q(e.kind)){let s=this.advance();return{value:s.text,type:d(s.kind)}}if(e.kind===149)return{value:this.advance().text,type:d(149)};if(e.kind===150)return{value:this.advance().text,type:d(150)};if(e.kind===18)return this.advance(),{value:"null",type:"NULL"};if(e.kind===24)return{value:this.advance().text,type:"TRUE"};if(e.kind===7)return{value:this.advance().text,type:"FALSE"};if(M(e.kind))return{value:this.advance().text,type:d(e.kind)};if(P(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,type:d(s.kind),variable:n}}throw this.error(`Expected array value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseApexBindVariable(){this.expect(144);let e=[];for(this.check(32)?e.push(this.parseApexBindNewInstantiation()):this.checkIdentifier()&&(this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):e.push(this.parseApexBindIdentifier()));this.check(142);)this.advance(),this.checkIdentifier()&&this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):this.checkIdentifier()&&e.push(this.parseApexBindIdentifier());return{value:e.join(".")}}parseApexBindIdentifier(){let e=this.consumeIdentifier().text;return this.check(140)&&(e+=this.parseApexBindArrayAccessor()),e}parseApexBindNewInstantiation(){this.expect(32);let s=`new ${this.consumeIdentifier().text}`;if(this.check(134)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);this.expect(136),s+=`<${t.join(", ")}>`}return s+=this.parseApexBindFunctionParams(),this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionCall(){let s=this.consumeIdentifier().text+this.parseApexBindFunctionParams();return this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionParams(){this.expect(138);let e=[];if(!this.check(139))for(e.push(this.consumeIdentifier().text);this.match(143);)e.push(this.consumeIdentifier().text);return this.expect(139),`(${e.join(", ")})`}parseApexBindArrayAccessor(){this.expect(140);let e=this.peek(),s;if(e.kind===156)s=this.advance().text;else if(I(e.kind))s=this.advance().text;else throw this.error(`Expected integer or identifier in array accessor at position ${e.start}`);return this.expect(141),`[${s}]`}parseAnyFunction(e){let s=this.peek();if(g(s.kind))return this.parseDateFunction(e);if(m(s.kind))return this.parseAggregateFunction(e);if(s.kind===68)return this.parseLocationFunction(e);if(j(s.kind))return this.parseFieldsFunction(e);if(x(s.kind))return this.parseOtherFunction(e);throw this.error(`Expected function name but found ${T[s.kind]} at position ${s.start}`)}parseDateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseAggregateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!0,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseFieldsFunction(e){this.advance(),this.expect(138);let s=this.peek(),t;if(te(s.kind))t=this.advance().text;else if(I(s.kind))t=this.advance().text;else throw this.error(`Expected FIELDS function parameter at position ${s.start}`);this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="FIELDS",n.parameters=[t],n.rawValue=`FIELDS(${t})`,n}parseOtherFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseLocationFunction(e){this.advance(),this.expect(138);let s=this.consumeIdentifier().text;this.expect(143);let t;this.check(69)&&this.peekAt(1).kind===138?t=this.parseGeolocationFunction(e):t=this.consumeIdentifier().text,this.expect(143);let n=this.expect(158).text;this.expect(139);let a={};e&&(a.type="FieldFunctionExpression"),a.functionName="DISTANCE",a.parameters=[s,t,n],e&&(a.isAggregateFn=!0);let o=typeof t=="string"?t:t.rawValue;return a.rawValue=`DISTANCE(${s}, ${o}, ${n})`,a}parseGeolocationFunction(e){this.advance(),this.expect(138);let s=this.parseNumberValue();this.expect(143);let t=this.parseNumberValue();this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="GEOLOCATION",n.parameters=[s,t],n.rawValue=`GEOLOCATION(${s}, ${t})`,n}parseNumberValue(){let e=this.peek();if(e.kind===156||e.kind===155||e.kind===154||e.kind===153)return this.advance().text;throw this.error(`Expected number but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseFunctionExpression(e,s=!0){this.expect(138);let t=[];if(!this.check(139))for(t.push(this.parseFunctionParam(e,s));this.match(143);)t.push(this.parseFunctionParam(e,s));return this.expect(139),t}parseFunctionParam(e,s){let t=this.peek(),n=this.peekAt(1);if(!e&&m(t.kind)&&n.kind===138)return this.parseAggregateFunction(s);if(x(t.kind)&&n.kind===138)return this.parseOtherFunction(s);if(t.kind===157)return this.advance().text;if(q(t.kind))return this.advance().text;if(I(t.kind))return this.advance().text;throw this.error(`Expected function parameter but found ${T[t.kind]} ("${t.text}") at position ${t.start}`)}parseGroupByClause(){this.expect(10);let e=[];for(e.push(this.parseGroupByItem());this.match(143);)e.push(this.parseGroupByItem());return e}parseGroupByItem(){let e=this.peek();return e.kind===4&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("CUBE")}:e.kind===22&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("ROLLUP")}:g(e.kind)&&this.peekAt(1).kind===138?{fn:this.parseDateFunction(!1)}:{field:this.consumeIdentifier().text}}parseCubeOrRollupFunction(e){let s=this.advance(),t=this.parseFunctionExpression(!1);return{functionName:e,parameters:t,rawValue:`${e}(${t.join(", ")})`}}parseHavingClause(){return this.expect(11),this.parseConditionExpressions(!0,!1)}parseOrderByClause(){this.expect(21);let e=[];for(e.push(this.parseOrderByItem());this.match(143);)e.push(this.parseOrderByItem());return e}parseOrderByItem(){let e=this.peek(),s=this.peekAt(1),t;if(e.kind===75&&s.kind===138){let n=this.advance(),a=this.parseFunctionExpression(!1);t={fn:{functionName:d(n.kind),parameters:a,rawValue:`${n.text}(${a.join(", ")})`}}}else m(e.kind)&&s.kind===138?t={fn:this.parseAggregateFunction(!1)}:g(e.kind)&&s.kind===138?t={fn:this.parseDateFunction(!1)}:e.kind===68&&s.kind===138?t={fn:this.parseLocationFunction(!1)}:t={field:this.consumeIdentifier().text};if(this.check(2)?t.order=d(this.advance().kind):this.check(3)&&(t.order=d(this.advance().kind)),this.check(19))if(this.advance(),this.check(8))t.nulls=d(this.advance().kind);else if(this.check(14))t.nulls=d(this.advance().kind);else throw this.error(`Expected FIRST or LAST after NULLS at position ${this.peek().start}`);return t}parseWithClause(){this.expect(27);let e=this.peek();if(e.kind===46)return this.advance(),{withSecurityEnforced:!0};if(e.kind===48)return this.advance(),{withAccessLevel:"USER_MODE"};if(e.kind===47)return this.advance(),{withAccessLevel:"SYSTEM_MODE"};if(e.kind===35)return{withDataCategory:this.parseWithDataCategory()};throw this.error(`Expected SECURITY_ENFORCED, USER_MODE, SYSTEM_MODE, or DATA CATEGORY after WITH at position ${e.start}`)}parseWithDataCategory(){this.expect(35);let e=[];for(e.push(this.parseDataCategoryCondition());this.check(0);)this.advance(),e.push(this.parseDataCategoryCondition());return{conditions:e}}parseDataCategoryCondition(){let e=this.consumeIdentifier().text,s=this.peek(),t;if(s.kind===33||s.kind===30||s.kind===34||s.kind===31)t=this.advance().text;else throw this.error(`Expected data category selector (AT, ABOVE, BELOW, ABOVE_OR_BELOW) at position ${s.start}`);let n=this.match(138),a=[];for(a.push(this.consumeIdentifier().text);this.match(143);)a.push(this.consumeIdentifier().text);return n&&this.expect(139),{groupName:e,selector:t,parameters:a}}},D=new z;function b(i,e){let s=e||{},t=se(i);D.init(t,s);let n;if(s.allowPartialQuery?n=D.parseSelectStatementPartial():n=D.parseSelectStatement(!1),!D.isAtEnd()&&!s.ignoreParseErrors){let a=t[D.getPos()];throw new Error(`Unexpected token "${a.text}" at position ${a.start}. Expected end of query.`)}return n}function ne(i,e){try{return b(i,e),!0}catch{return!1}}function f(i){return typeof i=="string"}function Ue(i){return i?i.every(e=>f(e)):!1}function L(i){return Number.isFinite(i)}function N(i){return i==null}function O(i,e,s){return N(i)?"":`${s||""}${i}${e||""}`}function ae(i,e=!1){return Array.isArray(i)?i.length>0?`(${i.join(", ")})`:e?"()":"":e?`(${i||""})`:i||""}function oe(i,e,s=""){return L(i)&&i>0?new Array(i).fill(e).join(s):""}function Z(i){return!i.parameters||i.parameters.length===0?[]:Ue(i.parameters)?i.parameters:f(i.parameters[0])?[i.parameters[0]]:Z(i.parameters[0])}function le(i){if(!i.parameters||i.parameters.length===0)return!1;let e=i.parameters[0];return f(e)?!1:!!e.isAggregateFn}function U(i){return i&&!N(i.alias)}function ue(i){return f(i.field)&&!Array.isArray(i.relationships)&&!Array.isArray(i.conditions)}function Ee(i){return!N(i.functionName||i.fn)}function he(i){return f(i.field)&&Array.isArray(i.relationships)}function pe(i){return!N(i.subquery)}function ce(i){return f(i.field)&&Array.isArray(i.conditions)}function F(i){return f(i.relationshipName)}function J(i){return!!i&&!!i.type&&i.type==="FieldSubquery"}function fe(i){return!!i&&!!i.operator&&!!i.right}function Ae(i){return!!i&&!!i.operator&&!!i.right}function v(i){return!!i&&!!i.operator&&!!i.right}function de(i){return i&&f(i.field)&&f(i.operator)&&!N(i.value)}function Ne(i){return i&&f(i.field)&&f(i.operator)&&!N(i.value)&&(i.literalType==="DATE_LITERAL"||Array.isArray(i.literalType)&&i.literalType[0]==="DATE_LITERAL")}function Re(i){return i&&f(i.field)&&f(i.operator)&&!N(i.value)&&!N(i.dateLiteralVariable)}function Y(i){return i&&!N(i.fn)&&f(i.operator)&&!N(i.value)}function y(i){return i&&L(i.openParen)&&N(i.operator)&&N(i.field)&&N(i.fn)&&N(i.closeParen)}function w(i){return i&&f(i.field)&&f(i.operator)&&!N(i.valueQuery)&&N(i.value)}function $(i){return i&&!N(i.field)}function Te(i){return i&&!N(i.fn)}function k(i){return i&&!N(i.field)}function Ce(i){return i&&!N(i.fn)}function ke(i){return["IN","NOT IN","INCLUDES","EXCLUDES"].includes(i)}function _e(i,e,s){if(N(e))return i;if(s&&e!=="APEX_BIND_VARIABLE"&&ke(s)&&!Array.isArray(i)&&(i=[i],e=Array.isArray(e)?e:[e]),Array.isArray(e)&&Array.isArray(i))return i.map((t,n)=>Ge(t,e?.[n]));switch(Array.isArray(e)&&(e=e[0]),e){case"STRING":return Array.isArray(i)?i.filter(Boolean).map(t=>f(t)&&t.startsWith("'")?t:`'${t??""}'`):(i=String(i??""),f(i)&&i.startsWith("'")?i:`'${i??""}'`);case"APEX_BIND_VARIABLE":return`:${i}`;default:return i}}function Ge(i,e){return e==="STRING"?f(i)&&i.startsWith("'")?i:`'${i??""}'`:i}function Pe(i){return Se(i)}function Se(i){if(typeof i=="string")return{type:"Field",field:i};if(Ee(i)){let e=[];return i.parameters&&(e=Array.isArray(i.parameters)?i.parameters:[i.parameters]),{type:"FieldFunctionExpression",functionName:i.functionName||i.fn,parameters:e,alias:i.alias}}else{if(he(i))return{type:"FieldRelationship",field:i.field,relationships:i.relationships,objectPrefix:i.objectPrefix};if(pe(i))return{type:"FieldSubquery",subquery:i.subquery};if(ce(i))return{type:"FieldTypeof",field:i.field,conditions:i.conditions};if(ue(i))return{type:"Field",field:i.field,objectPrefix:i.objectPrefix};throw new TypeError("The input object provided did not match any valid field types")}}function ve(i,e){if(!i)return[];i=J(i)?i.subquery:i;let s=i.fields;if(!s)return[];let t={};i.groupBy&&(t=(Array.isArray(i.groupBy)?i.groupBy:[i.groupBy]).reduce((l,E)=>(k(E)&&(l[E.field.toLocaleLowerCase()]=E.field),l),{}));let n=-1,a=(F(i)?i.relationshipName:i.sObject||"").toLowerCase(),o=(i.sObjectAlias||"").toLowerCase();return s.flatMap(l=>{switch(l.type){case"Field":return l.alias||l.field;case"FieldFunctionExpression":{let E=Z(l);if(E=E.map(u=>{if(u.includes(".")){let R=u.split("."),A=R[0].toLowerCase();return(A===o||A===a)&&(R=R.slice(1)),R.join(".")}return u}),l.alias&&(l.isAggregateFn||e))return l.alias;if(l.alias){let u=E[0];return u.includes(".")?(E=u.split(".").slice(0,-1),E.push(l.alias),E.join(".")):l.alias}return l.isAggregateFn||le(l)||e?(n++,`expr${n}`):E.length>0?E.join("."):l.functionName}case"FieldRelationship":{let E=l.relationships[0].toLowerCase();return U(l)?l.alias:l.rawValue&&t[l.rawValue.toLocaleLowerCase()]?l.field:E===o||E===a?l.relationships.concat([l.field]).slice(1).join("."):l.relationships.concat([l.field]).join(".")}case"FieldSubquery":return l.subquery.relationshipName;case"FieldTypeof":{let E=new Set,u=[];return l.conditions.forEach(R=>{R.fieldList.forEach(A=>{E.has(A)||(E.add(A),u.push(`${l.field}.${A}`))})}),u}default:break}}).filter(l=>f(l))}var W=class{constructor(e,s){this.currIndent=1;this.enabled=e,this.options={numIndent:s.numIndent??1,fieldMaxLineLength:s.fieldMaxLineLength??60,fieldSubqueryParensOnOwnLine:s.fieldSubqueryParensOnOwnLine??!0,whereClauseOperatorsIndented:!0,newLineAfterKeywords:s.newLineAfterKeywords??!1,logging:s.logging??!1},this.options.newLineAfterKeywords&&(this.options.fieldSubqueryParensOnOwnLine=!0)}log(e){this.options.logging&&console.log(e)}getIndent(e=0){return this.repeatChar((this.currIndent+e)*(this.options.numIndent||1)," ")}repeatChar(e,s){return new Array(e).fill(s).join("")}setSubquery(e){this.currIndent=e?this.currIndent+=1:this.currIndent-=1}stepCurrIndex(e){this.currIndent+=e}formatFields(e){function s(t){e.fields[t-1]&&(e.fields[t-1].suffix=e.fields[t-1].suffix.trim())}if(e.fields.forEach((t,n)=>{t.suffix=e.fields.length-1===n?"":", "}),this.enabled){let t=0,n=!1;e.fields.forEach((a,o)=>{a.isSubquery?(s(o),a.prefix=`
1
+ "use strict";var B=Object.defineProperty;var Ie=Object.getOwnPropertyDescriptor;var Le=Object.getOwnPropertyNames;var Oe=Object.prototype.hasOwnProperty;var Fe=(i,e)=>{for(var s in e)B(i,s,{get:e[s],enumerable:!0})},ge=(i,e,s,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Le(e))!Oe.call(i,n)&&n!==s&&B(i,n,{get:()=>e[n],enumerable:!(t=Ie(e,n))||t.enumerable});return i};var me=i=>ge(B({},"__esModule",{value:!0}),i);var Ye={};Fe(Ye,{Compose:()=>W,composeQuery:()=>Se,formatQuery:()=>ve,getComposedField:()=>Me,getField:()=>_e,getFlattenedFields:()=>Pe,hasAlias:()=>b,isFieldSubquery:()=>Z,isGroupByField:()=>U,isGroupByFn:()=>Te,isHavingClauseWithRightCondition:()=>fe,isNegationCondition:()=>y,isOrderByField:()=>w,isOrderByFn:()=>Re,isQueryValid:()=>se,isString:()=>f,isSubquery:()=>F,isValueCondition:()=>Ae,isValueFunctionCondition:()=>v,isValueQueryCondition:()=>Y,isValueWithDateLiteralCondition:()=>de,isValueWithDateNLiteralCondition:()=>Ne,isWhereClauseWithRightCondition:()=>ce,isWhereOrHavingClauseWithRightCondition:()=>P,parseQuery:()=>D});module.exports=me(Ye);var T=(r=>(r[r.AND=0]="AND",r[r.AS=1]="AS",r[r.ASC=2]="ASC",r[r.DESC=3]="DESC",r[r.CUBE=4]="CUBE",r[r.ELSE=5]="ELSE",r[r.EXCLUDES=6]="EXCLUDES",r[r.FALSE=7]="FALSE",r[r.FIRST=8]="FIRST",r[r.FROM=9]="FROM",r[r.GROUP_BY=10]="GROUP_BY",r[r.HAVING=11]="HAVING",r[r.IN=12]="IN",r[r.INCLUDES=13]="INCLUDES",r[r.LAST=14]="LAST",r[r.LIKE=15]="LIKE",r[r.LIMIT=16]="LIMIT",r[r.NOT=17]="NOT",r[r.NULL=18]="NULL",r[r.NULLS=19]="NULLS",r[r.OR=20]="OR",r[r.ORDER_BY=21]="ORDER_BY",r[r.ROLLUP=22]="ROLLUP",r[r.SELECT=23]="SELECT",r[r.TRUE=24]="TRUE",r[r.USING=25]="USING",r[r.WHERE=26]="WHERE",r[r.WITH=27]="WITH",r[r.FOR=28]="FOR",r[r.UPDATE=29]="UPDATE",r[r.ABOVE=30]="ABOVE",r[r.ABOVE_OR_BELOW=31]="ABOVE_OR_BELOW",r[r.APEX_NEW=32]="APEX_NEW",r[r.AT=33]="AT",r[r.BELOW=34]="BELOW",r[r.DATA_CATEGORY=35]="DATA_CATEGORY",r[r.END=36]="END",r[r.OFFSET=37]="OFFSET",r[r.REFERENCE=38]="REFERENCE",r[r.SCOPE=39]="SCOPE",r[r.TRACKING=40]="TRACKING",r[r.THEN=41]="THEN",r[r.TYPEOF=42]="TYPEOF",r[r.VIEW=43]="VIEW",r[r.VIEWSTAT=44]="VIEWSTAT",r[r.WHEN=45]="WHEN",r[r.SECURITY_ENFORCED=46]="SECURITY_ENFORCED",r[r.SYSTEM_MODE=47]="SYSTEM_MODE",r[r.USER_MODE=48]="USER_MODE",r[r.CALENDAR_MONTH=49]="CALENDAR_MONTH",r[r.CALENDAR_QUARTER=50]="CALENDAR_QUARTER",r[r.CALENDAR_YEAR=51]="CALENDAR_YEAR",r[r.DAY_IN_MONTH=52]="DAY_IN_MONTH",r[r.DAY_IN_WEEK=53]="DAY_IN_WEEK",r[r.DAY_IN_YEAR=54]="DAY_IN_YEAR",r[r.DAY_ONLY=55]="DAY_ONLY",r[r.FISCAL_MONTH=56]="FISCAL_MONTH",r[r.FISCAL_QUARTER=57]="FISCAL_QUARTER",r[r.FISCAL_YEAR=58]="FISCAL_YEAR",r[r.HOUR_IN_DAY=59]="HOUR_IN_DAY",r[r.WEEK_IN_MONTH=60]="WEEK_IN_MONTH",r[r.WEEK_IN_YEAR=61]="WEEK_IN_YEAR",r[r.AVG=62]="AVG",r[r.COUNT=63]="COUNT",r[r.COUNT_DISTINCT=64]="COUNT_DISTINCT",r[r.MIN=65]="MIN",r[r.MAX=66]="MAX",r[r.SUM=67]="SUM",r[r.DISTANCE=68]="DISTANCE",r[r.GEOLOCATION=69]="GEOLOCATION",r[r.FIELDS=70]="FIELDS",r[r.FORMAT=71]="FORMAT",r[r.TOLABEL=72]="TOLABEL",r[r.CONVERT_TIMEZONE=73]="CONVERT_TIMEZONE",r[r.CONVERT_CURRENCY=74]="CONVERT_CURRENCY",r[r.GROUPING=75]="GROUPING",r[r.ALL=76]="ALL",r[r.CUSTOM=77]="CUSTOM",r[r.STANDARD=78]="STANDARD",r[r.YESTERDAY=79]="YESTERDAY",r[r.TODAY=80]="TODAY",r[r.TOMORROW=81]="TOMORROW",r[r.LAST_WEEK=82]="LAST_WEEK",r[r.THIS_WEEK=83]="THIS_WEEK",r[r.NEXT_WEEK=84]="NEXT_WEEK",r[r.LAST_MONTH=85]="LAST_MONTH",r[r.THIS_MONTH=86]="THIS_MONTH",r[r.NEXT_MONTH=87]="NEXT_MONTH",r[r.LAST_90_DAYS=88]="LAST_90_DAYS",r[r.NEXT_90_DAYS=89]="NEXT_90_DAYS",r[r.THIS_QUARTER=90]="THIS_QUARTER",r[r.LAST_QUARTER=91]="LAST_QUARTER",r[r.NEXT_QUARTER=92]="NEXT_QUARTER",r[r.THIS_YEAR=93]="THIS_YEAR",r[r.LAST_YEAR=94]="LAST_YEAR",r[r.NEXT_YEAR=95]="NEXT_YEAR",r[r.THIS_FISCAL_QUARTER=96]="THIS_FISCAL_QUARTER",r[r.LAST_FISCAL_QUARTER=97]="LAST_FISCAL_QUARTER",r[r.NEXT_FISCAL_QUARTER=98]="NEXT_FISCAL_QUARTER",r[r.THIS_FISCAL_YEAR=99]="THIS_FISCAL_YEAR",r[r.LAST_FISCAL_YEAR=100]="LAST_FISCAL_YEAR",r[r.NEXT_FISCAL_YEAR=101]="NEXT_FISCAL_YEAR",r[r.NEXT_N_DAYS=102]="NEXT_N_DAYS",r[r.LAST_N_DAYS=103]="LAST_N_DAYS",r[r.N_DAYS_AGO=104]="N_DAYS_AGO",r[r.NEXT_N_WEEKS=105]="NEXT_N_WEEKS",r[r.LAST_N_WEEKS=106]="LAST_N_WEEKS",r[r.N_WEEKS_AGO=107]="N_WEEKS_AGO",r[r.NEXT_N_MONTHS=108]="NEXT_N_MONTHS",r[r.LAST_N_MONTHS=109]="LAST_N_MONTHS",r[r.N_MONTHS_AGO=110]="N_MONTHS_AGO",r[r.NEXT_N_QUARTERS=111]="NEXT_N_QUARTERS",r[r.LAST_N_QUARTERS=112]="LAST_N_QUARTERS",r[r.N_QUARTERS_AGO=113]="N_QUARTERS_AGO",r[r.NEXT_N_YEARS=114]="NEXT_N_YEARS",r[r.LAST_N_YEARS=115]="LAST_N_YEARS",r[r.N_YEARS_AGO=116]="N_YEARS_AGO",r[r.NEXT_N_FISCAL_QUARTERS=117]="NEXT_N_FISCAL_QUARTERS",r[r.LAST_N_FISCAL_QUARTERS=118]="LAST_N_FISCAL_QUARTERS",r[r.N_FISCAL_QUARTERS_AGO=119]="N_FISCAL_QUARTERS_AGO",r[r.NEXT_N_FISCAL_YEARS=120]="NEXT_N_FISCAL_YEARS",r[r.LAST_N_FISCAL_YEARS=121]="LAST_N_FISCAL_YEARS",r[r.N_FISCAL_YEARS_AGO=122]="N_FISCAL_YEARS_AGO",r[r.DELEGATED=123]="DELEGATED",r[r.EVERYTHING=124]="EVERYTHING",r[r.MINE_AND_MY_GROUPS=125]="MINE_AND_MY_GROUPS",r[r.MINE=126]="MINE",r[r.MY_TERRITORY=127]="MY_TERRITORY",r[r.MY_TEAM_TERRITORY=128]="MY_TEAM_TERRITORY",r[r.TEAM=129]="TEAM",r[r.ALL_PRIVATE=130]="ALL_PRIVATE",r[r.MRU=131]="MRU",r[r.EQUAL=132]="EQUAL",r[r.NOT_EQUAL=133]="NOT_EQUAL",r[r.LESS_THAN=134]="LESS_THAN",r[r.LESS_THAN_OR_EQUAL=135]="LESS_THAN_OR_EQUAL",r[r.GREATER_THAN=136]="GREATER_THAN",r[r.GREATER_THAN_OR_EQUAL=137]="GREATER_THAN_OR_EQUAL",r[r.L_PAREN=138]="L_PAREN",r[r.R_PAREN=139]="R_PAREN",r[r.L_SQUARE_BRACKET=140]="L_SQUARE_BRACKET",r[r.R_SQUARE_BRACKET=141]="R_SQUARE_BRACKET",r[r.DOT=142]="DOT",r[r.COMMA=143]="COMMA",r[r.COLON=144]="COLON",r[r.SEMICOLON=145]="SEMICOLON",r[r.ASTERISK=146]="ASTERISK",r[r.PLUS=147]="PLUS",r[r.MINUS=148]="MINUS",r[r.DATETIME=149]="DATETIME",r[r.DATE_TOKEN=150]="DATE_TOKEN",r[r.CURRENCY_PREFIXED_DECIMAL=151]="CURRENCY_PREFIXED_DECIMAL",r[r.CURRENCY_PREFIXED_INTEGER=152]="CURRENCY_PREFIXED_INTEGER",r[r.SIGNED_DECIMAL=153]="SIGNED_DECIMAL",r[r.UNSIGNED_DECIMAL=154]="UNSIGNED_DECIMAL",r[r.SIGNED_INTEGER=155]="SIGNED_INTEGER",r[r.UNSIGNED_INTEGER=156]="UNSIGNED_INTEGER",r[r.STRING_LITERAL=157]="STRING_LITERAL",r[r.GEOLOCATION_UNIT=158]="GEOLOCATION_UNIT",r[r.IDENTIFIER=159]="IDENTIFIER",r[r.EOF=160]="EOF",r))(T||{});function g(i){return i>=49&&i<=61}function m(i){return i>=62&&i<=67}function Q(i){return i>=68&&i<=69}function X(i){return i===70}function x(i){return i>=71&&i<=75}function ee(i){return i>=76&&i<=78}function G(i){return i>=79&&i<=101}function M(i){return i>=102&&i<=122}function te(i){return i>=123&&i<=131}function I(i){return i===159||i>=30&&i<=48||i>=49&&i<=61||i>=62&&i<=67||i>=68&&i<=69||i===70||i>=71&&i<=75||i>=76&&i<=78||i>=79&&i<=101||i>=102&&i<=122||i>=123&&i<=131||i===158||i===152}function j(i){return i===153||i===154||i===155||i===156}function ie(i){return i>=132&&i<=137}var ye=new Map([["AND",0],["AS",1],["ASC",2],["DESC",3],["CUBE",4],["ELSE",5],["EXCLUDES",6],["FALSE",7],["FIRST",8],["FROM",9],["HAVING",11],["IN",12],["INCLUDES",13],["LAST",14],["LIKE",15],["LIMIT",16],["NOT",17],["NULL",18],["NULLS",19],["OR",20],["ROLLUP",22],["SELECT",23],["TRUE",24],["USING",25],["WHERE",26],["WITH",27],["FOR",28],["UPDATE",29],["ABOVE_OR_BELOW",31],["ABOVE",30],["NEW",32],["AT",33],["BELOW",34],["END",36],["OFFSET",37],["REFERENCE",38],["SCOPE",39],["TRACKING",40],["THEN",41],["TYPEOF",42],["VIEW",43],["VIEWSTAT",44],["WHEN",45],["SECURITY_ENFORCED",46],["SYSTEM_MODE",47],["USER_MODE",48],["CALENDAR_MONTH",49],["CALENDAR_QUARTER",50],["CALENDAR_YEAR",51],["DAY_IN_MONTH",52],["DAY_IN_WEEK",53],["DAY_IN_YEAR",54],["DAY_ONLY",55],["FISCAL_MONTH",56],["FISCAL_QUARTER",57],["FISCAL_YEAR",58],["HOUR_IN_DAY",59],["WEEK_IN_MONTH",60],["WEEK_IN_YEAR",61],["AVG",62],["COUNT_DISTINCT",64],["COUNT",63],["MIN",65],["MAX",66],["SUM",67],["DISTANCE",68],["GEOLOCATION",69],["FIELDS",70],["FORMAT",71],["TOLABEL",72],["CONVERTTIMEZONE",73],["CONVERTCURRENCY",74],["GROUPING",75],["ALL",76],["CUSTOM",77],["STANDARD",78],["YESTERDAY",79],["TODAY",80],["TOMORROW",81],["LAST_WEEK",82],["THIS_WEEK",83],["NEXT_WEEK",84],["LAST_MONTH",85],["THIS_MONTH",86],["NEXT_MONTH",87],["LAST_90_DAYS",88],["NEXT_90_DAYS",89],["THIS_QUARTER",90],["LAST_QUARTER",91],["NEXT_QUARTER",92],["THIS_YEAR",93],["LAST_YEAR",94],["NEXT_YEAR",95],["THIS_FISCAL_QUARTER",96],["LAST_FISCAL_QUARTER",97],["NEXT_FISCAL_QUARTER",98],["THIS_FISCAL_YEAR",99],["LAST_FISCAL_YEAR",100],["NEXT_FISCAL_YEAR",101],["NEXT_N_DAYS",102],["LAST_N_DAYS",103],["N_DAYS_AGO",104],["NEXT_N_WEEKS",105],["LAST_N_WEEKS",106],["N_WEEKS_AGO",107],["NEXT_N_MONTHS",108],["LAST_N_MONTHS",109],["N_MONTHS_AGO",110],["NEXT_N_QUARTERS",111],["LAST_N_QUARTERS",112],["N_QUARTERS_AGO",113],["NEXT_N_YEARS",114],["LAST_N_YEARS",115],["N_YEARS_AGO",116],["NEXT_N_FISCAL_QUARTERS",117],["LAST_N_FISCAL_QUARTERS",118],["N_FISCAL_QUARTERS_AGO",119],["NEXT_N_FISCAL_YEARS",120],["LAST_N_FISCAL_YEARS",121],["N_FISCAL_YEARS_AGO",122],["DELEGATED",123],["EVERYTHING",124],["MINEANDMYGROUPS",125],["MINE",126],["MY_TERRITORY",127],["MY_TEAM_TERRITORY",128],["TEAM",129],["ALLPRIVATE",130],["MRU",131]]),c=[];(function(){for(let e of Object.keys(T)){let s=T[e];typeof s=="number"&&(c[s]=e)}c[72]="toLabel",c[73]="convertTimezone",c[74]="convertCurrency",c[157]="StringIdentifier",c[150]="DATE",c[32]="new",c[125]="MineAndMyGroups",c[127]="MyTerritory",c[128]="MyTeamTerritory",c[130]="AllPrivate",c[123]="Delegated",c[124]="Everything",c[126]="Mine",c[129]="Team",c[131]="Mru",c[159]="Identifier",c[132]="EQUAL",c[156]="UNSIGNED_INTEGER",c[155]="SIGNED_INTEGER",c[154]="UNSIGNED_DECIMAL",c[153]="SIGNED_DECIMAL",c[151]="CURRENCY_PREFIXED_DECIMAL",c[152]="CURRENCY_PREFIXED_INTEGER"})();function d(i){return c[i]||T[i]}function q(i){return i>=65&&i<=90||i>=97&&i<=122}function p(i){return i>=48&&i<=57}function V(i){return q(i)||p(i)||i===95}function k(i){return i===32||i===9||i===10||i===13||i===12}function re(i){let e=[],s=i.length,t=0;for(;t<s;){let n=i.charCodeAt(t);if(k(n)){for(t++;t<s&&k(i.charCodeAt(t));)t++;continue}if(n===39){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(E===92){t+=2;continue}if(E===39){t++;break}t++}let o=i.substring(a,t),l=o.substring(1,o.length-1).toUpperCase();l==="MI"||l==="KM"?e.push({kind:158,text:o,start:a}):e.push({kind:157,text:o,start:a});continue}if(p(n)){if(t+9<s&&i.charCodeAt(t+4)===45&&i.charCodeAt(t+7)===45){let o=!0;for(let h of[0,1,2,3,5,6,8,9])if(!p(i.charCodeAt(t+h))){o=!1;break}if(o)if(t+10<s&&(i.charCodeAt(t+10)===84||i.charCodeAt(t+10)===116)){let h=t;for(t+=11;t<s;){let l=i.charCodeAt(t);if(p(l)||l===58)t++;else break}if(t<s&&i.charCodeAt(t)===46)for(t++;t<s&&p(i.charCodeAt(t));)t++;if(t<s){let l=i.charCodeAt(t);if(l===90||l===122)t++;else if(l===46){for(t++;t<s&&p(i.charCodeAt(t));)t++;t<s&&(i.charCodeAt(t)===90||i.charCodeAt(t)===122)&&t++}else if(l===43||l===45)for(t++;t<s;){let E=i.charCodeAt(t);if(p(E)||E===58)t++;else break}}e.push({kind:149,text:i.substring(h,t),start:h});continue}else{let h=t;t+=10,e.push({kind:150,text:i.substring(h,t),start:h});continue}}let a=t;for(;t<s&&p(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&p(i.charCodeAt(t+1))){for(t++;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:156,text:i.substring(a,t),start:a});continue}if(n===33){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:133,text:"!=",start:t}),t+=2;continue}throw new Error(`Unexpected character '!' at position ${t}`)}if(n===60){if(t+1<s){let a=i.charCodeAt(t+1);if(a===61){e.push({kind:135,text:"<=",start:t}),t+=2;continue}if(a===62){e.push({kind:133,text:"<>",start:t}),t+=2;continue}}e.push({kind:134,text:"<",start:t}),t++;continue}if(n===62){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:137,text:">=",start:t}),t+=2;continue}e.push({kind:136,text:">",start:t}),t++;continue}if(n===61){e.push({kind:132,text:"=",start:t}),t++;continue}if(n===40){e.push({kind:138,text:"(",start:t}),t++;continue}if(n===41){e.push({kind:139,text:")",start:t}),t++;continue}if(n===91){e.push({kind:140,text:"[",start:t}),t++;continue}if(n===93){e.push({kind:141,text:"]",start:t}),t++;continue}if(n===44){e.push({kind:143,text:",",start:t}),t++;continue}if(n===58){e.push({kind:144,text:":",start:t}),t++;continue}if(n===59){e.push({kind:145,text:";",start:t}),t++;continue}if(n===42){e.push({kind:146,text:"*",start:t}),t++;continue}if(n===43||n===45){if(t+1<s){let a=i.charCodeAt(t+1);if(p(a)){let o=t;for(t++;t<s&&p(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&p(i.charCodeAt(t+1))){for(t++;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}e.push({kind:155,text:i.substring(o,t),start:o});continue}if(a===46&&t+2<s&&p(i.charCodeAt(t+2))){let o=t;for(t+=2;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}}e.push({kind:n===43?147:148,text:i[t],start:t}),t++;continue}if(n===46){if(t+1<s&&p(i.charCodeAt(t+1))){let a=t;for(t++;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:142,text:".",start:t}),t++;continue}if(q(n)){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(V(E)||E===46)t++;else break}let o=i.substring(a,t);if(o.includes(".."))throw new Error(`Invalid identifier with consecutive dots at position ${a}: "${o}"`);let h=o.toUpperCase();if(h==="ORDER"&&!o.includes(".")){let E=H(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:21,text:u,start:a}),t=E;continue}}if(h==="GROUP"&&!o.includes(".")){let E=H(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:10,text:u,start:a}),t=E;continue}}if(h==="DATA"&&!o.includes(".")){let E=H(i,t,"CATEGORY");if(E!==-1){let u=i.substring(a,E);e.push({kind:35,text:u,start:a}),t=E;continue}}if(o.length>=4){let E=o.substring(0,3);if(J(E)){let u=o.substring(3);if(xe(u)){u.includes(".")?e.push({kind:151,text:o,start:a}):e.push({kind:152,text:o,start:a});continue}}}if(o.length===3&&J(o)){let E=t,u=t,R=!1;if(u<s&&i.charCodeAt(u)===45&&(R=!0,u++),R&&u<s&&p(i.charCodeAt(u))){for(;u<s&&p(i.charCodeAt(u));)u++;if(u<s&&i.charCodeAt(u)===46&&u+1<s&&p(i.charCodeAt(u+1))){for(u++;u<s&&p(i.charCodeAt(u));)u++;let A=i.substring(a,u);e.push({kind:151,text:A,start:a}),t=u;continue}if(u>=s||!V(i.charCodeAt(u))&&i.charCodeAt(u)!==46){let A=i.substring(a,u);e.push({kind:152,text:A,start:a}),t=u;continue}t=E}}if(o.includes(".")){e.push({kind:159,text:o,start:a});continue}let l=ye.get(h);if(l!==void 0){e.push({kind:l,text:o,start:a});continue}e.push({kind:159,text:o,start:a});continue}throw new Error(`Unexpected character '${i[t]}' (code ${n}) at position ${t}`)}return e.push({kind:160,text:"",start:t}),e}function H(i,e,s){let t=i.length,n=e;if(n>=t||!k(i.charCodeAt(n)))return-1;for(;n<t&&k(i.charCodeAt(n));)n++;let a=s.length;if(n+a>t)return-1;for(let h=0;h<a;h++){let l=i.charCodeAt(n+h),E=s.charCodeAt(h);if((l>=97&&l<=122?l-32:l)!==E)return-1}let o=n+a;if(o<t){let h=i.charCodeAt(o);if(V(h)||h===46)return-1}return o}function J(i){for(let e=0;e<i.length;e++)if(!q(i.charCodeAt(e)))return!1;return!0}function xe(i){if(i.length===0)return!1;let e=0;if(i.charCodeAt(e)===45&&e++,e>=i.length||!p(i.charCodeAt(e)))return!1;for(;e<i.length&&p(i.charCodeAt(e));)e++;if(e<i.length&&i.charCodeAt(e)===46){if(e++,e>=i.length||!p(i.charCodeAt(e)))return!1;for(;e<i.length&&p(i.charCodeAt(e));)e++}return e===i.length}var De=new Set([23,9,26,21,10,11,16,27,28,29,25,19,2,3,0,20,17,45,41,5,36,42,143,139,160]),K=class i{constructor(e,s){this.pos=0;this.tokens=e,this.config=s}getPos(){return this.pos}peek(){return this.tokens[this.pos]}peekAt(e){let s=this.pos+e;return s>=this.tokens.length?this.tokens[this.tokens.length-1]:this.tokens[s]}advance(){let e=this.tokens[this.pos];return e.kind!==160&&this.pos++,e}expect(e){let s=this.peek();if(s.kind!==e)throw this.error(`Expected ${T[e]} but found ${T[s.kind]} ("${s.text}") at position ${s.start}`);return this.advance()}match(e){return this.peek().kind===e?(this.advance(),!0):!1}check(e){return this.peek().kind===e}isAtEnd(){return this.peek().kind===160}error(e){return this.config.logErrors&&console.log(e),new Error(e)}static{this.CLAUSE_BOUNDARY_KINDS=new Set([26,21,10,11,16,37,27,28,29,25,160])}isAtClauseBoundary(){let e=this.peek().kind;return i.CLAUSE_BOUNDARY_KINDS.has(e)||e===139}synchronize(){for(;!this.isAtEnd();){if(this.isAtClauseBoundary())return;this.advance()}}consumeIdentifier(){let e=this.peek();if(I(e.kind))return this.advance();throw this.error(`Expected identifier but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}checkIdentifier(){return I(this.peek().kind)}isFunctionCall(){let e=this.peek(),s=this.peekAt(1);return(g(e.kind)||m(e.kind)||Q(e.kind)||X(e.kind)||x(e.kind))&&s.kind===138}canBeAlias(){let e=this.peek();return!(!I(e.kind)||De.has(e.kind)||e.kind===37&&this.peekAt(1).kind===156)}getLiteralType(e){let s=d(e);return s==="UNSIGNED_INTEGER"||s==="SIGNED_INTEGER"?"INTEGER":s==="UNSIGNED_DECIMAL"||s==="SIGNED_DECIMAL"?"DECIMAL":s==="CURRENCY_PREFIXED_DECIMAL"?"DECIMAL_WITH_CURRENCY_PREFIX":s==="CURRENCY_PREFIXED_INTEGER"?"INTEGER_WITH_CURRENCY_PREFIX":s==="DATETIME"?"DATETIME":s==="DATE"?"DATE":s==="NULL"?"NULL":s==="StringIdentifier"||s==="Identifier"?"STRING":s==="TRUE"||s==="FALSE"?"BOOLEAN":G(e)?"DATE_LITERAL":M(e)?"DATE_N_LITERAL":"STRING"}parseSelectStatement(e=!1){let s=this.parseSelectClause(),t=this.parseFromClause(),n;e?(n={fields:s,relationshipName:t.sObject},t.sObjectPrefix&&(n.sObjectPrefix=t.sObjectPrefix)):n={fields:s,sObject:t.sObject},t.alias&&(n.sObjectAlias=t.alias),this.applySObjectAlias(n);let a=this.parseClauseStatements();return Object.assign(n,a),n}parseSelectStatementPartial(){let e,s;this.check(23)&&(e=this.parseSelectClause()),this.check(9)&&(s=this.parseFromClause());let t={};e&&(t.fields=e),s&&(t.sObject=s.sObject,s.alias&&(t.sObjectAlias=s.alias)),t.fields&&this.applySObjectAlias(t);let n=this.parseClauseStatements();return Object.assign(t,n),t}applySObjectAlias(e){if(!e.sObjectAlias||!Array.isArray(e.fields))return;let s=e.sObjectAlias;e.fields.forEach(t=>{t.relationships&&t.relationships[0]===s&&(t.relationships=t.relationships.slice(1),t.objectPrefix=s,t.relationships.length===0&&(delete t.relationships,t.type="Field"))})}parseSelectClause(){this.expect(23);let e=[];for(e.push(this.parseSelectField());this.match(143);)e.push(this.parseSelectField());return e}parseSelectField(){let e=this.peek(),s=this.peekAt(1);return e.kind===138&&s.kind===23?this.parseSelectClauseSubqueryIdentifier():e.kind===42?this.parseSelectClauseTypeOf():this.isFunctionCall()?this.parseSelectClauseFunctionIdentifier():this.parseSelectClauseIdentifier()}parseSelectClauseFunctionIdentifier(){let e=this.parseAnyFunction(!0);return this.canBeAlias()&&(e.alias=this.consumeIdentifier().text),e}parseSelectClauseSubqueryIdentifier(){this.expect(138);let e=this.parseSelectStatement(!0);return this.expect(139),{type:"FieldSubquery",subquery:e}}parseSelectClauseTypeOf(){this.expect(42);let e=this.consumeIdentifier().text,s=[];for(;this.check(45);){this.advance();let t=this.consumeIdentifier().text;this.expect(41);let n=[];for(n.push(this.consumeIdentifier().text);this.match(143);)n.push(this.consumeIdentifier().text);s.push({type:"WHEN",objectType:t,fieldList:n})}if(this.check(5)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);s.push({type:"ELSE",fieldList:t})}return this.expect(36),{type:"FieldTypeof",field:e,conditions:s}}parseSelectClauseIdentifier(){let s=this.consumeIdentifier().text,t;if(!s.includes("."))t={type:"Field",field:s};else{let n=s.split(".");t={type:"FieldRelationship",field:n[n.length-1],relationships:n.slice(0,n.length-1),rawValue:s}}return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseFromClause(){this.expect(9);let s=this.consumeIdentifier().text,t;if(s.includes(".")){let n=s.split(".");t={sObjectPrefix:n.slice(0,n.length-1),sObject:n[n.length-1]}}else t={sObject:s};return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseClauseStatements(){let e={},s=(t,n)=>{if(this.config.ignoreParseErrors)try{t(),this.isAtClauseBoundary()||(n&&n(),this.synchronize())}catch(a){this.config.logErrors&&console.log(a),n&&n(),this.synchronize()}else t()};for(this.check(25)&&s(()=>{e.usingScope=this.parseUsingScope()}),this.check(26)&&s(()=>{e.where=this.parseWhereClause()},()=>{delete e.where});this.check(27);)s(()=>{let t=this.parseWithClause();t.withSecurityEnforced&&(e.withSecurityEnforced=!0),t.withAccessLevel&&(e.withAccessLevel=t.withAccessLevel),t.withDataCategory&&(e.withDataCategory=t.withDataCategory)});return this.check(10)&&(s(()=>{e.groupBy=this.parseGroupByClause()}),this.check(11)&&s(()=>{e.having=this.parseHavingClause()})),this.check(21)&&s(()=>{e.orderBy=this.parseOrderByClause()}),this.check(16)&&s(()=>{this.advance(),e.limit=Number(this.expect(156).text)}),this.check(37)&&s(()=>{this.advance(),e.offset=Number(this.expect(156).text)}),this.check(28)&&s(()=>{this.advance();let t=this.peek();if(t.kind===43||t.kind===38||t.kind===29)e.for=d(this.advance().kind);else throw this.error(`Expected VIEW, REFERENCE, or UPDATE after FOR at position ${t.start}`)}),this.check(29)&&s(()=>{this.advance();let t=this.peek();if(t.kind===40||t.kind===44)e.update=d(this.advance().kind);else throw this.error(`Expected TRACKING or VIEWSTAT after UPDATE at position ${t.start}`)}),e}parseUsingScope(){this.expect(25),this.expect(39);let e=this.peek();if(te(e.kind))return this.advance().text;if(I(e.kind))return this.advance().text;throw this.error(`Expected scope enumeration value at position ${e.start}`)}parseWhereClause(){return this.expect(26),this.parseConditionExpressions(!0,!0)}parseConditionExpressions(e,s){let t,n,a={left:0,right:0};for(;;){let o;if(t)if(this.check(0))o="AND",this.advance();else if(this.check(20))o="OR",this.advance();else break;let h={},l=h,E=null;for(;this.hasNegationPrefix();){let A=0;for(;this.check(138)&&this.isNegationAhead();)this.advance(),A++,a.left++;if(this.check(17)){this.advance();let _={left:A>0?{openParen:A}:null,operator:"NOT",right:{left:{}}};E?E.right=_:h=_,E=_,l=_.right}else break}l.left=this.parseExpression(e,s,a),t?o&&(n.operator=o,n.right=h):(t=h,n=h);let u=h;for(;u.right&&u.right.left!==void 0;){let A=u.right;if(A.right&&A.right.left!==void 0)u=A;else{u=A;break}}if(n=u,this.isAtEnd())break;let R=this.peek().kind;if(R!==0&&R!==20)break}if(a.left!==a.right)throw this.error(`Mismatched parentheses in WHERE/HAVING clause: ${a.left} open vs ${a.right} close`);return t}hasNegationPrefix(){let e=0;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}isNegationAhead(){let e=1;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}parseExpression(e,s,t){let n=0;for(;this.check(138)&&!this.isSubqueryStart()&&!this.isArrayExpressionStart();)this.advance(),n++,t&&t.left++;let a,o=!1;e&&m(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseAggregateFunction(!1),o=!0):s&&Q(this.peek().kind)&&this.peek().kind===68&&this.peekAt(1).kind===138?(a=this.parseLocationFunction(!1),o=!0):g(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseDateFunction(!1),o=!0):x(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseOtherFunction(!1),o=!0):a=this.consumeIdentifier().text;let h=this.parseOperator(),l=h.operator,E=h.isSet,u,R,A;if(E)if(this.check(138)&&this.peekAt(1).kind===23)this.expect(138),u=this.parseSelectStatement(!1),this.expect(139),R="SUBQUERY";else if(this.config.allowApexBindVariables&&this.check(144))u=this.parseApexBindVariable().value,R="APEX_BIND_VARIABLE";else{let S=this.parseArrayExpression();u=S.values,R=S.literalType,A=S.dateLiteralVariable}else{let S=this.parseAtomicValue();u=S.value,R=S.literalType,A=S.dateLiteralVariable}let _=0;for(;this.check(139)&&(!t||t.left>t.right);)this.advance(),_++,t&&t.right++;let C={};return o?C.fn=a:C.field=a,C.operator=l,R==="SUBQUERY"?C.valueQuery=u:C.value=u,C.literalType=R,A!==void 0&&(C.dateLiteralVariable=A),n>0&&(C.openParen=n),_>0&&(C.closeParen=_),C}isSubqueryStart(){return this.check(138)&&this.peekAt(1).kind===23}isArrayExpressionStart(){return!1}parseOperator(){let e=this.peek();if(ie(e.kind))return{operator:this.advance().text,isSet:!1};if(e.kind===15)return this.advance(),{operator:"LIKE",isSet:!1};if(e.kind===12)return this.advance(),{operator:"IN",isSet:!0};if(e.kind===17&&this.peekAt(1).kind===12)return this.advance(),this.advance(),{operator:"NOT IN",isSet:!0};if(e.kind===13)return this.advance(),{operator:"INCLUDES",isSet:!0};if(e.kind===6)return this.advance(),{operator:"EXCLUDES",isSet:!0};throw this.error(`Expected operator but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseAtomicValue(){let e=this.peek();if(this.config.allowApexBindVariables&&e.kind===144)return{value:this.parseApexBindVariable().value,literalType:"APEX_BIND_VARIABLE"};if(e.kind===157)return{value:this.advance().text,literalType:"STRING"};if(e.kind===158)return{value:this.advance().text,literalType:"STRING"};if(e.kind===156)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===155)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===154)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===153)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===151)return{value:this.advance().text,literalType:"DECIMAL_WITH_CURRENCY_PREFIX"};if(e.kind===152)return{value:this.advance().text,literalType:"INTEGER_WITH_CURRENCY_PREFIX"};if(e.kind===149)return{value:this.advance().text,literalType:"DATETIME"};if(e.kind===150)return{value:this.advance().text,literalType:"DATE"};if(e.kind===18)return this.advance(),{value:"NULL",literalType:"NULL"};if(e.kind===24||e.kind===7)return{value:d(this.advance().kind),literalType:"BOOLEAN"};if(G(e.kind))return{value:this.advance().text,literalType:"DATE_LITERAL"};if(M(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,literalType:"DATE_N_LITERAL",dateLiteralVariable:n}}throw this.error(`Expected value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseArrayExpression(){this.expect(138);let e=[];for(e.push(this.parseArrayItem());this.match(143);)e.push(this.parseArrayItem());this.expect(139);let s=e.map(l=>l.value),t=e.map(l=>l.type),n=e.map(l=>l.variable!==void 0?l.variable:null),a=n.some(l=>l!==null),o;new Set(t).size===1?o=this.getLiteralType(this.tokenKindFromTypeName(t[0])):o=t.map(l=>this.getLiteralType(this.tokenKindFromTypeName(l)));let h={values:s,literalType:o};return a&&(h.dateLiteralVariable=n),h}tokenKindFromTypeName(e){for(let s=0;s<=160;s++)if(d(s)===e)return s;return 159}parseArrayItem(){let e=this.peek();if(e.kind===157)return{value:this.advance().text,type:d(157)};if(e.kind===158)return{value:this.advance().text,type:d(158)};if(e.kind===151)return{value:this.advance().text,type:d(151)};if(e.kind===152)return{value:this.advance().text,type:d(152)};if(j(e.kind)){let s=this.advance();return{value:s.text,type:d(s.kind)}}if(e.kind===149)return{value:this.advance().text,type:d(149)};if(e.kind===150)return{value:this.advance().text,type:d(150)};if(e.kind===18)return this.advance(),{value:"NULL",type:"NULL"};if(e.kind===24)return{value:this.advance().text,type:"TRUE"};if(e.kind===7)return{value:this.advance().text,type:"FALSE"};if(G(e.kind))return{value:this.advance().text,type:d(e.kind)};if(M(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,type:d(s.kind),variable:n}}throw this.error(`Expected array value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseApexBindVariable(){this.expect(144);let e=[];for(this.check(32)?e.push(this.parseApexBindNewInstantiation()):this.checkIdentifier()&&(this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):e.push(this.parseApexBindIdentifier()));this.check(142);)this.advance(),this.checkIdentifier()&&this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):this.checkIdentifier()&&e.push(this.parseApexBindIdentifier());return{value:e.join(".")}}parseApexBindIdentifier(){let e=this.consumeIdentifier().text;return this.check(140)&&(e+=this.parseApexBindArrayAccessor()),e}parseApexBindNewInstantiation(){this.expect(32);let s=`new ${this.consumeIdentifier().text}`;if(this.check(134)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);this.expect(136),s+=`<${t.join(", ")}>`}return s+=this.parseApexBindFunctionParams(),this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionCall(){let s=this.consumeIdentifier().text+this.parseApexBindFunctionParams();return this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionParams(){this.expect(138);let e=[];if(!this.check(139))for(e.push(this.consumeIdentifier().text);this.match(143);)e.push(this.consumeIdentifier().text);return this.expect(139),`(${e.join(", ")})`}parseApexBindArrayAccessor(){this.expect(140);let e=this.peek(),s;if(e.kind===156)s=this.advance().text;else if(I(e.kind))s=this.advance().text;else throw this.error(`Expected integer or identifier in array accessor at position ${e.start}`);return this.expect(141),`[${s}]`}parseAnyFunction(e){let s=this.peek();if(g(s.kind))return this.parseDateFunction(e);if(m(s.kind))return this.parseAggregateFunction(e);if(s.kind===68)return this.parseLocationFunction(e);if(X(s.kind))return this.parseFieldsFunction(e);if(x(s.kind))return this.parseOtherFunction(e);throw this.error(`Expected function name but found ${T[s.kind]} at position ${s.start}`)}parseDateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseAggregateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!0,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseFieldsFunction(e){this.advance(),this.expect(138);let s=this.peek(),t;if(ee(s.kind))t=this.advance().text;else if(I(s.kind))t=this.advance().text;else throw this.error(`Expected FIELDS function parameter at position ${s.start}`);this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="FIELDS",n.parameters=[t],n.rawValue=`FIELDS(${t})`,n}parseOtherFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseLocationFunction(e){this.advance(),this.expect(138);let s=this.consumeIdentifier().text;this.expect(143);let t;this.check(69)&&this.peekAt(1).kind===138?t=this.parseGeolocationFunction(e):t=this.consumeIdentifier().text,this.expect(143);let n=this.expect(158).text;this.expect(139);let a={};e&&(a.type="FieldFunctionExpression"),a.functionName="DISTANCE",a.parameters=[s,t,n],e&&(a.isAggregateFn=!0);let o=typeof t=="string"?t:t.rawValue;return a.rawValue=`DISTANCE(${s}, ${o}, ${n})`,a}parseGeolocationFunction(e){this.advance(),this.expect(138);let s=this.parseNumberValue();this.expect(143);let t=this.parseNumberValue();this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="GEOLOCATION",n.parameters=[s,t],n.rawValue=`GEOLOCATION(${s}, ${t})`,n}parseNumberValue(){let e=this.peek();if(e.kind===156||e.kind===155||e.kind===154||e.kind===153)return this.advance().text;throw this.error(`Expected number but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseFunctionExpression(e,s=!0){this.expect(138);let t=[];if(!this.check(139))for(t.push(this.parseFunctionParam(e,s));this.match(143);)t.push(this.parseFunctionParam(e,s));return this.expect(139),t}parseFunctionParam(e,s){let t=this.peek(),n=this.peekAt(1);if(!e&&m(t.kind)&&n.kind===138)return this.parseAggregateFunction(s);if(x(t.kind)&&n.kind===138)return this.parseOtherFunction(s);if(t.kind===157)return this.advance().text;if(j(t.kind))return this.advance().text;if(I(t.kind))return this.advance().text;throw this.error(`Expected function parameter but found ${T[t.kind]} ("${t.text}") at position ${t.start}`)}parseGroupByClause(){this.expect(10);let e=[];for(e.push(this.parseGroupByItem());this.match(143);)e.push(this.parseGroupByItem());return e}parseGroupByItem(){let e=this.peek();return e.kind===4&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("CUBE")}:e.kind===22&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("ROLLUP")}:g(e.kind)&&this.peekAt(1).kind===138?{fn:this.parseDateFunction(!1)}:{field:this.consumeIdentifier().text}}parseCubeOrRollupFunction(e){let s=this.advance(),t=this.parseFunctionExpression(!1);return{functionName:e,parameters:t,rawValue:`${e}(${t.join(", ")})`}}parseHavingClause(){return this.expect(11),this.parseConditionExpressions(!0,!1)}parseOrderByClause(){this.expect(21);let e=[];for(e.push(this.parseOrderByItem());this.match(143);)e.push(this.parseOrderByItem());return e}parseOrderByItem(){let e=this.peek(),s=this.peekAt(1),t;if(e.kind===75&&s.kind===138){let n=this.advance(),a=this.parseFunctionExpression(!1);t={fn:{functionName:d(n.kind),parameters:a,rawValue:`${n.text}(${a.join(", ")})`}}}else m(e.kind)&&s.kind===138?t={fn:this.parseAggregateFunction(!1)}:g(e.kind)&&s.kind===138?t={fn:this.parseDateFunction(!1)}:e.kind===68&&s.kind===138?t={fn:this.parseLocationFunction(!1)}:t={field:this.consumeIdentifier().text};if(this.check(2)?t.order=d(this.advance().kind):this.check(3)&&(t.order=d(this.advance().kind)),this.check(19))if(this.advance(),this.check(8))t.nulls=d(this.advance().kind);else if(this.check(14))t.nulls=d(this.advance().kind);else throw this.error(`Expected FIRST or LAST after NULLS at position ${this.peek().start}`);return t}parseWithClause(){this.expect(27);let e=this.peek();if(e.kind===46)return this.advance(),{withSecurityEnforced:!0};if(e.kind===48)return this.advance(),{withAccessLevel:"USER_MODE"};if(e.kind===47)return this.advance(),{withAccessLevel:"SYSTEM_MODE"};if(e.kind===35)return{withDataCategory:this.parseWithDataCategory()};throw this.error(`Expected SECURITY_ENFORCED, USER_MODE, SYSTEM_MODE, or DATA CATEGORY after WITH at position ${e.start}`)}parseWithDataCategory(){this.expect(35);let e=[];for(e.push(this.parseDataCategoryCondition());this.check(0);)this.advance(),e.push(this.parseDataCategoryCondition());return{conditions:e}}parseDataCategoryCondition(){let e=this.consumeIdentifier().text,s=this.peek(),t;if(s.kind===33||s.kind===30||s.kind===34||s.kind===31)t=this.advance().text;else throw this.error(`Expected data category selector (AT, ABOVE, BELOW, ABOVE_OR_BELOW) at position ${s.start}`);let n=this.match(138),a=[];for(a.push(this.consumeIdentifier().text);this.match(143);)a.push(this.consumeIdentifier().text);return n&&this.expect(139),{groupName:e,selector:t,parameters:a}}};function D(i,e){let s=e||{},t=re(i),n=new K(t,s),a;if(s.allowPartialQuery?a=n.parseSelectStatementPartial():a=n.parseSelectStatement(!1),!n.isAtEnd()&&!s.ignoreParseErrors){let o=t[n.getPos()];throw new Error(`Unexpected token "${o.text}" at position ${o.start}. Expected end of query.`)}return a}function se(i,e){try{return D(i,e),!0}catch{return!1}}function f(i){return typeof i=="string"}function be(i){return i?i.every(e=>f(e)):!1}function L(i){return Number.isFinite(i)}function N(i){return i==null}function O(i,e,s){return N(i)?"":`${s||""}${i}${e||""}`}function ne(i,e=!1){return Array.isArray(i)?i.length>0?`(${i.join(", ")})`:e?"()":"":e?`(${i||""})`:i||""}function ae(i,e,s=""){return L(i)&&i>0?new Array(i).fill(e).join(s):""}function z(i){return!i.parameters||i.parameters.length===0?[]:be(i.parameters)?i.parameters:f(i.parameters[0])?[i.parameters[0]]:z(i.parameters[0])}function oe(i){if(!i.parameters||i.parameters.length===0)return!1;let e=i.parameters[0];return f(e)?!1:!!e.isAggregateFn}function b(i){return i&&!N(i.alias)}function le(i){return f(i.field)&&!Array.isArray(i.relationships)&&!Array.isArray(i.conditions)}function ue(i){return!N(i.functionName||i.fn)}function Ee(i){return f(i.field)&&Array.isArray(i.relationships)}function he(i){return!N(i.subquery)}function pe(i){return f(i.field)&&Array.isArray(i.conditions)}function F(i){return f(i.relationshipName)}function Z(i){return!!i&&!!i.type&&i.type==="FieldSubquery"}function ce(i){return!!i&&!!i.operator&&!!i.right}function fe(i){return!!i&&!!i.operator&&!!i.right}function P(i){return!!i&&!!i.operator&&!!i.right}function Ae(i){return i&&f(i.field)&&f(i.operator)&&!N(i.value)}function de(i){return i&&f(i.field)&&f(i.operator)&&!N(i.value)&&(i.literalType==="DATE_LITERAL"||Array.isArray(i.literalType)&&i.literalType[0]==="DATE_LITERAL")}function Ne(i){return i&&f(i.field)&&f(i.operator)&&!N(i.value)&&!N(i.dateLiteralVariable)}function v(i){return i&&!N(i.fn)&&f(i.operator)&&!N(i.value)}function y(i){return i&&L(i.openParen)&&N(i.operator)&&N(i.field)&&N(i.fn)&&N(i.closeParen)}function Y(i){return i&&f(i.field)&&f(i.operator)&&!N(i.valueQuery)&&N(i.value)}function w(i){return i&&!N(i.field)}function Re(i){return i&&!N(i.fn)}function U(i){return i&&!N(i.field)}function Te(i){return i&&!N(i.fn)}function Ue(i){return["IN","NOT IN","INCLUDES","EXCLUDES"].includes(i)}function Ce(i,e,s){if(N(e))return i;if(s&&e!=="APEX_BIND_VARIABLE"&&Ue(s)&&!Array.isArray(i)&&(i=[i],e=Array.isArray(e)?e:[e]),Array.isArray(e)&&Array.isArray(i))return i.map((t,n)=>ke(t,e?.[n]));switch(Array.isArray(e)&&(e=e[0]),e){case"STRING":return Array.isArray(i)?i.filter(Boolean).map(t=>f(t)&&t.startsWith("'")?t:`'${t??""}'`):(i=String(i??""),f(i)&&i.startsWith("'")?i:`'${i??""}'`);case"APEX_BIND_VARIABLE":return`:${i}`;default:return i}}function ke(i,e){switch(e){case"STRING":return f(i)&&i.startsWith("'")?i:`'${i??""}'`;case"NULL":return"null";default:return i}}function Me(i){return _e(i)}function _e(i){if(typeof i=="string")return{type:"Field",field:i};if(ue(i)){let e=[];return i.parameters&&(e=Array.isArray(i.parameters)?i.parameters:[i.parameters]),{type:"FieldFunctionExpression",functionName:i.functionName||i.fn,parameters:e,alias:i.alias}}else{if(Ee(i))return{type:"FieldRelationship",field:i.field,relationships:i.relationships,objectPrefix:i.objectPrefix};if(he(i))return{type:"FieldSubquery",subquery:i.subquery};if(pe(i))return{type:"FieldTypeof",field:i.field,conditions:i.conditions};if(le(i))return{type:"Field",field:i.field,objectPrefix:i.objectPrefix};throw new TypeError("The input object provided did not match any valid field types")}}function Pe(i,e){if(!i)return[];i=Z(i)?i.subquery:i;let s=i.fields;if(!s)return[];let t={};i.groupBy&&(t=(Array.isArray(i.groupBy)?i.groupBy:[i.groupBy]).reduce((l,E)=>(U(E)&&(l[E.field.toLocaleLowerCase()]=E.field),l),{}));let n=-1,a=(F(i)?i.relationshipName:i.sObject||"").toLowerCase(),o=(i.sObjectAlias||"").toLowerCase();return s.flatMap(l=>{switch(l.type){case"Field":return l.alias||l.field;case"FieldFunctionExpression":{let E=z(l);if(E=E.map(u=>{if(u.includes(".")){let R=u.split("."),A=R[0].toLowerCase();return(A===o||A===a)&&(R=R.slice(1)),R.join(".")}return u}),l.alias&&(l.isAggregateFn||e))return l.alias;if(l.alias){let u=E[0];return u.includes(".")?(E=u.split(".").slice(0,-1),E.push(l.alias),E.join(".")):l.alias}return l.isAggregateFn||oe(l)||e?(n++,`expr${n}`):E.length>0?E.join("."):l.functionName}case"FieldRelationship":{let E=l.relationships[0].toLowerCase();return b(l)?l.alias:l.rawValue&&t[l.rawValue.toLocaleLowerCase()]?l.field:E===o||E===a?l.relationships.concat([l.field]).slice(1).join("."):l.relationships.concat([l.field]).join(".")}case"FieldSubquery":return l.subquery.relationshipName;case"FieldTypeof":{let E=new Set,u=[];return l.conditions.forEach(R=>{R.fieldList.forEach(A=>{E.has(A)||(E.add(A),u.push(`${l.field}.${A}`))})}),u}default:break}}).filter(l=>f(l))}var $=class{constructor(e,s){this.currIndent=1;this.enabled=e,this.options={numIndent:s.numIndent??1,fieldMaxLineLength:s.fieldMaxLineLength??60,fieldSubqueryParensOnOwnLine:s.fieldSubqueryParensOnOwnLine??!0,whereClauseOperatorsIndented:!0,newLineAfterKeywords:s.newLineAfterKeywords??!1,logging:s.logging??!1},this.options.newLineAfterKeywords&&(this.options.fieldSubqueryParensOnOwnLine=!0)}log(e){this.options.logging&&console.log(e)}getIndent(e=0){return this.repeatChar((this.currIndent+e)*(this.options.numIndent||1)," ")}repeatChar(e,s){return new Array(e).fill(s).join("")}setSubquery(e){this.currIndent=e?this.currIndent+=1:this.currIndent-=1}stepCurrIndex(e){this.currIndent+=e}formatFields(e){function s(t){e.fields[t-1]&&(e.fields[t-1].suffix=e.fields[t-1].suffix.trim())}if(e.fields.forEach((t,n)=>{t.suffix=e.fields.length-1===n?"":", "}),this.enabled){let t=0,n=!1;e.fields.forEach((a,o)=>{a.isSubquery?(s(o),a.prefix=`
2
2
  ${this.getIndent()}`,a.suffix=e.fields.length-1===o?"":", ",t=0,n=!0):Array.isArray(a.typeOfClause)?(s(o),a.prefix=`
3
3
  ${this.getIndent()}`,n=!0):L(this.options.fieldMaxLineLength)&&(t+=a.text.length+a.suffix.length,(t>this.options.fieldMaxLineLength||n)&&(s(o),(!this.options.newLineAfterKeywords||o>0)&&(a.prefix+=`
4
4
  ${this.getIndent()}`),t=0,n=!1)),this.log(a)})}}formatTyeOfField(e,s){return this.enabled&&this.options.newLineAfterKeywords?s.map((t,n)=>n===0?t:n===s.length-1?`${this.getIndent()}${t}`:`${this.getIndent()} ${t}`).join(`
@@ -18,6 +18,6 @@ ${e}`:` ${e}`}formatText(e){return this.enabled&&(this.options.newLineAfterKeywo
18
18
  `,s=0):t+=`${n}${o?", ":""}`}),t}else return e.join(", ")}formatParens(e,s,t=!1){let n="";if(L(e)&&e>0)if(this.enabled)if(s==="("){for(let a=0;a<e;a++)t&&a===e-1?n+="(":a===0?n+=`(
19
19
  `:(this.currIndent++,n+=`${this.getIndent()}(
20
20
  `);(!t||e>1)&&this.currIndent++}else for(let a=e-1;a>=0;a--)t&&a===e-1?n+=")":(this.currIndent--,n+=`
21
- ${this.getIndent()})`);else n+=oe(e,s);return n}formatWhereClauseOperators(e,s,t=0){let n=e==="NOT";return this.enabled&&!n?`
21
+ ${this.getIndent()})`);else n+=ae(e,s);return n}formatWhereClauseOperators(e,s,t=0){let n=e==="NOT";return this.enabled&&!n?`
22
22
  ${this.getIndent(t)}${e} ${s}`:`${n?"":" "}${e} ${s}`}formatAddNewLine(e=" ",s){return this.enabled&&!s?`
23
- `:e}};function Ye(i,e,s){return Ie(b(i,s),{format:!0,formatOptions:e})}function Ie(i,e={}){if(!i)return"";e=e||{},e.format=!!e.format,e.logging&&(console.time("composer"),console.log("Composing Query:",i),console.log("Format output:",e.format));let s=new B(i,e).query;return e.logging&&console.timeEnd("composer"),s}var B=class{constructor(e,s={}){this.soql=e;this.logging=!1;this.format=!1;s={autoCompose:!0,...s};let{logging:t,format:n}=s;this.logging=!!t,this.format=!!n,this.query="",this.formatter=new W(this.format,{logging:this.logging,...s.formatOptions}),s.autoCompose&&this.start()}start(){this.query=this.parseQuery(this.soql)}log(e){this.logging&&console.log("Current SOQL:",e)}parseFn(e){let s;return e.rawValue?s=e.rawValue:(s=e.functionName,s+=`(${(e.parameters||[]).map(t=>f(t)?t:this.parseFn(t)).join(", ")})`),e.alias&&(s+=` ${e.alias}`),s}parseQuery(e){let s={fields:this.parseFields(e.fields||[]).map(a=>({text:a.text,typeOfClause:a.typeOfClause,isSubquery:a.text.startsWith("("),prefix:"",suffix:""})),isSubquery:F(e),lineBreaks:[]},t="";e.fields&&(t+=this.formatter.formatClause("SELECT").trimStart()),this.formatter.formatFields(s);let n="";if(s.fields.forEach(a=>{Array.isArray(a.typeOfClause)?n+=`${a.prefix}${this.formatter.formatTyeOfField(a.text,a.typeOfClause)}${a.suffix}`:n+=`${a.prefix}${a.text}${a.suffix}`}),t+=this.formatter.formatText(n),(F(e)?e.relationshipName:e.sObject)&&(t+=this.formatter.formatClause("FROM")),F(e)){let a=e.sObjectPrefix||[];a.push(e.relationshipName),t+=this.formatter.formatText(`${a.join(".")}${O(e.sObjectAlias,""," ")}`)}else e.sObject&&(t+=this.formatter.formatText(`${e.sObject}${O(e.sObjectAlias,""," ")}`));return this.log(t),e.usingScope&&(t+=this.formatter.formatClause("USING SCOPE"),t+=this.formatter.formatText(e.usingScope),this.log(t)),e.where&&(t+=this.formatter.formatClause("WHERE"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.where)),this.log(t)),e.groupBy&&(t+=this.formatter.formatClause("GROUP BY"),t+=this.formatter.formatText(this.parseGroupByClause(e.groupBy)),this.log(t),e.having&&(t+=this.formatter.formatClause("HAVING"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.having)),this.log(t))),e.orderBy&&(!Array.isArray(e.orderBy)||e.orderBy.length>0)&&(t+=this.formatter.formatClause("ORDER BY"),t+=this.formatter.formatText(this.parseOrderBy(e.orderBy)),this.log(t)),L(e.limit)&&(t+=this.formatter.formatClause("LIMIT"),t+=this.formatter.formatText(`${e.limit}`),this.log(t)),L(e.offset)&&(t+=this.formatter.formatClause("OFFSET"),t+=this.formatter.formatText(`${e.offset}`),this.log(t)),e.withDataCategory&&(t+=this.formatter.formatClause("WITH DATA CATEGORY"),t+=this.formatter.formatText(this.parseWithDataCategory(e.withDataCategory)),this.log(t)),e.withSecurityEnforced&&(t+=this.formatter.formatClause("WITH SECURITY_ENFORCED"),this.log(t)),e.withAccessLevel&&(t+=this.formatter.formatClause(`WITH ${e.withAccessLevel}`),this.log(t)),e.for&&(t+=this.formatter.formatClause("FOR"),t+=this.formatter.formatText(e.for),this.log(t)),e.update&&(t+=this.formatter.formatClause("UPDATE"),t+=this.formatter.formatText(e.update),this.log(t)),t.trim()}parseFields(e){return e.map(s=>{let t="",n,a=s.objectPrefix?`${s.objectPrefix}.`:"";switch(s.type){case"Field":{t=`${a}${s.field}${s.alias?` ${s.alias}`:""}`;break}case"FieldFunctionExpression":{let o="";s.parameters&&(o=s.parameters.map(h=>f(h)?h:this.parseFields([h]).map(l=>l.text)).join(", ")),t=`${s.functionName}(${o})${s.alias?` ${s.alias}`:""}`;break}case"FieldRelationship":{t=`${a}${s.relationships.join(".")}.${s.field}${U(s)?` ${s.alias}`:""}`;break}case"FieldSubquery":{t=this.formatter.formatSubquery(this.parseQuery(s.subquery));break}case"FieldTypeof":{n=this.parseTypeOfField(s),t=n.join(" ");break}default:break}return{text:t,typeOfClause:n}})}parseTypeOfField(e){let s=[`TYPEOF ${e.field}`].concat(e.conditions.map(t=>this.formatter.formatTypeofFieldCondition(t)));return s.push("END"),s}parseWhereOrHavingClause(e,s=0,t=!1){let n="",a=e.left,o=!1;if(a&&(n+=this.formatter.formatParens(a.openParen,"(",y(a)),!y(a))){s=s+(a.openParen||0)-(a.closeParen||0),t&&s++;let h="";h+=Y(a)?this.parseFn(a.fn):a.field,h+=` ${a.operator} `,w(a)?h+=this.formatter.formatSubquery(this.parseQuery(a.valueQuery),1,!0):h+=ae(_e(a.value,a.literalType,a.operator)),n+=this.formatter.formatWithIndent(h),n+=this.formatter.formatParens(a.closeParen,")",t)}if(v(e)){let h=O(e.operator);o=h==="NOT";let l=this.formatter.formatWhereClauseOperators(h,this.parseWhereOrHavingClause(e.right,s,y(a)),s);return`${o?n.trimRight():n}${l}`.trim()}else return n.trim()}parseGroupByClause(e){return(Array.isArray(e)?e:[e]).map(s=>k(s)?s.field:this.parseFn(s.fn)).join(", ")}parseOrderBy(e){if(Array.isArray(e))return this.formatter.formatOrderByArray(e.map(s=>this.parseOrderBy(s)));{let s="";return $(e)?s=`${O(e.field," ")}`:s+=`${this.parseFn(e.fn)} `,s+=`${O(e.order," ")}${O(e.nulls,"","NULLS ")}`,s.trim()}}parseWithDataCategory(e){return e.conditions.map(s=>{let t=s.parameters.length>1?`(${s.parameters.join(", ")})`:`${s.parameters.join(", ")}`;return`${s.groupName} ${s.selector} ${t}`}).join(" AND ")}};
23
+ `:e}};function ve(i,e,s){return Se(D(i,s),{format:!0,formatOptions:e})}function Se(i,e={}){if(!i)return"";e=e||{},e.format=!!e.format,e.logging&&(console.time("composer"),console.log("Composing Query:",i),console.log("Format output:",e.format));let s=new W(i,e).query;return e.logging&&console.timeEnd("composer"),s}var W=class{constructor(e,s={}){this.soql=e;this.logging=!1;this.format=!1;s={autoCompose:!0,...s};let{logging:t,format:n}=s;this.logging=!!t,this.format=!!n,this.query="",this.formatter=new $(this.format,{logging:this.logging,...s.formatOptions}),s.autoCompose&&this.start()}start(){this.query=this.parseQuery(this.soql)}log(e){this.logging&&console.log("Current SOQL:",e)}parseFn(e){let s;return e.rawValue?s=e.rawValue:(s=e.functionName,s+=`(${(e.parameters||[]).map(t=>f(t)?t:this.parseFn(t)).join(", ")})`),e.alias&&(s+=` ${e.alias}`),s}parseQuery(e){let s={fields:this.parseFields(e.fields||[]).map(a=>({text:a.text,typeOfClause:a.typeOfClause,isSubquery:a.text.startsWith("("),prefix:"",suffix:""})),isSubquery:F(e),lineBreaks:[]},t="";e.fields&&(t+=this.formatter.formatClause("SELECT").trimStart()),this.formatter.formatFields(s);let n="";if(s.fields.forEach(a=>{Array.isArray(a.typeOfClause)?n+=`${a.prefix}${this.formatter.formatTyeOfField(a.text,a.typeOfClause)}${a.suffix}`:n+=`${a.prefix}${a.text}${a.suffix}`}),t+=this.formatter.formatText(n),(F(e)?e.relationshipName:e.sObject)&&(t+=this.formatter.formatClause("FROM")),F(e)){let a=e.sObjectPrefix||[];a.push(e.relationshipName),t+=this.formatter.formatText(`${a.join(".")}${O(e.sObjectAlias,""," ")}`)}else e.sObject&&(t+=this.formatter.formatText(`${e.sObject}${O(e.sObjectAlias,""," ")}`));return this.log(t),e.usingScope&&(t+=this.formatter.formatClause("USING SCOPE"),t+=this.formatter.formatText(e.usingScope),this.log(t)),e.where&&(t+=this.formatter.formatClause("WHERE"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.where)),this.log(t)),e.groupBy&&(t+=this.formatter.formatClause("GROUP BY"),t+=this.formatter.formatText(this.parseGroupByClause(e.groupBy)),this.log(t),e.having&&(t+=this.formatter.formatClause("HAVING"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.having)),this.log(t))),e.orderBy&&(!Array.isArray(e.orderBy)||e.orderBy.length>0)&&(t+=this.formatter.formatClause("ORDER BY"),t+=this.formatter.formatText(this.parseOrderBy(e.orderBy)),this.log(t)),L(e.limit)&&(t+=this.formatter.formatClause("LIMIT"),t+=this.formatter.formatText(`${e.limit}`),this.log(t)),L(e.offset)&&(t+=this.formatter.formatClause("OFFSET"),t+=this.formatter.formatText(`${e.offset}`),this.log(t)),e.withDataCategory&&(t+=this.formatter.formatClause("WITH DATA CATEGORY"),t+=this.formatter.formatText(this.parseWithDataCategory(e.withDataCategory)),this.log(t)),e.withSecurityEnforced&&(t+=this.formatter.formatClause("WITH SECURITY_ENFORCED"),this.log(t)),e.withAccessLevel&&(t+=this.formatter.formatClause(`WITH ${e.withAccessLevel}`),this.log(t)),e.for&&(t+=this.formatter.formatClause("FOR"),t+=this.formatter.formatText(e.for),this.log(t)),e.update&&(t+=this.formatter.formatClause("UPDATE"),t+=this.formatter.formatText(e.update),this.log(t)),t.trim()}parseFields(e){return e.map(s=>{let t="",n,a=s.objectPrefix?`${s.objectPrefix}.`:"";switch(s.type){case"Field":{t=`${a}${s.field}${s.alias?` ${s.alias}`:""}`;break}case"FieldFunctionExpression":{let o="";s.parameters&&(o=s.parameters.map(h=>f(h)?h:this.parseFields([h]).map(l=>l.text)).join(", ")),t=`${s.functionName}(${o})${s.alias?` ${s.alias}`:""}`;break}case"FieldRelationship":{t=`${a}${s.relationships.join(".")}.${s.field}${b(s)?` ${s.alias}`:""}`;break}case"FieldSubquery":{t=this.formatter.formatSubquery(this.parseQuery(s.subquery));break}case"FieldTypeof":{n=this.parseTypeOfField(s),t=n.join(" ");break}default:break}return{text:t,typeOfClause:n}})}parseTypeOfField(e){let s=[`TYPEOF ${e.field}`].concat(e.conditions.map(t=>this.formatter.formatTypeofFieldCondition(t)));return s.push("END"),s}parseWhereOrHavingClause(e,s=0,t=!1){let n="",a=e.left,o=!1;if(a&&(n+=this.formatter.formatParens(a.openParen,"(",y(a)),!y(a))){s=s+(a.openParen||0)-(a.closeParen||0),t&&s++;let h="";h+=v(a)?this.parseFn(a.fn):a.field,h+=` ${a.operator} `,Y(a)?h+=this.formatter.formatSubquery(this.parseQuery(a.valueQuery),1,!0):h+=ne(Ce(a.value,a.literalType,a.operator)),n+=this.formatter.formatWithIndent(h),n+=this.formatter.formatParens(a.closeParen,")",t)}if(P(e)){let h=O(e.operator);o=h==="NOT";let l=this.formatter.formatWhereClauseOperators(h,this.parseWhereOrHavingClause(e.right,s,y(a)),s);return`${o?n.trimRight():n}${l}`.trim()}else return n.trim()}parseGroupByClause(e){return(Array.isArray(e)?e:[e]).map(s=>U(s)?s.field:this.parseFn(s.fn)).join(", ")}parseOrderBy(e){if(Array.isArray(e))return this.formatter.formatOrderByArray(e.map(s=>this.parseOrderBy(s)));{let s="";return w(e)?s=`${O(e.field," ")}`:s+=`${this.parseFn(e.fn)} `,s+=`${O(e.order," ")}${O(e.nulls,"","NULLS ")}`,s.trim()}}parseWithDataCategory(e){return e.conditions.map(s=>{let t=s.parameters.length>1?`(${s.parameters.join(", ")})`:`${s.parameters.join(", ")}`;return`${s.groupName} ${s.selector} ${t}`}).join(" AND ")}};
package/dist/cli/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var R=(i=>(i[i.AND=0]="AND",i[i.AS=1]="AS",i[i.ASC=2]="ASC",i[i.DESC=3]="DESC",i[i.CUBE=4]="CUBE",i[i.ELSE=5]="ELSE",i[i.EXCLUDES=6]="EXCLUDES",i[i.FALSE=7]="FALSE",i[i.FIRST=8]="FIRST",i[i.FROM=9]="FROM",i[i.GROUP_BY=10]="GROUP_BY",i[i.HAVING=11]="HAVING",i[i.IN=12]="IN",i[i.INCLUDES=13]="INCLUDES",i[i.LAST=14]="LAST",i[i.LIKE=15]="LIKE",i[i.LIMIT=16]="LIMIT",i[i.NOT=17]="NOT",i[i.NULL=18]="NULL",i[i.NULLS=19]="NULLS",i[i.OR=20]="OR",i[i.ORDER_BY=21]="ORDER_BY",i[i.ROLLUP=22]="ROLLUP",i[i.SELECT=23]="SELECT",i[i.TRUE=24]="TRUE",i[i.USING=25]="USING",i[i.WHERE=26]="WHERE",i[i.WITH=27]="WITH",i[i.FOR=28]="FOR",i[i.UPDATE=29]="UPDATE",i[i.ABOVE=30]="ABOVE",i[i.ABOVE_OR_BELOW=31]="ABOVE_OR_BELOW",i[i.APEX_NEW=32]="APEX_NEW",i[i.AT=33]="AT",i[i.BELOW=34]="BELOW",i[i.DATA_CATEGORY=35]="DATA_CATEGORY",i[i.END=36]="END",i[i.OFFSET=37]="OFFSET",i[i.REFERENCE=38]="REFERENCE",i[i.SCOPE=39]="SCOPE",i[i.TRACKING=40]="TRACKING",i[i.THEN=41]="THEN",i[i.TYPEOF=42]="TYPEOF",i[i.VIEW=43]="VIEW",i[i.VIEWSTAT=44]="VIEWSTAT",i[i.WHEN=45]="WHEN",i[i.SECURITY_ENFORCED=46]="SECURITY_ENFORCED",i[i.SYSTEM_MODE=47]="SYSTEM_MODE",i[i.USER_MODE=48]="USER_MODE",i[i.CALENDAR_MONTH=49]="CALENDAR_MONTH",i[i.CALENDAR_QUARTER=50]="CALENDAR_QUARTER",i[i.CALENDAR_YEAR=51]="CALENDAR_YEAR",i[i.DAY_IN_MONTH=52]="DAY_IN_MONTH",i[i.DAY_IN_WEEK=53]="DAY_IN_WEEK",i[i.DAY_IN_YEAR=54]="DAY_IN_YEAR",i[i.DAY_ONLY=55]="DAY_ONLY",i[i.FISCAL_MONTH=56]="FISCAL_MONTH",i[i.FISCAL_QUARTER=57]="FISCAL_QUARTER",i[i.FISCAL_YEAR=58]="FISCAL_YEAR",i[i.HOUR_IN_DAY=59]="HOUR_IN_DAY",i[i.WEEK_IN_MONTH=60]="WEEK_IN_MONTH",i[i.WEEK_IN_YEAR=61]="WEEK_IN_YEAR",i[i.AVG=62]="AVG",i[i.COUNT=63]="COUNT",i[i.COUNT_DISTINCT=64]="COUNT_DISTINCT",i[i.MIN=65]="MIN",i[i.MAX=66]="MAX",i[i.SUM=67]="SUM",i[i.DISTANCE=68]="DISTANCE",i[i.GEOLOCATION=69]="GEOLOCATION",i[i.FIELDS=70]="FIELDS",i[i.FORMAT=71]="FORMAT",i[i.TOLABEL=72]="TOLABEL",i[i.CONVERT_TIMEZONE=73]="CONVERT_TIMEZONE",i[i.CONVERT_CURRENCY=74]="CONVERT_CURRENCY",i[i.GROUPING=75]="GROUPING",i[i.ALL=76]="ALL",i[i.CUSTOM=77]="CUSTOM",i[i.STANDARD=78]="STANDARD",i[i.YESTERDAY=79]="YESTERDAY",i[i.TODAY=80]="TODAY",i[i.TOMORROW=81]="TOMORROW",i[i.LAST_WEEK=82]="LAST_WEEK",i[i.THIS_WEEK=83]="THIS_WEEK",i[i.NEXT_WEEK=84]="NEXT_WEEK",i[i.LAST_MONTH=85]="LAST_MONTH",i[i.THIS_MONTH=86]="THIS_MONTH",i[i.NEXT_MONTH=87]="NEXT_MONTH",i[i.LAST_90_DAYS=88]="LAST_90_DAYS",i[i.NEXT_90_DAYS=89]="NEXT_90_DAYS",i[i.THIS_QUARTER=90]="THIS_QUARTER",i[i.LAST_QUARTER=91]="LAST_QUARTER",i[i.NEXT_QUARTER=92]="NEXT_QUARTER",i[i.THIS_YEAR=93]="THIS_YEAR",i[i.LAST_YEAR=94]="LAST_YEAR",i[i.NEXT_YEAR=95]="NEXT_YEAR",i[i.THIS_FISCAL_QUARTER=96]="THIS_FISCAL_QUARTER",i[i.LAST_FISCAL_QUARTER=97]="LAST_FISCAL_QUARTER",i[i.NEXT_FISCAL_QUARTER=98]="NEXT_FISCAL_QUARTER",i[i.THIS_FISCAL_YEAR=99]="THIS_FISCAL_YEAR",i[i.LAST_FISCAL_YEAR=100]="LAST_FISCAL_YEAR",i[i.NEXT_FISCAL_YEAR=101]="NEXT_FISCAL_YEAR",i[i.NEXT_N_DAYS=102]="NEXT_N_DAYS",i[i.LAST_N_DAYS=103]="LAST_N_DAYS",i[i.N_DAYS_AGO=104]="N_DAYS_AGO",i[i.NEXT_N_WEEKS=105]="NEXT_N_WEEKS",i[i.LAST_N_WEEKS=106]="LAST_N_WEEKS",i[i.N_WEEKS_AGO=107]="N_WEEKS_AGO",i[i.NEXT_N_MONTHS=108]="NEXT_N_MONTHS",i[i.LAST_N_MONTHS=109]="LAST_N_MONTHS",i[i.N_MONTHS_AGO=110]="N_MONTHS_AGO",i[i.NEXT_N_QUARTERS=111]="NEXT_N_QUARTERS",i[i.LAST_N_QUARTERS=112]="LAST_N_QUARTERS",i[i.N_QUARTERS_AGO=113]="N_QUARTERS_AGO",i[i.NEXT_N_YEARS=114]="NEXT_N_YEARS",i[i.LAST_N_YEARS=115]="LAST_N_YEARS",i[i.N_YEARS_AGO=116]="N_YEARS_AGO",i[i.NEXT_N_FISCAL_QUARTERS=117]="NEXT_N_FISCAL_QUARTERS",i[i.LAST_N_FISCAL_QUARTERS=118]="LAST_N_FISCAL_QUARTERS",i[i.N_FISCAL_QUARTERS_AGO=119]="N_FISCAL_QUARTERS_AGO",i[i.NEXT_N_FISCAL_YEARS=120]="NEXT_N_FISCAL_YEARS",i[i.LAST_N_FISCAL_YEARS=121]="LAST_N_FISCAL_YEARS",i[i.N_FISCAL_YEARS_AGO=122]="N_FISCAL_YEARS_AGO",i[i.DELEGATED=123]="DELEGATED",i[i.EVERYTHING=124]="EVERYTHING",i[i.MINE_AND_MY_GROUPS=125]="MINE_AND_MY_GROUPS",i[i.MINE=126]="MINE",i[i.MY_TERRITORY=127]="MY_TERRITORY",i[i.MY_TEAM_TERRITORY=128]="MY_TEAM_TERRITORY",i[i.TEAM=129]="TEAM",i[i.ALL_PRIVATE=130]="ALL_PRIVATE",i[i.MRU=131]="MRU",i[i.EQUAL=132]="EQUAL",i[i.NOT_EQUAL=133]="NOT_EQUAL",i[i.LESS_THAN=134]="LESS_THAN",i[i.LESS_THAN_OR_EQUAL=135]="LESS_THAN_OR_EQUAL",i[i.GREATER_THAN=136]="GREATER_THAN",i[i.GREATER_THAN_OR_EQUAL=137]="GREATER_THAN_OR_EQUAL",i[i.L_PAREN=138]="L_PAREN",i[i.R_PAREN=139]="R_PAREN",i[i.L_SQUARE_BRACKET=140]="L_SQUARE_BRACKET",i[i.R_SQUARE_BRACKET=141]="R_SQUARE_BRACKET",i[i.DOT=142]="DOT",i[i.COMMA=143]="COMMA",i[i.COLON=144]="COLON",i[i.SEMICOLON=145]="SEMICOLON",i[i.ASTERISK=146]="ASTERISK",i[i.PLUS=147]="PLUS",i[i.MINUS=148]="MINUS",i[i.DATETIME=149]="DATETIME",i[i.DATE_TOKEN=150]="DATE_TOKEN",i[i.CURRENCY_PREFIXED_DECIMAL=151]="CURRENCY_PREFIXED_DECIMAL",i[i.CURRENCY_PREFIXED_INTEGER=152]="CURRENCY_PREFIXED_INTEGER",i[i.SIGNED_DECIMAL=153]="SIGNED_DECIMAL",i[i.UNSIGNED_DECIMAL=154]="UNSIGNED_DECIMAL",i[i.SIGNED_INTEGER=155]="SIGNED_INTEGER",i[i.UNSIGNED_INTEGER=156]="UNSIGNED_INTEGER",i[i.STRING_LITERAL=157]="STRING_LITERAL",i[i.GEOLOCATION_UNIT=158]="GEOLOCATION_UNIT",i[i.IDENTIFIER=159]="IDENTIFIER",i[i.EOF=160]="EOF",i))(R||{});function y(s){return s>=49&&s<=61}function F(s){return s>=62&&s<=67}function H(s){return s>=68&&s<=69}function Q(s){return s===70}function D(s){return s>=71&&s<=75}function ne(s){return s>=76&&s<=78}function P(s){return s>=79&&s<=101}function v(s){return s>=102&&s<=122}function ae(s){return s>=123&&s<=131}function g(s){return s===159||s>=30&&s<=48||s>=49&&s<=61||s>=62&&s<=67||s>=68&&s<=69||s===70||s>=71&&s<=75||s>=76&&s<=78||s>=79&&s<=101||s>=102&&s<=122||s>=123&&s<=131||s===158||s===152}function V(s){return s===153||s===154||s===155||s===156}function oe(s){return s>=132&&s<=137}var fe=new Map([["AND",0],["AS",1],["ASC",2],["DESC",3],["CUBE",4],["ELSE",5],["EXCLUDES",6],["FALSE",7],["FIRST",8],["FROM",9],["HAVING",11],["IN",12],["INCLUDES",13],["LAST",14],["LIKE",15],["LIMIT",16],["NOT",17],["NULL",18],["NULLS",19],["OR",20],["ROLLUP",22],["SELECT",23],["TRUE",24],["USING",25],["WHERE",26],["WITH",27],["FOR",28],["UPDATE",29],["ABOVE_OR_BELOW",31],["ABOVE",30],["NEW",32],["AT",33],["BELOW",34],["END",36],["OFFSET",37],["REFERENCE",38],["SCOPE",39],["TRACKING",40],["THEN",41],["TYPEOF",42],["VIEW",43],["VIEWSTAT",44],["WHEN",45],["SECURITY_ENFORCED",46],["SYSTEM_MODE",47],["USER_MODE",48],["CALENDAR_MONTH",49],["CALENDAR_QUARTER",50],["CALENDAR_YEAR",51],["DAY_IN_MONTH",52],["DAY_IN_WEEK",53],["DAY_IN_YEAR",54],["DAY_ONLY",55],["FISCAL_MONTH",56],["FISCAL_QUARTER",57],["FISCAL_YEAR",58],["HOUR_IN_DAY",59],["WEEK_IN_MONTH",60],["WEEK_IN_YEAR",61],["AVG",62],["COUNT_DISTINCT",64],["COUNT",63],["MIN",65],["MAX",66],["SUM",67],["DISTANCE",68],["GEOLOCATION",69],["FIELDS",70],["FORMAT",71],["TOLABEL",72],["CONVERTTIMEZONE",73],["CONVERTCURRENCY",74],["GROUPING",75],["ALL",76],["CUSTOM",77],["STANDARD",78],["YESTERDAY",79],["TODAY",80],["TOMORROW",81],["LAST_WEEK",82],["THIS_WEEK",83],["NEXT_WEEK",84],["LAST_MONTH",85],["THIS_MONTH",86],["NEXT_MONTH",87],["LAST_90_DAYS",88],["NEXT_90_DAYS",89],["THIS_QUARTER",90],["LAST_QUARTER",91],["NEXT_QUARTER",92],["THIS_YEAR",93],["LAST_YEAR",94],["NEXT_YEAR",95],["THIS_FISCAL_QUARTER",96],["LAST_FISCAL_QUARTER",97],["NEXT_FISCAL_QUARTER",98],["THIS_FISCAL_YEAR",99],["LAST_FISCAL_YEAR",100],["NEXT_FISCAL_YEAR",101],["NEXT_N_DAYS",102],["LAST_N_DAYS",103],["N_DAYS_AGO",104],["NEXT_N_WEEKS",105],["LAST_N_WEEKS",106],["N_WEEKS_AGO",107],["NEXT_N_MONTHS",108],["LAST_N_MONTHS",109],["N_MONTHS_AGO",110],["NEXT_N_QUARTERS",111],["LAST_N_QUARTERS",112],["N_QUARTERS_AGO",113],["NEXT_N_YEARS",114],["LAST_N_YEARS",115],["N_YEARS_AGO",116],["NEXT_N_FISCAL_QUARTERS",117],["LAST_N_FISCAL_QUARTERS",118],["N_FISCAL_QUARTERS_AGO",119],["NEXT_N_FISCAL_YEARS",120],["LAST_N_FISCAL_YEARS",121],["N_FISCAL_YEARS_AGO",122],["DELEGATED",123],["EVERYTHING",124],["MINEANDMYGROUPS",125],["MINE",126],["MY_TERRITORY",127],["MY_TEAM_TERRITORY",128],["TEAM",129],["ALLPRIVATE",130],["MRU",131]]),c=[];(function(){for(let e of Object.keys(R)){let r=R[e];typeof r=="number"&&(c[r]=e)}c[72]="toLabel",c[73]="convertTimezone",c[74]="convertCurrency",c[157]="StringIdentifier",c[150]="DATE",c[32]="new",c[125]="MineAndMyGroups",c[127]="MyTerritory",c[128]="MyTeamTerritory",c[130]="AllPrivate",c[123]="Delegated",c[124]="Everything",c[126]="Mine",c[129]="Team",c[131]="Mru",c[159]="Identifier",c[142]="DECIMAL",c[132]="EQUAL",c[156]="UNSIGNED_INTEGER",c[155]="SIGNED_INTEGER",c[154]="UNSIGNED_DECIMAL",c[153]="SIGNED_DECIMAL",c[151]="CURRENCY_PREFIXED_DECIMAL",c[152]="CURRENCY_PREFIXED_INTEGER"})();function f(s){return c[s]||R[s]}function j(s){return s>=65&&s<=90||s>=97&&s<=122}function h(s){return s>=48&&s<=57}function B(s){return j(s)||h(s)||s===95}function M(s){return s===32||s===9||s===10||s===13||s===12}function le(s){let e=[],r=s.length,t=0;for(;t<r;){let n=s.charCodeAt(t);if(M(n)){for(t++;t<r&&M(s.charCodeAt(t));)t++;continue}if(n===39){let a=t;for(t++;t<r;){let p=s.charCodeAt(t);if(p===92){t+=2;continue}if(p===39){t++;break}t++}let o=s.substring(a,t),u=o.substring(1,o.length-1).toUpperCase();u==="MI"||u==="KM"?e.push({kind:158,text:o,start:a}):e.push({kind:157,text:o,start:a});continue}if(h(n)){if(t+9<r&&s.charCodeAt(t+4)===45&&s.charCodeAt(t+7)===45){let o=!0;for(let l of[0,1,2,3,5,6,8,9])if(!h(s.charCodeAt(t+l))){o=!1;break}if(o)if(t+10<r&&(s.charCodeAt(t+10)===84||s.charCodeAt(t+10)===116)){let l=t;for(t+=11;t<r;){let u=s.charCodeAt(t);if(h(u)||u===58)t++;else break}if(t<r&&s.charCodeAt(t)===46)for(t++;t<r&&h(s.charCodeAt(t));)t++;if(t<r){let u=s.charCodeAt(t);if(u===90||u===122)t++;else if(u===46){for(t++;t<r&&h(s.charCodeAt(t));)t++;t<r&&(s.charCodeAt(t)===90||s.charCodeAt(t)===122)&&t++}else if(u===43||u===45)for(t++;t<r;){let p=s.charCodeAt(t);if(h(p)||p===58)t++;else break}}e.push({kind:149,text:s.substring(l,t),start:l});continue}else{let l=t;t+=10,e.push({kind:150,text:s.substring(l,t),start:l});continue}}let a=t;for(;t<r&&h(s.charCodeAt(t));)t++;if(t<r&&s.charCodeAt(t)===46&&t+1<r&&h(s.charCodeAt(t+1))){for(t++;t<r&&h(s.charCodeAt(t));)t++;e.push({kind:154,text:s.substring(a,t),start:a});continue}e.push({kind:156,text:s.substring(a,t),start:a});continue}if(n===33){if(t+1<r&&s.charCodeAt(t+1)===61){e.push({kind:133,text:"!=",start:t}),t+=2;continue}throw new Error(`Unexpected character '!' at position ${t}`)}if(n===60){if(t+1<r){let a=s.charCodeAt(t+1);if(a===61){e.push({kind:135,text:"<=",start:t}),t+=2;continue}if(a===62){e.push({kind:133,text:"<>",start:t}),t+=2;continue}}e.push({kind:134,text:"<",start:t}),t++;continue}if(n===62){if(t+1<r&&s.charCodeAt(t+1)===61){e.push({kind:137,text:">=",start:t}),t+=2;continue}e.push({kind:136,text:">",start:t}),t++;continue}if(n===61){e.push({kind:132,text:"=",start:t}),t++;continue}if(n===40){e.push({kind:138,text:"(",start:t}),t++;continue}if(n===41){e.push({kind:139,text:")",start:t}),t++;continue}if(n===91){e.push({kind:140,text:"[",start:t}),t++;continue}if(n===93){e.push({kind:141,text:"]",start:t}),t++;continue}if(n===44){e.push({kind:143,text:",",start:t}),t++;continue}if(n===58){e.push({kind:144,text:":",start:t}),t++;continue}if(n===59){e.push({kind:145,text:";",start:t}),t++;continue}if(n===42){e.push({kind:146,text:"*",start:t}),t++;continue}if(n===43||n===45){if(t+1<r){let a=s.charCodeAt(t+1);if(h(a)){let o=t;for(t++;t<r&&h(s.charCodeAt(t));)t++;if(t<r&&s.charCodeAt(t)===46&&t+1<r&&h(s.charCodeAt(t+1))){for(t++;t<r&&h(s.charCodeAt(t));)t++;e.push({kind:153,text:s.substring(o,t),start:o});continue}e.push({kind:155,text:s.substring(o,t),start:o});continue}if(a===46&&t+2<r&&h(s.charCodeAt(t+2))){let o=t;for(t+=2;t<r&&h(s.charCodeAt(t));)t++;e.push({kind:153,text:s.substring(o,t),start:o});continue}}e.push({kind:n===43?147:148,text:s[t],start:t}),t++;continue}if(n===46){if(t+1<r&&h(s.charCodeAt(t+1))){let a=t;for(t++;t<r&&h(s.charCodeAt(t));)t++;e.push({kind:154,text:s.substring(a,t),start:a});continue}e.push({kind:142,text:".",start:t}),t++;continue}if(j(n)){let a=t;for(t++;t<r;){let p=s.charCodeAt(t);if(B(p)||p===46)t++;else break}let o=s.substring(a,t);if(o.includes(".."))throw new Error(`Invalid identifier with consecutive dots at position ${a}: "${o}"`);let l=o.toUpperCase();if(l==="ORDER"&&!o.includes(".")){let p=W(s,t,"BY");if(p!==-1){let E=s.substring(a,p);e.push({kind:21,text:E,start:a}),t=p;continue}}if(l==="GROUP"&&!o.includes(".")){let p=W(s,t,"BY");if(p!==-1){let E=s.substring(a,p);e.push({kind:10,text:E,start:a}),t=p;continue}}if(l==="DATA"&&!o.includes(".")){let p=W(s,t,"CATEGORY");if(p!==-1){let E=s.substring(a,p);e.push({kind:35,text:E,start:a}),t=p;continue}}if(o.length>=4){let p=o.substring(0,3);if(se(p)){let E=o.substring(3);if(Ae(E)){E.includes(".")?e.push({kind:151,text:o,start:a}):e.push({kind:152,text:o,start:a});continue}}}if(o.length===3&&se(o)){let p=t,E=t,C=!1;if(E<r&&s.charCodeAt(E)===45&&(C=!0,E++),C&&E<r&&h(s.charCodeAt(E))){for(;E<r&&h(s.charCodeAt(E));)E++;if(E<r&&s.charCodeAt(E)===46&&E+1<r&&h(s.charCodeAt(E+1))){for(E++;E<r&&h(s.charCodeAt(E));)E++;let d=s.substring(a,E);e.push({kind:151,text:d,start:a}),t=E;continue}if(E>=r||!B(s.charCodeAt(E))&&s.charCodeAt(E)!==46){let d=s.substring(a,E);e.push({kind:152,text:d,start:a}),t=E;continue}t=p}}if(o.includes(".")){e.push({kind:159,text:o,start:a});continue}let u=fe.get(l);if(u!==void 0){e.push({kind:u,text:o,start:a});continue}e.push({kind:159,text:o,start:a});continue}throw new Error(`Unexpected character '${s[t]}' (code ${n}) at position ${t}`)}return e.push({kind:160,text:"",start:t}),e}function W(s,e,r){let t=s.length,n=e;if(n>=t||!M(s.charCodeAt(n)))return-1;for(;n<t&&M(s.charCodeAt(n));)n++;let a=r.length;if(n+a>t)return-1;for(let l=0;l<a;l++){let u=s.charCodeAt(n+l),p=r.charCodeAt(l);if((u>=97&&u<=122?u-32:u)!==p)return-1}let o=n+a;if(o<t){let l=s.charCodeAt(o);if(B(l)||l===46)return-1}return o}function se(s){for(let e=0;e<s.length;e++)if(!j(s.charCodeAt(e)))return!1;return!0}function Ae(s){if(s.length===0)return!1;let e=0;if(s.charCodeAt(e)===45&&e++,e>=s.length||!h(s.charCodeAt(e)))return!1;for(;e<s.length&&h(s.charCodeAt(e));)e++;if(e<s.length&&s.charCodeAt(e)===46){if(e++,e>=s.length||!h(s.charCodeAt(e)))return!1;for(;e<s.length&&h(s.charCodeAt(e));)e++}return e===s.length}var de=new Set([23,9,26,21,10,11,16,27,28,29,25,19,2,3,0,20,17,45,41,5,36,42,143,139,160]),X=class s{constructor(){this.tokens=[];this.pos=0;this.config={}}init(e,r){this.tokens=e,this.pos=0,this.config=r}getPos(){return this.pos}peek(){return this.tokens[this.pos]}peekAt(e){let r=this.pos+e;return r>=this.tokens.length?this.tokens[this.tokens.length-1]:this.tokens[r]}advance(){let e=this.tokens[this.pos];return e.kind!==160&&this.pos++,e}expect(e){let r=this.peek();if(r.kind!==e)throw this.error(`Expected ${R[e]} but found ${R[r.kind]} ("${r.text}") at position ${r.start}`);return this.advance()}match(e){return this.peek().kind===e?(this.advance(),!0):!1}check(e){return this.peek().kind===e}isAtEnd(){return this.peek().kind===160}error(e){return this.config.logErrors&&console.log(e),new Error(e)}static{this.CLAUSE_BOUNDARY_KINDS=new Set([26,21,10,11,16,37,27,28,29,25,160])}isAtClauseBoundary(){let e=this.peek().kind;return s.CLAUSE_BOUNDARY_KINDS.has(e)||e===139}synchronize(){for(;!this.isAtEnd();){if(this.isAtClauseBoundary())return;this.advance()}}consumeIdentifier(){let e=this.peek();if(g(e.kind))return this.advance();throw this.error(`Expected identifier but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}checkIdentifier(){return g(this.peek().kind)}isFunctionCall(){let e=this.peek(),r=this.peekAt(1);return(y(e.kind)||F(e.kind)||H(e.kind)||Q(e.kind)||D(e.kind))&&r.kind===138}canBeAlias(){let e=this.peek();return!(!g(e.kind)||de.has(e.kind)||e.kind===37&&this.peekAt(1).kind===156)}getLiteralType(e){let r=f(e);return r==="UNSIGNED_INTEGER"||r==="SIGNED_INTEGER"?"INTEGER":r==="UNSIGNED_DECIMAL"||r==="SIGNED_DECIMAL"?"DECIMAL":r==="CURRENCY_PREFIXED_DECIMAL"?"DECIMAL_WITH_CURRENCY_PREFIX":r==="CURRENCY_PREFIXED_INTEGER"?"INTEGER_WITH_CURRENCY_PREFIX":r==="DATETIME"?"DATETIME":r==="DATE"?"DATE":r==="NULL"?"NULL":r==="StringIdentifier"||r==="Identifier"?"STRING":r==="TRUE"||r==="FALSE"?"BOOLEAN":P(e)?"DATE_LITERAL":v(e)?"DATE_N_LITERAL":"STRING"}parseSelectStatement(e=!1){let r=this.parseSelectClause(),t=this.parseFromClause(),n;e?(n={fields:r,relationshipName:t.sObject},t.sObjectPrefix&&(n.sObjectPrefix=t.sObjectPrefix)):n={fields:r,sObject:t.sObject},t.alias&&(n.sObjectAlias=t.alias),this.applySObjectAlias(n);let a=this.parseClauseStatements();return Object.assign(n,a),n}parseSelectStatementPartial(){let e,r;this.check(23)&&(e=this.parseSelectClause()),this.check(9)&&(r=this.parseFromClause());let t={};e&&(t.fields=e),r&&(t.sObject=r.sObject,r.alias&&(t.sObjectAlias=r.alias)),t.fields&&this.applySObjectAlias(t);let n=this.parseClauseStatements();return Object.assign(t,n),t}applySObjectAlias(e){if(!e.sObjectAlias||!Array.isArray(e.fields))return;let r=e.sObjectAlias;e.fields.forEach(t=>{t.relationships&&t.relationships[0]===r&&(t.relationships=t.relationships.slice(1),t.objectPrefix=r,t.relationships.length===0&&(delete t.relationships,t.type="Field"))})}parseSelectClause(){this.expect(23);let e=[];for(e.push(this.parseSelectField());this.match(143);)e.push(this.parseSelectField());return e}parseSelectField(){let e=this.peek(),r=this.peekAt(1);return e.kind===138&&r.kind===23?this.parseSelectClauseSubqueryIdentifier():e.kind===42?this.parseSelectClauseTypeOf():this.isFunctionCall()?this.parseSelectClauseFunctionIdentifier():this.parseSelectClauseIdentifier()}parseSelectClauseFunctionIdentifier(){let e=this.parseAnyFunction(!0);return this.canBeAlias()&&(e.alias=this.consumeIdentifier().text),e}parseSelectClauseSubqueryIdentifier(){this.expect(138);let e=this.parseSelectStatement(!0);return this.expect(139),{type:"FieldSubquery",subquery:e}}parseSelectClauseTypeOf(){this.expect(42);let e=this.consumeIdentifier().text,r=[];for(;this.check(45);){this.advance();let t=this.consumeIdentifier().text;this.expect(41);let n=[];for(n.push(this.consumeIdentifier().text);this.match(143);)n.push(this.consumeIdentifier().text);r.push({type:"WHEN",objectType:t,fieldList:n})}if(this.check(5)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);r.push({type:"ELSE",fieldList:t})}return this.expect(36),{type:"FieldTypeof",field:e,conditions:r}}parseSelectClauseIdentifier(){let r=this.consumeIdentifier().text,t;if(!r.includes("."))t={type:"Field",field:r};else{let n=r.split(".");t={type:"FieldRelationship",field:n[n.length-1],relationships:n.slice(0,n.length-1),rawValue:r}}return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseFromClause(){this.expect(9);let r=this.consumeIdentifier().text,t;if(r.includes(".")){let n=r.split(".");t={sObjectPrefix:n.slice(0,n.length-1),sObject:n[n.length-1]}}else t={sObject:r};return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseClauseStatements(){let e={},r=(t,n)=>{if(this.config.ignoreParseErrors){let a=this.pos;try{t(),this.isAtClauseBoundary()||(n&&n(),this.synchronize())}catch(o){this.config.logErrors&&console.log(o),n&&n(),this.pos=a,this.synchronize()}}else t()};for(this.check(25)&&r(()=>{e.usingScope=this.parseUsingScope()}),this.check(26)&&r(()=>{e.where=this.parseWhereClause()},()=>{delete e.where});this.check(27);)r(()=>{let t=this.parseWithClause();t.withSecurityEnforced&&(e.withSecurityEnforced=!0),t.withAccessLevel&&(e.withAccessLevel=t.withAccessLevel),t.withDataCategory&&(e.withDataCategory=t.withDataCategory)});return this.check(10)&&(r(()=>{e.groupBy=this.parseGroupByClause()}),this.check(11)&&r(()=>{e.having=this.parseHavingClause()})),this.check(21)&&r(()=>{e.orderBy=this.parseOrderByClause()}),this.check(16)&&r(()=>{this.advance(),e.limit=Number(this.expect(156).text)}),this.check(37)&&r(()=>{this.advance(),e.offset=Number(this.expect(156).text)}),this.check(28)&&r(()=>{this.advance();let t=this.peek();if(t.kind===43||t.kind===38||t.kind===29)e.for=f(this.advance().kind);else throw this.error(`Expected VIEW, REFERENCE, or UPDATE after FOR at position ${t.start}`)}),this.check(29)&&r(()=>{this.advance();let t=this.peek();if(t.kind===40||t.kind===44)e.update=f(this.advance().kind);else throw this.error(`Expected TRACKING or VIEWSTAT after UPDATE at position ${t.start}`)}),e}parseUsingScope(){this.expect(25),this.expect(39);let e=this.peek();if(ae(e.kind))return this.advance().text;if(g(e.kind))return this.advance().text;throw this.error(`Expected scope enumeration value at position ${e.start}`)}parseWhereClause(){return this.expect(26),this.parseConditionExpressions(!0,!0)}parseConditionExpressions(e,r){let t,n,a={left:0,right:0};for(;;){let o;if(t)if(this.check(0))o="AND",this.advance();else if(this.check(20))o="OR",this.advance();else break;let l={},u=l,p=null;for(;this.hasNegationPrefix();){let d=0;for(;this.check(138)&&this.isNegationAhead();)this.advance(),d++,a.left++;if(this.check(17)){this.advance();let I={left:d>0?{openParen:d}:null,operator:"NOT",right:{left:{}}};p?p.right=I:l=I,p=I,u=I.right}else break}u.left=this.parseExpression(e,r,a),t?o&&(n.operator=o,n.right=l):(t=l,n=l);let E=l;for(;E.right&&E.right.left!==void 0;){let d=E.right;if(d.right&&d.right.left!==void 0)E=d;else{E=d;break}}if(n=E,this.isAtEnd())break;let C=this.peek().kind;if(C!==0&&C!==20)break}if(a.left!==a.right)throw this.error(`Mismatched parentheses in WHERE/HAVING clause: ${a.left} open vs ${a.right} close`);return t}hasNegationPrefix(){let e=0;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}isNegationAhead(){let e=1;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}parseExpression(e,r,t){let n=0;for(;this.check(138)&&!this.isSubqueryStart()&&!this.isArrayExpressionStart();)this.advance(),n++,t&&t.left++;let a,o=!1;e&&F(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseAggregateFunction(!1),o=!0):r&&H(this.peek().kind)&&this.peek().kind===68&&this.peekAt(1).kind===138?(a=this.parseLocationFunction(!1),o=!0):y(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseDateFunction(!1),o=!0):D(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseOtherFunction(!1),o=!0):a=this.consumeIdentifier().text;let l=this.parseOperator(),u=l.operator,p=l.isSet,E,C,d;if(p)if(this.check(138)&&this.peekAt(1).kind===23)this.expect(138),E=this.parseSelectStatement(!1),this.expect(139),C="SUBQUERY";else if(this.config.allowApexBindVariables&&this.check(144))E=this.parseApexBindVariable().value,C="APEX_BIND_VARIABLE";else{let L=this.parseArrayExpression();E=L.values,C=L.literalType,d=L.dateLiteralVariable}else{let L=this.parseAtomicValue();E=L.value,C=L.literalType,d=L.dateLiteralVariable}let I=0;for(;this.check(139)&&(!t||t.left>t.right);)this.advance(),I++,t&&t.right++;let S={};return o?S.fn=a:S.field=a,S.operator=u,C==="SUBQUERY"?S.valueQuery=E:S.value=E,S.literalType=C,d!==void 0&&(S.dateLiteralVariable=d),n>0&&(S.openParen=n),I>0&&(S.closeParen=I),S}isSubqueryStart(){return this.check(138)&&this.peekAt(1).kind===23}isArrayExpressionStart(){return!1}parseOperator(){let e=this.peek();if(oe(e.kind))return{operator:this.advance().text,isSet:!1};if(e.kind===15)return this.advance(),{operator:"LIKE",isSet:!1};if(e.kind===12)return this.advance(),{operator:"IN",isSet:!0};if(e.kind===17&&this.peekAt(1).kind===12)return this.advance(),this.advance(),{operator:"NOT IN",isSet:!0};if(e.kind===13)return this.advance(),{operator:"INCLUDES",isSet:!0};if(e.kind===6)return this.advance(),{operator:"EXCLUDES",isSet:!0};throw this.error(`Expected operator but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseAtomicValue(){let e=this.peek();if(this.config.allowApexBindVariables&&e.kind===144)return{value:this.parseApexBindVariable().value,literalType:"APEX_BIND_VARIABLE"};if(e.kind===157)return{value:this.advance().text,literalType:"STRING"};if(e.kind===158)return{value:this.advance().text,literalType:"STRING"};if(e.kind===156)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===155)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===154)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===153)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===151)return{value:this.advance().text,literalType:"DECIMAL_WITH_CURRENCY_PREFIX"};if(e.kind===152)return{value:this.advance().text,literalType:"INTEGER_WITH_CURRENCY_PREFIX"};if(e.kind===149)return{value:this.advance().text,literalType:"DATETIME"};if(e.kind===150)return{value:this.advance().text,literalType:"DATE"};if(e.kind===18)return this.advance(),{value:"NULL",literalType:"NULL"};if(e.kind===24||e.kind===7)return{value:f(this.advance().kind),literalType:"BOOLEAN"};if(P(e.kind))return{value:this.advance().text,literalType:"DATE_LITERAL"};if(v(e.kind)){let r=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${r.text}:${n}`,literalType:"DATE_N_LITERAL",dateLiteralVariable:n}}throw this.error(`Expected value but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseArrayExpression(){this.expect(138);let e=[];for(e.push(this.parseArrayItem());this.match(143);)e.push(this.parseArrayItem());this.expect(139);let r=e.map(u=>u.value),t=e.map(u=>u.type),n=e.map(u=>u.variable!==void 0?u.variable:null),a=n.some(u=>u!==null),o;new Set(t).size===1?o=this.getLiteralType(this.tokenKindFromTypeName(t[0])):o=t.map(u=>this.getLiteralType(this.tokenKindFromTypeName(u)));let l={values:r,literalType:o};return a&&(l.dateLiteralVariable=n),l}tokenKindFromTypeName(e){for(let r=0;r<=160;r++)if(f(r)===e)return r;return 159}parseArrayItem(){let e=this.peek();if(e.kind===157)return{value:this.advance().text,type:f(157)};if(e.kind===158)return{value:this.advance().text,type:f(158)};if(e.kind===151)return{value:this.advance().text,type:f(151)};if(e.kind===152)return{value:this.advance().text,type:f(152)};if(V(e.kind)){let r=this.advance();return{value:r.text,type:f(r.kind)}}if(e.kind===149)return{value:this.advance().text,type:f(149)};if(e.kind===150)return{value:this.advance().text,type:f(150)};if(e.kind===18)return this.advance(),{value:"null",type:"NULL"};if(e.kind===24)return{value:this.advance().text,type:"TRUE"};if(e.kind===7)return{value:this.advance().text,type:"FALSE"};if(P(e.kind))return{value:this.advance().text,type:f(e.kind)};if(v(e.kind)){let r=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${r.text}:${n}`,type:f(r.kind),variable:n}}throw this.error(`Expected array value but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseApexBindVariable(){this.expect(144);let e=[];for(this.check(32)?e.push(this.parseApexBindNewInstantiation()):this.checkIdentifier()&&(this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):e.push(this.parseApexBindIdentifier()));this.check(142);)this.advance(),this.checkIdentifier()&&this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):this.checkIdentifier()&&e.push(this.parseApexBindIdentifier());return{value:e.join(".")}}parseApexBindIdentifier(){let e=this.consumeIdentifier().text;return this.check(140)&&(e+=this.parseApexBindArrayAccessor()),e}parseApexBindNewInstantiation(){this.expect(32);let r=`new ${this.consumeIdentifier().text}`;if(this.check(134)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);this.expect(136),r+=`<${t.join(", ")}>`}return r+=this.parseApexBindFunctionParams(),this.check(140)&&(r+=this.parseApexBindArrayAccessor()),r}parseApexBindFunctionCall(){let r=this.consumeIdentifier().text+this.parseApexBindFunctionParams();return this.check(140)&&(r+=this.parseApexBindArrayAccessor()),r}parseApexBindFunctionParams(){this.expect(138);let e=[];if(!this.check(139))for(e.push(this.consumeIdentifier().text);this.match(143);)e.push(this.consumeIdentifier().text);return this.expect(139),`(${e.join(", ")})`}parseApexBindArrayAccessor(){this.expect(140);let e=this.peek(),r;if(e.kind===156)r=this.advance().text;else if(g(e.kind))r=this.advance().text;else throw this.error(`Expected integer or identifier in array accessor at position ${e.start}`);return this.expect(141),`[${r}]`}parseAnyFunction(e){let r=this.peek();if(y(r.kind))return this.parseDateFunction(e);if(F(r.kind))return this.parseAggregateFunction(e);if(r.kind===68)return this.parseLocationFunction(e);if(Q(r.kind))return this.parseFieldsFunction(e);if(D(r.kind))return this.parseOtherFunction(e);throw this.error(`Expected function name but found ${R[r.kind]} at position ${r.start}`)}parseDateFunction(e){let r=this.advance(),t=f(r.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${r.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseAggregateFunction(e){let r=this.advance(),t=f(r.kind),n=this.parseFunctionExpression(!0,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${r.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseFieldsFunction(e){this.advance(),this.expect(138);let r=this.peek(),t;if(ne(r.kind))t=this.advance().text;else if(g(r.kind))t=this.advance().text;else throw this.error(`Expected FIELDS function parameter at position ${r.start}`);this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="FIELDS",n.parameters=[t],n.rawValue=`FIELDS(${t})`,n}parseOtherFunction(e){let r=this.advance(),t=f(r.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,a.rawValue=`${r.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseLocationFunction(e){this.advance(),this.expect(138);let r=this.consumeIdentifier().text;this.expect(143);let t;this.check(69)&&this.peekAt(1).kind===138?t=this.parseGeolocationFunction(e):t=this.consumeIdentifier().text,this.expect(143);let n=this.expect(158).text;this.expect(139);let a={};e&&(a.type="FieldFunctionExpression"),a.functionName="DISTANCE",a.parameters=[r,t,n],e&&(a.isAggregateFn=!0);let o=typeof t=="string"?t:t.rawValue;return a.rawValue=`DISTANCE(${r}, ${o}, ${n})`,a}parseGeolocationFunction(e){this.advance(),this.expect(138);let r=this.parseNumberValue();this.expect(143);let t=this.parseNumberValue();this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="GEOLOCATION",n.parameters=[r,t],n.rawValue=`GEOLOCATION(${r}, ${t})`,n}parseNumberValue(){let e=this.peek();if(e.kind===156||e.kind===155||e.kind===154||e.kind===153)return this.advance().text;throw this.error(`Expected number but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseFunctionExpression(e,r=!0){this.expect(138);let t=[];if(!this.check(139))for(t.push(this.parseFunctionParam(e,r));this.match(143);)t.push(this.parseFunctionParam(e,r));return this.expect(139),t}parseFunctionParam(e,r){let t=this.peek(),n=this.peekAt(1);if(!e&&F(t.kind)&&n.kind===138)return this.parseAggregateFunction(r);if(D(t.kind)&&n.kind===138)return this.parseOtherFunction(r);if(t.kind===157)return this.advance().text;if(V(t.kind))return this.advance().text;if(g(t.kind))return this.advance().text;throw this.error(`Expected function parameter but found ${R[t.kind]} ("${t.text}") at position ${t.start}`)}parseGroupByClause(){this.expect(10);let e=[];for(e.push(this.parseGroupByItem());this.match(143);)e.push(this.parseGroupByItem());return e}parseGroupByItem(){let e=this.peek();return e.kind===4&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("CUBE")}:e.kind===22&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("ROLLUP")}:y(e.kind)&&this.peekAt(1).kind===138?{fn:this.parseDateFunction(!1)}:{field:this.consumeIdentifier().text}}parseCubeOrRollupFunction(e){let r=this.advance(),t=this.parseFunctionExpression(!1);return{functionName:e,parameters:t,rawValue:`${e}(${t.join(", ")})`}}parseHavingClause(){return this.expect(11),this.parseConditionExpressions(!0,!1)}parseOrderByClause(){this.expect(21);let e=[];for(e.push(this.parseOrderByItem());this.match(143);)e.push(this.parseOrderByItem());return e}parseOrderByItem(){let e=this.peek(),r=this.peekAt(1),t;if(e.kind===75&&r.kind===138){let n=this.advance(),a=this.parseFunctionExpression(!1);t={fn:{functionName:f(n.kind),parameters:a,rawValue:`${n.text}(${a.join(", ")})`}}}else F(e.kind)&&r.kind===138?t={fn:this.parseAggregateFunction(!1)}:y(e.kind)&&r.kind===138?t={fn:this.parseDateFunction(!1)}:e.kind===68&&r.kind===138?t={fn:this.parseLocationFunction(!1)}:t={field:this.consumeIdentifier().text};if(this.check(2)?t.order=f(this.advance().kind):this.check(3)&&(t.order=f(this.advance().kind)),this.check(19))if(this.advance(),this.check(8))t.nulls=f(this.advance().kind);else if(this.check(14))t.nulls=f(this.advance().kind);else throw this.error(`Expected FIRST or LAST after NULLS at position ${this.peek().start}`);return t}parseWithClause(){this.expect(27);let e=this.peek();if(e.kind===46)return this.advance(),{withSecurityEnforced:!0};if(e.kind===48)return this.advance(),{withAccessLevel:"USER_MODE"};if(e.kind===47)return this.advance(),{withAccessLevel:"SYSTEM_MODE"};if(e.kind===35)return{withDataCategory:this.parseWithDataCategory()};throw this.error(`Expected SECURITY_ENFORCED, USER_MODE, SYSTEM_MODE, or DATA CATEGORY after WITH at position ${e.start}`)}parseWithDataCategory(){this.expect(35);let e=[];for(e.push(this.parseDataCategoryCondition());this.check(0);)this.advance(),e.push(this.parseDataCategoryCondition());return{conditions:e}}parseDataCategoryCondition(){let e=this.consumeIdentifier().text,r=this.peek(),t;if(r.kind===33||r.kind===30||r.kind===34||r.kind===31)t=this.advance().text;else throw this.error(`Expected data category selector (AT, ABOVE, BELOW, ABOVE_OR_BELOW) at position ${r.start}`);let n=this.match(138),a=[];for(a.push(this.consumeIdentifier().text);this.match(143);)a.push(this.consumeIdentifier().text);return n&&this.expect(139),{groupName:e,selector:t,parameters:a}}},b=new X;function x(s,e){let r=e||{},t=le(s);b.init(t,r);let n;if(r.allowPartialQuery?n=b.parseSelectStatementPartial():n=b.parseSelectStatement(!1),!b.isAtEnd()&&!r.ignoreParseErrors){let a=t[b.getPos()];throw new Error(`Unexpected token "${a.text}" at position ${a.start}. Expected end of query.`)}return n}function q(s,e){try{return x(s,e),!0}catch{return!1}}function _(s){return typeof s=="string"}function O(s){return Number.isFinite(s)}function T(s){return s==null}function m(s,e,r){return T(s)?"":`${r||""}${s}${e||""}`}function ue(s,e=!1){return Array.isArray(s)?s.length>0?`(${s.join(", ")})`:e?"()":"":e?`(${s||""})`:s||""}function Ee(s,e,r=""){return O(s)&&s>0?new Array(s).fill(e).join(r):""}function K(s){return s&&!T(s.alias)}function k(s){return _(s.relationshipName)}function J(s){return!!s&&!!s.operator&&!!s.right}function z(s){return s&&!T(s.fn)&&_(s.operator)&&!T(s.value)}function U(s){return s&&O(s.openParen)&&T(s.operator)&&T(s.field)&&T(s.fn)&&T(s.closeParen)}function Z(s){return s&&_(s.field)&&_(s.operator)&&!T(s.valueQuery)&&T(s.value)}function ee(s){return s&&!T(s.field)}function te(s){return s&&!T(s.field)}function Ne(s){return["IN","NOT IN","INCLUDES","EXCLUDES"].includes(s)}function pe(s,e,r){if(T(e))return s;if(r&&e!=="APEX_BIND_VARIABLE"&&Ne(r)&&!Array.isArray(s)&&(s=[s],e=Array.isArray(e)?e:[e]),Array.isArray(e)&&Array.isArray(s))return s.map((t,n)=>Re(t,e?.[n]));switch(Array.isArray(e)&&(e=e[0]),e){case"STRING":return Array.isArray(s)?s.filter(Boolean).map(t=>_(t)&&t.startsWith("'")?t:`'${t??""}'`):(s=String(s??""),_(s)&&s.startsWith("'")?s:`'${s??""}'`);case"APEX_BIND_VARIABLE":return`:${s}`;default:return s}}function Re(s,e){return e==="STRING"?_(s)&&s.startsWith("'")?s:`'${s??""}'`:s}var w=class{constructor(e,r){this.currIndent=1;this.enabled=e,this.options={numIndent:r.numIndent??1,fieldMaxLineLength:r.fieldMaxLineLength??60,fieldSubqueryParensOnOwnLine:r.fieldSubqueryParensOnOwnLine??!0,whereClauseOperatorsIndented:!0,newLineAfterKeywords:r.newLineAfterKeywords??!1,logging:r.logging??!1},this.options.newLineAfterKeywords&&(this.options.fieldSubqueryParensOnOwnLine=!0)}log(e){this.options.logging&&console.log(e)}getIndent(e=0){return this.repeatChar((this.currIndent+e)*(this.options.numIndent||1)," ")}repeatChar(e,r){return new Array(e).fill(r).join("")}setSubquery(e){this.currIndent=e?this.currIndent+=1:this.currIndent-=1}stepCurrIndex(e){this.currIndent+=e}formatFields(e){function r(t){e.fields[t-1]&&(e.fields[t-1].suffix=e.fields[t-1].suffix.trim())}if(e.fields.forEach((t,n)=>{t.suffix=e.fields.length-1===n?"":", "}),this.enabled){let t=0,n=!1;e.fields.forEach((a,o)=>{a.isSubquery?(r(o),a.prefix=`
1
+ "use strict";var R=(i=>(i[i.AND=0]="AND",i[i.AS=1]="AS",i[i.ASC=2]="ASC",i[i.DESC=3]="DESC",i[i.CUBE=4]="CUBE",i[i.ELSE=5]="ELSE",i[i.EXCLUDES=6]="EXCLUDES",i[i.FALSE=7]="FALSE",i[i.FIRST=8]="FIRST",i[i.FROM=9]="FROM",i[i.GROUP_BY=10]="GROUP_BY",i[i.HAVING=11]="HAVING",i[i.IN=12]="IN",i[i.INCLUDES=13]="INCLUDES",i[i.LAST=14]="LAST",i[i.LIKE=15]="LIKE",i[i.LIMIT=16]="LIMIT",i[i.NOT=17]="NOT",i[i.NULL=18]="NULL",i[i.NULLS=19]="NULLS",i[i.OR=20]="OR",i[i.ORDER_BY=21]="ORDER_BY",i[i.ROLLUP=22]="ROLLUP",i[i.SELECT=23]="SELECT",i[i.TRUE=24]="TRUE",i[i.USING=25]="USING",i[i.WHERE=26]="WHERE",i[i.WITH=27]="WITH",i[i.FOR=28]="FOR",i[i.UPDATE=29]="UPDATE",i[i.ABOVE=30]="ABOVE",i[i.ABOVE_OR_BELOW=31]="ABOVE_OR_BELOW",i[i.APEX_NEW=32]="APEX_NEW",i[i.AT=33]="AT",i[i.BELOW=34]="BELOW",i[i.DATA_CATEGORY=35]="DATA_CATEGORY",i[i.END=36]="END",i[i.OFFSET=37]="OFFSET",i[i.REFERENCE=38]="REFERENCE",i[i.SCOPE=39]="SCOPE",i[i.TRACKING=40]="TRACKING",i[i.THEN=41]="THEN",i[i.TYPEOF=42]="TYPEOF",i[i.VIEW=43]="VIEW",i[i.VIEWSTAT=44]="VIEWSTAT",i[i.WHEN=45]="WHEN",i[i.SECURITY_ENFORCED=46]="SECURITY_ENFORCED",i[i.SYSTEM_MODE=47]="SYSTEM_MODE",i[i.USER_MODE=48]="USER_MODE",i[i.CALENDAR_MONTH=49]="CALENDAR_MONTH",i[i.CALENDAR_QUARTER=50]="CALENDAR_QUARTER",i[i.CALENDAR_YEAR=51]="CALENDAR_YEAR",i[i.DAY_IN_MONTH=52]="DAY_IN_MONTH",i[i.DAY_IN_WEEK=53]="DAY_IN_WEEK",i[i.DAY_IN_YEAR=54]="DAY_IN_YEAR",i[i.DAY_ONLY=55]="DAY_ONLY",i[i.FISCAL_MONTH=56]="FISCAL_MONTH",i[i.FISCAL_QUARTER=57]="FISCAL_QUARTER",i[i.FISCAL_YEAR=58]="FISCAL_YEAR",i[i.HOUR_IN_DAY=59]="HOUR_IN_DAY",i[i.WEEK_IN_MONTH=60]="WEEK_IN_MONTH",i[i.WEEK_IN_YEAR=61]="WEEK_IN_YEAR",i[i.AVG=62]="AVG",i[i.COUNT=63]="COUNT",i[i.COUNT_DISTINCT=64]="COUNT_DISTINCT",i[i.MIN=65]="MIN",i[i.MAX=66]="MAX",i[i.SUM=67]="SUM",i[i.DISTANCE=68]="DISTANCE",i[i.GEOLOCATION=69]="GEOLOCATION",i[i.FIELDS=70]="FIELDS",i[i.FORMAT=71]="FORMAT",i[i.TOLABEL=72]="TOLABEL",i[i.CONVERT_TIMEZONE=73]="CONVERT_TIMEZONE",i[i.CONVERT_CURRENCY=74]="CONVERT_CURRENCY",i[i.GROUPING=75]="GROUPING",i[i.ALL=76]="ALL",i[i.CUSTOM=77]="CUSTOM",i[i.STANDARD=78]="STANDARD",i[i.YESTERDAY=79]="YESTERDAY",i[i.TODAY=80]="TODAY",i[i.TOMORROW=81]="TOMORROW",i[i.LAST_WEEK=82]="LAST_WEEK",i[i.THIS_WEEK=83]="THIS_WEEK",i[i.NEXT_WEEK=84]="NEXT_WEEK",i[i.LAST_MONTH=85]="LAST_MONTH",i[i.THIS_MONTH=86]="THIS_MONTH",i[i.NEXT_MONTH=87]="NEXT_MONTH",i[i.LAST_90_DAYS=88]="LAST_90_DAYS",i[i.NEXT_90_DAYS=89]="NEXT_90_DAYS",i[i.THIS_QUARTER=90]="THIS_QUARTER",i[i.LAST_QUARTER=91]="LAST_QUARTER",i[i.NEXT_QUARTER=92]="NEXT_QUARTER",i[i.THIS_YEAR=93]="THIS_YEAR",i[i.LAST_YEAR=94]="LAST_YEAR",i[i.NEXT_YEAR=95]="NEXT_YEAR",i[i.THIS_FISCAL_QUARTER=96]="THIS_FISCAL_QUARTER",i[i.LAST_FISCAL_QUARTER=97]="LAST_FISCAL_QUARTER",i[i.NEXT_FISCAL_QUARTER=98]="NEXT_FISCAL_QUARTER",i[i.THIS_FISCAL_YEAR=99]="THIS_FISCAL_YEAR",i[i.LAST_FISCAL_YEAR=100]="LAST_FISCAL_YEAR",i[i.NEXT_FISCAL_YEAR=101]="NEXT_FISCAL_YEAR",i[i.NEXT_N_DAYS=102]="NEXT_N_DAYS",i[i.LAST_N_DAYS=103]="LAST_N_DAYS",i[i.N_DAYS_AGO=104]="N_DAYS_AGO",i[i.NEXT_N_WEEKS=105]="NEXT_N_WEEKS",i[i.LAST_N_WEEKS=106]="LAST_N_WEEKS",i[i.N_WEEKS_AGO=107]="N_WEEKS_AGO",i[i.NEXT_N_MONTHS=108]="NEXT_N_MONTHS",i[i.LAST_N_MONTHS=109]="LAST_N_MONTHS",i[i.N_MONTHS_AGO=110]="N_MONTHS_AGO",i[i.NEXT_N_QUARTERS=111]="NEXT_N_QUARTERS",i[i.LAST_N_QUARTERS=112]="LAST_N_QUARTERS",i[i.N_QUARTERS_AGO=113]="N_QUARTERS_AGO",i[i.NEXT_N_YEARS=114]="NEXT_N_YEARS",i[i.LAST_N_YEARS=115]="LAST_N_YEARS",i[i.N_YEARS_AGO=116]="N_YEARS_AGO",i[i.NEXT_N_FISCAL_QUARTERS=117]="NEXT_N_FISCAL_QUARTERS",i[i.LAST_N_FISCAL_QUARTERS=118]="LAST_N_FISCAL_QUARTERS",i[i.N_FISCAL_QUARTERS_AGO=119]="N_FISCAL_QUARTERS_AGO",i[i.NEXT_N_FISCAL_YEARS=120]="NEXT_N_FISCAL_YEARS",i[i.LAST_N_FISCAL_YEARS=121]="LAST_N_FISCAL_YEARS",i[i.N_FISCAL_YEARS_AGO=122]="N_FISCAL_YEARS_AGO",i[i.DELEGATED=123]="DELEGATED",i[i.EVERYTHING=124]="EVERYTHING",i[i.MINE_AND_MY_GROUPS=125]="MINE_AND_MY_GROUPS",i[i.MINE=126]="MINE",i[i.MY_TERRITORY=127]="MY_TERRITORY",i[i.MY_TEAM_TERRITORY=128]="MY_TEAM_TERRITORY",i[i.TEAM=129]="TEAM",i[i.ALL_PRIVATE=130]="ALL_PRIVATE",i[i.MRU=131]="MRU",i[i.EQUAL=132]="EQUAL",i[i.NOT_EQUAL=133]="NOT_EQUAL",i[i.LESS_THAN=134]="LESS_THAN",i[i.LESS_THAN_OR_EQUAL=135]="LESS_THAN_OR_EQUAL",i[i.GREATER_THAN=136]="GREATER_THAN",i[i.GREATER_THAN_OR_EQUAL=137]="GREATER_THAN_OR_EQUAL",i[i.L_PAREN=138]="L_PAREN",i[i.R_PAREN=139]="R_PAREN",i[i.L_SQUARE_BRACKET=140]="L_SQUARE_BRACKET",i[i.R_SQUARE_BRACKET=141]="R_SQUARE_BRACKET",i[i.DOT=142]="DOT",i[i.COMMA=143]="COMMA",i[i.COLON=144]="COLON",i[i.SEMICOLON=145]="SEMICOLON",i[i.ASTERISK=146]="ASTERISK",i[i.PLUS=147]="PLUS",i[i.MINUS=148]="MINUS",i[i.DATETIME=149]="DATETIME",i[i.DATE_TOKEN=150]="DATE_TOKEN",i[i.CURRENCY_PREFIXED_DECIMAL=151]="CURRENCY_PREFIXED_DECIMAL",i[i.CURRENCY_PREFIXED_INTEGER=152]="CURRENCY_PREFIXED_INTEGER",i[i.SIGNED_DECIMAL=153]="SIGNED_DECIMAL",i[i.UNSIGNED_DECIMAL=154]="UNSIGNED_DECIMAL",i[i.SIGNED_INTEGER=155]="SIGNED_INTEGER",i[i.UNSIGNED_INTEGER=156]="UNSIGNED_INTEGER",i[i.STRING_LITERAL=157]="STRING_LITERAL",i[i.GEOLOCATION_UNIT=158]="GEOLOCATION_UNIT",i[i.IDENTIFIER=159]="IDENTIFIER",i[i.EOF=160]="EOF",i))(R||{});function y(s){return s>=49&&s<=61}function F(s){return s>=62&&s<=67}function B(s){return s>=68&&s<=69}function H(s){return s===70}function b(s){return s>=71&&s<=75}function se(s){return s>=76&&s<=78}function M(s){return s>=79&&s<=101}function P(s){return s>=102&&s<=122}function ne(s){return s>=123&&s<=131}function g(s){return s===159||s>=30&&s<=48||s>=49&&s<=61||s>=62&&s<=67||s>=68&&s<=69||s===70||s>=71&&s<=75||s>=76&&s<=78||s>=79&&s<=101||s>=102&&s<=122||s>=123&&s<=131||s===158||s===152}function Q(s){return s===153||s===154||s===155||s===156}function ae(s){return s>=132&&s<=137}var he=new Map([["AND",0],["AS",1],["ASC",2],["DESC",3],["CUBE",4],["ELSE",5],["EXCLUDES",6],["FALSE",7],["FIRST",8],["FROM",9],["HAVING",11],["IN",12],["INCLUDES",13],["LAST",14],["LIKE",15],["LIMIT",16],["NOT",17],["NULL",18],["NULLS",19],["OR",20],["ROLLUP",22],["SELECT",23],["TRUE",24],["USING",25],["WHERE",26],["WITH",27],["FOR",28],["UPDATE",29],["ABOVE_OR_BELOW",31],["ABOVE",30],["NEW",32],["AT",33],["BELOW",34],["END",36],["OFFSET",37],["REFERENCE",38],["SCOPE",39],["TRACKING",40],["THEN",41],["TYPEOF",42],["VIEW",43],["VIEWSTAT",44],["WHEN",45],["SECURITY_ENFORCED",46],["SYSTEM_MODE",47],["USER_MODE",48],["CALENDAR_MONTH",49],["CALENDAR_QUARTER",50],["CALENDAR_YEAR",51],["DAY_IN_MONTH",52],["DAY_IN_WEEK",53],["DAY_IN_YEAR",54],["DAY_ONLY",55],["FISCAL_MONTH",56],["FISCAL_QUARTER",57],["FISCAL_YEAR",58],["HOUR_IN_DAY",59],["WEEK_IN_MONTH",60],["WEEK_IN_YEAR",61],["AVG",62],["COUNT_DISTINCT",64],["COUNT",63],["MIN",65],["MAX",66],["SUM",67],["DISTANCE",68],["GEOLOCATION",69],["FIELDS",70],["FORMAT",71],["TOLABEL",72],["CONVERTTIMEZONE",73],["CONVERTCURRENCY",74],["GROUPING",75],["ALL",76],["CUSTOM",77],["STANDARD",78],["YESTERDAY",79],["TODAY",80],["TOMORROW",81],["LAST_WEEK",82],["THIS_WEEK",83],["NEXT_WEEK",84],["LAST_MONTH",85],["THIS_MONTH",86],["NEXT_MONTH",87],["LAST_90_DAYS",88],["NEXT_90_DAYS",89],["THIS_QUARTER",90],["LAST_QUARTER",91],["NEXT_QUARTER",92],["THIS_YEAR",93],["LAST_YEAR",94],["NEXT_YEAR",95],["THIS_FISCAL_QUARTER",96],["LAST_FISCAL_QUARTER",97],["NEXT_FISCAL_QUARTER",98],["THIS_FISCAL_YEAR",99],["LAST_FISCAL_YEAR",100],["NEXT_FISCAL_YEAR",101],["NEXT_N_DAYS",102],["LAST_N_DAYS",103],["N_DAYS_AGO",104],["NEXT_N_WEEKS",105],["LAST_N_WEEKS",106],["N_WEEKS_AGO",107],["NEXT_N_MONTHS",108],["LAST_N_MONTHS",109],["N_MONTHS_AGO",110],["NEXT_N_QUARTERS",111],["LAST_N_QUARTERS",112],["N_QUARTERS_AGO",113],["NEXT_N_YEARS",114],["LAST_N_YEARS",115],["N_YEARS_AGO",116],["NEXT_N_FISCAL_QUARTERS",117],["LAST_N_FISCAL_QUARTERS",118],["N_FISCAL_QUARTERS_AGO",119],["NEXT_N_FISCAL_YEARS",120],["LAST_N_FISCAL_YEARS",121],["N_FISCAL_YEARS_AGO",122],["DELEGATED",123],["EVERYTHING",124],["MINEANDMYGROUPS",125],["MINE",126],["MY_TERRITORY",127],["MY_TEAM_TERRITORY",128],["TEAM",129],["ALLPRIVATE",130],["MRU",131]]),h=[];(function(){for(let e of Object.keys(R)){let r=R[e];typeof r=="number"&&(h[r]=e)}h[72]="toLabel",h[73]="convertTimezone",h[74]="convertCurrency",h[157]="StringIdentifier",h[150]="DATE",h[32]="new",h[125]="MineAndMyGroups",h[127]="MyTerritory",h[128]="MyTeamTerritory",h[130]="AllPrivate",h[123]="Delegated",h[124]="Everything",h[126]="Mine",h[129]="Team",h[131]="Mru",h[159]="Identifier",h[132]="EQUAL",h[156]="UNSIGNED_INTEGER",h[155]="SIGNED_INTEGER",h[154]="UNSIGNED_DECIMAL",h[153]="SIGNED_DECIMAL",h[151]="CURRENCY_PREFIXED_DECIMAL",h[152]="CURRENCY_PREFIXED_INTEGER"})();function f(s){return h[s]||R[s]}function V(s){return s>=65&&s<=90||s>=97&&s<=122}function c(s){return s>=48&&s<=57}function W(s){return V(s)||c(s)||s===95}function G(s){return s===32||s===9||s===10||s===13||s===12}function oe(s){let e=[],r=s.length,t=0;for(;t<r;){let n=s.charCodeAt(t);if(G(n)){for(t++;t<r&&G(s.charCodeAt(t));)t++;continue}if(n===39){let a=t;for(t++;t<r;){let p=s.charCodeAt(t);if(p===92){t+=2;continue}if(p===39){t++;break}t++}let o=s.substring(a,t),u=o.substring(1,o.length-1).toUpperCase();u==="MI"||u==="KM"?e.push({kind:158,text:o,start:a}):e.push({kind:157,text:o,start:a});continue}if(c(n)){if(t+9<r&&s.charCodeAt(t+4)===45&&s.charCodeAt(t+7)===45){let o=!0;for(let l of[0,1,2,3,5,6,8,9])if(!c(s.charCodeAt(t+l))){o=!1;break}if(o)if(t+10<r&&(s.charCodeAt(t+10)===84||s.charCodeAt(t+10)===116)){let l=t;for(t+=11;t<r;){let u=s.charCodeAt(t);if(c(u)||u===58)t++;else break}if(t<r&&s.charCodeAt(t)===46)for(t++;t<r&&c(s.charCodeAt(t));)t++;if(t<r){let u=s.charCodeAt(t);if(u===90||u===122)t++;else if(u===46){for(t++;t<r&&c(s.charCodeAt(t));)t++;t<r&&(s.charCodeAt(t)===90||s.charCodeAt(t)===122)&&t++}else if(u===43||u===45)for(t++;t<r;){let p=s.charCodeAt(t);if(c(p)||p===58)t++;else break}}e.push({kind:149,text:s.substring(l,t),start:l});continue}else{let l=t;t+=10,e.push({kind:150,text:s.substring(l,t),start:l});continue}}let a=t;for(;t<r&&c(s.charCodeAt(t));)t++;if(t<r&&s.charCodeAt(t)===46&&t+1<r&&c(s.charCodeAt(t+1))){for(t++;t<r&&c(s.charCodeAt(t));)t++;e.push({kind:154,text:s.substring(a,t),start:a});continue}e.push({kind:156,text:s.substring(a,t),start:a});continue}if(n===33){if(t+1<r&&s.charCodeAt(t+1)===61){e.push({kind:133,text:"!=",start:t}),t+=2;continue}throw new Error(`Unexpected character '!' at position ${t}`)}if(n===60){if(t+1<r){let a=s.charCodeAt(t+1);if(a===61){e.push({kind:135,text:"<=",start:t}),t+=2;continue}if(a===62){e.push({kind:133,text:"<>",start:t}),t+=2;continue}}e.push({kind:134,text:"<",start:t}),t++;continue}if(n===62){if(t+1<r&&s.charCodeAt(t+1)===61){e.push({kind:137,text:">=",start:t}),t+=2;continue}e.push({kind:136,text:">",start:t}),t++;continue}if(n===61){e.push({kind:132,text:"=",start:t}),t++;continue}if(n===40){e.push({kind:138,text:"(",start:t}),t++;continue}if(n===41){e.push({kind:139,text:")",start:t}),t++;continue}if(n===91){e.push({kind:140,text:"[",start:t}),t++;continue}if(n===93){e.push({kind:141,text:"]",start:t}),t++;continue}if(n===44){e.push({kind:143,text:",",start:t}),t++;continue}if(n===58){e.push({kind:144,text:":",start:t}),t++;continue}if(n===59){e.push({kind:145,text:";",start:t}),t++;continue}if(n===42){e.push({kind:146,text:"*",start:t}),t++;continue}if(n===43||n===45){if(t+1<r){let a=s.charCodeAt(t+1);if(c(a)){let o=t;for(t++;t<r&&c(s.charCodeAt(t));)t++;if(t<r&&s.charCodeAt(t)===46&&t+1<r&&c(s.charCodeAt(t+1))){for(t++;t<r&&c(s.charCodeAt(t));)t++;e.push({kind:153,text:s.substring(o,t),start:o});continue}e.push({kind:155,text:s.substring(o,t),start:o});continue}if(a===46&&t+2<r&&c(s.charCodeAt(t+2))){let o=t;for(t+=2;t<r&&c(s.charCodeAt(t));)t++;e.push({kind:153,text:s.substring(o,t),start:o});continue}}e.push({kind:n===43?147:148,text:s[t],start:t}),t++;continue}if(n===46){if(t+1<r&&c(s.charCodeAt(t+1))){let a=t;for(t++;t<r&&c(s.charCodeAt(t));)t++;e.push({kind:154,text:s.substring(a,t),start:a});continue}e.push({kind:142,text:".",start:t}),t++;continue}if(V(n)){let a=t;for(t++;t<r;){let p=s.charCodeAt(t);if(W(p)||p===46)t++;else break}let o=s.substring(a,t);if(o.includes(".."))throw new Error(`Invalid identifier with consecutive dots at position ${a}: "${o}"`);let l=o.toUpperCase();if(l==="ORDER"&&!o.includes(".")){let p=$(s,t,"BY");if(p!==-1){let E=s.substring(a,p);e.push({kind:21,text:E,start:a}),t=p;continue}}if(l==="GROUP"&&!o.includes(".")){let p=$(s,t,"BY");if(p!==-1){let E=s.substring(a,p);e.push({kind:10,text:E,start:a}),t=p;continue}}if(l==="DATA"&&!o.includes(".")){let p=$(s,t,"CATEGORY");if(p!==-1){let E=s.substring(a,p);e.push({kind:35,text:E,start:a}),t=p;continue}}if(o.length>=4){let p=o.substring(0,3);if(re(p)){let E=o.substring(3);if(fe(E)){E.includes(".")?e.push({kind:151,text:o,start:a}):e.push({kind:152,text:o,start:a});continue}}}if(o.length===3&&re(o)){let p=t,E=t,C=!1;if(E<r&&s.charCodeAt(E)===45&&(C=!0,E++),C&&E<r&&c(s.charCodeAt(E))){for(;E<r&&c(s.charCodeAt(E));)E++;if(E<r&&s.charCodeAt(E)===46&&E+1<r&&c(s.charCodeAt(E+1))){for(E++;E<r&&c(s.charCodeAt(E));)E++;let d=s.substring(a,E);e.push({kind:151,text:d,start:a}),t=E;continue}if(E>=r||!W(s.charCodeAt(E))&&s.charCodeAt(E)!==46){let d=s.substring(a,E);e.push({kind:152,text:d,start:a}),t=E;continue}t=p}}if(o.includes(".")){e.push({kind:159,text:o,start:a});continue}let u=he.get(l);if(u!==void 0){e.push({kind:u,text:o,start:a});continue}e.push({kind:159,text:o,start:a});continue}throw new Error(`Unexpected character '${s[t]}' (code ${n}) at position ${t}`)}return e.push({kind:160,text:"",start:t}),e}function $(s,e,r){let t=s.length,n=e;if(n>=t||!G(s.charCodeAt(n)))return-1;for(;n<t&&G(s.charCodeAt(n));)n++;let a=r.length;if(n+a>t)return-1;for(let l=0;l<a;l++){let u=s.charCodeAt(n+l),p=r.charCodeAt(l);if((u>=97&&u<=122?u-32:u)!==p)return-1}let o=n+a;if(o<t){let l=s.charCodeAt(o);if(W(l)||l===46)return-1}return o}function re(s){for(let e=0;e<s.length;e++)if(!V(s.charCodeAt(e)))return!1;return!0}function fe(s){if(s.length===0)return!1;let e=0;if(s.charCodeAt(e)===45&&e++,e>=s.length||!c(s.charCodeAt(e)))return!1;for(;e<s.length&&c(s.charCodeAt(e));)e++;if(e<s.length&&s.charCodeAt(e)===46){if(e++,e>=s.length||!c(s.charCodeAt(e)))return!1;for(;e<s.length&&c(s.charCodeAt(e));)e++}return e===s.length}var Ae=new Set([23,9,26,21,10,11,16,27,28,29,25,19,2,3,0,20,17,45,41,5,36,42,143,139,160]),j=class s{constructor(e,r){this.pos=0;this.tokens=e,this.config=r}getPos(){return this.pos}peek(){return this.tokens[this.pos]}peekAt(e){let r=this.pos+e;return r>=this.tokens.length?this.tokens[this.tokens.length-1]:this.tokens[r]}advance(){let e=this.tokens[this.pos];return e.kind!==160&&this.pos++,e}expect(e){let r=this.peek();if(r.kind!==e)throw this.error(`Expected ${R[e]} but found ${R[r.kind]} ("${r.text}") at position ${r.start}`);return this.advance()}match(e){return this.peek().kind===e?(this.advance(),!0):!1}check(e){return this.peek().kind===e}isAtEnd(){return this.peek().kind===160}error(e){return this.config.logErrors&&console.log(e),new Error(e)}static{this.CLAUSE_BOUNDARY_KINDS=new Set([26,21,10,11,16,37,27,28,29,25,160])}isAtClauseBoundary(){let e=this.peek().kind;return s.CLAUSE_BOUNDARY_KINDS.has(e)||e===139}synchronize(){for(;!this.isAtEnd();){if(this.isAtClauseBoundary())return;this.advance()}}consumeIdentifier(){let e=this.peek();if(g(e.kind))return this.advance();throw this.error(`Expected identifier but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}checkIdentifier(){return g(this.peek().kind)}isFunctionCall(){let e=this.peek(),r=this.peekAt(1);return(y(e.kind)||F(e.kind)||B(e.kind)||H(e.kind)||b(e.kind))&&r.kind===138}canBeAlias(){let e=this.peek();return!(!g(e.kind)||Ae.has(e.kind)||e.kind===37&&this.peekAt(1).kind===156)}getLiteralType(e){let r=f(e);return r==="UNSIGNED_INTEGER"||r==="SIGNED_INTEGER"?"INTEGER":r==="UNSIGNED_DECIMAL"||r==="SIGNED_DECIMAL"?"DECIMAL":r==="CURRENCY_PREFIXED_DECIMAL"?"DECIMAL_WITH_CURRENCY_PREFIX":r==="CURRENCY_PREFIXED_INTEGER"?"INTEGER_WITH_CURRENCY_PREFIX":r==="DATETIME"?"DATETIME":r==="DATE"?"DATE":r==="NULL"?"NULL":r==="StringIdentifier"||r==="Identifier"?"STRING":r==="TRUE"||r==="FALSE"?"BOOLEAN":M(e)?"DATE_LITERAL":P(e)?"DATE_N_LITERAL":"STRING"}parseSelectStatement(e=!1){let r=this.parseSelectClause(),t=this.parseFromClause(),n;e?(n={fields:r,relationshipName:t.sObject},t.sObjectPrefix&&(n.sObjectPrefix=t.sObjectPrefix)):n={fields:r,sObject:t.sObject},t.alias&&(n.sObjectAlias=t.alias),this.applySObjectAlias(n);let a=this.parseClauseStatements();return Object.assign(n,a),n}parseSelectStatementPartial(){let e,r;this.check(23)&&(e=this.parseSelectClause()),this.check(9)&&(r=this.parseFromClause());let t={};e&&(t.fields=e),r&&(t.sObject=r.sObject,r.alias&&(t.sObjectAlias=r.alias)),t.fields&&this.applySObjectAlias(t);let n=this.parseClauseStatements();return Object.assign(t,n),t}applySObjectAlias(e){if(!e.sObjectAlias||!Array.isArray(e.fields))return;let r=e.sObjectAlias;e.fields.forEach(t=>{t.relationships&&t.relationships[0]===r&&(t.relationships=t.relationships.slice(1),t.objectPrefix=r,t.relationships.length===0&&(delete t.relationships,t.type="Field"))})}parseSelectClause(){this.expect(23);let e=[];for(e.push(this.parseSelectField());this.match(143);)e.push(this.parseSelectField());return e}parseSelectField(){let e=this.peek(),r=this.peekAt(1);return e.kind===138&&r.kind===23?this.parseSelectClauseSubqueryIdentifier():e.kind===42?this.parseSelectClauseTypeOf():this.isFunctionCall()?this.parseSelectClauseFunctionIdentifier():this.parseSelectClauseIdentifier()}parseSelectClauseFunctionIdentifier(){let e=this.parseAnyFunction(!0);return this.canBeAlias()&&(e.alias=this.consumeIdentifier().text),e}parseSelectClauseSubqueryIdentifier(){this.expect(138);let e=this.parseSelectStatement(!0);return this.expect(139),{type:"FieldSubquery",subquery:e}}parseSelectClauseTypeOf(){this.expect(42);let e=this.consumeIdentifier().text,r=[];for(;this.check(45);){this.advance();let t=this.consumeIdentifier().text;this.expect(41);let n=[];for(n.push(this.consumeIdentifier().text);this.match(143);)n.push(this.consumeIdentifier().text);r.push({type:"WHEN",objectType:t,fieldList:n})}if(this.check(5)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);r.push({type:"ELSE",fieldList:t})}return this.expect(36),{type:"FieldTypeof",field:e,conditions:r}}parseSelectClauseIdentifier(){let r=this.consumeIdentifier().text,t;if(!r.includes("."))t={type:"Field",field:r};else{let n=r.split(".");t={type:"FieldRelationship",field:n[n.length-1],relationships:n.slice(0,n.length-1),rawValue:r}}return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseFromClause(){this.expect(9);let r=this.consumeIdentifier().text,t;if(r.includes(".")){let n=r.split(".");t={sObjectPrefix:n.slice(0,n.length-1),sObject:n[n.length-1]}}else t={sObject:r};return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseClauseStatements(){let e={},r=(t,n)=>{if(this.config.ignoreParseErrors)try{t(),this.isAtClauseBoundary()||(n&&n(),this.synchronize())}catch(a){this.config.logErrors&&console.log(a),n&&n(),this.synchronize()}else t()};for(this.check(25)&&r(()=>{e.usingScope=this.parseUsingScope()}),this.check(26)&&r(()=>{e.where=this.parseWhereClause()},()=>{delete e.where});this.check(27);)r(()=>{let t=this.parseWithClause();t.withSecurityEnforced&&(e.withSecurityEnforced=!0),t.withAccessLevel&&(e.withAccessLevel=t.withAccessLevel),t.withDataCategory&&(e.withDataCategory=t.withDataCategory)});return this.check(10)&&(r(()=>{e.groupBy=this.parseGroupByClause()}),this.check(11)&&r(()=>{e.having=this.parseHavingClause()})),this.check(21)&&r(()=>{e.orderBy=this.parseOrderByClause()}),this.check(16)&&r(()=>{this.advance(),e.limit=Number(this.expect(156).text)}),this.check(37)&&r(()=>{this.advance(),e.offset=Number(this.expect(156).text)}),this.check(28)&&r(()=>{this.advance();let t=this.peek();if(t.kind===43||t.kind===38||t.kind===29)e.for=f(this.advance().kind);else throw this.error(`Expected VIEW, REFERENCE, or UPDATE after FOR at position ${t.start}`)}),this.check(29)&&r(()=>{this.advance();let t=this.peek();if(t.kind===40||t.kind===44)e.update=f(this.advance().kind);else throw this.error(`Expected TRACKING or VIEWSTAT after UPDATE at position ${t.start}`)}),e}parseUsingScope(){this.expect(25),this.expect(39);let e=this.peek();if(ne(e.kind))return this.advance().text;if(g(e.kind))return this.advance().text;throw this.error(`Expected scope enumeration value at position ${e.start}`)}parseWhereClause(){return this.expect(26),this.parseConditionExpressions(!0,!0)}parseConditionExpressions(e,r){let t,n,a={left:0,right:0};for(;;){let o;if(t)if(this.check(0))o="AND",this.advance();else if(this.check(20))o="OR",this.advance();else break;let l={},u=l,p=null;for(;this.hasNegationPrefix();){let d=0;for(;this.check(138)&&this.isNegationAhead();)this.advance(),d++,a.left++;if(this.check(17)){this.advance();let I={left:d>0?{openParen:d}:null,operator:"NOT",right:{left:{}}};p?p.right=I:l=I,p=I,u=I.right}else break}u.left=this.parseExpression(e,r,a),t?o&&(n.operator=o,n.right=l):(t=l,n=l);let E=l;for(;E.right&&E.right.left!==void 0;){let d=E.right;if(d.right&&d.right.left!==void 0)E=d;else{E=d;break}}if(n=E,this.isAtEnd())break;let C=this.peek().kind;if(C!==0&&C!==20)break}if(a.left!==a.right)throw this.error(`Mismatched parentheses in WHERE/HAVING clause: ${a.left} open vs ${a.right} close`);return t}hasNegationPrefix(){let e=0;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}isNegationAhead(){let e=1;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}parseExpression(e,r,t){let n=0;for(;this.check(138)&&!this.isSubqueryStart()&&!this.isArrayExpressionStart();)this.advance(),n++,t&&t.left++;let a,o=!1;e&&F(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseAggregateFunction(!1),o=!0):r&&B(this.peek().kind)&&this.peek().kind===68&&this.peekAt(1).kind===138?(a=this.parseLocationFunction(!1),o=!0):y(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseDateFunction(!1),o=!0):b(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseOtherFunction(!1),o=!0):a=this.consumeIdentifier().text;let l=this.parseOperator(),u=l.operator,p=l.isSet,E,C,d;if(p)if(this.check(138)&&this.peekAt(1).kind===23)this.expect(138),E=this.parseSelectStatement(!1),this.expect(139),C="SUBQUERY";else if(this.config.allowApexBindVariables&&this.check(144))E=this.parseApexBindVariable().value,C="APEX_BIND_VARIABLE";else{let L=this.parseArrayExpression();E=L.values,C=L.literalType,d=L.dateLiteralVariable}else{let L=this.parseAtomicValue();E=L.value,C=L.literalType,d=L.dateLiteralVariable}let I=0;for(;this.check(139)&&(!t||t.left>t.right);)this.advance(),I++,t&&t.right++;let S={};return o?S.fn=a:S.field=a,S.operator=u,C==="SUBQUERY"?S.valueQuery=E:S.value=E,S.literalType=C,d!==void 0&&(S.dateLiteralVariable=d),n>0&&(S.openParen=n),I>0&&(S.closeParen=I),S}isSubqueryStart(){return this.check(138)&&this.peekAt(1).kind===23}isArrayExpressionStart(){return!1}parseOperator(){let e=this.peek();if(ae(e.kind))return{operator:this.advance().text,isSet:!1};if(e.kind===15)return this.advance(),{operator:"LIKE",isSet:!1};if(e.kind===12)return this.advance(),{operator:"IN",isSet:!0};if(e.kind===17&&this.peekAt(1).kind===12)return this.advance(),this.advance(),{operator:"NOT IN",isSet:!0};if(e.kind===13)return this.advance(),{operator:"INCLUDES",isSet:!0};if(e.kind===6)return this.advance(),{operator:"EXCLUDES",isSet:!0};throw this.error(`Expected operator but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseAtomicValue(){let e=this.peek();if(this.config.allowApexBindVariables&&e.kind===144)return{value:this.parseApexBindVariable().value,literalType:"APEX_BIND_VARIABLE"};if(e.kind===157)return{value:this.advance().text,literalType:"STRING"};if(e.kind===158)return{value:this.advance().text,literalType:"STRING"};if(e.kind===156)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===155)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===154)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===153)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===151)return{value:this.advance().text,literalType:"DECIMAL_WITH_CURRENCY_PREFIX"};if(e.kind===152)return{value:this.advance().text,literalType:"INTEGER_WITH_CURRENCY_PREFIX"};if(e.kind===149)return{value:this.advance().text,literalType:"DATETIME"};if(e.kind===150)return{value:this.advance().text,literalType:"DATE"};if(e.kind===18)return this.advance(),{value:"NULL",literalType:"NULL"};if(e.kind===24||e.kind===7)return{value:f(this.advance().kind),literalType:"BOOLEAN"};if(M(e.kind))return{value:this.advance().text,literalType:"DATE_LITERAL"};if(P(e.kind)){let r=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${r.text}:${n}`,literalType:"DATE_N_LITERAL",dateLiteralVariable:n}}throw this.error(`Expected value but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseArrayExpression(){this.expect(138);let e=[];for(e.push(this.parseArrayItem());this.match(143);)e.push(this.parseArrayItem());this.expect(139);let r=e.map(u=>u.value),t=e.map(u=>u.type),n=e.map(u=>u.variable!==void 0?u.variable:null),a=n.some(u=>u!==null),o;new Set(t).size===1?o=this.getLiteralType(this.tokenKindFromTypeName(t[0])):o=t.map(u=>this.getLiteralType(this.tokenKindFromTypeName(u)));let l={values:r,literalType:o};return a&&(l.dateLiteralVariable=n),l}tokenKindFromTypeName(e){for(let r=0;r<=160;r++)if(f(r)===e)return r;return 159}parseArrayItem(){let e=this.peek();if(e.kind===157)return{value:this.advance().text,type:f(157)};if(e.kind===158)return{value:this.advance().text,type:f(158)};if(e.kind===151)return{value:this.advance().text,type:f(151)};if(e.kind===152)return{value:this.advance().text,type:f(152)};if(Q(e.kind)){let r=this.advance();return{value:r.text,type:f(r.kind)}}if(e.kind===149)return{value:this.advance().text,type:f(149)};if(e.kind===150)return{value:this.advance().text,type:f(150)};if(e.kind===18)return this.advance(),{value:"NULL",type:"NULL"};if(e.kind===24)return{value:this.advance().text,type:"TRUE"};if(e.kind===7)return{value:this.advance().text,type:"FALSE"};if(M(e.kind))return{value:this.advance().text,type:f(e.kind)};if(P(e.kind)){let r=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${r.text}:${n}`,type:f(r.kind),variable:n}}throw this.error(`Expected array value but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseApexBindVariable(){this.expect(144);let e=[];for(this.check(32)?e.push(this.parseApexBindNewInstantiation()):this.checkIdentifier()&&(this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):e.push(this.parseApexBindIdentifier()));this.check(142);)this.advance(),this.checkIdentifier()&&this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):this.checkIdentifier()&&e.push(this.parseApexBindIdentifier());return{value:e.join(".")}}parseApexBindIdentifier(){let e=this.consumeIdentifier().text;return this.check(140)&&(e+=this.parseApexBindArrayAccessor()),e}parseApexBindNewInstantiation(){this.expect(32);let r=`new ${this.consumeIdentifier().text}`;if(this.check(134)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);this.expect(136),r+=`<${t.join(", ")}>`}return r+=this.parseApexBindFunctionParams(),this.check(140)&&(r+=this.parseApexBindArrayAccessor()),r}parseApexBindFunctionCall(){let r=this.consumeIdentifier().text+this.parseApexBindFunctionParams();return this.check(140)&&(r+=this.parseApexBindArrayAccessor()),r}parseApexBindFunctionParams(){this.expect(138);let e=[];if(!this.check(139))for(e.push(this.consumeIdentifier().text);this.match(143);)e.push(this.consumeIdentifier().text);return this.expect(139),`(${e.join(", ")})`}parseApexBindArrayAccessor(){this.expect(140);let e=this.peek(),r;if(e.kind===156)r=this.advance().text;else if(g(e.kind))r=this.advance().text;else throw this.error(`Expected integer or identifier in array accessor at position ${e.start}`);return this.expect(141),`[${r}]`}parseAnyFunction(e){let r=this.peek();if(y(r.kind))return this.parseDateFunction(e);if(F(r.kind))return this.parseAggregateFunction(e);if(r.kind===68)return this.parseLocationFunction(e);if(H(r.kind))return this.parseFieldsFunction(e);if(b(r.kind))return this.parseOtherFunction(e);throw this.error(`Expected function name but found ${R[r.kind]} at position ${r.start}`)}parseDateFunction(e){let r=this.advance(),t=f(r.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${r.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseAggregateFunction(e){let r=this.advance(),t=f(r.kind),n=this.parseFunctionExpression(!0,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${r.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseFieldsFunction(e){this.advance(),this.expect(138);let r=this.peek(),t;if(se(r.kind))t=this.advance().text;else if(g(r.kind))t=this.advance().text;else throw this.error(`Expected FIELDS function parameter at position ${r.start}`);this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="FIELDS",n.parameters=[t],n.rawValue=`FIELDS(${t})`,n}parseOtherFunction(e){let r=this.advance(),t=f(r.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,a.rawValue=`${r.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseLocationFunction(e){this.advance(),this.expect(138);let r=this.consumeIdentifier().text;this.expect(143);let t;this.check(69)&&this.peekAt(1).kind===138?t=this.parseGeolocationFunction(e):t=this.consumeIdentifier().text,this.expect(143);let n=this.expect(158).text;this.expect(139);let a={};e&&(a.type="FieldFunctionExpression"),a.functionName="DISTANCE",a.parameters=[r,t,n],e&&(a.isAggregateFn=!0);let o=typeof t=="string"?t:t.rawValue;return a.rawValue=`DISTANCE(${r}, ${o}, ${n})`,a}parseGeolocationFunction(e){this.advance(),this.expect(138);let r=this.parseNumberValue();this.expect(143);let t=this.parseNumberValue();this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="GEOLOCATION",n.parameters=[r,t],n.rawValue=`GEOLOCATION(${r}, ${t})`,n}parseNumberValue(){let e=this.peek();if(e.kind===156||e.kind===155||e.kind===154||e.kind===153)return this.advance().text;throw this.error(`Expected number but found ${R[e.kind]} ("${e.text}") at position ${e.start}`)}parseFunctionExpression(e,r=!0){this.expect(138);let t=[];if(!this.check(139))for(t.push(this.parseFunctionParam(e,r));this.match(143);)t.push(this.parseFunctionParam(e,r));return this.expect(139),t}parseFunctionParam(e,r){let t=this.peek(),n=this.peekAt(1);if(!e&&F(t.kind)&&n.kind===138)return this.parseAggregateFunction(r);if(b(t.kind)&&n.kind===138)return this.parseOtherFunction(r);if(t.kind===157)return this.advance().text;if(Q(t.kind))return this.advance().text;if(g(t.kind))return this.advance().text;throw this.error(`Expected function parameter but found ${R[t.kind]} ("${t.text}") at position ${t.start}`)}parseGroupByClause(){this.expect(10);let e=[];for(e.push(this.parseGroupByItem());this.match(143);)e.push(this.parseGroupByItem());return e}parseGroupByItem(){let e=this.peek();return e.kind===4&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("CUBE")}:e.kind===22&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("ROLLUP")}:y(e.kind)&&this.peekAt(1).kind===138?{fn:this.parseDateFunction(!1)}:{field:this.consumeIdentifier().text}}parseCubeOrRollupFunction(e){let r=this.advance(),t=this.parseFunctionExpression(!1);return{functionName:e,parameters:t,rawValue:`${e}(${t.join(", ")})`}}parseHavingClause(){return this.expect(11),this.parseConditionExpressions(!0,!1)}parseOrderByClause(){this.expect(21);let e=[];for(e.push(this.parseOrderByItem());this.match(143);)e.push(this.parseOrderByItem());return e}parseOrderByItem(){let e=this.peek(),r=this.peekAt(1),t;if(e.kind===75&&r.kind===138){let n=this.advance(),a=this.parseFunctionExpression(!1);t={fn:{functionName:f(n.kind),parameters:a,rawValue:`${n.text}(${a.join(", ")})`}}}else F(e.kind)&&r.kind===138?t={fn:this.parseAggregateFunction(!1)}:y(e.kind)&&r.kind===138?t={fn:this.parseDateFunction(!1)}:e.kind===68&&r.kind===138?t={fn:this.parseLocationFunction(!1)}:t={field:this.consumeIdentifier().text};if(this.check(2)?t.order=f(this.advance().kind):this.check(3)&&(t.order=f(this.advance().kind)),this.check(19))if(this.advance(),this.check(8))t.nulls=f(this.advance().kind);else if(this.check(14))t.nulls=f(this.advance().kind);else throw this.error(`Expected FIRST or LAST after NULLS at position ${this.peek().start}`);return t}parseWithClause(){this.expect(27);let e=this.peek();if(e.kind===46)return this.advance(),{withSecurityEnforced:!0};if(e.kind===48)return this.advance(),{withAccessLevel:"USER_MODE"};if(e.kind===47)return this.advance(),{withAccessLevel:"SYSTEM_MODE"};if(e.kind===35)return{withDataCategory:this.parseWithDataCategory()};throw this.error(`Expected SECURITY_ENFORCED, USER_MODE, SYSTEM_MODE, or DATA CATEGORY after WITH at position ${e.start}`)}parseWithDataCategory(){this.expect(35);let e=[];for(e.push(this.parseDataCategoryCondition());this.check(0);)this.advance(),e.push(this.parseDataCategoryCondition());return{conditions:e}}parseDataCategoryCondition(){let e=this.consumeIdentifier().text,r=this.peek(),t;if(r.kind===33||r.kind===30||r.kind===34||r.kind===31)t=this.advance().text;else throw this.error(`Expected data category selector (AT, ABOVE, BELOW, ABOVE_OR_BELOW) at position ${r.start}`);let n=this.match(138),a=[];for(a.push(this.consumeIdentifier().text);this.match(143);)a.push(this.consumeIdentifier().text);return n&&this.expect(139),{groupName:e,selector:t,parameters:a}}};function x(s,e){let r=e||{},t=oe(s),n=new j(t,r),a;if(r.allowPartialQuery?a=n.parseSelectStatementPartial():a=n.parseSelectStatement(!1),!n.isAtEnd()&&!r.ignoreParseErrors){let o=t[n.getPos()];throw new Error(`Unexpected token "${o.text}" at position ${o.start}. Expected end of query.`)}return a}function X(s,e){try{return x(s,e),!0}catch{return!1}}function _(s){return typeof s=="string"}function O(s){return Number.isFinite(s)}function T(s){return s==null}function m(s,e,r){return T(s)?"":`${r||""}${s}${e||""}`}function le(s,e=!1){return Array.isArray(s)?s.length>0?`(${s.join(", ")})`:e?"()":"":e?`(${s||""})`:s||""}function ue(s,e,r=""){return O(s)&&s>0?new Array(s).fill(e).join(r):""}function q(s){return s&&!T(s.alias)}function D(s){return _(s.relationshipName)}function K(s){return!!s&&!!s.operator&&!!s.right}function J(s){return s&&!T(s.fn)&&_(s.operator)&&!T(s.value)}function k(s){return s&&O(s.openParen)&&T(s.operator)&&T(s.field)&&T(s.fn)&&T(s.closeParen)}function z(s){return s&&_(s.field)&&_(s.operator)&&!T(s.valueQuery)&&T(s.value)}function Z(s){return s&&!T(s.field)}function ee(s){return s&&!T(s.field)}function de(s){return["IN","NOT IN","INCLUDES","EXCLUDES"].includes(s)}function Ee(s,e,r){if(T(e))return s;if(r&&e!=="APEX_BIND_VARIABLE"&&de(r)&&!Array.isArray(s)&&(s=[s],e=Array.isArray(e)?e:[e]),Array.isArray(e)&&Array.isArray(s))return s.map((t,n)=>Ne(t,e?.[n]));switch(Array.isArray(e)&&(e=e[0]),e){case"STRING":return Array.isArray(s)?s.filter(Boolean).map(t=>_(t)&&t.startsWith("'")?t:`'${t??""}'`):(s=String(s??""),_(s)&&s.startsWith("'")?s:`'${s??""}'`);case"APEX_BIND_VARIABLE":return`:${s}`;default:return s}}function Ne(s,e){switch(e){case"STRING":return _(s)&&s.startsWith("'")?s:`'${s??""}'`;case"NULL":return"null";default:return s}}var v=class{constructor(e,r){this.currIndent=1;this.enabled=e,this.options={numIndent:r.numIndent??1,fieldMaxLineLength:r.fieldMaxLineLength??60,fieldSubqueryParensOnOwnLine:r.fieldSubqueryParensOnOwnLine??!0,whereClauseOperatorsIndented:!0,newLineAfterKeywords:r.newLineAfterKeywords??!1,logging:r.logging??!1},this.options.newLineAfterKeywords&&(this.options.fieldSubqueryParensOnOwnLine=!0)}log(e){this.options.logging&&console.log(e)}getIndent(e=0){return this.repeatChar((this.currIndent+e)*(this.options.numIndent||1)," ")}repeatChar(e,r){return new Array(e).fill(r).join("")}setSubquery(e){this.currIndent=e?this.currIndent+=1:this.currIndent-=1}stepCurrIndex(e){this.currIndent+=e}formatFields(e){function r(t){e.fields[t-1]&&(e.fields[t-1].suffix=e.fields[t-1].suffix.trim())}if(e.fields.forEach((t,n)=>{t.suffix=e.fields.length-1===n?"":", "}),this.enabled){let t=0,n=!1;e.fields.forEach((a,o)=>{a.isSubquery?(r(o),a.prefix=`
2
2
  ${this.getIndent()}`,a.suffix=e.fields.length-1===o?"":", ",t=0,n=!0):Array.isArray(a.typeOfClause)?(r(o),a.prefix=`
3
3
  ${this.getIndent()}`,n=!0):O(this.options.fieldMaxLineLength)&&(t+=a.text.length+a.suffix.length,(t>this.options.fieldMaxLineLength||n)&&(r(o),(!this.options.newLineAfterKeywords||o>0)&&(a.prefix+=`
4
4
  ${this.getIndent()}`),t=0,n=!1)),this.log(a)})}}formatTyeOfField(e,r){return this.enabled&&this.options.newLineAfterKeywords?r.map((t,n)=>n===0?t:n===r.length-1?`${this.getIndent()}${t}`:`${this.getIndent()} ${t}`).join(`
@@ -18,9 +18,9 @@ ${e}`:` ${e}`}formatText(e){return this.enabled&&(this.options.newLineAfterKeywo
18
18
  `,r=0):t+=`${n}${o?", ":""}`}),t}else return e.join(", ")}formatParens(e,r,t=!1){let n="";if(O(e)&&e>0)if(this.enabled)if(r==="("){for(let a=0;a<e;a++)t&&a===e-1?n+="(":a===0?n+=`(
19
19
  `:(this.currIndent++,n+=`${this.getIndent()}(
20
20
  `);(!t||e>1)&&this.currIndent++}else for(let a=e-1;a>=0;a--)t&&a===e-1?n+=")":(this.currIndent--,n+=`
21
- ${this.getIndent()})`);else n+=Ee(e,r);return n}formatWhereClauseOperators(e,r,t=0){let n=e==="NOT";return this.enabled&&!n?`
21
+ ${this.getIndent()})`);else n+=ue(e,r);return n}formatWhereClauseOperators(e,r,t=0){let n=e==="NOT";return this.enabled&&!n?`
22
22
  ${this.getIndent(t)}${e} ${r}`:`${n?"":" "}${e} ${r}`}formatAddNewLine(e=" ",r){return this.enabled&&!r?`
23
- `:e}};function ce(s,e,r){return re(x(s,r),{format:!0,formatOptions:e})}function re(s,e={}){if(!s)return"";e=e||{},e.format=!!e.format,e.logging&&(console.time("composer"),console.log("Composing Query:",s),console.log("Format output:",e.format));let r=new ie(s,e).query;return e.logging&&console.timeEnd("composer"),r}var ie=class{constructor(e,r={}){this.soql=e;this.logging=!1;this.format=!1;r={autoCompose:!0,...r};let{logging:t,format:n}=r;this.logging=!!t,this.format=!!n,this.query="",this.formatter=new w(this.format,{logging:this.logging,...r.formatOptions}),r.autoCompose&&this.start()}start(){this.query=this.parseQuery(this.soql)}log(e){this.logging&&console.log("Current SOQL:",e)}parseFn(e){let r;return e.rawValue?r=e.rawValue:(r=e.functionName,r+=`(${(e.parameters||[]).map(t=>_(t)?t:this.parseFn(t)).join(", ")})`),e.alias&&(r+=` ${e.alias}`),r}parseQuery(e){let r={fields:this.parseFields(e.fields||[]).map(a=>({text:a.text,typeOfClause:a.typeOfClause,isSubquery:a.text.startsWith("("),prefix:"",suffix:""})),isSubquery:k(e),lineBreaks:[]},t="";e.fields&&(t+=this.formatter.formatClause("SELECT").trimStart()),this.formatter.formatFields(r);let n="";if(r.fields.forEach(a=>{Array.isArray(a.typeOfClause)?n+=`${a.prefix}${this.formatter.formatTyeOfField(a.text,a.typeOfClause)}${a.suffix}`:n+=`${a.prefix}${a.text}${a.suffix}`}),t+=this.formatter.formatText(n),(k(e)?e.relationshipName:e.sObject)&&(t+=this.formatter.formatClause("FROM")),k(e)){let a=e.sObjectPrefix||[];a.push(e.relationshipName),t+=this.formatter.formatText(`${a.join(".")}${m(e.sObjectAlias,""," ")}`)}else e.sObject&&(t+=this.formatter.formatText(`${e.sObject}${m(e.sObjectAlias,""," ")}`));return this.log(t),e.usingScope&&(t+=this.formatter.formatClause("USING SCOPE"),t+=this.formatter.formatText(e.usingScope),this.log(t)),e.where&&(t+=this.formatter.formatClause("WHERE"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.where)),this.log(t)),e.groupBy&&(t+=this.formatter.formatClause("GROUP BY"),t+=this.formatter.formatText(this.parseGroupByClause(e.groupBy)),this.log(t),e.having&&(t+=this.formatter.formatClause("HAVING"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.having)),this.log(t))),e.orderBy&&(!Array.isArray(e.orderBy)||e.orderBy.length>0)&&(t+=this.formatter.formatClause("ORDER BY"),t+=this.formatter.formatText(this.parseOrderBy(e.orderBy)),this.log(t)),O(e.limit)&&(t+=this.formatter.formatClause("LIMIT"),t+=this.formatter.formatText(`${e.limit}`),this.log(t)),O(e.offset)&&(t+=this.formatter.formatClause("OFFSET"),t+=this.formatter.formatText(`${e.offset}`),this.log(t)),e.withDataCategory&&(t+=this.formatter.formatClause("WITH DATA CATEGORY"),t+=this.formatter.formatText(this.parseWithDataCategory(e.withDataCategory)),this.log(t)),e.withSecurityEnforced&&(t+=this.formatter.formatClause("WITH SECURITY_ENFORCED"),this.log(t)),e.withAccessLevel&&(t+=this.formatter.formatClause(`WITH ${e.withAccessLevel}`),this.log(t)),e.for&&(t+=this.formatter.formatClause("FOR"),t+=this.formatter.formatText(e.for),this.log(t)),e.update&&(t+=this.formatter.formatClause("UPDATE"),t+=this.formatter.formatText(e.update),this.log(t)),t.trim()}parseFields(e){return e.map(r=>{let t="",n,a=r.objectPrefix?`${r.objectPrefix}.`:"";switch(r.type){case"Field":{t=`${a}${r.field}${r.alias?` ${r.alias}`:""}`;break}case"FieldFunctionExpression":{let o="";r.parameters&&(o=r.parameters.map(l=>_(l)?l:this.parseFields([l]).map(u=>u.text)).join(", ")),t=`${r.functionName}(${o})${r.alias?` ${r.alias}`:""}`;break}case"FieldRelationship":{t=`${a}${r.relationships.join(".")}.${r.field}${K(r)?` ${r.alias}`:""}`;break}case"FieldSubquery":{t=this.formatter.formatSubquery(this.parseQuery(r.subquery));break}case"FieldTypeof":{n=this.parseTypeOfField(r),t=n.join(" ");break}default:break}return{text:t,typeOfClause:n}})}parseTypeOfField(e){let r=[`TYPEOF ${e.field}`].concat(e.conditions.map(t=>this.formatter.formatTypeofFieldCondition(t)));return r.push("END"),r}parseWhereOrHavingClause(e,r=0,t=!1){let n="",a=e.left,o=!1;if(a&&(n+=this.formatter.formatParens(a.openParen,"(",U(a)),!U(a))){r=r+(a.openParen||0)-(a.closeParen||0),t&&r++;let l="";l+=z(a)?this.parseFn(a.fn):a.field,l+=` ${a.operator} `,Z(a)?l+=this.formatter.formatSubquery(this.parseQuery(a.valueQuery),1,!0):l+=ue(pe(a.value,a.literalType,a.operator)),n+=this.formatter.formatWithIndent(l),n+=this.formatter.formatParens(a.closeParen,")",t)}if(J(e)){let l=m(e.operator);o=l==="NOT";let u=this.formatter.formatWhereClauseOperators(l,this.parseWhereOrHavingClause(e.right,r,U(a)),r);return`${o?n.trimRight():n}${u}`.trim()}else return n.trim()}parseGroupByClause(e){return(Array.isArray(e)?e:[e]).map(r=>te(r)?r.field:this.parseFn(r.fn)).join(", ")}parseOrderBy(e){if(Array.isArray(e))return this.formatter.formatOrderByArray(e.map(r=>this.parseOrderBy(r)));{let r="";return ee(e)?r=`${m(e.field," ")}`:r+=`${this.parseFn(e.fn)} `,r+=`${m(e.order," ")}${m(e.nulls,"","NULLS ")}`,r.trim()}}parseWithDataCategory(e){return e.conditions.map(r=>{let t=r.parameters.length>1?`(${r.parameters.join(", ")})`:`${r.parameters.join(", ")}`;return`${r.groupName} ${r.selector} ${t}`}).join(" AND ")}};function Ce(s){let e=s.slice(2),r=e[0]??"",t=e[1]??"",n={};for(let a=2;a<e.length;a++){let o=e[a];if(o.startsWith("--")){let l=o.slice(2),u=e[a+1];u&&!u.startsWith("-")?(n[l]=u,a++):n[l]=!0}else if(o.startsWith("-")&&o.length===2){let l=o.slice(1),u=e[a+1];u&&!u.startsWith("-")?(n[l]=u,a++):n[l]=!0}}return{command:r,query:t,flags:n}}function N(s,e,r){return s[e]===!0||s[r]===!0}function Y(s,e,r){let t=s[e]??s[r];return typeof t=="string"?t:void 0}function he(){console.log(`Usage: soql-parser-js <command> <query> [options]
23
+ `:e}};function pe(s,e,r){return ie(x(s,r),{format:!0,formatOptions:e})}function ie(s,e={}){if(!s)return"";e=e||{},e.format=!!e.format,e.logging&&(console.time("composer"),console.log("Composing Query:",s),console.log("Format output:",e.format));let r=new te(s,e).query;return e.logging&&console.timeEnd("composer"),r}var te=class{constructor(e,r={}){this.soql=e;this.logging=!1;this.format=!1;r={autoCompose:!0,...r};let{logging:t,format:n}=r;this.logging=!!t,this.format=!!n,this.query="",this.formatter=new v(this.format,{logging:this.logging,...r.formatOptions}),r.autoCompose&&this.start()}start(){this.query=this.parseQuery(this.soql)}log(e){this.logging&&console.log("Current SOQL:",e)}parseFn(e){let r;return e.rawValue?r=e.rawValue:(r=e.functionName,r+=`(${(e.parameters||[]).map(t=>_(t)?t:this.parseFn(t)).join(", ")})`),e.alias&&(r+=` ${e.alias}`),r}parseQuery(e){let r={fields:this.parseFields(e.fields||[]).map(a=>({text:a.text,typeOfClause:a.typeOfClause,isSubquery:a.text.startsWith("("),prefix:"",suffix:""})),isSubquery:D(e),lineBreaks:[]},t="";e.fields&&(t+=this.formatter.formatClause("SELECT").trimStart()),this.formatter.formatFields(r);let n="";if(r.fields.forEach(a=>{Array.isArray(a.typeOfClause)?n+=`${a.prefix}${this.formatter.formatTyeOfField(a.text,a.typeOfClause)}${a.suffix}`:n+=`${a.prefix}${a.text}${a.suffix}`}),t+=this.formatter.formatText(n),(D(e)?e.relationshipName:e.sObject)&&(t+=this.formatter.formatClause("FROM")),D(e)){let a=e.sObjectPrefix||[];a.push(e.relationshipName),t+=this.formatter.formatText(`${a.join(".")}${m(e.sObjectAlias,""," ")}`)}else e.sObject&&(t+=this.formatter.formatText(`${e.sObject}${m(e.sObjectAlias,""," ")}`));return this.log(t),e.usingScope&&(t+=this.formatter.formatClause("USING SCOPE"),t+=this.formatter.formatText(e.usingScope),this.log(t)),e.where&&(t+=this.formatter.formatClause("WHERE"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.where)),this.log(t)),e.groupBy&&(t+=this.formatter.formatClause("GROUP BY"),t+=this.formatter.formatText(this.parseGroupByClause(e.groupBy)),this.log(t),e.having&&(t+=this.formatter.formatClause("HAVING"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.having)),this.log(t))),e.orderBy&&(!Array.isArray(e.orderBy)||e.orderBy.length>0)&&(t+=this.formatter.formatClause("ORDER BY"),t+=this.formatter.formatText(this.parseOrderBy(e.orderBy)),this.log(t)),O(e.limit)&&(t+=this.formatter.formatClause("LIMIT"),t+=this.formatter.formatText(`${e.limit}`),this.log(t)),O(e.offset)&&(t+=this.formatter.formatClause("OFFSET"),t+=this.formatter.formatText(`${e.offset}`),this.log(t)),e.withDataCategory&&(t+=this.formatter.formatClause("WITH DATA CATEGORY"),t+=this.formatter.formatText(this.parseWithDataCategory(e.withDataCategory)),this.log(t)),e.withSecurityEnforced&&(t+=this.formatter.formatClause("WITH SECURITY_ENFORCED"),this.log(t)),e.withAccessLevel&&(t+=this.formatter.formatClause(`WITH ${e.withAccessLevel}`),this.log(t)),e.for&&(t+=this.formatter.formatClause("FOR"),t+=this.formatter.formatText(e.for),this.log(t)),e.update&&(t+=this.formatter.formatClause("UPDATE"),t+=this.formatter.formatText(e.update),this.log(t)),t.trim()}parseFields(e){return e.map(r=>{let t="",n,a=r.objectPrefix?`${r.objectPrefix}.`:"";switch(r.type){case"Field":{t=`${a}${r.field}${r.alias?` ${r.alias}`:""}`;break}case"FieldFunctionExpression":{let o="";r.parameters&&(o=r.parameters.map(l=>_(l)?l:this.parseFields([l]).map(u=>u.text)).join(", ")),t=`${r.functionName}(${o})${r.alias?` ${r.alias}`:""}`;break}case"FieldRelationship":{t=`${a}${r.relationships.join(".")}.${r.field}${q(r)?` ${r.alias}`:""}`;break}case"FieldSubquery":{t=this.formatter.formatSubquery(this.parseQuery(r.subquery));break}case"FieldTypeof":{n=this.parseTypeOfField(r),t=n.join(" ");break}default:break}return{text:t,typeOfClause:n}})}parseTypeOfField(e){let r=[`TYPEOF ${e.field}`].concat(e.conditions.map(t=>this.formatter.formatTypeofFieldCondition(t)));return r.push("END"),r}parseWhereOrHavingClause(e,r=0,t=!1){let n="",a=e.left,o=!1;if(a&&(n+=this.formatter.formatParens(a.openParen,"(",k(a)),!k(a))){r=r+(a.openParen||0)-(a.closeParen||0),t&&r++;let l="";l+=J(a)?this.parseFn(a.fn):a.field,l+=` ${a.operator} `,z(a)?l+=this.formatter.formatSubquery(this.parseQuery(a.valueQuery),1,!0):l+=le(Ee(a.value,a.literalType,a.operator)),n+=this.formatter.formatWithIndent(l),n+=this.formatter.formatParens(a.closeParen,")",t)}if(K(e)){let l=m(e.operator);o=l==="NOT";let u=this.formatter.formatWhereClauseOperators(l,this.parseWhereOrHavingClause(e.right,r,k(a)),r);return`${o?n.trimRight():n}${u}`.trim()}else return n.trim()}parseGroupByClause(e){return(Array.isArray(e)?e:[e]).map(r=>ee(r)?r.field:this.parseFn(r.fn)).join(", ")}parseOrderBy(e){if(Array.isArray(e))return this.formatter.formatOrderByArray(e.map(r=>this.parseOrderBy(r)));{let r="";return Z(e)?r=`${m(e.field," ")}`:r+=`${this.parseFn(e.fn)} `,r+=`${m(e.order," ")}${m(e.nulls,"","NULLS ")}`,r.trim()}}parseWithDataCategory(e){return e.conditions.map(r=>{let t=r.parameters.length>1?`(${r.parameters.join(", ")})`:`${r.parameters.join(", ")}`;return`${r.groupName} ${r.selector} ${t}`}).join(" AND ")}};function Te(s){let e=s.slice(2),r=e[0]??"",t=e[1]??"",n={};for(let a=2;a<e.length;a++){let o=e[a];if(o.startsWith("--")){let l=o.slice(2),u=e[a+1];u&&!u.startsWith("-")?(n[l]=u,a++):n[l]=!0}else if(o.startsWith("-")&&o.length===2){let l=o.slice(1),u=e[a+1];u&&!u.startsWith("-")?(n[l]=u,a++):n[l]=!0}}return{command:r,query:t,flags:n}}function N(s,e,r){return s[e]===!0||s[r]===!0}function w(s,e,r){let t=s[e]??s[r];return typeof t=="string"?t:void 0}function ce(){console.log(`Usage: soql-parser-js <command> <query> [options]
24
24
 
25
25
  Commands:
26
26
  parse <soql> Parse a SOQL query and output JSON AST
@@ -53,4 +53,4 @@ Format options:
53
53
  Valid options:
54
54
  -a, --allow-apex Allow apex bind variables
55
55
  -p, --allow-partial Allow partial queries
56
- -j, --json Output as JSON`)}var{command:$,query:G,flags:A}=Ce(process.argv);(!$||!G)&&(he(),process.exit($?1:0));switch($){case"parse":{console.log(JSON.stringify(x(G,{allowApexBindVariables:N(A,"allow-apex","a"),allowPartialQuery:N(A,"allow-partial","p"),ignoreParseErrors:N(A,"ignore-errors","i")})));break}case"compose":{let s={},e=Y(A,"indent","i"),r=Y(A,"line-length","m");e&&(s.numIndent=Number(e)),r&&(s.fieldMaxLineLength=Number(r)),N(A,"subquery-parens-new-line","s")&&(s.fieldSubqueryParensOnOwnLine=!0),N(A,"keywords-new-line","k")&&(s.newLineAfterKeywords=!0);let t=N(A,"format","f"),n=re(JSON.parse(G),{format:t,formatOptions:s});N(A,"json","j")?console.log(JSON.stringify({query:n})):console.log(n);break}case"format":{let s={},e={};N(A,"allow-apex","a")&&(s.allowApexBindVariables=!0),N(A,"allow-partial","p")&&(s.allowPartialQuery=!0);let r=Y(A,"indent","i"),t=Y(A,"line-length","m");r&&(e.numIndent=Number(r)),t&&(e.fieldMaxLineLength=Number(t)),N(A,"subquery-parens-new-line","s")&&(e.fieldSubqueryParensOnOwnLine=!0),N(A,"keywords-new-line","k")&&(e.newLineAfterKeywords=!0);let n=ce(G,e,s);N(A,"json","j")?console.log(JSON.stringify({query:n})):console.log(n);break}case"valid":{let s={};N(A,"allow-apex","a")&&(s.allowApexBindVariables=!0),N(A,"allow-partial","p")&&(s.allowPartialQuery=!0);let e=q(G,s);N(A,"json","j")?console.log(JSON.stringify({isValid:e})):e?console.log("true"):(console.error("false"),process.exit(1));break}default:console.error(`Unknown command: ${$}`),he(),process.exit(1)}
56
+ -j, --json Output as JSON`)}var{command:Y,query:U,flags:A}=Te(process.argv);(!Y||!U)&&(ce(),process.exit(Y?1:0));switch(Y){case"parse":{console.log(JSON.stringify(x(U,{allowApexBindVariables:N(A,"allow-apex","a"),allowPartialQuery:N(A,"allow-partial","p"),ignoreParseErrors:N(A,"ignore-errors","i")})));break}case"compose":{let s={},e=w(A,"indent","i"),r=w(A,"line-length","m");e&&(s.numIndent=Number(e)),r&&(s.fieldMaxLineLength=Number(r)),N(A,"subquery-parens-new-line","s")&&(s.fieldSubqueryParensOnOwnLine=!0),N(A,"keywords-new-line","k")&&(s.newLineAfterKeywords=!0);let t=N(A,"format","f"),n=ie(JSON.parse(U),{format:t,formatOptions:s});N(A,"json","j")?console.log(JSON.stringify({query:n})):console.log(n);break}case"format":{let s={},e={};N(A,"allow-apex","a")&&(s.allowApexBindVariables=!0),N(A,"allow-partial","p")&&(s.allowPartialQuery=!0);let r=w(A,"indent","i"),t=w(A,"line-length","m");r&&(e.numIndent=Number(r)),t&&(e.fieldMaxLineLength=Number(t)),N(A,"subquery-parens-new-line","s")&&(e.fieldSubqueryParensOnOwnLine=!0),N(A,"keywords-new-line","k")&&(e.newLineAfterKeywords=!0);let n=pe(U,e,s);N(A,"json","j")?console.log(JSON.stringify({query:n})):console.log(n);break}case"valid":{let s={};N(A,"allow-apex","a")&&(s.allowApexBindVariables=!0),N(A,"allow-partial","p")&&(s.allowPartialQuery=!0);let e=X(U,s);N(A,"json","j")?console.log(JSON.stringify({isValid:e})):e?console.log("true"):(console.error("false"),process.exit(1));break}default:console.error(`Unknown command: ${Y}`),ce(),process.exit(1)}
@@ -1,4 +1,4 @@
1
- var T=(r=>(r[r.AND=0]="AND",r[r.AS=1]="AS",r[r.ASC=2]="ASC",r[r.DESC=3]="DESC",r[r.CUBE=4]="CUBE",r[r.ELSE=5]="ELSE",r[r.EXCLUDES=6]="EXCLUDES",r[r.FALSE=7]="FALSE",r[r.FIRST=8]="FIRST",r[r.FROM=9]="FROM",r[r.GROUP_BY=10]="GROUP_BY",r[r.HAVING=11]="HAVING",r[r.IN=12]="IN",r[r.INCLUDES=13]="INCLUDES",r[r.LAST=14]="LAST",r[r.LIKE=15]="LIKE",r[r.LIMIT=16]="LIMIT",r[r.NOT=17]="NOT",r[r.NULL=18]="NULL",r[r.NULLS=19]="NULLS",r[r.OR=20]="OR",r[r.ORDER_BY=21]="ORDER_BY",r[r.ROLLUP=22]="ROLLUP",r[r.SELECT=23]="SELECT",r[r.TRUE=24]="TRUE",r[r.USING=25]="USING",r[r.WHERE=26]="WHERE",r[r.WITH=27]="WITH",r[r.FOR=28]="FOR",r[r.UPDATE=29]="UPDATE",r[r.ABOVE=30]="ABOVE",r[r.ABOVE_OR_BELOW=31]="ABOVE_OR_BELOW",r[r.APEX_NEW=32]="APEX_NEW",r[r.AT=33]="AT",r[r.BELOW=34]="BELOW",r[r.DATA_CATEGORY=35]="DATA_CATEGORY",r[r.END=36]="END",r[r.OFFSET=37]="OFFSET",r[r.REFERENCE=38]="REFERENCE",r[r.SCOPE=39]="SCOPE",r[r.TRACKING=40]="TRACKING",r[r.THEN=41]="THEN",r[r.TYPEOF=42]="TYPEOF",r[r.VIEW=43]="VIEW",r[r.VIEWSTAT=44]="VIEWSTAT",r[r.WHEN=45]="WHEN",r[r.SECURITY_ENFORCED=46]="SECURITY_ENFORCED",r[r.SYSTEM_MODE=47]="SYSTEM_MODE",r[r.USER_MODE=48]="USER_MODE",r[r.CALENDAR_MONTH=49]="CALENDAR_MONTH",r[r.CALENDAR_QUARTER=50]="CALENDAR_QUARTER",r[r.CALENDAR_YEAR=51]="CALENDAR_YEAR",r[r.DAY_IN_MONTH=52]="DAY_IN_MONTH",r[r.DAY_IN_WEEK=53]="DAY_IN_WEEK",r[r.DAY_IN_YEAR=54]="DAY_IN_YEAR",r[r.DAY_ONLY=55]="DAY_ONLY",r[r.FISCAL_MONTH=56]="FISCAL_MONTH",r[r.FISCAL_QUARTER=57]="FISCAL_QUARTER",r[r.FISCAL_YEAR=58]="FISCAL_YEAR",r[r.HOUR_IN_DAY=59]="HOUR_IN_DAY",r[r.WEEK_IN_MONTH=60]="WEEK_IN_MONTH",r[r.WEEK_IN_YEAR=61]="WEEK_IN_YEAR",r[r.AVG=62]="AVG",r[r.COUNT=63]="COUNT",r[r.COUNT_DISTINCT=64]="COUNT_DISTINCT",r[r.MIN=65]="MIN",r[r.MAX=66]="MAX",r[r.SUM=67]="SUM",r[r.DISTANCE=68]="DISTANCE",r[r.GEOLOCATION=69]="GEOLOCATION",r[r.FIELDS=70]="FIELDS",r[r.FORMAT=71]="FORMAT",r[r.TOLABEL=72]="TOLABEL",r[r.CONVERT_TIMEZONE=73]="CONVERT_TIMEZONE",r[r.CONVERT_CURRENCY=74]="CONVERT_CURRENCY",r[r.GROUPING=75]="GROUPING",r[r.ALL=76]="ALL",r[r.CUSTOM=77]="CUSTOM",r[r.STANDARD=78]="STANDARD",r[r.YESTERDAY=79]="YESTERDAY",r[r.TODAY=80]="TODAY",r[r.TOMORROW=81]="TOMORROW",r[r.LAST_WEEK=82]="LAST_WEEK",r[r.THIS_WEEK=83]="THIS_WEEK",r[r.NEXT_WEEK=84]="NEXT_WEEK",r[r.LAST_MONTH=85]="LAST_MONTH",r[r.THIS_MONTH=86]="THIS_MONTH",r[r.NEXT_MONTH=87]="NEXT_MONTH",r[r.LAST_90_DAYS=88]="LAST_90_DAYS",r[r.NEXT_90_DAYS=89]="NEXT_90_DAYS",r[r.THIS_QUARTER=90]="THIS_QUARTER",r[r.LAST_QUARTER=91]="LAST_QUARTER",r[r.NEXT_QUARTER=92]="NEXT_QUARTER",r[r.THIS_YEAR=93]="THIS_YEAR",r[r.LAST_YEAR=94]="LAST_YEAR",r[r.NEXT_YEAR=95]="NEXT_YEAR",r[r.THIS_FISCAL_QUARTER=96]="THIS_FISCAL_QUARTER",r[r.LAST_FISCAL_QUARTER=97]="LAST_FISCAL_QUARTER",r[r.NEXT_FISCAL_QUARTER=98]="NEXT_FISCAL_QUARTER",r[r.THIS_FISCAL_YEAR=99]="THIS_FISCAL_YEAR",r[r.LAST_FISCAL_YEAR=100]="LAST_FISCAL_YEAR",r[r.NEXT_FISCAL_YEAR=101]="NEXT_FISCAL_YEAR",r[r.NEXT_N_DAYS=102]="NEXT_N_DAYS",r[r.LAST_N_DAYS=103]="LAST_N_DAYS",r[r.N_DAYS_AGO=104]="N_DAYS_AGO",r[r.NEXT_N_WEEKS=105]="NEXT_N_WEEKS",r[r.LAST_N_WEEKS=106]="LAST_N_WEEKS",r[r.N_WEEKS_AGO=107]="N_WEEKS_AGO",r[r.NEXT_N_MONTHS=108]="NEXT_N_MONTHS",r[r.LAST_N_MONTHS=109]="LAST_N_MONTHS",r[r.N_MONTHS_AGO=110]="N_MONTHS_AGO",r[r.NEXT_N_QUARTERS=111]="NEXT_N_QUARTERS",r[r.LAST_N_QUARTERS=112]="LAST_N_QUARTERS",r[r.N_QUARTERS_AGO=113]="N_QUARTERS_AGO",r[r.NEXT_N_YEARS=114]="NEXT_N_YEARS",r[r.LAST_N_YEARS=115]="LAST_N_YEARS",r[r.N_YEARS_AGO=116]="N_YEARS_AGO",r[r.NEXT_N_FISCAL_QUARTERS=117]="NEXT_N_FISCAL_QUARTERS",r[r.LAST_N_FISCAL_QUARTERS=118]="LAST_N_FISCAL_QUARTERS",r[r.N_FISCAL_QUARTERS_AGO=119]="N_FISCAL_QUARTERS_AGO",r[r.NEXT_N_FISCAL_YEARS=120]="NEXT_N_FISCAL_YEARS",r[r.LAST_N_FISCAL_YEARS=121]="LAST_N_FISCAL_YEARS",r[r.N_FISCAL_YEARS_AGO=122]="N_FISCAL_YEARS_AGO",r[r.DELEGATED=123]="DELEGATED",r[r.EVERYTHING=124]="EVERYTHING",r[r.MINE_AND_MY_GROUPS=125]="MINE_AND_MY_GROUPS",r[r.MINE=126]="MINE",r[r.MY_TERRITORY=127]="MY_TERRITORY",r[r.MY_TEAM_TERRITORY=128]="MY_TEAM_TERRITORY",r[r.TEAM=129]="TEAM",r[r.ALL_PRIVATE=130]="ALL_PRIVATE",r[r.MRU=131]="MRU",r[r.EQUAL=132]="EQUAL",r[r.NOT_EQUAL=133]="NOT_EQUAL",r[r.LESS_THAN=134]="LESS_THAN",r[r.LESS_THAN_OR_EQUAL=135]="LESS_THAN_OR_EQUAL",r[r.GREATER_THAN=136]="GREATER_THAN",r[r.GREATER_THAN_OR_EQUAL=137]="GREATER_THAN_OR_EQUAL",r[r.L_PAREN=138]="L_PAREN",r[r.R_PAREN=139]="R_PAREN",r[r.L_SQUARE_BRACKET=140]="L_SQUARE_BRACKET",r[r.R_SQUARE_BRACKET=141]="R_SQUARE_BRACKET",r[r.DOT=142]="DOT",r[r.COMMA=143]="COMMA",r[r.COLON=144]="COLON",r[r.SEMICOLON=145]="SEMICOLON",r[r.ASTERISK=146]="ASTERISK",r[r.PLUS=147]="PLUS",r[r.MINUS=148]="MINUS",r[r.DATETIME=149]="DATETIME",r[r.DATE_TOKEN=150]="DATE_TOKEN",r[r.CURRENCY_PREFIXED_DECIMAL=151]="CURRENCY_PREFIXED_DECIMAL",r[r.CURRENCY_PREFIXED_INTEGER=152]="CURRENCY_PREFIXED_INTEGER",r[r.SIGNED_DECIMAL=153]="SIGNED_DECIMAL",r[r.UNSIGNED_DECIMAL=154]="UNSIGNED_DECIMAL",r[r.SIGNED_INTEGER=155]="SIGNED_INTEGER",r[r.UNSIGNED_INTEGER=156]="UNSIGNED_INTEGER",r[r.STRING_LITERAL=157]="STRING_LITERAL",r[r.GEOLOCATION_UNIT=158]="GEOLOCATION_UNIT",r[r.IDENTIFIER=159]="IDENTIFIER",r[r.EOF=160]="EOF",r))(T||{});function F(i){return i>=49&&i<=61}function g(i){return i>=62&&i<=67}function $(i){return i>=68&&i<=69}function W(i){return i===70}function y(i){return i>=71&&i<=75}function J(i){return i>=76&&i<=78}function U(i){return i>=79&&i<=101}function k(i){return i>=102&&i<=122}function ee(i){return i>=123&&i<=131}function I(i){return i===159||i>=30&&i<=48||i>=49&&i<=61||i>=62&&i<=67||i>=68&&i<=69||i===70||i>=71&&i<=75||i>=76&&i<=78||i>=79&&i<=101||i>=102&&i<=122||i>=123&&i<=131||i===158||i===152}function B(i){return i===153||i===154||i===155||i===156}function te(i){return i>=132&&i<=137}var ce=new Map([["AND",0],["AS",1],["ASC",2],["DESC",3],["CUBE",4],["ELSE",5],["EXCLUDES",6],["FALSE",7],["FIRST",8],["FROM",9],["HAVING",11],["IN",12],["INCLUDES",13],["LAST",14],["LIKE",15],["LIMIT",16],["NOT",17],["NULL",18],["NULLS",19],["OR",20],["ROLLUP",22],["SELECT",23],["TRUE",24],["USING",25],["WHERE",26],["WITH",27],["FOR",28],["UPDATE",29],["ABOVE_OR_BELOW",31],["ABOVE",30],["NEW",32],["AT",33],["BELOW",34],["END",36],["OFFSET",37],["REFERENCE",38],["SCOPE",39],["TRACKING",40],["THEN",41],["TYPEOF",42],["VIEW",43],["VIEWSTAT",44],["WHEN",45],["SECURITY_ENFORCED",46],["SYSTEM_MODE",47],["USER_MODE",48],["CALENDAR_MONTH",49],["CALENDAR_QUARTER",50],["CALENDAR_YEAR",51],["DAY_IN_MONTH",52],["DAY_IN_WEEK",53],["DAY_IN_YEAR",54],["DAY_ONLY",55],["FISCAL_MONTH",56],["FISCAL_QUARTER",57],["FISCAL_YEAR",58],["HOUR_IN_DAY",59],["WEEK_IN_MONTH",60],["WEEK_IN_YEAR",61],["AVG",62],["COUNT_DISTINCT",64],["COUNT",63],["MIN",65],["MAX",66],["SUM",67],["DISTANCE",68],["GEOLOCATION",69],["FIELDS",70],["FORMAT",71],["TOLABEL",72],["CONVERTTIMEZONE",73],["CONVERTCURRENCY",74],["GROUPING",75],["ALL",76],["CUSTOM",77],["STANDARD",78],["YESTERDAY",79],["TODAY",80],["TOMORROW",81],["LAST_WEEK",82],["THIS_WEEK",83],["NEXT_WEEK",84],["LAST_MONTH",85],["THIS_MONTH",86],["NEXT_MONTH",87],["LAST_90_DAYS",88],["NEXT_90_DAYS",89],["THIS_QUARTER",90],["LAST_QUARTER",91],["NEXT_QUARTER",92],["THIS_YEAR",93],["LAST_YEAR",94],["NEXT_YEAR",95],["THIS_FISCAL_QUARTER",96],["LAST_FISCAL_QUARTER",97],["NEXT_FISCAL_QUARTER",98],["THIS_FISCAL_YEAR",99],["LAST_FISCAL_YEAR",100],["NEXT_FISCAL_YEAR",101],["NEXT_N_DAYS",102],["LAST_N_DAYS",103],["N_DAYS_AGO",104],["NEXT_N_WEEKS",105],["LAST_N_WEEKS",106],["N_WEEKS_AGO",107],["NEXT_N_MONTHS",108],["LAST_N_MONTHS",109],["N_MONTHS_AGO",110],["NEXT_N_QUARTERS",111],["LAST_N_QUARTERS",112],["N_QUARTERS_AGO",113],["NEXT_N_YEARS",114],["LAST_N_YEARS",115],["N_YEARS_AGO",116],["NEXT_N_FISCAL_QUARTERS",117],["LAST_N_FISCAL_QUARTERS",118],["N_FISCAL_QUARTERS_AGO",119],["NEXT_N_FISCAL_YEARS",120],["LAST_N_FISCAL_YEARS",121],["N_FISCAL_YEARS_AGO",122],["DELEGATED",123],["EVERYTHING",124],["MINEANDMYGROUPS",125],["MINE",126],["MY_TERRITORY",127],["MY_TEAM_TERRITORY",128],["TEAM",129],["ALLPRIVATE",130],["MRU",131]]),p=[];(function(){for(let e of Object.keys(T)){let s=T[e];typeof s=="number"&&(p[s]=e)}p[72]="toLabel",p[73]="convertTimezone",p[74]="convertCurrency",p[157]="StringIdentifier",p[150]="DATE",p[32]="new",p[125]="MineAndMyGroups",p[127]="MyTerritory",p[128]="MyTeamTerritory",p[130]="AllPrivate",p[123]="Delegated",p[124]="Everything",p[126]="Mine",p[129]="Team",p[131]="Mru",p[159]="Identifier",p[142]="DECIMAL",p[132]="EQUAL",p[156]="UNSIGNED_INTEGER",p[155]="SIGNED_INTEGER",p[154]="UNSIGNED_DECIMAL",p[153]="SIGNED_DECIMAL",p[151]="CURRENCY_PREFIXED_DECIMAL",p[152]="CURRENCY_PREFIXED_INTEGER"})();function d(i){return p[i]||T[i]}function H(i){return i>=65&&i<=90||i>=97&&i<=122}function c(i){return i>=48&&i<=57}function w(i){return H(i)||c(i)||i===95}function b(i){return i===32||i===9||i===10||i===13||i===12}function ie(i){let e=[],s=i.length,t=0;for(;t<s;){let n=i.charCodeAt(t);if(b(n)){for(t++;t<s&&b(i.charCodeAt(t));)t++;continue}if(n===39){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(E===92){t+=2;continue}if(E===39){t++;break}t++}let o=i.substring(a,t),l=o.substring(1,o.length-1).toUpperCase();l==="MI"||l==="KM"?e.push({kind:158,text:o,start:a}):e.push({kind:157,text:o,start:a});continue}if(c(n)){if(t+9<s&&i.charCodeAt(t+4)===45&&i.charCodeAt(t+7)===45){let o=!0;for(let h of[0,1,2,3,5,6,8,9])if(!c(i.charCodeAt(t+h))){o=!1;break}if(o)if(t+10<s&&(i.charCodeAt(t+10)===84||i.charCodeAt(t+10)===116)){let h=t;for(t+=11;t<s;){let l=i.charCodeAt(t);if(c(l)||l===58)t++;else break}if(t<s&&i.charCodeAt(t)===46)for(t++;t<s&&c(i.charCodeAt(t));)t++;if(t<s){let l=i.charCodeAt(t);if(l===90||l===122)t++;else if(l===46){for(t++;t<s&&c(i.charCodeAt(t));)t++;t<s&&(i.charCodeAt(t)===90||i.charCodeAt(t)===122)&&t++}else if(l===43||l===45)for(t++;t<s;){let E=i.charCodeAt(t);if(c(E)||E===58)t++;else break}}e.push({kind:149,text:i.substring(h,t),start:h});continue}else{let h=t;t+=10,e.push({kind:150,text:i.substring(h,t),start:h});continue}}let a=t;for(;t<s&&c(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&c(i.charCodeAt(t+1))){for(t++;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:156,text:i.substring(a,t),start:a});continue}if(n===33){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:133,text:"!=",start:t}),t+=2;continue}throw new Error(`Unexpected character '!' at position ${t}`)}if(n===60){if(t+1<s){let a=i.charCodeAt(t+1);if(a===61){e.push({kind:135,text:"<=",start:t}),t+=2;continue}if(a===62){e.push({kind:133,text:"<>",start:t}),t+=2;continue}}e.push({kind:134,text:"<",start:t}),t++;continue}if(n===62){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:137,text:">=",start:t}),t+=2;continue}e.push({kind:136,text:">",start:t}),t++;continue}if(n===61){e.push({kind:132,text:"=",start:t}),t++;continue}if(n===40){e.push({kind:138,text:"(",start:t}),t++;continue}if(n===41){e.push({kind:139,text:")",start:t}),t++;continue}if(n===91){e.push({kind:140,text:"[",start:t}),t++;continue}if(n===93){e.push({kind:141,text:"]",start:t}),t++;continue}if(n===44){e.push({kind:143,text:",",start:t}),t++;continue}if(n===58){e.push({kind:144,text:":",start:t}),t++;continue}if(n===59){e.push({kind:145,text:";",start:t}),t++;continue}if(n===42){e.push({kind:146,text:"*",start:t}),t++;continue}if(n===43||n===45){if(t+1<s){let a=i.charCodeAt(t+1);if(c(a)){let o=t;for(t++;t<s&&c(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&c(i.charCodeAt(t+1))){for(t++;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}e.push({kind:155,text:i.substring(o,t),start:o});continue}if(a===46&&t+2<s&&c(i.charCodeAt(t+2))){let o=t;for(t+=2;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}}e.push({kind:n===43?147:148,text:i[t],start:t}),t++;continue}if(n===46){if(t+1<s&&c(i.charCodeAt(t+1))){let a=t;for(t++;t<s&&c(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:142,text:".",start:t}),t++;continue}if(H(n)){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(w(E)||E===46)t++;else break}let o=i.substring(a,t);if(o.includes(".."))throw new Error(`Invalid identifier with consecutive dots at position ${a}: "${o}"`);let h=o.toUpperCase();if(h==="ORDER"&&!o.includes(".")){let E=Y(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:21,text:u,start:a}),t=E;continue}}if(h==="GROUP"&&!o.includes(".")){let E=Y(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:10,text:u,start:a}),t=E;continue}}if(h==="DATA"&&!o.includes(".")){let E=Y(i,t,"CATEGORY");if(E!==-1){let u=i.substring(a,E);e.push({kind:35,text:u,start:a}),t=E;continue}}if(o.length>=4){let E=o.substring(0,3);if(Z(E)){let u=o.substring(3);if(fe(u)){u.includes(".")?e.push({kind:151,text:o,start:a}):e.push({kind:152,text:o,start:a});continue}}}if(o.length===3&&Z(o)){let E=t,u=t,R=!1;if(u<s&&i.charCodeAt(u)===45&&(R=!0,u++),R&&u<s&&c(i.charCodeAt(u))){for(;u<s&&c(i.charCodeAt(u));)u++;if(u<s&&i.charCodeAt(u)===46&&u+1<s&&c(i.charCodeAt(u+1))){for(u++;u<s&&c(i.charCodeAt(u));)u++;let f=i.substring(a,u);e.push({kind:151,text:f,start:a}),t=u;continue}if(u>=s||!w(i.charCodeAt(u))&&i.charCodeAt(u)!==46){let f=i.substring(a,u);e.push({kind:152,text:f,start:a}),t=u;continue}t=E}}if(o.includes(".")){e.push({kind:159,text:o,start:a});continue}let l=ce.get(h);if(l!==void 0){e.push({kind:l,text:o,start:a});continue}e.push({kind:159,text:o,start:a});continue}throw new Error(`Unexpected character '${i[t]}' (code ${n}) at position ${t}`)}return e.push({kind:160,text:"",start:t}),e}function Y(i,e,s){let t=i.length,n=e;if(n>=t||!b(i.charCodeAt(n)))return-1;for(;n<t&&b(i.charCodeAt(n));)n++;let a=s.length;if(n+a>t)return-1;for(let h=0;h<a;h++){let l=i.charCodeAt(n+h),E=s.charCodeAt(h);if((l>=97&&l<=122?l-32:l)!==E)return-1}let o=n+a;if(o<t){let h=i.charCodeAt(o);if(w(h)||h===46)return-1}return o}function Z(i){for(let e=0;e<i.length;e++)if(!H(i.charCodeAt(e)))return!1;return!0}function fe(i){if(i.length===0)return!1;let e=0;if(i.charCodeAt(e)===45&&e++,e>=i.length||!c(i.charCodeAt(e)))return!1;for(;e<i.length&&c(i.charCodeAt(e));)e++;if(e<i.length&&i.charCodeAt(e)===46){if(e++,e>=i.length||!c(i.charCodeAt(e)))return!1;for(;e<i.length&&c(i.charCodeAt(e));)e++}return e===i.length}var Ae=new Set([23,9,26,21,10,11,16,27,28,29,25,19,2,3,0,20,17,45,41,5,36,42,143,139,160]),V=class i{constructor(){this.tokens=[];this.pos=0;this.config={}}init(e,s){this.tokens=e,this.pos=0,this.config=s}getPos(){return this.pos}peek(){return this.tokens[this.pos]}peekAt(e){let s=this.pos+e;return s>=this.tokens.length?this.tokens[this.tokens.length-1]:this.tokens[s]}advance(){let e=this.tokens[this.pos];return e.kind!==160&&this.pos++,e}expect(e){let s=this.peek();if(s.kind!==e)throw this.error(`Expected ${T[e]} but found ${T[s.kind]} ("${s.text}") at position ${s.start}`);return this.advance()}match(e){return this.peek().kind===e?(this.advance(),!0):!1}check(e){return this.peek().kind===e}isAtEnd(){return this.peek().kind===160}error(e){return this.config.logErrors&&console.log(e),new Error(e)}static{this.CLAUSE_BOUNDARY_KINDS=new Set([26,21,10,11,16,37,27,28,29,25,160])}isAtClauseBoundary(){let e=this.peek().kind;return i.CLAUSE_BOUNDARY_KINDS.has(e)||e===139}synchronize(){for(;!this.isAtEnd();){if(this.isAtClauseBoundary())return;this.advance()}}consumeIdentifier(){let e=this.peek();if(I(e.kind))return this.advance();throw this.error(`Expected identifier but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}checkIdentifier(){return I(this.peek().kind)}isFunctionCall(){let e=this.peek(),s=this.peekAt(1);return(F(e.kind)||g(e.kind)||$(e.kind)||W(e.kind)||y(e.kind))&&s.kind===138}canBeAlias(){let e=this.peek();return!(!I(e.kind)||Ae.has(e.kind)||e.kind===37&&this.peekAt(1).kind===156)}getLiteralType(e){let s=d(e);return s==="UNSIGNED_INTEGER"||s==="SIGNED_INTEGER"?"INTEGER":s==="UNSIGNED_DECIMAL"||s==="SIGNED_DECIMAL"?"DECIMAL":s==="CURRENCY_PREFIXED_DECIMAL"?"DECIMAL_WITH_CURRENCY_PREFIX":s==="CURRENCY_PREFIXED_INTEGER"?"INTEGER_WITH_CURRENCY_PREFIX":s==="DATETIME"?"DATETIME":s==="DATE"?"DATE":s==="NULL"?"NULL":s==="StringIdentifier"||s==="Identifier"?"STRING":s==="TRUE"||s==="FALSE"?"BOOLEAN":U(e)?"DATE_LITERAL":k(e)?"DATE_N_LITERAL":"STRING"}parseSelectStatement(e=!1){let s=this.parseSelectClause(),t=this.parseFromClause(),n;e?(n={fields:s,relationshipName:t.sObject},t.sObjectPrefix&&(n.sObjectPrefix=t.sObjectPrefix)):n={fields:s,sObject:t.sObject},t.alias&&(n.sObjectAlias=t.alias),this.applySObjectAlias(n);let a=this.parseClauseStatements();return Object.assign(n,a),n}parseSelectStatementPartial(){let e,s;this.check(23)&&(e=this.parseSelectClause()),this.check(9)&&(s=this.parseFromClause());let t={};e&&(t.fields=e),s&&(t.sObject=s.sObject,s.alias&&(t.sObjectAlias=s.alias)),t.fields&&this.applySObjectAlias(t);let n=this.parseClauseStatements();return Object.assign(t,n),t}applySObjectAlias(e){if(!e.sObjectAlias||!Array.isArray(e.fields))return;let s=e.sObjectAlias;e.fields.forEach(t=>{t.relationships&&t.relationships[0]===s&&(t.relationships=t.relationships.slice(1),t.objectPrefix=s,t.relationships.length===0&&(delete t.relationships,t.type="Field"))})}parseSelectClause(){this.expect(23);let e=[];for(e.push(this.parseSelectField());this.match(143);)e.push(this.parseSelectField());return e}parseSelectField(){let e=this.peek(),s=this.peekAt(1);return e.kind===138&&s.kind===23?this.parseSelectClauseSubqueryIdentifier():e.kind===42?this.parseSelectClauseTypeOf():this.isFunctionCall()?this.parseSelectClauseFunctionIdentifier():this.parseSelectClauseIdentifier()}parseSelectClauseFunctionIdentifier(){let e=this.parseAnyFunction(!0);return this.canBeAlias()&&(e.alias=this.consumeIdentifier().text),e}parseSelectClauseSubqueryIdentifier(){this.expect(138);let e=this.parseSelectStatement(!0);return this.expect(139),{type:"FieldSubquery",subquery:e}}parseSelectClauseTypeOf(){this.expect(42);let e=this.consumeIdentifier().text,s=[];for(;this.check(45);){this.advance();let t=this.consumeIdentifier().text;this.expect(41);let n=[];for(n.push(this.consumeIdentifier().text);this.match(143);)n.push(this.consumeIdentifier().text);s.push({type:"WHEN",objectType:t,fieldList:n})}if(this.check(5)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);s.push({type:"ELSE",fieldList:t})}return this.expect(36),{type:"FieldTypeof",field:e,conditions:s}}parseSelectClauseIdentifier(){let s=this.consumeIdentifier().text,t;if(!s.includes("."))t={type:"Field",field:s};else{let n=s.split(".");t={type:"FieldRelationship",field:n[n.length-1],relationships:n.slice(0,n.length-1),rawValue:s}}return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseFromClause(){this.expect(9);let s=this.consumeIdentifier().text,t;if(s.includes(".")){let n=s.split(".");t={sObjectPrefix:n.slice(0,n.length-1),sObject:n[n.length-1]}}else t={sObject:s};return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseClauseStatements(){let e={},s=(t,n)=>{if(this.config.ignoreParseErrors){let a=this.pos;try{t(),this.isAtClauseBoundary()||(n&&n(),this.synchronize())}catch(o){this.config.logErrors&&console.log(o),n&&n(),this.pos=a,this.synchronize()}}else t()};for(this.check(25)&&s(()=>{e.usingScope=this.parseUsingScope()}),this.check(26)&&s(()=>{e.where=this.parseWhereClause()},()=>{delete e.where});this.check(27);)s(()=>{let t=this.parseWithClause();t.withSecurityEnforced&&(e.withSecurityEnforced=!0),t.withAccessLevel&&(e.withAccessLevel=t.withAccessLevel),t.withDataCategory&&(e.withDataCategory=t.withDataCategory)});return this.check(10)&&(s(()=>{e.groupBy=this.parseGroupByClause()}),this.check(11)&&s(()=>{e.having=this.parseHavingClause()})),this.check(21)&&s(()=>{e.orderBy=this.parseOrderByClause()}),this.check(16)&&s(()=>{this.advance(),e.limit=Number(this.expect(156).text)}),this.check(37)&&s(()=>{this.advance(),e.offset=Number(this.expect(156).text)}),this.check(28)&&s(()=>{this.advance();let t=this.peek();if(t.kind===43||t.kind===38||t.kind===29)e.for=d(this.advance().kind);else throw this.error(`Expected VIEW, REFERENCE, or UPDATE after FOR at position ${t.start}`)}),this.check(29)&&s(()=>{this.advance();let t=this.peek();if(t.kind===40||t.kind===44)e.update=d(this.advance().kind);else throw this.error(`Expected TRACKING or VIEWSTAT after UPDATE at position ${t.start}`)}),e}parseUsingScope(){this.expect(25),this.expect(39);let e=this.peek();if(ee(e.kind))return this.advance().text;if(I(e.kind))return this.advance().text;throw this.error(`Expected scope enumeration value at position ${e.start}`)}parseWhereClause(){return this.expect(26),this.parseConditionExpressions(!0,!0)}parseConditionExpressions(e,s){let t,n,a={left:0,right:0};for(;;){let o;if(t)if(this.check(0))o="AND",this.advance();else if(this.check(20))o="OR",this.advance();else break;let h={},l=h,E=null;for(;this.hasNegationPrefix();){let f=0;for(;this.check(138)&&this.isNegationAhead();)this.advance(),f++,a.left++;if(this.check(17)){this.advance();let _={left:f>0?{openParen:f}:null,operator:"NOT",right:{left:{}}};E?E.right=_:h=_,E=_,l=_.right}else break}l.left=this.parseExpression(e,s,a),t?o&&(n.operator=o,n.right=h):(t=h,n=h);let u=h;for(;u.right&&u.right.left!==void 0;){let f=u.right;if(f.right&&f.right.left!==void 0)u=f;else{u=f;break}}if(n=u,this.isAtEnd())break;let R=this.peek().kind;if(R!==0&&R!==20)break}if(a.left!==a.right)throw this.error(`Mismatched parentheses in WHERE/HAVING clause: ${a.left} open vs ${a.right} close`);return t}hasNegationPrefix(){let e=0;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}isNegationAhead(){let e=1;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}parseExpression(e,s,t){let n=0;for(;this.check(138)&&!this.isSubqueryStart()&&!this.isArrayExpressionStart();)this.advance(),n++,t&&t.left++;let a,o=!1;e&&g(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseAggregateFunction(!1),o=!0):s&&$(this.peek().kind)&&this.peek().kind===68&&this.peekAt(1).kind===138?(a=this.parseLocationFunction(!1),o=!0):F(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseDateFunction(!1),o=!0):y(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseOtherFunction(!1),o=!0):a=this.consumeIdentifier().text;let h=this.parseOperator(),l=h.operator,E=h.isSet,u,R,f;if(E)if(this.check(138)&&this.peekAt(1).kind===23)this.expect(138),u=this.parseSelectStatement(!1),this.expect(139),R="SUBQUERY";else if(this.config.allowApexBindVariables&&this.check(144))u=this.parseApexBindVariable().value,R="APEX_BIND_VARIABLE";else{let S=this.parseArrayExpression();u=S.values,R=S.literalType,f=S.dateLiteralVariable}else{let S=this.parseAtomicValue();u=S.value,R=S.literalType,f=S.dateLiteralVariable}let _=0;for(;this.check(139)&&(!t||t.left>t.right);)this.advance(),_++,t&&t.right++;let C={};return o?C.fn=a:C.field=a,C.operator=l,R==="SUBQUERY"?C.valueQuery=u:C.value=u,C.literalType=R,f!==void 0&&(C.dateLiteralVariable=f),n>0&&(C.openParen=n),_>0&&(C.closeParen=_),C}isSubqueryStart(){return this.check(138)&&this.peekAt(1).kind===23}isArrayExpressionStart(){return!1}parseOperator(){let e=this.peek();if(te(e.kind))return{operator:this.advance().text,isSet:!1};if(e.kind===15)return this.advance(),{operator:"LIKE",isSet:!1};if(e.kind===12)return this.advance(),{operator:"IN",isSet:!0};if(e.kind===17&&this.peekAt(1).kind===12)return this.advance(),this.advance(),{operator:"NOT IN",isSet:!0};if(e.kind===13)return this.advance(),{operator:"INCLUDES",isSet:!0};if(e.kind===6)return this.advance(),{operator:"EXCLUDES",isSet:!0};throw this.error(`Expected operator but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseAtomicValue(){let e=this.peek();if(this.config.allowApexBindVariables&&e.kind===144)return{value:this.parseApexBindVariable().value,literalType:"APEX_BIND_VARIABLE"};if(e.kind===157)return{value:this.advance().text,literalType:"STRING"};if(e.kind===158)return{value:this.advance().text,literalType:"STRING"};if(e.kind===156)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===155)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===154)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===153)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===151)return{value:this.advance().text,literalType:"DECIMAL_WITH_CURRENCY_PREFIX"};if(e.kind===152)return{value:this.advance().text,literalType:"INTEGER_WITH_CURRENCY_PREFIX"};if(e.kind===149)return{value:this.advance().text,literalType:"DATETIME"};if(e.kind===150)return{value:this.advance().text,literalType:"DATE"};if(e.kind===18)return this.advance(),{value:"NULL",literalType:"NULL"};if(e.kind===24||e.kind===7)return{value:d(this.advance().kind),literalType:"BOOLEAN"};if(U(e.kind))return{value:this.advance().text,literalType:"DATE_LITERAL"};if(k(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,literalType:"DATE_N_LITERAL",dateLiteralVariable:n}}throw this.error(`Expected value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseArrayExpression(){this.expect(138);let e=[];for(e.push(this.parseArrayItem());this.match(143);)e.push(this.parseArrayItem());this.expect(139);let s=e.map(l=>l.value),t=e.map(l=>l.type),n=e.map(l=>l.variable!==void 0?l.variable:null),a=n.some(l=>l!==null),o;new Set(t).size===1?o=this.getLiteralType(this.tokenKindFromTypeName(t[0])):o=t.map(l=>this.getLiteralType(this.tokenKindFromTypeName(l)));let h={values:s,literalType:o};return a&&(h.dateLiteralVariable=n),h}tokenKindFromTypeName(e){for(let s=0;s<=160;s++)if(d(s)===e)return s;return 159}parseArrayItem(){let e=this.peek();if(e.kind===157)return{value:this.advance().text,type:d(157)};if(e.kind===158)return{value:this.advance().text,type:d(158)};if(e.kind===151)return{value:this.advance().text,type:d(151)};if(e.kind===152)return{value:this.advance().text,type:d(152)};if(B(e.kind)){let s=this.advance();return{value:s.text,type:d(s.kind)}}if(e.kind===149)return{value:this.advance().text,type:d(149)};if(e.kind===150)return{value:this.advance().text,type:d(150)};if(e.kind===18)return this.advance(),{value:"null",type:"NULL"};if(e.kind===24)return{value:this.advance().text,type:"TRUE"};if(e.kind===7)return{value:this.advance().text,type:"FALSE"};if(U(e.kind))return{value:this.advance().text,type:d(e.kind)};if(k(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,type:d(s.kind),variable:n}}throw this.error(`Expected array value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseApexBindVariable(){this.expect(144);let e=[];for(this.check(32)?e.push(this.parseApexBindNewInstantiation()):this.checkIdentifier()&&(this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):e.push(this.parseApexBindIdentifier()));this.check(142);)this.advance(),this.checkIdentifier()&&this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):this.checkIdentifier()&&e.push(this.parseApexBindIdentifier());return{value:e.join(".")}}parseApexBindIdentifier(){let e=this.consumeIdentifier().text;return this.check(140)&&(e+=this.parseApexBindArrayAccessor()),e}parseApexBindNewInstantiation(){this.expect(32);let s=`new ${this.consumeIdentifier().text}`;if(this.check(134)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);this.expect(136),s+=`<${t.join(", ")}>`}return s+=this.parseApexBindFunctionParams(),this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionCall(){let s=this.consumeIdentifier().text+this.parseApexBindFunctionParams();return this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionParams(){this.expect(138);let e=[];if(!this.check(139))for(e.push(this.consumeIdentifier().text);this.match(143);)e.push(this.consumeIdentifier().text);return this.expect(139),`(${e.join(", ")})`}parseApexBindArrayAccessor(){this.expect(140);let e=this.peek(),s;if(e.kind===156)s=this.advance().text;else if(I(e.kind))s=this.advance().text;else throw this.error(`Expected integer or identifier in array accessor at position ${e.start}`);return this.expect(141),`[${s}]`}parseAnyFunction(e){let s=this.peek();if(F(s.kind))return this.parseDateFunction(e);if(g(s.kind))return this.parseAggregateFunction(e);if(s.kind===68)return this.parseLocationFunction(e);if(W(s.kind))return this.parseFieldsFunction(e);if(y(s.kind))return this.parseOtherFunction(e);throw this.error(`Expected function name but found ${T[s.kind]} at position ${s.start}`)}parseDateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseAggregateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!0,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseFieldsFunction(e){this.advance(),this.expect(138);let s=this.peek(),t;if(J(s.kind))t=this.advance().text;else if(I(s.kind))t=this.advance().text;else throw this.error(`Expected FIELDS function parameter at position ${s.start}`);this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="FIELDS",n.parameters=[t],n.rawValue=`FIELDS(${t})`,n}parseOtherFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseLocationFunction(e){this.advance(),this.expect(138);let s=this.consumeIdentifier().text;this.expect(143);let t;this.check(69)&&this.peekAt(1).kind===138?t=this.parseGeolocationFunction(e):t=this.consumeIdentifier().text,this.expect(143);let n=this.expect(158).text;this.expect(139);let a={};e&&(a.type="FieldFunctionExpression"),a.functionName="DISTANCE",a.parameters=[s,t,n],e&&(a.isAggregateFn=!0);let o=typeof t=="string"?t:t.rawValue;return a.rawValue=`DISTANCE(${s}, ${o}, ${n})`,a}parseGeolocationFunction(e){this.advance(),this.expect(138);let s=this.parseNumberValue();this.expect(143);let t=this.parseNumberValue();this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="GEOLOCATION",n.parameters=[s,t],n.rawValue=`GEOLOCATION(${s}, ${t})`,n}parseNumberValue(){let e=this.peek();if(e.kind===156||e.kind===155||e.kind===154||e.kind===153)return this.advance().text;throw this.error(`Expected number but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseFunctionExpression(e,s=!0){this.expect(138);let t=[];if(!this.check(139))for(t.push(this.parseFunctionParam(e,s));this.match(143);)t.push(this.parseFunctionParam(e,s));return this.expect(139),t}parseFunctionParam(e,s){let t=this.peek(),n=this.peekAt(1);if(!e&&g(t.kind)&&n.kind===138)return this.parseAggregateFunction(s);if(y(t.kind)&&n.kind===138)return this.parseOtherFunction(s);if(t.kind===157)return this.advance().text;if(B(t.kind))return this.advance().text;if(I(t.kind))return this.advance().text;throw this.error(`Expected function parameter but found ${T[t.kind]} ("${t.text}") at position ${t.start}`)}parseGroupByClause(){this.expect(10);let e=[];for(e.push(this.parseGroupByItem());this.match(143);)e.push(this.parseGroupByItem());return e}parseGroupByItem(){let e=this.peek();return e.kind===4&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("CUBE")}:e.kind===22&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("ROLLUP")}:F(e.kind)&&this.peekAt(1).kind===138?{fn:this.parseDateFunction(!1)}:{field:this.consumeIdentifier().text}}parseCubeOrRollupFunction(e){let s=this.advance(),t=this.parseFunctionExpression(!1);return{functionName:e,parameters:t,rawValue:`${e}(${t.join(", ")})`}}parseHavingClause(){return this.expect(11),this.parseConditionExpressions(!0,!1)}parseOrderByClause(){this.expect(21);let e=[];for(e.push(this.parseOrderByItem());this.match(143);)e.push(this.parseOrderByItem());return e}parseOrderByItem(){let e=this.peek(),s=this.peekAt(1),t;if(e.kind===75&&s.kind===138){let n=this.advance(),a=this.parseFunctionExpression(!1);t={fn:{functionName:d(n.kind),parameters:a,rawValue:`${n.text}(${a.join(", ")})`}}}else g(e.kind)&&s.kind===138?t={fn:this.parseAggregateFunction(!1)}:F(e.kind)&&s.kind===138?t={fn:this.parseDateFunction(!1)}:e.kind===68&&s.kind===138?t={fn:this.parseLocationFunction(!1)}:t={field:this.consumeIdentifier().text};if(this.check(2)?t.order=d(this.advance().kind):this.check(3)&&(t.order=d(this.advance().kind)),this.check(19))if(this.advance(),this.check(8))t.nulls=d(this.advance().kind);else if(this.check(14))t.nulls=d(this.advance().kind);else throw this.error(`Expected FIRST or LAST after NULLS at position ${this.peek().start}`);return t}parseWithClause(){this.expect(27);let e=this.peek();if(e.kind===46)return this.advance(),{withSecurityEnforced:!0};if(e.kind===48)return this.advance(),{withAccessLevel:"USER_MODE"};if(e.kind===47)return this.advance(),{withAccessLevel:"SYSTEM_MODE"};if(e.kind===35)return{withDataCategory:this.parseWithDataCategory()};throw this.error(`Expected SECURITY_ENFORCED, USER_MODE, SYSTEM_MODE, or DATA CATEGORY after WITH at position ${e.start}`)}parseWithDataCategory(){this.expect(35);let e=[];for(e.push(this.parseDataCategoryCondition());this.check(0);)this.advance(),e.push(this.parseDataCategoryCondition());return{conditions:e}}parseDataCategoryCondition(){let e=this.consumeIdentifier().text,s=this.peek(),t;if(s.kind===33||s.kind===30||s.kind===34||s.kind===31)t=this.advance().text;else throw this.error(`Expected data category selector (AT, ABOVE, BELOW, ABOVE_OR_BELOW) at position ${s.start}`);let n=this.match(138),a=[];for(a.push(this.consumeIdentifier().text);this.match(143);)a.push(this.consumeIdentifier().text);return n&&this.expect(139),{groupName:e,selector:t,parameters:a}}},x=new V;function G(i,e){let s=e||{},t=ie(i);x.init(t,s);let n;if(s.allowPartialQuery?n=x.parseSelectStatementPartial():n=x.parseSelectStatement(!1),!x.isAtEnd()&&!s.ignoreParseErrors){let a=t[x.getPos()];throw new Error(`Unexpected token "${a.text}" at position ${a.start}. Expected end of query.`)}return n}function de(i,e){try{return G(i,e),!0}catch{return!1}}function A(i){return typeof i=="string"}function Ne(i){return i?i.every(e=>A(e)):!1}function L(i){return Number.isFinite(i)}function N(i){return i==null}function O(i,e,s){return N(i)?"":`${s||""}${i}${e||""}`}function re(i,e=!1){return Array.isArray(i)?i.length>0?`(${i.join(", ")})`:e?"()":"":e?`(${i||""})`:i||""}function se(i,e,s=""){return L(i)&&i>0?new Array(i).fill(e).join(s):""}function Q(i){return!i.parameters||i.parameters.length===0?[]:Ne(i.parameters)?i.parameters:A(i.parameters[0])?[i.parameters[0]]:Q(i.parameters[0])}function ne(i){if(!i.parameters||i.parameters.length===0)return!1;let e=i.parameters[0];return A(e)?!1:!!e.isAggregateFn}function M(i){return i&&!N(i.alias)}function ae(i){return A(i.field)&&!Array.isArray(i.relationships)&&!Array.isArray(i.conditions)}function oe(i){return!N(i.functionName||i.fn)}function le(i){return A(i.field)&&Array.isArray(i.relationships)}function ue(i){return!N(i.subquery)}function Ee(i){return A(i.field)&&Array.isArray(i.conditions)}function m(i){return A(i.relationshipName)}function he(i){return!!i&&!!i.type&&i.type==="FieldSubquery"}function Re(i){return!!i&&!!i.operator&&!!i.right}function Te(i){return!!i&&!!i.operator&&!!i.right}function X(i){return!!i&&!!i.operator&&!!i.right}function Ce(i){return i&&A(i.field)&&A(i.operator)&&!N(i.value)}function _e(i){return i&&A(i.field)&&A(i.operator)&&!N(i.value)&&(i.literalType==="DATE_LITERAL"||Array.isArray(i.literalType)&&i.literalType[0]==="DATE_LITERAL")}function Se(i){return i&&A(i.field)&&A(i.operator)&&!N(i.value)&&!N(i.dateLiteralVariable)}function j(i){return i&&!N(i.fn)&&A(i.operator)&&!N(i.value)}function D(i){return i&&L(i.openParen)&&N(i.operator)&&N(i.field)&&N(i.fn)&&N(i.closeParen)}function q(i){return i&&A(i.field)&&A(i.operator)&&!N(i.valueQuery)&&N(i.value)}function K(i){return i&&!N(i.field)}function Ie(i){return i&&!N(i.fn)}function P(i){return i&&!N(i.field)}function Le(i){return i&&!N(i.fn)}function Oe(i){return["IN","NOT IN","INCLUDES","EXCLUDES"].includes(i)}function pe(i,e,s){if(N(e))return i;if(s&&e!=="APEX_BIND_VARIABLE"&&Oe(s)&&!Array.isArray(i)&&(i=[i],e=Array.isArray(e)?e:[e]),Array.isArray(e)&&Array.isArray(i))return i.map((t,n)=>Fe(t,e?.[n]));switch(Array.isArray(e)&&(e=e[0]),e){case"STRING":return Array.isArray(i)?i.filter(Boolean).map(t=>A(t)&&t.startsWith("'")?t:`'${t??""}'`):(i=String(i??""),A(i)&&i.startsWith("'")?i:`'${i??""}'`);case"APEX_BIND_VARIABLE":return`:${i}`;default:return i}}function Fe(i,e){return e==="STRING"?A(i)&&i.startsWith("'")?i:`'${i??""}'`:i}function Ge(i){return me(i)}function me(i){if(typeof i=="string")return{type:"Field",field:i};if(oe(i)){let e=[];return i.parameters&&(e=Array.isArray(i.parameters)?i.parameters:[i.parameters]),{type:"FieldFunctionExpression",functionName:i.functionName||i.fn,parameters:e,alias:i.alias}}else{if(le(i))return{type:"FieldRelationship",field:i.field,relationships:i.relationships,objectPrefix:i.objectPrefix};if(ue(i))return{type:"FieldSubquery",subquery:i.subquery};if(Ee(i))return{type:"FieldTypeof",field:i.field,conditions:i.conditions};if(ae(i))return{type:"Field",field:i.field,objectPrefix:i.objectPrefix};throw new TypeError("The input object provided did not match any valid field types")}}function Me(i,e){if(!i)return[];i=he(i)?i.subquery:i;let s=i.fields;if(!s)return[];let t={};i.groupBy&&(t=(Array.isArray(i.groupBy)?i.groupBy:[i.groupBy]).reduce((l,E)=>(P(E)&&(l[E.field.toLocaleLowerCase()]=E.field),l),{}));let n=-1,a=(m(i)?i.relationshipName:i.sObject||"").toLowerCase(),o=(i.sObjectAlias||"").toLowerCase();return s.flatMap(l=>{switch(l.type){case"Field":return l.alias||l.field;case"FieldFunctionExpression":{let E=Q(l);if(E=E.map(u=>{if(u.includes(".")){let R=u.split("."),f=R[0].toLowerCase();return(f===o||f===a)&&(R=R.slice(1)),R.join(".")}return u}),l.alias&&(l.isAggregateFn||e))return l.alias;if(l.alias){let u=E[0];return u.includes(".")?(E=u.split(".").slice(0,-1),E.push(l.alias),E.join(".")):l.alias}return l.isAggregateFn||ne(l)||e?(n++,`expr${n}`):E.length>0?E.join("."):l.functionName}case"FieldRelationship":{let E=l.relationships[0].toLowerCase();return M(l)?l.alias:l.rawValue&&t[l.rawValue.toLocaleLowerCase()]?l.field:E===o||E===a?l.relationships.concat([l.field]).slice(1).join("."):l.relationships.concat([l.field]).join(".")}case"FieldSubquery":return l.subquery.relationshipName;case"FieldTypeof":{let E=new Set,u=[];return l.conditions.forEach(R=>{R.fieldList.forEach(f=>{E.has(f)||(E.add(f),u.push(`${l.field}.${f}`))})}),u}default:break}}).filter(l=>A(l))}var v=class{constructor(e,s){this.currIndent=1;this.enabled=e,this.options={numIndent:s.numIndent??1,fieldMaxLineLength:s.fieldMaxLineLength??60,fieldSubqueryParensOnOwnLine:s.fieldSubqueryParensOnOwnLine??!0,whereClauseOperatorsIndented:!0,newLineAfterKeywords:s.newLineAfterKeywords??!1,logging:s.logging??!1},this.options.newLineAfterKeywords&&(this.options.fieldSubqueryParensOnOwnLine=!0)}log(e){this.options.logging&&console.log(e)}getIndent(e=0){return this.repeatChar((this.currIndent+e)*(this.options.numIndent||1)," ")}repeatChar(e,s){return new Array(e).fill(s).join("")}setSubquery(e){this.currIndent=e?this.currIndent+=1:this.currIndent-=1}stepCurrIndex(e){this.currIndent+=e}formatFields(e){function s(t){e.fields[t-1]&&(e.fields[t-1].suffix=e.fields[t-1].suffix.trim())}if(e.fields.forEach((t,n)=>{t.suffix=e.fields.length-1===n?"":", "}),this.enabled){let t=0,n=!1;e.fields.forEach((a,o)=>{a.isSubquery?(s(o),a.prefix=`
1
+ var T=(r=>(r[r.AND=0]="AND",r[r.AS=1]="AS",r[r.ASC=2]="ASC",r[r.DESC=3]="DESC",r[r.CUBE=4]="CUBE",r[r.ELSE=5]="ELSE",r[r.EXCLUDES=6]="EXCLUDES",r[r.FALSE=7]="FALSE",r[r.FIRST=8]="FIRST",r[r.FROM=9]="FROM",r[r.GROUP_BY=10]="GROUP_BY",r[r.HAVING=11]="HAVING",r[r.IN=12]="IN",r[r.INCLUDES=13]="INCLUDES",r[r.LAST=14]="LAST",r[r.LIKE=15]="LIKE",r[r.LIMIT=16]="LIMIT",r[r.NOT=17]="NOT",r[r.NULL=18]="NULL",r[r.NULLS=19]="NULLS",r[r.OR=20]="OR",r[r.ORDER_BY=21]="ORDER_BY",r[r.ROLLUP=22]="ROLLUP",r[r.SELECT=23]="SELECT",r[r.TRUE=24]="TRUE",r[r.USING=25]="USING",r[r.WHERE=26]="WHERE",r[r.WITH=27]="WITH",r[r.FOR=28]="FOR",r[r.UPDATE=29]="UPDATE",r[r.ABOVE=30]="ABOVE",r[r.ABOVE_OR_BELOW=31]="ABOVE_OR_BELOW",r[r.APEX_NEW=32]="APEX_NEW",r[r.AT=33]="AT",r[r.BELOW=34]="BELOW",r[r.DATA_CATEGORY=35]="DATA_CATEGORY",r[r.END=36]="END",r[r.OFFSET=37]="OFFSET",r[r.REFERENCE=38]="REFERENCE",r[r.SCOPE=39]="SCOPE",r[r.TRACKING=40]="TRACKING",r[r.THEN=41]="THEN",r[r.TYPEOF=42]="TYPEOF",r[r.VIEW=43]="VIEW",r[r.VIEWSTAT=44]="VIEWSTAT",r[r.WHEN=45]="WHEN",r[r.SECURITY_ENFORCED=46]="SECURITY_ENFORCED",r[r.SYSTEM_MODE=47]="SYSTEM_MODE",r[r.USER_MODE=48]="USER_MODE",r[r.CALENDAR_MONTH=49]="CALENDAR_MONTH",r[r.CALENDAR_QUARTER=50]="CALENDAR_QUARTER",r[r.CALENDAR_YEAR=51]="CALENDAR_YEAR",r[r.DAY_IN_MONTH=52]="DAY_IN_MONTH",r[r.DAY_IN_WEEK=53]="DAY_IN_WEEK",r[r.DAY_IN_YEAR=54]="DAY_IN_YEAR",r[r.DAY_ONLY=55]="DAY_ONLY",r[r.FISCAL_MONTH=56]="FISCAL_MONTH",r[r.FISCAL_QUARTER=57]="FISCAL_QUARTER",r[r.FISCAL_YEAR=58]="FISCAL_YEAR",r[r.HOUR_IN_DAY=59]="HOUR_IN_DAY",r[r.WEEK_IN_MONTH=60]="WEEK_IN_MONTH",r[r.WEEK_IN_YEAR=61]="WEEK_IN_YEAR",r[r.AVG=62]="AVG",r[r.COUNT=63]="COUNT",r[r.COUNT_DISTINCT=64]="COUNT_DISTINCT",r[r.MIN=65]="MIN",r[r.MAX=66]="MAX",r[r.SUM=67]="SUM",r[r.DISTANCE=68]="DISTANCE",r[r.GEOLOCATION=69]="GEOLOCATION",r[r.FIELDS=70]="FIELDS",r[r.FORMAT=71]="FORMAT",r[r.TOLABEL=72]="TOLABEL",r[r.CONVERT_TIMEZONE=73]="CONVERT_TIMEZONE",r[r.CONVERT_CURRENCY=74]="CONVERT_CURRENCY",r[r.GROUPING=75]="GROUPING",r[r.ALL=76]="ALL",r[r.CUSTOM=77]="CUSTOM",r[r.STANDARD=78]="STANDARD",r[r.YESTERDAY=79]="YESTERDAY",r[r.TODAY=80]="TODAY",r[r.TOMORROW=81]="TOMORROW",r[r.LAST_WEEK=82]="LAST_WEEK",r[r.THIS_WEEK=83]="THIS_WEEK",r[r.NEXT_WEEK=84]="NEXT_WEEK",r[r.LAST_MONTH=85]="LAST_MONTH",r[r.THIS_MONTH=86]="THIS_MONTH",r[r.NEXT_MONTH=87]="NEXT_MONTH",r[r.LAST_90_DAYS=88]="LAST_90_DAYS",r[r.NEXT_90_DAYS=89]="NEXT_90_DAYS",r[r.THIS_QUARTER=90]="THIS_QUARTER",r[r.LAST_QUARTER=91]="LAST_QUARTER",r[r.NEXT_QUARTER=92]="NEXT_QUARTER",r[r.THIS_YEAR=93]="THIS_YEAR",r[r.LAST_YEAR=94]="LAST_YEAR",r[r.NEXT_YEAR=95]="NEXT_YEAR",r[r.THIS_FISCAL_QUARTER=96]="THIS_FISCAL_QUARTER",r[r.LAST_FISCAL_QUARTER=97]="LAST_FISCAL_QUARTER",r[r.NEXT_FISCAL_QUARTER=98]="NEXT_FISCAL_QUARTER",r[r.THIS_FISCAL_YEAR=99]="THIS_FISCAL_YEAR",r[r.LAST_FISCAL_YEAR=100]="LAST_FISCAL_YEAR",r[r.NEXT_FISCAL_YEAR=101]="NEXT_FISCAL_YEAR",r[r.NEXT_N_DAYS=102]="NEXT_N_DAYS",r[r.LAST_N_DAYS=103]="LAST_N_DAYS",r[r.N_DAYS_AGO=104]="N_DAYS_AGO",r[r.NEXT_N_WEEKS=105]="NEXT_N_WEEKS",r[r.LAST_N_WEEKS=106]="LAST_N_WEEKS",r[r.N_WEEKS_AGO=107]="N_WEEKS_AGO",r[r.NEXT_N_MONTHS=108]="NEXT_N_MONTHS",r[r.LAST_N_MONTHS=109]="LAST_N_MONTHS",r[r.N_MONTHS_AGO=110]="N_MONTHS_AGO",r[r.NEXT_N_QUARTERS=111]="NEXT_N_QUARTERS",r[r.LAST_N_QUARTERS=112]="LAST_N_QUARTERS",r[r.N_QUARTERS_AGO=113]="N_QUARTERS_AGO",r[r.NEXT_N_YEARS=114]="NEXT_N_YEARS",r[r.LAST_N_YEARS=115]="LAST_N_YEARS",r[r.N_YEARS_AGO=116]="N_YEARS_AGO",r[r.NEXT_N_FISCAL_QUARTERS=117]="NEXT_N_FISCAL_QUARTERS",r[r.LAST_N_FISCAL_QUARTERS=118]="LAST_N_FISCAL_QUARTERS",r[r.N_FISCAL_QUARTERS_AGO=119]="N_FISCAL_QUARTERS_AGO",r[r.NEXT_N_FISCAL_YEARS=120]="NEXT_N_FISCAL_YEARS",r[r.LAST_N_FISCAL_YEARS=121]="LAST_N_FISCAL_YEARS",r[r.N_FISCAL_YEARS_AGO=122]="N_FISCAL_YEARS_AGO",r[r.DELEGATED=123]="DELEGATED",r[r.EVERYTHING=124]="EVERYTHING",r[r.MINE_AND_MY_GROUPS=125]="MINE_AND_MY_GROUPS",r[r.MINE=126]="MINE",r[r.MY_TERRITORY=127]="MY_TERRITORY",r[r.MY_TEAM_TERRITORY=128]="MY_TEAM_TERRITORY",r[r.TEAM=129]="TEAM",r[r.ALL_PRIVATE=130]="ALL_PRIVATE",r[r.MRU=131]="MRU",r[r.EQUAL=132]="EQUAL",r[r.NOT_EQUAL=133]="NOT_EQUAL",r[r.LESS_THAN=134]="LESS_THAN",r[r.LESS_THAN_OR_EQUAL=135]="LESS_THAN_OR_EQUAL",r[r.GREATER_THAN=136]="GREATER_THAN",r[r.GREATER_THAN_OR_EQUAL=137]="GREATER_THAN_OR_EQUAL",r[r.L_PAREN=138]="L_PAREN",r[r.R_PAREN=139]="R_PAREN",r[r.L_SQUARE_BRACKET=140]="L_SQUARE_BRACKET",r[r.R_SQUARE_BRACKET=141]="R_SQUARE_BRACKET",r[r.DOT=142]="DOT",r[r.COMMA=143]="COMMA",r[r.COLON=144]="COLON",r[r.SEMICOLON=145]="SEMICOLON",r[r.ASTERISK=146]="ASTERISK",r[r.PLUS=147]="PLUS",r[r.MINUS=148]="MINUS",r[r.DATETIME=149]="DATETIME",r[r.DATE_TOKEN=150]="DATE_TOKEN",r[r.CURRENCY_PREFIXED_DECIMAL=151]="CURRENCY_PREFIXED_DECIMAL",r[r.CURRENCY_PREFIXED_INTEGER=152]="CURRENCY_PREFIXED_INTEGER",r[r.SIGNED_DECIMAL=153]="SIGNED_DECIMAL",r[r.UNSIGNED_DECIMAL=154]="UNSIGNED_DECIMAL",r[r.SIGNED_INTEGER=155]="SIGNED_INTEGER",r[r.UNSIGNED_INTEGER=156]="UNSIGNED_INTEGER",r[r.STRING_LITERAL=157]="STRING_LITERAL",r[r.GEOLOCATION_UNIT=158]="GEOLOCATION_UNIT",r[r.IDENTIFIER=159]="IDENTIFIER",r[r.EOF=160]="EOF",r))(T||{});function F(i){return i>=49&&i<=61}function g(i){return i>=62&&i<=67}function w(i){return i>=68&&i<=69}function $(i){return i===70}function y(i){return i>=71&&i<=75}function Z(i){return i>=76&&i<=78}function b(i){return i>=79&&i<=101}function U(i){return i>=102&&i<=122}function J(i){return i>=123&&i<=131}function I(i){return i===159||i>=30&&i<=48||i>=49&&i<=61||i>=62&&i<=67||i>=68&&i<=69||i===70||i>=71&&i<=75||i>=76&&i<=78||i>=79&&i<=101||i>=102&&i<=122||i>=123&&i<=131||i===158||i===152}function W(i){return i===153||i===154||i===155||i===156}function ee(i){return i>=132&&i<=137}var pe=new Map([["AND",0],["AS",1],["ASC",2],["DESC",3],["CUBE",4],["ELSE",5],["EXCLUDES",6],["FALSE",7],["FIRST",8],["FROM",9],["HAVING",11],["IN",12],["INCLUDES",13],["LAST",14],["LIKE",15],["LIMIT",16],["NOT",17],["NULL",18],["NULLS",19],["OR",20],["ROLLUP",22],["SELECT",23],["TRUE",24],["USING",25],["WHERE",26],["WITH",27],["FOR",28],["UPDATE",29],["ABOVE_OR_BELOW",31],["ABOVE",30],["NEW",32],["AT",33],["BELOW",34],["END",36],["OFFSET",37],["REFERENCE",38],["SCOPE",39],["TRACKING",40],["THEN",41],["TYPEOF",42],["VIEW",43],["VIEWSTAT",44],["WHEN",45],["SECURITY_ENFORCED",46],["SYSTEM_MODE",47],["USER_MODE",48],["CALENDAR_MONTH",49],["CALENDAR_QUARTER",50],["CALENDAR_YEAR",51],["DAY_IN_MONTH",52],["DAY_IN_WEEK",53],["DAY_IN_YEAR",54],["DAY_ONLY",55],["FISCAL_MONTH",56],["FISCAL_QUARTER",57],["FISCAL_YEAR",58],["HOUR_IN_DAY",59],["WEEK_IN_MONTH",60],["WEEK_IN_YEAR",61],["AVG",62],["COUNT_DISTINCT",64],["COUNT",63],["MIN",65],["MAX",66],["SUM",67],["DISTANCE",68],["GEOLOCATION",69],["FIELDS",70],["FORMAT",71],["TOLABEL",72],["CONVERTTIMEZONE",73],["CONVERTCURRENCY",74],["GROUPING",75],["ALL",76],["CUSTOM",77],["STANDARD",78],["YESTERDAY",79],["TODAY",80],["TOMORROW",81],["LAST_WEEK",82],["THIS_WEEK",83],["NEXT_WEEK",84],["LAST_MONTH",85],["THIS_MONTH",86],["NEXT_MONTH",87],["LAST_90_DAYS",88],["NEXT_90_DAYS",89],["THIS_QUARTER",90],["LAST_QUARTER",91],["NEXT_QUARTER",92],["THIS_YEAR",93],["LAST_YEAR",94],["NEXT_YEAR",95],["THIS_FISCAL_QUARTER",96],["LAST_FISCAL_QUARTER",97],["NEXT_FISCAL_QUARTER",98],["THIS_FISCAL_YEAR",99],["LAST_FISCAL_YEAR",100],["NEXT_FISCAL_YEAR",101],["NEXT_N_DAYS",102],["LAST_N_DAYS",103],["N_DAYS_AGO",104],["NEXT_N_WEEKS",105],["LAST_N_WEEKS",106],["N_WEEKS_AGO",107],["NEXT_N_MONTHS",108],["LAST_N_MONTHS",109],["N_MONTHS_AGO",110],["NEXT_N_QUARTERS",111],["LAST_N_QUARTERS",112],["N_QUARTERS_AGO",113],["NEXT_N_YEARS",114],["LAST_N_YEARS",115],["N_YEARS_AGO",116],["NEXT_N_FISCAL_QUARTERS",117],["LAST_N_FISCAL_QUARTERS",118],["N_FISCAL_QUARTERS_AGO",119],["NEXT_N_FISCAL_YEARS",120],["LAST_N_FISCAL_YEARS",121],["N_FISCAL_YEARS_AGO",122],["DELEGATED",123],["EVERYTHING",124],["MINEANDMYGROUPS",125],["MINE",126],["MY_TERRITORY",127],["MY_TEAM_TERRITORY",128],["TEAM",129],["ALLPRIVATE",130],["MRU",131]]),c=[];(function(){for(let e of Object.keys(T)){let s=T[e];typeof s=="number"&&(c[s]=e)}c[72]="toLabel",c[73]="convertTimezone",c[74]="convertCurrency",c[157]="StringIdentifier",c[150]="DATE",c[32]="new",c[125]="MineAndMyGroups",c[127]="MyTerritory",c[128]="MyTeamTerritory",c[130]="AllPrivate",c[123]="Delegated",c[124]="Everything",c[126]="Mine",c[129]="Team",c[131]="Mru",c[159]="Identifier",c[132]="EQUAL",c[156]="UNSIGNED_INTEGER",c[155]="SIGNED_INTEGER",c[154]="UNSIGNED_DECIMAL",c[153]="SIGNED_DECIMAL",c[151]="CURRENCY_PREFIXED_DECIMAL",c[152]="CURRENCY_PREFIXED_INTEGER"})();function d(i){return c[i]||T[i]}function B(i){return i>=65&&i<=90||i>=97&&i<=122}function p(i){return i>=48&&i<=57}function Y(i){return B(i)||p(i)||i===95}function D(i){return i===32||i===9||i===10||i===13||i===12}function te(i){let e=[],s=i.length,t=0;for(;t<s;){let n=i.charCodeAt(t);if(D(n)){for(t++;t<s&&D(i.charCodeAt(t));)t++;continue}if(n===39){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(E===92){t+=2;continue}if(E===39){t++;break}t++}let o=i.substring(a,t),l=o.substring(1,o.length-1).toUpperCase();l==="MI"||l==="KM"?e.push({kind:158,text:o,start:a}):e.push({kind:157,text:o,start:a});continue}if(p(n)){if(t+9<s&&i.charCodeAt(t+4)===45&&i.charCodeAt(t+7)===45){let o=!0;for(let h of[0,1,2,3,5,6,8,9])if(!p(i.charCodeAt(t+h))){o=!1;break}if(o)if(t+10<s&&(i.charCodeAt(t+10)===84||i.charCodeAt(t+10)===116)){let h=t;for(t+=11;t<s;){let l=i.charCodeAt(t);if(p(l)||l===58)t++;else break}if(t<s&&i.charCodeAt(t)===46)for(t++;t<s&&p(i.charCodeAt(t));)t++;if(t<s){let l=i.charCodeAt(t);if(l===90||l===122)t++;else if(l===46){for(t++;t<s&&p(i.charCodeAt(t));)t++;t<s&&(i.charCodeAt(t)===90||i.charCodeAt(t)===122)&&t++}else if(l===43||l===45)for(t++;t<s;){let E=i.charCodeAt(t);if(p(E)||E===58)t++;else break}}e.push({kind:149,text:i.substring(h,t),start:h});continue}else{let h=t;t+=10,e.push({kind:150,text:i.substring(h,t),start:h});continue}}let a=t;for(;t<s&&p(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&p(i.charCodeAt(t+1))){for(t++;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:156,text:i.substring(a,t),start:a});continue}if(n===33){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:133,text:"!=",start:t}),t+=2;continue}throw new Error(`Unexpected character '!' at position ${t}`)}if(n===60){if(t+1<s){let a=i.charCodeAt(t+1);if(a===61){e.push({kind:135,text:"<=",start:t}),t+=2;continue}if(a===62){e.push({kind:133,text:"<>",start:t}),t+=2;continue}}e.push({kind:134,text:"<",start:t}),t++;continue}if(n===62){if(t+1<s&&i.charCodeAt(t+1)===61){e.push({kind:137,text:">=",start:t}),t+=2;continue}e.push({kind:136,text:">",start:t}),t++;continue}if(n===61){e.push({kind:132,text:"=",start:t}),t++;continue}if(n===40){e.push({kind:138,text:"(",start:t}),t++;continue}if(n===41){e.push({kind:139,text:")",start:t}),t++;continue}if(n===91){e.push({kind:140,text:"[",start:t}),t++;continue}if(n===93){e.push({kind:141,text:"]",start:t}),t++;continue}if(n===44){e.push({kind:143,text:",",start:t}),t++;continue}if(n===58){e.push({kind:144,text:":",start:t}),t++;continue}if(n===59){e.push({kind:145,text:";",start:t}),t++;continue}if(n===42){e.push({kind:146,text:"*",start:t}),t++;continue}if(n===43||n===45){if(t+1<s){let a=i.charCodeAt(t+1);if(p(a)){let o=t;for(t++;t<s&&p(i.charCodeAt(t));)t++;if(t<s&&i.charCodeAt(t)===46&&t+1<s&&p(i.charCodeAt(t+1))){for(t++;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}e.push({kind:155,text:i.substring(o,t),start:o});continue}if(a===46&&t+2<s&&p(i.charCodeAt(t+2))){let o=t;for(t+=2;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:153,text:i.substring(o,t),start:o});continue}}e.push({kind:n===43?147:148,text:i[t],start:t}),t++;continue}if(n===46){if(t+1<s&&p(i.charCodeAt(t+1))){let a=t;for(t++;t<s&&p(i.charCodeAt(t));)t++;e.push({kind:154,text:i.substring(a,t),start:a});continue}e.push({kind:142,text:".",start:t}),t++;continue}if(B(n)){let a=t;for(t++;t<s;){let E=i.charCodeAt(t);if(Y(E)||E===46)t++;else break}let o=i.substring(a,t);if(o.includes(".."))throw new Error(`Invalid identifier with consecutive dots at position ${a}: "${o}"`);let h=o.toUpperCase();if(h==="ORDER"&&!o.includes(".")){let E=v(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:21,text:u,start:a}),t=E;continue}}if(h==="GROUP"&&!o.includes(".")){let E=v(i,t,"BY");if(E!==-1){let u=i.substring(a,E);e.push({kind:10,text:u,start:a}),t=E;continue}}if(h==="DATA"&&!o.includes(".")){let E=v(i,t,"CATEGORY");if(E!==-1){let u=i.substring(a,E);e.push({kind:35,text:u,start:a}),t=E;continue}}if(o.length>=4){let E=o.substring(0,3);if(z(E)){let u=o.substring(3);if(ce(u)){u.includes(".")?e.push({kind:151,text:o,start:a}):e.push({kind:152,text:o,start:a});continue}}}if(o.length===3&&z(o)){let E=t,u=t,R=!1;if(u<s&&i.charCodeAt(u)===45&&(R=!0,u++),R&&u<s&&p(i.charCodeAt(u))){for(;u<s&&p(i.charCodeAt(u));)u++;if(u<s&&i.charCodeAt(u)===46&&u+1<s&&p(i.charCodeAt(u+1))){for(u++;u<s&&p(i.charCodeAt(u));)u++;let f=i.substring(a,u);e.push({kind:151,text:f,start:a}),t=u;continue}if(u>=s||!Y(i.charCodeAt(u))&&i.charCodeAt(u)!==46){let f=i.substring(a,u);e.push({kind:152,text:f,start:a}),t=u;continue}t=E}}if(o.includes(".")){e.push({kind:159,text:o,start:a});continue}let l=pe.get(h);if(l!==void 0){e.push({kind:l,text:o,start:a});continue}e.push({kind:159,text:o,start:a});continue}throw new Error(`Unexpected character '${i[t]}' (code ${n}) at position ${t}`)}return e.push({kind:160,text:"",start:t}),e}function v(i,e,s){let t=i.length,n=e;if(n>=t||!D(i.charCodeAt(n)))return-1;for(;n<t&&D(i.charCodeAt(n));)n++;let a=s.length;if(n+a>t)return-1;for(let h=0;h<a;h++){let l=i.charCodeAt(n+h),E=s.charCodeAt(h);if((l>=97&&l<=122?l-32:l)!==E)return-1}let o=n+a;if(o<t){let h=i.charCodeAt(o);if(Y(h)||h===46)return-1}return o}function z(i){for(let e=0;e<i.length;e++)if(!B(i.charCodeAt(e)))return!1;return!0}function ce(i){if(i.length===0)return!1;let e=0;if(i.charCodeAt(e)===45&&e++,e>=i.length||!p(i.charCodeAt(e)))return!1;for(;e<i.length&&p(i.charCodeAt(e));)e++;if(e<i.length&&i.charCodeAt(e)===46){if(e++,e>=i.length||!p(i.charCodeAt(e)))return!1;for(;e<i.length&&p(i.charCodeAt(e));)e++}return e===i.length}var fe=new Set([23,9,26,21,10,11,16,27,28,29,25,19,2,3,0,20,17,45,41,5,36,42,143,139,160]),H=class i{constructor(e,s){this.pos=0;this.tokens=e,this.config=s}getPos(){return this.pos}peek(){return this.tokens[this.pos]}peekAt(e){let s=this.pos+e;return s>=this.tokens.length?this.tokens[this.tokens.length-1]:this.tokens[s]}advance(){let e=this.tokens[this.pos];return e.kind!==160&&this.pos++,e}expect(e){let s=this.peek();if(s.kind!==e)throw this.error(`Expected ${T[e]} but found ${T[s.kind]} ("${s.text}") at position ${s.start}`);return this.advance()}match(e){return this.peek().kind===e?(this.advance(),!0):!1}check(e){return this.peek().kind===e}isAtEnd(){return this.peek().kind===160}error(e){return this.config.logErrors&&console.log(e),new Error(e)}static{this.CLAUSE_BOUNDARY_KINDS=new Set([26,21,10,11,16,37,27,28,29,25,160])}isAtClauseBoundary(){let e=this.peek().kind;return i.CLAUSE_BOUNDARY_KINDS.has(e)||e===139}synchronize(){for(;!this.isAtEnd();){if(this.isAtClauseBoundary())return;this.advance()}}consumeIdentifier(){let e=this.peek();if(I(e.kind))return this.advance();throw this.error(`Expected identifier but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}checkIdentifier(){return I(this.peek().kind)}isFunctionCall(){let e=this.peek(),s=this.peekAt(1);return(F(e.kind)||g(e.kind)||w(e.kind)||$(e.kind)||y(e.kind))&&s.kind===138}canBeAlias(){let e=this.peek();return!(!I(e.kind)||fe.has(e.kind)||e.kind===37&&this.peekAt(1).kind===156)}getLiteralType(e){let s=d(e);return s==="UNSIGNED_INTEGER"||s==="SIGNED_INTEGER"?"INTEGER":s==="UNSIGNED_DECIMAL"||s==="SIGNED_DECIMAL"?"DECIMAL":s==="CURRENCY_PREFIXED_DECIMAL"?"DECIMAL_WITH_CURRENCY_PREFIX":s==="CURRENCY_PREFIXED_INTEGER"?"INTEGER_WITH_CURRENCY_PREFIX":s==="DATETIME"?"DATETIME":s==="DATE"?"DATE":s==="NULL"?"NULL":s==="StringIdentifier"||s==="Identifier"?"STRING":s==="TRUE"||s==="FALSE"?"BOOLEAN":b(e)?"DATE_LITERAL":U(e)?"DATE_N_LITERAL":"STRING"}parseSelectStatement(e=!1){let s=this.parseSelectClause(),t=this.parseFromClause(),n;e?(n={fields:s,relationshipName:t.sObject},t.sObjectPrefix&&(n.sObjectPrefix=t.sObjectPrefix)):n={fields:s,sObject:t.sObject},t.alias&&(n.sObjectAlias=t.alias),this.applySObjectAlias(n);let a=this.parseClauseStatements();return Object.assign(n,a),n}parseSelectStatementPartial(){let e,s;this.check(23)&&(e=this.parseSelectClause()),this.check(9)&&(s=this.parseFromClause());let t={};e&&(t.fields=e),s&&(t.sObject=s.sObject,s.alias&&(t.sObjectAlias=s.alias)),t.fields&&this.applySObjectAlias(t);let n=this.parseClauseStatements();return Object.assign(t,n),t}applySObjectAlias(e){if(!e.sObjectAlias||!Array.isArray(e.fields))return;let s=e.sObjectAlias;e.fields.forEach(t=>{t.relationships&&t.relationships[0]===s&&(t.relationships=t.relationships.slice(1),t.objectPrefix=s,t.relationships.length===0&&(delete t.relationships,t.type="Field"))})}parseSelectClause(){this.expect(23);let e=[];for(e.push(this.parseSelectField());this.match(143);)e.push(this.parseSelectField());return e}parseSelectField(){let e=this.peek(),s=this.peekAt(1);return e.kind===138&&s.kind===23?this.parseSelectClauseSubqueryIdentifier():e.kind===42?this.parseSelectClauseTypeOf():this.isFunctionCall()?this.parseSelectClauseFunctionIdentifier():this.parseSelectClauseIdentifier()}parseSelectClauseFunctionIdentifier(){let e=this.parseAnyFunction(!0);return this.canBeAlias()&&(e.alias=this.consumeIdentifier().text),e}parseSelectClauseSubqueryIdentifier(){this.expect(138);let e=this.parseSelectStatement(!0);return this.expect(139),{type:"FieldSubquery",subquery:e}}parseSelectClauseTypeOf(){this.expect(42);let e=this.consumeIdentifier().text,s=[];for(;this.check(45);){this.advance();let t=this.consumeIdentifier().text;this.expect(41);let n=[];for(n.push(this.consumeIdentifier().text);this.match(143);)n.push(this.consumeIdentifier().text);s.push({type:"WHEN",objectType:t,fieldList:n})}if(this.check(5)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);s.push({type:"ELSE",fieldList:t})}return this.expect(36),{type:"FieldTypeof",field:e,conditions:s}}parseSelectClauseIdentifier(){let s=this.consumeIdentifier().text,t;if(!s.includes("."))t={type:"Field",field:s};else{let n=s.split(".");t={type:"FieldRelationship",field:n[n.length-1],relationships:n.slice(0,n.length-1),rawValue:s}}return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseFromClause(){this.expect(9);let s=this.consumeIdentifier().text,t;if(s.includes(".")){let n=s.split(".");t={sObjectPrefix:n.slice(0,n.length-1),sObject:n[n.length-1]}}else t={sObject:s};return this.canBeAlias()&&(t.alias=this.consumeIdentifier().text),t}parseClauseStatements(){let e={},s=(t,n)=>{if(this.config.ignoreParseErrors)try{t(),this.isAtClauseBoundary()||(n&&n(),this.synchronize())}catch(a){this.config.logErrors&&console.log(a),n&&n(),this.synchronize()}else t()};for(this.check(25)&&s(()=>{e.usingScope=this.parseUsingScope()}),this.check(26)&&s(()=>{e.where=this.parseWhereClause()},()=>{delete e.where});this.check(27);)s(()=>{let t=this.parseWithClause();t.withSecurityEnforced&&(e.withSecurityEnforced=!0),t.withAccessLevel&&(e.withAccessLevel=t.withAccessLevel),t.withDataCategory&&(e.withDataCategory=t.withDataCategory)});return this.check(10)&&(s(()=>{e.groupBy=this.parseGroupByClause()}),this.check(11)&&s(()=>{e.having=this.parseHavingClause()})),this.check(21)&&s(()=>{e.orderBy=this.parseOrderByClause()}),this.check(16)&&s(()=>{this.advance(),e.limit=Number(this.expect(156).text)}),this.check(37)&&s(()=>{this.advance(),e.offset=Number(this.expect(156).text)}),this.check(28)&&s(()=>{this.advance();let t=this.peek();if(t.kind===43||t.kind===38||t.kind===29)e.for=d(this.advance().kind);else throw this.error(`Expected VIEW, REFERENCE, or UPDATE after FOR at position ${t.start}`)}),this.check(29)&&s(()=>{this.advance();let t=this.peek();if(t.kind===40||t.kind===44)e.update=d(this.advance().kind);else throw this.error(`Expected TRACKING or VIEWSTAT after UPDATE at position ${t.start}`)}),e}parseUsingScope(){this.expect(25),this.expect(39);let e=this.peek();if(J(e.kind))return this.advance().text;if(I(e.kind))return this.advance().text;throw this.error(`Expected scope enumeration value at position ${e.start}`)}parseWhereClause(){return this.expect(26),this.parseConditionExpressions(!0,!0)}parseConditionExpressions(e,s){let t,n,a={left:0,right:0};for(;;){let o;if(t)if(this.check(0))o="AND",this.advance();else if(this.check(20))o="OR",this.advance();else break;let h={},l=h,E=null;for(;this.hasNegationPrefix();){let f=0;for(;this.check(138)&&this.isNegationAhead();)this.advance(),f++,a.left++;if(this.check(17)){this.advance();let _={left:f>0?{openParen:f}:null,operator:"NOT",right:{left:{}}};E?E.right=_:h=_,E=_,l=_.right}else break}l.left=this.parseExpression(e,s,a),t?o&&(n.operator=o,n.right=h):(t=h,n=h);let u=h;for(;u.right&&u.right.left!==void 0;){let f=u.right;if(f.right&&f.right.left!==void 0)u=f;else{u=f;break}}if(n=u,this.isAtEnd())break;let R=this.peek().kind;if(R!==0&&R!==20)break}if(a.left!==a.right)throw this.error(`Mismatched parentheses in WHERE/HAVING clause: ${a.left} open vs ${a.right} close`);return t}hasNegationPrefix(){let e=0;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}isNegationAhead(){let e=1;for(;this.peekAt(e).kind===138;)e++;return this.peekAt(e).kind===17}parseExpression(e,s,t){let n=0;for(;this.check(138)&&!this.isSubqueryStart()&&!this.isArrayExpressionStart();)this.advance(),n++,t&&t.left++;let a,o=!1;e&&g(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseAggregateFunction(!1),o=!0):s&&w(this.peek().kind)&&this.peek().kind===68&&this.peekAt(1).kind===138?(a=this.parseLocationFunction(!1),o=!0):F(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseDateFunction(!1),o=!0):y(this.peek().kind)&&this.peekAt(1).kind===138?(a=this.parseOtherFunction(!1),o=!0):a=this.consumeIdentifier().text;let h=this.parseOperator(),l=h.operator,E=h.isSet,u,R,f;if(E)if(this.check(138)&&this.peekAt(1).kind===23)this.expect(138),u=this.parseSelectStatement(!1),this.expect(139),R="SUBQUERY";else if(this.config.allowApexBindVariables&&this.check(144))u=this.parseApexBindVariable().value,R="APEX_BIND_VARIABLE";else{let S=this.parseArrayExpression();u=S.values,R=S.literalType,f=S.dateLiteralVariable}else{let S=this.parseAtomicValue();u=S.value,R=S.literalType,f=S.dateLiteralVariable}let _=0;for(;this.check(139)&&(!t||t.left>t.right);)this.advance(),_++,t&&t.right++;let C={};return o?C.fn=a:C.field=a,C.operator=l,R==="SUBQUERY"?C.valueQuery=u:C.value=u,C.literalType=R,f!==void 0&&(C.dateLiteralVariable=f),n>0&&(C.openParen=n),_>0&&(C.closeParen=_),C}isSubqueryStart(){return this.check(138)&&this.peekAt(1).kind===23}isArrayExpressionStart(){return!1}parseOperator(){let e=this.peek();if(ee(e.kind))return{operator:this.advance().text,isSet:!1};if(e.kind===15)return this.advance(),{operator:"LIKE",isSet:!1};if(e.kind===12)return this.advance(),{operator:"IN",isSet:!0};if(e.kind===17&&this.peekAt(1).kind===12)return this.advance(),this.advance(),{operator:"NOT IN",isSet:!0};if(e.kind===13)return this.advance(),{operator:"INCLUDES",isSet:!0};if(e.kind===6)return this.advance(),{operator:"EXCLUDES",isSet:!0};throw this.error(`Expected operator but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseAtomicValue(){let e=this.peek();if(this.config.allowApexBindVariables&&e.kind===144)return{value:this.parseApexBindVariable().value,literalType:"APEX_BIND_VARIABLE"};if(e.kind===157)return{value:this.advance().text,literalType:"STRING"};if(e.kind===158)return{value:this.advance().text,literalType:"STRING"};if(e.kind===156)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===155)return{value:this.advance().text,literalType:"INTEGER"};if(e.kind===154)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===153)return{value:this.advance().text,literalType:"DECIMAL"};if(e.kind===151)return{value:this.advance().text,literalType:"DECIMAL_WITH_CURRENCY_PREFIX"};if(e.kind===152)return{value:this.advance().text,literalType:"INTEGER_WITH_CURRENCY_PREFIX"};if(e.kind===149)return{value:this.advance().text,literalType:"DATETIME"};if(e.kind===150)return{value:this.advance().text,literalType:"DATE"};if(e.kind===18)return this.advance(),{value:"NULL",literalType:"NULL"};if(e.kind===24||e.kind===7)return{value:d(this.advance().kind),literalType:"BOOLEAN"};if(b(e.kind))return{value:this.advance().text,literalType:"DATE_LITERAL"};if(U(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,literalType:"DATE_N_LITERAL",dateLiteralVariable:n}}throw this.error(`Expected value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseArrayExpression(){this.expect(138);let e=[];for(e.push(this.parseArrayItem());this.match(143);)e.push(this.parseArrayItem());this.expect(139);let s=e.map(l=>l.value),t=e.map(l=>l.type),n=e.map(l=>l.variable!==void 0?l.variable:null),a=n.some(l=>l!==null),o;new Set(t).size===1?o=this.getLiteralType(this.tokenKindFromTypeName(t[0])):o=t.map(l=>this.getLiteralType(this.tokenKindFromTypeName(l)));let h={values:s,literalType:o};return a&&(h.dateLiteralVariable=n),h}tokenKindFromTypeName(e){for(let s=0;s<=160;s++)if(d(s)===e)return s;return 159}parseArrayItem(){let e=this.peek();if(e.kind===157)return{value:this.advance().text,type:d(157)};if(e.kind===158)return{value:this.advance().text,type:d(158)};if(e.kind===151)return{value:this.advance().text,type:d(151)};if(e.kind===152)return{value:this.advance().text,type:d(152)};if(W(e.kind)){let s=this.advance();return{value:s.text,type:d(s.kind)}}if(e.kind===149)return{value:this.advance().text,type:d(149)};if(e.kind===150)return{value:this.advance().text,type:d(150)};if(e.kind===18)return this.advance(),{value:"NULL",type:"NULL"};if(e.kind===24)return{value:this.advance().text,type:"TRUE"};if(e.kind===7)return{value:this.advance().text,type:"FALSE"};if(b(e.kind))return{value:this.advance().text,type:d(e.kind)};if(U(e.kind)){let s=this.advance();this.expect(144);let t=this.peek(),n;if(t.kind===156||t.kind===155)n=Number(this.advance().text);else throw this.error(`Expected integer after date N literal colon at position ${t.start}`);return{value:`${s.text}:${n}`,type:d(s.kind),variable:n}}throw this.error(`Expected array value but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseApexBindVariable(){this.expect(144);let e=[];for(this.check(32)?e.push(this.parseApexBindNewInstantiation()):this.checkIdentifier()&&(this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):e.push(this.parseApexBindIdentifier()));this.check(142);)this.advance(),this.checkIdentifier()&&this.peekAt(1).kind===138?e.push(this.parseApexBindFunctionCall()):this.checkIdentifier()&&e.push(this.parseApexBindIdentifier());return{value:e.join(".")}}parseApexBindIdentifier(){let e=this.consumeIdentifier().text;return this.check(140)&&(e+=this.parseApexBindArrayAccessor()),e}parseApexBindNewInstantiation(){this.expect(32);let s=`new ${this.consumeIdentifier().text}`;if(this.check(134)){this.advance();let t=[];for(t.push(this.consumeIdentifier().text);this.match(143);)t.push(this.consumeIdentifier().text);this.expect(136),s+=`<${t.join(", ")}>`}return s+=this.parseApexBindFunctionParams(),this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionCall(){let s=this.consumeIdentifier().text+this.parseApexBindFunctionParams();return this.check(140)&&(s+=this.parseApexBindArrayAccessor()),s}parseApexBindFunctionParams(){this.expect(138);let e=[];if(!this.check(139))for(e.push(this.consumeIdentifier().text);this.match(143);)e.push(this.consumeIdentifier().text);return this.expect(139),`(${e.join(", ")})`}parseApexBindArrayAccessor(){this.expect(140);let e=this.peek(),s;if(e.kind===156)s=this.advance().text;else if(I(e.kind))s=this.advance().text;else throw this.error(`Expected integer or identifier in array accessor at position ${e.start}`);return this.expect(141),`[${s}]`}parseAnyFunction(e){let s=this.peek();if(F(s.kind))return this.parseDateFunction(e);if(g(s.kind))return this.parseAggregateFunction(e);if(s.kind===68)return this.parseLocationFunction(e);if($(s.kind))return this.parseFieldsFunction(e);if(y(s.kind))return this.parseOtherFunction(e);throw this.error(`Expected function name but found ${T[s.kind]} at position ${s.start}`)}parseDateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseAggregateFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!0,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,e&&(a.isAggregateFn=!0),a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseFieldsFunction(e){this.advance(),this.expect(138);let s=this.peek(),t;if(Z(s.kind))t=this.advance().text;else if(I(s.kind))t=this.advance().text;else throw this.error(`Expected FIELDS function parameter at position ${s.start}`);this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="FIELDS",n.parameters=[t],n.rawValue=`FIELDS(${t})`,n}parseOtherFunction(e){let s=this.advance(),t=d(s.kind),n=this.parseFunctionExpression(!1,e),a={};return e&&(a.type="FieldFunctionExpression"),a.functionName=t,a.parameters=n,a.rawValue=`${s.text}(${n.map(o=>typeof o=="string"?o:o.rawValue).join(", ")})`,a}parseLocationFunction(e){this.advance(),this.expect(138);let s=this.consumeIdentifier().text;this.expect(143);let t;this.check(69)&&this.peekAt(1).kind===138?t=this.parseGeolocationFunction(e):t=this.consumeIdentifier().text,this.expect(143);let n=this.expect(158).text;this.expect(139);let a={};e&&(a.type="FieldFunctionExpression"),a.functionName="DISTANCE",a.parameters=[s,t,n],e&&(a.isAggregateFn=!0);let o=typeof t=="string"?t:t.rawValue;return a.rawValue=`DISTANCE(${s}, ${o}, ${n})`,a}parseGeolocationFunction(e){this.advance(),this.expect(138);let s=this.parseNumberValue();this.expect(143);let t=this.parseNumberValue();this.expect(139);let n={};return e&&(n.type="FieldFunctionExpression"),n.functionName="GEOLOCATION",n.parameters=[s,t],n.rawValue=`GEOLOCATION(${s}, ${t})`,n}parseNumberValue(){let e=this.peek();if(e.kind===156||e.kind===155||e.kind===154||e.kind===153)return this.advance().text;throw this.error(`Expected number but found ${T[e.kind]} ("${e.text}") at position ${e.start}`)}parseFunctionExpression(e,s=!0){this.expect(138);let t=[];if(!this.check(139))for(t.push(this.parseFunctionParam(e,s));this.match(143);)t.push(this.parseFunctionParam(e,s));return this.expect(139),t}parseFunctionParam(e,s){let t=this.peek(),n=this.peekAt(1);if(!e&&g(t.kind)&&n.kind===138)return this.parseAggregateFunction(s);if(y(t.kind)&&n.kind===138)return this.parseOtherFunction(s);if(t.kind===157)return this.advance().text;if(W(t.kind))return this.advance().text;if(I(t.kind))return this.advance().text;throw this.error(`Expected function parameter but found ${T[t.kind]} ("${t.text}") at position ${t.start}`)}parseGroupByClause(){this.expect(10);let e=[];for(e.push(this.parseGroupByItem());this.match(143);)e.push(this.parseGroupByItem());return e}parseGroupByItem(){let e=this.peek();return e.kind===4&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("CUBE")}:e.kind===22&&this.peekAt(1).kind===138?{fn:this.parseCubeOrRollupFunction("ROLLUP")}:F(e.kind)&&this.peekAt(1).kind===138?{fn:this.parseDateFunction(!1)}:{field:this.consumeIdentifier().text}}parseCubeOrRollupFunction(e){let s=this.advance(),t=this.parseFunctionExpression(!1);return{functionName:e,parameters:t,rawValue:`${e}(${t.join(", ")})`}}parseHavingClause(){return this.expect(11),this.parseConditionExpressions(!0,!1)}parseOrderByClause(){this.expect(21);let e=[];for(e.push(this.parseOrderByItem());this.match(143);)e.push(this.parseOrderByItem());return e}parseOrderByItem(){let e=this.peek(),s=this.peekAt(1),t;if(e.kind===75&&s.kind===138){let n=this.advance(),a=this.parseFunctionExpression(!1);t={fn:{functionName:d(n.kind),parameters:a,rawValue:`${n.text}(${a.join(", ")})`}}}else g(e.kind)&&s.kind===138?t={fn:this.parseAggregateFunction(!1)}:F(e.kind)&&s.kind===138?t={fn:this.parseDateFunction(!1)}:e.kind===68&&s.kind===138?t={fn:this.parseLocationFunction(!1)}:t={field:this.consumeIdentifier().text};if(this.check(2)?t.order=d(this.advance().kind):this.check(3)&&(t.order=d(this.advance().kind)),this.check(19))if(this.advance(),this.check(8))t.nulls=d(this.advance().kind);else if(this.check(14))t.nulls=d(this.advance().kind);else throw this.error(`Expected FIRST or LAST after NULLS at position ${this.peek().start}`);return t}parseWithClause(){this.expect(27);let e=this.peek();if(e.kind===46)return this.advance(),{withSecurityEnforced:!0};if(e.kind===48)return this.advance(),{withAccessLevel:"USER_MODE"};if(e.kind===47)return this.advance(),{withAccessLevel:"SYSTEM_MODE"};if(e.kind===35)return{withDataCategory:this.parseWithDataCategory()};throw this.error(`Expected SECURITY_ENFORCED, USER_MODE, SYSTEM_MODE, or DATA CATEGORY after WITH at position ${e.start}`)}parseWithDataCategory(){this.expect(35);let e=[];for(e.push(this.parseDataCategoryCondition());this.check(0);)this.advance(),e.push(this.parseDataCategoryCondition());return{conditions:e}}parseDataCategoryCondition(){let e=this.consumeIdentifier().text,s=this.peek(),t;if(s.kind===33||s.kind===30||s.kind===34||s.kind===31)t=this.advance().text;else throw this.error(`Expected data category selector (AT, ABOVE, BELOW, ABOVE_OR_BELOW) at position ${s.start}`);let n=this.match(138),a=[];for(a.push(this.consumeIdentifier().text);this.match(143);)a.push(this.consumeIdentifier().text);return n&&this.expect(139),{groupName:e,selector:t,parameters:a}}};function k(i,e){let s=e||{},t=te(i),n=new H(t,s),a;if(s.allowPartialQuery?a=n.parseSelectStatementPartial():a=n.parseSelectStatement(!1),!n.isAtEnd()&&!s.ignoreParseErrors){let o=t[n.getPos()];throw new Error(`Unexpected token "${o.text}" at position ${o.start}. Expected end of query.`)}return a}function Ae(i,e){try{return k(i,e),!0}catch{return!1}}function A(i){return typeof i=="string"}function de(i){return i?i.every(e=>A(e)):!1}function L(i){return Number.isFinite(i)}function N(i){return i==null}function O(i,e,s){return N(i)?"":`${s||""}${i}${e||""}`}function ie(i,e=!1){return Array.isArray(i)?i.length>0?`(${i.join(", ")})`:e?"()":"":e?`(${i||""})`:i||""}function re(i,e,s=""){return L(i)&&i>0?new Array(i).fill(e).join(s):""}function V(i){return!i.parameters||i.parameters.length===0?[]:de(i.parameters)?i.parameters:A(i.parameters[0])?[i.parameters[0]]:V(i.parameters[0])}function se(i){if(!i.parameters||i.parameters.length===0)return!1;let e=i.parameters[0];return A(e)?!1:!!e.isAggregateFn}function G(i){return i&&!N(i.alias)}function ne(i){return A(i.field)&&!Array.isArray(i.relationships)&&!Array.isArray(i.conditions)}function ae(i){return!N(i.functionName||i.fn)}function oe(i){return A(i.field)&&Array.isArray(i.relationships)}function le(i){return!N(i.subquery)}function ue(i){return A(i.field)&&Array.isArray(i.conditions)}function m(i){return A(i.relationshipName)}function Ee(i){return!!i&&!!i.type&&i.type==="FieldSubquery"}function Ne(i){return!!i&&!!i.operator&&!!i.right}function Re(i){return!!i&&!!i.operator&&!!i.right}function Q(i){return!!i&&!!i.operator&&!!i.right}function Te(i){return i&&A(i.field)&&A(i.operator)&&!N(i.value)}function Ce(i){return i&&A(i.field)&&A(i.operator)&&!N(i.value)&&(i.literalType==="DATE_LITERAL"||Array.isArray(i.literalType)&&i.literalType[0]==="DATE_LITERAL")}function _e(i){return i&&A(i.field)&&A(i.operator)&&!N(i.value)&&!N(i.dateLiteralVariable)}function X(i){return i&&!N(i.fn)&&A(i.operator)&&!N(i.value)}function x(i){return i&&L(i.openParen)&&N(i.operator)&&N(i.field)&&N(i.fn)&&N(i.closeParen)}function j(i){return i&&A(i.field)&&A(i.operator)&&!N(i.valueQuery)&&N(i.value)}function q(i){return i&&!N(i.field)}function Se(i){return i&&!N(i.fn)}function M(i){return i&&!N(i.field)}function Ie(i){return i&&!N(i.fn)}function Le(i){return["IN","NOT IN","INCLUDES","EXCLUDES"].includes(i)}function he(i,e,s){if(N(e))return i;if(s&&e!=="APEX_BIND_VARIABLE"&&Le(s)&&!Array.isArray(i)&&(i=[i],e=Array.isArray(e)?e:[e]),Array.isArray(e)&&Array.isArray(i))return i.map((t,n)=>Oe(t,e?.[n]));switch(Array.isArray(e)&&(e=e[0]),e){case"STRING":return Array.isArray(i)?i.filter(Boolean).map(t=>A(t)&&t.startsWith("'")?t:`'${t??""}'`):(i=String(i??""),A(i)&&i.startsWith("'")?i:`'${i??""}'`);case"APEX_BIND_VARIABLE":return`:${i}`;default:return i}}function Oe(i,e){switch(e){case"STRING":return A(i)&&i.startsWith("'")?i:`'${i??""}'`;case"NULL":return"null";default:return i}}function ke(i){return ge(i)}function ge(i){if(typeof i=="string")return{type:"Field",field:i};if(ae(i)){let e=[];return i.parameters&&(e=Array.isArray(i.parameters)?i.parameters:[i.parameters]),{type:"FieldFunctionExpression",functionName:i.functionName||i.fn,parameters:e,alias:i.alias}}else{if(oe(i))return{type:"FieldRelationship",field:i.field,relationships:i.relationships,objectPrefix:i.objectPrefix};if(le(i))return{type:"FieldSubquery",subquery:i.subquery};if(ue(i))return{type:"FieldTypeof",field:i.field,conditions:i.conditions};if(ne(i))return{type:"Field",field:i.field,objectPrefix:i.objectPrefix};throw new TypeError("The input object provided did not match any valid field types")}}function Ge(i,e){if(!i)return[];i=Ee(i)?i.subquery:i;let s=i.fields;if(!s)return[];let t={};i.groupBy&&(t=(Array.isArray(i.groupBy)?i.groupBy:[i.groupBy]).reduce((l,E)=>(M(E)&&(l[E.field.toLocaleLowerCase()]=E.field),l),{}));let n=-1,a=(m(i)?i.relationshipName:i.sObject||"").toLowerCase(),o=(i.sObjectAlias||"").toLowerCase();return s.flatMap(l=>{switch(l.type){case"Field":return l.alias||l.field;case"FieldFunctionExpression":{let E=V(l);if(E=E.map(u=>{if(u.includes(".")){let R=u.split("."),f=R[0].toLowerCase();return(f===o||f===a)&&(R=R.slice(1)),R.join(".")}return u}),l.alias&&(l.isAggregateFn||e))return l.alias;if(l.alias){let u=E[0];return u.includes(".")?(E=u.split(".").slice(0,-1),E.push(l.alias),E.join(".")):l.alias}return l.isAggregateFn||se(l)||e?(n++,`expr${n}`):E.length>0?E.join("."):l.functionName}case"FieldRelationship":{let E=l.relationships[0].toLowerCase();return G(l)?l.alias:l.rawValue&&t[l.rawValue.toLocaleLowerCase()]?l.field:E===o||E===a?l.relationships.concat([l.field]).slice(1).join("."):l.relationships.concat([l.field]).join(".")}case"FieldSubquery":return l.subquery.relationshipName;case"FieldTypeof":{let E=new Set,u=[];return l.conditions.forEach(R=>{R.fieldList.forEach(f=>{E.has(f)||(E.add(f),u.push(`${l.field}.${f}`))})}),u}default:break}}).filter(l=>A(l))}var P=class{constructor(e,s){this.currIndent=1;this.enabled=e,this.options={numIndent:s.numIndent??1,fieldMaxLineLength:s.fieldMaxLineLength??60,fieldSubqueryParensOnOwnLine:s.fieldSubqueryParensOnOwnLine??!0,whereClauseOperatorsIndented:!0,newLineAfterKeywords:s.newLineAfterKeywords??!1,logging:s.logging??!1},this.options.newLineAfterKeywords&&(this.options.fieldSubqueryParensOnOwnLine=!0)}log(e){this.options.logging&&console.log(e)}getIndent(e=0){return this.repeatChar((this.currIndent+e)*(this.options.numIndent||1)," ")}repeatChar(e,s){return new Array(e).fill(s).join("")}setSubquery(e){this.currIndent=e?this.currIndent+=1:this.currIndent-=1}stepCurrIndex(e){this.currIndent+=e}formatFields(e){function s(t){e.fields[t-1]&&(e.fields[t-1].suffix=e.fields[t-1].suffix.trim())}if(e.fields.forEach((t,n)=>{t.suffix=e.fields.length-1===n?"":", "}),this.enabled){let t=0,n=!1;e.fields.forEach((a,o)=>{a.isSubquery?(s(o),a.prefix=`
2
2
  ${this.getIndent()}`,a.suffix=e.fields.length-1===o?"":", ",t=0,n=!0):Array.isArray(a.typeOfClause)?(s(o),a.prefix=`
3
3
  ${this.getIndent()}`,n=!0):L(this.options.fieldMaxLineLength)&&(t+=a.text.length+a.suffix.length,(t>this.options.fieldMaxLineLength||n)&&(s(o),(!this.options.newLineAfterKeywords||o>0)&&(a.prefix+=`
4
4
  ${this.getIndent()}`),t=0,n=!1)),this.log(a)})}}formatTyeOfField(e,s){return this.enabled&&this.options.newLineAfterKeywords?s.map((t,n)=>n===0?t:n===s.length-1?`${this.getIndent()}${t}`:`${this.getIndent()} ${t}`).join(`
@@ -18,6 +18,6 @@ ${e}`:` ${e}`}formatText(e){return this.enabled&&(this.options.newLineAfterKeywo
18
18
  `,s=0):t+=`${n}${o?", ":""}`}),t}else return e.join(", ")}formatParens(e,s,t=!1){let n="";if(L(e)&&e>0)if(this.enabled)if(s==="("){for(let a=0;a<e;a++)t&&a===e-1?n+="(":a===0?n+=`(
19
19
  `:(this.currIndent++,n+=`${this.getIndent()}(
20
20
  `);(!t||e>1)&&this.currIndent++}else for(let a=e-1;a>=0;a--)t&&a===e-1?n+=")":(this.currIndent--,n+=`
21
- ${this.getIndent()})`);else n+=se(e,s);return n}formatWhereClauseOperators(e,s,t=0){let n=e==="NOT";return this.enabled&&!n?`
21
+ ${this.getIndent()})`);else n+=re(e,s);return n}formatWhereClauseOperators(e,s,t=0){let n=e==="NOT";return this.enabled&&!n?`
22
22
  ${this.getIndent(t)}${e} ${s}`:`${n?"":" "}${e} ${s}`}formatAddNewLine(e=" ",s){return this.enabled&&!s?`
23
- `:e}};function He(i,e,s){return ye(G(i,s),{format:!0,formatOptions:e})}function ye(i,e={}){if(!i)return"";e=e||{},e.format=!!e.format,e.logging&&(console.time("composer"),console.log("Composing Query:",i),console.log("Format output:",e.format));let s=new z(i,e).query;return e.logging&&console.timeEnd("composer"),s}var z=class{constructor(e,s={}){this.soql=e;this.logging=!1;this.format=!1;s={autoCompose:!0,...s};let{logging:t,format:n}=s;this.logging=!!t,this.format=!!n,this.query="",this.formatter=new v(this.format,{logging:this.logging,...s.formatOptions}),s.autoCompose&&this.start()}start(){this.query=this.parseQuery(this.soql)}log(e){this.logging&&console.log("Current SOQL:",e)}parseFn(e){let s;return e.rawValue?s=e.rawValue:(s=e.functionName,s+=`(${(e.parameters||[]).map(t=>A(t)?t:this.parseFn(t)).join(", ")})`),e.alias&&(s+=` ${e.alias}`),s}parseQuery(e){let s={fields:this.parseFields(e.fields||[]).map(a=>({text:a.text,typeOfClause:a.typeOfClause,isSubquery:a.text.startsWith("("),prefix:"",suffix:""})),isSubquery:m(e),lineBreaks:[]},t="";e.fields&&(t+=this.formatter.formatClause("SELECT").trimStart()),this.formatter.formatFields(s);let n="";if(s.fields.forEach(a=>{Array.isArray(a.typeOfClause)?n+=`${a.prefix}${this.formatter.formatTyeOfField(a.text,a.typeOfClause)}${a.suffix}`:n+=`${a.prefix}${a.text}${a.suffix}`}),t+=this.formatter.formatText(n),(m(e)?e.relationshipName:e.sObject)&&(t+=this.formatter.formatClause("FROM")),m(e)){let a=e.sObjectPrefix||[];a.push(e.relationshipName),t+=this.formatter.formatText(`${a.join(".")}${O(e.sObjectAlias,""," ")}`)}else e.sObject&&(t+=this.formatter.formatText(`${e.sObject}${O(e.sObjectAlias,""," ")}`));return this.log(t),e.usingScope&&(t+=this.formatter.formatClause("USING SCOPE"),t+=this.formatter.formatText(e.usingScope),this.log(t)),e.where&&(t+=this.formatter.formatClause("WHERE"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.where)),this.log(t)),e.groupBy&&(t+=this.formatter.formatClause("GROUP BY"),t+=this.formatter.formatText(this.parseGroupByClause(e.groupBy)),this.log(t),e.having&&(t+=this.formatter.formatClause("HAVING"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.having)),this.log(t))),e.orderBy&&(!Array.isArray(e.orderBy)||e.orderBy.length>0)&&(t+=this.formatter.formatClause("ORDER BY"),t+=this.formatter.formatText(this.parseOrderBy(e.orderBy)),this.log(t)),L(e.limit)&&(t+=this.formatter.formatClause("LIMIT"),t+=this.formatter.formatText(`${e.limit}`),this.log(t)),L(e.offset)&&(t+=this.formatter.formatClause("OFFSET"),t+=this.formatter.formatText(`${e.offset}`),this.log(t)),e.withDataCategory&&(t+=this.formatter.formatClause("WITH DATA CATEGORY"),t+=this.formatter.formatText(this.parseWithDataCategory(e.withDataCategory)),this.log(t)),e.withSecurityEnforced&&(t+=this.formatter.formatClause("WITH SECURITY_ENFORCED"),this.log(t)),e.withAccessLevel&&(t+=this.formatter.formatClause(`WITH ${e.withAccessLevel}`),this.log(t)),e.for&&(t+=this.formatter.formatClause("FOR"),t+=this.formatter.formatText(e.for),this.log(t)),e.update&&(t+=this.formatter.formatClause("UPDATE"),t+=this.formatter.formatText(e.update),this.log(t)),t.trim()}parseFields(e){return e.map(s=>{let t="",n,a=s.objectPrefix?`${s.objectPrefix}.`:"";switch(s.type){case"Field":{t=`${a}${s.field}${s.alias?` ${s.alias}`:""}`;break}case"FieldFunctionExpression":{let o="";s.parameters&&(o=s.parameters.map(h=>A(h)?h:this.parseFields([h]).map(l=>l.text)).join(", ")),t=`${s.functionName}(${o})${s.alias?` ${s.alias}`:""}`;break}case"FieldRelationship":{t=`${a}${s.relationships.join(".")}.${s.field}${M(s)?` ${s.alias}`:""}`;break}case"FieldSubquery":{t=this.formatter.formatSubquery(this.parseQuery(s.subquery));break}case"FieldTypeof":{n=this.parseTypeOfField(s),t=n.join(" ");break}default:break}return{text:t,typeOfClause:n}})}parseTypeOfField(e){let s=[`TYPEOF ${e.field}`].concat(e.conditions.map(t=>this.formatter.formatTypeofFieldCondition(t)));return s.push("END"),s}parseWhereOrHavingClause(e,s=0,t=!1){let n="",a=e.left,o=!1;if(a&&(n+=this.formatter.formatParens(a.openParen,"(",D(a)),!D(a))){s=s+(a.openParen||0)-(a.closeParen||0),t&&s++;let h="";h+=j(a)?this.parseFn(a.fn):a.field,h+=` ${a.operator} `,q(a)?h+=this.formatter.formatSubquery(this.parseQuery(a.valueQuery),1,!0):h+=re(pe(a.value,a.literalType,a.operator)),n+=this.formatter.formatWithIndent(h),n+=this.formatter.formatParens(a.closeParen,")",t)}if(X(e)){let h=O(e.operator);o=h==="NOT";let l=this.formatter.formatWhereClauseOperators(h,this.parseWhereOrHavingClause(e.right,s,D(a)),s);return`${o?n.trimRight():n}${l}`.trim()}else return n.trim()}parseGroupByClause(e){return(Array.isArray(e)?e:[e]).map(s=>P(s)?s.field:this.parseFn(s.fn)).join(", ")}parseOrderBy(e){if(Array.isArray(e))return this.formatter.formatOrderByArray(e.map(s=>this.parseOrderBy(s)));{let s="";return K(e)?s=`${O(e.field," ")}`:s+=`${this.parseFn(e.fn)} `,s+=`${O(e.order," ")}${O(e.nulls,"","NULLS ")}`,s.trim()}}parseWithDataCategory(e){return e.conditions.map(s=>{let t=s.parameters.length>1?`(${s.parameters.join(", ")})`:`${s.parameters.join(", ")}`;return`${s.groupName} ${s.selector} ${t}`}).join(" AND ")}};export{z as Compose,ye as composeQuery,He as formatQuery,Ge as getComposedField,me as getField,Me as getFlattenedFields,M as hasAlias,he as isFieldSubquery,P as isGroupByField,Le as isGroupByFn,Te as isHavingClauseWithRightCondition,D as isNegationCondition,K as isOrderByField,Ie as isOrderByFn,de as isQueryValid,A as isString,m as isSubquery,Ce as isValueCondition,j as isValueFunctionCondition,q as isValueQueryCondition,_e as isValueWithDateLiteralCondition,Se as isValueWithDateNLiteralCondition,Re as isWhereClauseWithRightCondition,X as isWhereOrHavingClauseWithRightCondition,G as parseQuery};
23
+ `:e}};function Be(i,e,s){return me(k(i,s),{format:!0,formatOptions:e})}function me(i,e={}){if(!i)return"";e=e||{},e.format=!!e.format,e.logging&&(console.time("composer"),console.log("Composing Query:",i),console.log("Format output:",e.format));let s=new K(i,e).query;return e.logging&&console.timeEnd("composer"),s}var K=class{constructor(e,s={}){this.soql=e;this.logging=!1;this.format=!1;s={autoCompose:!0,...s};let{logging:t,format:n}=s;this.logging=!!t,this.format=!!n,this.query="",this.formatter=new P(this.format,{logging:this.logging,...s.formatOptions}),s.autoCompose&&this.start()}start(){this.query=this.parseQuery(this.soql)}log(e){this.logging&&console.log("Current SOQL:",e)}parseFn(e){let s;return e.rawValue?s=e.rawValue:(s=e.functionName,s+=`(${(e.parameters||[]).map(t=>A(t)?t:this.parseFn(t)).join(", ")})`),e.alias&&(s+=` ${e.alias}`),s}parseQuery(e){let s={fields:this.parseFields(e.fields||[]).map(a=>({text:a.text,typeOfClause:a.typeOfClause,isSubquery:a.text.startsWith("("),prefix:"",suffix:""})),isSubquery:m(e),lineBreaks:[]},t="";e.fields&&(t+=this.formatter.formatClause("SELECT").trimStart()),this.formatter.formatFields(s);let n="";if(s.fields.forEach(a=>{Array.isArray(a.typeOfClause)?n+=`${a.prefix}${this.formatter.formatTyeOfField(a.text,a.typeOfClause)}${a.suffix}`:n+=`${a.prefix}${a.text}${a.suffix}`}),t+=this.formatter.formatText(n),(m(e)?e.relationshipName:e.sObject)&&(t+=this.formatter.formatClause("FROM")),m(e)){let a=e.sObjectPrefix||[];a.push(e.relationshipName),t+=this.formatter.formatText(`${a.join(".")}${O(e.sObjectAlias,""," ")}`)}else e.sObject&&(t+=this.formatter.formatText(`${e.sObject}${O(e.sObjectAlias,""," ")}`));return this.log(t),e.usingScope&&(t+=this.formatter.formatClause("USING SCOPE"),t+=this.formatter.formatText(e.usingScope),this.log(t)),e.where&&(t+=this.formatter.formatClause("WHERE"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.where)),this.log(t)),e.groupBy&&(t+=this.formatter.formatClause("GROUP BY"),t+=this.formatter.formatText(this.parseGroupByClause(e.groupBy)),this.log(t),e.having&&(t+=this.formatter.formatClause("HAVING"),t+=this.formatter.formatText(this.parseWhereOrHavingClause(e.having)),this.log(t))),e.orderBy&&(!Array.isArray(e.orderBy)||e.orderBy.length>0)&&(t+=this.formatter.formatClause("ORDER BY"),t+=this.formatter.formatText(this.parseOrderBy(e.orderBy)),this.log(t)),L(e.limit)&&(t+=this.formatter.formatClause("LIMIT"),t+=this.formatter.formatText(`${e.limit}`),this.log(t)),L(e.offset)&&(t+=this.formatter.formatClause("OFFSET"),t+=this.formatter.formatText(`${e.offset}`),this.log(t)),e.withDataCategory&&(t+=this.formatter.formatClause("WITH DATA CATEGORY"),t+=this.formatter.formatText(this.parseWithDataCategory(e.withDataCategory)),this.log(t)),e.withSecurityEnforced&&(t+=this.formatter.formatClause("WITH SECURITY_ENFORCED"),this.log(t)),e.withAccessLevel&&(t+=this.formatter.formatClause(`WITH ${e.withAccessLevel}`),this.log(t)),e.for&&(t+=this.formatter.formatClause("FOR"),t+=this.formatter.formatText(e.for),this.log(t)),e.update&&(t+=this.formatter.formatClause("UPDATE"),t+=this.formatter.formatText(e.update),this.log(t)),t.trim()}parseFields(e){return e.map(s=>{let t="",n,a=s.objectPrefix?`${s.objectPrefix}.`:"";switch(s.type){case"Field":{t=`${a}${s.field}${s.alias?` ${s.alias}`:""}`;break}case"FieldFunctionExpression":{let o="";s.parameters&&(o=s.parameters.map(h=>A(h)?h:this.parseFields([h]).map(l=>l.text)).join(", ")),t=`${s.functionName}(${o})${s.alias?` ${s.alias}`:""}`;break}case"FieldRelationship":{t=`${a}${s.relationships.join(".")}.${s.field}${G(s)?` ${s.alias}`:""}`;break}case"FieldSubquery":{t=this.formatter.formatSubquery(this.parseQuery(s.subquery));break}case"FieldTypeof":{n=this.parseTypeOfField(s),t=n.join(" ");break}default:break}return{text:t,typeOfClause:n}})}parseTypeOfField(e){let s=[`TYPEOF ${e.field}`].concat(e.conditions.map(t=>this.formatter.formatTypeofFieldCondition(t)));return s.push("END"),s}parseWhereOrHavingClause(e,s=0,t=!1){let n="",a=e.left,o=!1;if(a&&(n+=this.formatter.formatParens(a.openParen,"(",x(a)),!x(a))){s=s+(a.openParen||0)-(a.closeParen||0),t&&s++;let h="";h+=X(a)?this.parseFn(a.fn):a.field,h+=` ${a.operator} `,j(a)?h+=this.formatter.formatSubquery(this.parseQuery(a.valueQuery),1,!0):h+=ie(he(a.value,a.literalType,a.operator)),n+=this.formatter.formatWithIndent(h),n+=this.formatter.formatParens(a.closeParen,")",t)}if(Q(e)){let h=O(e.operator);o=h==="NOT";let l=this.formatter.formatWhereClauseOperators(h,this.parseWhereOrHavingClause(e.right,s,x(a)),s);return`${o?n.trimRight():n}${l}`.trim()}else return n.trim()}parseGroupByClause(e){return(Array.isArray(e)?e:[e]).map(s=>M(s)?s.field:this.parseFn(s.fn)).join(", ")}parseOrderBy(e){if(Array.isArray(e))return this.formatter.formatOrderByArray(e.map(s=>this.parseOrderBy(s)));{let s="";return q(e)?s=`${O(e.field," ")}`:s+=`${this.parseFn(e.fn)} `,s+=`${O(e.order," ")}${O(e.nulls,"","NULLS ")}`,s.trim()}}parseWithDataCategory(e){return e.conditions.map(s=>{let t=s.parameters.length>1?`(${s.parameters.join(", ")})`:`${s.parameters.join(", ")}`;return`${s.groupName} ${s.selector} ${t}`}).join(" AND ")}};export{K as Compose,me as composeQuery,Be as formatQuery,ke as getComposedField,ge as getField,Ge as getFlattenedFields,G as hasAlias,Ee as isFieldSubquery,M as isGroupByField,Ie as isGroupByFn,Re as isHavingClauseWithRightCondition,x as isNegationCondition,q as isOrderByField,Se as isOrderByFn,Ae as isQueryValid,A as isString,m as isSubquery,Te as isValueCondition,X as isValueFunctionCondition,j as isValueQueryCondition,Ce as isValueWithDateLiteralCondition,_e as isValueWithDateNLiteralCondition,Ne as isWhereClauseWithRightCondition,Q as isWhereOrHavingClauseWithRightCondition,k as parseQuery};
package/dist/lwc/index.js CHANGED
@@ -378,7 +378,6 @@ var _tokenTypeNames = [];
378
378
  _tokenTypeNames[129 /* TEAM */] = "Team";
379
379
  _tokenTypeNames[131 /* MRU */] = "Mru";
380
380
  _tokenTypeNames[159 /* IDENTIFIER */] = "Identifier";
381
- _tokenTypeNames[142 /* DOT */] = "DECIMAL";
382
381
  _tokenTypeNames[132 /* EQUAL */] = "EQUAL";
383
382
  _tokenTypeNames[156 /* UNSIGNED_INTEGER */] = "UNSIGNED_INTEGER";
384
383
  _tokenTypeNames[155 /* SIGNED_INTEGER */] = "SIGNED_INTEGER";
@@ -841,14 +840,9 @@ var CLAUSE_KEYWORDS = /* @__PURE__ */ new Set([
841
840
  160 /* EOF */
842
841
  ]);
843
842
  var SoqlParser = class _SoqlParser {
844
- constructor() {
845
- this.tokens = [];
843
+ constructor(tokens, config) {
846
844
  this.pos = 0;
847
- this.config = {};
848
- }
849
- init(tokens, config) {
850
845
  this.tokens = tokens;
851
- this.pos = 0;
852
846
  this.config = config;
853
847
  }
854
848
  getPos() {
@@ -1164,7 +1158,6 @@ var SoqlParser = class _SoqlParser {
1164
1158
  const query = {};
1165
1159
  const tryParse = (fn, cleanup) => {
1166
1160
  if (this.config.ignoreParseErrors) {
1167
- const savedPos = this.pos;
1168
1161
  try {
1169
1162
  fn();
1170
1163
  if (!this.isAtClauseBoundary()) {
@@ -1176,7 +1169,6 @@ var SoqlParser = class _SoqlParser {
1176
1169
  console.log(e);
1177
1170
  }
1178
1171
  if (cleanup) cleanup();
1179
- this.pos = savedPos;
1180
1172
  this.synchronize();
1181
1173
  }
1182
1174
  } else {
@@ -1621,7 +1613,7 @@ var SoqlParser = class _SoqlParser {
1621
1613
  }
1622
1614
  if (t.kind === 18 /* NULL */) {
1623
1615
  this.advance();
1624
- return { value: "null", type: "NULL" };
1616
+ return { value: "NULL", type: "NULL" };
1625
1617
  }
1626
1618
  if (t.kind === 24 /* TRUE */) {
1627
1619
  return { value: this.advance().text, type: "TRUE" };
@@ -2045,11 +2037,10 @@ var SoqlParser = class _SoqlParser {
2045
2037
  return { groupName, selector, parameters };
2046
2038
  }
2047
2039
  };
2048
- var parser = new SoqlParser();
2049
2040
  function parseQuery(soql, options) {
2050
2041
  const config = options || {};
2051
2042
  const tokens = tokenize(soql);
2052
- parser.init(tokens, config);
2043
+ const parser = new SoqlParser(tokens, config);
2053
2044
  let query;
2054
2045
  if (config.allowPartialQuery) {
2055
2046
  query = parser.parseSelectStatementPartial();
@@ -2231,6 +2222,9 @@ function whereValueHelper(value, literalType) {
2231
2222
  case "STRING": {
2232
2223
  return isString(value) && value.startsWith("'") ? value : `'${value ?? ""}'`;
2233
2224
  }
2225
+ case "NULL": {
2226
+ return "null";
2227
+ }
2234
2228
  default: {
2235
2229
  return value;
2236
2230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetstreamapp/soql-parser-js",
3
- "version": "7.0.0",
3
+ "version": "7.1.1",
4
4
  "description": "Salesforce.com SOQL parser and composer",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.mjs",
@@ -51,7 +51,7 @@
51
51
  "lodash.get": "^4.4.2",
52
52
  "npm-run-all": "^4.1.5",
53
53
  "prettier": "^3.8.1",
54
- "release-it": "^19.2.4",
54
+ "release-it": "^20.0.1",
55
55
  "typescript": "^5.9.3",
56
56
  "vitest": "^4.1.0"
57
57
  },