Index: runtime/vm/json.h |
=================================================================== |
--- runtime/vm/json.h (revision 0) |
+++ runtime/vm/json.h (revision 0) |
@@ -0,0 +1,118 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+#ifndef VM_JSON_H_ |
+#define VM_JSON_H_ |
+ |
+#include "vm/allocation.h" |
+#include "vm/globals.h" |
+ |
+namespace dart { |
+ |
+ |
+// A low level interface to tokenize JSON strings. |
turnidge
2012/04/09 22:26:42
Will anybody ever use this directly, or will they
hausner
2012/04/10 16:26:51
The JSONReader declarations in this file need it.
|
+class JSONScanner : ValueObject { |
+ public: |
+ enum Token { |
+ TokenIllegal = 0, |
+ TokenLBrace, |
+ TokenRBrace, |
+ TokenLBrack, |
+ TokenRBrack, |
+ TokenColon, |
+ TokenComma, |
+ TokenString, |
+ TokenInteger, |
+ TokenTrue, |
+ TokenFalse, |
+ TokenNull, |
+ TokenEOM |
+ }; |
+ explicit JSONScanner(const char* msg); |
turnidge
2012/04/09 22:26:42
For consistency, consider renaming msg -> json_obj
hausner
2012/04/10 16:26:51
Done.
|
+ |
+ void Set(const char* str); |
turnidge
2012/04/09 22:26:42
str -> json_object
hausner
2012/04/10 16:26:51
Done.
|
+ void Scan(); |
+ Token CurrentToken() const { return token_; } |
+ bool EOM() const { return token_ == TokenEOM; } |
+ const char* TokenChars() const { return token_start_; } |
+ int TokenLen() const { return token_length_; } |
+ bool IsStringLiteral(const char* literal) const; |
+ void Skip(Token matching_token); |
+ |
+ private: |
+ bool IsLetter(char ch) const; |
+ bool IsDigit(char ch) const; |
+ bool IsLiteral(const char* literal); |
+ void ScanNumber(); |
+ void ScanString(); |
+ void Recognize(Token t); |
+ |
+ const char* current_pos_; |
+ const char* token_start_; |
+ int token_length_; |
+ Token token_; |
+ const char* string_; |
turnidge
2012/04/09 22:26:42
string_ => json_object_
hausner
2012/04/10 16:26:51
Actuall, string_ wasn't even used anywhere. Remove
|
+}; |
+ |
+ |
+// JSONReader is a higher level interface that allows for lookup of |
+// name-value pairs in JSON objects. |
turnidge
2012/04/09 22:26:42
JSONReader reparses the whole json text on every c
hausner
2012/04/10 16:26:51
As we discussed in person, this is a low-level int
|
+class JSONReader : ValueObject { |
+ public: |
+ enum JSONType { |
+ kString, |
+ kInteger, |
+ kObject, |
+ kArray, |
+ kLiteral, |
+ kNone |
+ }; |
+ |
+ explicit JSONReader(const char* json_object); |
+ void Set(const char* json_object); |
turnidge
2012/04/09 22:26:42
Set is a pretty generic name. Consider something
hausner
2012/04/10 16:26:51
Done.
|
+ |
+ // Returns true if the a pair with the given name was found. |
turnidge
2012/04/09 22:26:42
typo: "the a"
hausner
2012/04/10 16:26:51
Done.
|
+ bool Seek(const char* name); |
+ |
+ // Returns true if a syntax error was found. |
+ bool Error() const { return error_; } |
+ |
+ JSONType Type() const; |
+ const char* ValueChars() const { |
+ return (Type() != kNone) ? scanner_.TokenChars() : NULL; |
+ } |
+ int ValueLen() const { |
+ return (Type() != kNone) ? scanner_.TokenLen() : 0; |
+ } |
+ bool IsStringLiteral(const char* literal) const { |
+ return scanner_.IsStringLiteral(literal); |
+ } |
+ |
+ private: |
+ JSONScanner scanner_; |
+ const char* json_object_; |
+ bool error_; |
+}; |
+ |
+ |
turnidge
2012/04/09 22:26:42
Add a class-level comment here?
hausner
2012/04/10 16:26:51
Done.
|
+class JSONWriter : ValueObject { |
turnidge
2012/04/09 22:26:42
It's confusing to call this a JSONWriter since it
hausner
2012/04/10 16:26:51
I agree, this has nothing to do with JSON at this
|
+ public: |
+ explicit JSONWriter(intptr_t buf_size); |
+ ~JSONWriter(); |
+ |
+ intptr_t Printf(const char* format, ...); |
+ void Clear(); |
+ |
+ char* buf() { return buf_; } |
+ |
+ private: |
+ void GrowBuffer(intptr_t len); |
+ char* buf_; |
+ intptr_t buf_size_; |
+ intptr_t msg_len_; |
+}; |
+ |
+} // namespace dart |
+ |
+#endif // VM_JSON_H_ |