Skip to content

Commit 892e8cd

Browse files
committed
add indexed_grammar type annotations, rewrite rules in a stricter way
1 parent 98ffd66 commit 892e8cd

File tree

9 files changed

+381
-324
lines changed

9 files changed

+381
-324
lines changed

pyformlang/fst/fst.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,22 @@ def _extract_indexed_grammar_rules_intersection(
291291
new_rules: List[ReducedRule]) \
292292
-> None:
293293
for rule in rules.rules:
294-
if rule.is_duplication():
294+
if isinstance(rule, DuplicationRule):
295295
for state_p in self._states:
296296
for state_q in self._states:
297297
for state_r in self._states:
298298
new_rules.append(DuplicationRule(
299299
str((state_p, rule.left_term, state_q)),
300300
str((state_p, rule.right_terms[0], state_r)),
301301
str((state_r, rule.right_terms[1], state_q))))
302-
elif rule.is_production():
302+
elif isinstance(rule, ProductionRule):
303303
for state_p in self._states:
304304
for state_q in self._states:
305305
new_rules.append(ProductionRule(
306306
str((state_p, rule.left_term, state_q)),
307307
str((state_p, rule.right_term, state_q)),
308308
str(rule.production)))
309-
elif rule.is_end_rule():
309+
elif isinstance(rule, EndRule):
310310
for state_p in self._states:
311311
for state_q in self._states:
312312
new_rules.append(DuplicationRule(
@@ -346,7 +346,7 @@ def _extract_consumption_rules_intersection(
346346
new_rules.append(ConsumptionRule(
347347
consumption.f_parameter,
348348
str((state_r, consumption.left_term, state_s)),
349-
str((state_r, consumption.right, state_s))))
349+
str((state_r, consumption.right_term, state_s))))
350350

351351
def __and__(self, other: IndexedGrammar) -> IndexedGrammar:
352352
return self.intersection(other)

pyformlang/indexed_grammar/consumption_rule.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
the stack
44
"""
55

6-
from typing import Any, Iterable, AbstractSet
6+
from typing import List, Set, Any
7+
8+
from pyformlang.cfg import Variable, Terminal
9+
from pyformlang.cfg.utils import to_variable, to_terminal
10+
from pyformlang.cfg.cfg_object import CFGObject
711

812
from .reduced_rule import ReducedRule
913

@@ -24,30 +28,19 @@ class ConsumptionRule(ReducedRule):
2428
"""
2529

2630
@property
27-
def right_term(self):
28-
raise NotImplementedError
29-
30-
@property
31-
def right_terms(self):
31+
def production(self) -> Terminal:
3232
raise NotImplementedError
3333

34-
def __init__(self, f_param: Any, left: Any, right: Any):
35-
self._f = f_param
36-
self._right = right
37-
self._left_term = left
38-
39-
def is_consumption(self) -> bool:
40-
"""Whether the rule is a consumption rule or not
41-
42-
Returns
43-
----------
44-
is_consumption : bool
45-
Whether the rule is a consumption rule or not
46-
"""
47-
return True
34+
def __init__(self,
35+
f_param: Any,
36+
left_term: Any,
37+
right_term: Any) -> None:
38+
self._f = to_terminal(f_param)
39+
self._left_term = to_variable(left_term)
40+
self._right_term = to_variable(right_term)
4841

4942
@property
50-
def f_parameter(self) -> Any:
43+
def f_parameter(self) -> Terminal:
5144
"""Gets the symbol which is consumed
5245
5346
Returns
@@ -58,49 +51,58 @@ def f_parameter(self) -> Any:
5851
return self._f
5952

6053
@property
61-
def production(self):
62-
raise NotImplementedError
54+
def left_term(self) -> Variable:
55+
"""Gets the symbol on the left of the rule
56+
57+
left : any
58+
The left symbol of the rule
59+
"""
60+
return self._left_term
6361

6462
@property
65-
def right(self) -> Any:
66-
"""Gets the symbole on the right of the rule
63+
def right_term(self) -> Variable:
64+
"""Gets the symbol on the right of the rule
6765
6866
right : any
6967
The right symbol
7068
"""
71-
return self._right
69+
return self._right_term
7270

7371
@property
74-
def left_term(self) -> Any:
75-
"""Gets the symbol on the left of the rule
72+
def right_terms(self) -> List[CFGObject]:
73+
"""Gives the non-terminals on the right of the rule
7674
77-
left : any
78-
The left symbol of the rule
75+
Returns
76+
---------
77+
right_terms : iterable of any
78+
The right terms of the rule
7979
"""
80-
return self._left_term
80+
return [self._right_term]
8181

8282
@property
83-
def non_terminals(self) -> Iterable[Any]:
83+
def non_terminals(self) -> Set[Variable]:
8484
"""Gets the non-terminals used in the rule
8585
8686
non_terminals : iterable of any
8787
The non_terminals used in the rule
8888
"""
89-
return [self._left_term, self._right]
89+
return {self._left_term, self._right_term}
9090

9191
@property
92-
def terminals(self) -> AbstractSet[Any]:
92+
def terminals(self) -> Set[Terminal]:
9393
"""Gets the terminals used in the rule
9494
9595
terminals : set of any
9696
The terminals used in the rule
9797
"""
9898
return {self._f}
9999

100-
def __repr__(self):
101-
return self._left_term + " [ " + self._f + " ] -> " + self._right
100+
def __repr__(self) -> str:
101+
return f"{self._left_term} [ {self._f} ] -> {self._right_term}"
102102

103-
def __eq__(self, other):
104-
return other.is_consumption() and other.left_term == \
105-
self.left_term and other.right == self.right and \
106-
other.f_parameter() == self.f_parameter
103+
def __eq__(self, other: Any) -> bool:
104+
if not isinstance(other, ConsumptionRule):
105+
return False
106+
return other.left_term == self.left_term \
107+
and other.right_term == self.right_term \
108+
and other.f_parameter == self.f_parameter

pyformlang/indexed_grammar/duplication_rule.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
A representation of a duplication rule, i.e. a rule that duplicates the stack
33
"""
44

5-
from typing import Any, Iterable, AbstractSet, Tuple
5+
from typing import List, Set, Any
6+
7+
from pyformlang.cfg import Variable, Terminal
8+
from pyformlang.cfg.utils import to_variable
9+
from pyformlang.cfg.cfg_object import CFGObject
610

711
from .reduced_rule import ReducedRule
812

@@ -22,66 +26,60 @@ class DuplicationRule(ReducedRule):
2226
"""
2327

2428
@property
25-
def production(self):
29+
def f_parameter(self) -> Terminal:
2630
raise NotImplementedError
2731

2832
@property
29-
def right_term(self):
33+
def production(self) -> Terminal:
3034
raise NotImplementedError
3135

3236
@property
33-
def f_parameter(self):
37+
def right_term(self) -> CFGObject:
3438
raise NotImplementedError
3539

36-
def __init__(self, left_term, right_term0, right_term1):
37-
self._left_term = left_term
38-
self._right_terms = (right_term0, right_term1)
40+
def __init__(self,
41+
left_term: Any,
42+
right_term0: Any,
43+
right_term1: Any) -> None:
44+
self._left_term = to_variable(left_term)
45+
self._right_terms = (to_variable(right_term0),
46+
to_variable(right_term1))
3947

40-
def is_duplication(self) -> bool:
41-
"""Whether the rule is a duplication rule or not
48+
@property
49+
def left_term(self) -> Variable:
50+
"""Gives the non-terminal on the left of the rule
4251
4352
Returns
44-
----------
45-
is_duplication : bool
46-
Whether the rule is a duplication rule or not
53+
---------
54+
left_term : any
55+
The left term of the rule
4756
"""
48-
return True
57+
return self._left_term
4958

5059
@property
51-
def right_terms(self) -> Tuple[Any, Any]:
60+
def right_terms(self) -> List[CFGObject]:
5261
"""Gives the non-terminals on the right of the rule
5362
5463
Returns
5564
---------
5665
right_terms : iterable of any
5766
The right terms of the rule
5867
"""
59-
return self._right_terms
68+
return list(self._right_terms)
6069

6170
@property
62-
def left_term(self) -> Any:
63-
"""Gives the non-terminal on the left of the rule
64-
65-
Returns
66-
---------
67-
left_term : any
68-
The left term of the rule
69-
"""
70-
return self._left_term
71-
72-
@property
73-
def non_terminals(self) -> Iterable[Any]:
71+
def non_terminals(self) -> Set[Variable]:
7472
"""Gives the set of non-terminals used in this rule
7573
7674
Returns
7775
---------
7876
non_terminals : iterable of any
7977
The non terminals used in this rule
8078
"""
81-
return [self._left_term, self._right_terms[0], self._right_terms[1]]
79+
return {self._left_term, *self._right_terms}
8280

8381
@property
84-
def terminals(self) -> AbstractSet[Any]:
82+
def terminals(self) -> Set[Terminal]:
8583
"""Gets the terminals used in the rule
8684
8785
Returns
@@ -91,11 +89,13 @@ def terminals(self) -> AbstractSet[Any]:
9189
"""
9290
return set()
9391

94-
def __repr__(self):
92+
def __repr__(self) -> str:
9593
"""Gives a string representation of the rule, ignoring the sigmas"""
96-
return self._left_term + " -> " + self._right_terms[0] + \
97-
" " + self._right_terms[1]
98-
99-
def __eq__(self, other):
100-
return other.is_duplication() and other.left_term == \
101-
self._left_term and other.right_terms == self.right_terms
94+
return f"{self._left_term} -> \
95+
{self._right_terms[0]} {self._right_terms[1]}"
96+
97+
def __eq__(self, other: Any) -> bool:
98+
if not isinstance(other, DuplicationRule):
99+
return False
100+
return other.left_term == self._left_term \
101+
and other.right_terms == self.right_terms

0 commit comments

Comments
 (0)