View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * Switch construct.
31   */
32  public class ThemisAnalysisSwitch
33          implements ThemisAnalysisContainer, ThemisAnalysisAdoptable, ThemisAnalysisStatementHolder {
34      /**
35       * The switch value.
36       */
37      private final ThemisAnalysisStatement theSwitch;
38  
39      /**
40       * The contents.
41       */
42      private final Deque<ThemisAnalysisElement> theContents;
43  
44      /**
45       * The number of lines.
46       */
47      private final int theNumLines;
48  
49      /**
50       * The parent.
51       */
52      private ThemisAnalysisContainer theParent;
53  
54      /**
55       * Constructor.
56       *
57       * @param pParser the parser
58       * @param pLine   the initial switch line
59       * @throws OceanusException on error
60       */
61      ThemisAnalysisSwitch(final ThemisAnalysisParser pParser,
62                           final ThemisAnalysisLine pLine) throws OceanusException {
63          /* Access details from parser */
64          theParent = pParser.getParent();
65  
66          /* Parse the switch */
67          final Deque<ThemisAnalysisElement> myHeaders = ThemisAnalysisBuilder.parseHeaders(pParser, pLine);
68          theNumLines = myHeaders.size() + 1;
69          theSwitch = new ThemisAnalysisStatement(myHeaders);
70  
71          /* Parse the body */
72          final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
73  
74          /* Create a parser */
75          theContents = new ArrayDeque<>();
76          final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, theParent);
77          processLines(myParser);
78      }
79  
80      /**
81       * process the lines.
82       *
83       * @param pParser the parser
84       * @throws OceanusException on error
85       */
86      void processLines(final ThemisAnalysisParser pParser) throws OceanusException {
87          /* Loop through the lines */
88          while (pParser.hasLines()) {
89              /* Access next line */
90              final ThemisAnalysisLine myLine = (ThemisAnalysisLine) pParser.popNextLine();
91  
92              /* Process comments and blanks */
93              final boolean processed = pParser.processCommentsAndBlanks(myLine)
94                      || pParser.processCase(this, myLine);
95  
96              /* If we haven't processed yet */
97              if (!processed) {
98                  /* We should never reach here */
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 }