MySQL++ SSQLS v2 Translator  3.2.5
parsev2.h
1 /***********************************************************************
2  ssx/parsev2.h - Declares the SSQLS v2 language parsing related classes.
3 
4  Copyright (c) 2009 by Warren Young and (c) 2009-2010 by Educational
5  Technology Resources, Inc. Others may also hold copyrights on code
6  in this file. See the CREDITS.txt file in the top directory of the
7  distribution for details.
8 
9  This file is part of MySQL++.
10 
11  MySQL++ is free software; you can redistribute it and/or modify it
12  under the terms of the GNU Lesser General Public License as published
13  by the Free Software Foundation; either version 2.1 of the License, or
14  (at your option) any later version.
15 
16  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19  License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with MySQL++; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
24  USA
25 ***********************************************************************/
26 
27 #if !defined(MYSQLPP_SSX_PARSEV2_H)
28 #define MYSQLPP_SSX_PARSEV2_H
29 
30 #include <exceptions.h>
31 
32 #include <cassert>
33 #include <fstream>
34 #include <string>
35 #include <vector>
36 
41 class ParseV2
42 {
43 public:
45  typedef std::vector<std::string> StringList;
46 
50  typedef StringList::const_iterator StringListIt;
51 
57  class File
58  {
59  public:
62  File(const char* file_name);
63 
65  void error(const std::string& msg) const;
66 
68  void error(const std::ostringstream& msg) const
69  { error(msg.str()); }
70 
72  const char* name() const { return file_name_.c_str(); }
73 
76  void parse_error(const std::string& msg) const;
77 
79  void parse_error(const std::ostringstream& msg) const
80  { error(msg.str()); }
81 
92  bool read_line(std::string& line, bool& subdirective);
93 
94  private:
104  void add_directory_to_search_path(const char* filepath);
105 
108  void split_path(StringList& parts, const std::string& path) const;
109 
111  std::ifstream ifs_;
112 
116  std::string file_name_;
117 
121  size_t line_number_;
122 
131  static StringList search_path_;
132  };
133 
136  class Line
137  {
138  public:
140  virtual ~Line() { }
141 
152  static Line* parse(const StringList& tl, bool subdirective,
153  const File& file);
154 
157  virtual void print(std::ostream& os) const = 0;
158 
159  protected:
161  Line() { }
162  };
163 
167  typedef std::vector<Line*> LineList;
168 
173  typedef LineList::const_iterator LineListIt;
174 
176  class Field : public Line
177  {
178  public:
192  Field(const std::string& name, const std::string& type,
193  bool is_unsigned = false, bool is_null = false,
194  bool is_autoinc = false, bool is_key = false,
195  const std::string& alias = 0) :
196  name_(name),
197  type_(type),
198  is_autoinc_(is_autoinc),
199  is_key_(is_key),
200  is_null_(is_null),
201  is_unsigned_(is_unsigned),
202  alias_(alias)
203  {
204  }
205 
213  static Field* parse(const StringList& tl, bool subdirective,
214  const File& file);
215 
218  void print(std::ostream& os) const;
219 
229  class Type
230  {
231  public:
245  enum Value {
258  };
259 
266  Type(const std::string& s);
267 
269  Type(Value v) :
270  value_(v)
271  {
272  }
273 
276  void print(std::ostream& os) const;
277 
279  operator Value() const { return value_; }
280 
282  bool operator ==(const Type& rhs) const
283  { return value_ == rhs.value_; }
284 
285  private:
286  Value value_;
287  };
288 
289  private:
290  std::string name_;
291  Type type_;
292  bool is_autoinc_;
293  bool is_key_;
294  bool is_null_;
295  bool is_unsigned_;
296  std::string alias_;
297  };
298 
300  class Include : public Line
301  {
302  public:
308  Include(const char* file_name) :
309  pp2_(new ParseV2(file_name))
310  {
311  }
312 
314  ~Include() { pp2_->clear(); delete pp2_; }
315 
317  // sub-parse's LineList
318  LineListIt begin() const { return pp2_->begin(); }
319 
322  LineListIt end() const { return pp2_->end(); }
323 
331  static Include* parse(const StringList& tl, bool subdirective,
332  const File& file);
333 
334  private:
335  // Never called. Include directives don't appear in the parse
336  // list; the included file's contents appear in its place
337  // instead. Since this method only exists to test parsing
338  // behavior, we can't be called.
339  void print(std::ostream&) const { assert(0); }
340 
343  ParseV2* pp2_;
344  };
345 
351  class Option : public Line
352  {
353  public:
355  virtual ~Option() { }
356 
364  static Option* parse(const StringList& tl, bool subdirective,
365  const File& file);
366 
367  protected:
371  Option(const std::string& value) :
372  value_(value)
373  {
374  }
375 
386  static bool parse_bool(const std::string& value);
387 
395  const char* value() const { return value_.c_str(); }
396 
397  private:
398  // The option's raw value string
399  std::string value_;
400  };
401 
404  {
405  public:
407  AccessorStyleOption(const std::string& value, const File& file) :
408  Option(value),
409  type_(AccessorStyleOption::parse_type(value, file))
410  {
411  }
412 
415  void print(std::ostream& os) const;
416 
417  private:
424  enum Type {
425  unknown,
426  camel_case_lower,
427  camel_case_upper,
428  stroustrup,
429  overloaded
430  };
431 
435  static Type parse_type(const std::string& style,
436  const File& file);
437 
439  Type type_;
440  };
441 
444  {
445  public:
448  Option(value),
449  throw_(Option::parse_bool(value))
450  {
451  }
452 
455  operator bool() const { return throw_; }
456 
459  void print(std::ostream& os) const;
460 
461  private:
462  bool throw_;
463  };
464 
467  {
468  public:
470  HeaderExtensionOption(const std::string& value) :
471  Option(value)
472  {
473  }
474 
476  const char* extension() const { return value(); }
477 
480  void print(std::ostream& os) const;
481  };
482 
485  {
486  public:
488  ImplementationExtensionOption(const std::string& value) :
489  Option(value)
490  {
491  }
492 
495  const char* extension() const { return value(); }
496 
499  void print(std::ostream& os) const;
500  };
501 
503  class Table : public Line
504  {
505  public:
512  Table(const std::string& name, const std::string& alias,
513  const std::string& filebase);
514 
522  static Table* parse(const StringList& tl, bool subdirective,
523  const File& file);
524 
527  void print(std::ostream& os) const;
528 
529  private:
530  std::string name_, alias_, filebase_;
531  };
532 
535  class FileException : public mysqlpp::Exception
536  {
537  public:
542  FileException(const std::string& what) : Exception(what) { }
543  };
544 
547  class ParseException : public mysqlpp::Exception
548  {
549  public:
555  ParseException(const std::string& what,
556  const std::string& file_name, size_t line) :
557  Exception(what),
558  file_name_(file_name),
559  line_(line)
560  {
561  }
562 
564  ~ParseException() throw() { }
565 
567  const char* file_name() const { return file_name_.c_str(); }
568 
570  size_t line() const { return line_; }
571 
572  private:
573  std::string file_name_;
574  size_t line_;
575  };
576 
588  ParseV2(const char* file_name);
589 
592  {
593  for (LineListIt it = lines_.begin(); it != lines_.end(); ++it) {
594  delete *it;
595  }
596  }
597 
599  LineListIt begin() const { return lines_.begin(); }
600 
605  void clear() { lines_.clear(); }
606 
608  LineListIt end() const { return lines_.end(); }
609 
610 private:
612  void tokenize(StringList& tokens, const std::string& line) const;
613 
615  File file_;
616 
627  LineList lines_;
628 };
629 
635 std::ostream& operator<<(std::ostream& os, const ParseV2::Line& line);
636 
637 #endif // !defined(MYSQLPP_SSX_PARSEV2_H)
HeaderExtensionOption(const std::string &value)
Constructor.
Definition: parsev2.h:470
SET.
Definition: parsev2.h:257
LineListIt end() const
Get an iterator pointing just past the end of our LineList.
Definition: parsev2.h:608
Value
Known SQL field types.
Definition: parsev2.h:245
virtual ~Option()
Virtual dtor, since this is a base class.
Definition: parsev2.h:355
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
FLOAT, FLOAT4, FLOAT8.
Definition: parsev2.h:250
'option implementation_extension' directive line
Definition: parsev2.h:484
const char * name() const
Return the file's name.
Definition: parsev2.h:72
'option exception_on_schema_mismatch' directive line
Definition: parsev2.h:443
'include' directive line
Definition: parsev2.h:300
AccessorStyleOption(const std::string &value, const File &file)
Constructor.
Definition: parsev2.h:407
TIME.
Definition: parsev2.h:256
A smart enum for converting SQL type strings to one of a relatively few types we directly support.
Definition: parsev2.h:229
Exception object thrown by File::error() to report an SSQLS v2 parsing error.
Definition: parsev2.h:547
Type(const std::string &s)
Constructor.
Definition: parsev2.cpp:251
const char * file_name() const
Get name of file where error occurred.
Definition: parsev2.h:567
LineListIt end() const
Get an iterator pointing to just past the end of the sub-parse's LineList.
Definition: parsev2.h:322
~Include()
Destructor.
Definition: parsev2.h:314
const char * extension() const
Return the extension used for C++ implementation files we emit.
Definition: parsev2.h:495
LineListIt begin() const
Get an iterator pointing to the start of the.
Definition: parsev2.h:318
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
virtual ~Line()
Virtual dtor, since this is a base class.
Definition: parsev2.h:140
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:452
static bool parse_bool(const std::string &value)
Convert a string expressing a boolean value to a bool.
Definition: parsev2.cpp:575
std::vector< Line * > LineList
A list of pointers to Line objects.
Definition: parsev2.h:167
void print(std::ostream &os) const
Print type description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:305
Exception object thrown to indicate a file I/O error.
Definition: parsev2.h:535
bool operator==(const Type &rhs) const
Equality operator.
Definition: parsev2.h:282
ExceptionOnSchemaMismatchOption(const std::string &value)
Constructor.
Definition: parsev2.h:447
Base class for parsed SSQLS v2 declaration lines.
Definition: parsev2.h:136
'option accessor_style' directive line
Definition: parsev2.h:403
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:459
'field' directive line
Definition: parsev2.h:176
void parse_error(const std::ostringstream &msg) const
Definition: parsev2.h:79
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
Type(Value v)
Copy constructor.
Definition: parsev2.h:269
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()
Destructor.
Definition: parsev2.h:591
~ParseException()
Destructor.
Definition: parsev2.h:564
void error(const std::ostringstream &msg) const
Definition: parsev2.h:68
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:122
'option header_extension' directive line
Definition: parsev2.h:466
FileException(const std::string &what)
Constructor.
Definition: parsev2.h:542
DOUBLE, DECIMAL, FIXED, NUMERIC.
Definition: parsev2.h:251
INT, MEDIUMINT, INT3, INT4.
Definition: parsev2.h:248
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
Line()
Protected ctor, to prevent instantiation.
Definition: parsev2.h:161
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
const char * value() const
Return the option's value in string form, unmodified from the original parse.
Definition: parsev2.h:395
Parses SSQLS v2 documents and holds the parse result.
Definition: parsev2.h:41
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
LineListIt begin() const
Get an iterator pointing to the start of our LineList.
Definition: parsev2.h:599
Holds information about an SSQLS v2 file we're parsing.
Definition: parsev2.h:57
BIGINT, INT8.
Definition: parsev2.h:249
ParseException(const std::string &what, const std::string &file_name, size_t line)
Constructor.
Definition: parsev2.h:555
const char * extension() const
Return the extension used for C++ headers we emit.
Definition: parsev2.h:476
Base class for known SSQLS v2 'option' directives.
Definition: parsev2.h:351
ParseV2(const char *file_name)
Constructor.
Definition: parsev2.cpp:52
std::vector< std::string > StringList
List of tokens as returned by boost::algorithm::split.
Definition: parsev2.h:45
StringList::const_iterator StringListIt
Iterator into a StringList.
Definition: parsev2.h:50
SMALLINT, INT2.
Definition: parsev2.h:247
DATE.
Definition: parsev2.h:254
void error(const std::string &msg) const
Throw a FileException containing the given message.
Definition: parsev2.cpp:392
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
virtual void print(std::ostream &os) const =0
Print line's contents out to a stream in SSQLS v2 form.
void print(std::ostream &os) const
Print the option description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:162
TINYINT, INT1, BOOL.
Definition: parsev2.h:246
void clear()
Dump our line list.
Definition: parsev2.h:605
size_t line() const
Get line number where error occurred.
Definition: parsev2.h:570
*CHAR, ENUM, *TEXT
Definition: parsev2.h:252
bool read_line(std::string &line, bool &subdirective)
Read a line in from a file.
Definition: parsev2.cpp:406
LineList::const_iterator LineListIt
Iterator into a LineList.
Definition: parsev2.h:173
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
'table' directive line
Definition: parsev2.h:503
Option(const std::string &value)
Protected ctor, so we cannot be directly instantiated.
Definition: parsev2.h:371
void print(std::ostream &os) const
Print field description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:239
ImplementationExtensionOption(const std::string &value)
Constructor.
Definition: parsev2.h:488
void print(std::ostream &os) const
Print the table description out to a stream in SSQLS v2 form.
Definition: parsev2.cpp:645
DATETIME, TIMESTAMP.
Definition: parsev2.h:255
Table(const std::string &name, const std::string &alias, const std::string &filebase)
Constructor.
Definition: parsev2.cpp:595
*BLOB, VARBINARY
Definition: parsev2.h:253