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-2026 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(), t.lineNumber());
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 
124 void Foam::symbols::tokeniser::splitWord(const word& w, const label lineNumber)
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  const scalar s = readScalar(IStringStream(subWord)());
137  push(token(s, lineNumber));
138  }
139  else
140  {
141  push(token(subWord, lineNumber));
142  }
143  }
144  if (w[i] != token::SPACE)
145  {
146  if (isdigit(w[i]))
147  {
148  const scalar s = readScalar(IStringStream(w[i])());
149  push(token(s, lineNumber));
150  }
151  else
152  {
153  push(token(token::punctuationToken(w[i]), lineNumber));
154  }
155  }
156  start = i+1;
157  }
158  }
159  if (start < w.size())
160  {
161  word subWord = w(start, w.size()-start);
162  if (isdigit(subWord[0]) || subWord[0] == token::SUBTRACT)
163  {
164  push(token(readScalar(IStringStream(subWord)()), lineNumber));
165  }
166  else
167  {
168  push(token(subWord, lineNumber));
169  }
170  }
171 }
172 
173 
175 {
176  return
177  (
178  !isspace(c)
179  && c != '"' // string quote
180  && c != '\'' // string quote
181  && c != '/' // divide
182  && c != ';' // end statement
183  && c != '{' // begin sub-dictionary
184  && c != '}' // end sub-dictionary
185  && c != '(' // begin expression
186  && c != ')' // end expression
187  && c != '[' // begin dimensions/units
188  && c != ']' // end dimensions/units
189  && c != ':' // separate dimensions/units
190  && c != '^' // power
191  && c != '*' // multiply
192  );
193 }
194 
195 
197 {
198  if (!t.isPunctuation())
199  {
200  return 0;
201  }
202  else if
203  (
204  t.pToken() == token::MULTIPLY
205  || t.pToken() == token::DIVIDE
206  )
207  {
208  return 2;
209  }
210  else if (t.pToken() == '^')
211  {
212  return 3;
213  }
214  else
215  {
216  return 0;
217  }
218 }
219 
220 
221 // ************************************************************************* //
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
void splitWord(const word &, const label)
...
Definition: symbols.C:124
bool hasToken() const
...
Definition: symbols.C:82
static bool valid(char c)
...
Definition: symbols.C:174
tokeniser(Istream &)
Construct for an input stream.
Definition: symbols.C:65
static label priority(const token &t)
...
Definition: symbols.C:196
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:74
bool isPunctuation() const
Definition: tokenI.H:324
punctuationToken
Standard punctuation tokens.
Definition: token.H:103
@ DIVIDE
Definition: token.H:127
@ SUBTRACT
Definition: token.H:125
@ MULTIPLY
Definition: token.H:126
punctuationToken pToken() const
Definition: tokenI.H:329
bool isWord() const
Definition: tokenI.H:342
const word & wordToken() const
Definition: tokenI.H:347
label lineNumber() const
Definition: tokenI.H:847
A class for handling words, derived from string.
Definition: word.H:63
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
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