1 /*
2  * Copyright 2005 Maksim Orlovich <maksim@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 THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_PARSER_H_
28 #define THIRD_PARTY_BLINK_RENDERER_CORE_XML_XPATH_PARSER_H_
29 
30 #include <memory>
31 #include "base/macros.h"
32 #include "third_party/blink/renderer/core/xml/xpath_predicate.h"
33 #include "third_party/blink/renderer/core/xml/xpath_step.h"
34 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
35 
36 namespace blink {
37 
38 class ExceptionState;
39 class XPathNSResolver;
40 
41 namespace xpath {
42 
43 class Expression;
44 class LocationPath;
45 class Parser;
46 
47 struct Token {
48   STACK_ALLOCATED();
49 
50  public:
51   int type;
52   String str;
53   Step::Axis axis;
54   NumericOp::Opcode numop;
55   EqTestOp::Opcode eqop;
56 
TokenToken57   Token(int t) : type(t) {}
TokenToken58   Token(int t, const String& v) : type(t), str(v) {}
TokenToken59   Token(int t, Step::Axis v) : type(t), axis(v) {}
TokenToken60   Token(int t, NumericOp::Opcode v) : type(t), numop(v) {}
TokenToken61   Token(int t, EqTestOp::Opcode v) : type(t), eqop(v) {}
62 };
63 
64 class Parser {
65   STACK_ALLOCATED();
66 
67  public:
68   Parser();
69   ~Parser();
70 
Resolver()71   XPathNSResolver* Resolver() const { return resolver_; }
72   bool ExpandQName(const String& q_name,
73                    AtomicString& local_name,
74                    AtomicString& namespace_uri);
75 
76   Expression* ParseStatement(const String& statement,
77                              XPathNSResolver*,
78                              ExceptionState&);
79 
Current()80   static Parser* Current() { return current_parser_; }
81 
82   int Lex(void* yylval);
83 
84   Expression* top_expr_;
85   bool got_namespace_error_;
86 
87  private:
88   bool IsBinaryOperatorContext() const;
89 
90   void SkipWS();
91   Token MakeTokenAndAdvance(int type, int advance = 1);
92   Token MakeTokenAndAdvance(int type, NumericOp::Opcode, int advance = 1);
93   Token MakeTokenAndAdvance(int type, EqTestOp::Opcode, int advance = 1);
94   char PeekAheadHelper();
95   char PeekCurHelper();
96 
97   Token LexString();
98   Token LexNumber();
99   bool LexNCName(String&);
100   bool LexQName(String&);
101 
102   Token NextToken();
103   Token NextTokenInternal();
104 
105   void Reset(const String& data);
106 
107   static Parser* current_parser_;
108 
109   unsigned next_pos_;
110   String data_;
111   int last_token_type_;
112   XPathNSResolver* resolver_ = nullptr;
113 
114   DISALLOW_COPY_AND_ASSIGN(Parser);
115 };
116 
117 }  // namespace xpath
118 
119 }  // namespace blink
120 
121 #endif
122