yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
6.4 KiB202 linesraw
1// slang-doc.h
2#ifndef SLANG_DOC_EXTRACTOR_H
3#define SLANG_DOC_EXTRACTOR_H
4
5#include "../core/slang-basic.h"
6#include "slang-lexer.h"
7#include "slang-source-loc.h"
8
9namespace Slang
10{
11
12enum class MarkupVisibility : uint8_t
13{
14    Public,   ///< Always available
15    Internal, ///< Can be available in more verbose 'internal' documentation
16    Hidden,   ///< Not generally available
17};
18
19/* Extracts 'markup' from comments in Slang source core. The comments are extracted and associated
20in declarations. The association is held in DocMarkup type. The comment style follows the doxygen
21style */
22class DocMarkupExtractor
23{
24public:
25    typedef uint32_t MarkupFlags;
26    struct MarkupFlag
27    {
28        enum Enum : MarkupFlags
29        {
30            Before = 0x1,
31            After = 0x2,
32            IsMultiToken = 0x4, ///< Can use more than one token
33            IsBlock = 0x8,      ///<
34        };
35    };
36
37    // NOTE! Don't change order without fixing isBefore and isAfter
38    enum class MarkupType
39    {
40        None,
41
42        BlockBefore,     /// /**  */ or /*!  */.
43        LineBangBefore,  /// //! Can be multiple lines
44        LineSlashBefore, /// /// Can be multiple lines
45        OrdinaryBlockBefore,
46        OrdinaryLineBefore,
47
48        BlockAfter,     /// /*!< */ or /**< */
49        LineBangAfter,  /// //!< Can be multiple lines
50        LineSlashAfter, /// ///< Can be multiple lines
51        OrdinaryLineAfter,
52    };
53
54    static bool isBefore(MarkupType type)
55    {
56        return Index(type) >= Index(MarkupType::BlockBefore) &&
57               Index(type) <= Index(MarkupType::OrdinaryLineBefore);
58    }
59    static bool isAfter(MarkupType type) { return Index(type) >= Index(MarkupType::BlockAfter); }
60
61    struct IndexRange
62    {
63        SLANG_FORCE_INLINE Index getCount() const { return end - start; }
64
65        Index start;
66        Index end;
67    };
68
69    enum class Location
70    {
71        None, ///< No defined location
72        Before,
73        AfterParam,        ///< Can have trailing , or )
74        AfterSemicolon,    ///< Can have a trailing ;
75        AfterEnumCase,     ///< Can have a , or before }
76        AfterGenericParam, ///< Can have trailing , or >
77    };
78
79    static bool isAfter(Location location)
80    {
81        return Index(location) >= Index(Location::AfterParam);
82    }
83    static bool isBefore(Location location) { return location == Location::Before; }
84
85    struct FoundMarkup
86    {
87        void reset()
88        {
89            location = Location::None;
90            type = MarkupType::None;
91            range = IndexRange{0, 0};
92        }
93
94        Location location = Location::None;
95        MarkupType type = MarkupType::None;
96        IndexRange range;
97    };
98
99    enum SearchStyle
100    {
101        None,         ///< Cannot be searched for
102        EnumCase,     ///< An enum case
103        Param,        ///< A parameter in a function/method
104        Variable,     ///< A variable-like declaration
105        Before,       ///< Only allows before
106        Function,     ///< Function/method
107        GenericParam, ///< Generic parameter
108        Attribute,    ///< Attribute definition
109    };
110
111    /// An input search item
112    struct SearchItemInput
113    {
114        SourceLoc sourceLoc;
115        SearchStyle searchStyle; ///< The search style when looking for an item
116    };
117
118    /// The items will be in source order
119    struct SearchItemOutput
120    {
121        Index viewIndex;            ///< Index into the array of views on the output
122        Index inputIndex;           ///< The index to this item in the input
123        String text;                ///< The found text
124        MarkupVisibility visibilty; ///< Visibility of the item
125    };
126
127    struct FindInfo
128    {
129        SourceView* sourceView; ///< The source view the tokens were generated from
130        TokenList* tokenList;   ///< The token list
131        Index tokenIndex;       ///< The token index location (where searches start from)
132        Index lineIndex;        ///< The line number for the decl
133    };
134
135    void setSearchInOrdinaryComments(bool val) { m_searchInOrindaryComments = val; }
136
137    /// Extracts 'markup' doc information for the specified input items
138    /// The output is placed in out - with the items now in the source order *not* the order of the
139    /// input items The inputIndex on the output holds the input item index The outViews holds the
140    /// views specified in viewIndex in the output, which may be useful for determining where the
141    /// documentation was placed in source
142    SlangResult extract(
143        const SearchItemInput* inputItems,
144        Index inputCount,
145        SourceManager* sourceManager,
146        DiagnosticSink* sink,
147        List<SourceView*>& outViews,
148        List<SearchItemOutput>& out);
149
150    static MarkupFlags getFlags(MarkupType type);
151    static MarkupType findMarkupType(const Token& tok);
152    static UnownedStringSlice removeStart(MarkupType type, const UnownedStringSlice& comment);
153
154protected:
155    /// returns SLANG_E_NOT_FOUND if not found, SLANG_OK on success else an error
156    SlangResult _findMarkup(const FindInfo& info, Location location, FoundMarkup& out);
157
158    /// Locations are processed in order, and the first successful used. If found in another
159    /// location will issue a warning. returns SLANG_E_NOT_FOUND if not found, SLANG_OK on success
160    /// else an error
161    SlangResult _findFirstMarkup(
162        const FindInfo& info,
163        const Location* locs,
164        Index locCount,
165        FoundMarkup& out,
166        Index& outIndex);
167
168    SlangResult _findMarkup(
169        const FindInfo& info,
170        const Location* locs,
171        Index locCount,
172        FoundMarkup& out);
173
174    /// Given the decl, the token stream, and the decls tokenIndex, try to find some associated
175    /// markup
176    SlangResult _findMarkup(const FindInfo& info, SearchStyle searchStyle, FoundMarkup& out);
177
178    /// Given a found markup location extracts the contents of the tokens into out
179    SlangResult _extractMarkup(
180        const FindInfo& info,
181        const FoundMarkup& foundMarkup,
182        StringBuilder& out);
183
184    /// Given a location, try to find the first token index that could potentially be markup
185    /// Will return -1 if not found
186    Index _findStartIndex(const FindInfo& info, Location location);
187
188    /// True if the tok is 'on' lineIndex. Interpretation of 'on' depends on the markup type.
189    static bool _isTokenOnLineIndex(
190        SourceView* sourceView,
191        MarkupType type,
192        const Token& tok,
193        Index lineIndex);
194
195    DiagnosticSink* m_sink;
196
197    bool m_searchInOrindaryComments = false;
198};
199
200} // namespace Slang
201
202#endif