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 
33 ctokenitt match_bracket(ctokenitt t, char open, char close)
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 
65 match_bracket(ctokenitt t, char open, char close, ctokenitt::tokenst &dest)
66 {
67  auto end = match_bracket(t, open, close);
68  std::copy(t.cit(), end.cit(), dest.end());
69  return end;
70 }
exception_utils.h
ctokenitt
Definition: ctokenit.h:17
invalid_input_exceptiont
Thrown when user-provided input cannot be processed.
Definition: exception_utils.h:162
invariant.h
ctokenit.h
ctokenitt::operator++
ctokenitt operator++(int)
Definition: ctokenit.cpp:25
is_ws
static bool is_ws(const ctokent &t)
Definition: ctoken.h:83
is_comment
static bool is_comment(const ctokent &t)
Definition: ctoken.h:93
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
match_bracket
ctokenitt match_bracket(ctokenitt t, char open, char close)
Definition: ctokenit.cpp:33
ctokent
Definition: ctoken.h:18
is_preprocessor_directive
static bool is_preprocessor_directive(const ctokent &t)
Definition: ctoken.h:98
ctokenitt::eof
bool eof() const
Definition: ctokenit.h:31
ctokenitt::operator*
const ctokent & operator*() const
Definition: ctokenit.cpp:19
ctokenitt::tokens
const tokenst & tokens
Definition: ctokenit.h:62
ctokenitt::cit
tokenst::const_iterator cit() const
Definition: ctokenit.h:51
ctokenitt::tokenst
std::vector< ctokent > tokenst
Definition: ctokenit.h:20
ctokenitt::pos
std::size_t pos
Definition: ctokenit.h:63