This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
75 lines (64 loc) · 2.08 KB
/
model.py
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
67
68
69
70
71
72
73
74
75
# override a DFA model
# the self.model used in MyParser class of filter_content.py
class model:
"""
# the self.model used in MyParser class of filter_content.py
# setup an instance with push() method before use it
lamF = lambda *pargs: False
lamN = lambda *pargs: None
"""
def __init__(self):
self.stack = []
self.pos = 0
def push(self, *funcs):
""" To store tuples of funcs in stack
at most 4 funcs: (nextCond, prevCond, handleTag, handleData)
if there is no func, set None in position
otherwise all the funcs after that will be None
handleTag, handleData:
handle the data in the state
nextCond, prevCond
check whether goto next state.
the return value(num) decide next state will be.
"""
self.stack.append(funcs)
""" To return handle func to user """
def getHandleTag(self):
try:
#handle = self.stack[self.pos].handleTag
handle = self.stack[self.pos][2]
except IndexError:
handle = None
return handle
def getHandleData(self):
try:
#handle = self.stack[self.pos].handleData
handle = self.stack[self.pos][3]
except IndexError:
handle = None
return handle
""" To move the current state """
def prev(self, *pargs):
try:
#func = self.stack[self.pos].prevCond
func = self.stack[self.pos][1]
if not func:
return
except IndexError:
pass
else:
check = func(*pargs)
if check:
self.pos -= int(check)
def next(self, *pargs):
try:
#func = self.stack[self.pos].nextCond
func = self.stack[self.pos][0]
if not func:
return
except IndexError:
pass
else:
check = func(*pargs)
if check:
self.pos += int(check)