-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultscanner.cpp
More file actions
118 lines (94 loc) · 2.65 KB
/
defaultscanner.cpp
File metadata and controls
118 lines (94 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This file is part of the ParsePlot library
ParsePlot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ParsePlot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser Public License for more details.
You should have received a copy of the GNU Lesser Public License
along with ParsePlot. If not, see <http://www.gnu.org/licenses/>.
*/
#include "defaultscanner.h"
#include "stringmodifier.h"
namespace ParsePlot{
/* ------ Scanner implementation ------ */
DefaultScanner::~DefaultScanner()
{
if (!tokQueue.empty())
{
deque<Token*>::iterator it;
for (it=tokQueue.begin(); it!=tokQueue.end(); ++it)
{
delete *it;
}
}
}
void DefaultScanner::Scan(const string &aInput)
{
// Make a deep copy of the string
sInput.SetString(aInput);
// Push all tokens to the queue
while (PushNext()) {};
current_elem = tokQueue.begin();
}
Token* DefaultScanner::Next()
{
return *(current_elem++);
}
bool DefaultScanner::PushNext()
{
// Get start symbol of next token
char c = sInput.pop_front();
tokQueue.push_back( ExtractToken(c) );
// Return true if there are more characters in the stream
if (sInput.Length() >= 1)
return true;
else
return false;
}
Token* DefaultScanner::ExtractToken(char symStart)
{
StringModifier sm;
ParseToken current = GetTokenType(symStart);
sm.push_back(symStart);
while ( GetTokenType(sInput.front()) == current && !sInput.Empty() )
{
sm.push_back(sInput.pop_front());
}
return new Token(current, sm.GetString());
}
ParseToken DefaultScanner::GetTokenType(char tokenPart)
{
if ( (tokenPart >= NUM_OFFSET && tokenPart <= NUM_OFFSET + NUM_RANGE) || tokenPart == NUM_DECIMAL_POINT )
return TOK_NUMERIC;
else if (tokenPart == LPAREN)
return TOK_LPAREN;
else if (tokenPart == RPAREN)
return TOK_RPAREN;
else if (tokenPart == OP_MULT || tokenPart == OP_ADD || tokenPart == OP_SUB || tokenPart == OP_DIV)
return TOK_OPERATOR;
else
return TOK_UNMATCHED;
}
bool DefaultScanner::HasNext()
{
return ( current_elem != tokQueue.end() );
}
/* ------ Token implementation ------ */
Token::Token(ParseToken aPt, string aStr)
{
pt = aPt;
str = aStr;
}
ParseToken Token::GetToken()
{
return this->pt;
}
string Token::GetValue()
{
return this->str;
}
}