forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.h
189 lines (150 loc) · 3.7 KB
/
parser.h
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************\
Module: Parser utilities
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Parser utilities
#ifndef CPROVER_UTIL_PARSER_H
#define CPROVER_UTIL_PARSER_H
#include "deprecate.h"
#include "expr.h"
#include "message.h"
#include <filesystem>
#include <iosfwd>
#include <string>
#include <vector>
class parsert
{
public:
std::istream *in;
std::string this_line, last_line;
std::vector<exprt> stack;
virtual void clear()
{
line_no=0;
previous_line_no=0;
column=1;
stack.clear();
source_location.clear();
last_line.clear();
}
DEPRECATED(SINCE(2023, 12, 20, "use parsert(message_handler) instead"))
parsert():in(nullptr) { clear(); }
explicit parsert(message_handlert &message_handler)
: in(nullptr), log(message_handler)
{
clear();
}
virtual ~parsert() { }
// The following are for the benefit of the scanner
bool read(char &ch)
{
if(!in->read(&ch, 1))
return false;
if(ch=='\n')
{
last_line.swap(this_line);
this_line.clear();
}
else
this_line+=ch;
return true;
}
virtual bool parse()=0;
bool eof()
{
return in->eof();
}
void parse_error(
const std::string &message,
const std::string &before);
void inc_line_no()
{
++line_no;
column=1;
}
void set_line_no(unsigned _line_no)
{
line_no=_line_no;
}
void set_file(const irep_idt &file)
{
source_location.set_file(file);
source_location.set_working_directory(
std::filesystem::current_path().string());
}
irep_idt get_file() const
{
return source_location.get_file();
}
unsigned get_line_no() const
{
return line_no;
}
unsigned get_column() const
{
return column;
}
void set_column(unsigned _column)
{
column=_column;
}
void set_source_location(exprt &e)
{
// Only set line number when needed, as this destroys sharing.
if(previous_line_no!=line_no)
{
previous_line_no=line_no;
source_location.set_line(line_no);
}
e.add_source_location()=source_location;
}
void set_function(const irep_idt &function)
{
source_location.set_function(function);
}
void advance_column(unsigned token_width)
{
column+=token_width;
}
// should be protected or even just be a reference to a message handler, but
// for now enables a step-by-step transition
messaget log;
protected:
source_locationt source_location;
unsigned line_no, previous_line_no;
unsigned column;
};
exprt &_newstack(parsert &parser, unsigned &x);
#define newstack(x) _newstack(PARSER, (x))
#define parser_stack(x) (PARSER.stack[x])
#define stack_expr(x) (PARSER.stack[x])
#define stack_type(x) \
(static_cast<typet &>(static_cast<irept &>(PARSER.stack[x])))
#define YY_INPUT(buf, result, max_size) \
do { \
for(result=0; result<max_size;) \
{ \
char ch; \
if(!PARSER.read(ch)) /* NOLINT(readability/braces) */ \
{ \
if(result==0) \
result=YY_NULL; \
break; \
} \
\
if(ch!='\r') /* NOLINT(readability/braces) */ \
{ \
buf[result++]=ch; \
if(ch=='\n') /* NOLINT(readability/braces) */ \
{ \
PARSER.inc_line_no(); \
break; \
} \
} \
} \
} while(0)
// The following tracks the column of the token, and is nicely explained here:
// http://oreilly.com/linux/excerpts/9780596155971/error-reporting-recovery.html
#define YY_USER_ACTION PARSER.advance_column(yyleng);
#endif // CPROVER_UTIL_PARSER_H