CBMC
Loading...
Searching...
No Matches
smt2_tokenizer.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
14
15#ifndef CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
16#define CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
17
18#include <optional>
19#include <sstream>
20#include <string>
21
23{
24public:
25 explicit smt2_tokenizert(std::istream &_in) : in(&_in)
26 {
27 }
28
34 {
35 public:
36 smt2_errort(smt2_errort &&) = default;
37
39 {
40 // ostringstream does not have a copy constructor
41 message << other.message.str();
42 line_no = other.line_no;
43 }
44
45 smt2_errort(const std::string &_message, unsigned _line_no)
47 {
49 }
50
51 explicit smt2_errort(unsigned _line_no) : line_no(_line_no)
52 {
53 }
54
55 std::string what() const
56 {
57 return message.str();
58 }
59
60 unsigned get_line_no() const
61 {
62 return line_no;
63 }
64
65 std::ostringstream &message_ostream()
66 {
67 return message;
68 }
69
70 protected:
71 std::ostringstream message;
72 unsigned line_no;
73 };
74
75 using token_kindt = enum {
76 NONE,
79 NUMERAL,
80 SYMBOL,
81 KEYWORD,
82 OPEN,
83 CLOSE
84 };
85
90 class tokent
91 {
92 public:
93 tokent() = default;
95 {
96 }
97
104 std::string text;
106 unsigned line_no = 0;
109 bool quoted_symbol = false;
110
113 operator token_kindt() const
114 {
115 return kind;
116 }
117 };
118
122 tokent next_token();
123
128 const tokent &peek()
129 {
130 if(!peeked.has_value())
131 peeked = read_token();
132 return *peeked;
133 }
134
136 smt2_errort error(const std::string &message) const
137 {
138 return smt2_errort(message, current_line_no());
139 }
140
143 {
145 }
146
147protected:
148 std::istream *in;
149
153
154private:
158 unsigned line_no = 1;
159
161 std::optional<tokent> peeked;
162
165 unsigned current_line_no() const
166 {
167 return peeked.has_value() ? peeked->line_no : line_no;
168 }
169
173 tokent get_decimal_numeral();
176 tokent get_hex_numeral();
179 tokent get_bin_numeral();
184 tokent get_simple_symbol();
189 tokent get_quoted_symbol();
195 tokent get_string_literal();
196
199 tokent read_token();
200};
201
203template <typename T>
205operator<<(smt2_tokenizert::smt2_errort &&e, const T &message)
206{
207 e.message_ostream() << message;
208 return std::move(e);
209}
210
212
213#endif // CPROVER_SOLVERS_SMT2_SMT2_PARSER_H
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
Exception thrown by the tokenizer (and the parser built on top of it) to report a syntactic error at ...
smt2_errort(const smt2_errort &other)
smt2_errort(smt2_errort &&)=default
smt2_errort(const std::string &_message, unsigned _line_no)
std::ostringstream & message_ostream()
smt2_errort(unsigned _line_no)
One SMT-LIB v2.6 token.
token_kindt kind
The kind of token; see token_kindt.
std::string text
The source text of the token (excluding any delimiters): the symbol name for SYMBOL/KEYWORD,...
unsigned line_no
The source line number on which the token ends.
tokent(token_kindt _kind)
bool quoted_symbol
True iff kind == SYMBOL and the symbol was given in |...| quoted form; always false for other token k...
tokent get_string_literal()
Read a STRING_LITERAL of the form "..." from the stream, applying SMT-LIB v2.6 quote-doubling: a "" i...
std::optional< tokent > peeked
Token that has been peeked but not yet consumed.
tokent get_decimal_numeral()
Read a NUMERAL of the form "[0-9.]+" from the stream.
tokent next_token()
Consume and return the next token.
unsigned current_line_no() const
Line number to attribute a diagnostic to: the line of the peeked token if there is one,...
enum { NONE, END_OF_FILE, STRING_LITERAL, NUMERAL, SYMBOL, KEYWORD, OPEN, CLOSE } token_kindt
smt2_tokenizert(std::istream &_in)
std::istream * in
smt2_errort error() const
generate an error exception
tokent get_bin_numeral()
Read a NUMERAL of the form "#b[01]+" from the stream.
tokent get_simple_symbol()
Read a SYMBOL of the form "[A-Za-z~!@$%^&*_\-+=<>.?/][...]*" from the stream.
tokent get_quoted_symbol()
Read a quoted SYMBOL of the form |...| from the stream.
const tokent & peek()
Return the next token without consuming it.
smt2_errort error(const std::string &message) const
generate an error exception, pre-filled with a message
unsigned line_no
Number of the source line currently being read.
tokent read_token()
Read a single token directly from the input stream.
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()
Read a NUMERAL of the form "#x[0-9a-fA-F]+" from the stream.
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)