File tree 8 files changed +27
-60
lines changed
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:
30
30
31
31
def __repr__ (self ) -> str :
32
32
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 1
1
""" General terminal representation """
2
2
3
- from typing import Any
4
3
from abc import abstractmethod
5
4
6
5
from .formal_object import FormalObject
9
8
class BaseTerminal (FormalObject ):
10
9
""" General terminal representation """
11
10
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
-
22
11
@abstractmethod
23
12
def __repr__ (self ):
24
13
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 1
1
""" A variable in a CFG """
2
2
3
- from typing import Any
4
3
from string import ascii_uppercase
5
4
6
5
from .cfg_object import CFGObject
@@ -16,16 +15,6 @@ class Variable(CFGObject):
16
15
The value of the variable
17
16
"""
18
17
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
-
29
18
def __repr__ (self ) -> str :
30
19
return f"Variable({ self } )"
31
20
@@ -34,3 +23,6 @@ def to_text(self) -> str:
34
23
if text and text [0 ] not in ascii_uppercase :
35
24
return '"VAR:' + text + '"'
36
25
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 2
2
Representation of a state in a finite state automaton
3
3
"""
4
4
5
- from typing import Any
6
-
7
5
from .finite_automaton_object import FiniteAutomatonObject
8
6
from ..formal_object import FormalObject
9
7
@@ -24,15 +22,8 @@ class State(FiniteAutomatonObject):
24
22
25
23
"""
26
24
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
-
37
25
def __repr__ (self ) -> str :
38
26
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:
23
23
"""
24
24
return self ._value
25
25
26
- @abstractmethod
27
26
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 )
29
30
30
31
def __hash__ (self ) -> int :
31
32
if self ._hash is None :
@@ -38,3 +39,7 @@ def __str__(self) -> str:
38
39
@abstractmethod
39
40
def __repr__ (self ) -> str :
40
41
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 1
1
""" A StackSymbol in a pushdown automaton """
2
2
3
- from typing import Any
4
-
5
3
from .symbol import Symbol
6
4
from ..formal_object import FormalObject
7
5
@@ -16,15 +14,8 @@ class StackSymbol(Symbol):
16
14
17
15
"""
18
16
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
-
29
17
def __repr__ (self ) -> str :
30
18
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 1
1
""" A State in a pushdown automaton """
2
2
3
- from typing import Any
4
-
5
3
from .pda_object import PDAObject
6
4
from ..formal_object import FormalObject
7
5
@@ -16,15 +14,8 @@ class State(PDAObject):
16
14
17
15
"""
18
16
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
-
29
17
def __repr__ (self ) -> str :
30
18
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):
389
389
assert StackSymbol ("ABC" ) != Symbol ("ABC" )
390
390
assert State ("ABC" ) != FAState ("ABC" )
391
391
assert Symbol ("s" ) == Terminal ("s" )
392
+ assert Terminal (1 ) != StackSymbol (1 )
393
+ assert StackSymbol (42 ) != FAState (42 )
392
394
393
395
def test_contains (self , pda_example : PDA ):
394
396
""" Tests the transition containment checks """
You can’t perform that action at this time.
0 commit comments