User Manual, Developers Guide and API Documentation

reader.hpp

Go to the documentation of this file.
00001 /**********************************************
00002 
00003 License: BSD
00004 Project Webpage: http://cajun-jsonapi.sourceforge.net/
00005 Author: Terry Caton
00006 
00007 ***********************************************/
00008 
00009 #ifndef WNS_JSON_READER_HPP
00010 #define WNS_JSON_READER_HPP
00011 
00012 #include <WNS/probe/bus/json/elements.hpp>
00013 
00014 #include <iostream>
00015 #include <vector>
00016 
00017 namespace wns { namespace probe  { namespace bus { namespace json {
00018 
00019 class Reader
00020 {
00021 public:
00022    // this structure will be reported in one of the exceptions defined below
00023    struct Location
00024    {
00025       Location();
00026 
00027       unsigned int m_nLine;       // document line, zero-indexed
00028       unsigned int m_nLineOffset; // character offset from beginning of line, zero indexed
00029       unsigned int m_nDocOffset;  // character offset from entire document, zero indexed
00030    };
00031 
00032    // thrown during the first phase of reading. generally catches low-level problems such
00033    //  as errant characters or corrupt/incomplete documents
00034    class ScanException : public Exception
00035    {
00036    public:
00037       ScanException(const std::string& sMessage, const Reader::Location& locError) :
00038          Exception(sMessage),
00039          m_locError(locError) {}
00040 
00041       Reader::Location m_locError;
00042    };
00043 
00044    // thrown during the second phase of reading. generally catches higher-level problems such
00045    //  as missing commas or brackets
00046    class ParseException : public Exception
00047    {
00048    public:
00049       ParseException(const std::string& sMessage, const Reader::Location& locTokenBegin, const Reader::Location& locTokenEnd) :
00050          Exception(sMessage),
00051          m_locTokenBegin(locTokenBegin),
00052          m_locTokenEnd(locTokenEnd) {}
00053 
00054       Reader::Location m_locTokenBegin;
00055       Reader::Location m_locTokenEnd;
00056    };
00057 
00058 
00059    // if you know what the document looks like, call one of these...
00060    static void Read(Object& object, std::istream& istr);
00061    static void Read(Array& array, std::istream& istr);
00062    static void Read(String& string, std::istream& istr);
00063    static void Read(Number& number, std::istream& istr);
00064    static void Read(Boolean& boolean, std::istream& istr);
00065    static void Read(Null& null, std::istream& istr);
00066 
00067    // ...otherwise, if you don't know, call this & visit it
00068    static void Read(UnknownElement& elementRoot, std::istream& istr);
00069 
00070 private:
00071    struct Token
00072    {
00073       enum Type
00074       {
00075          TOKEN_OBJECT_BEGIN,  //    {
00076          TOKEN_OBJECT_END,    //    }
00077          TOKEN_ARRAY_BEGIN,   //    [
00078          TOKEN_ARRAY_END,     //    ]
00079          TOKEN_NEXT_ELEMENT,  //    ,
00080          TOKEN_MEMBER_ASSIGN, //    :
00081          TOKEN_STRING,        //    "xxx"
00082          TOKEN_NUMBER,        //    [+/-]000.000[e[+/-]000]
00083          TOKEN_BOOLEAN,       //    true -or- false
00084          TOKEN_NULL           //    null
00085       };
00086 
00087       Type nType;
00088       std::string sValue;
00089 
00090       // for malformed file debugging
00091       Reader::Location locBegin;
00092       Reader::Location locEnd;
00093    };
00094 
00095    class InputStream;
00096    class TokenStream;
00097    typedef std::vector<Token> Tokens;
00098 
00099    template <typename ElementTypeT>   
00100    static void Read_i(ElementTypeT& element, std::istream& istr);
00101 
00102    // scanning istream into token sequence
00103    void Scan(Tokens& tokens, InputStream& inputStream);
00104 
00105    void EatWhiteSpace(InputStream& inputStream);
00106    void MatchString(std::string& sValue, InputStream& inputStream);
00107    void MatchNumber(std::string& sNumber, InputStream& inputStream);
00108    void MatchExpectedString(const std::string& sExpected, InputStream& inputStream);
00109 
00110    // parsing token sequence into element structure
00111    void Parse(UnknownElement& element, TokenStream& tokenStream);
00112    void Parse(Object& object, TokenStream& tokenStream);
00113    void Parse(Array& array, TokenStream& tokenStream);
00114    void Parse(String& string, TokenStream& tokenStream);
00115    void Parse(Number& number, TokenStream& tokenStream);
00116    void Parse(Boolean& boolean, TokenStream& tokenStream);
00117    void Parse(Null& null, TokenStream& tokenStream);
00118 
00119    const std::string& MatchExpectedToken(Token::Type nExpected, TokenStream& tokenStream);
00120 };
00121 
00122 
00123 } // json
00124 } // bus
00125 } // probe
00126 } // wns
00127 
00128 
00129 #include <WNS/probe/bus/json/readerInl.hpp>
00130 
00131 #endif // WNS_JSON_READER_HPP

Generated on Sun May 27 03:31:50 2012 for openWNS by  doxygen 1.5.5