-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex-parser.html
More file actions
217 lines (203 loc) · 10.8 KB
/
index-parser.html
File metadata and controls
217 lines (203 loc) · 10.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SQL Parser Playground</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Spline+Sans:wght@400;600;700&display=swap" rel="stylesheet" />
<style>
html, body { height: 100%; }
body { font-family: "Spline Sans", system-ui, sans-serif; }
.fade-in { animation: fade 400ms ease-out; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
</style>
</head>
<body class="bg-gray-50 text-gray-800">
<div class="min-h-screen px-4 py-10">
<div class="mx-auto max-w-2xl">
<header class="mb-6 flex flex-wrap items-start justify-between gap-3">
<div>
<h1 class="text-2xl font-bold tracking-tight sm:text-3xl">SQL Parser Playground</h1>
<p class="mt-2 text-sm text-gray-600">SQL を入力して AST をテキストとして出力します。</p>
</div>
<div class="flex items-center gap-2 text-xs text-gray-500">
<span class="rounded-full bg-gray-100 px-2 py-1 font-semibold">AST</span>
</div>
</header>
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
<section class="rounded-xl border border-gray-200 bg-white p-4 shadow-md">
<div class="flex flex-wrap items-center justify-between gap-2">
<h2 class="text-sm font-semibold text-gray-700">SQL Input</h2>
<button id="parseBtn" class="rounded-lg bg-green-600 px-3 py-1.5 text-xs font-semibold text-white transition-colors hover:bg-green-700">Parse</button>
</div>
<textarea id="sqlInput" class="mt-3 h-64 w-full resize-none rounded-lg border border-gray-200 bg-white p-3 text-sm leading-6 outline-none transition-colors focus:border-gray-400 focus:ring-2 focus:ring-green-200" spellcheck="false"></textarea>
<pre id="sqlPreview" class="mt-3 h-32 overflow-auto rounded-lg border border-dashed border-gray-300 bg-gray-50 p-3 text-xs leading-5 text-gray-700"></pre>
<div class="mt-3 flex flex-wrap gap-2">
<button data-sample="SELECT a, COUNT(*) FROM t GROUP BY a" class="sampleBtn rounded-full border border-gray-200 px-3 py-1 text-xs transition-colors hover:border-green-600">Sample 1</button>
<button data-sample="SELECT * FROM t MATCH_RECOGNIZE (PATTERN (A B) DEFINE A AS a > 0, B AS b > 0)" class="sampleBtn rounded-full border border-gray-200 px-3 py-1 text-xs transition-colors hover:border-green-600">Sample 2</button>
<button data-sample="SELECT * FROM t PIVOT (SUM(x) AS sx FOR y IN (1 AS one, 2 AS two))" class="sampleBtn rounded-full border border-gray-200 px-3 py-1 text-xs transition-colors hover:border-green-600">Sample 3</button>
</div>
</section>
<section class="rounded-xl border border-gray-200 bg-white p-4 shadow-md">
<div class="flex flex-wrap items-center justify-between gap-2">
<h2 class="text-sm font-semibold text-gray-700">AST Output</h2>
<span id="status" class="text-xs text-gray-500">idle</span>
</div>
<pre id="astOutput" class="mt-3 h-64 overflow-auto rounded-lg border border-gray-200 bg-slate-900 p-3 text-xs leading-5 text-slate-100"></pre>
</section>
</div>
<section class="mt-6 rounded-xl border border-gray-200 bg-white p-4 shadow-md">
<div class="flex flex-wrap items-center justify-between gap-2">
<h2 class="text-sm font-semibold text-gray-700">Tokens</h2>
<span id="tokenMeta" class="text-xs text-gray-500">0 tokens</span>
</div>
<pre id="tokenOutput" class="mt-3 h-40 overflow-auto rounded-lg border border-gray-200 bg-slate-900 p-3 text-xs leading-5 text-slate-100"></pre>
</section>
<hr class="mt-10 border-gray-200" />
<footer class="mt-4 flex flex-wrap items-center gap-4 text-xs text-gray-600">
<a href="https://github.com/igapyon/sql-formatter" class="inline-flex items-center gap-2 text-blue-600 transition-colors hover:text-blue-800 underline" target="_blank" rel="noopener noreferrer">
<svg aria-hidden="true" viewBox="0 0 16 16" class="h-4 w-4" fill="currentColor">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8Z"/>
</svg>
<span>GitHub</span>
</a>
<a href="./LICENSE" class="inline-flex items-center gap-2 text-blue-600 transition-colors hover:text-blue-800 underline" target="_blank" rel="noopener noreferrer">
<span class="inline-flex items-center justify-center rounded-full border border-gray-200 bg-gray-100 px-2 py-0.5 text-xs font-semibold transition-colors hover:bg-gray-200">License</span>
</a>
<a href="https://igapyon.github.io/sql-formatter/" class="inline-flex items-center gap-2 text-blue-600 transition-colors hover:text-blue-800 underline" target="_blank" rel="noopener noreferrer">
<svg aria-hidden="true" viewBox="0 0 24 24" class="h-4 w-4" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="9"/>
<path d="M3 12h18"/>
<path d="M12 3a14 14 0 0 1 0 18"/>
<path d="M12 3a14 14 0 0 0 0 18"/>
</svg>
<span>GitHub Pages</span>
</a>
</footer>
</div>
</div>
<script src="./spec/apache-calcite-Parser.js"></script>
<script src="./spec/wellknown-sql-ddl.js"></script>
<script>
const input = document.getElementById('sqlInput');
const output = document.getElementById('astOutput');
const status = document.getElementById('status');
const tokenOutput = document.getElementById('tokenOutput');
const tokenMeta = document.getElementById('tokenMeta');
const parseBtn = document.getElementById('parseBtn');
const sqlPreview = document.getElementById('sqlPreview');
const defaultSql = "SELECT a FROM t WINDOW w AS (PARTITION BY a ORDER BY b ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)";
input.value = defaultSql;
function render(obj) {
output.textContent = JSON.stringify(obj, null, 2);
output.classList.add('fade-in');
setTimeout(() => output.classList.remove('fade-in'), 420);
}
function renderTokens(tokens) {
tokenMeta.textContent = `${tokens.length} tokens`;
const lines = tokens.map((t, i) => {
const v = t.value === null ? 'null' : String(t.value);
return `${String(i).padStart(3, ' ')} ${t.type.padEnd(6, ' ')} ${v}`;
});
tokenOutput.textContent = lines.join('\n');
tokenOutput.classList.add('fade-in');
setTimeout(() => tokenOutput.classList.remove('fade-in'), 420);
}
function escapeHtml(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\"/g, '"')
.replace(/'/g, ''');
}
function renderSqlPreview(sql, token) {
if (!token || typeof token.start !== 'number' || typeof token.end !== 'number') {
sqlPreview.textContent = sql;
return;
}
const before = escapeHtml(sql.slice(0, token.start));
const mid = escapeHtml(sql.slice(token.start, token.end));
const after = escapeHtml(sql.slice(token.end));
sqlPreview.innerHTML = `${before}<mark class="bg-amber-200 rounded px-1">${mid || ' '}</mark>${after}`;
input.setSelectionRange(token.start, token.end);
}
function parseSql() {
const sql = input.value.trim();
if (!sql) {
output.textContent = '';
tokenOutput.textContent = '';
tokenMeta.textContent = '0 tokens';
status.textContent = 'empty';
sqlPreview.textContent = '';
return;
}
try {
const lexer = new window.CalciteLexer(sql);
const tokens = lexer.tokenize();
renderTokens(tokens);
renderSqlPreview(sql, null);
const parser = new window.CalciteParser(tokens);
const ast = parser.SqlStmtList();
status.textContent = `ok (calcite pos: ${parser.pos})`;
render(ast);
} catch (err) {
if (window.WellknownDdlLexer && window.WellknownDdlParser) {
try {
const ddlLexer = new window.WellknownDdlLexer(sql);
const ddlTokens = ddlLexer.tokenize();
renderTokens(ddlTokens);
renderSqlPreview(sql, null);
const ddlParser = new window.WellknownDdlParser(ddlTokens);
const ddlAst = ddlParser.SqlStmtList();
status.textContent = `ok (ddl pos: ${ddlParser.pos})`;
render(ddlAst);
return;
} catch (ddlErr) {
let errorMessage = String(err.message || err);
status.textContent = 'error';
render({ error: errorMessage, ddlError: String(ddlErr.message || ddlErr) });
return;
}
}
let errorMessage = String(err.message || err);
let errorPos = null;
let errorToken = null;
try {
const lexer = new window.CalciteLexer(sql);
const tokens = lexer.tokenize();
renderTokens(tokens);
const parser = new window.CalciteParser(tokens);
try { parser.SqlStmtList(); } catch (_e) {
errorPos = parser.pos;
errorToken = tokens[parser.pos] || null;
}
} catch (_ignored) {}
if (errorPos !== null && errorToken) {
status.textContent = `error (pos: ${errorPos}, token: ${errorToken.type}:${errorToken.value})`;
renderSqlPreview(sql, errorToken);
} else {
status.textContent = 'error';
sqlPreview.textContent = sql;
}
render({ error: errorMessage, pos: errorPos, token: errorToken });
}
}
parseBtn.addEventListener('click', parseSql);
input.addEventListener('keydown', (e) => {
if (e.metaKey && e.key === 'Enter') parseSql();
if (e.ctrlKey && e.key === 'Enter') parseSql();
});
document.querySelectorAll('.sampleBtn').forEach((btn) => {
btn.addEventListener('click', () => {
input.value = btn.getAttribute('data-sample');
parseSql();
});
});
parseSql();
</script>
</body>
</html>