~drizzle-trunk/drizzle/development

1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
20
#pragma once
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
21
22
/**
23
  @brief This class represents the character input stream consumed during
24
  lexical analysis.
25
26
  In addition to consuming the input stream, this class performs some
27
  comment pre processing, by filtering out out of bound special text
28
  from the query input stream.
29
  Two buffers, with pointers inside each buffers, are maintained in
30
  parallel. The 'raw' buffer is the original query text, which may
31
  contain out-of-bound comments. The 'cpp' (for comments pre processor)
32
  is the pre-processed buffer that contains only the query text that
33
  should be seen once out-of-bound data is removed.
34
*/
35
2279.2.2 by Olaf van der Spek
Prune
36
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
37
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
38
class Lex_input_stream
39
{
40
public:
41
  Lex_input_stream(Session *session, const char* buff, unsigned int length);
42
43
  /**
44
    Set the echo mode.
45
46
    When echo is true, characters parsed from the raw input stream are
47
    preserved. When false, characters parsed are silently ignored.
48
    @param echo the echo mode.
49
  */
50
  void set_echo(bool echo)
51
  {
52
    m_echo= echo;
53
  }
54
55
  /**
56
    Skip binary from the input stream.
57
    @param n number of bytes to accept.
58
  */
59
  void skip_binary(int n)
60
  {
61
    if (m_echo)
62
    {
63
      memcpy(m_cpp_ptr, m_ptr, n);
64
      m_cpp_ptr += n;
65
    }
66
    m_ptr += n;
67
  }
68
69
  /**
70
    Get a character, and advance in the stream.
71
    @return the next character to parse.
72
  */
73
  char yyGet()
74
  {
75
    char c= *m_ptr++;
76
    if (m_echo)
77
      *m_cpp_ptr++ = c;
78
    return c;
79
  }
80
81
  /**
82
    Get the last character accepted.
83
    @return the last character accepted.
84
  */
2318.8.7 by Olaf van der Spek
Add const
85
  char yyGetLast() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
86
  {
87
    return m_ptr[-1];
88
  }
89
90
  /**
91
    Look at the next character to parse, but do not accept it.
92
  */
2318.8.7 by Olaf van der Spek
Add const
93
  char yyPeek() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
94
  {
95
    return m_ptr[0];
96
  }
97
98
  /**
99
    Look ahead at some character to parse.
100
    @param n offset of the character to look up
101
  */
2318.8.7 by Olaf van der Spek
Add const
102
  char yyPeekn(int n) const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
103
  {
104
    return m_ptr[n];
105
  }
106
107
  /**
108
    Cancel the effect of the last yyGet() or yySkip().
109
    Note that the echo mode should not change between calls to yyGet / yySkip
110
    and yyUnget. The caller is responsible for ensuring that.
111
  */
112
  void yyUnget()
113
  {
114
    m_ptr--;
115
    if (m_echo)
116
      m_cpp_ptr--;
117
  }
118
119
  /**
120
    Accept a character, by advancing the input stream.
121
  */
122
  void yySkip()
123
  {
124
    if (m_echo)
125
      *m_cpp_ptr++ = *m_ptr++;
126
    else
127
      m_ptr++;
128
  }
129
130
  /**
131
    Accept multiple characters at once.
132
    @param n the number of characters to accept.
133
  */
134
  void yySkipn(int n)
135
  {
136
    if (m_echo)
137
    {
138
      memcpy(m_cpp_ptr, m_ptr, n);
139
      m_cpp_ptr += n;
140
    }
141
    m_ptr += n;
142
  }
143
144
  /**
145
    End of file indicator for the query text to parse.
146
    @return true if there are no more characters to parse
147
  */
2385.2.5 by Olaf van der Spek
Refactor
148
  bool eof() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
149
  {
2385.2.5 by Olaf van der Spek
Refactor
150
    return m_ptr >= m_end_of_query;
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
151
  }
152
153
  /**
154
    End of file indicator for the query text to parse.
155
    @param n number of characters expected
156
    @return true if there are less than n characters to parse
157
  */
2385.2.5 by Olaf van der Spek
Refactor
158
  bool eof(int n) const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
159
  {
2385.2.5 by Olaf van der Spek
Refactor
160
    return m_ptr + n >= m_end_of_query;
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
161
  }
162
163
  /** Get the raw query buffer. */
2385.2.5 by Olaf van der Spek
Refactor
164
  const char *get_buf() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
165
  {
166
    return m_buf;
167
  }
168
169
  /** Get the pre-processed query buffer. */
2385.2.5 by Olaf van der Spek
Refactor
170
  const char *get_cpp_buf() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
171
  {
172
    return m_cpp_buf;
173
  }
174
175
  /** Get the end of the raw query buffer. */
2385.2.5 by Olaf van der Spek
Refactor
176
  const char *get_end_of_query() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
177
  {
178
    return m_end_of_query;
179
  }
180
181
  /** Mark the stream position as the start of a new token. */
182
  void start_token()
183
  {
184
    m_tok_start_prev= m_tok_start;
185
    m_tok_start= m_ptr;
186
    m_tok_end= m_ptr;
187
188
    m_cpp_tok_start_prev= m_cpp_tok_start;
189
    m_cpp_tok_start= m_cpp_ptr;
190
    m_cpp_tok_end= m_cpp_ptr;
191
  }
192
193
  /**
194
    Adjust the starting position of the current token.
195
    This is used to compensate for starting whitespace.
196
  */
197
  void restart_token()
198
  {
199
    m_tok_start= m_ptr;
200
    m_cpp_tok_start= m_cpp_ptr;
201
  }
202
203
  /** Get the token start position, in the raw buffer. */
2318.8.7 by Olaf van der Spek
Add const
204
  const char *get_tok_start() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
205
  {
206
    return m_tok_start;
207
  }
208
209
  /** Get the token start position, in the pre-processed buffer. */
2318.8.7 by Olaf van der Spek
Add const
210
  const char *get_cpp_tok_start() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
211
  {
212
    return m_cpp_tok_start;
213
  }
214
215
  /** Get the token end position, in the raw buffer. */
2318.8.7 by Olaf van der Spek
Add const
216
  const char *get_tok_end() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
217
  {
218
    return m_tok_end;
219
  }
220
221
  /** Get the token end position, in the pre-processed buffer. */
2318.8.7 by Olaf van der Spek
Add const
222
  const char *get_cpp_tok_end() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
223
  {
224
    return m_cpp_tok_end;
225
  }
226
227
  /** Get the previous token start position, in the raw buffer. */
2318.8.7 by Olaf van der Spek
Add const
228
  const char *get_tok_start_prev() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
229
  {
230
    return m_tok_start_prev;
231
  }
232
233
  /** Get the current stream pointer, in the raw buffer. */
2318.8.7 by Olaf van der Spek
Add const
234
  const char *get_ptr() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
235
  {
236
    return m_ptr;
237
  }
238
239
  /** Get the current stream pointer, in the pre-processed buffer. */
2318.8.7 by Olaf van der Spek
Add const
240
  const char *get_cpp_ptr() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
241
  {
242
    return m_cpp_ptr;
243
  }
244
245
  /** Get the length of the current token, in the raw buffer. */
2318.8.7 by Olaf van der Spek
Add const
246
  uint32_t yyLength() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
247
  {
248
    /*
249
      The assumption is that the lexical analyser is always 1 character ahead,
250
      which the -1 account for.
251
    */
252
    assert(m_ptr > m_tok_start);
253
    return (uint32_t) ((m_ptr - m_tok_start) - 1);
254
  }
255
256
  /** Get the utf8-body string. */
2318.8.7 by Olaf van der Spek
Add const
257
  const char *get_body_utf8_str() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
258
  {
259
    return m_body_utf8;
260
  }
261
262
  /** Get the utf8-body length. */
2318.8.7 by Olaf van der Spek
Add const
263
  uint32_t get_body_utf8_length() const
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
264
  {
265
    return m_body_utf8_ptr - m_body_utf8;
266
  }
267
268
  void body_utf8_append(const char *ptr);
269
  void body_utf8_append(const char *ptr, const char *end_ptr);
2445.1.19 by Olaf van der Spek
Refactor
270
  void body_utf8_append_literal(str_ref, const char *end_ptr);
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
271
272
  /** Current thread. */
273
  Session *m_session;
274
275
  /** Current line number. */
276
  uint32_t yylineno;
277
278
  /** Length of the last token parsed. */
279
  uint32_t yytoklen;
280
281
  /** Interface with bison, value of the last token parsed. */
282
  LEX_YYSTYPE yylval;
283
284
  /** LALR(2) resolution, look ahead token.*/
285
  int lookahead_token;
286
287
  /** LALR(2) resolution, value of the look ahead token.*/
288
  LEX_YYSTYPE lookahead_yylval;
289
290
private:
291
  /** Pointer to the current position in the raw input stream. */
292
  const char *m_ptr;
293
294
  /** Starting position of the last token parsed, in the raw buffer. */
295
  const char *m_tok_start;
296
297
  /** Ending position of the previous token parsed, in the raw buffer. */
298
  const char *m_tok_end;
299
300
  /** End of the query text in the input stream, in the raw buffer. */
301
  const char *m_end_of_query;
302
303
  /** Starting position of the previous token parsed, in the raw buffer. */
304
  const char *m_tok_start_prev;
305
306
  /** Begining of the query text in the input stream, in the raw buffer. */
307
  const char *m_buf;
308
309
  /** Length of the raw buffer. */
310
  uint32_t m_buf_length;
311
312
  /** Echo the parsed stream to the pre-processed buffer. */
313
  bool m_echo;
314
315
  /** Pre-processed buffer. */
316
  char *m_cpp_buf;
317
318
  /** Pointer to the current position in the pre-processed input stream. */
319
  char *m_cpp_ptr;
320
321
  /**
322
    Starting position of the last token parsed,
323
    in the pre-processed buffer.
324
  */
325
  const char *m_cpp_tok_start;
326
327
  /**
328
    Starting position of the previous token parsed,
329
    in the pre-procedded buffer.
330
  */
331
  const char *m_cpp_tok_start_prev;
332
333
  /**
334
    Ending position of the previous token parsed,
335
    in the pre-processed buffer.
336
  */
337
  const char *m_cpp_tok_end;
338
339
  /** UTF8-body buffer created during parsing. */
340
  char *m_body_utf8;
341
342
  /** Pointer to the current position in the UTF8-body buffer. */
343
  char *m_body_utf8_ptr;
344
345
  /**
346
    Position in the pre-processed buffer. The query from m_cpp_buf to
347
    m_cpp_utf_processed_ptr is converted to UTF8-body.
348
  */
349
  const char *m_cpp_utf8_processed_ptr;
350
351
public:
352
353
  /** Current state of the lexical analyser. */
354
  enum my_lex_states next_state;
355
356
  /** Token character bitmaps, to detect 7bit strings. */
357
  unsigned char tok_bitmap;
358
359
  /** SQL_MODE = IGNORE_SPACE. */
360
  bool ignore_space;
361
362
  /** State of the lexical analyser for comments. */
363
  enum_comment_state in_comment;
364
365
  /**
366
    Starting position of the TEXT_STRING or IDENT in the pre-processed
367
    buffer.
368
2172.3.6 by Brian Aker
Namespace the parser just a bit, and update our call for the type of parser
369
    NOTE: this member must be used within base_sql_lex() function only.
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
370
  */
371
  const char *m_cpp_text_start;
372
373
  /**
374
    Ending position of the TEXT_STRING or IDENT in the pre-processed
375
    buffer.
376
2172.3.6 by Brian Aker
Namespace the parser just a bit, and update our call for the type of parser
377
    NOTE: this member must be used within base_sql_lex() function only.
1014.4.8 by Jay Pipes
Split Lex_input_stream out into its own header.
378
    */
379
  const char *m_cpp_text_end;
380
381
};
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
382
383
} /* namespace drizzled */
384