rawsql-ts 0.11.38-beta → 0.11.39-beta

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.
@@ -1,10 +1,11 @@
1
1
  var v=class{constructor(){this.comments=null;this.positionedComments=null}getKind(){return this.constructor.kind}accept(e){return e.visit(this)}toSqlString(e){return this.accept(e)}addPositionedComments(e,t){if(!t||t.length===0)return;this.positionedComments||(this.positionedComments=[]);let n=this.positionedComments.find(i=>i.position===e);n?n.comments.push(...t):this.positionedComments.push({position:e,comments:[...t]})}getPositionedComments(e){if(!this.positionedComments)return[];let t=this.positionedComments.find(n=>n.position===e);return t?t.comments:[]}getAllPositionedComments(){if(!this.positionedComments)return[];let e=[],t=this.getPositionedComments("before");e.push(...t);let n=this.getPositionedComments("after");return e.push(...n),e}},Ti=class{constructor(){this.parameterSymbol=":";this.identifierEscape={start:'"',end:'"'};this.exportComment=!0}};var ot=class extends v{static{this.kind=Symbol("InsertQuery")}constructor(e){super(),this.insertClause=e.insertClause,this.selectQuery=e.selectQuery??null}};var ge=class extends v{static{this.kind=Symbol("InlineQuery")}constructor(e){super(),this.selectQuery=e}},D=class extends v{static{this.kind=Symbol("ValueList")}constructor(e){super(),this.values=e}},b=class extends v{get namespaces(){return this.qualifiedName.namespaces}get column(){return this.qualifiedName.name instanceof I?this.qualifiedName.name:new I(this.qualifiedName.name.value)}static{this.kind=Symbol("ColumnReference")}constructor(e,t){super();let n=typeof t=="string"?new I(t):t;this.qualifiedName=new De(Pi(e),n)}toString(){return this.qualifiedName.toString()}getNamespace(){return this.qualifiedName.namespaces?this.qualifiedName.namespaces.map(e=>e.name).join("."):""}},U=class extends v{static{this.kind=Symbol("FunctionCall")}constructor(e,t,n,i,s=null,r=!1,o=null){super(),this.qualifiedName=new De(e,t),this.argument=n,this.over=i,this.withinGroup=s,this.withOrdinality=r,this.internalOrderBy=o}get namespaces(){return this.qualifiedName.namespaces}get name(){return this.qualifiedName.name}},ki=(n=>(n.Rows="rows",n.Range="range",n.Groups="groups",n))(ki||{}),Ii=(n=>(n.UnboundedPreceding="unbounded preceding",n.UnboundedFollowing="unbounded following",n.CurrentRow="current row",n))(Ii||{}),Et=class extends v{static{this.kind=Symbol("WindowFrameStaticBound")}constructor(e){super(),this.bound=e}},xt=class extends v{static{this.kind=Symbol("WindowFrameBoundary")}constructor(e,t){super(),this.value=e,this.isFollowing=t}},He=class extends v{static{this.kind=Symbol("WindowFrameSpec")}constructor(e,t,n){super(),this.frameType=e,this.startBound=t,this.endBound=n}},Ve=class extends v{static{this.kind=Symbol("WindowFrameExpression")}constructor(e,t,n=null){super(),this.partition=e,this.order=t,this.frameSpec=n}},ne=class extends v{static{this.kind=Symbol("UnaryExpression")}constructor(e,t){super(),this.operator=new V(e),this.expression=t}},S=class extends v{static{this.kind=Symbol("BinaryExpression")}constructor(e,t,n){super(),this.left=e,this.operator=new V(t),this.right=n}},Y=class extends v{static{this.kind=Symbol("LiteralExpression")}constructor(e,t,n){super(),this.value=e,this.isStringLiteral=n}},L=class extends v{static{this.kind=Symbol("ParameterExpression")}constructor(e,t=null){super(),this.name=new V(e),this.value=t,this.index=null}},Ee=class extends v{static{this.kind=Symbol("SwitchCaseArgument")}constructor(e,t=null){super(),this.cases=e,this.elseValue=t}},xe=class extends v{static{this.kind=Symbol("CaseKeyValuePair")}constructor(e,t){super(),this.key=e,this.value=t}},V=class extends v{static{this.kind=Symbol("RawString")}constructor(e){super(),this.value=e}},I=class extends v{static{this.kind=Symbol("IdentifierString")}constructor(e){super(),this.name=e}},Z=class extends v{static{this.kind=Symbol("ParenExpression")}constructor(e){super(),this.expression=e}},ce=class extends v{static{this.kind=Symbol("CastExpression")}constructor(e,t){super(),this.input=e,this.castType=t}},se=class extends v{static{this.kind=Symbol("CaseExpression")}constructor(e,t){super(),this.condition=e,this.switchCase=t}},ye=class extends v{static{this.kind=Symbol("ArrayExpression")}constructor(e){super(),this.expression=e}},ve=class extends v{static{this.kind=Symbol("ArrayQueryExpression")}constructor(e){super(),this.query=e}},we=class extends v{static{this.kind=Symbol("BetweenExpression")}constructor(e,t,n,i){super(),this.expression=e,this.lower=t,this.upper=n,this.negated=i}},je=class extends v{static{this.kind=Symbol("StringSpecifierExpression")}constructor(e,t){super(),this.specifier=new V(e),this.value=new Y(t)}},Re=class extends v{static{this.kind=Symbol("TypeValue")}constructor(e,t,n=null){super(),this.qualifiedName=new De(e,t),this.argument=n}get namespaces(){return this.qualifiedName.namespaces}get name(){return this.qualifiedName.name}getTypeName(){let e=this.qualifiedName.name instanceof V?this.qualifiedName.name.value:this.qualifiedName.name.name;return this.qualifiedName.namespaces&&this.qualifiedName.namespaces.length>0?this.qualifiedName.namespaces.map(t=>t.name).join(".")+"."+e:e}},Ie=class extends v{static{this.kind=Symbol("TupleExpression")}constructor(e){super(),this.values=e}},et=class extends v{static{this.kind=Symbol("ArraySliceExpression")}constructor(e,t,n){super(),this.array=e,this.startIndex=t,this.endIndex=n}},tt=class extends v{static{this.kind=Symbol("ArrayIndexExpression")}constructor(e,t){super(),this.array=e,this.index=t}};function Pi(a){if(a==null)return null;if(typeof a=="string")return a.trim()===""?null:[new I(a)];if(Array.isArray(a)){if(a.length===0)return null;if(typeof a[0]=="string"){let e=a.filter(t=>t.trim()!=="");return e.length===0?null:e.map(t=>new I(t))}else{let e=a.filter(t=>t.name.trim()!=="");return e.length===0?null:e}}return null}var De=class extends v{static{this.kind=Symbol("QualifiedName")}constructor(e,t){super(),this.namespaces=Pi(e),typeof t=="string"?this.name=new V(t):this.name=t}toString(){let e=this.name instanceof V?this.name.value:this.name.name;return this.namespaces&&this.namespaces.length>0?this.namespaces.map(t=>t.name).join(".")+"."+e:e}};var _=class extends v{static{this.kind=Symbol("SelectItem")}constructor(e,t=null){super(),this.value=e,this.identifier=t?new I(t):null}},q=class extends v{static{this.kind=Symbol("SelectClause")}constructor(e,t=null,n=[]){super(),this.items=e,this.distinct=t,this.hints=n}},Tt=class extends v{static{this.kind=Symbol("Distinct")}constructor(){super()}},kt=class extends v{static{this.kind=Symbol("DistinctOn")}constructor(e){super(),this.value=e}},re=class extends v{static{this.kind=Symbol("WhereClause")}constructor(e){super(),this.condition=e}},Ue=class extends v{static{this.kind=Symbol("PartitionByClause")}constructor(e){super(),this.value=e}},Pe=class extends v{static{this.kind=Symbol("WindowFrameClause")}constructor(e,t){super(),this.name=new I(e),this.expression=t}},nt=class extends v{static{this.kind=Symbol("WindowsClause")}constructor(e){super(),this.windows=e}};var pe=class extends v{static{this.kind=Symbol("OrderByClause")}constructor(e){super(),this.order=e}},Le=class extends v{static{this.kind=Symbol("OrderByItem")}constructor(e,t,n){super(),this.value=e,this.sortDirection=t===null?"asc":t,this.nullsPosition=n}},me=class extends v{static{this.kind=Symbol("GroupByClause")}constructor(e){super(),this.grouping=e}},de=class extends v{static{this.kind=Symbol("HavingClause")}constructor(e){super(),this.condition=e}},O=class extends v{static{this.kind=Symbol("TableSource")}get namespaces(){return this.qualifiedName.namespaces}get table(){return this.qualifiedName.name instanceof I?this.qualifiedName.name:new I(this.qualifiedName.name.value)}get identifier(){return this.table}constructor(e,t){super();let n=typeof t=="string"?new I(t):t;this.qualifiedName=new De(e,n)}getSourceName(){return this.qualifiedName.namespaces&&this.qualifiedName.namespaces.length>0?this.qualifiedName.namespaces.map(e=>e.name).join(".")+"."+(this.qualifiedName.name instanceof V?this.qualifiedName.name.value:this.qualifiedName.name.name):this.qualifiedName.name instanceof V?this.qualifiedName.name.value:this.qualifiedName.name.name}},ze=class extends v{static{this.kind=Symbol("FunctionSource")}constructor(e,t){if(super(),typeof e=="object"&&e!==null&&"name"in e){let n=e;this.qualifiedName=new De(n.namespaces,n.name)}else this.qualifiedName=new De(null,e);this.argument=t}get namespaces(){return this.qualifiedName.namespaces}get name(){return this.qualifiedName.name}},$e=class extends v{static{this.kind=Symbol("ParenSource")}constructor(e){super(),this.source=e}},M=class extends v{static{this.kind=Symbol("SubQuerySource")}constructor(e){super(),this.query=e}},B=class extends v{static{this.kind=Symbol("SourceExpression")}constructor(e,t){super(),this.datasource=e,this.aliasExpression=t}getAliasName(){return this.aliasExpression?this.aliasExpression.table.name:this.datasource instanceof O?this.datasource.getSourceName():null}},he=class extends v{static{this.kind=Symbol("JoinOnClause")}constructor(e){super(),this.condition=e}},Se=class extends v{static{this.kind=Symbol("JoinUsingClause")}constructor(e){super(),this.condition=e}},fe=class extends v{static{this.kind=Symbol("JoinItem")}constructor(e,t,n,i){super(),this.joinType=new V(e),this.source=t,this.condition=n,this.lateral=i}getSourceAliasName(){return this.source.aliasExpression?this.source.aliasExpression.table.name:this.source instanceof O?this.source.table.name:null}},K=class extends v{static{this.kind=Symbol("FromClause")}constructor(e,t){super(),this.source=e,this.joins=t}getSourceAliasName(){return this.source.aliasExpression?this.source.aliasExpression.table.name:this.source.datasource instanceof O?this.source.datasource.table.name:null}getSources(){let e=[this.source];if(this.joins)for(let t of this.joins)e.push(t.source);return e}},ie=class extends v{static{this.kind=Symbol("CommonTable")}constructor(e,t,n){super(),this.query=e,this.materialized=n,typeof t=="string"?this.aliasExpression=new ee(t,null):this.aliasExpression=t}getSourceAliasName(){return this.aliasExpression.table.name}},ae=class extends v{constructor(t,n){super();this.trailingComments=null;this.globalComments=null;this.recursive=t,this.tables=n}static{this.kind=Symbol("WithClause")}},oe=class extends v{static{this.kind=Symbol("LimitClause")}constructor(e){super(),this.value=e}};var Fe=class extends v{static{this.kind=Symbol("OffsetClause")}constructor(e){super(),this.value=e}},Ke=class extends v{static{this.kind=Symbol("FetchClause")}constructor(e){super(),this.expression=e}},gt=class extends v{static{this.kind=Symbol("FetchExpression")}constructor(e,t,n){super(),this.type=e,this.count=t,this.unit=n}};var Ae=class extends v{static{this.kind=Symbol("ForClause")}constructor(e){super(),this.lockMode=e}},ee=class extends v{static{this.kind=Symbol("SourceAliasExpression")}constructor(e,t){super(),this.table=new I(e),this.columns=t!==null?t.map(n=>new I(n)):null}},ln=class extends v{static{this.kind=Symbol("ReturningClause")}constructor(e){super(),this.columns=e.map(t=>typeof t=="string"?new I(t):t)}},at=class extends v{static{this.kind=Symbol("SetClause")}constructor(e){super(),this.items=e.map(t=>t instanceof ut?t:new ut(t.column,t.value))}},ut=class extends v{static{this.kind=Symbol("SetClauseItem")}constructor(e,t){if(super(),typeof e=="object"&&e!==null&&"column"in e){let n=e,i=typeof n.column=="string"?new I(n.column):n.column;this.qualifiedName=new De(n.namespaces,i)}else{let n=typeof e=="string"?new I(e):e;this.qualifiedName=new De(null,n)}this.value=t}get namespaces(){return this.qualifiedName.namespaces}get column(){return this.qualifiedName.name instanceof I?this.qualifiedName.name:new I(this.qualifiedName.name.value)}getFullName(){return this.qualifiedName.toString()}},It=class extends v{static{this.kind=Symbol("UpdateClause")}constructor(e){super(),this.source=e}getSourceAliasName(){return this.source.aliasExpression?this.source.aliasExpression.table.name:this.source.datasource instanceof O?this.source.datasource.table.name:null}},ct=class extends v{constructor(e,t){super(),this.source=e,this.columns=t.map(n=>new I(n))}};var $=(f=>(f[f.None=0]="None",f[f.Literal=1]="Literal",f[f.Operator=2]="Operator",f[f.OpenParen=4]="OpenParen",f[f.CloseParen=8]="CloseParen",f[f.Comma=16]="Comma",f[f.Dot=32]="Dot",f[f.Identifier=64]="Identifier",f[f.Command=128]="Command",f[f.Parameter=256]="Parameter",f[f.OpenBracket=512]="OpenBracket",f[f.CloseBracket=1024]="CloseBracket",f[f.Function=2048]="Function",f[f.StringSpecifier=4096]="StringSpecifier",f[f.Type=8192]="Type",f))($||{});var te=class{static isWhitespace(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t===32||t===9||t===10||t===13}static isDigit(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t>=48&&t<=57}static isHexChar(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t>=48&&t<=57||t>=97&&t<=102||t>=65&&t<=70}static isOperatorSymbol(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t===43||t===45||t===42||t===47||t===37||t===126||t===64||t===35||t===94||t===38||t===58||t===33||t===60||t===62||t===61||t===124||t===63}static isDelimiter(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t===46||t===44||t===40||t===41||t===91||t===93||t===123||t===125||t===59||t===32||t===9||t===10||t===13?!0:t===43||t===45||t===42||t===47||t===37||t===126||t===64||t===35||t===94||t===38||t===58||t===33||t===60||t===62||t===61||t===124||t===63}static isNamedParameterPrefix(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t===64||t===58||t===36}};var le=class a{static getDebugPositionInfo(e,t){let n=Math.max(0,t-5),i=Math.min(e.length,t+5),s=e.slice(n,i),r=" ".repeat(t-n)+"^";return`${s}
2
- ${r}`}static skipWhiteSpace(e,t){let n=e.length;for(;t+4<=n&&e.slice(t,t+4)===" ";)t+=4;for(;t<n;){let i=e.charCodeAt(t);if(i!==32&&i!==9&&i!==10&&i!==13)break;t++}return t}static readLineComment(e,t){if(t+1>=e.length)return{newPosition:t,comment:null};if(e.charCodeAt(t)===45&&e.charCodeAt(t+1)===45){let n=t;for(t+=2;t<e.length&&e.charCodeAt(t)!==10;)t++;let i=e.slice(n+2,t).trim();return{newPosition:t,comment:i}}return{newPosition:t,comment:null}}static readBlockComment(e,t){if(t+3>=e.length)return{newPosition:t,comments:null};if(e.charCodeAt(t)===47&&e.charCodeAt(t+1)===42&&e.charCodeAt(t+2)!==43){let n=t;for(t+=2;t+1<e.length;){if(e.charCodeAt(t)===42&&e.charCodeAt(t+1)===47){t+=2;let i=e.slice(n+2,t-2).replace(/\r/g,"").split(`
3
- `),s=[];for(let r of i){let o=r.trim(),l=/^\s*[-=_+*#]+\s*$/.test(r);o!==""||l?s.push(l?r.trim():o):s.push("")}for(;s.length>0&&s[0]==="";)s.shift();for(;s.length>0&&s[s.length-1]==="";)s.pop();return{newPosition:t,comments:s}}t++}throw new Error(`Block comment is not closed. position: ${t}`)}return{newPosition:t,comments:null}}static readWhiteSpaceAndComment(e,t){let n=[],i=e.length;for(;t<i;){let s=t;if(t=a.skipWhiteSpace(e,t),t!==s)continue;let r=e.charCodeAt(t);if(r===45){let o=a.readLineComment(e,t);if(o.newPosition!==t){t=o.newPosition,o.comment&&n.push(o.comment.trim());continue}}else if(r===47){let o=a.readBlockComment(e,t);if(o.newPosition!==t){t=o.newPosition,o.comments&&n.push(...o.comments);continue}}break}return{position:t,lines:n}}static readRegularIdentifier(e,t){let n=this.tryReadRegularIdentifier(e,t);if(!n)throw new Error(`Unexpected character. position: ${t}
2
+ ${r}`}static skipWhiteSpace(e,t){let n=e.length;for(;t+4<=n&&e.slice(t,t+4)===" ";)t+=4;for(;t<n;){let i=e.charCodeAt(t);if(i!==32&&i!==9&&i!==10&&i!==13)break;t++}return t}static readLineComment(e,t){if(t+1>=e.length)return{newPosition:t,comment:null};if(e.charCodeAt(t)===45&&e.charCodeAt(t+1)===45){let n=t;for(t+=2;t<e.length&&e.charCodeAt(t)!==10;)t++;let i=e.slice(n+2,t).trim();return{newPosition:t,comment:i}}return{newPosition:t,comment:null}}static readBlockComment(e,t){if(t+1>=e.length)return{newPosition:t,comments:null};if(e.charCodeAt(t)!==47||e.charCodeAt(t+1)!==42)return{newPosition:t,comments:null};if(t+2<e.length&&e.charCodeAt(t+2)===43)return{newPosition:t,comments:null};let i=t;for(t+=2;t+1<e.length;){if(e.charCodeAt(t)===42&&e.charCodeAt(t+1)===47){t+=2;let r=this.processBlockCommentContent(e.slice(i+2,t-2));return{newPosition:t,comments:r}}t++}let s=this.processBlockCommentContent(e.slice(i+2));return{newPosition:e.length,comments:s}}static processBlockCommentContent(e){let t=e.replace(/\r/g,"").split(`
3
+ `),n=[];for(let i of t){let s=i.trim(),r=/^\s*[-=_+*#]+\s*$/.test(i);s!==""||r?n.push(r?i.trim():s):n.push("")}for(;n.length>0&&n[0]==="";)n.shift();for(;n.length>0&&n[n.length-1]==="";)n.pop();return n}static readWhiteSpaceAndComment(e,t){let n=[],i=e.length;for(;t<i;){let s=t;if(t=a.skipWhiteSpace(e,t),t!==s)continue;let r=e.charCodeAt(t);if(r===45){let o=a.readLineComment(e,t);if(o.newPosition!==t){t=o.newPosition,o.comment&&n.push(o.comment.trim());continue}}else if(r===47){let o=a.readBlockComment(e,t);if(o.newPosition!==t){t=o.newPosition,o.comments&&n.push(...o.comments);continue}}break}return{position:t,lines:n}}static readRegularIdentifier(e,t){let n=this.tryReadRegularIdentifier(e,t);if(!n)throw new Error(`Unexpected character. position: ${t}
4
4
  ${a.getDebugPositionInfo(e,t)}`);return n}static tryReadRegularIdentifier(e,t){let n=t;for(;t<e.length&&!te.isDelimiter(e[t]);)t++;if(n===t)return null;for(;t+1<e.length&&e[t]==="["&&e[t+1]==="]";){let i=e.slice(0,n).trim();if(i===""||/[)]$/.test(i)||/\b(select|from|where|and|or|set|values|insert|update|delete)\s*$/i.test(i))break;t+=2}return{identifier:e.slice(n,t),newPosition:t}}};var ue=class{constructor(e,t=0){this.input=e,this.position=t}getPosition(){return this.position}setPosition(e){this.position=e}isEndOfInput(e=0){return this.position+e>=this.input.length}canRead(e=0){return!this.isEndOfInput(e)}read(e){if(this.isEndOfInput())throw new Error(`Unexpected character. expect: ${e}, actual: EndOfInput, position: ${this.position}`);let t=this.input[this.position];if(t!==e)throw new Error(`Unexpected character. expect: ${e}, actual: ${t}, position: ${this.position}`);return this.position++,t}createLexeme(e,t,n=null,i,s){let r={type:e,value:e===128||e===2||e===2048?t.toLowerCase():t,comments:n};return i!==void 0&&s!==void 0&&(r.position={startPosition:i,endPosition:s}),r}createLexemeWithPosition(e,t,n,i=null){return this.createLexeme(e,t,i,n,n+t.length)}getDebugPositionInfo(e){return le.getDebugPositionInfo(this.input,e)}};var un=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];if(t==="*")return this.position++,this.createLexeme(64,t);let n=le.readRegularIdentifier(this.input,this.position);return this.position=n.newPosition,this.createLexeme(64,n.identifier)}};var Te=class{constructor(e){this.trie=e}isEndOfInput(e,t,n=0){return t+n>=e.length}canParse(e,t,n=0){return!this.isEndOfInput(e,t,n)}parse(e,t){if(this.isEndOfInput(e,t))return null;this.trie.reset();let n=le.tryReadRegularIdentifier(e,t);if(n===null)return null;let i=this.trie.pushLexeme(n.identifier.toLowerCase());if(i===0)return null;if(i===3)return{keyword:n.identifier,newPosition:n.newPosition};let s=n.identifier,r=le.readWhiteSpaceAndComment(e,n.newPosition);t=r.position;let o=[...r.lines];if(this.isEndOfInput(e,t))return i===2?{keyword:s,newPosition:t,comments:o.length>0?o:void 0}:null;for(;this.canParse(e,t);){let l=i,u=le.tryReadRegularIdentifier(e,t);if(u!==null){if(i=this.trie.pushLexeme(u.identifier.toLowerCase()),i===0){if(l===2)break;return null}s+=" "+u.identifier;let c=le.readWhiteSpaceAndComment(e,u.newPosition);if(t=c.position,o.push(...c.lines),i===3)break}else{if(l===2)break;return null}}return{keyword:s,newPosition:t,comments:o.length>0?o:void 0}}};var Je=class{constructor(e){this.root=new Map;this.hasEndProperty=!1;this.hasMoreProperties=!1;for(let t of e)this.addKeyword(t);this.currentNode=this.root}addKeyword(e){let t=this.root;for(let n of e)t.has(n)||t.set(n,new Map),t=t.get(n);t.set("__end__",!0)}reset(){this.currentNode=this.root,this.hasEndProperty=!1,this.hasMoreProperties=!1}pushLexeme(e){return this.currentNode.has(e)?(this.currentNode=this.currentNode.get(e),this.hasEndProperty=this.currentNode.has("__end__"),this.hasMoreProperties=this.currentNode.size>(this.hasEndProperty?1:0),this.hasEndProperty&&!this.hasMoreProperties?3:this.hasEndProperty&&this.hasMoreProperties?2:1):0}};var zi=[["null"],["true"],["false"],["current_date"],["current_time"],["current_timestamp"],["localtime"],["localtimestamp"],["unbounded"],["normalized"],["nfc","normalized"],["nfd","normalized"],["nfkc","normalized"],["nfkd","normalized"],["nfc"],["nfd"],["nfkc"],["nfkd"]],Hi=new Je(zi),fi=new Te(Hi),cn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];if(t==="'"){let i=this.readSingleQuotedString();return this.createLexeme(1,i)}let n=this.tryReadKeyword();if(n)return n;if(t==="."&&this.canRead(1)&&te.isDigit(this.input[this.position+1]))return this.createLexeme(1,this.readDigit());if(te.isDigit(t))return this.createLexeme(1,this.readDigit());if(t==="$"&&this.isDollarQuotedString())return this.createLexeme(1,this.readDollarQuotedString());if(t==="$"&&this.canRead(1)&&te.isDigit(this.input[this.position+1])){let i=this.position+1,s=!1;for(;i<this.input.length&&(te.isDigit(this.input[i])||this.input[i]===","||this.input[i]===".");){if(this.input[i]==="."||this.input[i]===","){s=!0;break}i++}if(s){this.position++;let r=this.readMoneyDigit();return this.createLexeme(1,"$"+r)}}if((t==="+"||t==="-")&&this.determineSignOrOperator(e)==="sign"){let i=t;this.position++;let s=this.position;for(;this.canRead()&&te.isWhitespace(this.input[this.position]);)this.position++;if(this.canRead()&&(te.isDigit(this.input[this.position])||this.input[this.position]==="."&&this.canRead(1)&&te.isDigit(this.input[this.position+1])))return this.createLexeme(1,i==="-"?i+this.readDigit():this.readDigit());this.position=s-1}return null}tryReadKeyword(){let e=fi.parse(this.input,this.position);return e?(this.position=e.newPosition,this.createLexeme(1,e.keyword)):null}determineSignOrOperator(e){return e===null?"sign":e.type&1||e.type&64||e.type&256||e.type&8?"operator":"sign"}readDigit(){let e=this.position,t=!1,n=!1;if(this.canRead(1)&&this.input[this.position]==="0"&&"xbo".includes(this.input[this.position+1].toLowerCase())){let i=this.input[this.position+1].toLowerCase();this.position+=2;let s=i==="x";for(;this.canRead();){let r=this.input[this.position];if(te.isDigit(r)||s&&te.isHexChar(r))this.position++;else break}return this.input.slice(e,this.position)}for(this.input[e]==="."&&(t=!0,this.position++);this.canRead();){let i=this.input[this.position];if(i==="."&&!t)t=!0;else if((i==="e"||i==="E")&&!n)n=!0,this.canRead(1)&&(this.input[this.position+1]==="+"||this.input[this.position+1]==="-")&&this.position++;else if(!te.isDigit(i))break;this.position++}if(e===this.position)throw new Error(`Unexpected character. position: ${e}
5
5
  ${this.getDebugPositionInfo(e)}`);return this.input[e]==="."?"0"+this.input.slice(e,this.position):this.input.slice(e,this.position)}readMoneyDigit(){let e=this.position,t=!1;for(;this.canRead();){let n=this.input[this.position];if(n==="."&&!t)t=!0;else if(!(n===","&&!t)){if(!te.isDigit(n))break}this.position++}if(e===this.position)throw new Error(`Unexpected character. position: ${e}
6
6
  ${this.getDebugPositionInfo(e)}`);return this.input.slice(e,this.position)}readSingleQuotedString(){let e=this.position,t=!1;for(this.read("'");this.canRead();){let n=this.input[this.position];if(this.position++,n==="\\"&&this.canRead(1)){this.position++;continue}else if(n==="'"){t=!0;break}}if(t===!1)throw new Error(`Single quote is not closed. position: ${e}
7
- ${this.getDebugPositionInfo(e)}`);return this.input.slice(e,this.position)}isDollarQuotedString(){if(!this.canRead(1))return!1;if(this.input[this.position+1]==="$")return!0;let e=this.position+1;for(;e<this.input.length;){let t=this.input[e];if(t==="$")return!0;if(!this.isAlphanumeric(t)&&t!=="_")return!1;e++}return!1}readDollarQuotedString(){let e=this.position;this.position++;let t="";for(;this.canRead()&&this.input[this.position]!=="$";)t+=this.input[this.position],this.position++;if(!this.canRead())throw new Error(`Unexpected end of input while reading dollar-quoted string tag at position ${e}`);this.position++;let n="$"+t+"$",i=n,s="";for(;this.canRead();){if(this.input.substring(this.position,this.position+i.length)===i)return this.position+=i.length,n+s+i;s+=this.input[this.position],this.position++}throw new Error(`Unclosed dollar-quoted string starting at position ${e}. Expected closing tag: ${i}`)}isAlphanumeric(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122}};var pn=class extends ue{constructor(e){super(e)}tryRead(e){if(this.isEndOfInput())return null;if(this.canRead(1)&&this.input[this.position]==="$"&&this.input[this.position+1]==="{"){this.position+=2;let n=this.position;for(;this.canRead()&&this.input[this.position]!=="}";)this.position++;if(this.isEndOfInput())throw new Error(`Unexpected end of input. Expected closing '}' for parameter at position ${n}`);let i=this.input.slice(n,this.position);if(i.length===0)throw new Error("Empty parameter name is not allowed: found ${} at position "+(n-2));return this.position++,this.createLexeme(256,"${"+i+"}")}let t=this.input[this.position];if(te.isNamedParameterPrefix(t)){if(this.canRead(1)&&te.isOperatorSymbol(this.input[this.position+1])||t===":"&&this.isInArraySliceContext()||t==="$"&&this.isDollarQuotedString())return null;if(t==="$"&&this.canRead(1)&&te.isDigit(this.input[this.position+1])){let s=this.position+1,r=!1;for(;s<this.input.length&&(te.isDigit(this.input[s])||this.input[s]===","||this.input[s]===".");){if(this.input[s]==="."||this.input[s]===","){r=!0;break}s++}if(r)return null}this.position++;let n=this.position;for(;this.canRead()&&!te.isDelimiter(this.input[this.position]);)this.position++;let i=this.input.slice(n,this.position);return this.createLexeme(256,t+i)}if(t==="?"){if(this.canRead(1)){let n=this.input[this.position+1];if(n==="|"||n==="&")return null}return e&&(e.type&64||e.type&1)?null:(this.position++,this.createLexeme(256,t))}return null}isInArraySliceContext(){let e=this.position-1,t=0,n=0;for(;e>=0;){let i=this.input[e];if(i==="]")t++;else if(i==="["){if(t--,t<0){if(e>0){let s=this.input[e-1];if(/[a-zA-Z0-9_)\]]/.test(s))return!0}if(e===0)return!1;break}}else i===")"?n++:i==="("&&n--;e--}return!1}isDollarQuotedString(){if(!this.canRead(1))return!1;if(this.input[this.position+1]==="$")return!0;let e=this.position+1;for(;e<this.input.length;){let t=this.input[e];if(t==="$")return!0;if(!this.isAlphanumeric(t)&&t!=="_")return!1;e++}return!1}isAlphanumeric(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122}};var mn=class a extends ue{static{this.SPECIAL_SYMBOL_TOKENS={".":32,",":16,"(":4,")":8,"[":512,"]":1024}}tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];return t in a.SPECIAL_SYMBOL_TOKENS?(this.position++,this.createLexeme(a.SPECIAL_SYMBOL_TOKENS[t],t)):null}};var dn=class{constructor(e,t=0){this.cacheHits=0;this.cacheMisses=0;this.input=e,this.position=t,this.readers=[],this.tokenCache=new Map}register(e){return this.readers.push(e),this}registerAll(e){return e.forEach(t=>this.register(t)),this}setPosition(e){this.position=e;for(let t of this.readers)t.setPosition(e)}tryRead(e,t){if(this.tokenCache.has(e))return this.cacheHits++,this.tokenCache.get(e)||null;this.cacheMisses++,this.setPosition(e);let n=null;for(let i of this.readers)if(n=i.tryRead(t),n){this.position=i.getPosition();break}for(let i of this.readers)i.setPosition(this.position);return this.tokenCache.set(e,n),n}getMaxPosition(){let e=this.position;for(let t of this.readers){let n=t.getPosition();n>e&&(e=n)}return e}getInput(){return this.input}getCacheStats(){let e=this.cacheHits+this.cacheMisses,t=e>0?this.cacheHits/e:0;return{hits:this.cacheHits,misses:this.cacheMisses,ratio:t}}};var Gi=new Je([["and"],["or"],["is"],["is","not"],["is","distinct","from"],["is","not","distinct","from"],["like"],["ilike"],["in"],["exists"],["between"],["not","like"],["not","ilike"],["not","in"],["not","exists"],["not","between"],["escape"],["uescape"],["similar","to"],["not","similar","to"],["similar"],["placing"],["rlike"],["regexp"],["mod"],["xor"],["not"],["both"],["leading"],["trailing"],["both","from"],["leading","from"],["trailing","from"],["year","from"],["month","from"],["day","from"],["hour","from"],["minute","from"],["second","from"],["dow","from"],["doy","from"],["isodow","from"],["quarter","from"],["week","from"],["epoch","from"],["at","time","zone"]]),Yi=new Je([["date"],["time"],["timestamp"],["timestamptz"],["timetz"],["interval"],["boolean"],["integer"],["bigint"],["smallint"],["numeric"],["decimal"],["real"],["double","precision"],["double","precision"],["character","varying"],["time","without","time","zone"],["time","with","time","zone"],["timestamp","without","time","zone"],["timestamp","with","time","zone"]]),Zi=new Te(Gi),Xi=new Te(Yi);var hn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];if(te.isOperatorSymbol(t)){let i=this.position;for(;this.canRead()&&te.isOperatorSymbol(this.input[this.position]);){if(this.canRead(1)){let r=this.input[this.position];if(r==="-"&&this.input[this.position+1]==="-")break;if(r==="/"&&this.input[this.position+1]==="*")break}this.position++}let s=this.input.slice(i,this.position);return this.createLexeme(2,s)}let n=Xi.parse(this.input,this.position);return n!==null?(this.position=n.newPosition,this.createLexeme(8258,n.keyword)):(n=Zi.parse(this.input,this.position),n!==null?(this.position=n.newPosition,this.createLexeme(2,n.keyword)):null)}};var es=new Je([["join"],["inner","join"],["cross","join"],["left","join"],["left","outer","join"],["right","join"],["right","outer","join"],["full","join"],["full","outer","join"],["natural","join"],["natural","inner","join"],["natural","left","join"],["natural","left","outer","join"],["natural","right","join"],["natural","right","outer","join"],["natural","full","join"],["natural","full","outer","join"],["lateral","join"],["lateral","inner","join"],["lateral","left","join"],["lateral","left","outer","join"]]),Kt=new Je([["with"],["recursive"],["materialized"],["not","materialized"],["select"],["from"],["distinct"],["distinct","on"],["where"],["group","by"],["having"],["order","by"],["limit"],["offset"],["fetch"],["first"],["next"],["row"],["row","only"],["rows","only"],["percent"],["percent","with","ties"],["for"],["update"],["share"],["key","share"],["no","key","update"],["union"],["union","all"],["intersect"],["intersect","all"],["except"],["except","all"],["beteen"],["window"],["over"],["partition","by"],["range"],["rows"],["groups"],["within","group"],["with","ordinality"],["current","row"],["unbounded","preceding"],["unbounded","following"],["preceding"],["following"],["on"],["using"],["lateral"],["case"],["case","when"],["when"],["then"],["else"],["end"],["insert","into"],["update"],["delete","from"],["merge","into"],["matched"],["not","matched"],["update","set"],["do","nothing"],["values"],["set"],["returning"],["create","table"],["create","temporary","table"],["tablesample"],["as"],["asc"],["desc"],["nulls","first"],["nulls","last"]]),ts=new Te(Kt),Jt=new Te(es);var fn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=Jt.parse(this.input,this.position);if(t!==null)return this.position=t.newPosition,this.createLexeme(128,t.keyword);let n=ts.parse(this.input,this.position);if(n!==null){this.position=n.newPosition;let i=this.createLexeme(128,n.keyword,n.comments);return n.comments&&n.comments.length>0&&(i.positionedComments=[{position:"after",comments:n.comments}]),i}if(this.canRead(2)&&this.input[this.position]==="/"&&this.input[this.position+1]==="*"&&this.input[this.position+2]==="+"){this.position+=3;let i=this.position;for(;this.position+1<this.input.length;){if(this.input[this.position]==="*"&&this.input[this.position+1]==="/")return this.position+=2,this.createLexeme(128,"/*+ "+this.input.slice(i,this.position-2).trim()+" */");this.position++}throw new Error(`Block comment is not closed. position: ${this.position}`)}return null}};var ns=new Set(["e'","E'","x'","X'","b'","B'"]),is=new Set(["u&'","U&'"]),Cn=class extends ue{tryRead(e){let t=this.position;return this.canRead(1)&&ns.has(this.input.slice(t,t+2))?(this.position+=1,this.createLexeme(4096,this.input.slice(t,this.position))):this.canRead(2)&&is.has(this.input.slice(t,t+3))?(this.position+=2,this.createLexeme(4096,this.input.slice(t,this.position))):null}};var ss=new Je([["grouping","sets"],["array"]]),rs=new Te(ss),gn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=rs.parse(this.input,this.position);if(t!==null)return this.position=t.newPosition,this.createLexeme(2048,t.keyword);let n=le.tryReadRegularIdentifier(this.input,this.position);if(!n)return null;this.position=n.newPosition;var i=le.readWhiteSpaceAndComment(this.input,this.position).position-this.position;return this.canRead(i)&&this.input[this.position+i]==="("?this.createLexeme(2048,n.identifier):null}};var os=new Je([["double","precision"],["character","varying"],["time","without","time","zone"],["time","with","time","zone"],["timestamp","without","time","zone"],["timestamp","with","time","zone"]]),as=new Te(os),yn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=as.parse(this.input,this.position);if(t!==null)return this.position=t.newPosition,this.createLexeme(8192,t.keyword);if(e===null)return null;let n=le.tryReadRegularIdentifier(this.input,this.position);return n?(this.position=n.newPosition,e.type&128&&e.value==="as"?this.createLexeme(8256,n.identifier):e.type&2&&e.value==="::"?this.createLexeme(8192,n.identifier):null):null}};var vn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];if(t==="`"){let n=this.readEscapedIdentifier("`");return this.createLexeme(64,n)}if(t==='"'){let n=this.readEscapedIdentifier('"');return this.createLexeme(64,n)}if(t==="["&&this.isSqlServerBracketIdentifier(e)){let n=this.readEscapedIdentifier("]");return this.createLexeme(64,n)}return null}isSqlServerBracketIdentifier(e){if(e?.value==="array")return!1;let t=this.position+1,n=t;for(;n<this.input.length&&this.input[n]!=="]";){let s=this.input[n];if(s===":"||s===","||s==="+"||s==="-"||s==="*"||s==="/"||s==="("||s===")")return!1;n++}if(n>=this.input.length)return!1;let i=this.input.slice(t,n).trim();return i===""?!1:/^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*$/.test(i)}readEscapedIdentifier(e){let t=this.position;for(this.position++;this.canRead()&&this.input[this.position]!==e;)this.position++;if(t===this.position)throw new Error(`Closing delimiter is not found. position: ${t}, delimiter: ${e}
7
+ ${this.getDebugPositionInfo(e)}`);return this.input.slice(e,this.position)}isDollarQuotedString(){if(!this.canRead(1))return!1;if(this.input[this.position+1]==="$")return!0;let e=this.position+1;for(;e<this.input.length;){let t=this.input[e];if(t==="$")return!0;if(!this.isAlphanumeric(t)&&t!=="_")return!1;e++}return!1}readDollarQuotedString(){let e=this.position;this.position++;let t="";for(;this.canRead()&&this.input[this.position]!=="$";)t+=this.input[this.position],this.position++;if(!this.canRead())throw new Error(`Unexpected end of input while reading dollar-quoted string tag at position ${e}`);this.position++;let n="$"+t+"$",i=n,s="";for(;this.canRead();){if(this.input.substring(this.position,this.position+i.length)===i)return this.position+=i.length,n+s+i;s+=this.input[this.position],this.position++}throw new Error(`Unclosed dollar-quoted string starting at position ${e}. Expected closing tag: ${i}`)}isAlphanumeric(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122}};var pn=class extends ue{constructor(e){super(e)}tryRead(e){if(this.isEndOfInput())return null;if(this.canRead(1)&&this.input[this.position]==="$"&&this.input[this.position+1]==="{"){this.position+=2;let n=this.position;for(;this.canRead()&&this.input[this.position]!=="}";)this.position++;if(this.isEndOfInput())throw new Error(`Unexpected end of input. Expected closing '}' for parameter at position ${n}`);let i=this.input.slice(n,this.position);if(i.length===0)throw new Error("Empty parameter name is not allowed: found ${} at position "+(n-2));return this.position++,this.createLexeme(256,"${"+i+"}")}let t=this.input[this.position];if(te.isNamedParameterPrefix(t)){if(this.canRead(1)&&te.isOperatorSymbol(this.input[this.position+1])||t===":"&&this.isInArraySliceContext()||t==="$"&&this.isDollarQuotedString())return null;if(t==="$"&&this.canRead(1)&&te.isDigit(this.input[this.position+1])){let s=this.position+1,r=!1;for(;s<this.input.length&&(te.isDigit(this.input[s])||this.input[s]===","||this.input[s]===".");){if(this.input[s]==="."||this.input[s]===","){r=!0;break}s++}if(r)return null}this.position++;let n=this.position;for(;this.canRead()&&!te.isDelimiter(this.input[this.position]);)this.position++;let i=this.input.slice(n,this.position);return this.createLexeme(256,t+i)}if(t==="?"){if(this.canRead(1)){let n=this.input[this.position+1];if(n==="|"||n==="&")return null}return e&&(e.type&64||e.type&1)?null:(this.position++,this.createLexeme(256,t))}return null}isInArraySliceContext(){let e=this.position-1,t=0,n=0;for(;e>=0;){let i=this.input[e];if(i==="]")t++;else if(i==="["){if(t--,t<0){if(e>0){let s=this.input[e-1];if(/[a-zA-Z0-9_)\]]/.test(s))return!0}if(e===0)return!1;break}}else i===")"?n++:i==="("&&n--;e--}return!1}isDollarQuotedString(){if(!this.canRead(1))return!1;if(this.input[this.position+1]==="$")return!0;let e=this.position+1;for(;e<this.input.length;){let t=this.input[e];if(t==="$")return!0;if(!this.isAlphanumeric(t)&&t!=="_")return!1;e++}return!1}isAlphanumeric(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122}};var mn=class a extends ue{static{this.SPECIAL_SYMBOL_TOKENS={".":32,",":16,"(":4,")":8,"[":512,"]":1024}}tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];return t in a.SPECIAL_SYMBOL_TOKENS?(this.position++,this.createLexeme(a.SPECIAL_SYMBOL_TOKENS[t],t)):null}};var dn=class{constructor(e,t=0){this.cacheHits=0;this.cacheMisses=0;this.input=e,this.position=t,this.readers=[],this.tokenCache=new Map}register(e){return this.readers.push(e),this}registerAll(e){return e.forEach(t=>this.register(t)),this}setPosition(e){this.position=e;for(let t of this.readers)t.setPosition(e)}tryRead(e,t){if(this.tokenCache.has(e))return this.cacheHits++,this.tokenCache.get(e)||null;this.cacheMisses++,this.setPosition(e);let n=null;for(let i of this.readers)if(n=i.tryRead(t),n){this.position=i.getPosition();break}for(let i of this.readers)i.setPosition(this.position);return this.tokenCache.set(e,n),n}getMaxPosition(){let e=this.position;for(let t of this.readers){let n=t.getPosition();n>e&&(e=n)}return e}getInput(){return this.input}getCacheStats(){let e=this.cacheHits+this.cacheMisses,t=e>0?this.cacheHits/e:0;return{hits:this.cacheHits,misses:this.cacheMisses,ratio:t}}};var Gi=new Je([["and"],["or"],["is"],["is","not"],["is","distinct","from"],["is","not","distinct","from"],["like"],["ilike"],["in"],["exists"],["between"],["not","like"],["not","ilike"],["not","in"],["not","exists"],["not","between"],["escape"],["uescape"],["similar","to"],["not","similar","to"],["similar"],["placing"],["rlike"],["regexp"],["mod"],["xor"],["not"],["both"],["leading"],["trailing"],["both","from"],["leading","from"],["trailing","from"],["year","from"],["month","from"],["day","from"],["hour","from"],["minute","from"],["second","from"],["dow","from"],["doy","from"],["isodow","from"],["quarter","from"],["week","from"],["epoch","from"],["at","time","zone"]]),Yi=new Je([["date"],["time"],["timestamp"],["timestamptz"],["timetz"],["interval"],["boolean"],["integer"],["bigint"],["smallint"],["numeric"],["decimal"],["real"],["double","precision"],["double","precision"],["character","varying"],["time","without","time","zone"],["time","with","time","zone"],["timestamp","without","time","zone"],["timestamp","with","time","zone"]]),Zi=new Te(Gi),Xi=new Te(Yi);var hn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];if(te.isOperatorSymbol(t)){let i=this.position;for(;this.canRead()&&te.isOperatorSymbol(this.input[this.position])&&(this.position++,!!this.canRead());){let r=this.input[this.position-1],o=this.input[this.position];if(r==="-"&&o==="-"||r==="/"&&o==="*")break}this.position===i&&this.position++;let s=this.input.slice(i,this.position);return this.createLexeme(2,s)}let n=Xi.parse(this.input,this.position);return n!==null?(this.position=n.newPosition,this.createLexeme(8258,n.keyword)):(n=Zi.parse(this.input,this.position),n!==null?(this.position=n.newPosition,this.createLexeme(2,n.keyword)):null)}};var es=new Je([["join"],["inner","join"],["cross","join"],["left","join"],["left","outer","join"],["right","join"],["right","outer","join"],["full","join"],["full","outer","join"],["natural","join"],["natural","inner","join"],["natural","left","join"],["natural","left","outer","join"],["natural","right","join"],["natural","right","outer","join"],["natural","full","join"],["natural","full","outer","join"],["lateral","join"],["lateral","inner","join"],["lateral","left","join"],["lateral","left","outer","join"]]),Kt=new Je([["with"],["recursive"],["materialized"],["not","materialized"],["select"],["from"],["distinct"],["distinct","on"],["where"],["group","by"],["having"],["order","by"],["limit"],["offset"],["fetch"],["first"],["next"],["row"],["row","only"],["rows","only"],["percent"],["percent","with","ties"],["for"],["update"],["share"],["key","share"],["no","key","update"],["union"],["union","all"],["intersect"],["intersect","all"],["except"],["except","all"],["beteen"],["window"],["over"],["partition","by"],["range"],["rows"],["groups"],["within","group"],["with","ordinality"],["current","row"],["unbounded","preceding"],["unbounded","following"],["preceding"],["following"],["on"],["using"],["lateral"],["case"],["case","when"],["when"],["then"],["else"],["end"],["insert","into"],["update"],["delete","from"],["merge","into"],["matched"],["not","matched"],["update","set"],["do","nothing"],["values"],["set"],["returning"],["create","table"],["create","temporary","table"],["tablesample"],["as"],["asc"],["desc"],["nulls","first"],["nulls","last"]]),ts=new Te(Kt),Jt=new Te(es);var fn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=Jt.parse(this.input,this.position);if(t!==null)return this.position=t.newPosition,this.createLexeme(128,t.keyword);let n=ts.parse(this.input,this.position);if(n!==null){this.position=n.newPosition;let i=this.createLexeme(128,n.keyword,n.comments);return n.comments&&n.comments.length>0&&(i.positionedComments=[{position:"after",comments:n.comments}]),i}if(this.canRead(2)&&this.input[this.position]==="/"&&this.input[this.position+1]==="*"&&this.input[this.position+2]==="+"){this.position+=3;let i=this.position;for(;this.position+1<this.input.length;){if(this.input[this.position]==="*"&&this.input[this.position+1]==="/")return this.position+=2,this.createLexeme(128,"/*+ "+this.input.slice(i,this.position-2).trim()+" */");this.position++}throw new Error(`Block comment is not closed. position: ${this.position}`)}return null}};var ns=new Set(["e'","E'","x'","X'","b'","B'"]),is=new Set(["u&'","U&'"]),Cn=class extends ue{tryRead(e){let t=this.position;return this.canRead(1)&&ns.has(this.input.slice(t,t+2))?(this.position+=1,this.createLexeme(4096,this.input.slice(t,this.position))):this.canRead(2)&&is.has(this.input.slice(t,t+3))?(this.position+=2,this.createLexeme(4096,this.input.slice(t,this.position))):null}};var ss=new Je([["grouping","sets"],["array"]]),rs=new Te(ss),gn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=rs.parse(this.input,this.position);if(t!==null)return this.position=t.newPosition,this.createLexeme(2048,t.keyword);let n=le.tryReadRegularIdentifier(this.input,this.position);if(!n)return null;this.position=n.newPosition;var i=le.readWhiteSpaceAndComment(this.input,this.position).position-this.position;return this.canRead(i)&&this.input[this.position+i]==="("?this.createLexeme(2048,n.identifier):null}};var os=new Je([["double","precision"],["character","varying"],["time","without","time","zone"],["time","with","time","zone"],["timestamp","without","time","zone"],["timestamp","with","time","zone"]]),as=new Te(os),yn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=as.parse(this.input,this.position);if(t!==null)return this.position=t.newPosition,this.createLexeme(8192,t.keyword);if(e===null)return null;let n=le.tryReadRegularIdentifier(this.input,this.position);return n?(this.position=n.newPosition,e.type&128&&e.value==="as"?this.createLexeme(8256,n.identifier):e.type&2&&e.value==="::"?this.createLexeme(8192,n.identifier):null):null}};var vn=class extends ue{tryRead(e){if(this.isEndOfInput())return null;let t=this.input[this.position];if(t==="`"){let n=this.readEscapedIdentifier("`");return this.createLexeme(64,n)}if(t==='"'){let n=this.readEscapedIdentifier('"');return this.createLexeme(64,n)}if(t==="["&&this.isSqlServerBracketIdentifier(e)){let n=this.readEscapedIdentifier("]");return this.createLexeme(64,n)}return null}isSqlServerBracketIdentifier(e){if(e?.value==="array")return!1;let t=this.position+1,n=t;for(;n<this.input.length&&this.input[n]!=="]";){let s=this.input[n];if(s===":"||s===","||s==="+"||s==="-"||s==="*"||s==="/"||s==="("||s===")")return!1;n++}if(n>=this.input.length)return!1;let i=this.input.slice(t,n).trim();return i===""?!1:/^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*$/.test(i)}readEscapedIdentifier(e){let t=this.position;this.position++;let n=!1;for(;this.canRead();){if(this.input[this.position]===e){n=!0;break}this.position++}if(!n)throw new Error(`Closing delimiter is not found. position: ${t}, delimiter: ${e}
8
+ ${this.getDebugPositionInfo(t)}`);if(t===this.position)throw new Error(`Closing delimiter is not found. position: ${t}, delimiter: ${e}
8
9
  ${this.getDebugPositionInfo(t)}`);return this.position++,this.input.slice(t+1,this.position-1)}};var R=class a{constructor(e){this.input=e,this.position=0,this.readerManager=new dn(e).register(new vn(e)).register(new pn(e)).register(new Cn(e)).register(new cn(e)).register(new mn(e)).register(new fn(e)).register(new hn(e)).register(new yn(e)).register(new gn(e)).register(new un(e))}isEndOfInput(e=0){return this.position+e>=this.input.length}canRead(e=0){return!this.isEndOfInput(e)}tokenize(e){return e?.preserveFormatting?this.tokenizeWithFormatting():new a(this.input).readLexmes()}readLexmes(){return this.tokenizeBasic()}tokenizeBasic(){this.position=0;let e=[],t=null;for(;this.canRead()&&this.input[this.position]!==";";){let i=this.readComment(),s=this.position=i.position,r=this.readerManager.tryRead(this.position,t);if(r===null)throw new Error(`Unexpected character. actual: ${this.input[this.position]}, position: ${this.position}
9
10
  ${this.getDebugPositionInfo(this.position)}`);let o=this.position=this.readerManager.getMaxPosition(),l=this.readComment();this.position=l.position,e.push({lexeme:r,startPos:s,endPos:o,prefixComments:i.lines,suffixComments:l.lines}),t=r}let n=new Array(e.length);for(let i=0;i<e.length;i++){let s=e[i],r=s.lexeme;if(r.value.toLowerCase()==="select"&&s.suffixComments.length>0){let o=i+1;for(;o<e.length;){let l=e[o];if(l.lexeme.type&64||l.lexeme.type&1||!(l.lexeme.type&128)&&!(l.lexeme.type&16)&&!(l.lexeme.type&2)){l.prefixComments.unshift(...s.suffixComments),s.suffixComments=[];break}o++}}if(r.type&16&&s.suffixComments.length>0){let o=i+1;o<e.length&&(e[o].prefixComments.unshift(...s.suffixComments),s.suffixComments=[])}if((r.value.toLowerCase()==="union"||r.value.toLowerCase()==="intersect"||r.value.toLowerCase()==="except")&&s.suffixComments.length>0){let o=i+1;for(;o<e.length;){let l=e[o];if(l.lexeme.value.toLowerCase()==="select"){l.prefixComments.unshift(...s.suffixComments),s.suffixComments=[];break}o++}}this.attachCommentsToLexeme(r,s),n[i]=r}return n}attachCommentsToLexeme(e,t){let n=[],i=[];e.positionedComments&&e.positionedComments.length>0&&n.push(...e.positionedComments),t.prefixComments.length>0&&(i.push(...t.prefixComments),n.push({position:"before",comments:[...t.prefixComments]})),t.suffixComments.length>0&&(i.push(...t.suffixComments),n.push({position:"after",comments:[...t.suffixComments]})),n.length>0?(e.positionedComments=n,e.comments=null):i.length>0?(e.comments=i,e.positionedComments=void 0):(e.comments=null,e.positionedComments=void 0)}readComment(){return le.readWhiteSpaceAndComment(this.input,this.position)}getDebugPositionInfo(e){return le.getDebugPositionInfo(this.input,e)}tokenizeWithFormatting(){let e=this.tokenizeBasic();return this.mapToFormattingLexemes(e)}mapToFormattingLexemes(e){if(e.length===0)return[];let t=[],n=0;for(let s of e){n=this.skipWhitespaceAndComments(n);let r=this.findLexemeAtPosition(s,n);if(r)t.push(r),n=r.endPosition;else{let o={startPosition:n,endPosition:n+s.value.length};t.push(o),n=o.endPosition}}let i=[];for(let s=0;s<e.length;s++){let r=e[s],o=t[s],l=s<e.length-1?t[s+1].startPosition:this.input.length,u=this.input.slice(o.endPosition,l),c=this.extractCommentsFromWhitespace(u),p={...r,followingWhitespace:u,inlineComments:c,position:{startPosition:o.startPosition,endPosition:o.endPosition,...this.getLineColumnInfo(o.startPosition,o.endPosition)}};i.push(p)}return i}findLexemeAtPosition(e,t){if(t>=this.input.length)return null;let n=[e.value,e.value.toUpperCase(),e.value.toLowerCase()];for(let i of n)if(t+i.length<=this.input.length&&this.input.substring(t,t+i.length)===i&&this.isValidLexemeMatch(i,t))return{startPosition:t,endPosition:t+i.length};return null}isValidLexemeMatch(e,t){if(t>0){let i=this.input[t-1];if(this.isAlphanumericUnderscore(i))return!1}let n=t+e.length;if(n<this.input.length){let i=this.input[n];if(this.isAlphanumericUnderscore(i))return!1}return!0}isAlphanumericUnderscore(e){let t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122||t===95}isWhitespace(e){let t=e.charCodeAt(0);return t===32||t===9||t===10||t===13}extractCommentsFromWhitespace(e){let t=[],n=0;for(;n<e.length;){let i=n,s=le.readWhiteSpaceAndComment(e,n);s.lines.length>0&&t.push(...s.lines),n=s.position,n===i&&n++}return t}skipWhitespaceAndComments(e){let t=e;for(;t<this.input.length;){let n=this.input[t];if(this.isWhitespace(n)){t++;continue}if(t<this.input.length-1&&this.input[t]==="-"&&this.input[t+1]==="-"){for(;t<this.input.length&&this.input[t]!==`
10
11
  `&&this.input[t]!=="\r";)t++;continue}if(t<this.input.length-1&&this.input[t]==="/"&&this.input[t+1]==="*"){for(t+=2;t<this.input.length-1;){if(this.input[t]==="*"&&this.input[t+1]==="/"){t+=2;break}t++}continue}break}return t}getLineColumnInfo(e,t){let n=this.getLineColumn(e),i=this.getLineColumn(t);return{startLine:n.line,startColumn:n.column,endLine:i.line,endColumn:i.column}}getLineColumn(e){let t=1,n=1;for(let i=0;i<Math.min(e,this.input.length);i++)this.input[i]===`