-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.cpp
More file actions
76 lines (64 loc) · 1.5 KB
/
Scanner.cpp
File metadata and controls
76 lines (64 loc) · 1.5 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
#include "Scanner.h"
#include "Interpreter.h"
#include "Token.h"
Scanner::Scanner(Interpreter *interpreter):_interpreter(interpreter)
{
}
Scanner::~Scanner(void)
{
}
void Scanner::TransformCharsToTokens(const char *source)
{
if(*source != '\0')
{
Context context(source,GetEndOfString(source));
Interpret(context);
}
}
void Scanner::Interpret(Context context)
{
Keyword *keyword = ExtractKeyword(context);
if(keyword!=NULL)
{
keyword->Interpret(context);
keyword->UpdateContext(context);
if(*context.GetBegin()!='\0')
Interpret(context);
}
}
Keyword *Scanner::ExtractKeyword(Context context)
{
AVLTree<Keyword*>* reservedWords = _interpreter->GetReservedWords();
IsValid(context.GetBegin());
std::string *keyword_str = GetKeywordAndUpdateContext(context);
Keyword *keyword = reservedWords->Search<std::string *>(keyword_str)->GetNodeContents();
delete keyword_str;
return keyword;
}
std::string* Scanner::GetKeywordAndUpdateContext(Context context)
{
char letter = *(context.GetBegin());
unsigned i=0;
while(IS_A_LETTER(letter))
{
i++;
letter = *(context.GetBegin()+i);
}
std::string *keyword_str = new std::string(context.GetBegin(),context.GetBegin()+i);
context.SetBegin(context.GetBegin()+i);
return keyword_str;
}
void Scanner::IsValid(const char *source)
{
if(source==NULL)
throw std::bad_typeid("null source");
}
const char* Scanner::GetEndOfString(const char *source)
{
const char *end_ptr = source;
while(*end_ptr != '\0')
{
end_ptr++;
}
return end_ptr;
}