|
| 1 | +#ifndef JUCC_LEXER_H |
| 2 | +#define JUCC_LEXER_H |
| 3 | + |
| 4 | +#include <cctype> |
| 5 | +#include <cstdio> |
| 6 | +#include <fstream> |
| 7 | +#include <iostream> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +namespace jucc { |
| 11 | + |
| 12 | +enum Token { |
| 13 | + TOK_EOF = -1, |
| 14 | + TOK_IDENTIFIER = -2, |
| 15 | + TOK_DECIMAL = -3, |
| 16 | + TOK_FRACTIONAL = -4, |
| 17 | + |
| 18 | + // Conditionals |
| 19 | + TOK_IF = -6, |
| 20 | + TOK_ELSE = -7, |
| 21 | + |
| 22 | + // Data types |
| 23 | + TOK_INT = -8, |
| 24 | + TOK_FLOAT = -9, |
| 25 | + TOK_VOID = -10, |
| 26 | + // error |
| 27 | + TOK_ERROR = -100, |
| 28 | + |
| 29 | + // punctuation tokens |
| 30 | + |
| 31 | + TOK_SEMICOLON = -11, // ; |
| 32 | + TOK_CURLY_OPEN = -12, // { |
| 33 | + TOK_CURLY_CLOSE = -13, // } |
| 34 | + TOK_PAREN_OPEN = -14, // ( |
| 35 | + TOK_PAREN_CLOSE = -15, // ) |
| 36 | + TOK_DOT = -16, // . |
| 37 | + TOK_COMMA = -17, // , |
| 38 | + TOK_LEFT_SHIFT = -18, // << |
| 39 | + TOK_RIGHT_SHIFT = -19, // >> |
| 40 | + TOK_LESS_THAN = -20, // < |
| 41 | + TOK_GREATER_THAN = -21, // > |
| 42 | + TOK_EQUAL_TO = -22, // == |
| 43 | + TOK_ASSIGNMENT = -23, // = |
| 44 | + TOK_COMMENT = -24, // // |
| 45 | + TOK_LITERAL = -25, // "c++" |
| 46 | + TOK_CHARACTER = -26, // 'c' |
| 47 | + |
| 48 | + // cout, cin |
| 49 | + |
| 50 | + TOK_COUT = -27, // cout |
| 51 | + TOK_CIN = -28, // cin |
| 52 | +}; |
| 53 | + |
| 54 | +class Lexer { |
| 55 | + public: |
| 56 | + /** |
| 57 | + * used to store a identifier token |
| 58 | + */ |
| 59 | + std::string identifier_string_; |
| 60 | + /** |
| 61 | + * used to store a corresponding error token |
| 62 | + * suppose a numerical token 16.3ere which is |
| 63 | + * neither a numerical token or a identifier |
| 64 | + */ |
| 65 | + std::string error_string_; |
| 66 | + /** |
| 67 | + * used to store a literal string |
| 68 | + * literal strings are of type "a string" |
| 69 | + */ |
| 70 | + std::string literal_string_; |
| 71 | + /** |
| 72 | + * used to store the value of the integer token |
| 73 | + * during tokenization. |
| 74 | + */ |
| 75 | + int intval_; |
| 76 | + /** |
| 77 | + * used to store the value of the float token |
| 78 | + * during tokenization. |
| 79 | + */ |
| 80 | + double floatval_; |
| 81 | + |
| 82 | + /** |
| 83 | + * Takes a ifstream object as input and gets the next character |
| 84 | + * from the input file and returns the appropriate token. |
| 85 | + */ |
| 86 | + int GetToken(std::ifstream &is); |
| 87 | +}; // class Lexer |
| 88 | + |
| 89 | +} // namespace jucc |
| 90 | + |
| 91 | +#endif |
0 commit comments