1 /* 2 * Copyright 2005 Maksim Orlovich <maksim (at) kde.org> 3 * Copyright (C) 2006 Apple Computer, Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef XPathParser_h 28 #define XPathParser_h 29 30 #include "core/xml/XPathPredicate.h" 31 #include "core/xml/XPathStep.h" 32 33 namespace WebCore { 34 35 class ExceptionState; 36 class XPathNSResolver; 37 38 namespace XPath { 39 40 class Expression; 41 class ParseNode; 42 class Predicate; 43 44 struct Token { 45 int type; 46 String str; 47 Step::Axis axis; 48 NumericOp::Opcode numop; 49 EqTestOp::Opcode eqop; 50 51 Token(int t) : type(t) { } 52 Token(int t, const String& v): type(t), str(v) { } 53 Token(int t, Step::Axis v): type(t), axis(v) { } 54 Token(int t, NumericOp::Opcode v): type(t), numop(v) { } 55 Token(int t, EqTestOp::Opcode v): type(t), eqop(v) { } 56 }; 57 58 class Parser { 59 WTF_MAKE_NONCOPYABLE(Parser); 60 public: 61 Parser(); 62 ~Parser(); 63 64 XPathNSResolver* resolver() const { return m_resolver.get(); } 65 bool expandQName(const String& qName, AtomicString& localName, AtomicString& namespaceURI); 66 67 Expression* parseStatement(const String& statement, PassRefPtr<XPathNSResolver>, ExceptionState&); 68 69 static Parser* current() { return currentParser; } 70 71 int lex(void* yylval); 72 73 Expression* m_topExpr; 74 bool m_gotNamespaceError; 75 76 void registerParseNode(ParseNode*); 77 void unregisterParseNode(ParseNode*); 78 79 void registerPredicateVector(Vector<OwnPtr<Predicate> >*); 80 void deletePredicateVector(Vector<OwnPtr<Predicate> >*); 81 82 void registerExpressionVector(Vector<OwnPtr<Expression> >*); 83 void deleteExpressionVector(Vector<OwnPtr<Expression> >*); 84 85 void registerString(String*); 86 void deleteString(String*); 87 88 void registerNodeTest(Step::NodeTest*); 89 void deleteNodeTest(Step::NodeTest*); 90 91 private: 92 bool isBinaryOperatorContext() const; 93 94 void skipWS(); 95 Token makeTokenAndAdvance(int type, int advance = 1); 96 Token makeTokenAndAdvance(int type, NumericOp::Opcode, int advance = 1); 97 Token makeTokenAndAdvance(int type, EqTestOp::Opcode, int advance = 1); 98 char peekAheadHelper(); 99 char peekCurHelper(); 100 101 Token lexString(); 102 Token lexNumber(); 103 bool lexNCName(String&); 104 bool lexQName(String&); 105 106 Token nextToken(); 107 Token nextTokenInternal(); 108 109 void reset(const String& data); 110 111 static Parser* currentParser; 112 113 unsigned m_nextPos; 114 String m_data; 115 int m_lastTokenType; 116 RefPtr<XPathNSResolver> m_resolver; 117 118 HashSet<ParseNode*> m_parseNodes; 119 HashSet<Vector<OwnPtr<Predicate> >*> m_predicateVectors; 120 HashSet<Vector<OwnPtr<Expression> >*> m_expressionVectors; 121 HashSet<String*> m_strings; 122 HashSet<Step::NodeTest*> m_nodeTests; 123 }; 124 125 } // XPath 126 127 } // WebCore 128 129 #endif 130