Drizzled Public API Documentation

reader.h
00001 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00002  * 
00003  *  JSON Library, originally from http://jsoncpp.sourceforge.net/
00004  *
00005  *  Copyright (C) 2011 Stewart Smith
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions are
00010  *  met:
00011  *
00012  *      * Redistributions of source code must retain the above copyright
00013  *  notice, this list of conditions and the following disclaimer.
00014  *
00015  *      * Redistributions in binary form must reproduce the above
00016  *  copyright notice, this list of conditions and the following disclaimer
00017  *  in the documentation and/or other materials provided with the
00018  *  distribution.
00019  *
00020  *      * The names of its contributors may not be used to endorse or
00021  *  promote products derived from this software without specific prior
00022  *  written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #pragma once
00039 
00040 #include "features.h"
00041 #include "value.h"
00042 #include <deque>
00043 #include <stack>
00044 #include <string>
00045 #include <iosfwd>
00046 
00047 namespace Json {
00048 
00052    class JSON_API Reader
00053    {
00054    public:
00055       typedef char Char;
00056       typedef const Char *Location;
00057 
00061       Reader();
00062 
00066       Reader( const Features &features );
00067 
00078       bool parse( const std::string &document, 
00079                   Value &root,
00080                   bool collectComments = true );
00081 
00092       bool parse( const char *beginDoc, const char *endDoc, 
00093                   Value &root,
00094                   bool collectComments = true );
00095 
00098       bool parse( std::istream &is,
00099                   Value &root,
00100                   bool collectComments = true );
00101 
00107       std::string getFormatedErrorMessages() const;
00108 
00109    private:
00110       enum TokenType
00111       {
00112          tokenEndOfStream = 0,
00113          tokenObjectBegin,
00114          tokenObjectEnd,
00115          tokenArrayBegin,
00116          tokenArrayEnd,
00117          tokenString,
00118          tokenNumber,
00119          tokenTrue,
00120          tokenFalse,
00121          tokenNull,
00122          tokenArraySeparator,
00123          tokenMemberSeparator,
00124          tokenComment,
00125          tokenError
00126       };
00127 
00128       class Token
00129       {
00130       public:
00131          TokenType type_;
00132          Location start_;
00133          Location end_;
00134       };
00135 
00136       class ErrorInfo
00137       {
00138       public:
00139          Token token_;
00140          std::string message_;
00141          Location extra_;
00142       };
00143 
00144       typedef std::deque<ErrorInfo> Errors;
00145 
00146       bool expectToken( TokenType type, Token &token, const char *message );
00147       bool readToken( Token &token );
00148       void skipSpaces();
00149       bool match( Location pattern, 
00150                   int patternLength );
00151       bool readComment();
00152       bool readCStyleComment();
00153       bool readCppStyleComment();
00154       bool readString();
00155       void readNumber();
00156       bool readValue();
00157       bool readObject( Token &token );
00158       bool readArray( Token &token );
00159       bool decodeNumber( Token &token );
00160       bool decodeString( Token &token );
00161       bool decodeString( Token &token, std::string &decoded );
00162       bool decodeDouble( Token &token );
00163       bool decodeUnicodeCodePoint( Token &token, 
00164                                    Location &current, 
00165                                    Location end, 
00166                                    unsigned int &unicode );
00167       bool decodeUnicodeEscapeSequence( Token &token, 
00168                                         Location &current, 
00169                                         Location end, 
00170                                         unsigned int &unicode );
00171       bool addError( const std::string &message, 
00172                      Token &token,
00173                      Location extra = 0 );
00174       bool recoverFromError( TokenType skipUntilToken );
00175       bool addErrorAndRecover( const std::string &message, 
00176                                Token &token,
00177                                TokenType skipUntilToken );
00178       void skipUntilSpace();
00179       Value &currentValue();
00180       Char getNextChar();
00181       void getLocationLineAndColumn( Location location,
00182                                      int &line,
00183                                      int &column ) const;
00184       std::string getLocationLineAndColumn( Location location ) const;
00185       void addComment( Location begin, 
00186                        Location end, 
00187                        CommentPlacement placement );
00188       void skipCommentTokens( Token &token );
00189    
00190       typedef std::stack<Value *> Nodes;
00191       Nodes nodes_;
00192       Errors errors_;
00193       std::string document_;
00194       Location begin_;
00195       Location end_;
00196       Location current_;
00197       Location lastValueEnd_;
00198       Value *lastValue_;
00199       std::string commentsBefore_;
00200       Features features_;
00201       bool collectComments_;
00202    };
00203 
00228    std::istream& operator>>( std::istream&, Value& );
00229 
00230 } // namespace Json