11#ifndef TOKEN_HPP
22#define TOKEN_HPP
33
4+ #include < iostream>
45#include < regex>
56#include < string>
67#include < vector>
@@ -9,6 +10,7 @@ using namespace std;
910
1011enum TokenType {
1112 NONE,
13+ KEYWORD,
1214 IDENTIFICATOR,
1315 OPERATOR,
1416 SEPARATOR,
@@ -26,9 +28,10 @@ const string LITERAL_INCOMPLETE_PATTERN = R"(^\"([^\"\\\n]|\\.)*$)";
2628const string COMMENT_LINE_PATTERN = R"( ^//.*)" ;
2729const string COMMENT_BLOCK_PATTERN = R"( /\*[\s\S]*?\*/)" ;
2830const string COMMENT_BLOCK_INCOMPLETE_PATTERN = R"( /\*[\s\S]*)" ;
29- const vector<string> OPERATORS = {" *" , " /" , " %" , " +" , " -" , " ==" , " !=" , " &&" , " ||" , " =" , " +=" , " -=" , " *=" , " /=" , " %=" , " &" , " |" , " ." , " >" , " <" , " >=" , " <=" , " !" };
31+ const vector<string> OPERATORS = {" *" , " /" , " %" , " +" , " -" , " ==" , " !=" , " &&" , " ||" , " =" , " +=" , " -=" , " *=" , " /=" , " %=" , " &" , " |" , " ." , " >" , " <" , " >=" , " <=" , " !" , " ++ " , " -- " };
3032const vector<string> SEPARATORS_IGNORED = {" " , " \n " , " \t " };
3133const vector<string> SEPARATORS_IMPORTANT = {" (" , " )" , " {" , " }" , " [" , " ]" , " ;" , " ," , " ." };
34+ const vector<string> KEYWORDS = {" break" , " case" , " char" , " const" , " continue" , " default" , " do" , " double" , " else" , " enum" , " float" , " for" , " goto" , " if" , " int" , " long" , " return" , " short" , " sizeof" , " static" , " struct" , " switch" , " typedef" , " void" , " while" };
3235
3336class Token {
3437 public:
@@ -39,13 +42,40 @@ class Token {
3942
4043 Token () {};
4144 Token (string content, TokenType type, pair<int , int > position) : content(move(content)), type(type), position(position) {};
45+
46+ void print () {
47+ cout << " (" << position.first << " ," << position.second << " ) " << enum_type_string (type) << endl;
48+ cout << content << " \n\n " ;
49+ }
50+
51+ static string enum_type_string (TokenType type) {
52+ switch (type) {
53+ case NONE:
54+ return " NONE" ;
55+ case IDENTIFICATOR:
56+ return " IDENTIFICATOR" ;
57+ case NUMBER:
58+ return " NUMBER" ;
59+ case SEPARATOR:
60+ return " SEPARATOR" ;
61+ case OPERATOR:
62+ return " OPERATOR" ;
63+ case LITERAL:
64+ return " LITERAL" ;
65+ case COMMENT:
66+ return " COMMENT" ;
67+ case KEYWORD:
68+ return " KEYWORD" ;
69+ default :
70+ return " UNKNOWN" ;
71+ }
72+ }
4273};
4374
4475void lexical_error (Token token);
4576vector<Token>
4677tokenize (string& buffer);
4778Token get_token (string::iterator& sentinel, string::iterator end, pair<int , int >& position);
4879regex create_regex (vector<string> tokens);
49- string enum_type_string (TokenType type);
5080
5181#endif
0 commit comments