vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
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
/**
 * VB6Parse Playground - Tree Visualization Module
 * 
 * Handles D3.js tree visualization rendering and interactions.
 * Creates an interactive visual representation of the CST.
 * 
 * TODO: Implement D3 tree layout and rendering
 * TODO: Add zoom and pan functionality
 * TODO: Implement node click interactions
 * TODO: Add layout toggle (horizontal/vertical)
 */

let svg = null;
let g = null;
let tree = null;
let root = null;
let currentLayout = 'vertical'; // 'vertical' or 'horizontal'
let zoom = null;

// Tree configuration
const config = {
    nodeRadius: 6,
    verticalSpacing: 60,
    horizontalSpacing: 120,
    transitionDuration: 750,
    maxLabelLength: 20
};

/**
 * Initialize the tree visualization
 * @param {string} containerId - Container element ID
 */
export function initTreeViz(containerId) {
    const container = document.getElementById(containerId);
    if (!container) {
        console.error(`Container ${containerId} not found`);
        return;
    }

    // Clear container
    container.innerHTML = '';

    // Create SVG
    const width = container.clientWidth;
    const height = container.clientHeight;

    svg = d3.select(container)
        .append('svg')
        .attr('id', 'tree-viz-svg')
        .attr('width', width)
        .attr('height', height);

    // Create zoom behavior
    zoom = d3.zoom()
        .scaleExtent([0.1, 3])
        .on('zoom', (event) => {
            g.attr('transform', event.transform);
        });

    svg.call(zoom);

    // Create main group for tree
    g = svg.append('g')
        .attr('class', 'tree-group')
        .attr('transform', `translate(${width / 2}, 50)`);

    console.log('✅ Tree visualization initialized');
}

/**
 * Render CST as a tree visualization
 * @param {CstNode} cst - Root CST node
 * 
 * TODO: Implement actual D3 tree rendering
 * This is a skeleton that needs D3.js implementation
 */
export function renderTree(cst) {
    if (!svg || !g) {
        console.error('Tree visualization not initialized');
        return;
    }

    console.log('🔧 TODO: Render D3 tree from CST:', cst);

    // TODO: Convert CST to D3 hierarchy
    // const hierarchy = d3.hierarchy(convertCstToD3Format(cst));
    
    // TODO: Create tree layout
    // const treeLayout = createTreeLayout();
    // treeLayout(hierarchy);
    
    // TODO: Render nodes and links
    // renderNodes(hierarchy.descendants());
    // renderLinks(hierarchy.links());

    // Placeholder visualization
    renderPlaceholder();
}

/**
 * Convert CST node to D3 hierarchy format
 * @param {CstNode} node - CST node
 * @returns {object} D3 hierarchy data
 * 
 * TODO: Implement full conversion
 */
function convertCstToD3Format(node) {
    return {
        name: node.type,
        value: node.value,
        range: node.range,
        children: node.children ? node.children.map(convertCstToD3Format) : undefined
    };
}

/**
 * Create tree layout based on current orientation
 * @returns {d3.tree} D3 tree layout
 * 
 * TODO: Implement layout configuration
 */
function createTreeLayout() {
    const container = document.getElementById('tree-viz-container');
    const width = container.clientWidth;
    const height = container.clientHeight;

    if (currentLayout === 'vertical') {
        return d3.tree()
            .size([width - 100, height - 100])
            .separation((a, b) => a.parent === b.parent ? 1 : 2);
    } else {
        return d3.tree()
            .size([height - 100, width - 100])
            .separation((a, b) => a.parent === b.parent ? 1 : 2);
    }
}

/**
 * Render tree nodes
 * @param {Array} nodes - D3 hierarchy nodes
 * 
 * TODO: Implement node rendering with D3
 */
function renderNodes(nodes) {
    // TODO: Implement D3 node rendering
    // - Bind data
    // - Create node groups
    // - Add circles
    // - Add text labels
    // - Add click handlers
    // - Add hover effects
}

/**
 * Render tree links (edges)
 * @param {Array} links - D3 hierarchy links
 * 
 * TODO: Implement link rendering with D3
 */
function renderLinks(links) {
    // TODO: Implement D3 link rendering
    // - Bind data
    // - Create paths
    // - Use curved or straight lines
    // - Add animations
}

/**
 * Render placeholder when no tree data
 */
function renderPlaceholder() {
    g.selectAll('*').remove();
    
    g.append('text')
        .attr('text-anchor', 'middle')
        .attr('fill', 'var(--placeholder-color)')
        .attr('font-size', '18px')
        .attr('y', 100)
        .text('🌳 Tree visualization coming soon!');
    
    g.append('text')
        .attr('text-anchor', 'middle')
        .attr('fill', 'var(--placeholder-color)')
        .attr('font-size', '14px')
        .attr('y', 130)
        .text('This will show an interactive D3.js tree of the parsed code');
}

/**
 * Toggle tree layout between vertical and horizontal
 */
export function toggleLayout() {
    currentLayout = currentLayout === 'vertical' ? 'horizontal' : 'vertical';
    
    // Update UI icon
    const icon = document.getElementById('layout-icon');
    if (icon) {
        icon.textContent = currentLayout === 'vertical' ? '↔️' : '↕️';
    }

    // Re-render tree with new layout
    if (root) {
        renderTree(root);
    }

    console.log(`🔄 Switched to ${currentLayout} layout`);
}

/**
 * Fit tree to screen
 */
