-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
445 lines (427 loc) · 18.4 KB
/
Copy pathindex.html
File metadata and controls
445 lines (427 loc) · 18.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lexical Analyzer</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<style>
body {
font-family: 'Fira Code', monospace;
background: linear-gradient(135deg, #1e1e2e 0%, #2e2e4e 100%);
color: #cdd6f4;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
}
.container {
background: linear-gradient(145deg, #2e2e3e, #35354e);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
padding: 2.5rem;
max-width: 1400px;
width: 100%;
margin: 2rem auto;
}
.code-editor {
background: #1a1a2a;
border: 2px solid #45455a;
border-radius: 12px;
padding: 1.5rem;
color: #cdd6f4;
width: 100%;
height: 400px;
resize: none;
font-family: 'Fira Code', monospace;
font-size: 16px;
line-height: 1.6;
transition: border-color 0.3s ease;
}
.code-editor:focus {
outline: none;
border-color: #89b4fa;
box-shadow: 0 0 8px rgba(137, 180, 250, 0.5);
}
.code-display {
background: #1a1a2a;
border: 2px solid #45455a;
border-radius: 12px;
padding: 1.5rem;
color: #cdd6f4;
min-height: 400px;
white-space: pre-wrap;
word-wrap: break-word;
font-family: 'Fira Code', monospace;
font-size: 16px;
line-height: 1.6;
transition: border-color 0.3s ease;
}
.cursor {
display: inline-block;
width: 2px;
height: 1.2em;
background: #89b4fa;
vertical-align: middle;
animation: blink 0.8s step-end infinite;
}
@keyframes blink {
50% { opacity: 0; }
}
.char {
display: inline-block;
}
/* Token-specific highlight colors for code display */
.token-highlight.token-keyword {
background: rgba(203, 166, 247, 0.4); /* Purple for keywords */
border-radius: 4px;
padding: 2px;
}
.token-highlight.token-identifier {
background: rgba(137, 180, 250, 0.4); /* Blue for identifiers */
border-radius: 4px;
padding: 2px;
}
.token-highlight.token-operator {
background: rgba(166, 227, 161, 0.4); /* Green for operators */
border-radius: 4px;
padding: 2px;
}
.token-highlight.token-number {
background: rgba(249, 226, 175, 0.4); /* Yellow for numbers */
border-radius: 4px;
padding: 2px;
}
.token-highlight.token-delimiter {
background: rgba(148, 226, 213, 0.4); /* Cyan for delimiters */
border-radius: 4px;
padding: 2px;
}
.token-highlight.token-parenthesis {
background: rgba(245, 194, 231, 0.4); /* Pink for parentheses */
border-radius: 4px;
padding: 2px;
}
.token-highlight.token-error {
background: rgba(243, 139, 168, 0.4); /* Red for errors */
border-radius: 4px;
padding: 2px;
}
.analyze-btn {
background: linear-gradient(90deg, #5850ec, #7f00ff);
color: #ffffff;
padding: 1rem 2.5rem;
border-radius: 12px;
font-weight: 700;
font-size: 16px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
position: relative;
}
.analyze-btn:hover {
transform: translateY(-3px);
box-shadow: 0 4px 16px rgba(88, 80, 236, 0.6);
}
.analyze-btn.loading::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 24px;
height: 24px;
border: 3px solid #ffffff;
border-top-color: transparent;
border-radius: 50%;
animation: spin 0.8s linear infinite;
transform: translate(-50%, -50%);
}
@keyframes spin {
to { transform: translate(-50%, -50%) rotate(360deg); }
}
.token-card {
background: #1a1a2a;
border-radius: 12px;
overflow-y: auto;
max-height: 400px;
border: 2px solid #45455a;
opacity: 0;
transform: translateY(20px);
transition: border-color 0.3s ease;
}
.token-table {
width: 100%;
border-collapse: collapse;
}
.token-table th, .token-table td {
padding: 1rem;
text-align: left;
border-bottom: 1px solid #45455a;
}
.token-table th {
background: #252535;
position: sticky;
top: 0;
z-index: 10;
font-weight: 600;
color: #89b4fa;
}
.token-row {
transition: background 0.3s ease;
}
.token-row:hover {
background: rgba(137, 180, 250, 0.2);
}
/* Token colors for table */
.token-keyword { color: #cba6f7; } /* Purple */
.token-identifier { color: #89b4fa; } /* Blue */
.token-operator { color: #a6e3a1; } /* Green */
.token-number { color: #f9e2af; } /* Yellow */
.token-delimiter { color: #94e2d5; } /* Cyan */
.token-parenthesis { color: #f5c2e7; } /* Pink */
.token-error { color: #f38ba8; } /* Red */
.title {
color: #89b4fa;
text-shadow: 0 0 10px rgba(137, 180, 250, 0.7);
font-size: 2.5rem;
font-weight: 800;
}
.scrollbar::-webkit-scrollbar {
width: 10px;
}
.scrollbar::-webkit-scrollbar-track {
background: #2e2e3e;
border-radius: 8px;
}
.scrollbar::-webkit-scrollbar-thumb {
background: #5850ec;
border-radius: 8px;
transition: background 0.3s ease;
}
.scrollbar::-webkit-scrollbar-thumb:hover {
background: #89b4fa;
}
@media (max-width: 768px) {
.container {
padding: 1.5rem;
}
.code-editor, .code-display {
height: 300px;
font-size: 14px;
}
.token-card {
max-height: 300px;
}
.title {
font-size: 2rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center title mb-10">Lexical Analyzer</h1>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<!-- Code Input -->
<div>
<label for="codeEditor" class="block text-sm font-medium mb-3 text-gray-300">Enter Your Code</label>
<textarea id="codeEditor" class="code-editor scrollbar" placeholder="Type your code here..."></textarea>
<button onclick="analyzeCode()" class="analyze-btn mt-6 w-full">Analyze</button>
</div>
<!-- Code Display and Tokens -->
<div>
<h2 class="text-2xl font-semibold mb-3 text-gray-300">Analysis Output</h2>
<div class="code-display scrollbar" id="codeDisplay"></div>
<h2 class="text-2xl font-semibold mt-6 mb-3 text-gray-300">Tokens</h2>
<div class="token-card scrollbar">
<table class="token-table">
<thead>
<tr>
<th>Token</th>
<th>Type</th>
</tr>
</thead>
<tbody id="tokenOutput"></tbody>
</table>
</div>
</div>
</div>
</div>
<script>
// Animation speed parameters (adjust these to control highlighting speed)
const CHAR_ANIMATION_DURATION = 0.3; // Seconds for each character fade-in
const TOKEN_DELAY_PER_CHAR = 100; // Milliseconds per character for token transition
const HIGHLIGHT_PULSE_DURATION = 0.3; // Seconds for scale pulse effect
// Initial GSAP Animations
gsap.from('.container', { opacity: 0, y: 30, duration: 1.2, ease: 'power3.out' });
gsap.from('.code-editor', { opacity: 0, x: -40, duration: 1, delay: 0.3, ease: 'power2.out' });
gsap.from('.analyze-btn', { opacity: 0, y: 20, duration: 1, delay: 0.5, ease: 'power2.out' });
gsap.from('.code-display', { opacity: 0, x: 50, duration: 2, delay: 0.14, ease: 'power2.out' });
gsap.from('.token-card', { opacity: 0, y: 20, duration: 1, delay: 0.9, ease: 'power2.out' });
const codeEditor = document.getElementById('codeEditor');
const codeDisplay = document.getElementById('codeDisplay');
const tokenOutput = document.getElementById('tokenOutput');
async function analyzeCode() {
console.log('Analyze button clicked');
const code = codeEditor.value.trim();
const button = document.querySelector('.analyze-btn');
button.classList.add('loading');
button.disabled = true;
codeDisplay.innerHTML = '';
tokenOutput.innerHTML = '';
gsap.to('.token-card', { opacity: 0, y: 20, duration: 0.3 });
try {
if (!code) {
console.log('Empty input detected');
displayToken({ token: 'No input', type: 'error' }, 0);
throw new Error('Empty input');
}
console.log('Sending request to /analyze with code:', code);
// Fetch tokens from backend
const response = await fetch('/analyze', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code })
});
if (!response.ok) {
console.error('Fetch error:', response.status, response.statusText);
throw new Error(`HTTP error: ${response.status}`);
}
const tokens = await response.json();
console.log('Backend tokens:', tokens);
// Animate code character by character, following backend tokens
let currentIndex = 0; // Tracks position in input code
let tokenIndex = 0; // Tracks current backend token
let charSpans = [];
let tokenCharIndices = []; // Tracks indices of token characters
const cursor = document.createElement('span');
cursor.className = 'cursor';
codeDisplay.appendChild(cursor);
function animateNextToken() {
try {
// Process backend tokens
if (tokenIndex < tokens.length) {
// Skip whitespace before the next token
while (currentIndex < code.length && /\s/.test(code[currentIndex])) {
const char = code[currentIndex];
const span = document.createElement('span');
span.className = 'char';
span.textContent = char === '\n' ? '\n' : char;
codeDisplay.insertBefore(span, cursor);
charSpans.push(span);
gsap.from(span, {
opacity: 0,
x: -5,
duration: CHAR_ANIMATION_DURATION,
ease: 'power2.out'
});
currentIndex++;
}
const { token, type } = tokens[tokenIndex];
console.log(`Processing token: ${token}, type: ${type}`);
// Track start index for this token
const tokenStartIndex = charSpans.length;
// Animate each character in the token
for (let i = 0; i < token.length; i++) {
if (currentIndex >= code.length) {
throw new Error('Index out of bounds');
}
const char = code[currentIndex];
const span = document.createElement('span');
span.className = 'char';
span.textContent = char === '\n' ? '\n' : char;
codeDisplay.insertBefore(span, cursor);
charSpans.push(span);
gsap.from(span, {
opacity: 0,
x: -5,
duration: CHAR_ANIMATION_DURATION,
ease: 'power2.out'
});
currentIndex++;
}
// Record token character indices
tokenCharIndices.push({
start: tokenStartIndex,
end: charSpans.length,
token,
type
});
// Highlight the token and add to table
checkToken(token, type, tokenStartIndex, charSpans.length);
tokenIndex++;
setTimeout(animateNextToken, token.length * TOKEN_DELAY_PER_CHAR);
} else {
// Handle remaining characters (whitespace)
while (currentIndex < code.length) {
const char = code[currentIndex];
const span = document.createElement('span');
span.className = 'char';
span.textContent = char === '\n' ? '\n' : char;
codeDisplay.insertBefore(span, cursor);
charSpans.push(span);
gsap.from(span, {
opacity: 0,
x: -5,
duration: CHAR_ANIMATION_DURATION,
ease: 'power2.out'
});
currentIndex++;
}
cursor.remove();
gsap.to('.token-card', { opacity: 1, y: 0, duration: 0.5, ease: 'power2.out' });
button.classList.remove('loading');
button.disabled = false;
}
} catch (error) {
console.error('Animation error:', error);
displayToken({ token: 'Animation Error', type: 'error' }, tokenOutput.childElementCount);
cursor.remove();
gsap.to('.token-card', { opacity: 1, y: 0, duration: 0.5, ease: 'power2.out' });
button.classList.remove('loading');
button.disabled = false;
}
}
function checkToken(token, type, startIndex, endIndex) {
console.log(`Highlighting token: ${token}, type: ${type}, start: ${startIndex}, end: ${endIndex}`);
for (let i = startIndex; i < endIndex && i < charSpans.length; i++) {
charSpans[i].classList.add('token-highlight', `token-${type}`);
gsap.to(charSpans[i], {
scale: 1.1,
duration: HIGHLIGHT_PULSE_DURATION,
repeat: 1,
yoyo: true,
ease: 'power2.inOut'
});
}
displayToken({ token, type }, tokenOutput.childElementCount);
}
function displayToken({ token, type }, index) {
console.log(`Displaying token: ${token}, type: ${type}`);
const row = document.createElement('tr');
row.className = 'token-row';
row.innerHTML = `
<td class="token-${type}">${token}</td>
<td>${type.charAt(0).toUpperCase() + type.slice(1)}</td>
`;
tokenOutput.appendChild(row);
gsap.from(row, {
opacity: 0,
y: 10,
duration: 0.4,
delay: index * 0.05,
ease: 'power2.out'
});
}
animateNextToken();
} catch (error) {
console.error('Analysis error:', error);
displayToken({ token: 'Analysis Error', type: 'error' }, 0);
gsap.to('.token-card', { opacity: 1, y: 0, duration: 0.5, ease: 'power2.out' });
button.classList.remove('loading');
button.disabled = false;
}
}
</script>
</body>
</html>