Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suport for FFmpeg 5.1 #211

Merged
merged 2 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<PackageId>$(AssemblyName)</PackageId>
<Version>5.0</Version>
<Version>5.1</Version>
<Authors>Ruslan Balanukhin</Authors>
<Company>Rational Core</Company>
<Product>FFmpeg.AutoGen</Product>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FFmpeg.AutoGen.ClangMacroParser\FFmpeg.AutoGen.ClangMacroParser.csproj" />
</ItemGroup>

</Project>
116 changes: 116 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser.Test/ParserTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using FFmpeg.AutoGen.ClangMacroParser.Expressions;

namespace FFmpeg.AutoGen.ClangMacroParser.Test
{
[TestClass]
public class ParserTest
{
private void CastExpression<T>(IExpression e, params Action<T>[] tests) where T : IExpression
{
if (e is T) tests.ToList().ForEach(test => test((T)e));
Assert.IsInstanceOfType(e, typeof(T));
}

[TestMethod]
public void String()
{
CastExpression<ConstantExpression>(Parser.Parse("\"abc\""), x => Assert.AreEqual("abc", x.Value));
}

[TestMethod]
public void Char()
{
CastExpression<ConstantExpression>(Parser.Parse("\'a\'"), x => Assert.AreEqual('a', x.Value));
}

[TestMethod]
public void Number()
{
CastExpression<ConstantExpression>(Parser.Parse("0"), x => Assert.AreEqual(0, x.Value)); // unit
CastExpression<ConstantExpression>(Parser.Parse("0.23"), x => Assert.AreEqual(0.23d, x.Value));
CastExpression<ConstantExpression>(Parser.Parse("1.23d"), x => Assert.AreEqual(1.23d, x.Value));
CastExpression<ConstantExpression>(Parser.Parse("1.23f"), x => Assert.AreEqual(1.23f, x.Value));
CastExpression<ConstantExpression>(Parser.Parse("0.23"), x => Assert.AreEqual(0.23d, x.Value));
CastExpression<ConstantExpression>(Parser.Parse("1.23d"), x => Assert.AreEqual(1.23d, x.Value));
CastExpression<ConstantExpression>(Parser.Parse("1.23f"), x => Assert.AreEqual(1.23f, x.Value));
CastExpression<ConstantExpression>(Parser.Parse("0x0123456789ABCDEF"), x => Assert.AreEqual(0x0123456789ABCDEF, x.Value)); // long
CastExpression<ConstantExpression>(Parser.Parse("0x80000000"), x => Assert.AreEqual(0x80000000, x.Value)); // unit
CastExpression<ConstantExpression>(Parser.Parse("0x8000000000000000ULL"), x => Assert.AreEqual(0x8000000000000000UL, x.Value)); // unit
}

[TestMethod]
public void Unary()
{
var e = Parser.Parse(@"(-(1))");
CastExpression<UnaryExpression>(e,
x =>
{
Assert.AreEqual(OperationType.Subtract, x.OperationType);
Assert.IsInstanceOfType(x.Operand, typeof(ConstantExpression));
});
}

[TestMethod]
public void CastThenBinary()
{
var e = Parser.Parse(@"((unsigned)(1 - 2))");
CastExpression<CastExpression>(e,
x =>
{
Assert.AreEqual("unsigned", x.TargetType);
Assert.IsInstanceOfType(x.Operand, typeof(BinaryExpression));
});
}

[TestMethod]
public void BinaryThenCast()
{
var e = Parser.Parse(@"A | (ulong)(B)");
CastExpression<BinaryExpression>(e,
x =>
{
Assert.AreEqual(OperationType.Or, x.OperationType);
CastExpression<VariableExpression>(x.Left, y => Assert.AreEqual("A", y.Name));
CastExpression<CastExpression>(x.Right,
y =>
{
Assert.AreEqual("ulong", y.TargetType);
CastExpression<VariableExpression>(y.Operand, z => Assert.AreEqual("B", z.Name));
});
});
}

[TestMethod]
public void BinaryPrecedence()
{
var e = Parser.Parse("1 + 2 * 3");
CastExpression<BinaryExpression>(e,
x =>
{
Assert.AreEqual(OperationType.Add, x.OperationType);
CastExpression<ConstantExpression>(x.Left, y => Assert.AreEqual(1, y.Value));
CastExpression<BinaryExpression>(x.Right,
y =>
{
Assert.AreEqual(OperationType.Multiply, y.OperationType);
CastExpression<ConstantExpression>(y.Left, z => Assert.AreEqual(2, z.Value));
CastExpression<ConstantExpression>(y.Right, z => Assert.AreEqual(3, z.Value));
});
});
}

[TestMethod]
public void Lamda()
{
var e = Parser.Parse(@"MKBETAG(a, b, c, d)((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))");
// todo parsing is incomplete
}

[TestMethod]
public void Enum()
{
var e = Parser.Parse("0x1UL << AVChannel.AV_CHAN_FRONT_LEFT");
// todo finalize test
}
}
}
108 changes: 108 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser.Test/TokenizerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using FFmpeg.AutoGen.ClangMacroParser.Tokenization;

