-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructorState.py
99 lines (94 loc) · 2.97 KB
/
ConstructorState.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from StringProcessor import StringProcessor
class ConstructorState(object):
_currentString = ''
@staticmethod
def nextState(s):
return ConstructorState
@staticmethod
def isStart():
return False
@staticmethod
def isEnd():
return False
@staticmethod
def isError():
return False
class StartState(ConstructorState):
@staticmethod
def isStart():
return True
@staticmethod
def nextState(s):
return super().nextState()
'''
class Statement:
def __init__(self):
self._childStatements = []
self._head = ''
self._tail = ''
def getHead(self):
return self._head
def setHead(self, head):
self._head = head
return self
def appendHead(self, append):
self._head += append
return self
def getTail(self):
return self._tail
def setTail(self, tail):
self._tail = tail
return self
def appendTail(self, append):
self._tail += append
return self
def addChildStatement(self, statement):
self._childStatements.append(statement)
return self
def getChildStatementsIter(self):
return iter(self._childStatements)
def getChildStatementAt(self, site):
return self._childStatements[site]
def getChildStatementCount(self):
return len(self._childStatements)
def removeChildStatement(self, statement):
if statement in self._childStatements:
self._childStatements.remove(statement)
return self
def splitStatements(s):
processor = StringProcessor(s)
resultList = []
tempStatement = Statement()
while True:
tempWord = processor.skipSpace().readWord()
if tempWord == '':
if not tempStatement == None:
raise RuntimeError('string ends incorrectly')
return resultList
if tempStatement == None:
tempStatement = Statement()
if tempWord == '"':
processor.unRead(1)
tempWord = processor.readStringWithWrapper('"')
if tempWord == '':
raise RuntimeError('can not find tail quote')
tempStatement.appendHead(tempWord)
elif tempWord == '{':
tempStatement.appendHead(tempWord)
processor.unRead(1)
tempWord = processor.readStringWithSameLayerBracket('{')
if tempWord == '':
raise RuntimeError('can not find tail brace')
tempWord = StringProcessor.cutHeadAndTail(tempWord)
for child in splitStatements(tempWord):
tempStatement.addChildStatement(child)
tempStatement.appendTail('}')
resultList.append(tempStatement)
tempStatement = None
elif tempWord == ';':
tempStatement.appendTail(tempWord)
resultList.append(tempStatement)
tempStatement = None
else:
tempStatement.appendHead(tempWord)
'''