Repository navigation
Expand file tree
/
Copy pathScannerDFANode.py
More file actions
66 lines (54 loc) · 1.9 KB
/
Copy pathScannerDFANode.py
File metadata and controls
66 lines (54 loc) · 1.9 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from tokens import Tokens
def make_path(string, root, letter_node, token):
node = root
dotorand = False
for i in range(len(string)):
c = string[i]
if c not in node.nx:
node.nx[c] = Node(string + ' ' + str(i))
node = node.nx[c]
if c == '.' or c == '&':
dotorand = True
if i + 1 != len(string):
if dotorand:
node.other_case(root)
else:
node.letter_case(letter_node)
node.other_case(root)
node.other_token(Tokens.IDENTIFIER)
node.other_case(root)
node.other_token(token)
PANIC_MODE_SCANNER_CHARS = [';', '}', ')']
class Node(object):
def __init__(self, name):
self.name = name
self.nx = dict()
self.ow = None
self.other = None
self.letter_goal = None
# print(self.nx)
def letter_case(self, sink):
self.letter_goal = sink
def add_edge(self, param, sink):
# print(self.name, param, sink)
for c in param:
if c in self.nx:
if self.nx[c] != sink:
raise Exception('duplicate edge in dfa with different sinks, node name is: {}'.format(self.name))
else:
self.nx[c] = sink
def other_case(self, sink):
self.ow = sink
def get_next_node(self, char, root):
if char in self.nx:
return self.nx[char], None
if self.letter_goal is not None and (str.isalpha(char) or str.isdigit(char)):
return self.letter_goal, None
if self.ow is not None:
return self.ow, self.other
if char in PANIC_MODE_SCANNER_CHARS: # panic mode error handling in dfa
print('Error: SKIPPED INPUT UNTIL {}'.format(char))
return root, None
return self, None
def other_token(self, other_token):
self.other = other_token