namespace FFmpeg.AutoGen.ClangMacroParser.Test
{
[TestClass]
public class TokenizerTest
{
private Token Token(TokenType tokenType, string value) => new(tokenType, value, 0, value.Length);
private static Token[] Tokenize(string expression) => Tokenizer.Tokenize(expression).ToArray();

private void AssertAreEqual(Token expected, Token actual)
{
Assert.AreEqual(expected.TokenType, actual.TokenType);
Assert.AreEqual(expected.Value, actual.Value);
Assert.AreEqual(expected.Length, actual.Length);
}

[TestMethod]
public void Number()
{
AssertAreEqual(Token(TokenType.Number, ".23"), Tokenize(@".23").First());
AssertAreEqual(Token(TokenType.Number, "1.23"), Tokenize(@"1.23").First());

AssertAreEqual(Token(TokenType.Number, ".23f"), Tokenize(@".23f").First());
AssertAreEqual(Token(TokenType.Number, "1.23f"), Tokenize(@"1.23f").First());

AssertAreEqual(Token(TokenType.Number, "123"), Tokenize(@"123").First());
AssertAreEqual(Token(TokenType.Number, "123ull"), Tokenize(@"123ull").First());

AssertAreEqual(Token(TokenType.Number, "0xABCDEF"), Tokenize(@"0xABCDEF").First());
AssertAreEqual(Token(TokenType.Number, "0xABCDEFull"), Tokenize(@"0xABCDEFull").First());
}

[TestMethod]
public void Char()
{
AssertAreEqual(Token(TokenType.Char, "a"), Tokenize(@"'a'").First());
//AssertAreEqual(Token(TokenType.Char, "\t"), Tokenize(@"'\t'").First()); // todo implement in tokenizer
//AssertAreEqual(Token(TokenType.Char, "\'"), Tokenize(@"'\''").First());
}

[TestMethod]
public void String()
{
AssertAreEqual(Token(TokenType.String, "abc"), Tokenize("\"abc\"").First());
//AssertAreEqual(Token(TokenType.String, "\t"), Tokenize("\"\t\"").First()); // todo implement in tokenizer
//AssertAreEqual(Token(TokenType.String, "\""), Tokenize("\"\"\"\"").First());
}

[TestMethod]
public void MultilineFunction()
{
var tokens = Tokenize(@"A_B_C(A_B_C, \
A_B_C)");
Assert.AreEqual(6, tokens.Length);
AssertAreEqual(Token(TokenType.Identifier, "A_B_C"), tokens[0]);
AssertAreEqual(Token(TokenType.Punctuator, "("), tokens[1]);
AssertAreEqual(Token(TokenType.Identifier, "A_B_C"), tokens[2]);
AssertAreEqual(Token(TokenType.Punctuator, ","), tokens[3]);
AssertAreEqual(Token(TokenType.Identifier, "A_B_C"), tokens[4]);
AssertAreEqual(Token(TokenType.Punctuator, ")"), tokens[5]);
}

[TestMethod]
public void Expression()
{
var tokens = Tokenize(@"-1 << 42");
Assert.AreEqual(4, tokens.Length);
AssertAreEqual(Token(TokenType.Operator, "-"), tokens[0]);
AssertAreEqual(Token(TokenType.Number, "1"), tokens[1]);
AssertAreEqual(Token(TokenType.Operator, "<<"), tokens[2]);
AssertAreEqual(Token(TokenType.Number, "42"), tokens[3]);
}

[TestMethod]
public void Call()
{
var tokens = Tokenize(@"TAG('A','B','C')");
AssertAreEqual(Token(TokenType.Identifier, "TAG"), tokens[0]);
AssertAreEqual(Token(TokenType.Punctuator, "("), tokens[1]);
AssertAreEqual(Token(TokenType.Char, "A"), tokens[2]);
AssertAreEqual(Token(TokenType.Punctuator, ","), tokens[3]);
AssertAreEqual(Token(TokenType.Char, "B"), tokens[4]);
AssertAreEqual(Token(TokenType.Punctuator, ","), tokens[5]);
AssertAreEqual(Token(TokenType.Char, "C"), tokens[6]);
AssertAreEqual(Token(TokenType.Punctuator, ")"), tokens[7]);
}

[TestMethod]
public void Cast()
{
var tokens = Tokenize(@"(int)1.0f");
AssertAreEqual(Token(TokenType.Punctuator, "("), tokens[0]);
AssertAreEqual(Token(TokenType.Keyword, "int"), tokens[1]);
AssertAreEqual(Token(TokenType.Punctuator, ")"), tokens[2]);
AssertAreEqual(Token(TokenType.Number, "1.0f"), tokens[3]);
}


[TestMethod]
public void ComplexIdentifier()
{
var tokens = Tokenize(@"A.B");
Assert.IsTrue(tokens.Length == 1);
AssertAreEqual(Token(TokenType.Identifier, "A.B"), tokens[0]);
}
}
}
1 change: 1 addition & 0 deletions FFmpeg.AutoGen.ClangMacroParser.Test/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
16 changes: 16 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser/Expressions/BinaryExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace FFmpeg.AutoGen.ClangMacroParser.Expressions
{
public class BinaryExpression : IExpression
{
public BinaryExpression(IExpression left, OperationType operationType, IExpression right)
{
Left = left;
OperationType = operationType;
Right = right;
}

public IExpression Left { get; }
public OperationType OperationType { get; }
public IExpression Right { get; }
}
}
18 changes: 18 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser/Expressions/CallExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;

