feat(dacli): _parsers.ts Python FORMS 解析器

This commit is contained in:
Claude committed 2026-06-14 19:23:07 +08:00
1 parent b8c2c92af8
commit d5cf11a70b
1 file changed
+56
+56
View File
@@ -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
}