Skip to content

Commit

Permalink
path recursive
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
  • Loading branch information
InvincibleRMC committed Oct 19, 2024
1 parent d90aa1a commit 356799b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rosidl_parser/rosidl_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,22 +338,28 @@ def find(t: Branch[Token]) -> bool:
def get_module_identifier_values(tree: ParseTree, target: ParseTree) -> List[Any]:
"""Get all module names between a tree node and a specific target node."""
path = _find_path(tree, target)
modules = [n for n in path if n is not None and n.data == 'module_dcl']
modules = [n for n in path if n.data == 'module_dcl']
return [
get_first_identifier_value(n) for n in modules]


def _find_path(node: ParseTree, target: ParseTree) -> List[ParseTree]:
path = _find_path_recursive(node, target)
if path is None:
raise ValueError(f'No path found between {node} and {target}')
return path


def _find_path_recursive(node: ParseTree, target: ParseTree) -> Optional[List[ParseTree]]:
if node == target:
return [node]
for c in node.children:
if not isinstance(c, Tree):
continue
tail = _find_path(c, target)
tail = _find_path_recursive(c, target)
if tail is not None:
return [node] + tail
return None
raise ValueError(f'No path found between {node} and {target}')


def get_abstract_type_from_const_expr(const_expr: ParseTree, value: Union[str, int, float, bool]
Expand Down Expand Up @@ -544,7 +550,7 @@ def get_positive_int_const(positive_int_const: ParseTree) -> int:
pass
else:
# TODO ensure that identifier resolves to a positive integer
return int(identifier_token.value)
return identifier_token.value

assert False, 'Unsupported tree: ' + str(positive_int_const)

Expand Down

0 comments on commit 356799b

Please sign in to comment.