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 JSValue
tryLiteralParse()
49 JSValue result
= parse(m_mode
== StrictJSON
? StartParseExpression
: StartParseStatement
);
50 if (m_lexer
.currentToken().type
== TokSemi
)
52 if (m_lexer
.currentToken().type
!= TokEnd
)
57 enum JSONPPathEntryType
{
58 JSONPPathEntryTypeDeclare
, // var pathEntryName = JSON
59 JSONPPathEntryTypeDot
, // <prior entries>.pathEntryName = JSON
60 JSONPPathEntryTypeLookup
, // <prior entries>[pathIndex] = JSON
61 JSONPPathEntryTypeCall
// <prior entries>(JSON)
64 struct JSONPPathEntry
{
65 JSONPPathEntryType m_type
;
66 Identifier m_pathEntryName
;
71 Vector
<JSONPPathEntry
> m_path
;
72 Strong
<Unknown
> m_value
;
75 bool tryJSONPParse(Vector
<JSONPData
>&, bool needsFullSourceInfo
);
78 enum ParserState
{ StartParseObject
, StartParseArray
, StartParseExpression
,
79 StartParseStatement
, StartParseStatementEndStatement
,
80 DoParseObjectStartExpression
, DoParseObjectEndExpression
,
81 DoParseArrayStartExpression
, DoParseArrayEndExpression
};
82 enum TokenType
{ TokLBracket
, TokRBracket
, TokLBrace
, TokRBrace
,
83 TokString
, TokIdentifier
, TokNumber
, TokColon
,
84 TokLParen
, TokRParen
, TokComma
, TokTrue
, TokFalse
,
85 TokNull
, TokEnd
, TokDot
, TokAssign
, TokSemi
, TokError
};
89 struct LiteralParserToken
{
97 const UChar
* stringToken
;
102 Lexer(const UChar
* characters
, unsigned length
, ParserMode mode
)
105 , m_end(characters
+ length
)
111 const LiteralParserToken
& currentToken()
113 return m_currentToken
;
117 template <ParserMode mode
> TokenType
lex(LiteralParserToken
&);
118 template <ParserMode mode
, UChar terminator
> ALWAYS_INLINE TokenType
lexString(LiteralParserToken
&);
119 ALWAYS_INLINE TokenType
lexNumber(LiteralParserToken
&);
120 LiteralParserToken m_currentToken
;
128 JSValue
parse(ParserState
);
131 LiteralParser::Lexer m_lexer
;
133 static unsigned const MaximumCachableCharacter
= 128;
134 FixedArray
<Identifier
, MaximumCachableCharacter
> m_shortIdentifiers
;
135 FixedArray
<Identifier
, MaximumCachableCharacter
> m_recentIdentifiers
;
136 ALWAYS_INLINE
const Identifier
makeIdentifier(const UChar
* characters
, size_t length
);