-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.cpp
More file actions
61 lines (48 loc) · 1.25 KB
/
Copy pathScanner.cpp
File metadata and controls
61 lines (48 loc) · 1.25 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
#include "Scanner.h"
#include <iostream>
#include "StateMachine.h"
#include "Debug.h"
ScannerClass::ScannerClass(std::string filename){
mFin.open(filename);
mLineNumber = 1;
if(!mFin){
std::cout << mFin.get();
exit(1);
}
}
int ScannerClass::GetLineNumber(){
return mLineNumber;
}
TokenClass ScannerClass::GetNextToken(){
StateMachineClass stateMachine;
TokenType t;
std::string lexeme;
MachineState UpdatedState = START_STATE;
char c;
do{
c = mFin.get();
lexeme += c;
UpdatedState = stateMachine.UpdateState(c,t);
if (UpdatedState == START_STATE || UpdatedState == ENDFILE_STATE){
lexeme = "";
if (c == '\n'){
mLineNumber ++;
}
}
}while(UpdatedState != CANTMOVE_STATE);
mFin.unget();
lexeme.pop_back();
TokenClass tc(t,lexeme);
tc.CheckReserved();
return tc;
}
TokenClass ScannerClass::PeekNextToken(){
int lineNum = mLineNumber;
std::streampos position = mFin.tellg();
TokenClass tc = GetNextToken();
if(!mFin) // if we triggered EOF, then seekg doesn't work,
mFin.clear();// unless we first clear()
mFin.seekg(position);
mLineNumber = lineNum;
return tc;
}