Skip to content

Commit

Permalink
Add Branch defintion
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carlstrom <[email protected]>
  • Loading branch information
InvincibleRMC committed Oct 31, 2024
1 parent 3ba595b commit 86ec612
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion rosidl_parser/rosidl_parser/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Final
from typing import Iterable
from typing import List
from typing import Literal
from typing import Optional
from typing import Set
from typing import Tuple
Expand Down Expand Up @@ -95,7 +96,7 @@
)

if TYPE_CHECKING:
from typing import Literal, TypeAlias
from typing_extensions import TypeAlias
SignedNonexplicitIntegerTypeValues = Literal['short', 'long', 'long long']
UnsignedNonexplicitIntegerTypeValues = Literal['unsigned short', 'unsigned long',
'unsigned long long']
Expand Down
24 changes: 18 additions & 6 deletions rosidl_parser/rosidl_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from lark import Lark
from lark.lexer import Token
from lark.tree import Branch
from lark.tree import ParseTree
from lark.tree import pydot__tree_to_png
from lark.tree import Tree
Expand Down Expand Up @@ -65,8 +64,20 @@
from rosidl_parser.definition import ValueType

if TYPE_CHECKING:
from typing_extensions import TypeAlias
from typing import TypeVar

# Definition taken from lark.tree to here since rhel's lark version does not have Branch.
_Leaf_T = TypeVar('_Leaf_T')
Branch: 'TypeAlias' = Union[_Leaf_T, 'Tree[_Leaf_T]']

from rosidl_parser.definition import BasicTypeValues

try:
from lark.tree import Branch
except ImportError:
pass


AbstractTypeAlias = Union[AbstractNestableType, BasicType, BoundedSequence, UnboundedSequence]

Expand Down Expand Up @@ -326,8 +337,8 @@ def get_child_identifier_value(tree: ParseTree) -> Any:
return None


def _find_tokens(token_type: Optional[Literal['IDENTIFIER']]) -> Callable[[Branch[Token]], bool]:
def find(t: Branch[Token]) -> bool:
def _find_tokens(token_type: Optional[Literal['IDENTIFIER']]) -> Callable[['Branch[Token]'], bool]:
def find(t: 'Branch[Token]') -> bool:
if isinstance(t, Token):
if token_type is None or t.type == token_type:
return True
Expand Down Expand Up @@ -461,7 +472,7 @@ def get_abstract_type_from_type_spec(type_spec: ParseTree) -> AbstractTypeAlias:
return get_abstract_type(child)


def get_abstract_type(tree: Branch[Token]) -> AbstractTypeAlias:
def get_abstract_type(tree: 'Branch[Token]') -> AbstractTypeAlias:
assert isinstance(tree, Tree)
if 'simple_type_spec' == tree.data:
assert len(tree.children) == 1
Expand Down Expand Up @@ -590,7 +601,7 @@ def get_annotations(tree: ParseTree) -> List[Annotation]:
return annotations


def get_const_expr_value(const_expr: Branch[Token]) -> Union[str, int, float, bool]:
def get_const_expr_value(const_expr: 'Branch[Token]') -> Union[str, int, float, bool]:
assert isinstance(const_expr, Tree)
# TODO support arbitrary expressions
expr = list(const_expr.find_data('primary_expr'))
Expand Down Expand Up @@ -694,7 +705,8 @@ def get_string_literals_value(string_literals: ParseTree, *, allow_unicode: bool
return value


def get_string_literal_value(string_literal: Branch[Token], *, allow_unicode: bool = False) -> str:
def get_string_literal_value(string_literal: 'Branch[Token]', *,
allow_unicode: bool = False) -> str:
assert isinstance(string_literal, Tree)
if len(string_literal.children) == 0:
return ''
Expand Down

0 comments on commit 86ec612

Please sign in to comment.