namespace FFmpeg.AutoGen.ClangMacroParser.Expressions
{
public class CallExpression : IExpression
{
public CallExpression(string name, IEnumerable<IExpression> args)
{
Name = name;
Arguments = args.ToArray();
}

public IReadOnlyCollection<IExpression> Arguments { get; }

public string Name { get; }
}
}
15 changes: 15 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser/Expressions/CastExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace FFmpeg.AutoGen.ClangMacroParser.Expressions
{
public class CastExpression : IExpression
{
public CastExpression(string targetType, IExpression operand)
{
TargetType = targetType;
Operand = operand;
}

public string TargetType { get; }

public IExpression Operand { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FFmpeg.AutoGen.ClangMacroParser.Expressions
{
public class ConstantExpression : IExpression
{
public ConstantExpression(object value) => Value = value;

public object Value { get; }
}
}
6 changes: 6 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser/Expressions/IExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FFmpeg.AutoGen.ClangMacroParser.Expressions
{
public interface IExpression
{
}
}
25 changes: 25 additions & 0 deletions FFmpeg.AutoGen.ClangMacroParser/Expressions/OperationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace FFmpeg.AutoGen.ClangMacroParser.Expressions
{
public enum OperationType
{
Add,
Divide,
Modulo,
Multiply,
Power,
Subtract,
And,
Or,
ExclusiveOr,
LeftShift,
RightShift,
AndAlso,
OrElse,
Equal,
NotEqual,
GreaterThanOrEqual,
GreaterThan,
LessThan,
LessThanOrEqual
}
}
Loading