symbols.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "symbols.H"
27 #include "IStringStream.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 void Foam::symbols::tokeniser::push(const token& t)
32 {
33  label end = (start_+size_)%tokens_.size();
34  tokens_[end] = t;
35  if (size_ == tokens_.size())
36  {
37  start_ = tokens_.fcIndex(start_);
38  }
39  else
40  {
41  size_++;
42  }
43 }
44 
45 
46 Foam::token Foam::symbols::tokeniser::pop()
47 {
48  token t = tokens_[start_];
49  start_ = tokens_.fcIndex(start_);
50  --size_;
51  return t;
52 }
53 
54 
55 void Foam::symbols::tokeniser::unpop(const token& t)
56 {
57  ++size_;
58  start_ = tokens_.rcIndex(start_);
59  tokens_[start_] = t;
60 }
61 
62 
63 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
64 
66 :
67  is_(is),
68  tokens_(100),
69  start_(0),
70  size_(0)
71 {}
72 
73 
74 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
75 
77 {
78  return is_;
79 }
80 
81 
83 {
84  return size_ || is_.good();
85 }
86 
87 
89 {
90  if (size_ == 0)
91  {
92  token t(is_);
93 
94  if (t.isWord())
95  {
96  splitWord(t.wordToken());
97  return pop();
98  }
99  else
100  {
101  return t;
102  }
103  }
104  else
105  {
106  return pop();
107  }
108 }
109 
110 
112 {
113  if (size_ == 0)
114  {
115  push(t);
116  }
117  else
118  {
119  unpop(t);
120  }
121 }
122 
123 
125 {
126  size_t start = 0;
127  for (size_t i=0; i<w.size(); ++i)
128  {
129  if (!valid(w[i]))
130  {
131  if (i > start)
132  {
133  word subWord = w(start, i-start);
134  if (isdigit(subWord[0]) || subWord[0] == token::SUBTRACT)
135  {
136  push(token(readScalar(IStringStream(subWord)())));
137  }
138  else
139  {
140  push(token(subWord));
141  }
142  }
143  if (w[i] != token::SPACE)
144  {
145  if (isdigit(w[i]))
146  {
147  push(token(readScalar(IStringStream(w[i])())));
148  }
149  else
150  {
151  push(token::punctuationToken(w[i]));
152  }
153  }
154  start = i+1;
155  }
156  }
157  if (start < w.size())
158  {
159  word subWord = w(start, w.size()-start);
160  if (isdigit(subWord[0]) || subWord[0] == token::SUBTRACT)
161  {
162  push(token(readScalar(IStringStream(subWord)())));
163  }
164  else
165  {
166  push(token(subWord));
167  }
168  }
169 }
170 
171 
173 {
174  return
175  (
176  !isspace(c)
177  && c != '"' // string quote
178  && c != '\'' // string quote
179  && c != '/' // divide
180  && c != ';' // end statement
181  && c != '{' // begin sub-dictionary
182  && c != '}' // end sub-dictionary
183  && c != '(' // begin expression
184  && c != ')' // end expression
185  && c != '[' // begin dimensions/units
186  && c != ']' // end dimensions/units
187  && c != ':' // separate dimensions/units
188  && c != '^' // power
189  && c != '*' // multiply
190  );
191 }
192 
193 
195 {
196  if (!t.isPunctuation())
197  {
198  return 0;
199  }
200  else if
201  (
202  t.pToken() == token::MULTIPLY
203  || t.pToken() == token::DIVIDE
204  )
205  {
206  return 2;
207  }
208  else if (t.pToken() == '^')
209  {
210  return 3;
211  }
212  else
213  {
214  return 0;
215  }
216 }
217 
218 
219 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
token nextToken()
...
Definition: symbols.C:88
bool hasToken() const
...
Definition: symbols.C:82
static bool valid(char c)
...
Definition: symbols.C:172
tokeniser(Istream &)
Construct for an input stream.
Definition: symbols.C:65
static label priority(const token &t)
...
Definition: symbols.C:194
void splitWord(const word &)
...
Definition: symbols.C:124
Istream & stream()
Access the stream.
Definition: symbols.C:76
void putBack(const token &)
...
Definition: symbols.C:111
A token holds items read from Istream.
Definition: token.H:73
bool isPunctuation() const
Definition: tokenI.H:280
punctuationToken
Standard punctuation tokens.
Definition: token.H:102
@ DIVIDE
Definition: token.H:126
@ SUBTRACT
Definition: token.H:124
@ MULTIPLY
Definition: token.H:125
punctuationToken pToken() const
Definition: tokenI.H:285
bool isWord() const
Definition: tokenI.H:298
const word & wordToken() const
Definition: tokenI.H:303
A class for handling words, derived from string.
Definition: word.H:62
bool valid(const PtrList< ModelType > &l)
const dimensionedScalar c
Speed of light in a vacuum.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:75
bool isspace(char c)
Definition: char.H:53