1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2012 Bernd Buschinski (b.buschinski@googlemail.com)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef JSONLEXER_H
23#define JSONLEXER_H
24
25#include "ustring.h"
26
27
28namespace KJS {
29
30class ExecState;
31class JSValue;
32class JSObject;
33class JSONParser;
34
35namespace JSONParserState {
36 enum ParserState {
37 JSONValue = 1,
38 JSONObject,
39 JSONArray
40 };
41
42 enum TokenType {
43 TokLBracket, // [
44 TokRBracket, // ]
45 TokLBrace, // {
46 TokRBrace, // }
47 TokString,
48 TokIdentifier,
49 TokNumber,
50 TokColon,
51 TokLParen,
52 TokRParen,
53 TokComma,
54 TokTrue,
55 TokFalse,
56 TokNull,
57 TokEnd,
58 TokError };
59};
60
61
62class JSONLexer {
63public:
64 explicit JSONLexer(const UString& code);
65
66 JSONParserState::TokenType next();
67 JSONParserState::TokenType current();
68 double currentNumber() const;
69 UString currentString() const;
70
71private:
72 inline JSONParserState::TokenType lexString();
73 inline JSONParserState::TokenType lexNumber();
74 UChar parseEscapeChar(bool *error);
75
76 UString m_code;
77 int m_pos;
78
79 //Token Data
80 JSONParserState::TokenType m_type;
81 UString m_stringToken;
82 double m_numberToken;
83};
84
85
86class JSONParser {
87public:
88 explicit JSONParser(const UString& code);
89
90 // Returns the root parsed JSValue*
91 // or NULL on failure
92 JSValue* tryParse(ExecState* exec);
93
94private:
95 JSValue* parse(ExecState* exec, JSONParserState::ParserState state = JSONParserState::JSONValue);
96 inline bool nextParseIsEOF();
97
98 JSONParserState::ParserState m_state;
99 JSONLexer m_lexer;
100};
101
102
103} // namespace KJS
104
105#endif // JSONLEXER_H
106