From 356799b46fa1c37a46cbf9ea1079ed667270cf38 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 19 Oct 2024 12:40:48 -0400 Subject: [PATCH] path recursive Signed-off-by: Michael Carlstrom --- rosidl_parser/rosidl_parser/parser.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rosidl_parser/rosidl_parser/parser.py b/rosidl_parser/rosidl_parser/parser.py index 151231639..d60415b89 100644 --- a/rosidl_parser/rosidl_parser/parser.py +++ b/rosidl_parser/rosidl_parser/parser.py @@ -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] @@ -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)