MySQL++ SSQLS v2 Translator: parsev2.h Source File
parsev2.h
/***********************************************************************
ssx/parsev2.h - Declares the SSQLS v2 language parsing related classes.
Copyright (c) 2009 by Warren Young and (c) 2009-2010 by Educational
Technology Resources, Inc. Others may also hold copyrights on code
in this file. See the CREDITS.txt file in the top directory of the
distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_SSX_PARSEV2_H)
#define MYSQLPP_SSX_PARSEV2_H
#include <exceptions.h>
#include <cassert>
#include <fstream>
#include <string>
#include <vector>
class ParseV2
{
public:
typedef std::vector<std::string> StringList;
typedef StringList::const_iterator StringListIt;
class File
{
public:
File(const char* file_name);
void error(const std::string& msg) const;
void error(const std::ostringstream& msg) const
{ error(msg.str()); }
const char* name() const { return file_name_.c_str(); }
void parse_error(const std::string& msg) const;
void parse_error(const std::ostringstream& msg) const
{ error(msg.str()); }
bool read_line(std::string& line, bool& subdirective);
private:
void add_directory_to_search_path(const char* filepath);
void split_path(StringList& parts, const std::string& path) const;
std::ifstream ifs_;
std::string file_name_;
size_t line_number_;
static StringList search_path_;
};
class Line
{
public:
virtual ~Line() { }
static Line* parse(const StringList& tl, bool subdirective,
const File& file);
virtual void print(std::ostream& os) const = 0;
protected:
Line() { }
};
typedef std::vector<Line*> LineList;
typedef LineList::const_iterator LineListIt;
class Field : public Line
{
public:
Field(const std::string& name, const std::string& type,
bool is_unsigned = false, bool is_null = false,
bool is_autoinc = false, bool is_key = false,
const std::string& alias = 0) :
name_(name),
type_(type),
is_autoinc_(is_autoinc),
is_key_(is_key),
is_null_(is_null),
is_unsigned_(is_unsigned),
alias_(alias)
{
}
static Field* parse(const StringList& tl, bool subdirective,
const File& file);
void print(std::ostream& os) const;
class Type
{
public:
enum Value {
ft_tinyint,
ft_smallint,
ft_mediumint,
ft_bigint,
ft_float,
ft_double,
ft_string,
ft_blob,
ft_date,
ft_datetime,
ft_time,
ft_set
};
Type(const std::string& s);
Type(Value v) :
value_(v)
{
}
void print(std::ostream& os) const;
operator Value() const { return value_; }
bool operator ==(const Type& rhs) const
{ return value_ == rhs.value_; }
private:
Value value_;
};
private:
std::string name_;
Type type_;
bool is_autoinc_;
bool is_key_;
bool is_null_;
bool is_unsigned_;
std::string alias_;
};
class Include : public Line
{
public:
Include(const char* file_name) :
pp2_(new ParseV2(file_name))
{
}
~Include() { pp2_->clear(); delete pp2_; }
// sub-parse's LineList
LineListIt begin() const { return pp2_->begin(); }
LineListIt end() const { return pp2_->end(); }
static Include* parse(const StringList& tl, bool subdirective,
const File& file);
private:
// Never called. Include directives don't appear in the parse
// list; the included file's contents appear in its place
// instead. Since this method only exists to test parsing
// behavior, we can't be called.
void print(std::ostream&) const { assert(0); }
ParseV2* pp2_;
};
class Option : public Line
{
public:
virtual ~Option() { }
static Option* parse(const StringList& tl, bool subdirective,
const File& file);
protected:
Option(const std::string& value) :
value_(value)
{
}
static bool parse_bool(const std::string& value);
const char* value() const { return value_.c_str(); }
private:
// The option's raw value string
std::string value_;
};
class AccessorStyleOption : public Option
{
public:
AccessorStyleOption(const std::string& value, const File& file) :
Option(value),
type_(AccessorStyleOption::parse_type(value, file))
{
}
void print(std::ostream& os) const;
private:
enum Type {
unknown,
camel_case_lower,
camel_case_upper,
stroustrup,
overloaded
};
static Type parse_type(const std::string& style,
const File& file);
Type type_;
};
class ExceptionOnSchemaMismatchOption : public Option
{
public:
ExceptionOnSchemaMismatchOption(const std::string& value) :
Option(value),
throw_(Option::parse_bool(value))
{
}
operator bool() const { return throw_; }
void print(std::ostream& os) const;
private:
bool throw_;
};
class HeaderExtensionOption : public Option
{
public:
HeaderExtensionOption(const std::string& value) :
Option(value)
{
}
const char* extension() const { return value(); }
void print(std::ostream& os) const;
};
class ImplementationExtensionOption : public Option
{
public:
ImplementationExtensionOption(const std::string& value) :
Option(value)
{
}
const char* extension() const { return value(); }
void print(std::ostream& os) const;
};
class Table : public Line
{
public:
Table(const std::string& name, const std::string& alias,
const std::string& filebase);
static Table* parse(const StringList& tl, bool subdirective,
const File& file);
void print(std::ostream& os) const;
private:
std::string name_, alias_, filebase_;
};
class FileException : public mysqlpp::Exception
{
public:
FileException(const std::string& what) : Exception(what) { }
};
class ParseException : public mysqlpp::Exception
{
public:
ParseException(const std::string& what,
const std::string& file_name, size_t line) :
Exception(what),
file_name_(file_name),
line_(line)
{
}
~ParseException() throw() { }
const char* file_name() const { return file_name_.c_str(); }
size_t line() const { return line_; }
private:
std::string file_name_;
size_t line_;
};
ParseV2(const char* file_name);
~ParseV2()
{
for (LineListIt it = lines_.begin(); it != lines_.end(); ++it) {
delete *it;
}
}
LineListIt begin() const { return lines_.begin(); }
void clear() { lines_.clear(); }
LineListIt end() const { return lines_.end(); }
private:
void tokenize(StringList& tokens, const std::string& line) const;
File file_;
LineList lines_;
};
std::ostream& operator<<(std::ostream& os, const ParseV2::Line& line);
#endif // !defined(MYSQLPP_SSX_PARSEV2_H)
ParseV2::HeaderExtensionOption::HeaderExtensionOption
HeaderExtensionOption(const std::string &value)
Constructor.
Definition: parsev2.h:470
ParseV2::Field::Type::ft_set
SET.
Definition: parsev2.h:257
ParseV2::end
LineListIt end() const
Get an iterator pointing just past the end of our LineList.
Definition: parsev2.h:608
ParseV2::Field::Type::Value
Value
Known SQL field types.
Definition: parsev2.h:245
ParseV2::Option::~Option
virtual ~Option()
Virtual dtor, since this is a base class.
Definition: parsev2.h:355
ParseV2::Field::Field
Field(const std::string &name, const std::string &type, bool is_unsigned=false, bool is_null=false, bool is_autoinc=false, bool is_key=false, const std::string &alias=0)
Holds information about a SQL field declared in the SSQLS v2 language.
Definition: parsev2.h:192
ParseV2::Field::Type::ft_float
FLOAT, FLOAT4, FLOAT8.
Definition: parsev2.h:250
ParseV2::ImplementationExtensionOption
'option implementation_extension' directive line
Definition: parsev2.h:484
ParseV2::File::name
const char * name() const
Return the file's name.
Definition: parsev2.h:72
ParseV2::ExceptionOnSchemaMismatchOption
'option exception_on_schema_mismatch' directive line
Definition: parsev2.h:443
ParseV2::Include
'include' directive line
Definition: parsev2.h:300
ParseV2::AccessorStyleOption::AccessorStyleOption
AccessorStyleOption(const std::string &value, const File &file)
Constructor.
Definition: parsev2.h:407
ParseV2::Field::Type::ft_time
TIME.
Definition: parsev2.h:256
ParseV2::Field::Type
A smart enum for converting SQL type strings to one of a relatively few types we directly support.
Definition: parsev2.h:229
ParseV2::ParseException
Exception object thrown by File::error() to report an SSQLS v2 parsing error.
Definition: parsev2.h:547
ParseV2::Field::Type::Type
Type(const std::string &s)
Constructor.
Definition: parsev2.cpp:251
ParseV2::ParseException::file_name
const char * file_name() const
Get name of file where error occurred.
Definition: parsev2.h:567
ParseV2::Include::end
LineListIt end() const
Get an iterator pointing to just past the end of the sub-parse's LineList.
Definition: parsev2.h:322
ParseV2::Include::~Include
~Include()
Destructor.
Definition: parsev2.h:314
ParseV2::ImplementationExtensionOption::extension
const char * extension() const
Return the extension used for C++ implementation files we emit.
Definition: parsev2.h:495
ParseV2::Include::begin
LineListIt begin() const
Get an iterator pointing to the start of the.
Definition: parsev2.h:318
ParseV2::Line::parse
static Line * parse(const StringList &tl, bool subdirective, const File &file)
Virtual ctor, creating one of our subclass objects based on what we're passed.
Definition: parsev2.cpp:485
ParseV2::Line::~Line
virtual ~Line()
Virtual dtor, since this is a base class.
Definition: parsev2.h:140
ParseV2::HeaderExtensionOption::print
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:452
ParseV2::Option::parse_bool
static bool parse_bool(const std::string &value)
Convert a string expressing a boolean value to a bool.
Definition: parsev2.cpp:575
ParseV2::LineList
std::vector< Line * > LineList
A list of pointers to Line objects.
Definition: parsev2.h:167
ParseV2::Field::Type::print
void print(std::ostream &os) const
Print type description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:305
ParseV2::FileException
Exception object thrown to indicate a file I/O error.
Definition: parsev2.h:535
ParseV2::Field::Type::operator==
bool operator==(const Type &rhs) const
Equality operator.
Definition: parsev2.h:282
ParseV2::ExceptionOnSchemaMismatchOption::ExceptionOnSchemaMismatchOption
ExceptionOnSchemaMismatchOption(const std::string &value)
Constructor.
Definition: parsev2.h:447
ParseV2::Line
Base class for parsed SSQLS v2 declaration lines.
Definition: parsev2.h:136
ParseV2::AccessorStyleOption
'option accessor_style' directive line
Definition: parsev2.h:403
ParseV2::ImplementationExtensionOption::print
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:459
ParseV2::Field
'field' directive line
Definition: parsev2.h:176
ParseV2::File::parse_error
void parse_error(const std::ostringstream &msg) const
Definition: parsev2.h:79
ParseV2::Option::parse
static Option * parse(const StringList &tl, bool subdirective, const File &file)
Attempt to create an Option object from information in the passed StringList.
Definition: parsev2.cpp:536
ParseV2::Field::Type::Type
Type(Value v)
Copy constructor.
Definition: parsev2.h:269
ParseV2::Include::Include
Include(const char *file_name)
Given the name of another SSQLS v2 file, load it up and parse it. Its contents will appear transparen...
Definition: parsev2.h:308
ParseV2::~ParseV2
~ParseV2()
Destructor.
Definition: parsev2.h:591
ParseV2::ParseException::~ParseException
~ParseException()
Destructor.
Definition: parsev2.h:564
ParseV2::File::error
void error(const std::ostringstream &msg) const
Definition: parsev2.h:68
ParseV2::AccessorStyleOption::print
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:122
ParseV2::HeaderExtensionOption
'option header_extension' directive line
Definition: parsev2.h:466
ParseV2::FileException::FileException
FileException(const std::string &what)
Constructor.
Definition: parsev2.h:542
ParseV2::Field::Type::ft_double
DOUBLE, DECIMAL, FIXED, NUMERIC.
Definition: parsev2.h:251
ParseV2::Field::Type::ft_mediumint
INT, MEDIUMINT, INT3, INT4.
Definition: parsev2.h:248
ParseV2::Include::parse
static Include * parse(const StringList &tl, bool subdirective, const File &file)
Attempt to create an Include object from information in the passed StringList.
Definition: parsev2.cpp:466
ParseV2::Line::Line
Line()
Protected ctor, to prevent instantiation.
Definition: parsev2.h:161
ParseV2::File::parse_error
void parse_error(const std::string &msg) const
Throw a ParseException containing the given message and our stored info about the file name and curre...
Definition: parsev2.cpp:399
ParseV2::Option::value
const char * value() const
Return the option's value in string form, unmodified from the original parse.
Definition: parsev2.h:395
ParseV2
Parses SSQLS v2 documents and holds the parse result.
Definition: parsev2.h:41
ParseV2::Table::parse
static Table * parse(const StringList &tl, bool subdirective, const File &file)
Attempt to create a Table object from information in the passed StringList.
Definition: parsev2.cpp:605
ParseV2::begin
LineListIt begin() const
Get an iterator pointing to the start of our LineList.
Definition: parsev2.h:599
ParseV2::File
Holds information about an SSQLS v2 file we're parsing.
Definition: parsev2.h:57
ParseV2::Field::Type::ft_bigint
BIGINT, INT8.
Definition: parsev2.h:249
ParseV2::ParseException::ParseException
ParseException(const std::string &what, const std::string &file_name, size_t line)
Constructor.
Definition: parsev2.h:555
ParseV2::HeaderExtensionOption::extension
const char * extension() const
Return the extension used for C++ headers we emit.
Definition: parsev2.h:476
ParseV2::Option
Base class for known SSQLS v2 'option' directives.
Definition: parsev2.h:351
ParseV2::ParseV2
ParseV2(const char *file_name)
Constructor.
Definition: parsev2.cpp:52
ParseV2::StringList
std::vector< std::string > StringList
List of tokens as returned by boost::algorithm::split.
Definition: parsev2.h:45
ParseV2::StringListIt
StringList::const_iterator StringListIt
Iterator into a StringList.
Definition: parsev2.h:50
ParseV2::Field::Type::ft_smallint
SMALLINT, INT2.
Definition: parsev2.h:247
ParseV2::Field::Type::ft_date
DATE.
Definition: parsev2.h:254
ParseV2::File::error
void error(const std::string &msg) const
Throw a FileException containing the given message.
Definition: parsev2.cpp:392
ParseV2::Field::parse
static Field * parse(const StringList &tl, bool subdirective, const File &file)
Attempt to create a Field object from information in the passed StringList.
Definition: parsev2.cpp:170
ParseV2::Line::print
virtual void print(std::ostream &os) const =0
Print line's contents out to a stream in SSQLS v2 form.
ParseV2::ExceptionOnSchemaMismatchOption::print
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:162
ParseV2::Field::Type::ft_tinyint
TINYINT, INT1, BOOL.
Definition: parsev2.h:246
ParseV2::clear
void clear()
Dump our line list.
Definition: parsev2.h:605
ParseV2::ParseException::line
size_t line() const
Get line number where error occurred.
Definition: parsev2.h:570
ParseV2::Field::Type::ft_string
*CHAR, ENUM, *TEXT
Definition: parsev2.h:252
ParseV2::File::read_line
bool read_line(std::string &line, bool &subdirective)
Read a line in from a file.
Definition: parsev2.cpp:406
ParseV2::LineListIt
LineList::const_iterator LineListIt
Iterator into a LineList.
Definition: parsev2.h:173
ParseV2::File::File
File(const char *file_name)
Open a file for reading, using the search path to file the file if the direct path isn't readable.
Definition: parsev2.cpp:325
ParseV2::Table
'table' directive line
Definition: parsev2.h:503
ParseV2::Option::Option
Option(const std::string &value)
Protected ctor, so we cannot be directly instantiated.
Definition: parsev2.h:371
ParseV2::Field::print
void print(std::ostream &os) const
Print field description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:239
ParseV2::ImplementationExtensionOption::ImplementationExtensionOption
ImplementationExtensionOption(const std::string &value)
Constructor.
Definition: parsev2.h:488
ParseV2::Table::print
void print(std::ostream &os) const
Print the table description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:645
ParseV2::Field::Type::ft_datetime
DATETIME, TIMESTAMP.
Definition: parsev2.h:255
ParseV2::Table::Table
Table(const std::string &name, const std::string &alias, const std::string &filebase)
Constructor.
Definition: parsev2.cpp:595
ParseV2::Field::Type::ft_blob
*BLOB, VARBINARY
Definition: parsev2.h:253