RavEngine
Loading...
Searching...
No Matches
NvFlowPreprocessor.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2014-2022 NVIDIA Corporation. All rights reserved.
26
27
28#pragma once
29
30#include "NvFlowString.h"
31
33
35{
36 NvFlowUint64 begin;
37 NvFlowUint64 end;
38};
39
40enum NvFlowPreprocessorTokenType
41{
42 eNvFlowPreprocessorTokenType_unknown = 0, // unclassified
43 eNvFlowPreprocessorTokenType_whitespace, //
44 eNvFlowPreprocessorTokenType_newline, // \n
45 eNvFlowPreprocessorTokenType_comment, // // comment
46 eNvFlowPreprocessorTokenType_name, // alpha_1234
47 eNvFlowPreprocessorTokenType_number, // 1234
48 eNvFlowPreprocessorTokenType_string, // "string"
49 eNvFlowPreprocessorTokenType_char, // 's'
50
51 eNvFlowPreprocessorTokenType_pound, // #
52 eNvFlowPreprocessorTokenType_comma, // ,
53 eNvFlowPreprocessorTokenType_period, // .
54 eNvFlowPreprocessorTokenType_semicolon, // ;
55 eNvFlowPreprocessorTokenType_colon, // :
56 eNvFlowPreprocessorTokenType_equals, // =
57 eNvFlowPreprocessorTokenType_asterisk, // *
58
59 eNvFlowPreprocessorTokenType_leftParenthesis, // (
60 eNvFlowPreprocessorTokenType_rightParenthesis, // )
61 eNvFlowPreprocessorTokenType_leftBracket, // [
62 eNvFlowPreprocessorTokenType_rightBracket, // ]
63 eNvFlowPreprocessorTokenType_leftCurlyBrace, // {
64 eNvFlowPreprocessorTokenType_rightCurlyBrace, // }
65
66 eNvFlowPreprocessorTokenType_lessThan, // <
67 eNvFlowPreprocessorTokenType_greaterThan, // >
68
69 eNvFlowPreprocessorTokenType_anyWhitespace, // For delimiter usage, aligns with NvFlowPreprocessorTokenIsWhitespace()
70
71 eNvFlowPreprocessorTokenType_count,
72 eNvFlowPreprocessorTokenType_maxEnum = 0x7FFFFFFF
73};
74
76{
77 NvFlowPreprocessorTokenType type;
78 const char* str;
79};
80
81NV_FLOW_INLINE NvFlowBool32 NvFlowPreprocessorTokenIsWhitespace(const NvFlowPreprocessorToken token)
82{
83 return token.type == eNvFlowPreprocessorTokenType_whitespace ||
84 token.type == eNvFlowPreprocessorTokenType_newline ||
85 token.type == eNvFlowPreprocessorTokenType_comment;
86}
87
88NV_FLOW_INLINE void NvFlowPreprocessorSkipWhitespaceTokens(NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens)
89{
90 while ((*pTokenIdx) < numTokens && NvFlowPreprocessorTokenIsWhitespace(tokens[(*pTokenIdx)]))
91 {
92 (*pTokenIdx)++;
93 }
94}
95
96enum NvFlowPreprocessorType
97{
98 eNvFlowPreprocessorType_constant = 0, // name
99 eNvFlowPreprocessorType_statement = 1, // name arg0 arg1;
100 eNvFlowPreprocessorType_function = 2, // name(arg0, arg1, arg2)
101 eNvFlowPreprocessorType_index = 3, // name[arg0] or name[arg0]= arg1 arg2 arg3;
102 eNvFlowPreprocessorType_attribute = 4, // [name(arg0, arg1, arg2)]
103 eNvFlowPreprocessorType_line = 5, // #name arg0 \n
104 eNvFlowPreprocessorType_body = 6, // name <arg0, arg1> arg2 arg3(arg4, arg5) { arg6; arg7; }
105 eNvFlowPreprocessorType_templateInstance = 7, // name<arg0, arg1>
106 eNvFlowPreprocessorType_statementComma = 8, // "name arg0," or "name arg0)"
107
108 eNvFlowPreprocessorType_maxEnum = 0x7FFFFFFF
109};
110
112{
113 const char* name;
114 const char* value;
115};
116
118{
119 const char* name;
120 NvFlowPreprocessorType type;
121 void* userdata;
122 char*(*substitute)(NvFlowPreprocessor* ptr, void* userdata, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens);
123 NvFlowBool32 allowRecursion;
124};
125
126enum NvFlowPreprocessorMode
127{
128 eNvFlowPreprocessorMode_default = 0, // Input string evaluated and substitution evaluated, no recursion
129 eNvFlowPreprocessorMode_singlePass = 1, // Input string evaluated once, no substitution evaluation
130 eNvFlowPreprocessorMode_disable_passthrough = 2, // Do not passthrough strings
131
132 eNvFlowPreprocessorMode_maxEnum = 0x7FFFFFFF
133};
134
135NvFlowPreprocessor* NvFlowPreprocessorCreate(NvFlowStringPool* pool);
136
137void NvFlowPreprocessorDestroy(NvFlowPreprocessor* ptr);
138
139void NvFlowPreprocessorReset(NvFlowPreprocessor* ptr);
140
141void NvFlowPreprocessorSetMode(NvFlowPreprocessor* ptr, NvFlowPreprocessorMode mode);
142
143NvFlowPreprocessorMode NvFlowPreprocessorGetMode(NvFlowPreprocessor* ptr);
144
145NvFlowStringPool* NvFlowPreprocessorStringPool(NvFlowPreprocessor* ptr);
146
147void NvFlowPreprocessorAddConstants(NvFlowPreprocessor* ptr, NvFlowUint64 numConstants, const NvFlowPreprocessorConstant* constants);
148
149void NvFlowPreprocessorAddFunctions(NvFlowPreprocessor* ptr, NvFlowUint64 numFunctions, const NvFlowPreprocessorFunction* functions);
150
151NvFlowPreprocessorRange NvFlowPreprocessorExtractTokensDelimitedN(NvFlowPreprocessor* ptr, NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens, NvFlowUint64 numDelimiters, const NvFlowPreprocessorTokenType* delimiters);
152
153NvFlowPreprocessorRange NvFlowPreprocessorExtractTokensDelimited(NvFlowPreprocessor* ptr, NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens, NvFlowPreprocessorTokenType delimiter);
154
155const char* NvFlowPreprocessorExtractDelimited(NvFlowPreprocessor* ptr, NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens, NvFlowPreprocessorTokenType delimiter);
156
157const char* NvFlowPreprocessorExtractDelimitedN(NvFlowPreprocessor* ptr, NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens, NvFlowUint64 numDelimiters, const NvFlowPreprocessorTokenType* delimiters);
158
159const char* NvFlowPreprocessorExtractDelimitedPreserve(NvFlowPreprocessor* ptr, NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens, NvFlowPreprocessorTokenType delimiter);
160
161const char* NvFlowPreprocessorExtractIfType(NvFlowPreprocessor* ptr, NvFlowUint64* pTokenIdx, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens, NvFlowPreprocessorTokenType type);
162
163const char* NvFlowPreprocessorConcatTokens(NvFlowPreprocessor* ptr, const NvFlowPreprocessorToken* tokens, NvFlowUint64 numTokens);
164
165NvFlowBool32 NvFlowPreprocessorFindKeyInSource(NvFlowPreprocessor* ptr, const NvFlowPreprocessorToken* keyTokens, NvFlowUint64 keyTokenCount, const NvFlowPreprocessorToken* sourceTokens, NvFlowUint64 sourceTokenCount, NvFlowUint64* pSourceIndex);
166
167char* NvFlowPreprocessorExecute(NvFlowPreprocessor* ptr, const char* input);
168
169void NvFlowPreprocessorTokenize(NvFlowPreprocessor* ptr, const char* input, NvFlowUint64* pTotalTokens, NvFlowPreprocessorToken** pTokens);
170
171enum NvFlowPreprocessorGlobalType
172{
173 eNvFlowPreprocessorGlobalType_unknown = 0, // Unknown global type
174 eNvFlowPreprocessorGlobalType_statement = 1, // ConstantBuffer<Params> gParams;
175 eNvFlowPreprocessorGlobalType_function = 2, // returnType functionName(arg1, arg2, arg3) { [functionbody] }
176 eNvFlowPreprocessorGlobalType_attribute = 3, // [name(arg0, arg1, arg2)]
177 eNvFlowPreprocessorGlobalType_line = 4, // #define CONSTANT \n
178
179 eNvFlowPreprocessorGlobalType_maxEnum = 0x7FFFFFFF
180};
181
182char* NvFlowPreprocessorExecuteGlobal(NvFlowPreprocessor* ptr, const char* input, void* userdata, char*(*substitute)(NvFlowPreprocessor* ptr, void* userdata, NvFlowPreprocessorGlobalType globalType, NvFlowUint64 numTokens, const NvFlowPreprocessorToken* tokens));
Definition NvFlowPreprocessor.h:112
Definition NvFlowPreprocessor.h:118
Definition NvFlowPreprocessor.h:35
Definition NvFlowPreprocessor.h:76
Definition NvFlowPreprocessor.cpp:44
Definition NvFlowString.cpp:35