forked from Aunsiels/pyformlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (30 loc) · 961 Bytes
/
utils.py
File metadata and controls
35 lines (30 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Utility for regex object creation."""
from .regex_objects import Symbol, Node, \
Empty, Concatenation, Union, KleeneStar, Epsilon
CONCATENATION_SYMBOLS = ["."]
UNION_SYMBOLS = ["|", "+"]
KLEENE_STAR_SYMBOLS = ["*"]
EPSILON_SYMBOLS = ["epsilon", "$"]
PARENTHESIS = ["(", ")"]
SPECIAL_SYMBOLS = CONCATENATION_SYMBOLS + \
UNION_SYMBOLS + \
KLEENE_STAR_SYMBOLS + \
EPSILON_SYMBOLS + \
PARENTHESIS
def to_node(value: str) -> Node:
"""Transforms a given value into a node."""
if not value:
res = Empty()
elif value in CONCATENATION_SYMBOLS:
res = Concatenation()
elif value in UNION_SYMBOLS:
res = Union()
elif value in KLEENE_STAR_SYMBOLS:
res = KleeneStar()
elif value in EPSILON_SYMBOLS:
res = Epsilon()
elif value[0] == "\\":
res = Symbol(value[1:])
else:
res = Symbol(value)
return res