js/src/jsonparser.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef jsonparser_h
michael@0 8 #define jsonparser_h
michael@0 9
michael@0 10 #include "mozilla/Attributes.h"
michael@0 11
michael@0 12 #include "ds/IdValuePair.h"
michael@0 13 #include "vm/String.h"
michael@0 14
michael@0 15 namespace js {
michael@0 16
michael@0 17 class MOZ_STACK_CLASS JSONParser : private AutoGCRooter
michael@0 18 {
michael@0 19 public:
michael@0 20 enum ErrorHandling { RaiseError, NoError };
michael@0 21
michael@0 22 private:
michael@0 23 /* Data members */
michael@0 24
michael@0 25 JSContext * const cx;
michael@0 26 JS::ConstTwoByteChars current;
michael@0 27 const JS::ConstTwoByteChars begin, end;
michael@0 28
michael@0 29 Value v;
michael@0 30
michael@0 31 const ErrorHandling errorHandling;
michael@0 32
michael@0 33 enum Token { String, Number, True, False, Null,
michael@0 34 ArrayOpen, ArrayClose,
michael@0 35 ObjectOpen, ObjectClose,
michael@0 36 Colon, Comma,
michael@0 37 OOM, Error };
michael@0 38
michael@0 39 // State related to the parser's current position. At all points in the
michael@0 40 // parse this keeps track of the stack of arrays and objects which have
michael@0 41 // been started but not finished yet. The actual JS object is not
michael@0 42 // allocated until the literal is closed, so that the result can be sized
michael@0 43 // according to its contents and have its type and shape filled in using
michael@0 44 // caches.
michael@0 45
michael@0 46 // State for an array that is currently being parsed. This includes all
michael@0 47 // elements that have been seen so far.
michael@0 48 typedef Vector<Value, 20> ElementVector;
michael@0 49
michael@0 50 // State for an object that is currently being parsed. This includes all
michael@0 51 // the key/value pairs that have been seen so far.
michael@0 52 typedef Vector<IdValuePair, 10> PropertyVector;
michael@0 53
michael@0 54 // Possible states the parser can be in between values.
michael@0 55 enum ParserState {
michael@0 56 // An array element has just being parsed.
michael@0 57 FinishArrayElement,
michael@0 58
michael@0 59 // An object property has just been parsed.
michael@0 60 FinishObjectMember,
michael@0 61
michael@0 62 // At the start of the parse, before any values have been processed.
michael@0 63 JSONValue
michael@0 64 };
michael@0 65
michael@0 66 // Stack element for an in progress array or object.
michael@0 67 struct StackEntry {
michael@0 68 ElementVector &elements() {
michael@0 69 JS_ASSERT(state == FinishArrayElement);
michael@0 70 return * static_cast<ElementVector *>(vector);
michael@0 71 }
michael@0 72
michael@0 73 PropertyVector &properties() {
michael@0 74 JS_ASSERT(state == FinishObjectMember);
michael@0 75 return * static_cast<PropertyVector *>(vector);
michael@0 76 }
michael@0 77
michael@0 78 StackEntry(ElementVector *elements)
michael@0 79 : state(FinishArrayElement), vector(elements)
michael@0 80 {}
michael@0 81
michael@0 82 StackEntry(PropertyVector *properties)
michael@0 83 : state(FinishObjectMember), vector(properties)
michael@0 84 {}
michael@0 85
michael@0 86 ParserState state;
michael@0 87
michael@0 88 private:
michael@0 89 void *vector;
michael@0 90 };
michael@0 91
michael@0 92 // All in progress arrays and objects being parsed, in order from outermost
michael@0 93 // to innermost.
michael@0 94 Vector<StackEntry, 10> stack;
michael@0 95
michael@0 96 // Unused element and property vectors for previous in progress arrays and
michael@0 97 // objects. These vectors are not freed until the end of the parse to avoid
michael@0 98 // unnecessary freeing and allocation.
michael@0 99 Vector<ElementVector*, 5> freeElements;
michael@0 100 Vector<PropertyVector*, 5> freeProperties;
michael@0 101
michael@0 102 #ifdef DEBUG
michael@0 103 Token lastToken;
michael@0 104 #endif
michael@0 105
michael@0 106 public:
michael@0 107 /* Public API */
michael@0 108
michael@0 109 /* Create a parser for the provided JSON data. */
michael@0 110 JSONParser(JSContext *cx, JS::ConstTwoByteChars data, size_t length,
michael@0 111 ErrorHandling errorHandling = RaiseError)
michael@0 112 : AutoGCRooter(cx, JSONPARSER),
michael@0 113 cx(cx),
michael@0 114 current(data),
michael@0 115 begin(data),
michael@0 116 end((data + length).get(), data.get(), length),
michael@0 117 errorHandling(errorHandling),
michael@0 118 stack(cx),
michael@0 119 freeElements(cx),
michael@0 120 freeProperties(cx)
michael@0 121 #ifdef DEBUG
michael@0 122 , lastToken(Error)
michael@0 123 #endif
michael@0 124 {
michael@0 125 JS_ASSERT(current <= end);
michael@0 126 }
michael@0 127
michael@0 128 ~JSONParser();
michael@0 129
michael@0 130 /*
michael@0 131 * Parse the JSON data specified at construction time. If it parses
michael@0 132 * successfully, store the prescribed value in *vp and return true. If an
michael@0 133 * internal error (e.g. OOM) occurs during parsing, return false.
michael@0 134 * Otherwise, if invalid input was specifed but no internal error occurred,
michael@0 135 * behavior depends upon the error handling specified at construction: if
michael@0 136 * error handling is RaiseError then throw a SyntaxError and return false,
michael@0 137 * otherwise return true and set *vp to |undefined|. (JSON syntax can't
michael@0 138 * represent |undefined|, so the JSON data couldn't have specified it.)
michael@0 139 */
michael@0 140 bool parse(MutableHandleValue vp);
michael@0 141
michael@0 142 private:
michael@0 143 Value numberValue() const {
michael@0 144 JS_ASSERT(lastToken == Number);
michael@0 145 JS_ASSERT(v.isNumber());
michael@0 146 return v;
michael@0 147 }
michael@0 148
michael@0 149 Value stringValue() const {
michael@0 150 JS_ASSERT(lastToken == String);
michael@0 151 JS_ASSERT(v.isString());
michael@0 152 return v;
michael@0 153 }
michael@0 154
michael@0 155 JSAtom *atomValue() const {
michael@0 156 Value strval = stringValue();
michael@0 157 return &strval.toString()->asAtom();
michael@0 158 }
michael@0 159
michael@0 160 Token token(Token t) {
michael@0 161 JS_ASSERT(t != String);
michael@0 162 JS_ASSERT(t != Number);
michael@0 163 #ifdef DEBUG
michael@0 164 lastToken = t;
michael@0 165 #endif
michael@0 166 return t;
michael@0 167 }
michael@0 168
michael@0 169 Token stringToken(JSString *str) {
michael@0 170 this->v = StringValue(str);
michael@0 171 #ifdef DEBUG
michael@0 172 lastToken = String;
michael@0 173 #endif
michael@0 174 return String;
michael@0 175 }
michael@0 176
michael@0 177 Token numberToken(double d) {
michael@0 178 this->v = NumberValue(d);
michael@0 179 #ifdef DEBUG
michael@0 180 lastToken = Number;
michael@0 181 #endif
michael@0 182 return Number;
michael@0 183 }
michael@0 184
michael@0 185 enum StringType { PropertyName, LiteralValue };
michael@0 186 template<StringType ST> Token readString();
michael@0 187
michael@0 188 Token readNumber();
michael@0 189
michael@0 190 Token advance();
michael@0 191 Token advancePropertyName();
michael@0 192 Token advancePropertyColon();
michael@0 193 Token advanceAfterProperty();
michael@0 194 Token advanceAfterObjectOpen();
michael@0 195 Token advanceAfterArrayElement();
michael@0 196
michael@0 197 void error(const char *msg);
michael@0 198 bool errorReturn();
michael@0 199
michael@0 200 JSObject *createFinishedObject(PropertyVector &properties);
michael@0 201 bool finishObject(MutableHandleValue vp, PropertyVector &properties);
michael@0 202 bool finishArray(MutableHandleValue vp, ElementVector &elements);
michael@0 203
michael@0 204 void getTextPosition(uint32_t *column, uint32_t *line);
michael@0 205
michael@0 206 friend void AutoGCRooter::trace(JSTracer *trc);
michael@0 207 void trace(JSTracer *trc);
michael@0 208
michael@0 209 private:
michael@0 210 JSONParser(const JSONParser &other) MOZ_DELETE;
michael@0 211 void operator=(const JSONParser &other) MOZ_DELETE;
michael@0 212 };
michael@0 213
michael@0 214 } /* namespace js */
michael@0 215
michael@0 216 #endif /* jsonparser_h */

mercurial