Add support for parsing inheritance with preprocessor blocks inside it

This commit is contained in:
Wojtek Figat
2025-08-29 21:03:04 +02:00
parent 785649f9d5
commit 5222f1d35c
2 changed files with 123 additions and 104 deletions

View File

@@ -13,6 +13,7 @@ namespace Flax.Build.Bindings
public FileInfo File;
public Tokenizer Tokenizer;
public ApiTypeInfo ScopeInfo;
public NativeCpp.BuildOptions ModuleOptions;
public AccessLevel CurrentAccessLevel;
public Stack<ApiTypeInfo> ScopeTypeStack;
public Stack<AccessLevel> ScopeAccessStack;
@@ -534,6 +535,12 @@ namespace Flax.Build.Bindings
}
if (token.Type == TokenType.LeftCurlyBrace)
break;
if (token.Type == TokenType.Preprocessor)
{
OnPreProcessorToken(ref context, ref token);
while (token.Type == TokenType.Newline)
token = context.Tokenizer.NextToken();
}
if (token.Type == TokenType.Colon)
{
token = context.Tokenizer.ExpectToken(TokenType.Colon);

View File

@@ -245,6 +245,7 @@ namespace Flax.Build.Bindings
File = fileInfo,
Tokenizer = tokenizer,
ScopeInfo = null,
ModuleOptions = moduleOptions,
CurrentAccessLevel = AccessLevel.Public,
ScopeTypeStack = new Stack<ApiTypeInfo>(),
ScopeAccessStack = new Stack<AccessLevel>(),
@@ -380,6 +381,40 @@ namespace Flax.Build.Bindings
// Handle preprocessor blocks
if (token.Type == TokenType.Preprocessor)
{
OnPreProcessorToken(ref context, ref token);
}
// Scope tracking
if (token.Type == TokenType.LeftCurlyBrace)
{
context.ScopeTypeStack.Push(scopeType);
context.ScopeInfo = context.ScopeTypeStack.Peek();
scopeType = null;
}
else if (token.Type == TokenType.RightCurlyBrace)
{
context.ScopeTypeStack.Pop();
if (context.ScopeTypeStack.Count == 0)
throw new Exception($"Mismatch of the {{}} braces pair in file '{fileInfo.Name}' at line {tokenizer.CurrentLine}.");
context.ScopeInfo = context.ScopeTypeStack.Peek();
if (context.ScopeInfo is FileInfo)
context.CurrentAccessLevel = AccessLevel.Public;
}
prevToken = token;
}
}
catch (Exception ex)
{
Log.Error($"Failed to parse '{fileInfo.Name}' file to generate bindings.");
Log.Exception(ex);
throw;
}
}
private static void OnPreProcessorToken(ref ParsingContext context, ref Token token)
{
var tokenizer = context.Tokenizer;
token = tokenizer.NextToken();
switch (token.Value)
{
@@ -418,9 +453,9 @@ namespace Flax.Build.Bindings
if (negate)
tokenValue = tokenValue.Substring(1);
tokenValue = ReplacePreProcessorDefines(tokenValue, context.PreprocessorDefines);
tokenValue = ReplacePreProcessorDefines(tokenValue, moduleOptions.PublicDefinitions);
tokenValue = ReplacePreProcessorDefines(tokenValue, moduleOptions.PrivateDefinitions);
tokenValue = ReplacePreProcessorDefines(tokenValue, moduleOptions.CompileEnv.PreprocessorDefinitions);
tokenValue = ReplacePreProcessorDefines(tokenValue, context.ModuleOptions.PublicDefinitions);
tokenValue = ReplacePreProcessorDefines(tokenValue, context.ModuleOptions.PrivateDefinitions);
tokenValue = ReplacePreProcessorDefines(tokenValue, context.ModuleOptions.CompileEnv.PreprocessorDefinitions);
tokenValue = tokenValue.Replace("false", "0");
tokenValue = tokenValue.Replace("true", "1");
tokenValue = tokenValue.Replace("||", "|");
@@ -458,7 +493,7 @@ namespace Flax.Build.Bindings
// Skip chunk of code of condition fails
if (condition != "1")
{
ParsePreprocessorIf(fileInfo, tokenizer, ref token);
ParsePreprocessorIf(context.File, tokenizer, ref token);
}
break;
@@ -476,41 +511,18 @@ namespace Flax.Build.Bindings
// Check condition
define = define.Trim();
if (!context.PreprocessorDefines.ContainsKey(define) && !moduleOptions.CompileEnv.PreprocessorDefinitions.Contains(define))
if (!context.PreprocessorDefines.ContainsKey(define) && !context.ModuleOptions.CompileEnv.PreprocessorDefinitions.Contains(define))
{
ParsePreprocessorIf(fileInfo, tokenizer, ref token);
ParsePreprocessorIf(context.File, tokenizer, ref token);
}
break;
}
}
}
// Scope tracking
if (token.Type == TokenType.LeftCurlyBrace)
case "endif":
{
context.ScopeTypeStack.Push(scopeType);
context.ScopeInfo = context.ScopeTypeStack.Peek();
scopeType = null;
token = tokenizer.NextToken(true);
break;
}
else if (token.Type == TokenType.RightCurlyBrace)
{
context.ScopeTypeStack.Pop();
if (context.ScopeTypeStack.Count == 0)
throw new Exception($"Mismatch of the {{}} braces pair in file '{fileInfo.Name}' at line {tokenizer.CurrentLine}.");
context.ScopeInfo = context.ScopeTypeStack.Peek();
if (context.ScopeInfo is FileInfo)
context.CurrentAccessLevel = AccessLevel.Public;
}
prevToken = token;
}
}
catch (Exception ex)
{
Log.Error($"Failed to parse '{fileInfo.Name}' file to generate bindings.");
Log.Exception(ex);
throw;
}
}