-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automaton.h
45 lines (36 loc) · 1.21 KB
/
Automaton.h
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
#ifndef AUTOMATON_H
#define AUTOMATON_H
#include "Token.h"
class Automaton
{
protected:
int inputRead = 0;
int newLines = 0;
unsigned int index = 0;
TokenType type;
public:
// Default constructor -- since we have a constructor that takes a parameter,
// the compiler will autogenerate a default constructor if not explicit.
Automaton() : Automaton(TokenType::UNDEFINED) {}
Automaton(TokenType type) { this->type = type; }
virtual ~Automaton() {}
// Start the automaton and return the number of characters read
// read == 0 indicates the input was rejected
// read > 0 indicates the input was accepted
int Start(const std::string& input) {
newLines = 0;
inputRead = 0;
index = 0;
S0(input);
return inputRead;
}
// Every subclass must define this method
virtual void S0(const std::string& input) = 0;
void Serr() {
// Indicate the input was rejected
inputRead = 0;
}
virtual Token* CreateToken(std::string input, int lineNumber) { return new Token(type, input, lineNumber); }
int NewLinesRead() const { return newLines; }
};
#endif // AUTOMATON_H