2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef LiteralParser_h
27 #define LiteralParser_h
29 #include "Identifier.h"
30 #include "JSGlobalObjectFunctions.h"
38 typedef enum { StrictJSON, NonStrictJSON, JSONP } ParserMode;
39 LiteralParser(ExecState* exec, const UChar* characters, unsigned length, ParserMode mode)
41 , m_lexer(characters, length, mode)
46 UString getErrorMessage()
48 if (!m_lexer.getErrorMessage().isEmpty())
49 return String::format("JSON Parse error: %s", m_lexer.getErrorMessage().ascii().data()).impl();
50 if (!m_parseErrorMessage.isEmpty())
51 return String::format("JSON Parse error: %s", m_parseErrorMessage.ascii().data()).impl();
52 return "JSON Parse error: Unable to parse JSON string";
55 JSValue tryLiteralParse()
58 JSValue result = parse(m_mode == StrictJSON ? StartParseExpression : StartParseStatement);
59 if (m_lexer.currentToken().type == TokSemi)
61 if (m_lexer.currentToken().type != TokEnd)
66 enum JSONPPathEntryType {
67 JSONPPathEntryTypeDeclare, // var pathEntryName = JSON
68 JSONPPathEntryTypeDot, // <prior entries>.pathEntryName = JSON
69 JSONPPathEntryTypeLookup, // <prior entries>[pathIndex] = JSON
70 JSONPPathEntryTypeCall // <prior entries>(JSON)
73 struct JSONPPathEntry {
74 JSONPPathEntryType m_type;
75 Identifier m_pathEntryName;
80 Vector<JSONPPathEntry> m_path;
81 Strong<Unknown> m_value;
84 bool tryJSONPParse(Vector<JSONPData>&, bool needsFullSourceInfo);
87 enum ParserState { StartParseObject, StartParseArray, StartParseExpression,
88 StartParseStatement, StartParseStatementEndStatement,
89 DoParseObjectStartExpression, DoParseObjectEndExpression,
90 DoParseArrayStartExpression, DoParseArrayEndExpression };
91 enum TokenType { TokLBracket, TokRBracket, TokLBrace, TokRBrace,
92 TokString, TokIdentifier, TokNumber, TokColon,
93 TokLParen, TokRParen, TokComma, TokTrue, TokFalse,
94 TokNull, TokEnd, TokDot, TokAssign, TokSemi, TokError };
98 struct LiteralParserToken {
102 UString stringBuffer;
106 const UChar* stringToken;
111 Lexer(const UChar* characters, unsigned length, ParserMode mode)
114 , m_end(characters + length)
120 const LiteralParserToken& currentToken()
122 return m_currentToken;
125 UString getErrorMessage() { return m_lexErrorMessage; }
128 UString m_lexErrorMessage;
129 template <ParserMode mode> TokenType lex(LiteralParserToken&);
130 template <ParserMode mode, UChar terminator> ALWAYS_INLINE TokenType lexString(LiteralParserToken&);
131 ALWAYS_INLINE TokenType lexNumber(LiteralParserToken&);
132 LiteralParserToken m_currentToken;
140 JSValue parse(ParserState);
143 LiteralParser::Lexer m_lexer;
145 UString m_parseErrorMessage;
146 static unsigned const MaximumCachableCharacter = 128;
147 FixedArray<Identifier, MaximumCachableCharacter> m_shortIdentifiers;
148 FixedArray<Identifier, MaximumCachableCharacter> m_recentIdentifiers;
149 ALWAYS_INLINE const Identifier makeIdentifier(const UChar* characters, size_t length);