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   * DoWhile construct.
30   */
31  public class ThemisAnalysisDoWhile
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       * @throws OceanusException on error
63       */
64      ThemisAnalysisDoWhile(final ThemisAnalysisParser pParser) throws OceanusException {
65          /* Access details from parser */
66          theParent = pParser.getParent();
67          theDataMap = new ThemisAnalysisDataMap(theParent.getDataMap());
68  
69          /* Create the arrays */
70          final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
71  
72          /* Parse trailers */
73          final ThemisAnalysisLine myLine = (ThemisAnalysisLine) pParser.popNextLine();
74          myLine.stripStartSequence(ThemisAnalysisKeyWord.WHILE.toString());
75          theCondition = new ThemisAnalysisStatement(pParser, myLine);
76          theNumLines = theCondition.getNumLines() + 1;
77  
78          /* Create a parser */
79          theContents = new ArrayDeque<>();
80          final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, this);
81          myParser.processLines();
82      }
83  
84      @Override
85      public Deque<ThemisAnalysisElement> getContents() {
86          return theContents;
87      }
88  
89      @Override
90      public Iterator<ThemisAnalysisStatement> statementIterator() {
91          return Collections.singleton(theCondition).iterator();
92      }
93  
94      @Override
95      public ThemisAnalysisContainer getParent() {
96          return theParent;
97      }
98  
99      @Override
100     public void setParent(final ThemisAnalysisContainer pParent) {
101         theParent = pParent;
102         theDataMap.setParent(pParent.getDataMap());
103     }
104 
105     @Override
106     public ThemisAnalysisDataMap getDataMap() {
107         return theDataMap;
108     }
109 
110     @Override
111     public int getNumLines() {
112         return theNumLines;
113     }
114 
115     @Override
116     public String toString() {
117         return theCondition.toString();
118     }
119 }