-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterReadingState.cpp
More file actions
42 lines (33 loc) · 1.44 KB
/
Copy pathCharacterReadingState.cpp
File metadata and controls
42 lines (33 loc) · 1.44 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
#include "CharacterReadingState.hpp"
#include "InvalidFormulaException.hpp"
#include "VarCompletedState.hpp"
#include "PostfixOperationState.hpp"
#include "ConstantCompletedState.hpp"
#include "characterType.hpp"
#include "ast/Parenthesis.hpp"
CharacterReadingState::CharacterReadingState(char initialDigit) {
numberBuffer << initialDigit;
}
void CharacterReadingState::validate(char readCharacter, char nextCharacter) const {
if (!isAlphabetic(readCharacter)) {
throw InvalidFormulaException("Invalid character found " + std::string(1, readCharacter));
}
}
std::shared_ptr<Transition> CharacterReadingState::transition(char readCharacter, char nextCharacter) {
numberBuffer << readCharacter;
if (nextCharacter == 0 || isOperator(nextCharacter) ||
nextCharacter == PARENTHESIS_CHAR_CLOSE ||
(nextCharacter == PARENTHESIS_CHAR_OPEN && containsKeyword(numberBuffer.str()))) {
if (containsConstant(numberBuffer.str())) {
return std::make_shared<ConstantCompletedState>(numberBuffer.str())->getTransition();
} else if (containsKeyword(numberBuffer.str())) {
return std::make_shared<PostfixOperationState>(numberBuffer.str())->getTransition();
} else {
return std::make_shared<VarCompletedState>(numberBuffer.str())->getTransition();
}
}
return getTransition();
}
std::shared_ptr<Token> CharacterReadingState::getToken() const {
return nullptr;
}