LCOV - app.info - js/src/jsonparser.h

LCOV - code coverage report
| Current view: | directory - js/src - jsonparser.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 28 | 28 | 100.0 %
| Date: | 2012-06-02 | Functions: | 7 | 7 | 100.0 %

           1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
           2                 :  * vim: set ts=8 sw=4 et tw=99:
           3                 :  *
           4                 :  * ***** BEGIN LICENSE BLOCK *****
           5                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           6                 :  *
           7                 :  * The contents of this file are subject to the Mozilla Public License Version
           8                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           9                 :  * the License. You may obtain a copy of the License at
          10                 :  * http://www.mozilla.org/MPL/
          11                 :  *
          12                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          13                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          14                 :  * for the specific language governing rights and limitations under the
          15                 :  * License.
          16                 :  *
          17                 :  * The Original Code is SpiderMonkey JSON.
          18                 :  *
          19                 :  * The Initial Developer of the Original Code is
          20                 :  * the Mozilla Foundation.
          21                 :  * Portions created by the Initial Developer are Copyright (C) 2011
          22                 :  * the Initial Developer. All Rights Reserved.
          23                 :  *
          24                 :  * Contributor(s):
          25                 :  *   Jeff Walden <jwalden+code@mit.edu> (original author)
          26                 :  *
          27                 :  * Alternatively, the contents of this file may be used under the terms of
          28                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          29                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          31                 :  * of those above. If you wish to allow use of your version of this file only
          32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          33                 :  * use your version of this file under the terms of the MPL, indicate your
          34                 :  * decision by deleting the provisions above and replace them with the notice
          35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          36                 :  * the provisions above, a recipient may use your version of this file under
          37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          38                 :  *
          39                 :  * ***** END LICENSE BLOCK ***** */
          40                 :
          41                 : #ifndef jsonparser_h___
          42                 : #define jsonparser_h___
          43                 :
          44                 : #include "mozilla/Attributes.h"
          45                 : #include "mozilla/RangedPtr.h"
          46                 :
          47                 : #include "jscntxt.h"
          48                 : #include "jsstr.h"
          49                 :
          50                 : /*
          51                 :  * NB: This class must only be used on the stack as it contains a js::Value.
          52                 :  */
          53                 : class JSONParser
          54                 : {
          55                 :     JSONParser(const JSONParser &other) MOZ_DELETE;
          56                 :     void operator=(const JSONParser &other) MOZ_DELETE;
          57                 :
          58                 :   public:
          59                 :     enum ErrorHandling { RaiseError, NoError };
          60                 :     enum ParsingMode { StrictJSON, LegacyJSON };
          61                 :
          62                 :   private:
          63                 :     /* Data members */
          64                 :
          65                 :     JSContext * const cx;
          66                 :     mozilla::RangedPtr<const jschar> current;
          67                 :     const mozilla::RangedPtr<const jschar> end;
          68                 :
          69                 :     js::Value v;
          70                 :
          71                 :     const ParsingMode parsingMode;
          72                 :     const ErrorHandling errorHandling;
          73                 :
          74                 :     enum Token { String, Number, True, False, Null,
          75                 :                  ArrayOpen, ArrayClose,
          76                 :                  ObjectOpen, ObjectClose,
          77                 :                  Colon, Comma,
          78                 :                  OOM, Error };
          79                 : #ifdef DEBUG
          80                 :     Token lastToken;
          81                 : #endif
          82                 :
          83                 :   public:
          84                 :     /* Public API */
          85                 :
          86                 :     /*
          87                 :      * Create a parser for the provided JSON data.  The parser will accept
          88                 :      * certain legacy, non-JSON syntax if decodingMode is LegacyJSON.
          89                 :      * Description of this syntax is deliberately omitted: new code should only
          90                 :      * use strict JSON parsing.
          91                 :      */
          92           13506 :     JSONParser(JSContext *cx, const jschar *data, size_t length,
          93                 :                ParsingMode parsingMode = StrictJSON,
          94                 :                ErrorHandling errorHandling = RaiseError)
          95                 :       : cx(cx),
          96                 :         current(data, length),
          97                 :         end(data + length, data, length),
          98                 :         parsingMode(parsingMode),
          99                 :         errorHandling(errorHandling)
         100                 : #ifdef DEBUG
         101           13506 :       , lastToken(Error)
         102                 : #endif
         103                 :     {
         104           13506 :         JS_ASSERT(current <= end);
         105           13506 :     }
         106                 :
         107                 :     /*
         108                 :      * Parse the JSON data specified at construction time.  If it parses
         109                 :      * successfully, store the prescribed value in *vp and return true.  If an
         110                 :      * internal error (e.g. OOM) occurs during parsing, return false.
         111                 :      * Otherwise, if invalid input was specifed but no internal error occurred,
         112                 :      * behavior depends upon the error handling specified at construction: if
         113                 :      * error handling is RaiseError then throw a SyntaxError and return false,
         114                 :      * otherwise return true and set *vp to |undefined|.  (JSON syntax can't
         115                 :      * represent |undefined|, so the JSON data couldn't have specified it.)
         116                 :      */
         117                 :     bool parse(js::Value *vp);
         118                 :
         119                 :   private:
         120           63617 :     js::Value numberValue() const {
         121           63617 :         JS_ASSERT(lastToken == Number);
         122           63617 :         JS_ASSERT(v.isNumber());
         123           63617 :         return v;
         124                 :     }
         125                 :
         126          350880 :     js::Value stringValue() const {
         127          350880 :         JS_ASSERT(lastToken == String);
         128          350880 :         JS_ASSERT(v.isString());
         129          350880 :         return v;
         130                 :     }
         131                 :
         132          211445 :     js::Value atomValue() const {
         133          211445 :         js::Value strval = stringValue();
         134          211445 :         JS_ASSERT(strval.toString()->isAtom());
         135                 :         return strval;
         136                 :     }
         137                 :
         138          540335 :     Token token(Token t) {
         139          540335 :         JS_ASSERT(t != String);
         140          540335 :         JS_ASSERT(t != Number);
         141                 : #ifdef DEBUG
         142          540335 :         lastToken = t;
         143                 : #endif
         144          540335 :         return t;
         145                 :     }
         146                 :
         147          350880 :     Token stringToken(JSString *str) {
         148          350880 :         this->v = js::StringValue(str);
         149                 : #ifdef DEBUG
         150          350880 :         lastToken = String;
         151                 : #endif
         152          350880 :         return String;
         153                 :     }
         154                 :
         155           63617 :     Token numberToken(double d) {
         156           63617 :         this->v = js::NumberValue(d);
         157                 : #ifdef DEBUG
         158           63617 :         lastToken = Number;
         159                 : #endif
         160           63617 :         return Number;
         161                 :     }
         162                 :
         163                 :     enum StringType { PropertyName, LiteralValue };
         164                 :     template<StringType ST> Token readString();
         165                 :
         166                 :     Token readNumber();
         167                 :
         168                 :     Token advance();
         169                 :     Token advancePropertyName();
         170                 :     Token advancePropertyColon();
         171                 :     Token advanceAfterProperty();
         172                 :     Token advanceAfterObjectOpen();
         173                 :     Token advanceAfterArrayElement();
         174                 :
         175                 :     void error(const char *msg);
         176                 :     bool errorReturn();
         177                 : };
         178                 :
         179                 : #endif /* jsonparser_h___ */

Generated by: LCOV version 1.7