-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
121 lines (104 loc) · 2.64 KB
/
lexer.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
RIGHT TO USE, DISTRIBUTE AND MODIFY
===================================
Copyright (C) 2015 Ganesh Prasad Sahoo - All Rights Reserved
You may use, distribute and modify this code under the Terms
of GNU GPL V3 License. You should have received a copy of the
GNU GPL v3 License with this file. If not please write to
sir.gnsp@gmail.com
or visit
http://www.gnu.org/licenses/gpl.txt
*****************************************************************************
*/
#include "lexer.hpp"
using namespace std;
Operator strToOp(const char *str){
switch(str[0]){
case '{': return LCURL;
case '}': return RCURL;
case ';': return SEMICOLON;
case '-': return HYPHEN;
case '=': return EQUAL;
default: return _ERROR;
}
}
KeyWord strToKey(const char *str){
for(int i=0; i<18; i++)
if(strcmp(KEYWORDS[i],str)==0) return ENT+(KeyWord)i;
return ERROR;
}
/* if(strcmp(str,"ENT") == 0) return ENT;
else if(strcmp(str, "ATTR")) return ATTR;
else if(strcmp(str, "WEAK")) return WEAK;
else if(strcmp(str, "MULTI")) return MULTI;
else if(strcmp(str, "COMPOSITE")) return COMPOSITE;
else if(strcmp(str, "KEY")) return KEY;
else if(strcmp(str, "DERIVED")) return DERIVED;
else if(strcmp(str, "RELN")) return RELN;
else if(strcmp(str, "ONE")) return ONE;
else if(strcmp(str, "MANY")) return MANY;
else return ERROR;
}*/
void Token::makeToken(TokenType type, const char *str){
type_ = type;
switch(type_){
case KEYWORD:
token_.kw_ = strToKey(str);
break;
case NAME:
strcpy(token_.name_, str);
break;
case OPERATOR:
token_.op_ = strToOp(str);
break;
default:
error("Token does not match any known type");
break;
}
return;
}
inline void readChar(char* &tmp, char* &prog, bool read=true){
if(*prog == '\n' || *prog == '\r'){
line_no++;
col_no = 0;
if(*prog == '\r') prog++;
}
else col_no++;
if(read)*tmp++ = *prog++;
else prog++;
}
bool Tokenizer::getNextToken(){
char *temp;
temp = token_;
type_ = _NONE;
*temp = '\0';
if(!*prog_) return false;
while(isspace(*prog_)) readChar(temp, prog_, false);
if(*prog_ == '#'){
while(*prog_ != '\n') readChar(temp, prog_, false);
readChar(temp, prog_, false);
}
else if(isOp(*prog_)){
readChar(temp, prog_);
type_ = OPERATOR;
}
else if(*prog_ == '\"'){
readChar(temp, prog_, false);
while(*prog_ != '\"') readChar(temp, prog_);
readChar(temp, prog_, false);
type_ = NAME;
}
else if(isalpha(*prog_)){
while(isalpha(*prog_)||*prog_=='_')
readChar(temp, prog_);
*temp = '\0';
if(isKey(token_)) type_ = KEYWORD;
else type_ = NAME;
}
else{
error("type of the token could not be resolved");
return false;
}
*temp = '\0';
return true;
}