aboutsummaryrefslogtreecommitdiff
path: root/odb/cxx-lexer.hxx
blob: 6d257d3461c83fdcee60bd7a1e9a4abebcfc4215 (plain)
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
// file      : odb/cxx-lexer.hxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#ifndef ODB_CXX_LEXER_HXX
#define ODB_CXX_LEXER_HXX

#include <odb/gcc.hxx>

#include <string>

#include <odb/cxx-token.hxx>

// A C++ keyword. This is an extension to libcpp token types. GCC 4.7.0
// adds this define.
//
#if BUILDING_GCC_MAJOR == 4 && BUILDING_GCC_MINOR <= 6
#  define CPP_KEYWORD ((cpp_ttype) (N_TTYPES + 1))
#endif

class cxx_lexer
{
public:
  virtual
  ~cxx_lexer ();

public:
  struct invalid_input {};

  virtual cpp_ttype
  next (std::string& token, tree* node = 0) = 0;

  // Location of the last returned token.
  //
  virtual location_t
  location () const = 0;

public:
  static char const* token_spelling[N_TTYPES + 1];
};


// Adapter to scan a saved token sequence. It returns numbers in the same
// form as they were saved in the token sequence.
//
class cxx_tokens_lexer: public cxx_lexer
{
public:
  void
  start (cxx_tokens const&, location_t start_loc = 0);

  virtual cpp_ttype
  next (std::string& token, tree* node = 0);

  virtual location_t
  location () const;

private:
  cxx_tokens const* tokens_;
  cxx_tokens::const_iterator cur_;
  location_t loc_;
};


// A thin wrapper around the pragma_lex() function that recognizes
// CPP_KEYWORD. It returns numbers as tree nodes.
//
class cxx_pragma_lexer: public cxx_lexer
{
public:
  void
  start ();

  // Start with an already extracted (using the pragma_lex() function)
  // token. This function translates the CPP_NAME to CPP_KEYWORD if
  // necessary and returns the string token. It also uses the passed
  // token and type for subsequent calls to next() so after the last
  // next() call they will contain the information about the last
  // token parsed.
  //
  std::string
  start (tree& token, cpp_ttype& type);

  virtual cpp_ttype
  next (std::string& token, tree* node = 0);

  virtual location_t
  location () const;

private:
  std::string
  translate ();

private:
  tree* token_;
  cpp_ttype* type_;

  tree token_data_;
  cpp_ttype type_data_;
};

// A thin wrapper around cpp_reader for lexing C++ code fragments. It
// returns numbers as string literals.
//
class cxx_string_lexer: public cxx_lexer
{
public:
  cxx_string_lexer ();

  virtual
  ~cxx_string_lexer ();

public:
  void
  start (std::string const&);

  virtual cpp_ttype
  next (std::string& token, tree* node = 0);

  virtual location_t
  location () const;

private:
  std::string data_;
  std::string buf_;
  line_maps line_map_;
  cpp_reader* reader_;
  cpp_callbacks* callbacks_;
  location_t loc_;
};

#endif // ODB_CXX_LEXER_HXX