/* * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in doc/pod/license.pod. * * JSON-RPC for Ermyth * * $Id: reader.h,v 1.3 2007/09/05 11:23:15 pippijn Exp $ */ //>>>>>>>>>>> reader.h <<<<<<<<<< #include #include #include #include "json/value.h" namespace json { class Value; class Reader { public: typedef char const *Location; Reader (); bool parse (const std::string &document, Value &root, bool collectComments = true); bool parse (char const *beginDoc, char const *endDOc, Value &root, bool collectComments = true); bool parse (std::istream &, Value &root, bool collectComments = true); std::string error_msgs () const; private: enum TokenType { tokenEndOfStream = 0, tokenObjectBegin, tokenObjectEnd, tokenArrayBegin, tokenArrayEnd, tokenString, tokenNumber, tokenTrue, tokenFalse, tokenNull, tokenArraySeparator, tokenMemberSeparator, tokenComment, tokenError }; class Token { public: TokenType type_; Location start_; Location end_; }; class ErrorInfo { public: Token token_; std::string message_; Location extra_; }; typedef std::deque Errors; bool expectToken (TokenType type, Token &token, char const *message); bool readToken (Token &token); void skipSpaces (); bool match (Location pattern, int patternLength); bool readComment (); bool readCStyleComment (); bool readCppStyleComment (); bool readString (); void readNumber (); bool readValue (); bool readObject (); bool readArray (); bool decodeNumber (Token &token); bool decodeString (Token &token); bool decodeString (Token &token, std::string &decoded); bool decodeDouble (Token &token); bool decodeUnicodeEscapeSequence (Token &token, Location ¤t, Location end, unsigned &unicode); bool addError (const std::string &message, Token &token, Location extra = 0); bool recoverFromError (TokenType skipUntilToken); bool addErrorAndRecover (const std::string &message, Token &token, TokenType skipUntilToken); void skipUntilSpace (); Value ¤tValue (); char getNextChar (); void getLocationLineAndColumn (Location location, int &line, int &column) const; std::string getLocationLineAndColumn (Location location) const; void addComment (Location begin, Location end, CommentPlacement placement); void skipCommentTokens (Token &token); typedef std::stack Nodes; Nodes nodes_; Errors errors_; std::string document_; Location begin_; Location end_; Location current_; Location lastValueEnd_; Value *lastValue_; std::string commentsBefore_; bool collectComments_; }; std::istream& operator >> (std::istream&, Value&); } // namespace json #endif // READER_H