-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d09b85f
commit 0941c12
Showing
21 changed files
with
153 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from codelimit.common.gsm.Automata import Automata | ||
from typing import Any | ||
|
||
from codelimit.common.gsm.NFA import NFA | ||
from codelimit.common.gsm.Operator import Operator | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class Atom(Operator): | ||
def __init__(self, item: str): | ||
def __init__(self, item: Any): | ||
self.item = item | ||
|
||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
start = State() | ||
accepting = State() | ||
start.transition.append((self.item, accepting)) | ||
stack.append(Automata(start, accepting)) | ||
stack.append(NFA(start, accepting)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
from abc import abstractmethod, ABC | ||
|
||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class Automata: | ||
def __init__(self, start: State, accepting: State | list[State]): | ||
class Automata(ABC): | ||
def __init__(self, start: State): | ||
self.start = start | ||
self.accepting = accepting | ||
|
||
@abstractmethod | ||
def is_accepting(self, state: State) -> bool: | ||
if isinstance(self.accepting, list): | ||
return state in self.accepting | ||
else: | ||
return state == self.accepting | ||
|
||
def __str__(self): | ||
return f'Automata(start={self.start}, accepting={self.accepting})' | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.NFA import NFA | ||
from codelimit.common.gsm.Operator import Operator | ||
|
||
|
||
class Concat(Operator): | ||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
if len(stack) < 2: | ||
return | ||
nfa1 = stack.pop() | ||
nfa2 = stack.pop() | ||
nfa2.accepting.assign(nfa1.start) | ||
nfa = Automata(nfa2.start, nfa1.accepting) | ||
nfa = NFA(nfa2.start, nfa1.accepting) | ||
stack.append(nfa) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class DFA(Automata): | ||
def __init__(self, start: State, accepting: list[State]): | ||
super().__init__(start) | ||
self.accepting = accepting | ||
|
||
def is_accepting(self, state: State) -> bool: | ||
return state in self.accepting | ||
|
||
def __str__(self): | ||
return f'DFA(start={self.start}, accepting={self.accepting})' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class NFA(Automata): | ||
def __init__(self, start: State, accepting: State): | ||
super().__init__(start) | ||
self.accepting = accepting | ||
|
||
def is_accepting(self, state: State) -> bool: | ||
return state == self.accepting | ||
|
||
def __str__(self): | ||
return f'NFA(start={self.start}, accepting={self.accepting})' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
from codelimit.common.gsm.Expression import expression_to_nfa | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.Expression import expression_to_nfa, Expression | ||
from codelimit.common.gsm.NFA import NFA | ||
from codelimit.common.gsm.Operator import Operator | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class OneOrMore(Operator): | ||
def __init__(self, expression: Operator | str | list[Operator | str]): | ||
def __init__(self, expression: Expression): | ||
self.expression = expression if isinstance(expression, list) else [expression] | ||
|
||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
start = State() | ||
nfa = expression_to_nfa(self.expression) | ||
accepting = State() | ||
start.epsilon_transitions = [nfa.start] | ||
nfa.accepting.epsilon_transitions = [nfa.start, accepting] | ||
stack.append(Automata(start, accepting)) | ||
stack.append(NFA(start, accepting)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.NFA import NFA | ||
|
||
|
||
class Operator(ABC): | ||
@abstractmethod | ||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
from codelimit.common.gsm.Expression import expression_to_nfa | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.Expression import expression_to_nfa, Expression | ||
from codelimit.common.gsm.NFA import NFA | ||
from codelimit.common.gsm.Operator import Operator | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class Optional(Operator): | ||
def __init__(self, expression: Operator | str | list[Operator | str]): | ||
def __init__(self, expression: Expression): | ||
self.expression = expression if isinstance(expression, list) else [expression] | ||
|
||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
start = State() | ||
nfa = expression_to_nfa(self.expression) | ||
accepting = State() | ||
start.epsilon_transitions = [nfa.start, accepting] | ||
nfa.accepting.epsilon_transitions = [accepting] | ||
stack.append(Automata(start, accepting)) | ||
stack.append(NFA(start, accepting)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
from codelimit.common.gsm.State import State | ||
from codelimit.common.gsm.Automata import Automata | ||
|
||
|
||
class Pattern: | ||
def __init__(self, start: int, state: State): | ||
def __init__(self, start: int, automata: Automata): | ||
self.start = start | ||
self.state = state | ||
self.automata = automata | ||
self.state = automata.start | ||
self.tokens: list = [] | ||
|
||
def is_accepting(self): | ||
return self.automata.is_accepting(self.state) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Generic, TypeVar | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
class Predicate(ABC, Generic[T]): | ||
|
||
@abstractmethod | ||
def accept(self, item: T) -> bool: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
from codelimit.common.gsm.Expression import expression_to_nfa | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.Expression import expression_to_nfa, Expression | ||
from codelimit.common.gsm.NFA import NFA | ||
from codelimit.common.gsm.Operator import Operator | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class Union(Operator): | ||
def __init__(self, left: Operator | str | list[Operator | str], right: Operator | str | list[Operator | str]): | ||
def __init__(self, left: Expression, right: Expression): | ||
self.left = left if isinstance(left, list) else [left] | ||
self.right = right if isinstance(right, list) else [right] | ||
|
||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
start = State() | ||
nfa1 = expression_to_nfa(self.left) | ||
nfa2 = expression_to_nfa(self.right) | ||
start.epsilon_transitions = [nfa1.start, nfa2.start] | ||
accepting = State() | ||
nfa1.accepting.epsilon_transitions = [accepting] | ||
nfa2.accepting.epsilon_transitions = [accepting] | ||
stack.append(Automata(start, accepting)) | ||
stack.append(NFA(start, accepting)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
from codelimit.common.gsm.Expression import expression_to_nfa | ||
from codelimit.common.gsm.Automata import Automata | ||
from codelimit.common.gsm.Expression import expression_to_nfa, Expression | ||
from codelimit.common.gsm.NFA import NFA | ||
from codelimit.common.gsm.Operator import Operator | ||
from codelimit.common.gsm.State import State | ||
|
||
|
||
class ZeroOrMore(Operator): | ||
def __init__(self, expression: Operator | str | list[Operator | str]): | ||
def __init__(self, expression: Expression): | ||
self.expression = expression if isinstance(expression, list) else [expression] | ||
|
||
def apply(self, stack: list[Automata]): | ||
def apply(self, stack: list[NFA]): | ||
start = State() | ||
nfa = expression_to_nfa(self.expression) | ||
accepting = State() | ||
start.epsilon_transitions = [nfa.start, accepting] | ||
nfa.accepting.epsilon_transitions = [nfa.start, accepting] | ||
stack.append(Automata(start, accepting)) | ||
stack.append(NFA(start, accepting)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.