| 1 |
/* |
| 2 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
| 3 |
* Rights to this code are as documented in doc/pod/license.pod. |
| 4 |
* |
| 5 |
* JSON-RPC for Ermyth |
| 6 |
* |
| 7 |
* $Id: reader.h,v 1.2 2007-08-28 17:08:12 pippijn Exp $ |
| 8 |
*/ |
| 9 |
|
| 10 |
//>>>>>>>>>>> reader.h <<<<<<<<<<<// |
| 11 |
#ifndef READER_H |
| 12 |
#define READER_H |
| 13 |
|
| 14 |
#include <deque> |
| 15 |
#include <stack> |
| 16 |
#include <string> |
| 17 |
#include <iostream> |
| 18 |
|
| 19 |
#include "json/value.h" |
| 20 |
|
| 21 |
namespace json |
| 22 |
{ |
| 23 |
class Value; |
| 24 |
|
| 25 |
class Reader |
| 26 |
{ |
| 27 |
public: |
| 28 |
typedef char const *Location; |
| 29 |
|
| 30 |
Reader (); |
| 31 |
|
| 32 |
bool parse (const std::string &document, Value &root, bool collectComments = true); |
| 33 |
bool parse (char const *beginDoc, char const *endDOc, Value &root, bool collectComments = true); |
| 34 |
bool parse (std::istream &, Value &root, bool collectComments = true); |
| 35 |
|
| 36 |
std::string error_msgs () const; |
| 37 |
|
| 38 |
private: |
| 39 |
enum TokenType |
| 40 |
{ |
| 41 |
tokenEndOfStream = 0, |
| 42 |
tokenObjectBegin, |
| 43 |
tokenObjectEnd, |
| 44 |
tokenArrayBegin, |
| 45 |
tokenArrayEnd, |
| 46 |
tokenString, |
| 47 |
tokenNumber, |
| 48 |
tokenTrue, |
| 49 |
tokenFalse, |
| 50 |
tokenNull, |
| 51 |
tokenArraySeparator, |
| 52 |
tokenMemberSeparator, |
| 53 |
tokenComment, |
| 54 |
tokenError |
| 55 |
}; |
| 56 |
|
| 57 |
class Token |
| 58 |
{ |
| 59 |
public: |
| 60 |
TokenType type_; |
| 61 |
Location start_; |
| 62 |
Location end_; |
| 63 |
}; |
| 64 |
|
| 65 |
class ErrorInfo |
| 66 |
{ |
| 67 |
public: |
| 68 |
Token token_; |
| 69 |
std::string message_; |
| 70 |
Location extra_; |
| 71 |
}; |
| 72 |
|
| 73 |
typedef std::deque<ErrorInfo> Errors; |
| 74 |
|
| 75 |
bool expectToken (TokenType type, Token &token, char const *message); |
| 76 |
bool readToken (Token &token); |
| 77 |
void skipSpaces (); |
| 78 |
bool match (Location pattern, |
| 79 |
int patternLength); |
| 80 |
bool readComment (); |
| 81 |
bool readCStyleComment (); |
| 82 |
bool readCppStyleComment (); |
| 83 |
bool readString (); |
| 84 |
void readNumber (); |
| 85 |
bool readValue (); |
| 86 |
bool readObject (); |
| 87 |
bool readArray (); |
| 88 |
bool decodeNumber (Token &token); |
| 89 |
bool decodeString (Token &token); |
| 90 |
bool decodeString (Token &token, std::string &decoded); |
| 91 |
bool decodeDouble (Token &token); |
| 92 |
bool decodeUnicodeEscapeSequence (Token &token, |
| 93 |
Location ¤t, |
| 94 |
Location end, |
| 95 |
unsigned &unicode); |
| 96 |
bool addError (const std::string &message, |
| 97 |
Token &token, |
| 98 |
Location extra = 0); |
| 99 |
bool recoverFromError (TokenType skipUntilToken); |
| 100 |
bool addErrorAndRecover (const std::string &message, |
| 101 |
Token &token, |
| 102 |
TokenType skipUntilToken); |
| 103 |
void skipUntilSpace (); |
| 104 |
Value ¤tValue (); |
| 105 |
char getNextChar (); |
| 106 |
void getLocationLineAndColumn (Location location, |
| 107 |
int &line, |
| 108 |
int &column) const; |
| 109 |
std::string getLocationLineAndColumn (Location location) const; |
| 110 |
void addComment (Location begin, |
| 111 |
Location end, |
| 112 |
CommentPlacement placement); |
| 113 |
void skipCommentTokens (Token &token); |
| 114 |
|
| 115 |
typedef std::stack<Value *> Nodes; |
| 116 |
Nodes nodes_; |
| 117 |
Errors errors_; |
| 118 |
std::string document_; |
| 119 |
Location begin_; |
| 120 |
Location end_; |
| 121 |
Location current_; |
| 122 |
Location lastValueEnd_; |
| 123 |
Value *lastValue_; |
| 124 |
std::string commentsBefore_; |
| 125 |
bool collectComments_; |
| 126 |
}; |
| 127 |
|
| 128 |
std::istream& operator >> (std::istream&, Value&); |
| 129 |
|
| 130 |
} // namespace json |
| 131 |
|
| 132 |
#endif // READER_H |