File tree Expand file tree Collapse file tree 8 files changed +27
-60
lines changed Expand file tree Collapse file tree 8 files changed +27
-60
lines changed Original file line number Diff line number Diff line change @@ -30,3 +30,6 @@ def __hash__(self) -> int:
3030
3131 def __repr__ (self ) -> str :
3232 return "epsilon"
33+
34+ def _is_equal_to (self , other : FormalObject ) -> bool :
35+ return isinstance (other , BaseEpsilon )
Original file line number Diff line number Diff line change 11""" General terminal representation """
22
3- from typing import Any
43from abc import abstractmethod
54
65from .formal_object import FormalObject
98class BaseTerminal (FormalObject ):
109 """ General terminal representation """
1110
12- def __eq__ (self , other : Any ) -> bool :
13- if isinstance (other , BaseTerminal ):
14- return self .value == other .value
15- if isinstance (other , FormalObject ):
16- return False
17- return self .value == other
18-
19- def __hash__ (self ) -> int :
20- return super ().__hash__ ()
21-
2211 @abstractmethod
2312 def __repr__ (self ):
2413 raise NotImplementedError
14+
15+ def _is_equal_to (self , other : FormalObject ) -> bool :
16+ return isinstance (other , BaseTerminal ) and self .value == other .value
Original file line number Diff line number Diff line change 11""" A variable in a CFG """
22
3- from typing import Any
43from string import ascii_uppercase
54
65from .cfg_object import CFGObject
@@ -16,16 +15,6 @@ class Variable(CFGObject):
1615 The value of the variable
1716 """
1817
19- def __eq__ (self , other : Any ) -> bool :
20- if isinstance (other , Variable ):
21- return self .value == other .value
22- if isinstance (other , FormalObject ):
23- return False
24- return self .value == other
25-
26- def __hash__ (self ) -> int :
27- return super ().__hash__ ()
28-
2918 def __repr__ (self ) -> str :
3019 return f"Variable({ self } )"
3120
@@ -34,3 +23,6 @@ def to_text(self) -> str:
3423 if text and text [0 ] not in ascii_uppercase :
3524 return '"VAR:' + text + '"'
3625 return text
26+
27+ def _is_equal_to (self , other : FormalObject ) -> bool :
28+ return isinstance (other , Variable ) and self .value == other .value
Original file line number Diff line number Diff line change 22Representation of a state in a finite state automaton
33"""
44
5- from typing import Any
6-
75from .finite_automaton_object import FiniteAutomatonObject
86from ..formal_object import FormalObject
97
@@ -24,15 +22,8 @@ class State(FiniteAutomatonObject):
2422
2523 """
2624
27- def __eq__ (self , other : Any ) -> bool :
28- if isinstance (other , State ):
29- return self .value == other .value
30- if isinstance (other , FormalObject ):
31- return False
32- return self .value == other
33-
34- def __hash__ (self ) -> int :
35- return super ().__hash__ ()
36-
3725 def __repr__ (self ) -> str :
3826 return f"State({ self } )"
27+
28+ def _is_equal_to (self , other : FormalObject ) -> bool :
29+ return isinstance (other , State ) and self .value == other .value
Original file line number Diff line number Diff line change @@ -23,9 +23,10 @@ def value(self) -> Hashable:
2323 """
2424 return self ._value
2525
26- @abstractmethod
2726 def __eq__ (self , other : Any ) -> bool :
28- raise NotImplementedError
27+ if not isinstance (other , FormalObject ):
28+ return self .value == other
29+ return self ._is_equal_to (other ) and other ._is_equal_to (self )
2930
3031 def __hash__ (self ) -> int :
3132 if self ._hash is None :
@@ -38,3 +39,7 @@ def __str__(self) -> str:
3839 @abstractmethod
3940 def __repr__ (self ) -> str :
4041 raise NotImplementedError
42+
43+ @abstractmethod
44+ def _is_equal_to (self , other : "FormalObject" ) -> bool :
45+ raise NotImplementedError
Original file line number Diff line number Diff line change 11""" A StackSymbol in a pushdown automaton """
22
3- from typing import Any
4-
53from .symbol import Symbol
64from ..formal_object import FormalObject
75
@@ -16,15 +14,8 @@ class StackSymbol(Symbol):
1614
1715 """
1816
19- def __eq__ (self , other : Any ) -> bool :
20- if isinstance (other , StackSymbol ):
21- return self .value == other .value
22- if isinstance (other , FormalObject ):
23- return False
24- return self .value == other
25-
26- def __hash__ (self ) -> int :
27- return super ().__hash__ ()
28-
2917 def __repr__ (self ) -> str :
3018 return f"StackSymbol({ self } )"
19+
20+ def _is_equal_to (self , other : FormalObject ) -> bool :
21+ return isinstance (other , StackSymbol ) and self .value == other .value
Original file line number Diff line number Diff line change 11""" A State in a pushdown automaton """
22
3- from typing import Any
4-
53from .pda_object import PDAObject
64from ..formal_object import FormalObject
75
@@ -16,15 +14,8 @@ class State(PDAObject):
1614
1715 """
1816
19- def __eq__ (self , other : Any ) -> bool :
20- if isinstance (other , State ):
21- return self .value == other .value
22- if isinstance (other , FormalObject ):
23- return False
24- return self .value == other
25-
26- def __hash__ (self ) -> int :
27- return super ().__hash__ ()
28-
2917 def __repr__ (self ) -> str :
3018 return f"State({ self } )"
19+
20+ def _is_equal_to (self , other : FormalObject ) -> bool :
21+ return isinstance (other , State ) and self .value == other .value
Original file line number Diff line number Diff line change @@ -389,6 +389,8 @@ def test_object_eq(self):
389389 assert StackSymbol ("ABC" ) != Symbol ("ABC" )
390390 assert State ("ABC" ) != FAState ("ABC" )
391391 assert Symbol ("s" ) == Terminal ("s" )
392+ assert Terminal (1 ) != StackSymbol (1 )
393+ assert StackSymbol (42 ) != FAState (42 )
392394
393395 def test_contains (self , pda_example : PDA ):
394396 """ Tests the transition containment checks """
You can’t perform that action at this time.
0 commit comments