CBMC
ctokenit.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C Token Iterator
4 
5 Author: Daniel Kroening, dkr@amazon.com
6 
7 \*******************************************************************/
8 
11 
12 #include "ctokenit.h"
13 
14 #include <util/exception_utils.h>
15 #include <util/invariant.h>
16 
17 #include <algorithm>
18 
20 {
21  PRECONDITION(!eof());
22  return tokens[pos];
23 }
24 
26 {
27  PRECONDITION(!eof());
28  auto pre_increment = *this; // copy
29  pos++;
30  return pre_increment;
31 }
32 
34 {
35  if(!t)
36  return t;
37 
38  // skip whitespace, if any
39  while(t && (is_ws(*t) || is_comment(*t) || is_preprocessor_directive(*t)))
40  t++;
41 
42  if(*t != open)
43  return t;
44 
45  std::size_t bracket_count = 0;
46  while(true)
47  {
48  if(!t)
49  throw invalid_input_exceptiont("expected " + std::string(1, close));
50 
51  const auto &token = *(t++);
52 
53  if(token == open)
54  bracket_count++;
55  else if(token == close)
56  {
57  bracket_count--;
58  if(bracket_count == 0)
59  return t; // done
60  }
61  }
62 }
63 
66 {
67  auto end = match_bracket(t, open, close);
68  std::copy(t.cit(), end.cit(), dest.end());
69  return end;
70 }
tokenst::const_iterator cit() const
Definition: ctokenit.h:53
std::size_t pos
Definition: ctokenit.h:65
bool eof() const
Definition: ctokenit.h:33
ctokenitt operator++(int)
Definition: ctokenit.cpp:25
const ctokent & operator*() const
Definition: ctokenit.cpp:19
std::vector< ctokent > tokenst
Definition: ctokenit.h:22
const tokenst & tokens
Definition: ctokenit.h:64
Definition: ctoken.h:19
Thrown when user-provided input cannot be processed.
static bool is_comment(const ctokent &t)
Definition: ctoken.h:93
static bool is_preprocessor_directive(const ctokent &t)
Definition: ctoken.h:98
static bool is_ws(const ctokent &t)
Definition: ctoken.h:83
ctokenitt match_bracket(ctokenitt t, char open, char close)
Definition: ctokenit.cpp:33
ctokenit
int open(const char *pathname, int flags,...)
Definition: fcntl.c:89
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
int close(int fildes)
Definition: unistd.c:139