refactor: replace array-based expressions with typed class hierarchy
Replace the old Lisp-like array expression representation (["AND", ["PARAM", ...], ...]) with a class hierarchy rooted in an abstract Expression class (Literal, Parameter, Binary, Unary, FunctionCall, Conditional). This gives compile-time type safety to expression construction, evaluation, and pattern matching. Key changes: - New lib/common/expression.ts defines the class hierarchy with map/evaluate/toString methods - Rewrite parser as hand-rolled recursive descent, removing the parsimmon dependency - Extend Path with a colon separator to unify parameter identity and attributes into a single address space (e.g. "Device.WiFi.SSID.1.Name:writable"). The colon field tracks how many trailing segments are attribute qualifiers. PathSet rewritten with separate param/attr segment indexes - Flatten device attribute representation in the UI to colon-delimited keys mirroring the new path syntax
This commit is contained in:
1 parent
75908e624f
commit
6f3ce6f7c8
71 files changed
+4045
-3449
No files matched your search
+69
-65
@@ -1,40 +1,32 @@
|
||||
import Path from "./path.ts";
|
||||
|
||||
export default class PathSet {
|
||||
declare private lengthIndex: Set<Path>[];
|
||||
declare private fragmentIndex: Map<string, Set<Path>>[];
|
||||
declare private stringIndex: Map<string, Path>;
|
||||
private paramSegmentIndex: Map<string, Set<Path>>[] = [];
|
||||
private attrSegmentIndex: Map<string, Set<Path>>[] = [];
|
||||
private stringIndex: Map<string, Path> = new Map();
|
||||
|
||||
public constructor() {
|
||||
this.lengthIndex = [];
|
||||
this.fragmentIndex = [];
|
||||
this.stringIndex = new Map();
|
||||
}
|
||||
public constructor() {}
|
||||
|
||||
public get depth(): number {
|
||||
return this.lengthIndex.length;
|
||||
}
|
||||
|
||||
public add(path: Path): Path {
|
||||
public add(pathStr: string): Path {
|
||||
let path: Path = this.get(pathStr);
|
||||
if (path) return path;
|
||||
path = Path.parse(pathStr);
|
||||
if (path.alias) throw new Error("PathSet does not support aliased paths");
|
||||
const p = this.get(path);
|
||||
|
||||
if (p) return p;
|
||||
|
||||
this.stringIndex.set(path.toString(), path);
|
||||
|
||||
while (this.lengthIndex.length <= path.length) {
|
||||
this.lengthIndex.push(new Set());
|
||||
// fragmentIndex is one less than lengthIndex
|
||||
if (this.lengthIndex.length > 1) this.fragmentIndex.push(new Map());
|
||||
}
|
||||
while (this.paramSegmentIndex.length < path.paramLength)
|
||||
this.paramSegmentIndex.push(new Map());
|
||||
|
||||
const lengthIndex = this.lengthIndex[path.length];
|
||||
lengthIndex.add(path);
|
||||
while (this.attrSegmentIndex.length < path.attrLength)
|
||||
this.attrSegmentIndex.push(new Map());
|
||||
|
||||
for (let i = 0; i < path.length; ++i) {
|
||||
const fragment = path.segments[i] as string;
|
||||
const fragmentIndex = this.fragmentIndex[i];
|
||||
const fragmentIndex =
|
||||
i < path.paramLength
|
||||
? this.paramSegmentIndex[i]
|
||||
: this.attrSegmentIndex[i - path.paramLength];
|
||||
|
||||
let fragmentIndexSet = fragmentIndex.get(fragment);
|
||||
if (!fragmentIndexSet) {
|
||||
@@ -48,64 +40,76 @@ export default class PathSet {
|
||||
return path;
|
||||
}
|
||||
|
||||
public get(path: Path): Path {
|
||||
return this.stringIndex.get(path.toString()) || null;
|
||||
public get(path: string): Path {
|
||||
return this.stringIndex.get(path);
|
||||
}
|
||||
|
||||
public find(
|
||||
public findCompat(
|
||||
path: Path,
|
||||
superset = false,
|
||||
subset = false,
|
||||
depth: number = path.length,
|
||||
depth = path.length,
|
||||
): Path[] {
|
||||
if (path.attrLength)
|
||||
throw new Error("findCompat() does not support attribute paths");
|
||||
|
||||
depth = Math.min(31, depth);
|
||||
const paramMask = (1 << path.paramLength) - 1;
|
||||
let mask = ((0b10 << depth) - 1) & ~paramMask;
|
||||
if (superset) mask |= ~path.wildcard & paramMask;
|
||||
if (subset) mask |= path.wildcard;
|
||||
return this.find(path, mask, 1);
|
||||
}
|
||||
|
||||
public find(path: Path, paramMask: number, attrMask: number): Path[] {
|
||||
if (path.alias) throw new Error("PathSet does not support aliased paths");
|
||||
if (path.paramLength > this.paramSegmentIndex.length) return [];
|
||||
if (path.attrLength > this.attrSegmentIndex.length) return [];
|
||||
|
||||
const len = path.length;
|
||||
const emptySet: Set<Path> = new Set();
|
||||
|
||||
if (!superset && depth === len && (!subset || !path.wildcard)) {
|
||||
const p = this.get(path);
|
||||
return p ? [p] : [];
|
||||
const indexes: [Set<Path>, Set<Path>][] = [];
|
||||
|
||||
const paramLengthMask = (1 << path.paramLength) - 1;
|
||||
const attrLengthMask = (1 << path.attrLength) - 1;
|
||||
|
||||
const mask = (paramMask & paramLengthMask) | (attrMask << path.paramLength);
|
||||
|
||||
for (const [i, s] of path.segments.entries()) {
|
||||
const b = 1 << i;
|
||||
const m = mask & b;
|
||||
const w = b & path.wildcard;
|
||||
if (w && m) continue;
|
||||
const idxSet =
|
||||
i < path.paramLength
|
||||
? this.paramSegmentIndex[i]
|
||||
: this.attrSegmentIndex[i - path.paramLength];
|
||||
const idx1 = idxSet.get(s as string) || emptySet;
|
||||
let idx2 = emptySet;
|
||||
if (m) idx2 = idxSet.get("*") || emptySet;
|
||||
indexes.push([idx1, idx2]);
|
||||
}
|
||||
|
||||
const lengthIndex = this.lengthIndex.slice(len, depth + 1);
|
||||
if (!lengthIndex.length) return [];
|
||||
indexes.sort((a, b) => a[0].size + a[1].size - (b[0].size + b[1].size));
|
||||
|
||||
let res;
|
||||
for (let i = len - 1; i >= 0; --i) {
|
||||
let fragmentIndexSet2;
|
||||
const fragmentIndex = this.fragmentIndex[i];
|
||||
let res: Path[];
|
||||
|
||||
if ((path.wildcard >> i) & 1) {
|
||||
if (subset) continue;
|
||||
} else if (superset) {
|
||||
fragmentIndexSet2 = fragmentIndex.get("*");
|
||||
}
|
||||
if (!indexes.length) res = [...this.stringIndex.values()];
|
||||
else res = [...indexes[0][0], ...indexes[0][1]];
|
||||
|
||||
const fragment = path.segments[i] as string;
|
||||
const fragmentIndexSet1 = fragmentIndex.get(fragment);
|
||||
const paramCover = ~paramLengthMask & paramMask;
|
||||
const attrCover = ~attrLengthMask & attrMask;
|
||||
|
||||
if (!fragmentIndexSet1) {
|
||||
if (!fragmentIndexSet2) return [];
|
||||
if (!res) res = [...fragmentIndexSet2];
|
||||
else res = res.filter((r) => fragmentIndexSet2.has(r));
|
||||
} else if (!fragmentIndexSet2) {
|
||||
if (!res) res = [...fragmentIndexSet1];
|
||||
else res = res.filter((r) => fragmentIndexSet1.has(r));
|
||||
} else {
|
||||
if (!res) {
|
||||
res = [...fragmentIndexSet1, ...fragmentIndexSet2];
|
||||
} else {
|
||||
res = res.filter(
|
||||
(r) => fragmentIndexSet1.has(r) || fragmentIndexSet2.has(r),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!res.length) return res;
|
||||
res = res.filter(
|
||||
(p) =>
|
||||
(1 << p.paramLength) & paramCover && (1 << p.attrLength) & attrCover,
|
||||
);
|
||||
|
||||
for (let i = 1; i < indexes.length; ++i) {
|
||||
const [idx1, idx2] = indexes[i];
|
||||
res = res.filter((p) => idx1.has(p) || idx2.has(p));
|
||||
}
|
||||
|
||||
if (!res) res = [].concat(...lengthIndex.map((a) => [...a]));
|
||||
else res = res.filter((r) => lengthIndex.some((a) => a.has(r)));
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user