Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(701)

Unified Diff: src/parser.cc

Issue 7039037: Create stand-alone json parser (including scanner). (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/parser.h ('k') | src/runtime.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parser.cc
===================================================================
--- src/parser.cc (revision 7898)
+++ src/parser.cc (working copy)
@@ -4014,201 +4014,7 @@
scanner().location().beg_pos);
}
-// ----------------------------------------------------------------------------
-// JSON
-Handle<Object> JsonParser::ParseJson(Handle<String> script,
- UC16CharacterStream* source) {
- scanner_.Initialize(source);
- stack_overflow_ = false;
- Handle<Object> result = ParseJsonValue();
- if (result.is_null() || scanner_.Next() != Token::EOS) {
- if (stack_overflow_) {
- // Scanner failed.
- isolate()->StackOverflow();
- } else {
- // Parse failed. Scanner's current token is the unexpected token.
- Token::Value token = scanner_.current_token();
-
- const char* message;
- const char* name_opt = NULL;
-
- switch (token) {
- case Token::EOS:
- message = "unexpected_eos";
- break;
- case Token::NUMBER:
- message = "unexpected_token_number";
- break;
- case Token::STRING:
- message = "unexpected_token_string";
- break;
- case Token::IDENTIFIER:
- case Token::FUTURE_RESERVED_WORD:
- message = "unexpected_token_identifier";
- break;
- default:
- message = "unexpected_token";
- name_opt = Token::String(token);
- ASSERT(name_opt != NULL);
- break;
- }
-
- Scanner::Location source_location = scanner_.location();
- Factory* factory = isolate()->factory();
- MessageLocation location(factory->NewScript(script),
- source_location.beg_pos,
- source_location.end_pos);
- Handle<JSArray> array;
- if (name_opt == NULL) {
- array = factory->NewJSArray(0);
- } else {
- Handle<String> name = factory->NewStringFromUtf8(CStrVector(name_opt));
- Handle<FixedArray> element = factory->NewFixedArray(1);
- element->set(0, *name);
- array = factory->NewJSArrayWithElements(element);
- }
- Handle<Object> result = factory->NewSyntaxError(message, array);
- isolate()->Throw(*result, &location);
- return Handle<Object>::null();
- }
- }
- return result;
-}
-
-
-Handle<String> JsonParser::GetString() {
- int literal_length = scanner_.literal_length();
- if (literal_length == 0) {
- return isolate()->factory()->empty_string();
- }
- if (scanner_.is_literal_ascii()) {
- return isolate()->factory()->NewStringFromAscii(
- scanner_.literal_ascii_string());
- } else {
- return isolate()->factory()->NewStringFromTwoByte(
- scanner_.literal_uc16_string());
- }
-}
-
-
-Handle<String> JsonParser::GetSymbol() {
- int literal_length = scanner_.literal_length();
- if (literal_length == 0) {
- return isolate()->factory()->empty_string();
- }
- if (scanner_.is_literal_ascii()) {
- return isolate()->factory()->LookupAsciiSymbol(
- scanner_.literal_ascii_string());
- } else {
- return isolate()->factory()->LookupTwoByteSymbol(
- scanner_.literal_uc16_string());
- }
-}
-
-
-// Parse any JSON value.
-Handle<Object> JsonParser::ParseJsonValue() {
- Token::Value token = scanner_.Next();
- switch (token) {
- case Token::STRING:
- return GetString();
- case Token::NUMBER:
- return isolate()->factory()->NewNumber(scanner_.number());
- case Token::FALSE_LITERAL:
- return isolate()->factory()->false_value();
- case Token::TRUE_LITERAL:
- return isolate()->factory()->true_value();
- case Token::NULL_LITERAL:
- return isolate()->factory()->null_value();
- case Token::LBRACE:
- return ParseJsonObject();
- case Token::LBRACK:
- return ParseJsonArray();
- default:
- return ReportUnexpectedToken();
- }
-}
-
-
-// Parse a JSON object. Scanner must be right after '{' token.
-Handle<Object> JsonParser::ParseJsonObject() {
- Handle<JSFunction> object_constructor(
- isolate()->global_context()->object_function());
- Handle<JSObject> json_object =
- isolate()->factory()->NewJSObject(object_constructor);
- if (scanner_.peek() == Token::RBRACE) {
- scanner_.Next();
- } else {
- if (StackLimitCheck(isolate()).HasOverflowed()) {
- stack_overflow_ = true;
- return Handle<Object>::null();
- }
- do {
- if (scanner_.Next() != Token::STRING) {
- return ReportUnexpectedToken();
- }
- Handle<String> key = GetSymbol();
- if (scanner_.Next() != Token::COLON) {
- return ReportUnexpectedToken();
- }
- Handle<Object> value = ParseJsonValue();
- if (value.is_null()) return Handle<Object>::null();
- uint32_t index;
- if (key->AsArrayIndex(&index)) {
- SetOwnElement(json_object, index, value, kNonStrictMode);
- } else if (key->Equals(isolate()->heap()->Proto_symbol())) {
- // We can't remove the __proto__ accessor since it's hardcoded
- // in several places. Instead go along and add the value as
- // the prototype of the created object if possible.
- SetPrototype(json_object, value);
- } else {
- SetLocalPropertyIgnoreAttributes(json_object, key, value, NONE);
- }
- } while (scanner_.Next() == Token::COMMA);
- if (scanner_.current_token() != Token::RBRACE) {
- return ReportUnexpectedToken();
- }
- }
- return json_object;
-}
-
-
-// Parse a JSON array. Scanner must be right after '[' token.
-Handle<Object> JsonParser::ParseJsonArray() {
- ZoneScope zone_scope(DELETE_ON_EXIT);
- ZoneList<Handle<Object> > elements(4);
-
- Token::Value token = scanner_.peek();
- if (token == Token::RBRACK) {
- scanner_.Next();
- } else {
- if (StackLimitCheck(isolate()).HasOverflowed()) {
- stack_overflow_ = true;
- return Handle<Object>::null();
- }
- do {
- Handle<Object> element = ParseJsonValue();
- if (element.is_null()) return Handle<Object>::null();
- elements.Add(element);
- token = scanner_.Next();
- } while (token == Token::COMMA);
- if (token != Token::RBRACK) {
- return ReportUnexpectedToken();
- }
- }
-
- // Allocate a fixed array with all the elements.
- Handle<FixedArray> fast_elements =
- isolate()->factory()->NewFixedArray(elements.length());
-
- for (int i = 0, n = elements.length(); i < n; i++) {
- fast_elements->set(i, *elements[i]);
- }
-
- return isolate()->factory()->NewJSArrayWithElements(fast_elements);
-}
-
// ----------------------------------------------------------------------------
// Regular expressions
« no previous file with comments | « src/parser.h ('k') | src/runtime.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698