-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigitReadingState.cpp
More file actions
34 lines (26 loc) · 1.09 KB
/
Copy pathDigitReadingState.cpp
File metadata and controls
34 lines (26 loc) · 1.09 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
#include "DigitReadingState.hpp"
#include "DigitCompleteState.hpp"
#include "ast/Parenthesis.hpp"
#include "characterType.hpp"
#include "InvalidFormulaException.hpp"
DigitReadingState::DigitReadingState(char initialDigit) {
numberBuffer << initialDigit;
}
void DigitReadingState::validate(char readCharacter, char nextCharacter) const {
if (isOperator(readCharacter)) {
throw InvalidFormulaException("Invalid character found " + std::string(1, readCharacter));
}
if (readCharacter == '.' && numberBuffer.str().find('.') != std::string::npos) {
throw InvalidFormulaException("Invalid character, duplicate decimal separator.");
}
}
std::shared_ptr<Transition> DigitReadingState::transition(char readCharacter, char nextCharacter) {
numberBuffer << readCharacter;
if (isOperator(nextCharacter) || nextCharacter == 0 || nextCharacter == PARENTHESIS_CHAR_CLOSE) {
return std::make_shared<DigitCompleteState>(numberBuffer.str())->getTransition();
}
return getTransition();
}
std::shared_ptr<Token> DigitReadingState::getToken() const {
return nullptr;
}