]>
Commit | Line | Data |
---|---|---|
ba379fdc A |
1 | /* |
2 | * Copyright (C) 2009 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
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. | |
12 | * | |
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. | |
24 | */ | |
25 | ||
26 | #ifndef LiteralParser_h | |
27 | #define LiteralParser_h | |
28 | ||
14957cd0 | 29 | #include "Identifier.h" |
93a37866 | 30 | #include "JSCJSValue.h" |
ba379fdc | 31 | #include "JSGlobalObjectFunctions.h" |
81345200 | 32 | #include <array> |
93a37866 | 33 | #include <wtf/text/WTFString.h> |
ba379fdc A |
34 | |
35 | namespace JSC { | |
36 | ||
6fe7ccc8 A |
37 | typedef enum { StrictJSON, NonStrictJSON, JSONP } ParserMode; |
38 | ||
39 | enum JSONPPathEntryType { | |
40 | JSONPPathEntryTypeDeclare, // var pathEntryName = JSON | |
41 | JSONPPathEntryTypeDot, // <prior entries>.pathEntryName = JSON | |
42 | JSONPPathEntryTypeLookup, // <prior entries>[pathIndex] = JSON | |
43 | JSONPPathEntryTypeCall // <prior entries>(JSON) | |
44 | }; | |
45 | ||
46 | enum ParserState { StartParseObject, StartParseArray, StartParseExpression, | |
47 | StartParseStatement, StartParseStatementEndStatement, | |
48 | DoParseObjectStartExpression, DoParseObjectEndExpression, | |
49 | DoParseArrayStartExpression, DoParseArrayEndExpression }; | |
50 | enum TokenType { TokLBracket, TokRBracket, TokLBrace, TokRBrace, | |
51 | TokString, TokIdentifier, TokNumber, TokColon, | |
52 | TokLParen, TokRParen, TokComma, TokTrue, TokFalse, | |
53 | TokNull, TokEnd, TokDot, TokAssign, TokSemi, TokError }; | |
54 | ||
55 | struct JSONPPathEntry { | |
56 | JSONPPathEntryType m_type; | |
57 | Identifier m_pathEntryName; | |
58 | int m_pathIndex; | |
59 | }; | |
60 | ||
61 | struct JSONPData { | |
62 | Vector<JSONPPathEntry> m_path; | |
63 | Strong<Unknown> m_value; | |
64 | }; | |
65 | ||
66 | template <typename CharType> | |
67 | struct LiteralParserToken { | |
68 | TokenType type; | |
69 | const CharType* start; | |
70 | const CharType* end; | |
93a37866 | 71 | String stringBuffer; |
6fe7ccc8 A |
72 | union { |
73 | double numberToken; | |
74 | struct { | |
75 | union { | |
76 | const LChar* stringToken8; | |
77 | const UChar* stringToken16; | |
78 | }; | |
79 | unsigned stringIs8Bit : 1; | |
80 | unsigned stringLength : 31; | |
81 | }; | |
82 | }; | |
83 | }; | |
84 | ||
85 | template <typename CharType> | |
86 | ALWAYS_INLINE void setParserTokenString(LiteralParserToken<CharType>&, const CharType* string); | |
87 | ||
88 | template <typename CharType> | |
89 | class LiteralParser { | |
90 | public: | |
91 | LiteralParser(ExecState* exec, const CharType* characters, unsigned length, ParserMode mode) | |
92 | : m_exec(exec) | |
93 | , m_lexer(characters, length, mode) | |
94 | , m_mode(mode) | |
95 | { | |
96 | } | |
97 | ||
93a37866 | 98 | String getErrorMessage() |
6fe7ccc8 A |
99 | { |
100 | if (!m_lexer.getErrorMessage().isEmpty()) | |
ed1e77d3 | 101 | return String::format("JSON Parse error: %s", m_lexer.getErrorMessage().ascii().data()); |
6fe7ccc8 | 102 | if (!m_parseErrorMessage.isEmpty()) |
ed1e77d3 | 103 | return String::format("JSON Parse error: %s", m_parseErrorMessage.ascii().data()); |
93a37866 | 104 | return ASCIILiteral("JSON Parse error: Unable to parse JSON string"); |
6fe7ccc8 A |
105 | } |
106 | ||
107 | JSValue tryLiteralParse() | |
108 | { | |
109 | m_lexer.next(); | |
110 | JSValue result = parse(m_mode == StrictJSON ? StartParseExpression : StartParseStatement); | |
111 | if (m_lexer.currentToken().type == TokSemi) | |
112 | m_lexer.next(); | |
113 | if (m_lexer.currentToken().type != TokEnd) | |
114 | return JSValue(); | |
115 | return result; | |
116 | } | |
117 | ||
118 | bool tryJSONPParse(Vector<JSONPData>&, bool needsFullSourceInfo); | |
119 | ||
120 | private: | |
121 | class Lexer { | |
ba379fdc | 122 | public: |
6fe7ccc8 A |
123 | Lexer(const CharType* characters, unsigned length, ParserMode mode) |
124 | : m_mode(mode) | |
125 | , m_ptr(characters) | |
126 | , m_end(characters + length) | |
ba379fdc A |
127 | { |
128 | } | |
129 | ||
6fe7ccc8 A |
130 | TokenType next(); |
131 | ||
132 | const LiteralParserToken<CharType>& currentToken() | |
ba379fdc | 133 | { |
6fe7ccc8 | 134 | return m_currentToken; |
ba379fdc | 135 | } |
14957cd0 | 136 | |
93a37866 | 137 | String getErrorMessage() { return m_lexErrorMessage; } |
ba379fdc | 138 | |
6fe7ccc8 | 139 | private: |
93a37866 | 140 | String m_lexErrorMessage; |
6fe7ccc8 A |
141 | template <ParserMode mode> TokenType lex(LiteralParserToken<CharType>&); |
142 | ALWAYS_INLINE TokenType lexIdentifier(LiteralParserToken<CharType>&); | |
143 | template <ParserMode mode, char terminator> ALWAYS_INLINE TokenType lexString(LiteralParserToken<CharType>&); | |
144 | ALWAYS_INLINE TokenType lexNumber(LiteralParserToken<CharType>&); | |
145 | LiteralParserToken<CharType> m_currentToken; | |
ba379fdc | 146 | ParserMode m_mode; |
6fe7ccc8 A |
147 | const CharType* m_ptr; |
148 | const CharType* m_end; | |
149 | }; | |
150 | ||
151 | class StackGuard; | |
152 | JSValue parse(ParserState); | |
153 | ||
154 | ExecState* m_exec; | |
155 | typename LiteralParser<CharType>::Lexer m_lexer; | |
156 | ParserMode m_mode; | |
93a37866 | 157 | String m_parseErrorMessage; |
6fe7ccc8 | 158 | static unsigned const MaximumCachableCharacter = 128; |
81345200 A |
159 | std::array<Identifier, MaximumCachableCharacter> m_shortIdentifiers; |
160 | std::array<Identifier, MaximumCachableCharacter> m_recentIdentifiers; | |
6fe7ccc8 A |
161 | ALWAYS_INLINE const Identifier makeIdentifier(const LChar* characters, size_t length); |
162 | ALWAYS_INLINE const Identifier makeIdentifier(const UChar* characters, size_t length); | |
ba379fdc | 163 | }; |
14957cd0 | 164 | |
ba379fdc A |
165 | } |
166 | ||
167 | #endif |