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  if (t.isWord())
94  {
95  splitWord(t.wordToken());
96  return pop();
97  }
98  else
99  {
100  return t;
101  }
102  }
103  else
104  {
105  return pop();
106  }
107 }
108 
109 
111 {
112  if (size_ == 0)
113  {
114  push(t);
115  }
116  else
117  {
118  unpop(t);
119  }
120 }
121 
122 
124 {
125  size_t start = 0;
126  for (size_t i=0; i<w.size(); ++i)
127  {
128  if (!valid(w[i]))
129  {
130  if (i > start)
131  {
132  word subWord = w(start, i-start);
133  if (isdigit(subWord[0]) || subWord[0] == token::SUBTRACT)
134  {
135  push(token(readScalar(IStringStream(subWord)())));
136  }
137  else
138  {
139  push(token(subWord));
140  }
141  }
142  if (w[i] != token::SPACE)
143  {
144  if (isdigit(w[i]))
145  {
146  push(token(readScalar(IStringStream(w[i])())));
147  }
148  else
149  {
150  push(token::punctuationToken(w[i]));
151  }
152  }
153  start = i+1;
154  }
155  }
156  if (start < w.size())
157  {
158  word subWord = w(start, w.size()-start);
159  if (isdigit(subWord[0]) || subWord[0] == token::SUBTRACT)
160  {
161  push(token(readScalar(IStringStream(subWord)())));
162  }
163  else
164  {
165  push(token(subWord));
166  }
167  }
168 }
169 
170 
172 {
173  return
174  (
175  !isspace(c)
176  && c != '"' // string quote
177  && c != '\'' // string quote
178  && c != '/' // div
179  && c != ';' // end statement
180  && c != '{' // beg subdict
181  && c != '}' // end subdict
182  && c != '(' // beg expr
183  && c != ')' // end expr
184  && c != '[' // beg dim
185  && c != ']' // end dim
186  && c != '^' // power
187  && c != '*' // mult
188  );
189 }
190 
191 
193 {
194  if (!t.isPunctuation())
195  {
196  return 0;
197  }
198  else if
199  (
200  t.pToken() == token::MULTIPLY
201  || t.pToken() == token::DIVIDE
202  )
203  {
204  return 2;
205  }
206  else if (t.pToken() == '^')
207  {
208  return 3;
209  }
210  else
211  {
212  return 0;
213  }
214 }
215 
216 
217 // ************************************************************************* //
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:171
tokeniser(Istream &)
Construct for an input stream.
Definition: symbols.C:65
static label priority(const token &t)
...
Definition: symbols.C:192
void splitWord(const word &)
...
Definition: symbols.C:123
Istream & stream()
Access the stream.
Definition: symbols.C:76
void putBack(const token &)
...
Definition: symbols.C:110
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