CBMC
ctoken.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C Token
4 
5 Author: Daniel Kroening, dkr@amazon.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_CRANGLER_CTOKEN_H
13 #define CPROVER_CRANGLER_CTOKEN_H
14 
15 #include <iosfwd>
16 #include <string>
17 
18 class ctokent
19 {
20 public:
21  using kindt = enum {
22  END_OF_FILE,
23  INT_LIT,
24  CHAR_LIT,
25  FLOAT_LIT,
26  STRING_LIT,
27  C_COMMENT,
28  CPP_COMMENT,
29  IDENTIFIER,
30  OPERATOR,
31  WS,
32  SEPARATOR,
33  PREPROCESSOR_DIRECTIVE,
34  UNKNOWN
35  };
36 
38 
39  // could be string_view, after C++17
40  std::string text;
41 
42  std::size_t line_number = 0;
43 
44  ctokent() = default;
45 
46  ctokent(kindt _kind, std::string _text) : kind(_kind), text(std::move(_text))
47  {
48  }
49 
50  void output(std::ostream &) const;
51 
52  bool operator==(const char *other_text) const
53  {
54  return text == other_text;
55  }
56 
57  bool operator==(char some_char) const
58  {
59  return text == std::string(1, some_char);
60  }
61 
62  bool operator!=(char some_char) const
63  {
64  return text != std::string(1, some_char);
65  }
66 };
67 
68 static inline bool is_identifier(const ctokent &t)
69 {
70  return t.kind == ctokent::IDENTIFIER;
71 }
72 
73 static inline bool is_separator(const ctokent &t)
74 {
75  return t.kind == ctokent::SEPARATOR;
76 }
77 
78 static inline bool is_operator(const ctokent &t)
79 {
80  return t.kind == ctokent::OPERATOR;
81 }
82 
83 static inline bool is_ws(const ctokent &t)
84 {
85  return t.kind == ctokent::WS;
86 }
87 
88 static inline bool is_eof(const ctokent &t)
89 {
90  return t.kind == ctokent::END_OF_FILE;
91 }
92 
93 static inline bool is_comment(const ctokent &t)
94 {
95  return t.kind == ctokent::C_COMMENT || t.kind == ctokent::CPP_COMMENT;
96 }
97 
98 static inline bool is_preprocessor_directive(const ctokent &t)
99 {
100  return t.kind == ctokent::PREPROCESSOR_DIRECTIVE;
101 }
102 
103 std::ostream &operator<<(std::ostream &, const ctokent &);
104 
105 #endif // CPROVER_CRANGLER_CTOKEN_H
Definition: ctoken.h:19
std::size_t line_number
Definition: ctoken.h:42
bool operator==(char some_char) const
Definition: ctoken.h:57
void output(std::ostream &) const
Definition: ctoken.cpp:16
bool operator!=(char some_char) const
Definition: ctoken.h:62
ctokent()=default
bool operator==(const char *other_text) const
Definition: ctoken.h:52
ctokent(kindt _kind, std::string _text)
Definition: ctoken.h:46
kindt kind
Definition: ctoken.h:37
std::string text
Definition: ctoken.h:40
enum { END_OF_FILE, INT_LIT, CHAR_LIT, FLOAT_LIT, STRING_LIT, C_COMMENT, CPP_COMMENT, IDENTIFIER, OPERATOR, WS, SEPARATOR, PREPROCESSOR_DIRECTIVE, UNKNOWN } kindt
Definition: ctoken.h:35
static bool is_identifier(const ctokent &t)
Definition: ctoken.h:68
static bool is_operator(const ctokent &t)
Definition: ctoken.h:78
std::ostream & operator<<(std::ostream &, const ctokent &)
Definition: ctoken.cpp:64
static bool is_separator(const ctokent &t)
Definition: ctoken.h:73
static bool is_comment(const ctokent &t)
Definition: ctoken.h:93
static bool is_preprocessor_directive(const ctokent &t)
Definition: ctoken.h:98
static bool is_ws(const ctokent &t)
Definition: ctoken.h:83
static bool is_eof(const ctokent &t)
Definition: ctoken.h:88