summaryrefslogtreecommitdiff
path: root/TAO/CIAO/CCF/CCF/IDL2/LexicalAnalyzer.hpp
blob: c5026369b5a568dcef95d62fc8cf3f1cb5d05fa5 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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 <cctype>
#include <string>
#include <istream>

#include "CCF/CompilerElements/TokenStream.hpp"

#include "CCF/IDL2/Token.hpp"

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

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

    public:
      LexicalAnalyzer (TokenStream<char>& is);

      virtual TokenPtr
      next ();

    protected:
      typedef
      TokenStream<char>::int_type
      int_type;

      typedef
      TokenStream<char>::char_type
      char_type;

      typedef
      TokenStream<char>::traits
      traits;

      struct Char
      {
        Char (int_type c, unsigned long line = 0)
            : c_ (c), line_ (line)
        {
        }

      public:
        bool
        is_eof () const
        {
          return !traits::not_eof (c_);
        }

        bool
        is_alpha (std::locale const& l) const
        {
          return std::isalpha (char_ (), l);
        }

        bool
        is_oct_digit (std::locale const& l) const
        {
          char_type c (char_ ());

          return std::isdigit (c, l) && c != '8' && c != '9';
        }

        bool
        is_dec_digit (std::locale const& l) const
        {
          return std::isdigit (char_ (), l);
        }

        bool
        is_hex_digit (std::locale const& l) const
        {
          return std::isxdigit (char_ (), l);
        }

        bool
        is_alnum (std::locale const& l) const
        {
          return std::isalnum (char_ (), l);
        }

        bool
        is_space (std::locale const& l) const
        {
          return std::isspace (char_ (), l);
        }

        Char
        to_upper (std::locale const& l) const
        {
          return Char (std::toupper (char_ (), l), line_);
        }

      public:
        char_type
        char_ () const
        {
          return traits::to_char_type (c_);
        }

        unsigned long
        line () const
        {
          return line_;
        }

      public:
        friend bool
        operator== (Char const& a, Char const& b)
        {
          return a.is_eof () && b.is_eof () || a.char_ () == b.char_ ();
        }

        friend bool
        operator== (Char const& a, char b)
        {
          return !a.is_eof () && a.char_ () == b;
        }

        friend bool
        operator== (char a, Char const& b)
        {
          return b == a;
        }

        friend bool
        operator!= (Char const& a, Char const& b)
        {
          return !(a.is_eof () && b.is_eof () || a.char_ () == b.char_ ());
        }

        friend bool
        operator!= (Char const& a, char b)
        {
          return a.is_eof () || a.char_ () != b;
        }

        friend bool
        operator!= (char a, Char const& b)
        {
          return b != a;
        }

      private:
        int_type c_;
        unsigned long line_;
      };


    protected:
      virtual Char
      get ();

      virtual Char
      peek ();

      virtual Char
      peek_more ();

      virtual void
      ret (Char const& c);

      /*
      char_type
      to_char_type (int_type i);
      */

      virtual void
      cxx_comment (Char c);

      virtual void
      c_comment (Char c);

      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);

      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:
      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;

      typedef
      std::deque<Char>
      CharBuffer;

    protected:

      Char
      get_from_stream ();

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

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

    protected:
      std::locale loc_;

      TokenStream<char>& is_;

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

      // line numbering mechanism
      bool after_nl;
      unsigned long line_;

      // look ahead mechanism

      CharBuffer buffer_;
    };
  }
}

#endif  // CCF_IDL2_LEXICAL_ANALYZER_HPP