initial import
[vuplus_webkit] / Source / JavaScriptCore / 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 "JSGlobalObjectFunctions.h"
31 #include "JSValue.h"
32 #include "UString.h"
33
34 namespace JSC {
35
36     class LiteralParser {
37     public:
38         typedef enum { StrictJSON, NonStrictJSON, JSONP } ParserMode;
39         LiteralParser(ExecState* exec, const UChar* characters, unsigned length, ParserMode mode)
40             : m_exec(exec)
41             , m_lexer(characters, length, mode)
42             , m_mode(mode)
43         {
44         }
45         
46         UString getErrorMessage() 
47         { 
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";
53         }
54         
55         JSValue tryLiteralParse()
56         {
57             m_lexer.next();
58             JSValue result = parse(m_mode == StrictJSON ? StartParseExpression : StartParseStatement);
59             if (m_lexer.currentToken().type == TokSemi)
60                 m_lexer.next();
61             if (m_lexer.currentToken().type != TokEnd)
62                 return JSValue();
63             return result;
64         }
65         
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)
71         };
72
73         struct JSONPPathEntry {
74             JSONPPathEntryType m_type;
75             Identifier m_pathEntryName;
76             int m_pathIndex;
77         };
78
79         struct JSONPData {
80             Vector<JSONPPathEntry> m_path;
81             Strong<Unknown> m_value;
82         };
83
84         bool tryJSONPParse(Vector<JSONPData>&, bool needsFullSourceInfo);
85
86     private:
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 };
95         
96         class Lexer {
97         public:
98             struct LiteralParserToken {
99                 TokenType type;
100                 const UChar* start;
101                 const UChar* end;
102                 UString stringBuffer;
103                 union {
104                     double numberToken;
105                     struct {
106                         const UChar* stringToken;
107                         int stringLength;
108                     };
109                 };
110             };
111             Lexer(const UChar* characters, unsigned length, ParserMode mode)
112                 : m_mode(mode)
113                 , m_ptr(characters)
114                 , m_end(characters + length)
115             {
116             }
117             
118             TokenType next();
119             
120             const LiteralParserToken& currentToken()
121             {
122                 return m_currentToken;
123             }
124             
125             UString getErrorMessage() { return m_lexErrorMessage; }
126             
127         private:
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;
133             UString m_string;
134             ParserMode m_mode;
135             const UChar* m_ptr;
136             const UChar* m_end;
137         };
138         
139         class StackGuard;
140         JSValue parse(ParserState);
141
142         ExecState* m_exec;
143         LiteralParser::Lexer m_lexer;
144         ParserMode m_mode;
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);
150     };
151
152 }
153
154 #endif