doxygen
parserintf.h
1 /******************************************************************************
2  *
3  *
4  *
5  * Copyright (C) 1997-2015 by Dimitri van Heesch.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation under the terms of the GNU General Public License is hereby
9  * granted. No representations are made about the suitability of this software
10  * for any purpose. It is provided "as is" without express or implied warranty.
11  * See the GNU General Public License for more details.
12  *
13  * Documents produced by Doxygen are derivative works derived from the
14  * input used in their production; they are not affected by this license.
15  *
16  */
17 
18 #ifndef PARSERINTF_H
19 #define PARSERINTF_H
20 
21 #include <functional>
22 #include <memory>
23 #include <map>
24 #include <string>
25 
26 #include "types.h"
27 #include "containers.h"
28 
29 class Entry;
30 class FileDef;
32 class MemberDef;
33 class Definition;
34 class ClangTUParser;
35 
43 {
44  public:
45  virtual ~OutlineParserInterface() {}
46 
55  virtual void parseInput(const QCString &fileName,
56  const char *fileBuf,
57  const std::shared_ptr<Entry> &root,
58  ClangTUParser *clangParser) = 0;
59 
65  virtual bool needsPreprocessing(const QCString &extension) const = 0;
66 
73  virtual void parsePrototype(const QCString &text) = 0;
74 
75 };
76 
84 {
85  public:
86  virtual ~CodeParserInterface() {}
87 
110  virtual void parseCode(CodeOutputInterface &codeOutIntf,
111  const QCString &scopeName,
112  const QCString &input,
113  SrcLangExt lang,
114  bool isExampleBlock,
115  const QCString &exampleName=QCString(),
116  const FileDef *fileDef=0,
117  int startLine=-1,
118  int endLine=-1,
119  bool inlineFragment=FALSE,
120  const MemberDef *memberDef=0,
121  bool showLineNumbers=TRUE,
122  const Definition *searchCtx=0,
123  bool collectXRefs=TRUE
124  ) = 0;
125 
131  virtual void resetCodeParserState() = 0;
132 
133 };
134 
135 //-----------------------------------------------------------------------------
136 
137 using OutlineParserFactory = std::function<std::unique_ptr<OutlineParserInterface>()>;
138 using CodeParserFactory = std::function<std::unique_ptr<CodeParserInterface>()>;
139 
146 {
147 
148  struct ParserPair
149  {
150  ParserPair(OutlineParserFactory opf, CodeParserFactory cpf, const QCString &pn)
151  : outlineParserFactory(opf), codeParserFactory(cpf), parserName(pn)
152  {
153  }
154 
155  OutlineParserFactory outlineParserFactory;
156  CodeParserFactory codeParserFactory;
157  QCString parserName;
158  };
159 
160  public:
165  ParserManager(OutlineParserFactory outlineParserFactory,
166  CodeParserFactory codeParserFactory)
167  : m_defaultParsers(outlineParserFactory,codeParserFactory, QCString())
168  {
169  }
170 
179  void registerParser(const QCString &name,OutlineParserFactory outlineParserFactory,
180  CodeParserFactory codeParserFactory)
181  {
182  m_parsers.emplace(name.str(),ParserPair(outlineParserFactory,codeParserFactory,name));
183  }
184 
188  bool registerExtension(const QCString &extension, const QCString &parserName)
189  {
190  if (parserName.isEmpty() || extension.isEmpty()) return FALSE;
191 
192  const auto &parserIt = m_parsers.find(parserName.str());
193  if (parserIt == m_parsers.end()) return FALSE;
194 
195  auto extensionIt = m_extensions.find(extension.str());
196  if (extensionIt != m_extensions.end()) // extension already exists
197  {
198  m_extensions.erase(extensionIt); // remove it (e.g. user specified extension overrules built in one)
199  }
200  m_extensions.emplace(extension.str(),parserIt->second); // add new mapping
201  return TRUE;
202  }
203 
208  std::unique_ptr<OutlineParserInterface> getOutlineParser(const QCString &extension)
209  {
210  return getParsers(extension).outlineParserFactory();
211  }
212 
217  std::unique_ptr<CodeParserInterface> getCodeParser(const QCString &extension)
218  {
219  auto factory = getCodeParserFactory(extension);
220  return factory();
221  }
222 
224  CodeParserFactory &getCodeParserFactory(const QCString &extension)
225  {
226  return getParsers(extension).codeParserFactory;
227  }
228 
233  QCString getParserName(const QCString &extension)
234  {
235  return getParsers(extension).parserName;
236  }
237 
238  private:
239  ParserPair &getParsers(const QCString &extension)
240  {
241  QCString ext = extension.lower();
242  if (ext.isEmpty()) ext=".no_extension";
243  auto it = m_extensions.find(ext.data());
244  if (it==m_extensions.end() && ext.length()>4)
245  {
246  it = m_extensions.find(ext.left(4).data());
247  }
248  return it!=m_extensions.end() ? it->second : m_defaultParsers;
249  }
250 
251  std::map<std::string,ParserPair> m_parsers;
252  std::map<std::string,ParserPair &> m_extensions;
253  ParserPair m_defaultParsers;
254 };
255 
256 #endif
This file contains a number of basic enums and types.
The common base class of all entity definitions found in the sources.
Definition: definition.h:76
A model of a class/file/namespace member symbol.
Definition: memberdef.h:45
Represents an unstructured piece of information, about an entity found in the sources.
Definition: entry.h:61
virtual void parseInput(const QCString &fileName, const char *fileBuf, const std::shared_ptr< Entry > &root, ClangTUParser *clangParser)=0
Parses a single input file with the goal to build an Entry tree.
A model of a file symbol.
Definition: filedef.h:73
SrcLangExt
Language as given by extension.
Definition: types.h:41
uint length() const
Returns the length of the string, not counting the 0-terminator.
Definition: qcstring.h:147
virtual bool needsPreprocessing(const QCString &extension) const =0
Returns TRUE if the language identified by extension needs the C preprocessor to be run before feed t...
bool registerExtension(const QCString &extension, const QCString &parserName)
Registers a file extension with a parser with name parserName.
Definition: parserintf.h:188
Manages programming language parsers.
Definition: parserintf.h:145
QCString getParserName(const QCString &extension)
Gets the name of the parser associated with given extension.
Definition: parserintf.h:233
void registerParser(const QCString &name, OutlineParserFactory outlineParserFactory, CodeParserFactory codeParserFactory)
Registers an additional parser.
Definition: parserintf.h:179
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition: qcstring.h:153
virtual void parsePrototype(const QCString &text)=0
Callback function called by the comment block scanner.
Abstract interface for outline parsers.
Definition: parserintf.h:42
std::unique_ptr< OutlineParserInterface > getOutlineParser(const QCString &extension)
Gets the interface to the parser associated with a given extension.
Definition: parserintf.h:208
ParserManager(OutlineParserFactory outlineParserFactory, CodeParserFactory codeParserFactory)
Create the parser manager.
Definition: parserintf.h:165
CodeParserFactory & getCodeParserFactory(const QCString &extension)
Get the factory for create code parser objects with a given extension.
Definition: parserintf.h:224
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition: qcstring.h:144
This is an alternative implementation of QCString.
Definition: qcstring.h:108
Output interface for code parser.
Definition: outputgen.h:61
Abstract interface for code parsers.
Definition: parserintf.h:83
Clang parser object for a single translation unit, which consists of a source file and the directly o...
Definition: clangparser.h:21
std::unique_ptr< CodeParserInterface > getCodeParser(const QCString &extension)
Gets the interface to the parser associated with a given extension.
Definition: parserintf.h:217