/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: * * Copyright (C) 2008 Sun Microsystems, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #pragma once /** @brief This class represents the character input stream consumed during lexical analysis. In addition to consuming the input stream, this class performs some comment pre processing, by filtering out out of bound special text from the query input stream. Two buffers, with pointers inside each buffers, are maintained in parallel. The 'raw' buffer is the original query text, which may contain out-of-bound comments. The 'cpp' (for comments pre processor) is the pre-processed buffer that contains only the query text that should be seen once out-of-bound data is removed. */ namespace drizzled { class Lex_input_stream { public: Lex_input_stream(Session *session, const char* buff, unsigned int length); /** Set the echo mode. When echo is true, characters parsed from the raw input stream are preserved. When false, characters parsed are silently ignored. @param echo the echo mode. */ void set_echo(bool echo) { m_echo= echo; } /** Skip binary from the input stream. @param n number of bytes to accept. */ void skip_binary(int n) { if (m_echo) { memcpy(m_cpp_ptr, m_ptr, n); m_cpp_ptr += n; } m_ptr += n; } /** Get a character, and advance in the stream. @return the next character to parse. */ char yyGet() { char c= *m_ptr++; if (m_echo) *m_cpp_ptr++ = c; return c; } /** Get the last character accepted. @return the last character accepted. */ char yyGetLast() const { return m_ptr[-1]; } /** Look at the next character to parse, but do not accept it. */ char yyPeek() const { return m_ptr[0]; } /** Look ahead at some character to parse. @param n offset of the character to look up */ char yyPeekn(int n) const { return m_ptr[n]; } /** Cancel the effect of the last yyGet() or yySkip(). Note that the echo mode should not change between calls to yyGet / yySkip and yyUnget. The caller is responsible for ensuring that. */ void yyUnget() { m_ptr--; if (m_echo) m_cpp_ptr--; } /** Accept a character, by advancing the input stream. */ void yySkip() { if (m_echo) *m_cpp_ptr++ = *m_ptr++; else m_ptr++; } /** Accept multiple characters at once. @param n the number of characters to accept. */ void yySkipn(int n) { if (m_echo) { memcpy(m_cpp_ptr, m_ptr, n); m_cpp_ptr += n; } m_ptr += n; } /** End of file indicator for the query text to parse. @return true if there are no more characters to parse */ bool eof() { return (m_ptr >= m_end_of_query); } /** End of file indicator for the query text to parse. @param n number of characters expected @return true if there are less than n characters to parse */ bool eof(int n) { return ((m_ptr + n) >= m_end_of_query); } /** Get the raw query buffer. */ const char *get_buf() { return m_buf; } /** Get the pre-processed query buffer. */ const char *get_cpp_buf() { return m_cpp_buf; } /** Get the end of the raw query buffer. */ const char *get_end_of_query() { return m_end_of_query; } /** Mark the stream position as the start of a new token. */ void start_token() { m_tok_start_prev= m_tok_start; m_tok_start= m_ptr; m_tok_end= m_ptr; m_cpp_tok_start_prev= m_cpp_tok_start; m_cpp_tok_start= m_cpp_ptr; m_cpp_tok_end= m_cpp_ptr; } /** Adjust the starting position of the current token. This is used to compensate for starting whitespace. */ void restart_token() { m_tok_start= m_ptr; m_cpp_tok_start= m_cpp_ptr; } /** Get the token start position, in the raw buffer. */ const char *get_tok_start() const { return m_tok_start; } /** Get the token start position, in the pre-processed buffer. */ const char *get_cpp_tok_start() const { return m_cpp_tok_start; } /** Get the token end position, in the raw buffer. */ const char *get_tok_end() const { return m_tok_end; } /** Get the token end position, in the pre-processed buffer. */ const char *get_cpp_tok_end() const { return m_cpp_tok_end; } /** Get the previous token start position, in the raw buffer. */ const char *get_tok_start_prev() const { return m_tok_start_prev; } /** Get the current stream pointer, in the raw buffer. */ const char *get_ptr() const { return m_ptr; } /** Get the current stream pointer, in the pre-processed buffer. */ const char *get_cpp_ptr() const { return m_cpp_ptr; } /** Get the length of the current token, in the raw buffer. */ uint32_t yyLength() const { /* The assumption is that the lexical analyser is always 1 character ahead, which the -1 account for. */ assert(m_ptr > m_tok_start); return (uint32_t) ((m_ptr - m_tok_start) - 1); } /** Get the utf8-body string. */ const char *get_body_utf8_str() const { return m_body_utf8; } /** Get the utf8-body length. */ uint32_t get_body_utf8_length() const { return m_body_utf8_ptr - m_body_utf8; } void body_utf8_append(const char *ptr); void body_utf8_append(const char *ptr, const char *end_ptr); void body_utf8_append_literal(const lex_string_t *txt, const char *end_ptr); /** Current thread. */ Session *m_session; /** Current line number. */ uint32_t yylineno; /** Length of the last token parsed. */ uint32_t yytoklen; /** Interface with bison, value of the last token parsed. */ LEX_YYSTYPE yylval; /** LALR(2) resolution, look ahead token.*/ int lookahead_token; /** LALR(2) resolution, value of the look ahead token.*/ LEX_YYSTYPE lookahead_yylval; private: /** Pointer to the current position in the raw input stream. */ const char *m_ptr; /** Starting position of the last token parsed, in the raw buffer. */ const char *m_tok_start; /** Ending position of the previous token parsed, in the raw buffer. */ const char *m_tok_end; /** End of the query text in the input stream, in the raw buffer. */ const char *m_end_of_query; /** Starting position of the previous token parsed, in the raw buffer. */ const char *m_tok_start_prev; /** Begining of the query text in the input stream, in the raw buffer. */ const char *m_buf; /** Length of the raw buffer. */ uint32_t m_buf_length; /** Echo the parsed stream to the pre-processed buffer. */ bool m_echo; /** Pre-processed buffer. */ char *m_cpp_buf; /** Pointer to the current position in the pre-processed input stream. */ char *m_cpp_ptr; /** Starting position of the last token parsed, in the pre-processed buffer. */ const char *m_cpp_tok_start; /** Starting position of the previous token parsed, in the pre-procedded buffer. */ const char *m_cpp_tok_start_prev; /** Ending position of the previous token parsed, in the pre-processed buffer. */ const char *m_cpp_tok_end; /** UTF8-body buffer created during parsing. */ char *m_body_utf8; /** Pointer to the current position in the UTF8-body buffer. */ char *m_body_utf8_ptr; /** Position in the pre-processed buffer. The query from m_cpp_buf to m_cpp_utf_processed_ptr is converted to UTF8-body. */ const char *m_cpp_utf8_processed_ptr; public: /** Current state of the lexical analyser. */ enum my_lex_states next_state; /** Token character bitmaps, to detect 7bit strings. */ unsigned char tok_bitmap; /** SQL_MODE = IGNORE_SPACE. */ bool ignore_space; /** State of the lexical analyser for comments. */ enum_comment_state in_comment; /** Starting position of the TEXT_STRING or IDENT in the pre-processed buffer. NOTE: this member must be used within base_sql_lex() function only. */ const char *m_cpp_text_start; /** Ending position of the TEXT_STRING or IDENT in the pre-processed buffer. NOTE: this member must be used within base_sql_lex() function only. */ const char *m_cpp_text_end; }; } /* namespace drizzled */