File indexing completed on 2024-05-12 15:43:24
0001 /* 0002 * This file is part of the KDE libraries 0003 * Copyright (C) 2012 Bernd Buschinski (b.buschinski@googlemail.com) 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 * 0020 */ 0021 0022 #ifndef JSONLEXER_H 0023 #define JSONLEXER_H 0024 0025 #include "ustring.h" 0026 0027 namespace KJS 0028 { 0029 0030 class ExecState; 0031 class JSValue; 0032 class JSObject; 0033 class JSONParser; 0034 0035 namespace JSONParserState 0036 { 0037 enum ParserState { 0038 JSONValue = 1, 0039 JSONObject, 0040 JSONArray 0041 }; 0042 0043 enum TokenType { 0044 TokLBracket, // [ 0045 TokRBracket, // ] 0046 TokLBrace, // { 0047 TokRBrace, // } 0048 TokString, 0049 TokIdentifier, 0050 TokNumber, 0051 TokColon, 0052 TokLParen, 0053 TokRParen, 0054 TokComma, 0055 TokTrue, 0056 TokFalse, 0057 TokNull, 0058 TokEnd, 0059 TokError 0060 }; 0061 } 0062 0063 class JSONLexer 0064 { 0065 public: 0066 explicit JSONLexer(const UString &code); 0067 0068 JSONParserState::TokenType next(); 0069 JSONParserState::TokenType current(); 0070 double currentNumber() const; 0071 UString currentString() const; 0072 0073 private: 0074 inline JSONParserState::TokenType lexString(); 0075 inline JSONParserState::TokenType lexNumber(); 0076 UChar parseEscapeChar(bool *error); 0077 0078 UString m_code; 0079 int m_pos; 0080 0081 //Token Data 0082 JSONParserState::TokenType m_type; 0083 UString m_stringToken; 0084 double m_numberToken; 0085 }; 0086 0087 class JSONParser 0088 { 0089 public: 0090 explicit JSONParser(const UString &code); 0091 0092 // Returns the root parsed JSValue* 0093 // or NULL on failure 0094 JSValue *tryParse(ExecState *exec); 0095 0096 private: 0097 JSValue *parse(ExecState *exec, JSONParserState::ParserState state = JSONParserState::JSONValue); 0098 inline bool nextParseIsEOF(); 0099 0100 JSONParserState::ParserState m_state; 0101 JSONLexer m_lexer; 0102 }; 0103 0104 } // namespace KJS 0105 0106 #endif // JSONLEXER_H