Doxygen
Loading...
Searching...
No Matches
parserintf.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2023 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#ifndef PARSERINTF_H
17#define PARSERINTF_H
18
19#include <functional>
20#include <map>
21#include <memory>
22#include <string>
23
24#include "construct.h"
25#include "types.h"
26
27class ClangTUParser;
28class Definition;
29class Entry;
30class FileDef;
31class MemberDef;
32class OutputCodeList;
33
34/** \brief Abstract interface for outline parsers.
35 *
36 * By implementing the methods of this interface one can add
37 * a new language parser to doxygen. The parser implementation can make use of the
38 * comment block parser to parse the contents of special comment blocks.
39 */
41{
42 public:
44
45 /** Parses a single input file with the goal to build an Entry tree.
46 * @param[in] fileName The full name of the file.
47 * @param[in] fileBuf The contents of the file (zero terminated).
48 * @param[in,out] root The root of the tree of Entry *nodes
49 * representing the information extracted from the file.
50 * @param[in] clangParser The clang translation unit parser object
51 * or nullptr if disabled.
52 */
53 virtual void parseInput(const DString &fileName,
54 const char *fileBuf,
55 const std::shared_ptr<Entry> &root,
56 ClangTUParser *clangParser) = 0;
57
58 /** Returns true if the language identified by \a extension needs
59 * the C preprocessor to be run before feed the result to the input
60 * parser.
61 * @see parseInput()
62 */
63 virtual bool needsPreprocessing(const DString &extension) const = 0;
64
65 /** Callback function called by the comment block scanner.
66 * It provides a string \a text containing the prototype of a function
67 * or variable. The parser should parse this and store the information
68 * in the Entry node that corresponds with the node for which the
69 * comment block parser was invoked.
70 */
71 virtual void parsePrototype(const DString &text) = 0;
72
73};
74
75/** Options to configure the code parser */
77{
78 public:
79 // === getters for optional params
80 bool isExample() const { return m_isExample; }
81 DString exampleName() const { return m_exampleName; }
82 const FileDef * fileDef() const { return m_fileDef; }
83 int startLine() const { return m_startLine; }
84 int endLine() const { return m_endLine; }
85 bool inlineFragment() const { return m_inlineFragment; }
86 const MemberDef * memberDef() const { return m_memberDef; }
87 bool showLineNumbers() const { return m_showLineNumbers; }
88 const Definition *searchCtx() const { return m_searchCtx; }
89 bool collectXRefs() const { return m_collectXRefs; }
90
91 // === setters for optional params
92
93 /// Associate this comment block with a given example
95 { m_isExample=isExample; m_exampleName = name; return *this; }
96
98 { m_fileDef = fd; return *this; }
99
101 { m_startLine = lineNr; return *this; }
102
104 { m_endLine = lineNr; return *this; }
105
107 { m_inlineFragment = enable; return *this; }
108
110 { m_memberDef = md; return *this; }
111
113 { m_showLineNumbers = enable; return *this; }
114
116 { m_searchCtx = d; return *this; }
117
119 { m_collectXRefs = enable; return *this; }
120
121 private:
122 bool m_isExample = false;
124 const FileDef * m_fileDef = nullptr;
125 int m_startLine = -1;
126 int m_endLine = -1;
127 bool m_inlineFragment = false;
128 const MemberDef * m_memberDef = nullptr;
129 bool m_showLineNumbers = true;
130 const Definition *m_searchCtx = nullptr;
131 bool m_collectXRefs = true;
132};
133
134/** \brief Abstract interface for code parsers.
135 *
136 * By implementing the methods of this interface one can add
137 * a new language parser to doxygen. This interface is used for
138 * syntax highlighting, but also to extract cross references and call graphs.
139 */
141{
142 public:
144
145 /** Parses a source file or fragment with the goal to produce
146 * highlighted and cross-referenced output.
147 * @param[in] codeOutList interface for writing the result.
148 * @param[in] scopeName Name of scope to which the code belongs.
149 * @param[in] input Actual code in the form of a string
150 * @param[in] lang The programming language of the code fragment.
151 * @param[in] stripCodeComments signals whether or not for the code block the doxygen comments should be stripped.
152 * @param[in] options Additional options to configure the parser.
153 */
154 virtual void parseCode(OutputCodeList &codeOutList,
155 const DString &scopeName,
156 const DString &input,
157 SrcLangExt lang,
158 bool stripCodeComments,
159 const CodeParserOptions &options
160 ) = 0;
161
162 /** Resets the state of the code parser.
163 * Since multiple code fragments can together form a single example, an
164 * explicit function is used to reset the code parser state.
165 * @see parseCode()
166 */
167 virtual void resetCodeParserState() = 0;
168
169};
170
171//-----------------------------------------------------------------------------
172
173using OutlineParserFactory = std::function<std::unique_ptr<OutlineParserInterface>()>;
174using CodeParserFactory = std::function<std::unique_ptr<CodeParserInterface>()>;
175
176/** \brief Manages programming language parsers.
177 *
178 * This class manages the language parsers in the system. One can
179 * register parsers, and obtain a parser given a file extension.
180 */
182{
183
195
196 public:
197 /** Create the parser manager
198 * @param outlineParserFactory the fallback outline parser factory to use for unknown extensions
199 * @param codeParserFactory the fallback code parser factory to use for unknown extensions
200 */
201 ParserManager(const OutlineParserFactory &outlineParserFactory,
202 const CodeParserFactory &codeParserFactory)
203 : m_defaultParsers(outlineParserFactory,codeParserFactory, DString())
204 {
205 }
206
207 /** Registers an additional parser.
208 * @param[in] name A symbolic name of the parser, i.e. "c",
209 * "python", "fortran", "vhdl", ...
210 * @param[in] outlineParserFactory A factory method to create a language parser (scanner) that
211 * is to be used for the given name.
212 * @param[in] codeParserFactory A factory method to create a code parser that is to be used
213 * for the given name.
214 */
215 void registerParser(const DString &name,const OutlineParserFactory &outlineParserFactory,
216 const CodeParserFactory &codeParserFactory)
217 {
218 m_parsers.emplace(name.str(),ParserPair(outlineParserFactory,codeParserFactory,name));
219 }
220
221 /** Registers a file \a extension with a parser with name \a parserName.
222 * Returns true if the extension was successfully registered.
223 */
224 bool registerExtension(const DString &extension, const DString &parserName)
225 {
226 if (parserName.empty() || extension.empty()) return false;
227
228 const auto &parserIt = m_parsers.find(parserName.str());
229 if (parserIt == m_parsers.end()) return false;
230
231 auto extensionIt = m_extensions.find(extension.str());
232 if (extensionIt != m_extensions.end()) // extension already exists
233 {
234 m_extensions.erase(extensionIt); // remove it (e.g. user specified extension overrules built in one)
235 }
236 m_extensions.emplace(extension.str(),parserIt->second); // add new mapping
237 return true;
238 }
239
240 /** Gets the interface to the parser associated with a given \a extension.
241 * If there is no parser explicitly registered for the supplied extension,
242 * the interface to the default parser will be returned.
243 */
244 std::unique_ptr<OutlineParserInterface> getOutlineParser(const DString &extension)
245 {
246 return getParsers(extension).outlineParserFactory();
247 }
248
249 /** Gets the interface to the parser associated with a given \a extension.
250 * If there is no parser explicitly registered for the supplied extension,
251 * the interface to the default parser will be returned.
252 */
253 std::unique_ptr<CodeParserInterface> getCodeParser(const DString &extension)
254 {
255 return getCodeParserFactory(extension)();
256 }
257
258 /** Get the factory for create code parser objects with a given \a extension. */
260 {
261 return getParsers(extension).codeParserFactory;
262 }
263
264 /** Gets the name of the parser associated with given \a extension.
265 * If there is no parser explicitly registered for the supplied extension,
266 * the empty string will be returned.
267 */
268 DString getParserName(const DString &extension)
269 {
270 return getParsers(extension).parserName;
271 }
272
273 private:
274 ParserPair &getParsers(const DString &extension)
275 {
276 DString ext = extension.lower();
277 if (ext.empty()) ext=".no_extension";
278 auto it = m_extensions.find(ext.data());
279 if (it==m_extensions.end() && ext.length()>4)
280 {
281 it = m_extensions.find(ext.left(4).data());
282 }
283 return it!=m_extensions.end() ? it->second : m_defaultParsers;
284 }
285
286 std::map<std::string,ParserPair> m_parsers;
287 std::map<std::string,ParserPair &> m_extensions;
289};
290
291#endif
Clang parser object for a single translation unit, which consists of a source file and the directly o...
Definition clangparser.h:25
Abstract interface for code parsers.
Definition parserintf.h:141
virtual void resetCodeParserState()=0
Resets the state of the code parser.
virtual void parseCode(OutputCodeList &codeOutList, const DString &scopeName, const DString &input, SrcLangExt lang, bool stripCodeComments, const CodeParserOptions &options)=0
Parses a source file or fragment with the goal to produce highlighted and cross-referenced output.
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
DString lower() const
Definition dstring.h:326
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
size_t find(char c, size_t pos=0) const
Definition dstring.h:239
DString left(size_t len) const
Definition dstring.h:306
const std::string & str() const
Definition dstring.h:645
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:157
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
The common base class of all entity definitions found in the sources.
Definition definition.h:77
Represents an unstructured piece of information, about an entity found in the sources.
Definition entry.h:115
A model of a file symbol.
Definition filedef.h:97
A model of a class/file/namespace member symbol.
Definition memberdef.h:45
Abstract interface for outline parsers.
Definition parserintf.h:41
virtual bool needsPreprocessing(const DString &extension) const =0
Returns true if the language identified by extension needs the C preprocessor to be run before feed t...
virtual void parseInput(const DString &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.
virtual void parsePrototype(const DString &text)=0
Callback function called by the comment block scanner.
Class representing a list of different code generators.
Definition outputlist.h:162
std::map< std::string, ParserPair > m_parsers
Definition parserintf.h:286
DString getParserName(const DString &extension)
Gets the name of the parser associated with given extension.
Definition parserintf.h:268
std::unique_ptr< CodeParserInterface > getCodeParser(const DString &extension)
Gets the interface to the parser associated with a given extension.
Definition parserintf.h:253
CodeParserFactory & getCodeParserFactory(const DString &extension)
Get the factory for create code parser objects with a given extension.
Definition parserintf.h:259
ParserPair m_defaultParsers
Definition parserintf.h:288
void registerParser(const DString &name, const OutlineParserFactory &outlineParserFactory, const CodeParserFactory &codeParserFactory)
Registers an additional parser.
Definition parserintf.h:215
ParserManager(const OutlineParserFactory &outlineParserFactory, const CodeParserFactory &codeParserFactory)
Create the parser manager.
Definition parserintf.h:201
ParserPair & getParsers(const DString &extension)
Definition parserintf.h:274
std::map< std::string, ParserPair & > m_extensions
Definition parserintf.h:287
std::unique_ptr< OutlineParserInterface > getOutlineParser(const DString &extension)
Gets the interface to the parser associated with a given extension.
Definition parserintf.h:244
bool registerExtension(const DString &extension, const DString &parserName)
Registers a file extension with a parser with name parserName.
Definition parserintf.h:224
#define ABSTRACT_BASE_CLASS(cls)
Macro to implement rule of 5 for an abstract base class.
Definition construct.h:20
Definition dstring.h:913
std::function< std::unique_ptr< CodeParserInterface >()> CodeParserFactory
Definition parserintf.h:174
std::function< std::unique_ptr< OutlineParserInterface >()> OutlineParserFactory
Definition parserintf.h:173
Options to configure the code parser.
Definition parserintf.h:77
DString m_exampleName
Definition parserintf.h:123
CodeParserOptions & setSearchCtx(const Definition *d)
Definition parserintf.h:115
bool isExample() const
Definition parserintf.h:80
CodeParserOptions & setStartLine(int lineNr)
Definition parserintf.h:100
const Definition * searchCtx() const
Definition parserintf.h:88
DString exampleName() const
Definition parserintf.h:81
const FileDef * fileDef() const
Definition parserintf.h:82
int endLine() const
Definition parserintf.h:84
CodeParserOptions & setInlineFragment(bool enable)
Definition parserintf.h:106
const MemberDef * memberDef() const
Definition parserintf.h:86
CodeParserOptions & setCollectXRefs(bool enable)
Definition parserintf.h:118
bool showLineNumbers() const
Definition parserintf.h:87
bool inlineFragment() const
Definition parserintf.h:85
CodeParserOptions & setEndLine(int lineNr)
Definition parserintf.h:103
int startLine() const
Definition parserintf.h:83
const MemberDef * m_memberDef
Definition parserintf.h:128
const Definition * m_searchCtx
Definition parserintf.h:130
CodeParserOptions & setMemberDef(const MemberDef *md)
Definition parserintf.h:109
bool collectXRefs() const
Definition parserintf.h:89
const FileDef * m_fileDef
Definition parserintf.h:124
CodeParserOptions & setExample(bool isExample, const DString &name)
Associate this comment block with a given example.
Definition parserintf.h:94
CodeParserOptions & setShowLineNumbers(bool enable)
Definition parserintf.h:112
CodeParserOptions & setFileDef(const FileDef *fd)
Definition parserintf.h:97
CodeParserFactory codeParserFactory
Definition parserintf.h:192
OutlineParserFactory outlineParserFactory
Definition parserintf.h:191
ParserPair(OutlineParserFactory opf, const CodeParserFactory &cpf, const DString &pn)
Definition parserintf.h:186
This file contains a number of basic enums and types.
SrcLangExt
Definition types.h:207