UnitStringLexer.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 //----------------------------------------------------------------------------
19 //----------------------------------------------------------------------------
20 #pragma once
21 
22 // Note: See top of CompoundUnit.h for a discussion of the lines below which export STL
23 // container instantiations. In this particular case, the compiler complains about the member
24 // variables "end" and "cursor". Note that exporting the iterator base must be done before
25 // exporting the iterator template itself.
26 #include <string>
27 
28 namespace biogears {
29 #pragma warning(disable : 4231)
32  : tokenID(TT_ERR)
33  , symbol()
34  , fltval(0.0)
35  {
36  }
38  : tokenID(src.tokenID)
39  , symbol(src.symbol)
40  , fltval(src.fltval)
41  {
42  }
43  std::string toString() const
44  {
45  switch (tokenID) {
46  case TT_IDENTIFIER:
47  return "TT_IDENTIFIER";
48  break;
49 
50  case TT_EXPONENT:
51  return "TT_EXPONENT";
52  break;
53 
54  case TT_FLOAT:
55  return "TT_FLOAT";
56  break;
57 
58  case TT_SLASH:
59  return "TT_SLASH";
60  break;
61 
62  case TT_PARENOPEN:
63  return "TT_PARENOPEN";
64  break;
65 
66  case TT_PARENCLOSE:
67  return "TT_PARENCLOSE";
68  break;
69 
70  case TT_ERR:
71  return "TT_ERR";
72  break;
73 
74  case TT_EOF:
75  return "TT_EOF";
76  break;
77 
78  default:
79  return "UNDEFINED";
80 
81  } // switch
82  } // toString
83 
84  // Enumerate the various Token Types
85  enum { TT_IDENTIFIER,
94  std::string symbol;
95  double fltval;
96 };
97 
99 public:
100  CUnitStringLexer(const std::string& src)
101  : source(src)
102  , scanNext(true)
103  , end(source.end())
104  , cursor(source.begin())
105  {
106  // Nothing else to do
107  }
108 
110  {
111  if (scanNext) {
112  ScanNextToken();
113  }
114  token = curToken;
115  scanNext = true;
116  }
117 
119  {
120  if (scanNext) {
121  ScanNextToken();
122  }
123  token = curToken;
124  scanNext = false;
125  }
126 
127 protected:
128  void ScanNextToken();
129 
130 private:
132 
133  std::string source;
135  bool scanNext; // Determines whether a call to PeekNextToken or GetNextToken needs to scan
136  std::string::iterator end;
137  std::string::iterator cursor; // current position in source string
138 };}
std::string::iterator cursor
Definition: UnitStringLexer.h:137
std::string toString() const
Definition: UnitStringLexer.h:43
Definition: UnitStringLexer.h:90
std::string source
Definition: UnitStringLexer.h:133
Definition: UnitStringLexer.h:91
SUnitStringToken(SUnitStringToken &src)
Definition: UnitStringLexer.h:37
SUnitStringToken()
Definition: UnitStringLexer.h:31
bool scanNext
Definition: UnitStringLexer.h:135
Definition: UnitStringLexer.h:89
Definition: UnitStringLexer.h:86
CUnitStringLexer(const std::string &src)
Definition: UnitStringLexer.h:100
Definition: UnitStringLexer.h:30
SUnitStringToken curToken
Definition: UnitStringLexer.h:134
Definition: UnitStringLexer.h:93
Definition: UnitStringLexer.h:87
std::string symbol
Definition: UnitStringLexer.h:94
void PeekNextToken(SUnitStringToken &token)
Definition: UnitStringLexer.h:118
Definition: UnitStringLexer.h:98
Definition: UnitStringLexer.h:92
enum biogears::SUnitStringToken::@0 tokenID
Definition: SEElectricalCircuit.h:18
std::string::iterator end
Definition: UnitStringLexer.h:136
Definition: UnitStringLexer.h:88
Definition: UnitStringLexer.h:85
double fltval
Definition: UnitStringLexer.h:95
void GetNextToken(SUnitStringToken &token)
Definition: UnitStringLexer.h:109
void ScanNextToken()
Definition: UnitStringLexer.cpp:24