Skip to content
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
9 changes: 4 additions & 5 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use squawk_syntax::{SyntaxNodePtr, ast, ast::AstNode};

use crate::literals::literal_string_value;
use crate::name::{AsName, schema_and_func_name};
use crate::scope::Scope;
use crate::symbols::{Name, Schema, Symbol, SymbolKind};
Expand Down Expand Up @@ -1775,7 +1774,7 @@ fn bind_prepare_transaction(b: &mut Binder, prepare: ast::PrepareTransaction) {
let Some(literal) = prepare.literal() else {
return;
};
let Some(transaction_id) = literal_string_value(&literal) else {
let Some(transaction_id) = literal.string_value() else {
return;
};

Expand All @@ -1787,7 +1786,7 @@ fn bind_prepared_transaction_ref(b: &mut Binder, literal: Option<ast::Literal>)
let Some(literal) = literal else {
return;
};
let Some(transaction_id) = literal_string_value(&literal) else {
let Some(transaction_id) = literal.string_value() else {
return;
};

Expand Down Expand Up @@ -2063,7 +2062,7 @@ fn bind_select_set_config(b: &mut Binder, select: &ast::Select, position: TextSi
let Some(ast::Expr::Literal(setting_name_literal)) = args.next().and_then(|a| a.expr()) else {
return;
};
let Some(setting_name) = literal_string_value(&setting_name_literal) else {
let Some(setting_name) = setting_name_literal.string_value() else {
return;
};
if !setting_name.eq_ignore_ascii_case("search_path") {
Expand All @@ -2073,7 +2072,7 @@ fn bind_select_set_config(b: &mut Binder, select: &ast::Select, position: TextSi
let Some(ast::Expr::Literal(new_value_literal)) = args.next().and_then(|a| a.expr()) else {
return;
};
let Some(new_value) = literal_string_value(&new_value_literal) else {
let Some(new_value) = new_value_literal.string_value() else {
return;
};

Expand Down
3 changes: 1 addition & 2 deletions crates/squawk_ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::file::InFile;
use crate::infer::{infer_type_from_expr, infer_type_from_literal};
use crate::literals::binary_digits_to_hex;
use crate::literals::hex_digits_to_binary;
use crate::literals::literal_string_value;
use crate::location::{Location, LocationKind};
use crate::name;
use crate::offsets::token_from_offset;
Expand Down Expand Up @@ -275,7 +274,7 @@ fn hover_literal(literal: &ast::Literal) -> Option<Hover> {
return None;
}

let value = literal_string_value(literal)?;
let value = literal.string_value()?;
let ty = infer_type_from_literal(literal)?.to_string();

let comment = match kind {
Expand Down
92 changes: 0 additions & 92 deletions crates/squawk_ide/src/literals.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
use squawk_syntax::{
SyntaxKind,
ast::{self, AstNode},
quote::{strip_dollar_quotes, strip_prefixed_quotes, strip_quotes, strip_unicode_esc_prefix},
unescape::{decode_esc_string, decode_plain_string, decode_unicode_esc_string, uescape_char},
};

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum IntegerRadix {
Binary,
Expand Down Expand Up @@ -86,88 +79,3 @@ pub(crate) fn hex_digits_to_binary(digits: &str) -> Option<String> {

Some(out)
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum StringDecoding {
BitOrByte,
EscString,
UnicodeEscString,
}

pub(crate) fn literal_string_value(literal: &ast::Literal) -> Option<String> {
let escape_char = unicode_escape_char(literal);
let mut out = String::with_capacity(literal.syntax().text().len().into());
let mut decoding: Option<StringDecoding> = None;

for element in literal.syntax().children_with_tokens() {
let Some(token) = element.into_token() else {
continue;
};
match token.kind() {
SyntaxKind::ESC_STRING => {
let inner = strip_prefixed_quotes(token.text(), ['e', 'E'])?;
decode_esc_string(inner, &mut out);
decoding = Some(StringDecoding::EscString);
}
SyntaxKind::UNICODE_ESC_STRING => {
let inner = strip_unicode_esc_prefix(token.text())?;
decode_unicode_esc_string(inner, escape_char, &mut out);
decoding = Some(StringDecoding::UnicodeEscString);
}
SyntaxKind::BIT_STRING => {
let inner = strip_prefixed_quotes(token.text(), ['b', 'B'])?;
out.push_str(inner);
decoding = Some(StringDecoding::BitOrByte);
}
SyntaxKind::BYTE_STRING => {
let inner = strip_prefixed_quotes(token.text(), ['x', 'X'])?;
out.push_str(inner);
decoding = Some(StringDecoding::BitOrByte);
}
SyntaxKind::DOLLAR_QUOTED_STRING => {
let inner = strip_dollar_quotes(token.text())?;
out.push_str(inner);
return Some(out);
}
SyntaxKind::NATIONAL_STRING => {
let inner = strip_prefixed_quotes(token.text(), ['n', 'N'])?;
decode_plain_string(inner, &mut out);
}
SyntaxKind::STRING => {
let inner = strip_quotes(token.text())?;
match decoding {
Some(StringDecoding::EscString) => decode_esc_string(inner, &mut out),
Some(StringDecoding::UnicodeEscString) => {
decode_unicode_esc_string(inner, escape_char, &mut out)
}
Some(StringDecoding::BitOrByte) => out.push_str(inner),
None => decode_plain_string(inner, &mut out),
}
}
SyntaxKind::UESCAPE_KW => break,
_ => (),
}
}

Some(out)
}

fn unicode_escape_char(literal: &ast::Literal) -> char {
let mut seen_uescape = false;
for element in literal.syntax().children_with_tokens() {
let Some(token) = element.into_token() else {
continue;
};
match token.kind() {
SyntaxKind::UESCAPE_KW => seen_uescape = true,
SyntaxKind::STRING if seen_uescape => {
if let Some(ch) = uescape_char(token.text()) {
return ch;
}
return '\\';
}
_ => (),
}
}
'\\'
}
66 changes: 65 additions & 1 deletion crates/squawk_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ impl Cursor<'_> {
TokenKind::Literal { kind: literal_kind }
}
'.' => match self.first() {
// https://github.com/postgres/postgres/blob/db0c96cc18aec417101e37e59fcc53d4bf647915/src/backend/parser/scan.l#L353
'.' => {
self.bump();
TokenKind::DotDot
}
'0'..='9' => {
let literal_kind = self.number('.');
TokenKind::Literal { kind: literal_kind }
Expand Down Expand Up @@ -302,7 +307,9 @@ impl Cursor<'_> {
};

match self.first() {
'.' => {
// `1..10` lexes as 1, .., 10 rather than `1.` followed by `.10`
// https://github.com/postgres/postgres/blob/db0c96cc18aec417101e37e59fcc53d4bf647915/src/backend/parser/scan.l#L410
'.' if self.second() != '.' => {
self.bump();
self.eat_fractional()
}
Expand Down Expand Up @@ -794,6 +801,63 @@ $foo$hello$world$bar$
"#)
}

#[test]
fn dot_dot_is_its_own_token() {
assert_debug_snapshot!(lex("1..10 1 .. 10 1.5..2 1...2 .. a[1..2]"), @r#"
[
"1" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
".." @ DotDot,
"10" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 2 } },
" " @ Whitespace,
"1" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
" " @ Whitespace,
".." @ DotDot,
" " @ Whitespace,
"10" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 2 } },
" " @ Whitespace,
"1.5" @ Literal { kind: Numeric { empty_exponent_start: None, trailing_junk_start: 3 } },
".." @ DotDot,
"2" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
" " @ Whitespace,
"1" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
".." @ DotDot,
".2" @ Literal { kind: Numeric { empty_exponent_start: None, trailing_junk_start: 2 } },
" " @ Whitespace,
".." @ DotDot,
" " @ Whitespace,
"a" @ Ident,
"[" @ OpenBracket,
"1" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
".." @ DotDot,
"2" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
"]" @ CloseBracket,
]
"#);
}

#[test]
fn dot_dot_does_not_disturb_neighboring_numerics() {
assert_debug_snapshot!(lex("1. .10 0 . .5 a.b 1.5"), @r#"
[
"1." @ Literal { kind: Numeric { empty_exponent_start: None, trailing_junk_start: 2 } },
" " @ Whitespace,
".10" @ Literal { kind: Numeric { empty_exponent_start: None, trailing_junk_start: 3 } },
" " @ Whitespace,
"0" @ Literal { kind: Int { base: Decimal, empty_int: false, trailing_junk_start: 1 } },
" " @ Whitespace,
"." @ Dot,
" " @ Whitespace,
".5" @ Literal { kind: Numeric { empty_exponent_start: None, trailing_junk_start: 2 } },
" " @ Whitespace,
"a" @ Ident,
"." @ Dot,
"b" @ Ident,
" " @ Whitespace,
"1.5" @ Literal { kind: Numeric { empty_exponent_start: None, trailing_junk_start: 3 } },
]
"#);
}

#[test]
fn numeric_exponent_underscore_after_sign() {
assert_debug_snapshot!(lex("1e+_2 1e-_2 1.0e+_2 .1e+_2"), @r#"
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_lexer/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum TokenKind {
Colon,
/// `.`
Dot,
/// `..`
DotDot,
/// `=`
Eq,
/// `>`
Expand Down
Loading
Loading