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.ThemisAnalysisContainer.ThemisAnalysisAdoptable;
22 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisStatement.ThemisAnalysisStatementHolder;
23
24 import java.util.ArrayDeque;
25 import java.util.Collections;
26 import java.util.Deque;
27 import java.util.Iterator;
28
29
30
31
32 public class ThemisAnalysisSwitch
33 implements ThemisAnalysisContainer, ThemisAnalysisAdoptable, ThemisAnalysisStatementHolder {
34
35
36
37 private final ThemisAnalysisStatement theSwitch;
38
39
40
41
42 private final Deque<ThemisAnalysisElement> theContents;
43
44
45
46
47 private final int theNumLines;
48
49
50
51
52 private ThemisAnalysisContainer theParent;
53
54
55
56
57
58
59
60
61 ThemisAnalysisSwitch(final ThemisAnalysisParser pParser,
62 final ThemisAnalysisLine pLine) throws OceanusException {
63
64 theParent = pParser.getParent();
65
66
67 final Deque<ThemisAnalysisElement> myHeaders = ThemisAnalysisBuilder.parseHeaders(pParser, pLine);
68 theNumLines = myHeaders.size() + 1;
69 theSwitch = new ThemisAnalysisStatement(myHeaders);
70
71
72 final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
73
74
75 theContents = new ArrayDeque<>();
76 final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, theParent);
77 processLines(myParser);
78 }
79
80
81
82
83
84
85
86 void processLines(final ThemisAnalysisParser pParser) throws OceanusException {
87
88 while (pParser.hasLines()) {
89
90 final ThemisAnalysisLine myLine = (ThemisAnalysisLine) pParser.popNextLine();
91
92
93 final boolean processed = pParser.processCommentsAndBlanks(myLine)
94 || pParser.processCase(this, myLine);
95
96
97 if (!processed) {
98
99 throw new ThemisDataException("Unexpected code in switch");
100 }
101 }
102 }
103
104 @Override
105 public Deque<ThemisAnalysisElement> getContents() {
106 return theContents;
107 }
108
109 @Override
110 public Iterator<ThemisAnalysisStatement> statementIterator() {
111 return Collections.singleton(theSwitch).iterator();
112 }
113
114 @Override
115 public ThemisAnalysisContainer getParent() {
116 return theParent;
117 }
118
119 @Override
120 public void setParent(final ThemisAnalysisContainer pParent) {
121 theParent = pParent;
122 }
123
124 @Override
125 public int getNumLines() {
126 return theNumLines;
127 }
128
129 @Override
130 public String toString() {
131 return theSwitch.toString();
132 }
133 }