00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef TRIGBUNCHCROSSINGTOOL_JSON_READER_H
00011 #define TRIGBUNCHCROSSINGTOOL_JSON_READER_H
00012
00013
00014 #include <iostream>
00015 #include <vector>
00016
00017
00018 #include "elements.h"
00019
00020 namespace json {
00021
00022 class Reader {
00023
00024 public:
00025
00026 struct Location {
00027 Location();
00028
00029 unsigned int m_nLine;
00030 unsigned int m_nLineOffset;
00031 unsigned int m_nDocOffset;
00032 };
00033
00034
00035
00036 class ScanException : public Exception {
00037 public:
00038 ScanException( const std::string& sMessage,
00039 const Reader::Location& locError ) :
00040 Exception(sMessage),
00041 m_locError(locError) {}
00042
00043 Reader::Location m_locError;
00044 };
00045
00046
00047
00048 class ParseException : public Exception {
00049
00050 public:
00051 ParseException( const std::string& sMessage,
00052 const Reader::Location& locTokenBegin,
00053 const Reader::Location& locTokenEnd ) :
00054 Exception(sMessage),
00055 m_locTokenBegin(locTokenBegin),
00056 m_locTokenEnd(locTokenEnd) {}
00057
00058 Reader::Location m_locTokenBegin;
00059 Reader::Location m_locTokenEnd;
00060 };
00061
00062
00063 static void Read( Object& object, std::istream& istr );
00064 static void Read( Array& array, std::istream& istr );
00065 static void Read( String& string, std::istream& istr );
00066 static void Read( Number& number, std::istream& istr );
00067 static void Read( Boolean& boolean, std::istream& istr );
00068 static void Read( Null& null, std::istream& istr );
00069
00070
00071 static void Read( UnknownElement& elementRoot, std::istream& istr );
00072
00073 private:
00074 struct Token {
00075 enum Type {
00076 TOKEN_OBJECT_BEGIN,
00077 TOKEN_OBJECT_END,
00078 TOKEN_ARRAY_BEGIN,
00079 TOKEN_ARRAY_END,
00080 TOKEN_NEXT_ELEMENT,
00081 TOKEN_MEMBER_ASSIGN,
00082 TOKEN_STRING,
00083 TOKEN_NUMBER,
00084 TOKEN_BOOLEAN,
00085 TOKEN_NULL
00086 };
00087
00088 Type nType;
00089 std::string sValue;
00090
00091
00092 Reader::Location locBegin;
00093 Reader::Location locEnd;
00094 };
00095
00096 class InputStream;
00097 class TokenStream;
00098 typedef std::vector<Token> Tokens;
00099
00100 template <typename ElementTypeT>
00101 static void Read_i( ElementTypeT& element, std::istream& istr );
00102
00103
00104 void Scan( Tokens& tokens, InputStream& inputStream );
00105
00106 void EatWhiteSpace( InputStream& inputStream );
00107 void MatchString( std::string& sValue, InputStream& inputStream );
00108 void MatchNumber( std::string& sNumber, InputStream& inputStream );
00109 void MatchExpectedString( const std::string& sExpected,
00110 InputStream& inputStream );
00111
00112
00113 void Parse( UnknownElement& element, TokenStream& tokenStream );
00114 void Parse( Object& object, TokenStream& tokenStream );
00115 void Parse( Array& array, TokenStream& tokenStream );
00116 void Parse( String& string, TokenStream& tokenStream );
00117 void Parse( Number& number, TokenStream& tokenStream );
00118 void Parse( Boolean& boolean, TokenStream& tokenStream );
00119 void Parse( Null& null, TokenStream& tokenStream );
00120
00121 const std::string& MatchExpectedToken( Token::Type nExpected,
00122 TokenStream& tokenStream );
00123 };
00124
00125 }
00126
00127 #ifndef __CINT__
00128
00129 #include "reader.inl"
00130 #endif // __CINT__
00131
00132 #endif // TRIGBUNCHCROSSINGTOOL_JSON_READER_H