mixone
Version:
MixOne is a Node scaffolding tool implemented based on Vite, used for compiling HTML5, JavasCript, Vue, React and other codes. It supports packaging Web applications with multiple HTML entry points (BS architecture) and desktop installation packages (CS a
192 lines (158 loc) • 6.67 kB
JavaScript
const fs = require('fs');
const path = require('path');
/**
* 语法糖编译器 V2 - 正则快速定位版本
* 采用正则表达式快速定位语法糖位置
*/
class SugarCompilerV2 {
constructor() {
this.stats = {
regexTime: 0,
processedMatches: 0
};
// 语法糖正则模式
this.patterns = {
// Main语法糖模式
main: /\bMain\.([\w.]+)(?:\((.*?)\))?/g,
// NodeJS语法糖模式
nodejs: /\bNodeJS\.([\w.]+)(?:\((.*?)\))?/g,
// PJS语法糖模式
pjs: /\bPJS\.([\w.]+)(?:\((.*?)\))?/g
};
}
/**
* 正则预处理 - 快速定位所有可能的语法糖
*/
regexPreprocess(code) {
const startTime = Date.now();
console.log('开始正则预处理...');
const matches = [];
const lines = code.split('\n');
// 遍历每种语法糖模式
Object.entries(this.patterns).forEach(([type, pattern]) => {
let match;
const globalPattern = new RegExp(pattern.source, 'g');
while ((match = globalPattern.exec(code)) !== null) {
// 计算行号
const beforeMatch = code.substring(0, match.index);
const lineNumber = beforeMatch.split('\n').length;
const lineStart = beforeMatch.lastIndexOf('\n') + 1;
const columnNumber = match.index - lineStart + 1;
// 获取上下文(当前行)
const currentLine = lines[lineNumber - 1] || '';
// 解析匹配信息
const fullMatch = match[0];
const methodPath = match[1] || '';
const params = match[2] || '';
const matchInfo = {
type: type.toUpperCase(),
keyword: type === 'main' ? 'Main' : type === 'nodejs' ? 'NodeJS' : 'PJS',
path: methodPath,
params: params,
line: lineNumber,
column: columnNumber,
startIndex: match.index,
endIndex: match.index + fullMatch.length,
originalText: fullMatch,
context: currentLine.trim(),
fullLine: currentLine
};
matches.push(matchInfo);
}
});
// 按行号排序
matches.sort((a, b) => a.line - b.line || a.column - b.column);
this.stats.regexTime = Date.now() - startTime;
console.log(`正则预处理完成: 发现 ${matches.length} 个潜在语法糖`);
return matches;
}
/**
* 主处理方法 - 仅使用正则快速定位
*/
process(code) {
console.log('开始语法糖编译 V2 - 正则快速定位模式...');
const startTime = Date.now();
// 第一步:正则预处理
const regexMatches = this.regexPreprocess(code);
// 暂时跳过AST分析,直接返回正则结果
const result = {
matches: regexMatches,
stats: {
...this.stats,
totalTime: Date.now() - startTime,
regexMatches: regexMatches.length,
validMatches: regexMatches.length
}
};
console.log(`\n=== 处理完成 ===`);
console.log(`总耗时: ${result.stats.totalTime}ms`);
console.log(`正则匹配: ${result.stats.regexMatches} 个`);
console.log(`有效匹配: ${result.stats.validMatches} 个`);
return result;
}
/**
* 生成详细报告
*/
generateReport(matches) {
const report = {
summary: {
total: matches.length,
byType: {},
byKeyword: {}
},
details: matches
};
// 统计各类型数量
matches.forEach(match => {
report.summary.byType[match.type] = (report.summary.byType[match.type] || 0) + 1;
report.summary.byKeyword[match.keyword] = (report.summary.byKeyword[match.keyword] || 0) + 1;
});
return report;
}
}
/**
* 测试函数
*/
function testSugarCompilerV2() {
console.log('=== 语法糖编译器 V2 测试 - 正则快速定位 ===\n');
const srcPath = path.join(__dirname, 'sugar_code.js');
if (!fs.existsSync(srcPath)) {
console.error(`测试文件不存在: ${srcPath}`);
return;
}
try {
const sugarCodeContent = fs.readFileSync(srcPath, 'utf8');
const compiler = new SugarCompilerV2();
const result = compiler.process(sugarCodeContent);
console.log('\n=== 详细结果 ===');
result.matches.forEach((match, index) => {
console.log(`${index + 1}. [${match.type}] ${match.keyword}.${match.path} (第${match.line}行:${match.column}列)`);
console.log(` 参数: ${match.params || '无'}`);
console.log(` 上下文: ${match.context}`);
console.log(` 原始代码: ${match.originalText}`);
console.log('');
});
// 生成报告
const report = compiler.generateReport(result.matches);
console.log('=== 类型统计 ===');
Object.entries(report.summary.byType).forEach(([type, count]) => {
console.log(`${type}: ${count} 个`);
});
console.log('\n=== 关键字统计 ===');
Object.entries(report.summary.byKeyword).forEach(([keyword, count]) => {
console.log(`${keyword}: ${count} 个`);
});
console.log('\n=== 性能统计 ===');
console.log(`正则处理时间: ${result.stats.regexTime}ms`);
console.log(`总处理时间: ${result.stats.totalTime}ms`);
console.log(`处理效率: ${(result.matches.length / result.stats.totalTime * 1000).toFixed(2)} 个/秒`);
} catch (error) {
console.error('测试失败:', error.message);
console.error(error.stack);
}
}
// 如果直接运行此文件,执行测试
if (require.main === module) {
testSugarCompilerV2();
}
module.exports = { SugarCompilerV2 };