summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/Lexer.cpp
blob: 681e9274650107a811682a5422eae10b6d79d7e5 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "Lexer.h"

Lexer::Lexer()
{
    string keys[] = {"function", "class", "for", "while", "if", "else", "true", "false", "and", "or"};
    keywords.assign(keys, keys+10);

    char punct[] = {'.', ',', ';', '{', '}', '[', ']', '(', ')'};
    punctuation.assign(punct, punct+9);

    string oper[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!="};
    operators.assign(oper, oper +17);
}

Lexer::~Lexer()
{
    //dtor
}

std::string Lexer::unescape_string(string text)
{
	std::string result = text;
	unsigned int offset = 0;
	for(unsigned int i = 0; i < text.size(); i++)
	{
		char replacement = 0;
		if(text[i] == '\\')
		{
			switch (text[i+1])
			{
			case 'n':
				replacement = '\n';
				break;
			case 'r':
				replacement = '\r';
				break;
			case 't':
				replacement = '\t';
				break;
			case '[':
				replacement = 27;
				break;
			}

		}
		if(replacement != 0)
		{
			char replacement_chars[2];
			replacement_chars[0] = replacement;
			replacement_chars[1] = 0;

			result.replace(i + offset, 2, replacement_chars);
			offset--;
		}


	}
	return result;
}

void Lexer::add_keyword(string word)
{
    if(!is_keyword(word))
    {
        keywords.push_back(word);
    }
}
void Lexer::add_punctuation_char(char c)
{
    if(!is_punctuation(c))
    {
        punctuation.push_back(c);
    }
}

void Lexer::add_operator(string oper)
{
    if(!is_operator(oper))
    {
        operators.push_back(oper);
    }
}


vector<Token> Lexer::parse_line(string line)
{
    vector<Token> tokens;
    while(line != "")
    {
        pair<string, Token> result_of_parsing = parse_token(line);
        line = result_of_parsing.first;
        Token token = result_of_parsing.second;
        if(token.get_value() != "")
        {
            tokens.push_back(token);
        }
    }
    return tokens;

}

pair<string, Token> Lexer::parse_token(string line)
{
    string token_value = "";
    unsigned int i;


    for(i=0; i< line.size(); i++)
    {
        if(token_value == "" && isspace(line[i])) continue;

        if(isdigit(line[i]))
        {
        	token_value += line[i++];
        	for(; i < line.size(); i++)
        	{
        		if(isdigit(line[i]) || line[i] == '.')
        		{
        			token_value += line[i];
        		}
        		else
        		{
        			break;
        		}
        	}
        }

        if(line[i] == '\"')
        {
        	token_value += line[i++];
        	for(; i < line.size() ; i++)
        	{
        		token_value += line[i];
        		if (line[i] == '\"')
        		{
        			i++;
        			break;
        		}
        	}
        }

        if(isalnum(line[i])  || line[i]== '_')
        {
            token_value += line[i];

        }

        else if(ispunct(line[i]))
        {
            if(token_value=="")
            {
                token_value=line[i];
                i++;
                if(i<line.size())
                {
                    if(line[i] == '=')
                    {
                        token_value+=line[i];
                        i++;
                    }
                }
            }
            break;
        }


        else break;
    }
    auto type = guess_type(token_value);
    if(type == t_literal) token_value = unescape_string(token_value);

    Token token = Token(type, token_value);
    string truncated_line = line.substr(i);

    return pair<string, Token>(truncated_line, token);
}

bool Lexer::is_keyword(string value)
{
    for(unsigned int i=0; i< keywords.size(); i++)
    {
        if(value == keywords[i]) return true;
    }
    return false;
}

bool Lexer::is_punctuation(char c)
{

    for(unsigned int i=0; i< punctuation.size(); i++)
    {
        if(c == punctuation[i]) return true;
    }
    return false;
}

bool Lexer::is_operator(string value)
{
    for(unsigned int i=0; i< operators.size(); i++)
    {
        if(value == operators[i]) return true;
    }
    return false;
}

type_of_token Lexer::guess_type(string value)
{
    /* I can have one of these types:
    typedef enum {  t_invalid_token=0, t_symbol, t_integer, t_literal,
                t_punctuation, t_keyword } type_of_token;

    */

    if(value == "") return t_invalid_token;
    if(isdigit(value[0]))
       {
           bool is_number = true;
           bool dot_used = false;
           for(unsigned int i=0; i<value.size(); i++)
           {
        	 if(value[i] == '.')
        	 {
        		 if(dot_used) return t_invalid_token;
        		 dot_used = true;
        		 continue;
        	 }
             if(!isdigit(value[i]))  is_number = false;
           }

           if(is_number)
           {
        	   if(dot_used)
        	   {
        		   return t_float;
        	   }
        	   else return t_integer;
           }

           else return t_invalid_token;
       }
    if(isalpha(value[0]))
    {
        if(is_keyword(value)) return t_keyword;
        else return t_symbol;

    }

    if(value[0]=='\"')
    {
        if(value[value.size()-1] == '\"') return t_literal;
        else return t_invalid_token;
    }

    if(value.size() == 1 )
    {
        if(is_punctuation(value[0])) return t_punctuation;
        else
        {
            if(is_operator(value)) return t_operator;
        }
    }
    if(value.size() == 2 && is_operator(value)) return t_operator;

    //If any...
    return t_invalid_token;
}