]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/LiteralParser.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / LiteralParser.h
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
29 #include "Identifier.h"
30 #include "JSCJSValue.h"
31 #include "JSGlobalObjectFunctions.h"
32 #include <array>
33 #include <wtf/text/WTFString.h>
34
35 namespace JSC {
36
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;
71 String stringBuffer;
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
98 String getErrorMessage()
99 {
100 if (!m_lexer.getErrorMessage().isEmpty())
101 return String::format("JSON Parse error: %s", m_lexer.getErrorMessage().ascii().data());
102 if (!m_parseErrorMessage.isEmpty())
103 return String::format("JSON Parse error: %s", m_parseErrorMessage.ascii().data());
104 return ASCIILiteral("JSON Parse error: Unable to parse JSON string");
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 {
122 public:
123 Lexer(const CharType* characters, unsigned length, ParserMode mode)
124 : m_mode(mode)
125 , m_ptr(characters)
126 , m_end(characters + length)
127 {
128 }
129
130 TokenType next();
131
132 const LiteralParserToken<CharType>& currentToken()
133 {
134 return m_currentToken;
135 }
136
137 String getErrorMessage() { return m_lexErrorMessage; }
138
139 private:
140 String m_lexErrorMessage;
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;
146 ParserMode m_mode;
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;
157 String m_parseErrorMessage;
158 static unsigned const MaximumCachableCharacter = 128;
159 std::array<Identifier, MaximumCachableCharacter> m_shortIdentifiers;
160 std::array<Identifier, MaximumCachableCharacter> m_recentIdentifiers;
161 ALWAYS_INLINE const Identifier makeIdentifier(const LChar* characters, size_t length);
162 ALWAYS_INLINE const Identifier makeIdentifier(const UChar* characters, size_t length);
163 };
164
165 }
166
167 #endif