summaryrefslogtreecommitdiff
path: root/cli/token.hxx
blob: 80fb7e7c7738fac7a5445fc19aec251dcac941f1 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// file      : cli/token.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CLI_TOKEN_HXX
#define CLI_TOKEN_HXX

#include <string>
#include <cstddef>

class Token
{
public:
  enum Type
  {
    t_eos,
    t_keyword,
    t_identifier,
    t_punctuation,
    t_bracket_lit,
    t_string_lit,
    t_bool_lit,
    t_int_lit,
    t_float_lit
  };

  Type
  type () const
  {
    return type_;
  }

  size_t
  line () const
  {
    return l_;
  }

  size_t
  column () const
  {
    return c_;
  }

  // Keyword
  //
public:
  enum Keyword
  {
    k_include,
    k_namespace,
    k_class,
    k_bool
  };

  Keyword
  keyword () const
  {
    return keyword_;
  }

  // Identifier
  //
public:
  std::string const&
  identifier () const
  {
    return str_;
  }

  // Punctuation
  //
public:
  enum Punctuation
  {
    p_semi,
    p_comma,
    p_colon,
    p_lcbrace,
    p_rcbrace,
    p_lparen,
    p_rparen,
    p_eq,
    p_or
  };

  Punctuation
  punctuation () const
  {
    return punctuation_;
  }

  // Literals.
  //
public:
  std::string const&
  literal () const
  {
    return str_;
  }

  // C-tors.
  //
public:
  // EOS.
  //
  Token (size_t l, size_t c)
      : l_ (l), c_ (c), type_ (t_eos)
  {
  }

  Token (Keyword k, size_t l, size_t c)
      : l_ (l), c_ (c), type_ (t_keyword), keyword_ (k)
  {
  }

  Token (Punctuation p, size_t l, size_t c)
      : l_ (l), c_ (c), type_ (t_punctuation), punctuation_ (p)
  {
  }

  // Identifier & literals.
  //
  Token (Type t, std::string const& s, size_t l, size_t c)
      : l_ (l), c_ (c), type_ (t), str_ (s)
  {
  }

private:
  size_t l_;
  size_t c_;

  Type type_;

  Keyword keyword_;
  Punctuation punctuation_;
  std::string str_;
};

#endif // CLI_TOKEN_HXX