Coverage for phml\nodes\literal.py: 64%

33 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-30 09:38 -0600

1from typing import Optional 

2 

3from .element import Element 

4from .node import Node 

5from .position import Position 

6from .root import Root 

7 

8 

9class Literal(Node): 

10 """Literal (UnistLiteral) represents a node in hast containing a value.""" 

11 

12 position: Position 

13 """The location of a node in a source document. 

14 The value of the position field implements the Position interface. 

15 The position field must not be present if a node is generated. 

16 """ 

17 

18 value: str 

19 """The Literal nodes value. All literal values must be strings""" 

20 

21 def __init__( 

22 self, 

23 value: str = "", 

24 parent: Optional[Element | Root] = None, 

25 position: Optional[Position] = None, 

26 ): 

27 super().__init__(position) 

28 self.value = value 

29 self.parent = parent 

30 

31 def __eq__(self, obj) -> bool: 

32 if obj is None: 

33 return False 

34 

35 if self.type == obj.type: 

36 if self.value == obj.value: 

37 return True 

38 else: 

39 return False 

40 return False 

41 

42 def __repr__(self) -> str: 

43 return f"{self.type}(value:{len(self.value)})" 

44 

45 def get_ancestry(self) -> list[str]: 

46 def get_parent(parent) -> list[str]: 

47 result = [] 

48 if parent is not None and hasattr(parent, "tag"): 

49 result.append(parent.tag) 

50 if parent.parent is not None: 

51 result.extend(get_parent(parent.parent)) 

52 return result 

53 

54 return get_parent(self.parent)