A simple scripting language in C++
Ferenc Szontágh
2025-04-19 ea9af87ba4399d094180f06be59c878d864a17e0
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
#ifndef TOKEN_HPP
#define TOKEN_HPP
 
#include <iostream>
#include <string>
#include <string_view>
 
#include "Lexer/TokenType.hpp"
 
namespace Lexer::Tokens {
 
struct Token {
    Lexer::Tokens::Type type;
    std::string         value;      // A token feldolgozott értéke
    std::string_view    lexeme;     // A nyers szövegrész az eredeti stringből
    size_t              start_pos;  // Kezdő index
    size_t              end_pos;    // Vég index (exclusive)
    int                 line_number;
    int                 column_number;
 
    // Módosított print metódus a lexeme kiírásával
    void print() const {
        std::cout << "Token { Type: " << Lexer::Tokens::TypeToString(type) << ", Value: \"" << value << "\""
                  << ", Pos: [" << start_pos << ", " << end_pos
                  << ")"
                  // Lexeme kiírása (stringgé konvertálva a biztonság kedvéért, ha pl. null lenne)
                  << ", Lexeme: \"" << std::string(lexeme) << "\""
                  << " }" << '\n';
    }
 
    std::string dump() const {
        return + "Token { Type: " + Lexer::Tokens::TypeToString(type) + ", Value: \"" + value + "\""
               + ", Pos: [" + std::to_string(start_pos) + ", " + std::to_string(end_pos)
               + ")"
               + ", Lexeme: \"" + std::string(lexeme) + "\""
               + " }" + '\n';
    }
};
 
inline bool operator==(const Token & lhs, const Token & rhs) {
    return lhs.type == rhs.type && lhs.value == rhs.value && lhs.start_pos == rhs.start_pos &&
           lhs.end_pos == rhs.end_pos;
}
};  // namespace Lexer::Tokens
#endif  // TOKEN_HPP