Skip to content

fix: add support for named param type $name #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions crates/pgt_tokenizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,29 @@ impl Cursor<'_> {
'$' => {
// Dollar quoted strings
if is_ident_start(self.first()) || self.first() == '$' {
self.dollar_quoted_string()
// Get the start sequence of the dollar quote, i.e., 'foo' in $foo$hello$foo$
// if ident does not continue and there is no terminating dollar
// sign, we have a positional param `$name`
let mut start = vec![];
loop {
match self.first() {
'$' => {
self.bump();
break self.dollar_quoted_string(start);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break self.dollar_quoted_string(start);
return self.dollar_quoted_string(start);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hat mich kurz irritiert, weil der breaked value ja auch aus der obrigen fn returned wird

}
c if is_ident_cont(c) => {
self.bump();
start.push(c);
}
_ => {
break TokenKind::NamedParam {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break TokenKind::NamedParam {
return TokenKind::NamedParam {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would actually behave differently. we need break, and the value is then returned from the loop. with return, we would return the value from the function.

kind: NamedParamKind::DollarRaw,
};
}
}
}
} else {
// Parameters
// positional parameter, e.g. `$1`
while self.first().is_ascii_digit() {
self.bump();
}
Expand Down Expand Up @@ -490,22 +510,7 @@ impl Cursor<'_> {
}

// https://www.postgresql.org/docs/16/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING
fn dollar_quoted_string(&mut self) -> TokenKind {
// Get the start sequence of the dollar quote, i.e., 'foo' in
// $foo$hello$foo$
let mut start = vec![];
while let Some(c) = self.bump() {
match c {
'$' => {
self.bump();
break;
}
_ => {
start.push(c);
}
}
}

fn dollar_quoted_string(&mut self, start: Vec<char>) -> TokenKind {
// we have a dollar quoted string deliminated with `$$`
if start.is_empty() {
loop {
Expand Down Expand Up @@ -658,6 +663,12 @@ mod tests {
assert_debug_snapshot!(result);
}

#[test]
fn named_param_dollar_raw() {
let result = lex("select 1 from c where id = $id;");
assert_debug_snapshot!(result);
}

#[test]
fn named_param_colon_raw() {
let result = lex("select 1 from c where id = :id;");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/pgt_tokenizer/src/lib.rs
expression: result
snapshot_kind: text
---
[
"select" @ Ident,
" " @ Space,
"1" @ Literal { kind: Int { base: Decimal, empty_int: false } },
" " @ Space,
"from" @ Ident,
" " @ Space,
"c" @ Ident,
" " @ Space,
"where" @ Ident,
" " @ Space,
"id" @ Ident,
" " @ Space,
"=" @ Eq,
" " @ Space,
"$id" @ NamedParam { kind: DollarRaw },
";" @ Semi,
]
3 changes: 3 additions & 0 deletions crates/pgt_tokenizer/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ pub enum NamedParamKind {
///
/// Used in: psql
ColonIdentifier { terminated: bool },

/// e.g. `$name`
DollarRaw,
}

/// Parsed token.
Expand Down
2 changes: 0 additions & 2 deletions crates/pgt_workspace/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,6 @@ impl Workspace for WorkspaceServer {
.await
.unwrap_or_else(|_| vec![]);

println!("{:#?}", plpgsql_check_results);

for d in plpgsql_check_results {
let r = d.span.map(|span| span + range.start());
diagnostics.push(
Expand Down