cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
parser.h
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Parser utilities
4
5
Author: Daniel Kroening, kroening@kroening.com
6
7
\*******************************************************************/
8
11
12
#ifndef CPROVER_UTIL_PARSER_H
13
#define CPROVER_UTIL_PARSER_H
14
15
#include "
expr.h
"
16
#include "
message.h
"
17
18
#include <filesystem>
19
#include <iosfwd>
20
#include <limits>
21
#include <string>
22
#include <vector>
23
24
class
parsert
25
{
26
public
:
27
std::istream *
in
;
28
29
std::string
this_line
,
last_line
;
30
31
std::vector<exprt>
stack
;
32
33
explicit
parsert
(
message_handlert
&message_handler)
34
:
in
(nullptr),
35
log
(message_handler),
36
line_no
(0),
37
previous_line_no
(
std
::numeric_limits<unsigned int>::max()),
38
column
(1)
39
{
40
}
41
42
virtual
~parsert
() { }
43
44
// The following are for the benefit of the scanner
45
46
bool
read
(
char
&ch)
47
{
48
if
(!
in
->read(&ch, 1))
49
return
false
;
50
51
if
(ch==
'\n'
)
52
{
53
last_line
.swap(
this_line
);
54
this_line
.clear();
55
}
56
else
57
this_line
+=ch;
58
59
return
true
;
60
}
61
62
virtual
bool
parse
()=0;
63
64
bool
eof
()
65
{
66
return
in
->eof();
67
}
68
69
void
parse_error
(
70
const
std::string &message,
71
const
std::string &before);
72
73
void
inc_line_no
()
74
{
75
++
line_no
;
76
column
=1;
77
}
78
79
void
set_line_no
(
unsigned
_line_no)
80
{
81
line_no
=_line_no;
82
}
83
84
void
set_file
(
const
irep_idt
&
file
)
85
{
86
_source_location
.set_file(
file
);
87
_source_location
.set_working_directory(
88
std::filesystem::current_path().
string
());
89
}
90
91
irep_idt
get_file
()
const
92
{
93
return
_source_location
.get_file();
94
}
95
96
unsigned
get_line_no
()
const
97
{
98
return
line_no
;
99
}
100
101
unsigned
get_column
()
const
102
{
103
return
column
;
104
}
105
106
void
set_column
(
unsigned
_column)
107
{
108
column
=_column;
109
}
110
111
const
source_locationt
&
source_location
()
112
{
113
// Only set line number when needed, as this destroys sharing.
114
if
(
previous_line_no
!=
line_no
)
115
{
116
previous_line_no
=
line_no
;
117
118
// for the case of a file with no newlines
119
if
(
line_no
== 0)
120
_source_location
.set_line(1);
121
else
122
_source_location
.set_line(
line_no
);
123
}
124
125
return
_source_location
;
126
}
127
128
void
set_source_location
(
exprt
&e)
129
{
130
e.
add_source_location
() =
source_location
();
131
}
132
133
void
set_function
(
const
irep_idt
&function)
134
{
135
_source_location
.set_function(function);
136
}
137
138
void
clear_function
()
139
{
140
_source_location
.clear_function();
141
}
142
143
void
advance_column
(
unsigned
token_width)
144
{
145
column
+=token_width;
146
}
147
148
protected
:
149
messaget
log
;
150
source_locationt
_source_location
;
151
unsigned
line_no
,
previous_line_no
;
152
unsigned
column
;
153
};
154
155
exprt
&
_newstack
(
parsert
&parser,
unsigned
&x);
156
157
#define newstack(x) _newstack(PARSER, (x))
158
159
#define parser_stack(x) (PARSER.stack[x])
160
#define stack_expr(x) (PARSER.stack[x])
161
#define stack_type(x) \
162
(static_cast<typet &>(static_cast<irept &>(PARSER.stack[x])))
163
164
#define YY_INPUT(buf, result, max_size) \
165
do { \
166
for(result=0; result<max_size;) \
167
{ \
168
char ch; \
169
if(!PARSER.read(ch))
/* NOLINT(readability/braces) */
\
170
{ \
171
if(result==0) \
172
result=YY_NULL; \
173
break; \
174
} \
175
\
176
if(ch!='\r')
/* NOLINT(readability/braces) */
\
177
{ \
178
buf[result++]=ch; \
179
if(ch=='\n')
/* NOLINT(readability/braces) */
\
180
{ \
181
PARSER.inc_line_no(); \
182
break; \
183
} \
184
} \
185
} \
186
} while(0)
187
188
// The following tracks the column of the token, and is nicely explained here:
189
// http://oreilly.com/linux/excerpts/9780596155971/error-reporting-recovery.html
190
191
#define YY_USER_ACTION PARSER.advance_column(yyleng);
192
193
#endif
// CPROVER_UTIL_PARSER_H
exprt
Base class for all expressions.
Definition
expr.h:57
exprt::add_source_location
source_locationt & add_source_location()
Definition
expr.h:241
message_handlert
Definition
message.h:27
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition
message.h:154
parsert
Definition
parser.h:25
parsert::advance_column
void advance_column(unsigned token_width)
Definition
parser.h:143
parsert::parsert
parsert(message_handlert &message_handler)
Definition
parser.h:33
parsert::in
std::istream * in
Definition
parser.h:27
parsert::clear_function
void clear_function()
Definition
parser.h:138
parsert::inc_line_no
void inc_line_no()
Definition
parser.h:73
parsert::set_column
void set_column(unsigned _column)
Definition
parser.h:106
parsert::eof
bool eof()
Definition
parser.h:64
parsert::source_location
const source_locationt & source_location()
Definition
parser.h:111
parsert::set_file
void set_file(const irep_idt &file)
Definition
parser.h:84
parsert::get_line_no
unsigned get_line_no() const
Definition
parser.h:96
parsert::last_line
std::string last_line
Definition
parser.h:29
parsert::~parsert
virtual ~parsert()
Definition
parser.h:42
parsert::parse
virtual bool parse()=0
parsert::column
unsigned column
Definition
parser.h:152
parsert::set_source_location
void set_source_location(exprt &e)
Definition
parser.h:128
parsert::log
messaget log
Definition
parser.h:149
parsert::this_line
std::string this_line
Definition
parser.h:29
parsert::read
bool read(char &ch)
Definition
parser.h:46
parsert::previous_line_no
unsigned previous_line_no
Definition
parser.h:151
parsert::parse_error
void parse_error(const std::string &message, const std::string &before)
Definition
parser.cpp:30
parsert::_source_location
source_locationt _source_location
Definition
parser.h:150
parsert::get_file
irep_idt get_file() const
Definition
parser.h:91
parsert::stack
std::vector< exprt > stack
Definition
parser.h:31
parsert::line_no
unsigned line_no
Definition
parser.h:151
parsert::set_line_no
void set_line_no(unsigned _line_no)
Definition
parser.h:79
parsert::get_column
unsigned get_column() const
Definition
parser.h:101
parsert::set_function
void set_function(const irep_idt &function)
Definition
parser.h:133
source_locationt
Definition
source_location.h:20
expr.h
std
STL namespace.
_newstack
exprt & _newstack(parsert &parser, unsigned &x)
Definition
parser.cpp:19
message.h
file
Definition
kdev_t.h:19
irep_idt
dstringt irep_idt
Definition
verification_result.h:16
util
parser.h
Generated by
1.17.0