vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
/**
 * VB6Parse Playground - Parser Module
 * 
 * Handles WASM module loading and provides wrapper functions for parsing VB6 code.
 * This is the bridge between the editor and the WASM parser.
 * 
 * TODO: Load and initialize WASM module
 * TODO: Implement parse functions that call WASM
 * TODO: Add error handling for WASM failures
 */

import init, { tokenize_vb6_code, parse_vb6_code } from "../../wasm/vb6parse.js";

let wasmInitialized = false;

/**
 * Initialize the WASM module
 * This should be called on page load
 * 
 * @returns {Promise<boolean>} True if initialization succeeded
 * 
 * TODO: Replace placeholder with actual WASM module loading
 * Example using wasm-bindgen output:
 * 
 * import init, { parse_vb6_code, tokenize_vb6_code } from '../wasm/vb6parse.js';
 * 
 * export async function initWasm() {
 *     try {
 *         await init();
 *         wasmInitialized = true;
 *         return true;
 *     } catch (error) {
 *         console.error('Failed to initialize WASM:', error);
 *         return false;
 *     }
 * }
 */
export async function initWasm() {
    const response = await fetch('assets/wasm/vb6parse_bg.wasm');
    const wasmBytes = await response.arrayBuffer();
    await init(wasmBytes); // This initializes the wasm module
    wasmInitialized = true;
    return wasmInitialized;
}

/**
 * Check if WASM is initialized
 * @returns {boolean}
 */
export function isWasmReady() {
    return wasmInitialized;
}

/**
 * Parse VB6 code and return full parse result
 * 
 * @param {string} code - VB6 source code
 * @param {string} fileType - 'module', 'class', 'form', or 'project'
 * @returns {Promise<ParseResult>} Parse result object
 * 
 * Example return structure:
 * {
 *     tokens: TokenInfo[],
 *     cst: CstNode,
 *     errors: ErrorInfo[],
 *     warnings: WarningInfo[],
 *     parseTimeMs: number,
 *     stats: {
 *         tokenCount: number,
 *         nodeCount: number,
 *         treeDepth: number
 *     }
 * }
 */
export async function parseCode(code, fileType) {
    if (!wasmInitialized) {
        throw new Error('WASM module not initialized');
    }

    const startTime = performance.now();

    try {
        
        const parseResult = parse_vb6_code(code, fileType);
        const parseTime = performance.now() - startTime;
        parseResult.parseTimeMs = parseTime;

        console.log(` Parsed ${fileType} in ${parseTime.toFixed(2)}ms`);
        return parseResult;

    } catch (error) {
        console.error('Parse error:', error);
        throw new Error(`Failed to parse ${fileType}: ${error.message}`);
    }
}

/**
 * Tokenize VB6 code (faster than full parse)
 * 
 * @param {string} code - VB6 source code
 * @returns {Promise<TokenInfo[]>} Array of tokens
 * 
 * TODO: Replace with actual WASM call
 */
export async function tokenizeCode(code) {
    if (!wasmInitialized) {
        throw new Error('WASM module not initialized');
    }

    try {
        // TODO: Call actual WASM tokenize function
        // const tokens = tokenize_vb6_code(code);
        
        return await tokenize_vb6_code(code);

    } catch (error) {
        console.error('Tokenize error:', error);
        throw error;
    }
}

/**
 * Create mock CST for testing UI
 * TODO: Remove when WASM is integrated
 */
function createMockCst(code, fileType) {
    return {
        kind: 'CompilationUnit',
        range: [0, code.length],
        children: [
            {
                kind: 'VersionStatement',
                range: [0, 20],
                children: [
                    { kind: 'Keyword', value: 'VERSION', range: [0, 7] },
                    { kind: 'Whitespace', value: ' ', range: [7, 8] },
                    { kind: 'Number', value: '1.0', range: [8, 11] }
                ]
            },
            {
                kind: 'OptionStatement',
                range: [21, 36],
                children: [
                    { kind: 'Keyword', value: 'Option', range: [21, 27] },
                    { kind: 'Whitespace', value: ' ', range: [27, 28] },
                    { kind: 'Keyword', value: 'Explicit', range: [28, 36] }
                ]
            }
        ]
    };
}

/**
 * Type definitions (for documentation)
 * 
 * @typedef {Object} TokenInfo
 * @property {string} kind - Token kind: 'keyword', 'identifier', 'literal', 'operator', 'comment', 'whitespace'
 * @property {string} content - Token text content
 * @property {number} line - Line number (1-based)
 * @property {number} column - Column number (1-based)
 * @property {number} length - Token length in characters
 * 
 * @typedef {Object} CstNode
 * @property {string} kind - Node kind (e.g., 'CompilationUnit', 'SubDeclaration')
 * @property {number[]} range - [start, end] byte offsets
 * @property {string} [value] - Node value for leaf nodes
 * @property {CstNode[]} [children] - Child nodes
 * 
 * @typedef {Object} ErrorInfo
 * @property {string} kind - Error kind
 * @property {string} message - Error message
 * @property {number} line - Line number
 * @property {number} column - Column number
 * @property {number[]} range - [start, end] offsets
 * 
 * @typedef {Object} ParseResult
 * @property {TokenInfo[]} tokens - All tokens
 * @property {CstNode} cst - Root CST node
 * @property {ErrorInfo[]} errors - Parse errors
 * @property {ErrorInfo[]} warnings - Parse warnings
 * @property {number} parseTimeMs - Parse time in milliseconds
 * @property {Object} stats - Parse statistics
 * @property {number} stats.tokenCount - Total token count
 * @property {number} stats.nodeCount - Total CST node count
 * @property {number} stats.treeDepth - Maximum tree depth
 */

/**
 * TODO: WASM Integration Checklist
 * 
 * 1. Build WASM module:
 *    - Run: python scripts/build-wasm.py --optimize
 *    - Verify output in docs/assets/wasm/
 * 
 * 2. Import WASM module:
 *    import init, { parse_vb6_code, tokenize_vb6_code } from '../wasm/vb6parse.js';
 * 
 * 3. Call WASM functions:
 *    - parse_vb6_code(code, file_type) -> returns JsValue (parse result)
 *    - tokenize_vb6_code(code) -> returns JsValue (tokens array)
 * 
 * 4. Handle WASM types:
 *    - Use serde-wasm-bindgen to convert between JS and Rust types
 *    - Ensure proper error handling for panics
 * 
 * 5. Error handling:
 *    - Catch WASM exceptions
 *    - Display user-friendly error messages
 *    - Log detailed errors to console
 * 
 * 6. Performance:
 *    - Consider using Web Workers for large files
 *    - Cache parsed results if code hasn't changed
 *    - Show loading indicator for long operations
 */

export default {
    initWasm,
    isWasmReady,
    parseCode,
    tokenizeCode
};