1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.themis.lethe.analysis;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
21 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisScanner.ThemisAnalysisSource;
22
23 import java.util.ArrayDeque;
24 import java.util.Deque;
25
26
27
28
29 public class ThemisAnalysisStack
30 implements ThemisAnalysisSource {
31
32
33
34 private final Deque<ThemisAnalysisElement> theStack;
35
36
37
38
39
40
41 ThemisAnalysisStack(final Deque<ThemisAnalysisElement> pStack) {
42 theStack = pStack;
43 }
44
45
46
47
48
49
50 ThemisAnalysisStack(final ThemisAnalysisElement pElement) {
51 theStack = new ArrayDeque<>();
52 theStack.add(pElement);
53 }
54
55 @Override
56 public boolean hasLines() {
57 return !theStack.isEmpty();
58 }
59
60 @Override
61 public ThemisAnalysisElement popNextLine() throws OceanusException {
62
63 if (theStack.isEmpty()) {
64 throw new ThemisDataException("No more lines");
65 }
66
67
68 return theStack.removeFirst();
69 }
70
71 @Override
72 public void pushLine(final ThemisAnalysisElement pLine) {
73
74 theStack.offerFirst(pLine);
75 }
76
77
78
79
80
81
82 public boolean isEmpty() {
83 return theStack.isEmpty()
84 || (theStack.size() == 1 && theStack.peekFirst().toString().length() == 0);
85 }
86
87
88
89
90
91
92 ThemisAnalysisElement peekLastLine() {
93 return theStack.peekLast();
94 }
95
96
97
98
99
100
101 public int size() {
102 return theStack.size();
103 }
104
105
106
107
108
109
110
111 public ThemisAnalysisStack extractParentheses() throws OceanusException {
112
113 final ThemisAnalysisLine myLine = (ThemisAnalysisLine) popNextLine();
114 final ThemisAnalysisScanner myScanner = new ThemisAnalysisScanner(this);
115 final Deque<ThemisAnalysisElement> myParenthesised = myScanner.scanForParenthesis(myLine);
116 final ThemisAnalysisStack myResult = new ThemisAnalysisStack(myParenthesised);
117
118
119 myResult.stripStartChar(ThemisAnalysisChar.PARENTHESIS_OPEN);
120 myResult.stripEndChar(ThemisAnalysisChar.PARENTHESIS_CLOSE);
121
122
123 return myResult;
124 }
125
126
127
128
129
130
131
132 public ThemisAnalysisStack extractArray() throws OceanusException {
133
134 final ThemisAnalysisLine myLine = (ThemisAnalysisLine) popNextLine();
135 final ThemisAnalysisScanner myScanner = new ThemisAnalysisScanner(this);
136 final Deque<ThemisAnalysisElement> myArray = myScanner.scanForArray(myLine);
137 final ThemisAnalysisStack myResult = new ThemisAnalysisStack(myArray);
138
139
140 myResult.stripStartChar(ThemisAnalysisChar.ARRAY_OPEN);
141 myResult.stripEndChar(ThemisAnalysisChar.ARRAY_CLOSE);
142
143
144 return myResult;
145 }
146
147
148
149
150
151
152
153 public boolean startsWithChar(final char pChar) {
154 return !theStack.isEmpty()
155 && ((ThemisAnalysisLine) theStack.peekFirst()).startsWithChar(pChar);
156 }
157
158
159
160
161
162
163 public void stripStartChar(final char pChar) {
164 if (!theStack.isEmpty()) {
165 ((ThemisAnalysisLine) theStack.peekFirst()).stripStartChar(pChar);
166 }
167 }
168
169
170
171
172
173
174 public void stripEndChar(final char pChar) {
175 if (!theStack.isEmpty()) {
176 ((ThemisAnalysisLine) theStack.peekLast()).stripEndChar(pChar);
177 }
178 }
179
180
181
182
183
184
185 public void rebuild(final Deque<ThemisAnalysisElement> pSource) {
186
187 theStack.clear();
188
189
190 for (ThemisAnalysisElement myElement : pSource) {
191 theStack.offerLast(myElement);
192 }
193 }
194
195 @Override
196 public String toString() {
197 return ThemisAnalysisBuilder.formatLines(theStack);
198 }
199 }