export function fitToScreen() {
    if (!svg || !g) return;

    // TODO: Calculate bounding box and zoom to fit
    const bounds = g.node().getBBox();
    const parent = svg.node().parentElement;
    const fullWidth = parent.clientWidth;
    const fullHeight = parent.clientHeight;
    const width = bounds.width;
    const height = bounds.height;
    const midX = bounds.x + width / 2;
    const midY = bounds.y + height / 2;

    if (width === 0 || height === 0) return;

    // Calculate scale to fit
    const scale = 0.9 / Math.max(width / fullWidth, height / fullHeight);
    const translate = [
        fullWidth / 2 - scale * midX,
        fullHeight / 2 - scale * midY
    ];

    // Apply transform with animation
    svg.transition()
        .duration(750)
        .call(zoom.transform, d3.zoomIdentity
            .translate(translate[0], translate[1])
            .scale(scale));

    console.log('📐 Fitted tree to screen');
}

/**
 * Reset zoom to default
 */
export function resetZoom() {
    if (!svg || !zoom) return;

    const container = document.getElementById('tree-viz-container');
    const width = container.clientWidth;
    const height = container.clientHeight;

    svg.transition()
        .duration(750)
        .call(zoom.transform, d3.zoomIdentity
            .translate(width / 2, 50)
            .scale(1));

    console.log('🔄 Reset zoom');
}

/**
 * Handle node click
 * @param {object} nodeData - D3 node data
 * 
 * TODO: Implement node selection and details display
 */
function handleNodeClick(nodeData) {
    console.log('Node clicked:', nodeData);
    
    // TODO: Show node details in sidebar
    // TODO: Highlight corresponding code in editor
    // TODO: Highlight node visually
}

/**
 * Show node details in sidebar
 * @param {object} nodeData - D3 node data
 * 
 * TODO: Implement details panel
 */
function showNodeDetails(nodeData) {
    const detailsPanel = document.getElementById('tree-node-details');
    const detailsContent = document.getElementById('node-details-content');
    
    if (!detailsPanel || !detailsContent) return;

    detailsContent.innerHTML = `
        <p><strong>Type:</strong> ${nodeData.name}</p>
        ${nodeData.value ? `<p><strong>Value:</strong> ${nodeData.value}</p>` : ''}
        ${nodeData.range ? `<p><strong>Range:</strong> [${nodeData.range[0]}..${nodeData.range[1]}]</p>` : ''}
        <p><strong>Depth:</strong> ${nodeData.depth}</p>
        <p><strong>Children:</strong> ${nodeData.children ? nodeData.children.length : 0}</p>
    `;

    detailsPanel.classList.remove('hidden');
}

/**
 * Clear tree visualization
 */
export function clearTree() {
    if (!g) return;
    
    g.selectAll('*').remove();
    root = null;
    
    renderPlaceholder();
}

/**
 * Get tree statistics
 * @param {CstNode} cst - Root CST node
 * @returns {object} Tree statistics
 */
export function getTreeStats(cst) {
    let nodeCount = 0;
    let maxDepth = 0;

    function traverse(node, depth) {
        nodeCount++;
        maxDepth = Math.max(maxDepth, depth);

        if (node.children) {
            node.children.forEach(child => traverse(child, depth + 1));
        }
    }

    traverse(cst, 0);

    return {
        nodeCount,
        maxDepth
    };
}

/**
 * Export tree as SVG
 * TODO: Implement SVG export
 */
export function exportAsSvg() {
    console.log('🔧 TODO: Export tree as SVG');
    // Get SVG element and serialize
    // Offer download to user
}

/**
 * Export tree as PNG
 * TODO: Implement PNG export using html2canvas or similar
 */
export function exportAsPng() {
    console.log('🔧 TODO: Export tree as PNG');
    // Convert SVG to PNG
    // Offer download to user
}

/**
 * TODO: D3.js Implementation Checklist
 * 
 * 1. Tree Layout:
 *    - Implement d3.hierarchy() conversion
 *    - Configure d3.tree() layout
 *    - Handle vertical and horizontal layouts
 *    - Optimize for large trees (>500 nodes)
 * 
 * 2. Nodes:
 *    - Draw circles with appropriate colors
 *    - Add text labels (truncated if needed)
 *    - Implement hover effects
 *    - Add click handlers
 *    - Show collapse/expand indicators
 * 
 * 3. Links:
 *    - Use curved paths (d3.linkVertical/linkHorizontal)
 *    - Add hover effects
 *    - Color code by node type
 * 
 * 4. Interactions:
 *    - Zoom and pan
 *    - Node selection
 *    - Collapse/expand nodes
 *    - Tooltip on hover
 *    - Highlight path from root to node
 * 
 * 5. Performance:
 *    - Virtualization for large trees
 *    - Lazy loading of subtrees
 *    - Disable animations for >500 nodes
 *    - Use canvas for >1000 nodes
 * 
 * 6. Styling:
 *    - Node colors by type (statement, expression, literal, keyword)
 *    - Highlight selected nodes
 *    - Theme support (light/dark)
 *    - Responsive sizing
 * 
 * 7. Export:
 *    - SVG download
 *    - PNG download
 *    - JSON export of tree data
 */

export default {
    initTreeViz,
    renderTree,
    toggleLayout,
    fitToScreen,
    resetZoom,
    clearTree,
    getTreeStats,
    exportAsSvg,
    exportAsPng
};