yum-mirror/slang

Making it easier to work with shaders

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

Lauro OyenMove c++ parsing code from slang-cpp-extractor to static library (#5675)eaa8dcfcc

master
3.4 KiB112 linesraw
1#pragma once
2
3#include "compiler-core/slang-lexer.h"
4#include "diagnostics.h"
5#include "identifier-lookup.h"
6#include "node-tree.h"
7#include "node.h"
8
9namespace CppParse
10{
11using namespace Slang;
12
13class Parser
14{
15public:
16    typedef uint32_t NodeTypeBitType;
17
18    SlangResult expect(TokenType type, Token* outToken = nullptr);
19
20    bool advanceIfMarker(Token* outToken = nullptr);
21    bool advanceIfToken(TokenType type, Token* outToken = nullptr);
22    bool advanceIfStyle(IdentifierStyle style, Token* outToken = nullptr);
23
24    SlangResult pushAnonymousNamespace();
25    SlangResult pushScope(ScopeNode* node);
26    SlangResult consumeToClosingBrace(const Token* openBraceToken = nullptr);
27    SlangResult popScope();
28
29    /// Parse the contents of the source file
30    SlangResult parse(SourceOrigin* sourceOrigin, const Options* options);
31
32    void setKindEnabled(Node::Kind kind, bool isEnabled = true);
33    bool isTypeEnabled(Node::Kind kind)
34    {
35        return (m_nodeTypeEnabled & (NodeTypeBitType(1) << int(kind))) != 0;
36    }
37
38    void setKindsEnabled(const Node::Kind* kinds, Index kindsCount, bool isEnabled = true);
39
40    Parser(NodeTree* nodeTree, DiagnosticSink* sink);
41
42protected:
43    static Node::Kind _toNodeKind(IdentifierStyle style);
44
45    bool _isMarker(const UnownedStringSlice& name);
46
47    SlangResult _maybeConsumeScope();
48
49    SlangResult _parsePreDeclare();
50    SlangResult _parseTypeSet();
51
52    SlangResult _maybeParseNode(Node::Kind kind);
53    SlangResult _maybeParseContained(Node** outNode);
54
55    SlangResult _parseTypeDef();
56    SlangResult _parseEnum();
57    SlangResult _parseMarker();
58    SlangResult _parseSpecialMacro();
59
60    SlangResult _maybeParseType(List<Token>& outToks, Token& outName);
61    SlangResult _maybeParseType(UnownedStringSlice& outType, Token& outName);
62    SlangResult _maybeParseType(Index& ioTemplateDepth, TokenReader::ParsingCursor& outCursor);
63
64    SlangResult _parseExpression(List<Token>& outExprTokens);
65
66    SlangResult _maybeParseTemplateArgs(Index& ioTemplateDepth);
67    SlangResult _maybeParseTemplateArg(Index& ioTemplateDepth);
68
69    /// Parse balanced - if a sink is set will report to that sink
70    SlangResult _parseBalanced(DiagnosticSink* sink);
71
72    bool _isCtor();
73
74    /// Concatenate all tokens from start to the current position
75    UnownedStringSlice _concatTokens(TokenReader::ParsingCursor start);
76    UnownedStringSlice _concatTokens(const Token* toks, Index toksCount);
77
78    UnownedStringSlice _concatType(
79        TokenReader::ParsingCursor start,
80        TokenReader::ParsingCursor nameCursor);
81
82    void _getTypeTokens(
83        TokenReader::ParsingCursor start,
84        TokenReader::ParsingCursor nameCursor,
85        List<Token>& outToks);
86
87    /// Consume what looks like a template definition
88    SlangResult _consumeTemplate();
89    SlangResult _maybeConsume(IdentifierStyle style);
90
91    SlangResult _consumeToSync();
92    /// Consumes balanced parens. Will return an error if not matched. Assumes starts on opening (
93    SlangResult _consumeBalancedParens();
94
95    NodeTypeBitType m_nodeTypeEnabled;
96
97    TokenList m_tokenList;
98    TokenReader m_reader;
99
100    List<ScopeNode*> m_scopeStack;
101
102    ScopeNode* m_currentScope;    ///< The current scope being processed
103    SourceOrigin* m_sourceOrigin; ///< The source origin that all tokens are in
104
105    DiagnosticSink* m_sink; ///< Diagnostic sink
106
107    NodeTree* m_nodeTree; ///< Shared state between parses. Nodes will be added to this
108
109    const Options* m_options;
110};
111
112} // namespace CppParse