1
2
3
4
5
6 #ifndef SOULNG_LEXER_LEXEME_INCLUDED
7 #define SOULNG_LEXER_LEXEME_INCLUDED
8 #include <soulng/lexer/LexerApi.hpp>
9 #include <string>
10
11 namespace soulng { namespace lexer {
12
13 struct Lexeme
14 {
15 Lexeme() : begin(nullptr), end(nullptr) { }
16 Lexeme(const char32_t* begin_, const char32_t* end_) : begin(begin_), end(end_) { }
17 std::u32string ToString() const { return std::u32string(begin, end); }
18 const char32_t* begin;
19 const char32_t* end;
20 };
21
22 inline bool operator==(const Lexeme& left, const Lexeme& right)
23 {
24 if (left.end - left.begin != right.end - right.begin) return false;
25 const char32_t* p = left.begin;
26 const char32_t* q = right.begin;
27 while (p != left.end)
28 {
29 if (*p != *q) return false;
30 ++p;
31 ++q;
32 }
33 return true;
34 }
35
36 inline bool operator<(const Lexeme& left, const Lexeme& right)
37 {
38 const char32_t* p = left.begin;
39 const char32_t* q = right.begin;
40 while (p != left.end && q != right.end)
41 {
42 if (*p < *q) return true;
43 if (*p > *q) return false;
44 ++p;
45 ++q;
46 }
47 if (p == left.end) return q != right.end;
48 return false;
49 }
50
51 } }
52
53 #endif // SOULNG_LEXER_LEXEME_INCLUDED