summaryrefslogtreecommitdiff
path: root/CIAO/CCF/CCF/IDL2/LexicalAnalyzer.hpp
blob: e75641e40c80a45e6161cf54cdbd08d24bc84e1d (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// file      : CCF/IDL2/LexicalAnalyzer.hpp
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#ifndef CCF_IDL2_LEXICAL_ANALYZER_HPP
#define CCF_IDL2_LEXICAL_ANALYZER_HPP

#include <set>
#include <map>
#include <deque>
#include <locale>
#include <string>

#include "CCF/CompilerElements/TokenStream.hpp"
#include "CCF/CompilerElements/PreprocessorToken.hpp"
#include "CCF/IDL2/Token.hpp"

namespace CCF
{
  namespace IDL2
  {
    //@@ Call to get() after eof is illegal.
    //
    //

    class LexicalAnalyzer : public CompilerElements::TokenStream<TokenPtr>
    {
    public:
      virtual
      ~LexicalAnalyzer () throw () {}

    public:

      typedef
      CompilerElements::CPP::Token
      Char;

      typedef
      std::deque<Char>
      CharBuffer;

      LexicalAnalyzer (CompilerElements::TokenStream<Char>& is);

      virtual TokenPtr
      next ();

    protected:
      virtual Char
      skip_space (Char c);

      virtual TokenPtr
      identifier (Char c);

      virtual bool
      punctuation (Char c, TokenPtr& token);

      virtual bool
      operator_ (Char c, TokenPtr& token);

      virtual bool
      character_literal (Char c, TokenPtr& token);

      virtual bool
      string_literal (Char c, TokenPtr& token);

      virtual std::string
      string_literal_trailer ();

      virtual bool
      integer_literal (Char c, TokenPtr& token);

      // Literal scanners.
      //
      //
      class Format {};
      class Boundary {};

      std::pair<char, std::size_t>
      scan_char (char const* s) throw (Format);

      std::string
      scan_string (std::string const& s) throw (Format);

      unsigned long long
      scan_integer (std::string const& s, unsigned short base)
        throw (Format, Boundary);

    protected:
      virtual Char
      get ();

      virtual Char
      peek ();

      virtual Char
      peek_more ();

      virtual void
      ret (Char const& c);

    protected:
      // Character utility functions.
      //
      bool
      is_alpha (char c) const
      {
        return std::isalpha (c, loc_);
      }

      bool
      is_oct_digit (char c) const
      {
        return std::isdigit (c, loc_) && c != '8' && c != '9';
      }

      bool
      is_dec_digit (char c) const
      {
        return std::isdigit (c, loc_);
      }

      bool
      is_hex_digit (char c) const
      {
        return std::isxdigit (c, loc_);
      }

      bool
      is_alnum (char c) const
      {
        return std::isalnum (c, loc_);
      }

      bool
      is_space (char c) const
      {
        return std::isspace (c, loc_);
      }

      bool
      is_eos (Char const& c) const
      {
        return c == Char::eos;
      }

      char
      to_upper (char c) const
      {
        return std::toupper (c, loc_);
      }

    protected:
      typedef
      std::set<std::string>
      KeywordTable;

      struct IdentifierTreeNode
      {
        typedef
        std::map<std::string, IdentifierTreeNode>
        PrefixMap;

        IdentifierTreeNode&
        operator[] (char const* key)
        {
          return map_[key];
        }

        PrefixMap map_;
      };

      typedef
      std::set<std::string>
      PunctuationTable;

      typedef
      std::set<std::string>
      OperatorTable;

    protected:
      bool
      read_simple_identifier (std::string& lexeme, CharBuffer& buf);

      bool
      traverse_identifier_tree (std::string& lexeme,
                                IdentifierTreeNode const& node);

    protected:
      std::locale loc_;

      CompilerElements::TokenStream<Char>& is_;

      CharBuffer ibuffer_;

      KeywordTable keyword_table_;
      IdentifierTreeNode identifier_tree_;
      PunctuationTable punctuation_table_;
      OperatorTable operator_table_;
    };
  }
}

#endif  // CCF_IDL2_LEXICAL_ANALYZER_HPP