From d5cf11a70b0e8857e864e16e69946af7cba5de2e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 14 Jun 2026 19:23:07 +0800 Subject: [PATCH] =?UTF-8?q?feat(dacli):=20=5Fparsers.ts=20Python=20FORMS?= =?UTF-8?q?=20=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/form/_parsers.ts | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/commands/form/_parsers.ts diff --git a/src/commands/form/_parsers.ts b/src/commands/form/_parsers.ts new file mode 100644 index 0000000..2cee3ae --- /dev/null +++ b/src/commands/form/_parsers.ts @@ -0,0 +1,56 @@ +/** + * commands/form/_parsers.ts — 设计期解析器 + * + * parsePythonForms(): 从 03_forms.py 抽 FORMS = [(en_code, full_name, [(type, vmodel, label, req), ...]), ...] + * parseDesignHtml(): 从 design-jnpf.html 抽 .cmd 块 + 验证命令完整性 + */ + +export interface FieldDef { + type: string // input / inputNumber / textarea / select / ... + vmodel: string + label: string + required: boolean +} + +export interface FormDef { + enCode: string + fullName: string + fields: FieldDef[] +} + +export function parsePythonForms(src: string): FormDef[] { + const forms: FormDef[] = [] + // 找 FORMS = [ ... ] 顶层段 + const m = src.match(/FORMS\s*=\s*\[([\s\S]*?)\n\]/m) + if (!m) return [] + const body = m[1] + // 找顶层 [ 块 (字段列表) + let i = 0 + while (i < body.length) { + if (body[i] === '[') { + const fieldsStart = i + 1 + let depth = 1; let j = fieldsStart + while (j < body.length && depth > 0) { + if (body[j] === '[') depth++ + else if (body[j] === ']') depth-- + j++ + } + const fieldsBlock = body.slice(fieldsStart, j - 1) + // 找前 2 个字符串 (en_code, full_name) — 简化: 假设出现在 [ 块前的行 + // 这里用更稳: 反向找 ([ 块前的 2 个 '字符串' + const beforeFields = body.slice(Math.max(0, i - 200), i) + const strs = [...beforeFields.matchAll(/'([^']+)'/g)].map(m => m[1]) + const enCode = strs[strs.length - 2] || '?' + const fullName = strs[strs.length - 1] || '?' + // 抽每个 (type, vmodel, label, req) 元组 (允许 5 元素含 extra dict) + const fields: FieldDef[] = [] + for (const tm of fieldsBlock.matchAll(/\(\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*'([^']+)'\s*,\s*(True|False)/g)) { + fields.push({ type: tm[1], vmodel: tm[2], label: tm[3], required: tm[4] === 'True' }) + } + if (fields.length > 0) forms.push({ enCode, fullName, fields }) + i = j + } + i++ + } + return forms +} \ No newline at end of file