-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.h
82 lines (68 loc) · 2.4 KB
/
token.h
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <iosfwd>
#include <string>
#include <vector>
#include <boost/optional.hpp> // TODO: to std::optional?
namespace pyjamas
{
enum class TokenType
{
Invalid,
Null,
True,
False,
String,
ArrayBegin,
ArrayEnd,
ObjectBegin,
ObjectEnd,
ItemSeparator,
KeyValueSeparator
};
struct Token
{
/// tag types
struct invalid_t {};
struct null_t {};
struct array_begin_t {};
struct array_end_t {};
struct object_begin_t {};
struct object_end_t {};
struct item_separator_t {};
struct key_value_separator_t {};
static constexpr null_t null = {};
static constexpr invalid_t invalid = {};
static constexpr array_begin_t array_begin = {};
static constexpr array_end_t array_end = {};
static constexpr object_begin_t object_begin = {};
static constexpr object_end_t object_end = {};
static constexpr item_separator_t item_separator = {};
static constexpr key_value_separator_t key_value_separator = {};
Token( invalid_t = {}, std::string message = {} ):
type{ TokenType::Invalid },
text{ std::move( message )}
{}
Token( null_t ): type{ TokenType::Null } {}
Token( array_begin_t ): type{ TokenType::ArrayBegin } {}
Token( array_end_t ): type{ TokenType::ArrayEnd } {}
Token( object_begin_t ): type{ TokenType::ObjectBegin } {}
Token( object_end_t ): type{ TokenType::ObjectEnd } {}
Token( item_separator_t ): type{ TokenType::ItemSeparator } {}
Token( key_value_separator_t ): type{ TokenType::KeyValueSeparator } {}
Token( bool b ):
type{
b ? TokenType::True : TokenType::False }
{}
Token( std::string text ):
type{ TokenType::String },
text{ std::move( text )}
{}
Token( const char* text ):
Token( std::string{ text })
{}
TokenType type;
boost::optional< std::string > text;
};
bool operator==( const Token& a, const Token& b );
std::ostream& operator<<( std::ostream& os, const Token& tok );
}