CBMC
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 "deprecate.h"
16 #include "expr.h"
17 #include "message.h"
18 
19 #include <filesystem>
20 #include <iosfwd>
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  DEPRECATED(SINCE(2023, 12, 20, "use parsert(message_handler) instead"))
34  parsert() : in(nullptr), line_no(0), previous_line_no(0), column(1)
35  {
36  }
37 
38  explicit parsert(message_handlert &message_handler)
39  : in(nullptr),
40  log(message_handler),
41  line_no(0),
43  column(1)
44  {
45  }
46 
47  virtual ~parsert() { }
48 
49  // The following are for the benefit of the scanner
50 
51  bool read(char &ch)
52  {
53  if(!in->read(&ch, 1))
54  return false;
55 
56  if(ch=='\n')
57  {
58  last_line.swap(this_line);
59  this_line.clear();
60  }
61  else
62  this_line+=ch;
63 
64  return true;
65  }
66 
67  virtual bool parse()=0;
68 
69  bool eof()
70  {
71  return in->eof();
72  }
73 
74  void parse_error(
75  const std::string &message,
76  const std::string &before);
77 
78  void inc_line_no()
79  {
80  ++line_no;
81  column=1;
82  }
83 
84  void set_line_no(unsigned _line_no)
85  {
86  line_no=_line_no;
87  }
88 
89  void set_file(const irep_idt &file)
90  {
93  std::filesystem::current_path().string());
94  }
95 
97  {
98  return source_location.get_file();
99  }
100 
101  unsigned get_line_no() const
102  {
103  return line_no;
104  }
105 
106  unsigned get_column() const
107  {
108  return column;
109  }
110 
111  void set_column(unsigned _column)
112  {
113  column=_column;
114  }
115 
117  {
118  // Only set line number when needed, as this destroys sharing.
120  {
123  }
124 
126  }
127 
128  void set_function(const irep_idt &function)
129  {
130  source_location.set_function(function);
131  }
132 
133  void advance_column(unsigned token_width)
134  {
135  column+=token_width;
136  }
137 
138  // should be protected or even just be a reference to a message handler, but
139  // for now enables a step-by-step transition
141 
142 protected:
145  unsigned column;
146 };
147 
148 exprt &_newstack(parsert &parser, unsigned &x);
149 
150 #define newstack(x) _newstack(PARSER, (x))
151 
152 #define parser_stack(x) (PARSER.stack[x])
153 #define stack_expr(x) (PARSER.stack[x])
154 #define stack_type(x) \
155  (static_cast<typet &>(static_cast<irept &>(PARSER.stack[x])))
156 
157 #define YY_INPUT(buf, result, max_size) \
158  do { \
159  for(result=0; result<max_size;) \
160  { \
161  char ch; \
162  if(!PARSER.read(ch)) /* NOLINT(readability/braces) */ \
163  { \
164  if(result==0) \
165  result=YY_NULL; \
166  break; \
167  } \
168  \
169  if(ch!='\r') /* NOLINT(readability/braces) */ \
170  { \
171  buf[result++]=ch; \
172  if(ch=='\n') /* NOLINT(readability/braces) */ \
173  { \
174  PARSER.inc_line_no(); \
175  break; \
176  } \
177  } \
178  } \
179  } while(0)
180 
181 // The following tracks the column of the token, and is nicely explained here:
182 // http://oreilly.com/linux/excerpts/9780596155971/error-reporting-recovery.html
183 
184 #define YY_USER_ACTION PARSER.advance_column(yyleng);
185 
186 #endif // CPROVER_UTIL_PARSER_H
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
source_locationt & add_source_location()
Definition: expr.h:236
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
Definition: parser.h:25
void advance_column(unsigned token_width)
Definition: parser.h:133
source_locationt source_location
Definition: parser.h:143
parsert(message_handlert &message_handler)
Definition: parser.h:38
std::istream * in
Definition: parser.h:27
void inc_line_no()
Definition: parser.h:78
void set_column(unsigned _column)
Definition: parser.h:111
bool eof()
Definition: parser.h:69
void set_file(const irep_idt &file)
Definition: parser.h:89
unsigned get_line_no() const
Definition: parser.h:101
std::string last_line
Definition: parser.h:29
virtual ~parsert()
Definition: parser.h:47
virtual bool parse()=0
unsigned column
Definition: parser.h:145
void set_source_location(exprt &e)
Definition: parser.h:116
messaget log
Definition: parser.h:140
std::string this_line
Definition: parser.h:29
bool read(char &ch)
Definition: parser.h:51
unsigned previous_line_no
Definition: parser.h:144
void parse_error(const std::string &message, const std::string &before)
Definition: parser.cpp:30
irep_idt get_file() const
Definition: parser.h:96
std::vector< exprt > stack
Definition: parser.h:31
unsigned line_no
Definition: parser.h:144
void set_line_no(unsigned _line_no)
Definition: parser.h:84
unsigned get_column() const
Definition: parser.h:106
void set_function(const irep_idt &function)
Definition: parser.h:128
void set_working_directory(const irep_idt &cwd)
const irep_idt & get_file() const
void set_file(const irep_idt &file)
void set_line(const irep_idt &line)
void set_function(const irep_idt &function)
#define SINCE(year, month, day, msg)
Definition: deprecate.h:26
#define DEPRECATED(msg)
Definition: deprecate.h:23
exprt & _newstack(parsert &parser, unsigned &x)
Definition: parser.cpp:19