1259701Sdim//===--- Parser.h - Matcher expression parser -----*- C++ -*-===//
2259701Sdim//
3259701Sdim//                     The LLVM Compiler Infrastructure
4259701Sdim//
5259701Sdim// This file is distributed under the University of Illinois Open Source
6259701Sdim// License. See LICENSE.TXT for details.
7259701Sdim//
8259701Sdim//===----------------------------------------------------------------------===//
9259701Sdim///
10259701Sdim/// \file
11259701Sdim/// \brief Simple matcher expression parser.
12259701Sdim///
13259701Sdim/// The parser understands matcher expressions of the form:
14259701Sdim///   MatcherName(Arg0, Arg1, ..., ArgN)
15259701Sdim/// as well as simple types like strings.
16259701Sdim/// The parser does not know how to process the matchers. It delegates this task
17259701Sdim/// to a Sema object received as an argument.
18259701Sdim///
19259701Sdim/// \code
20259701Sdim/// Grammar for the expressions supported:
21259701Sdim/// <Expression>        := <Literal> | <MatcherExpression>
22259701Sdim/// <Literal>           := <StringLiteral> | <Unsigned>
23259701Sdim/// <StringLiteral>     := "quoted string"
24259701Sdim/// <Unsigned>          := [0-9]+
25259701Sdim/// <MatcherExpression> := <MatcherName>(<ArgumentList>) |
26259701Sdim///                        <MatcherName>(<ArgumentList>).bind(<StringLiteral>)
27259701Sdim/// <MatcherName>       := [a-zA-Z]+
28259701Sdim/// <ArgumentList>      := <Expression> | <Expression>,<ArgumentList>
29259701Sdim/// \endcode
30259701Sdim///
31259701Sdim//===----------------------------------------------------------------------===//
32259701Sdim
33259701Sdim#ifndef LLVM_CLANG_AST_MATCHERS_DYNAMIC_PARSER_H
34259701Sdim#define LLVM_CLANG_AST_MATCHERS_DYNAMIC_PARSER_H
35259701Sdim
36259701Sdim#include "clang/ASTMatchers/Dynamic/Diagnostics.h"
37259701Sdim#include "clang/ASTMatchers/Dynamic/VariantValue.h"
38259701Sdim#include "clang/Basic/LLVM.h"
39259701Sdim#include "llvm/ADT/ArrayRef.h"
40259701Sdim#include "llvm/ADT/Optional.h"
41259701Sdim#include "llvm/ADT/StringRef.h"
42259701Sdim
43259701Sdimnamespace clang {
44259701Sdimnamespace ast_matchers {
45259701Sdimnamespace dynamic {
46259701Sdim
47259701Sdim/// \brief Matcher expression parser.
48259701Sdimclass Parser {
49259701Sdimpublic:
50259701Sdim  /// \brief Interface to connect the parser with the registry and more.
51259701Sdim  ///
52259701Sdim  /// The parser uses the Sema instance passed into
53259701Sdim  /// parseMatcherExpression() to handle all matcher tokens. The simplest
54259701Sdim  /// processor implementation would simply call into the registry to create
55259701Sdim  /// the matchers.
56259701Sdim  /// However, a more complex processor might decide to intercept the matcher
57259701Sdim  /// creation and do some extra work. For example, it could apply some
58259701Sdim  /// transformation to the matcher by adding some id() nodes, or could detect
59259701Sdim  /// specific matcher nodes for more efficient lookup.
60259701Sdim  class Sema {
61259701Sdim  public:
62259701Sdim    virtual ~Sema();
63259701Sdim
64259701Sdim    /// \brief Process a matcher expression.
65259701Sdim    ///
66259701Sdim    /// All the arguments passed here have already been processed.
67259701Sdim    ///
68259701Sdim    /// \param MatcherName The matcher name found by the parser.
69259701Sdim    ///
70259701Sdim    /// \param NameRange The location of the name in the matcher source.
71259701Sdim    ///   Useful for error reporting.
72259701Sdim    ///
73259701Sdim    /// \param BindID The ID to use to bind the matcher, or a null \c StringRef
74259701Sdim    ///   if no ID is specified.
75259701Sdim    ///
76259701Sdim    /// \param Args The argument list for the matcher.
77259701Sdim    ///
78259701Sdim    /// \return The matcher objects constructed by the processor, or a null
79259701Sdim    ///   matcher if an error occurred. In that case, \c Error will contain a
80259701Sdim    ///   description of the error.
81259701Sdim    virtual VariantMatcher actOnMatcherExpression(StringRef MatcherName,
82259701Sdim                                                  const SourceRange &NameRange,
83259701Sdim                                                  StringRef BindID,
84259701Sdim                                                  ArrayRef<ParserValue> Args,
85259701Sdim                                                  Diagnostics *Error) = 0;
86259701Sdim  };
87259701Sdim
88259701Sdim  /// \brief Parse a matcher expression, creating matchers from the registry.
89259701Sdim  ///
90259701Sdim  /// This overload creates matchers calling directly into the registry. If the
91259701Sdim  /// caller needs more control over how the matchers are created, then it can
92259701Sdim  /// use the overload below that takes a Sema.
93259701Sdim  ///
94259701Sdim  /// \param MatcherCode The matcher expression to parse.
95259701Sdim  ///
96259701Sdim  /// \return The matcher object constructed, or an empty Optional if an error
97259701Sdim  ///   occurred.
98259701Sdim  ///   In that case, \c Error will contain a description of the error.
99259701Sdim  ///   The caller takes ownership of the DynTypedMatcher object returned.
100259701Sdim  static llvm::Optional<DynTypedMatcher>
101259701Sdim  parseMatcherExpression(StringRef MatcherCode, Diagnostics *Error);
102259701Sdim
103259701Sdim  /// \brief Parse a matcher expression.
104259701Sdim  ///
105259701Sdim  /// \param MatcherCode The matcher expression to parse.
106259701Sdim  ///
107259701Sdim  /// \param S The Sema instance that will help the parser
108259701Sdim  ///   construct the matchers.
109259701Sdim  /// \return The matcher object constructed by the processor, or an empty
110259701Sdim  ///   Optional if an error occurred. In that case, \c Error will contain a
111259701Sdim  ///   description of the error.
112259701Sdim  ///   The caller takes ownership of the DynTypedMatcher object returned.
113259701Sdim  static llvm::Optional<DynTypedMatcher>
114259701Sdim  parseMatcherExpression(StringRef MatcherCode, Sema *S, Diagnostics *Error);
115259701Sdim
116259701Sdim  /// \brief Parse an expression, creating matchers from the registry.
117259701Sdim  ///
118259701Sdim  /// Parses any expression supported by this parser. In general, the
119259701Sdim  /// \c parseMatcherExpression function is a better approach to get a matcher
120259701Sdim  /// object.
121259701Sdim  static bool parseExpression(StringRef Code, VariantValue *Value,
122259701Sdim                              Diagnostics *Error);
123259701Sdim
124259701Sdim  /// \brief Parse an expression.
125259701Sdim  ///
126259701Sdim  /// Parses any expression supported by this parser. In general, the
127259701Sdim  /// \c parseMatcherExpression function is a better approach to get a matcher
128259701Sdim  /// object.
129259701Sdim  static bool parseExpression(StringRef Code, Sema *S,
130259701Sdim                              VariantValue *Value, Diagnostics *Error);
131259701Sdim
132259701Sdimprivate:
133259701Sdim  class CodeTokenizer;
134259701Sdim  struct TokenInfo;
135259701Sdim
136259701Sdim  Parser(CodeTokenizer *Tokenizer, Sema *S,
137259701Sdim         Diagnostics *Error);
138259701Sdim
139259701Sdim  bool parseExpressionImpl(VariantValue *Value);
140259701Sdim  bool parseMatcherExpressionImpl(VariantValue *Value);
141259701Sdim
142259701Sdim  CodeTokenizer *const Tokenizer;
143259701Sdim  Sema *const S;
144259701Sdim  Diagnostics *const Error;
145259701Sdim};
146259701Sdim
147259701Sdim}  // namespace dynamic
148259701Sdim}  // namespace ast_matchers
149259701Sdim}  // namespace clang
150259701Sdim
151259701Sdim#endif  // LLVM_CLANG_AST_MATCHERS_DYNAMIC_PARSER_H
152