mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
refactor(firmware): share SVD child text projection (#16634)
* refactor(firmware): share SVD child text projection * test(xml): cover first named child text
This commit is contained in:
1 parent
7815ed620c
commit
ced3d7837c
5 files changed
+54
-57
No files matched your search
@@ -9,6 +9,7 @@ import {
|
||||
parseXmlDocumentWithinNodeLimit,
|
||||
xmlChildrenNamed,
|
||||
xmlFirstChildNamed,
|
||||
xmlFirstChildTrimmedText,
|
||||
} from "../xmlDocument.js";
|
||||
import {
|
||||
DEFAULT_SVD_XML_NODES,
|
||||
@@ -111,12 +112,6 @@ interface RawSvdPeripheral {
|
||||
const CLASSIFICATION_CACHE_CAPACITY = 65_536;
|
||||
const SVD_COOPERATIVE_STEP = 16_384;
|
||||
|
||||
const trimmedText = (element: Element | undefined): string | null => {
|
||||
if (element === undefined) return null;
|
||||
const text = (element.textContent ?? "").trim();
|
||||
return text.length > 0 ? `${text}` : null;
|
||||
};
|
||||
|
||||
const stripLeadingZeros = (digits: string): string => {
|
||||
let idx = 0;
|
||||
while (idx < digits.length - 1 && digits.charCodeAt(idx) === 0x30) {
|
||||
@@ -149,11 +144,11 @@ const extractAddressBlockDescriptors = (
|
||||
const blocks: RelativeSpanDescriptor[] = [];
|
||||
for (const block of xmlChildrenNamed(peripheralEl, "addressBlock")) {
|
||||
const usage = (
|
||||
trimmedText(xmlFirstChildNamed(block, "usage")) ?? ""
|
||||
xmlFirstChildTrimmedText(block, "usage") ?? ""
|
||||
).toLowerCase();
|
||||
if (usage === "reserved") continue;
|
||||
const offsetText = trimmedText(xmlFirstChildNamed(block, "offset"));
|
||||
const sizeText = trimmedText(xmlFirstChildNamed(block, "size"));
|
||||
const offsetText = xmlFirstChildTrimmedText(block, "offset");
|
||||
const sizeText = xmlFirstChildTrimmedText(block, "size");
|
||||
if (offsetText === null || sizeText === null) continue;
|
||||
const blockOffset = parseUnsigned32(offsetText);
|
||||
const blockSize = parseUnsigned32(sizeText);
|
||||
@@ -179,20 +174,20 @@ const trimmedAttrCaseInsensitive = (
|
||||
};
|
||||
|
||||
const parseRegisterTemplate = (regEl: Element): SvdRegItemTemplate | null => {
|
||||
const offsetText = trimmedText(xmlFirstChildNamed(regEl, "addressOffset"));
|
||||
const offsetText = xmlFirstChildTrimmedText(regEl, "addressOffset");
|
||||
if (offsetText === null) return null;
|
||||
const relOffset = parseUnsigned32(offsetText);
|
||||
if (relOffset === null) return null;
|
||||
const name = trimmedText(xmlFirstChildNamed(regEl, "name"));
|
||||
const name = xmlFirstChildTrimmedText(regEl, "name");
|
||||
const derivedFrom = trimmedAttrCaseInsensitive(regEl, "derivedfrom");
|
||||
const ownSizeText = trimmedText(xmlFirstChildNamed(regEl, "size"));
|
||||
const ownSizeText = xmlFirstChildTrimmedText(regEl, "size");
|
||||
const ownSizeBits =
|
||||
ownSizeText !== null ? parseUnsigned32(ownSizeText) : null;
|
||||
if (ownSizeText !== null && (ownSizeBits === null || ownSizeBits === 0)) {
|
||||
return null;
|
||||
}
|
||||
const dimText = trimmedText(xmlFirstChildNamed(regEl, "dim"));
|
||||
const dimIndex = trimmedText(xmlFirstChildNamed(regEl, "dimIndex"));
|
||||
const dimText = xmlFirstChildTrimmedText(regEl, "dim");
|
||||
const dimIndex = xmlFirstChildTrimmedText(regEl, "dimIndex");
|
||||
if (dimText === null) {
|
||||
return {
|
||||
kind: "register",
|
||||
@@ -209,7 +204,7 @@ const parseRegisterTemplate = (regEl: Element): SvdRegItemTemplate | null => {
|
||||
}
|
||||
const dim = parseUnsigned32(dimText);
|
||||
if (dim === null || dim === 0) return null;
|
||||
const incText = trimmedText(xmlFirstChildNamed(regEl, "dimIncrement"));
|
||||
const incText = xmlFirstChildTrimmedText(regEl, "dimIncrement");
|
||||
const dimIncrement = incText !== null ? parseUnsigned32(incText) : null;
|
||||
if (incText !== null && (dimIncrement === null || dimIncrement === 0)) {
|
||||
return null;
|
||||
@@ -298,13 +293,11 @@ function* parseRegisterTemplatesSteps(
|
||||
continue;
|
||||
}
|
||||
if (child.localName !== "cluster") continue;
|
||||
const offsetText = trimmedText(
|
||||
xmlFirstChildNamed(child, "addressOffset"),
|
||||
);
|
||||
const offsetText = xmlFirstChildTrimmedText(child, "addressOffset");
|
||||
if (offsetText === null) continue;
|
||||
const clusterOffset = parseUnsigned32(offsetText);
|
||||
if (clusterOffset === null) continue;
|
||||
const ownSizeText = trimmedText(xmlFirstChildNamed(child, "size"));
|
||||
const ownSizeText = xmlFirstChildTrimmedText(child, "size");
|
||||
const ownSizeBits =
|
||||
ownSizeText !== null ? parseUnsigned32(ownSizeText) : null;
|
||||
if (
|
||||
@@ -313,15 +306,13 @@ function* parseRegisterTemplatesSteps(
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const dimText = trimmedText(xmlFirstChildNamed(child, "dim"));
|
||||
const dimIndex = trimmedText(xmlFirstChildNamed(child, "dimIndex"));
|
||||
const dimText = xmlFirstChildTrimmedText(child, "dim");
|
||||
const dimIndex = xmlFirstChildTrimmedText(child, "dimIndex");
|
||||
let dim = 1;
|
||||
let dimIncrement = 0;
|
||||
if (dimText !== null) {
|
||||
const parsedDim = parseUnsigned32(dimText);
|
||||
const incText = trimmedText(
|
||||
xmlFirstChildNamed(child, "dimIncrement"),
|
||||
);
|
||||
const incText = xmlFirstChildTrimmedText(child, "dimIncrement");
|
||||
const parsedInc = incText !== null ? parseUnsigned32(incText) : null;
|
||||
if (
|
||||
parsedDim === null ||
|
||||
@@ -337,7 +328,7 @@ function* parseRegisterTemplatesSteps(
|
||||
pendingSubframes.push({
|
||||
el: child,
|
||||
isRoot: false,
|
||||
name: trimmedText(xmlFirstChildNamed(child, "name")),
|
||||
name: xmlFirstChildTrimmedText(child, "name"),
|
||||
derivedFrom: trimmedAttrCaseInsensitive(child, "derivedfrom"),
|
||||
relOffset: clusterOffset,
|
||||
ownSizeBits,
|
||||
@@ -1710,8 +1701,8 @@ function* collectRawSvdPeripheralsSteps(
|
||||
const periphEl = node as Element;
|
||||
if (periphEl.localName !== "peripheral") continue;
|
||||
|
||||
const name = trimmedText(xmlFirstChildNamed(periphEl, "name"));
|
||||
const baseText = trimmedText(xmlFirstChildNamed(periphEl, "baseAddress"));
|
||||
const name = xmlFirstChildTrimmedText(periphEl, "name");
|
||||
const baseText = xmlFirstChildTrimmedText(periphEl, "baseAddress");
|
||||
if (name === null || baseText === null) continue;
|
||||
const baseAddress = parseUnsigned32(baseText);
|
||||
if (baseAddress === null) continue;
|
||||
@@ -1727,7 +1718,7 @@ function* collectRawSvdPeripheralsSteps(
|
||||
const derivedAttr = (rawDerived ?? "").trim();
|
||||
const derivedFrom = derivedAttr.length > 0 ? derivedAttr : null;
|
||||
|
||||
const sizeText = trimmedText(xmlFirstChildNamed(periphEl, "size"));
|
||||
const sizeText = xmlFirstChildTrimmedText(periphEl, "size");
|
||||
const ownSizeBits = sizeText !== null ? parseUnsigned32(sizeText) : null;
|
||||
const hasDeclaredBlocks =
|
||||
xmlChildrenNamed(periphEl, "addressBlock").length > 0;
|
||||
@@ -1957,7 +1948,7 @@ export function* parseSvdPeripheralSpansSteps(
|
||||
const peripheralsEl = xmlFirstChildNamed(root, "peripherals");
|
||||
if (peripheralsEl === undefined) return ok(EMPTY_SVD_PERIPHERAL_PROFILE);
|
||||
|
||||
const devSizeText = trimmedText(xmlFirstChildNamed(root, "size"));
|
||||
const devSizeText = xmlFirstChildTrimmedText(root, "size");
|
||||
const deviceDefaultBits =
|
||||
devSizeText !== null ? parseUnsigned32(devSizeText) : null;
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { Element } from "@xmldom/xmldom";
|
||||
import { Budget } from "../budget.js";
|
||||
import {
|
||||
decodeLimit,
|
||||
@@ -10,6 +9,7 @@ import {
|
||||
parseXmlDocumentWithinNodeLimit,
|
||||
xmlChildrenNamed,
|
||||
xmlFirstChildNamed,
|
||||
xmlFirstChildTrimmedText,
|
||||
} from "../xmlDocument.js";
|
||||
import {
|
||||
DEFAULT_SVD_EXPANDED_CHARS,
|
||||
@@ -46,12 +46,6 @@ export type SvdInterruptParseError =
|
||||
| ParseFailureError<"unsupported">
|
||||
| ParseFailureError<"limit">;
|
||||
|
||||
const trimmedText = (element: Element | undefined): string | null => {
|
||||
if (element === undefined) return null;
|
||||
const text = (element.textContent ?? "").trim();
|
||||
return text.length > 0 ? `${text}` : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* An `<interrupt><value>`: CMSIS-SVD declares it `xs:integer`, so decimal with
|
||||
* an optional `+`. Hexadecimal `0x`/`0X` is also accepted because vendor files
|
||||
@@ -155,7 +149,7 @@ export const parseSvdInterruptMap = (
|
||||
peripheralsEl,
|
||||
"peripheral",
|
||||
).entries()) {
|
||||
const periphName = trimmedText(xmlFirstChildNamed(periphEl, "name"));
|
||||
const periphName = xmlFirstChildTrimmedText(periphEl, "name");
|
||||
const interrupts = xmlChildrenNamed(periphEl, "interrupt");
|
||||
if (periphName === null) {
|
||||
if (interrupts.length > 0) {
|
||||
@@ -176,8 +170,8 @@ export const parseSvdInterruptMap = (
|
||||
boundByPeripheral.set(periphName, boundByIrq);
|
||||
}
|
||||
for (const intEl of interrupts) {
|
||||
const intName = trimmedText(xmlFirstChildNamed(intEl, "name"));
|
||||
const valueText = trimmedText(xmlFirstChildNamed(intEl, "value"));
|
||||
const intName = xmlFirstChildTrimmedText(intEl, "name");
|
||||
const valueText = xmlFirstChildTrimmedText(intEl, "value");
|
||||
const irqNumber = valueText === null ? null : parseIrqNumber(valueText);
|
||||
if (intName === null) {
|
||||
const skippedError =
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { Element } from "@xmldom/xmldom";
|
||||
import { Budget } from "../budget.js";
|
||||
import {
|
||||
decodeLimit,
|
||||
@@ -10,6 +9,7 @@ import {
|
||||
parseXmlDocumentWithinNodeLimit,
|
||||
xmlChildrenNamed,
|
||||
xmlFirstChildNamed,
|
||||
xmlFirstChildTrimmedText,
|
||||
} from "../xmlDocument.js";
|
||||
import {
|
||||
DEFAULT_SVD_EXPANDED_CHARS,
|
||||
@@ -117,12 +117,6 @@ const parseBigUint = (
|
||||
return null;
|
||||
};
|
||||
|
||||
const trimmedText = (element: Element | undefined): string | null => {
|
||||
if (element === undefined) return null;
|
||||
const text = (element.textContent ?? "").trim();
|
||||
return text.length > 0 ? `${text}` : null;
|
||||
};
|
||||
|
||||
const coalesceAddressBlocks = (
|
||||
blocks: SvdAddressBlockRange[],
|
||||
): readonly SvdAddressBlockRange[] => {
|
||||
@@ -286,10 +280,8 @@ export const parseSvdPeripheralMap = (
|
||||
const byName = new Map<string, RawPeripheral>();
|
||||
|
||||
for (const pEl of xmlChildrenNamed(peripheralsEl, "peripheral")) {
|
||||
const pName = trimmedText(xmlFirstChildNamed(pEl, "name"));
|
||||
const base = parseBigUint(
|
||||
trimmedText(xmlFirstChildNamed(pEl, "baseAddress")),
|
||||
);
|
||||
const pName = xmlFirstChildTrimmedText(pEl, "name");
|
||||
const base = parseBigUint(xmlFirstChildTrimmedText(pEl, "baseAddress"));
|
||||
if (pName !== null) {
|
||||
const limitErr = chargeExpandedChars(pName.length);
|
||||
if (limitErr !== null) return err(limitErr);
|
||||
@@ -316,10 +308,10 @@ export const parseSvdPeripheralMap = (
|
||||
if (blockEls.length > 0) {
|
||||
const rawBlocks: SvdAddressBlockRange[] = [];
|
||||
for (const ab of blockEls) {
|
||||
const rawOffText = trimmedText(xmlFirstChildNamed(ab, "offset"));
|
||||
const rawOffText = xmlFirstChildTrimmedText(ab, "offset");
|
||||
const abOff = rawOffText === null ? 0n : parseBigUint(rawOffText);
|
||||
const abSize = parseBigUint(
|
||||
trimmedText(xmlFirstChildNamed(ab, "size")),
|
||||
xmlFirstChildTrimmedText(ab, "size"),
|
||||
MAX_ADDR_SPACE_END,
|
||||
);
|
||||
if (
|
||||
@@ -347,13 +339,11 @@ export const parseSvdPeripheralMap = (
|
||||
const regsEl = xmlFirstChildNamed(pEl, "registers");
|
||||
if (regsEl !== undefined) {
|
||||
for (const rEl of xmlChildrenNamed(regsEl, "register")) {
|
||||
const rName = trimmedText(xmlFirstChildNamed(rEl, "name"));
|
||||
const rName = xmlFirstChildTrimmedText(rEl, "name");
|
||||
const rOff = parseBigUint(
|
||||
trimmedText(xmlFirstChildNamed(rEl, "addressOffset")),
|
||||
);
|
||||
const rSize = parseBigUint(
|
||||
trimmedText(xmlFirstChildNamed(rEl, "size")),
|
||||
xmlFirstChildTrimmedText(rEl, "addressOffset"),
|
||||
);
|
||||
const rSize = parseBigUint(xmlFirstChildTrimmedText(rEl, "size"));
|
||||
const sizeBits = rSize !== null && rSize <= 64n ? Number(rSize) : null;
|
||||
const byteWidth = BigInt(Math.max(1, (sizeBits ?? 32) >> 3));
|
||||
if (
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
parseXmlDocument,
|
||||
parseXmlDocumentWithinNodeLimit,
|
||||
xmlElementRanges,
|
||||
xmlFirstChildTrimmedText,
|
||||
xmlTrimmedAttribute,
|
||||
xmlTrimmedDirectText,
|
||||
} from "./xmlDocument.js";
|
||||
@@ -118,6 +119,20 @@ it("trims attributes and joins only direct character data", () => {
|
||||
expect(xmlTrimmedDirectText(item)).toBe("One & two three");
|
||||
});
|
||||
|
||||
it("trims subtree text from the first named child", () => {
|
||||
const parsed = parseXmlDocument(
|
||||
'<root xmlns:p="urn:example"><blank> </blank><blank>later</blank><p:item>one <part>two</part><![CDATA[ three ]]></p:item><zero> 0 </zero></root>',
|
||||
{ doctype: "reject", lineEndings: "preserve" },
|
||||
);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
const root = parsed.value.documentElement!;
|
||||
expect(
|
||||
["missing", "blank", "item", "zero"].map((name) =>
|
||||
xmlFirstChildTrimmedText(root, name),
|
||||
),
|
||||
).toEqual([null, null, "one two three", "0"]);
|
||||
});
|
||||
|
||||
it("refuses element depth at the syntax check", () => {
|
||||
const source =
|
||||
"<e>".repeat(XML_ELEMENT_DEPTH_LIMIT + 1) +
|
||||
|
||||
@@ -78,6 +78,13 @@ export const xmlFirstChildNamed = (
|
||||
): Element | undefined =>
|
||||
iterateXmlChildElements(element, local, namespace).next().value;
|
||||
|
||||
/** Trim the first matching child's subtree text, treating blank text as absent. */
|
||||
export const xmlFirstChildTrimmedText = (
|
||||
element: Element,
|
||||
local: string,
|
||||
): string | null =>
|
||||
xmlFirstChildNamed(element, local)?.textContent?.trim() || null;
|
||||
|
||||
/** Read a qualified attribute, trimming its value and treating blank text as absent. */
|
||||
export const xmlTrimmedAttribute = (
|
||||
element: Element,
|
||||
|
||||
Reference in new issue
Block a user