2 * Copyright (C) 2010 Google 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "IDBKeyPath.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include <wtf/ASCIICType.h>
36 class IDBKeyPathLexer {
45 explicit IDBKeyPathLexer(const String& s)
47 , m_ptr(s.characters())
48 , m_end(s.characters() + s.length())
49 , m_currentTokenType(TokenError)
53 TokenType currentTokenType() const { return m_currentTokenType; }
55 TokenType nextTokenType()
57 m_currentTokenType = lex(m_currentElement);
58 return m_currentTokenType;
61 const String& currentElement() { return m_currentElement; }
64 TokenType lex(String&);
65 TokenType lexIdentifier(String&);
66 String m_currentElement;
70 TokenType m_currentTokenType;
73 IDBKeyPathLexer::TokenType IDBKeyPathLexer::lex(String& element)
77 ASSERT(m_ptr < m_end);
83 return lexIdentifier(element);
86 static inline bool isSafeIdentifierStartCharacter(UChar c)
88 return isASCIIAlpha(c) || (c == '_') || (c == '$');
91 static inline bool isSafeIdentifierCharacter(UChar c)
93 return isASCIIAlphanumeric(c) || (c == '_') || (c == '$');
96 IDBKeyPathLexer::TokenType IDBKeyPathLexer::lexIdentifier(String& element)
98 const UChar* start = m_ptr;
99 if (m_ptr < m_end && isSafeIdentifierStartCharacter(*m_ptr))
104 while (m_ptr < m_end && isSafeIdentifierCharacter(*m_ptr))
107 element = String(start, m_ptr - start);
108 return TokenIdentifier;
111 bool IDBIsValidKeyPath(const String& keyPath)
113 IDBKeyPathParseError error;
114 Vector<String> keyPathElements;
115 IDBParseKeyPath(keyPath, keyPathElements, error);
116 return error == IDBKeyPathParseErrorNone;
119 void IDBParseKeyPath(const String& keyPath, Vector<String>& elements, IDBKeyPathParseError& error)
121 // IDBKeyPath ::= EMPTY_STRING | identifier ('.' identifier)*
122 // The basic state machine is:
123 // Start => {Identifier, End}
124 // Identifier => {Dot, End}
125 // Dot => {Identifier}
126 // It bails out as soon as it finds an error, but doesn't discard the bits it managed to parse.
127 enum ParserState { Identifier, Dot, End };
129 IDBKeyPathLexer lexer(keyPath);
130 IDBKeyPathLexer::TokenType tokenType = lexer.nextTokenType();
132 if (tokenType == IDBKeyPathLexer::TokenIdentifier)
134 else if (tokenType == IDBKeyPathLexer::TokenEnd)
137 error = IDBKeyPathParseErrorStart;
144 IDBKeyPathLexer::TokenType tokenType = lexer.currentTokenType();
145 ASSERT(tokenType == IDBKeyPathLexer::TokenIdentifier);
147 String element = lexer.currentElement();
148 elements.append(element);
150 tokenType = lexer.nextTokenType();
151 if (tokenType == IDBKeyPathLexer::TokenDot)
153 else if (tokenType == IDBKeyPathLexer::TokenEnd)
156 error = IDBKeyPathParseErrorIdentifier;
162 IDBKeyPathLexer::TokenType tokenType = lexer.currentTokenType();
163 ASSERT(tokenType == IDBKeyPathLexer::TokenDot);
165 tokenType = lexer.nextTokenType();
166 if (tokenType == IDBKeyPathLexer::TokenIdentifier)
169 error = IDBKeyPathParseErrorDot;
175 error = IDBKeyPathParseErrorNone;
182 } // namespace WebCore
184 #endif // ENABLE(INDEXED_DATABASE)