Skip to content

Commit 03c07b5

Browse files
committed
Fix Node.find_by_path() to allow empty array suffixes
1 parent 89e991d commit 03c07b5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/systemrdl/node.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,15 @@ def find_by_path(self, path: str) -> Optional['Node']:
360360
continue
361361

362362
# .. otherwise continue parsing the path
363-
m = re.fullmatch(r'^(\w+)((?:\[(?:\d+|0[xX][\da-fA-F]+)\])*)$', pathpart)
363+
m = re.fullmatch(r'(\w+)(.*)', pathpart)
364364
if not m:
365365
raise ValueError("Invalid path")
366366
inst_name, array_suffix = m.group(1, 2)
367-
idx_list = [int(s, 0) for s in re.findall(r'\[(\d+|0[xX][\da-fA-F]+)\]', array_suffix)]
367+
if array_suffix == "" or re.fullmatch(r"(\[\])+", array_suffix):
368+
# No indexes specified
369+
idx_list = []
370+
else:
371+
idx_list = [int(s, 0) for s in re.findall(r'\[(\d+|0[xX][\da-fA-F]+)\]', array_suffix)]
368372

369373
current_node = current_node.get_child_by_name(inst_name)
370374
if current_node is None:

test/test_node_utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
from unittest_utils import RDLSourceTestCase
22
from systemrdl.rdltypes import PrecedenceType
3-
from systemrdl.node import RegNode, FieldNode, RegfileNode, AddrmapNode, SignalNode, MemNode
3+
from systemrdl.node import RegNode, FieldNode, RegfileNode, AddrmapNode
44

55
class TestNodeUtils(RDLSourceTestCase):
66

7+
def test_find_by_path(self):
8+
top = self.compile(
9+
["rdl_src/address_packing.rdl"],
10+
"hier"
11+
)
12+
13+
14+
node = top.find_by_path("hier.y[2].a[0][1]")
15+
self.assertEqual(node.get_path(), "hier.y[2].a[0][1]")
16+
17+
node = top.find_by_path("hier.y.a")
18+
self.assertEqual(node.get_path(), "hier.y[].a[][]")
19+
20+
node = top.find_by_path("hier.y[].a[][]")
21+
self.assertEqual(node.get_path(), "hier.y[].a[][]")
22+
723
def test_index_tools(self):
824
top = self.compile(
925
["rdl_src/address_packing.rdl"],

0 commit comments

Comments
 (0)