-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbounded-sql-lexer.test.ts
More file actions
185 lines (176 loc) · 5.16 KB
/
Copy pathbounded-sql-lexer.test.ts
File metadata and controls
185 lines (176 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { describe, expect, it } from "vitest";
import {
BoundedSqlLexer,
MAX_BOUNDED_SQL_LEXEMES,
type BoundedSqlLexeme,
} from "../bounded-sql-lexer.js";
import {
BIGQUERY_SQL_LEXICAL_PROFILE,
POSTGRESQL_SQL_LEXICAL_PROFILE,
type SqlLexicalProfile,
} from "../lexical.js";
import {
createIdentitySqlSource,
createMaskedSqlSource,
type SqlSourceSnapshot,
} from "../source.js";
function lex(
source: SqlSourceSnapshot,
profile: SqlLexicalProfile = POSTGRESQL_SQL_LEXICAL_PROFILE,
): {
readonly lexemes: readonly BoundedSqlLexeme[];
readonly resource: BoundedSqlLexer["resource"];
} {
const lexer = new BoundedSqlLexer(
source,
0,
source.analysisText.length,
profile,
);
const lexemes: BoundedSqlLexeme[] = [];
while (true) {
const lexeme = lexer.next();
if (!lexeme) {
return { lexemes, resource: lexer.resource };
}
lexemes.push(lexeme);
}
}
describe("bounded SQL lexer", () => {
it("streams words, punctuation, strings, comments, and UTF-16 ranges", () => {
const text = "Se😀lect . 'x' -- note\n/* nested /* x */ */ end";
expect(lex(createIdentitySqlSource(text))).toEqual({
lexemes: [
{ closed: true, from: 0, kind: "word", to: 8 },
{ closed: true, from: 9, kind: "punctuation", to: 10 },
{ closed: true, from: 11, kind: "string", to: 14 },
{ closed: true, from: 15, kind: "line-comment", to: 22 },
{ closed: true, from: 23, kind: "comment", to: 43 },
{ closed: true, from: 44, kind: "word", to: 47 },
],
resource: null,
});
});
it("keeps BigQuery backticks, raw triples, and hash comments atomic", () => {
const text = "`a.b` R'''raw\\value''' # comment";
expect(
lex(
createIdentitySqlSource(text),
BIGQUERY_SQL_LEXICAL_PROFILE,
),
).toEqual({
lexemes: [
{
closed: true,
from: 0,
kind: "quoted-identifier",
to: 5,
},
{ closed: true, from: 6, kind: "word", to: 7 },
{ closed: true, from: 7, kind: "string", to: 22 },
{
closed: true,
from: 23,
kind: "line-comment",
to: 32,
},
],
resource: null,
});
});
it("emits embedded regions as barriers and finds exact boundaries", () => {
const source = createMaskedSqlSource("a {value} b", [
{ from: 2, language: "python", to: 9 },
]);
expect(lex(source).lexemes).toEqual([
{ closed: true, from: 0, kind: "word", to: 1 },
{ closed: true, from: 2, kind: "barrier", to: 9 },
{ closed: true, from: 10, kind: "word", to: 11 },
]);
});
it("pushes back one token without spending the budget twice", () => {
const source = createIdentitySqlSource("one two");
const lexer = new BoundedSqlLexer(
source,
0,
source.analysisText.length,
POSTGRESQL_SQL_LEXICAL_PROFILE,
);
const first = lexer.next();
expect(first).not.toBeNull();
if (!first) {
throw new Error("Expected first lexeme");
}
lexer.pushBack(first);
expect(lexer.next()).toBe(first);
expect(lexer.next()).toEqual({
closed: true,
from: 4,
kind: "word",
to: 7,
});
expect(lexer.next()).toBeNull();
expect(lexer.resource).toBeNull();
expect(lexer.resourceAt).toBeNull();
});
it("fails closed immediately after the shared lexeme budget", () => {
const acceptedWords = Array.from(
{ length: MAX_BOUNDED_SQL_LEXEMES },
() => "x",
).join(" ");
const accepted = lex(createIdentitySqlSource(acceptedWords));
expect(accepted.lexemes).toHaveLength(MAX_BOUNDED_SQL_LEXEMES);
expect(accepted.resource).toBeNull();
const words = Array.from(
{ length: MAX_BOUNDED_SQL_LEXEMES + 1 },
() => "x",
).join(" ");
const result = lex(createIdentitySqlSource(words));
expect(result.lexemes).toHaveLength(MAX_BOUNDED_SQL_LEXEMES);
expect(result.resource).toBe("lexical-token");
const source = createIdentitySqlSource(words);
const lexer = new BoundedSqlLexer(
source,
0,
source.analysisText.length,
POSTGRESQL_SQL_LEXICAL_PROFILE,
);
while (lexer.next()) {
// Consume the bounded prefix.
}
expect(lexer.resourceAt).toBe(
words.lastIndexOf("x"),
);
const prefixed = ` ${words}`;
const prefixedSource = createIdentitySqlSource(prefixed);
const prefixedLexer = new BoundedSqlLexer(
prefixedSource,
2,
prefixed.length,
POSTGRESQL_SQL_LEXICAL_PROFILE,
);
while (prefixedLexer.next()) {
// Consume the bounded prefix.
}
expect(prefixedLexer.resourceAt).toBe(
prefixed.lastIndexOf("x"),
);
});
it("reports oversized dollar-quote delimiters without emitting a token", () => {
const source = createIdentitySqlSource(
`$${"a".repeat(257)}$unterminated`,
);
expect(lex(source)).toEqual({
lexemes: [],
resource: "dollar-quote-delimiter",
});
const lexer = new BoundedSqlLexer(
source,
0,
source.analysisText.length,
POSTGRESQL_SQL_LEXICAL_PROFILE,
);
expect(lexer.next()).toBeNull();
expect(lexer.resourceAt).toBe(0);
});
});