mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-07-07 03:31:52 +00:00
ZoneCodeGenerator: Remove the const keyword from typename but instead save whether a reference is cosnt in the typedeclaration
This commit is contained in:
@ -8,89 +8,126 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Tests
|
||||
{
|
||||
class TestTypedef : AbstractTokenTest<IHeaderParserState>
|
||||
{
|
||||
private const string PointerToArrayVariant = "pointertoarray";
|
||||
private const string ArrayOfPointersVariant = "arrayofpointers";
|
||||
private const string TypeNameTokens = "typename";
|
||||
private const string PointerTokens = "pointer";
|
||||
private const string NameToken = "name";
|
||||
private const string ArrayTokens = "array";
|
||||
private const string AlignValueToken = "alignValue";
|
||||
private const string TokenConst = "constToken";
|
||||
|
||||
private static readonly TokenMatcher[] arrayOfPointersMatchers = {
|
||||
new MatcherLiteral("typedef"),
|
||||
private const string TagAlign = "alignTag";
|
||||
private const string TagArrayOfPointers = "arrayOfPointersTag";
|
||||
private const string TagPointerToArray = "pointerToArrayTag";
|
||||
|
||||
private static readonly TokenMatcher align = new MatcherGroupAnd(
|
||||
new MatcherLiteral("__declspec", "("),
|
||||
new MatcherLiteral("align", "("),
|
||||
new MatcherNumber().WithName(AlignValueToken),
|
||||
new MatcherLiteral(")"),
|
||||
new MatcherLiteral(")")
|
||||
).WithTag(TagAlign);
|
||||
|
||||
private static readonly TokenMatcher arrayOfPointers = new MatcherGroupAnd(
|
||||
new MatcherGroupOptional(new MatcherLiteral("const").WithName(TokenConst)),
|
||||
new MatcherTypename().WithName(TypeNameTokens),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, new MatcherLiteral("*").WithName(PointerTokens)),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple,
|
||||
new MatcherLiteral("*").WithName(PointerTokens)),
|
||||
new MatcherName().WithName(NameToken),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, new MatcherArray().WithName(ArrayTokens)),
|
||||
new MatcherLiteral(";").WithName(ArrayOfPointersVariant)
|
||||
};
|
||||
new MatcherLiteral(";")
|
||||
).WithTag(TagArrayOfPointers);
|
||||
|
||||
private static readonly TokenMatcher[] pointerToArrayMatchers = {
|
||||
new MatcherLiteral("typedef"),
|
||||
private static readonly TokenMatcher pointerToArray = new MatcherGroupAnd(
|
||||
new MatcherGroupOptional(new MatcherLiteral("const").WithName(TokenConst)),
|
||||
new MatcherTypename().WithName(TypeNameTokens),
|
||||
new MatcherLiteral("("),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, new MatcherLiteral("*").WithName(PointerTokens)),
|
||||
new MatcherLiteral("("),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple,
|
||||
new MatcherLiteral("*").WithName(PointerTokens)),
|
||||
new MatcherName().WithName(NameToken),
|
||||
new MatcherLiteral(")"),
|
||||
new MatcherLiteral(")"),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, new MatcherArray().WithName(ArrayTokens)),
|
||||
new MatcherLiteral(";").WithName(PointerToArrayVariant)
|
||||
};
|
||||
new MatcherLiteral(";")
|
||||
).WithTag(TagPointerToArray);
|
||||
|
||||
private static readonly TokenMatcher[] matchers =
|
||||
{
|
||||
new MatcherLiteral("typedef"),
|
||||
new MatcherGroupOptional(new MatcherWithTag(TagAlign)),
|
||||
new MatcherGroupOr(
|
||||
new MatcherGroupAnd(arrayOfPointersMatchers),
|
||||
new MatcherGroupAnd(pointerToArrayMatchers)
|
||||
)
|
||||
new MatcherWithTag(TagArrayOfPointers),
|
||||
new MatcherWithTag(TagPointerToArray)
|
||||
)
|
||||
};
|
||||
|
||||
public TestTypedef() : base(matchers)
|
||||
{
|
||||
|
||||
AddTaggedMatcher(align);
|
||||
AddTaggedMatcher(arrayOfPointers);
|
||||
AddTaggedMatcher(pointerToArray);
|
||||
}
|
||||
|
||||
protected override void ProcessMatch(IHeaderParserState state)
|
||||
{
|
||||
// Check if a custom alignment has been defined
|
||||
int? alignmentOverride = null;
|
||||
if (PeekTag().Equals(TagAlign))
|
||||
{
|
||||
NextTag();
|
||||
alignmentOverride = int.Parse(NextMatch(AlignValueToken));
|
||||
}
|
||||
|
||||
// See which variant has been used
|
||||
var isArrayOfPointersVariant = NextTag().Equals(TagArrayOfPointers);
|
||||
|
||||
// Get name of typedef and the amount of pointer depth it has
|
||||
var name = NextMatch(NameToken);
|
||||
var pointerDepth = GetMatcherTokenCount(PointerTokens);
|
||||
|
||||
// Find referenced type
|
||||
var typeName = NextMatch(TypeNameTokens);
|
||||
var type = state.FindType(typeName);
|
||||
|
||||
if (type == null)
|
||||
throw new TestFailedException($"Could not find type '{typeName}' of typedef '{name}'.");
|
||||
|
||||
|
||||
// Collect all array sizes that have been specified
|
||||
var arraySize = new int[GetMatcherTokenCount(ArrayTokens)];
|
||||
string arrayToken;
|
||||
var index = 0;
|
||||
while((arrayToken = NextMatch(ArrayTokens)) != null)
|
||||
while ((arrayToken = NextMatch(ArrayTokens)) != null)
|
||||
{
|
||||
if (!int.TryParse(arrayToken, out arraySize[index++]))
|
||||
throw new TestFailedException($"Array size '{arrayToken}' is not numeric.");
|
||||
}
|
||||
|
||||
var references = new List<ReferenceType>();
|
||||
|
||||
if (HasMatcherTokens(PointerToArrayVariant))
|
||||
if (isArrayOfPointersVariant)
|
||||
{
|
||||
foreach (var array in arraySize)
|
||||
references.Add(new ReferenceTypeArray(array));
|
||||
|
||||
for (var i = 0; i < pointerDepth; i++)
|
||||
references.Add(new ReferenceTypePointer());
|
||||
|
||||
foreach(var array in arraySize)
|
||||
references.Add(new ReferenceTypeArray(array));
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach(var array in arraySize)
|
||||
references.Add(new ReferenceTypeArray(array));
|
||||
|
||||
for (var i = 0; i < pointerDepth; i++)
|
||||
references.Add(new ReferenceTypePointer());
|
||||
|
||||
foreach (var array in arraySize)
|
||||
references.Add(new ReferenceTypeArray(array));
|
||||
}
|
||||
|
||||
var typeDeclaration = new TypeDeclaration(type, references);
|
||||
var typedef = new DataTypeTypedef(state.CurrentNamespace.ToString(), name, typeDeclaration);
|
||||
|
||||
|
||||
var typeDeclaration = new TypeDeclaration(type, references)
|
||||
{
|
||||
IsConst = HasMatcherTokens(TokenConst)
|
||||
};
|
||||
var typedef = new DataTypeTypedef(state.CurrentNamespace.ToString(), name, typeDeclaration)
|
||||
{
|
||||
|
||||
AlignmentOverride = alignmentOverride
|
||||
};
|
||||
|
||||
state.AddDataType(typedef);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -17,8 +17,10 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Tests
|
||||
private const string NameToken = "name";
|
||||
private const string ArrayTokens = "array";
|
||||
private const string BitSizeToken = "bitsize";
|
||||
private const string TokenConst = "constToken";
|
||||
|
||||
private static readonly TokenMatcher[] arrayOfPointersMatchers = {
|
||||
new MatcherGroupOptional(new MatcherLiteral("const").WithTag(TokenConst)),
|
||||
new MatcherTypename().WithName(TypeNameToken),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.ZeroOneMultiple, new MatcherLiteral("*").WithName(PointerTokens)),
|
||||
new MatcherName().WithName(NameToken),
|
||||
@ -31,6 +33,7 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Tests
|
||||
};
|
||||
|
||||
private static readonly TokenMatcher[] pointerToArrayMatchers = {
|
||||
new MatcherGroupOptional(new MatcherLiteral("const").WithTag(TokenConst)),
|
||||
new MatcherTypename().WithName(TypeNameToken),
|
||||
new MatcherLiteral("("),
|
||||
new MatcherGroupLoop(MatcherGroupLoop.LoopMode.OneMultiple, new MatcherLiteral("*").WithName(PointerTokens)),
|
||||
@ -97,7 +100,11 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Tests
|
||||
references.Add(new ReferenceTypePointer());
|
||||
}
|
||||
|
||||
var typeDeclaration = bitSize == null ? new TypeDeclaration(type, references) : new TypeDeclaration(type, bitSize.Value, references);
|
||||
var typeDeclaration = new TypeDeclaration(type, references)
|
||||
{
|
||||
CustomBitSize = bitSize,
|
||||
IsConst = HasMatcherTokens(TokenConst)
|
||||
};
|
||||
|
||||
var variable = new Variable(name, typeDeclaration);
|
||||
|
||||
|
@ -8,12 +8,6 @@ namespace ZoneCodeGenerator.Parsing.Matching.Matchers
|
||||
{
|
||||
private static readonly Regex nameRegex = new Regex(@"^[a-zA-Z_$][a-zA-Z0-9_$]*$");
|
||||
|
||||
private static readonly string[] typenamePrefixes = {
|
||||
"const",
|
||||
"signed",
|
||||
"unsigned"
|
||||
};
|
||||
|
||||
private int MatchTypenameExtension(MatchingContext context, int tokenOffset)
|
||||
{
|
||||
if (!":".Equals(context.Lexer.PeekToken(tokenOffset++)))
|
||||
@ -31,20 +25,18 @@ namespace ZoneCodeGenerator.Parsing.Matching.Matchers
|
||||
var startTokenOffset = tokenOffset;
|
||||
|
||||
var currentPart = lexer.PeekToken(tokenOffset++);
|
||||
if ("unsigned".Equals(currentPart)
|
||||
|| "signed".Equals(currentPart))
|
||||
{
|
||||
typename.Append(currentPart);
|
||||
typename.Append(' ');
|
||||
currentPart = lexer.PeekToken(tokenOffset++);
|
||||
}
|
||||
|
||||
if (!nameRegex.IsMatch(currentPart))
|
||||
return new MatchingResult(false, 0);
|
||||
typename.Append(currentPart);
|
||||
|
||||
while (typenamePrefixes.Contains(currentPart))
|
||||
{
|
||||
currentPart = lexer.PeekToken(tokenOffset++);
|
||||
|
||||
if (!nameRegex.IsMatch(currentPart))
|
||||
return new MatchingResult(false, 0);
|
||||
typename.Append(' ');
|
||||
typename.Append(currentPart);
|
||||
}
|
||||
|
||||
if ("long".Equals(currentPart))
|
||||
{
|
||||
currentPart = lexer.PeekToken(tokenOffset);
|
||||
@ -55,17 +47,19 @@ namespace ZoneCodeGenerator.Parsing.Matching.Matchers
|
||||
typename.Append(currentPart);
|
||||
}
|
||||
}
|
||||
|
||||
var extensionLength = MatchTypenameExtension(context, tokenOffset);
|
||||
while (extensionLength > 0)
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < extensionLength; i++)
|
||||
var extensionLength = MatchTypenameExtension(context, tokenOffset);
|
||||
while (extensionLength > 0)
|
||||
{
|
||||
typename.Append(lexer.PeekToken(tokenOffset + i));
|
||||
}
|
||||
for (var i = 0; i < extensionLength; i++)
|
||||
{
|
||||
typename.Append(lexer.PeekToken(tokenOffset + i));
|
||||
}
|
||||
|
||||
tokenOffset += extensionLength;
|
||||
extensionLength = MatchTypenameExtension(context, tokenOffset);
|
||||
tokenOffset += extensionLength;
|
||||
extensionLength = MatchTypenameExtension(context, tokenOffset);
|
||||
}
|
||||
}
|
||||
|
||||
SetMatcherOutput(typename.ToString());
|
||||
|
Reference in New Issue
Block a user