49 lines
1.0 KiB
C++
49 lines
1.0 KiB
C++
#ifndef TOKENIZE_H
|
|
#define TOKENIZE_H
|
|
|
|
#include <vector>
|
|
#include <variant>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
using namespace std;
|
|
|
|
enum class TokenType { Type, Identifier, Int, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else };
|
|
enum class Type { Int };
|
|
|
|
using TokenData = variant<int, string, Type>;
|
|
|
|
struct CodePosition {
|
|
int line;
|
|
int column;
|
|
};
|
|
|
|
struct Token {
|
|
TokenType type;
|
|
TokenData data { };
|
|
CodePosition pos;
|
|
};
|
|
|
|
class TokenError : public runtime_error {
|
|
public:
|
|
explicit TokenError(const string& message, CodePosition pos)
|
|
: runtime_error(message), pos(pos) {}
|
|
|
|
const CodePosition pos;
|
|
};
|
|
|
|
/*
|
|
Parses a string into a vector of tokens
|
|
*/
|
|
vector<Token> tokenize(vector<string> str, int initial_line=0);
|
|
|
|
/*
|
|
Format and print a Token
|
|
*/
|
|
void _debug_print_token(Token token);
|
|
|
|
/*
|
|
Formats a list of tokens and prints it
|
|
*/
|
|
void _debug_print_tokens(vector<Token> tokens);
|
|
|
|
#endif |