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
19
20#include <optional>
21#include <sstream>
22#include <string>
23
25{
26public:
27 explicit smt2_tokenizert(std::istream &_in) : in(&_in)
28 {
29 }
30
34 {
35 public:
36 smt2_errort(const std::string &_message, unsigned _line_no)
37 : line_no(_line_no)
38 {
39 _reason << _message;
40 }
41
42 explicit smt2_errort(unsigned _line_no) : line_no(_line_no)
43 {
44 }
45
46 unsigned get_line_no() const
47 {
48 return line_no;
49 }
50
51 protected:
52 unsigned line_no;
53 };
54
55 using token_kindt = enum {
56 NONE,
57 END_OF_FILE,
58 STRING_LITERAL,
59 NUMERAL,
60 SYMBOL,
61 KEYWORD,
62 OPEN,
63 CLOSE
64 };
65
70 class tokent
71 {
72 public:
73 tokent() = default;
74 explicit tokent(token_kindt _kind) : kind(_kind)
75 {
76 }
77
84 std::string text;
86 unsigned line_no = 0;
89 bool quoted_symbol = false;
90
93 operator token_kindt() const
94 {
95 return kind;
96 }
97 };
98
102 tokent next_token();
103
108 const tokent &peek()
109 {
110 if(!peeked.has_value())
111 peeked = read_token();
112 return *peeked;
113 }
114
116 smt2_errort error(const std::string &message) const
117 {
118 return smt2_errort(message, current_line_no());
119 }
120
123 {
125 }
126
127protected:
128 std::istream *in;
129
133
134private:
138 unsigned line_no = 1;
139
141 std::optional<tokent> peeked;
142
145 unsigned current_line_no() const
146 {
147 return peeked.has_value() ? peeked->line_no : line_no;
148 }
149
153 tokent get_decimal_numeral();
156 tokent get_hex_numeral();
159 tokent get_bin_numeral();
164 tokent get_simple_symbol();
169 tokent get_quoted_symbol();
175 tokent get_string_literal();
176
179 tokent read_token();
180};
181
183
184#endif // CPROVER_SOLVERS_SMT2_SMT2_PARSER_H
std::ostringstream _reason
The reason this exception was generated.
cprover_exception_baset(std::string reason)
This constructor is marked protected to ensure this class isn't used directly.
Definition c_errors.h:76
Exception thrown by the tokenizer (and the parser built on top of it) to report a syntactic error at ...
smt2_errort(const std::string &_message, unsigned _line_no)
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.
bool is_smt2_simple_symbol_character(char)