diff --git a/src/LibProtodec/Loaders/LuaSourceLoader.cs b/src/LibProtodec/Loaders/LuaSourceLoader.cs index 3789757..2d15ff8 100644 --- a/src/LibProtodec/Loaders/LuaSourceLoader.cs +++ b/src/LibProtodec/Loaders/LuaSourceLoader.cs @@ -35,7 +35,8 @@ public sealed class LuaSourceLoader return LuaSyntaxTree.ParseText( SourceText.From(fileStream), - null, // TODO: maybe expose the options parameter + null, // TODO: maybe expose this as a parameter Path.GetFileName(filePath)); + //TODO: consider checking lua source validity via SyntaxTree.GetDiagnostics() } } \ No newline at end of file diff --git a/src/LibProtodec/Models/Protobuf/Protobuf.cs b/src/LibProtodec/Models/Protobuf/Protobuf.cs index 02607d8..eaf12af 100644 --- a/src/LibProtodec/Models/Protobuf/Protobuf.cs +++ b/src/LibProtodec/Models/Protobuf/Protobuf.cs @@ -23,8 +23,11 @@ public sealed class Protobuf public string? AssemblyName { get; init; } public string? Namespace { get; init; } - public string FileName => - _fileName ??= $"{string.Join('_', TopLevels.Select(static topLevel => topLevel.Name))}.proto"; + public string FileName + { + get => _fileName ??= $"{string.Join('_', TopLevels.Select(static topLevel => topLevel.Name))}.proto"; + set => _fileName = value; + } public HashSet Imports => _imports ??= []; diff --git a/src/LibProtodec/ProtodecContext.Lua.cs b/src/LibProtodec/ProtodecContext.Lua.cs index 2125685..54689d7 100644 --- a/src/LibProtodec/ProtodecContext.Lua.cs +++ b/src/LibProtodec/ProtodecContext.Lua.cs @@ -4,8 +4,9 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -using System; +using CommunityToolkit.Diagnostics; using Loretta.CodeAnalysis; +using Loretta.CodeAnalysis.Lua.Syntax; namespace LibProtodec; @@ -13,6 +14,67 @@ partial class ProtodecContext { public void ParseLuaSyntaxTree(SyntaxTree ast) { - throw new NotImplementedException(); + CompilationUnitSyntax root = (CompilationUnitSyntax)ast.GetRoot(); + SyntaxList statements = root.Statements.Statements; + + LocalVariableDeclarationStatementSyntax pbTableDeclaration = + (LocalVariableDeclarationStatementSyntax)statements[0]; + + string pbTableName = pbTableDeclaration.Names[0].Name; + Guard.IsTrue( + pbTableName.EndsWith("_pbTable")); + + bool importedProtobufLib = false; + string? returns = null; + + foreach (StatementSyntax statement in statements) + { + switch (statement) + { + case LocalVariableDeclarationStatementSyntax + { + Names: [{ Name: { } varName }], + EqualsValues.Values: [FunctionCallExpressionSyntax { Expression: IdentifierNameSyntax { Name: "require" } } call] + }: + switch (call.Argument) + { + case StringFunctionArgumentSyntax { Expression.Token.ValueText: "protobuf/protobuf" }: + importedProtobufLib = true; + break; + case ExpressionListFunctionArgumentSyntax { Expressions: [LiteralExpressionSyntax { Token.ValueText: {} import }] }: + // TODO: handle imported protos + break; + } + break; + case ExpressionStatementSyntax + { + Expression: FunctionCallExpressionSyntax + { + Expression: IdentifierNameSyntax { Name: "module" }, + Argument: ExpressionListFunctionArgumentSyntax + { + Expressions: [LiteralExpressionSyntax literal] + } + } + }: + string moduleName = literal.Token.ValueText; + break; + case AssignmentStatementSyntax + { + Variables: [MemberAccessExpressionSyntax varExpr], + EqualsValues.Values: [{} valueExpr] + }: + // TODO: build protos + break; + case ReturnStatementSyntax { Expressions: [IdentifierNameSyntax identifier] }: + returns = identifier.Name; + break; + } + } + + if (!importedProtobufLib || returns != pbTableName) + { + ThrowHelper.ThrowInvalidDataException(); + } } } \ No newline at end of file