CBMC
smt2_tokenizer.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
10 #define CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
11 
12 #include <sstream>
13 #include <string>
14 
16 {
17 public:
18  explicit smt2_tokenizert(std::istream &_in) : peeked(false), token(NONE)
19  {
20  in=&_in;
21  line_no=1;
22  }
23 
25  {
26  public:
27  smt2_errort(smt2_errort &&) = default;
28 
29  smt2_errort(const smt2_errort &other)
30  {
31  // ostringstream does not have a copy constructor
32  message << other.message.str();
33  line_no = other.line_no;
34  }
35 
36  smt2_errort(const std::string &_message, unsigned _line_no)
37  : line_no(_line_no)
38  {
39  message << _message;
40  }
41 
42  explicit smt2_errort(unsigned _line_no) : line_no(_line_no)
43  {
44  }
45 
46  std::string what() const
47  {
48  return message.str();
49  }
50 
51  unsigned get_line_no() const
52  {
53  return line_no;
54  }
55 
56  std::ostringstream &message_ostream()
57  {
58  return message;
59  }
60 
61  protected:
62  std::ostringstream message;
63  unsigned line_no;
64  };
65 
66  using tokent = enum {
67  NONE,
68  END_OF_FILE,
69  STRING_LITERAL,
70  NUMERAL,
71  SYMBOL,
72  KEYWORD,
73  OPEN,
74  CLOSE
75  };
76 
78 
80  {
81  if(peeked)
82  return token;
83  else
84  {
86  peeked=true;
87  return token;
88  }
89  }
90 
91  const std::string &get_buffer() const
92  {
93  return buffer;
94  }
95 
97  {
98  return quoted_symbol;
99  }
100 
102  smt2_errort error(const std::string &message) const
103  {
104  return smt2_errort(message, line_no);
105  }
106 
109  {
110  return smt2_errort(line_no);
111  }
112 
113 protected:
114  std::istream *in;
115  unsigned line_no;
116  std::string buffer;
117  bool quoted_symbol = false;
118  bool peeked;
120 
124 
125 private:
132 
134  void get_token_from_stream();
135 };
136 
138 template <typename T>
140 operator<<(smt2_tokenizert::smt2_errort &&e, const T &message)
141 {
142  e.message_ostream() << message;
143  return std::move(e);
144 }
145 
147 
148 #endif // CPROVER_SOLVERS_SMT2_SMT2_PARSER_H
smt2_errort(const smt2_errort &other)
smt2_errort(smt2_errort &&)=default
smt2_errort(const std::string &_message, unsigned _line_no)
std::ostringstream & message_ostream()
std::ostringstream message
unsigned get_line_no() const
smt2_errort(unsigned _line_no)
std::string what() const
void get_token_from_stream()
read a token from the input stream and store it in 'token'
tokent get_string_literal()
tokent get_decimal_numeral()
enum { NONE, END_OF_FILE, STRING_LITERAL, NUMERAL, SYMBOL, KEYWORD, OPEN, CLOSE } tokent
const std::string & get_buffer() const
smt2_tokenizert(std::istream &_in)
std::istream * in
smt2_errort error() const
generate an error exception
tokent get_bin_numeral()
tokent get_simple_symbol()
tokent get_quoted_symbol()
smt2_errort error(const std::string &message) const
generate an error exception, pre-filled with a message
bool token_is_quoted_symbol() const
std::string buffer
void skip_to_end_of_list()
skip any tokens until all parentheses are closed or the end of file is reached
tokent get_hex_numeral()
smt2_tokenizert::smt2_errort operator<<(smt2_tokenizert::smt2_errort &&e, const T &message)
add to the diagnostic information in the given smt2_tokenizer exception
bool is_smt2_simple_symbol_character(char)