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.lethe.analysis.ThemisAnalysisContainer.ThemisAnalysisAdoptable;
21  import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisStatement.ThemisAnalysisStatementHolder;
22  
23  import java.util.ArrayDeque;
24  import java.util.Collections;
25  import java.util.Deque;
26  import java.util.Iterator;
27  
28  /**
29   * While construct.
30   */
31  public class ThemisAnalysisWhile
32          implements ThemisAnalysisContainer, ThemisAnalysisAdoptable, ThemisAnalysisStatementHolder {
33      /**
34       * The condition.
35       */
36      private final ThemisAnalysisStatement theCondition;
37  
38      /**
39       * The contents.
40       */
41      private final Deque<ThemisAnalysisElement> theContents;
42  
43      /**
44       * The number of lines.
45       */
46      private final int theNumLines;
47  
48      /**
49       * The dataMap.
50       */
51      private final ThemisAnalysisDataMap theDataMap;
52  
53      /**
54       * The parent.
55       */
56      private ThemisAnalysisContainer theParent;
57  
58      /**
59       * Constructor.
60       *
61       * @param pParser the parser
62       * @param pLine   the initial while line
63       * @throws OceanusException on error
64       */
65      ThemisAnalysisWhile(final ThemisAnalysisParser pParser,
66                          final ThemisAnalysisLine pLine) throws OceanusException {
67          /* Access details from parser */
68          theParent = pParser.getParent();
69          theDataMap = new ThemisAnalysisDataMap(theParent.getDataMap());
70  
71          /* Parse the condition */
72          final Deque<ThemisAnalysisElement> myHeaders = ThemisAnalysisBuilder.parseHeaders(pParser, pLine);
73          theCondition = new ThemisAnalysisStatement(myHeaders);
74          theNumLines = myHeaders.size() + 1;
75  
76          /* Parse the body */
77          final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
78  
79          /* Create a parser */
80          theContents = new ArrayDeque<>();
81          final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, this);
82          myParser.processLines();
83      }
84  
85      @Override
86      public Deque<ThemisAnalysisElement> getContents() {
87          return theContents;
88      }
89  
90      @Override
91      public Iterator<ThemisAnalysisStatement> statementIterator() {
92          return Collections.singleton(theCondition).iterator();
93      }
94  
95      @Override
96      public ThemisAnalysisContainer getParent() {
97          return theParent;
98      }
99  
100     @Override
101     public void setParent(final ThemisAnalysisContainer pParent) {
102         theParent = pParent;
103         theDataMap.setParent(pParent.getDataMap());
104     }
105 
106     @Override
107     public ThemisAnalysisDataMap getDataMap() {
108         return theDataMap;
109     }
110 
111     @Override
112     public int getNumLines() {
113         return theNumLines;
114     }
115 
116     @Override
117     public String toString() {
118         return theCondition.toString();
119     }
120 }