Skip to content

Commit 404f314

Browse files
Use VirtualTreeView only
1 parent 456b1ca commit 404f314

File tree

6 files changed

+187
-220
lines changed

6 files changed

+187
-220
lines changed

ProtoBuf.Logic/MessageBinder.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public void MergeFrom(CodedInputStream input)
3232
: ParseUnknownField(input, new WireTag(index, type));
3333
fields.AddRange(parsedFields);
3434
}
35-
Result = new TypedMessage(fields, messageDef);
35+
Result = new TypedMessage(fields, messageDef, messageDef);
3636
}
3737

3838
private IEnumerable<TypedField> ParseUnknownField(CodedInputStream stream, WireTag wireTag)
3939
{
4040
var (index, type) = wireTag;
4141
var value = stream.ReadType(type);
42-
yield return new TypedField("Unknown", index, new TypedUnknown(value));
42+
yield return new TypedField("Unknown", index, null, new TypedUnknown(value));
4343
}
4444

4545
private IEnumerable<TypedField> ParseField(CodedInputStream stream, ProtoContext protoContext, FieldType type, FieldContext field)
@@ -48,7 +48,7 @@ private IEnumerable<TypedField> ParseField(CodedInputStream stream, ProtoContext
4848
var values = ReadExpectedType(stream, protoContext, type, field.type_());
4949
foreach (var value in values)
5050
{
51-
yield return new TypedField(fieldName, index, value);
51+
yield return new TypedField(fieldName, index, field, value);
5252
}
5353
}
5454

@@ -78,38 +78,38 @@ private static IEnumerable<ProtoType> ReadExpectedType(CodedInputStream stream,
7878
private static ProtoType? ReadExpectedType(CodedInputStream stream, ProtoContext protoContext, Type_Context expectedType)
7979
{
8080
if (expectedType.INT32() is not null)
81-
return new TypedInt32(stream.ReadInt32());
81+
return new TypedInt32(stream.ReadInt32(), expectedType);
8282
else if (expectedType.INT64() is not null)
83-
return new TypedInt64(stream.ReadInt64());
83+
return new TypedInt64(stream.ReadInt64(), expectedType);
8484
else if (expectedType.SINT32() is not null)
85-
return new TypedSint32(stream.ReadSInt32());
85+
return new TypedSint32(stream.ReadSInt32(), expectedType);
8686
else if (expectedType.SINT64() is not null)
87-
return new TypedSint64(stream.ReadSInt64());
87+
return new TypedSint64(stream.ReadSInt64(), expectedType);
8888
else if (expectedType.UINT32() is not null)
89-
return new TypedUint32(stream.ReadUInt32());
89+
return new TypedUint32(stream.ReadUInt32(), expectedType);
9090
else if (expectedType.UINT64() is not null)
91-
return new TypedUint64(stream.ReadUInt64());
91+
return new TypedUint64(stream.ReadUInt64(), expectedType);
9292
else if (expectedType.BOOL() is not null)
93-
return new TypedBool(stream.ReadBool());
93+
return new TypedBool(stream.ReadBool(), expectedType);
9494
else if (expectedType.FIXED32() is not null)
95-
return new TypedFixed32(stream.ReadFixed32());
95+
return new TypedFixed32(stream.ReadFixed32(), expectedType);
9696
else if (expectedType.FIXED64() is not null)
97-
return new TypedFixed64(stream.ReadFixed64());
97+
return new TypedFixed64(stream.ReadFixed64(), expectedType);
9898
else if (expectedType.SFIXED32() is not null)
99-
return new TypedSfixed32(stream.ReadSFixed32());
99+
return new TypedSfixed32(stream.ReadSFixed32(), expectedType);
100100
else if (expectedType.SFIXED64() is not null)
101-
return new TypedSfixed64(stream.ReadSFixed64());
101+
return new TypedSfixed64(stream.ReadSFixed64(), expectedType);
102102
else if (expectedType.DOUBLE() is not null)
103-
return new TypedDouble(stream.ReadDouble());
103+
return new TypedDouble(stream.ReadDouble(), expectedType);
104104
else if (expectedType.FLOAT() is not null)
105-
return new TypedFloat(stream.ReadFloat());
105+
return new TypedFloat(stream.ReadFloat(), expectedType);
106106
else if (expectedType.STRING() is not null)
107-
return new TypedString(stream.ReadString());
107+
return new TypedString(stream.ReadString(), expectedType);
108108
else if (expectedType.enumType() is not null || expectedType.messageType() is not null)
109109
{
110110
return BindMessageOrEnumDef(protoContext, expectedType) switch
111111
{
112-
EnumDefContext enumDef => new TypedEnum(stream.ReadEnum(), enumDef),
112+
EnumDefContext enumDef => new TypedEnum(stream.ReadEnum(), expectedType, enumDef),
113113
MessageDefContext innerMessageDef => ParseMessage(stream, protoContext, innerMessageDef),
114114
_ => throw new NotImplementedException(),
115115
};

ProtoBuf.Logic/MessageViewModel.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@ public record class MessageViewModel(Protobuf3Parser.MessageDefContext MessageDe
1212
public HashSet<MessageViewModel> Nested { get => nested; }
1313
public IReadOnlyCollection<FieldViewModel> Fields { get => fields; }
1414

15-
public string Definition => GetFullText(MessageDefContext);
16-
17-
public static string GetFullText(ParserRuleContext context)
18-
{
19-
if (context.Start == null || context.Stop == null || context.Start.StartIndex < 0 || context.Stop.StopIndex < 0)
20-
return context.GetText(); // Fallback
21-
22-
return context.Start.InputStream.GetText(Interval.Of(context.Start.StartIndex, context.Stop.StopIndex));
23-
}
15+
public string Definition => MessageDefContext.GetFullText();
2416

2517
public class Visitor : Protobuf3BaseVisitor<bool>
2618
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Antlr4.Runtime.Misc;
2+
using Antlr4.Runtime;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Diagnostics.CodeAnalysis;
9+
10+
namespace ProtoBuf.Logic
11+
{
12+
public static class ParserRuleContextExtensions
13+
{
14+
[return: NotNullIfNotNull(nameof(context))]
15+
public static string? GetFullText(this ParserRuleContext? context)
16+
{
17+
if (context==null)
18+
{
19+
return null;
20+
}
21+
if (context.Start == null || context.Stop == null || context.Start.StartIndex < 0 || context.Stop.StopIndex < 0)
22+
return context.GetText(); // Fallback
23+
24+
return context.Start.InputStream.GetText(Interval.Of(context.Start.StartIndex, context.Stop.StopIndex));
25+
}
26+
}
27+
}

ProtoBuf.Logic/TypedMessageDecoder.cs

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
using Antlr4.Runtime.Misc;
1+
using Antlr4.Runtime;
2+
using Antlr4.Runtime.Misc;
23
using Google.Protobuf;
34
using static ProtoBuf.Antlr.Protobuf3Parser;
45

56
namespace ProtoBuf.Logic
67
{
7-
public abstract record class ProtoType(string Type);
8-
public sealed record class TypedDouble(double Value) : ProtoType("Double");
9-
public sealed record class TypedFloat(float Value) : ProtoType("Float");
10-
public sealed record class TypedInt32(Int32 Value) : ProtoType("Int32");
11-
public sealed record class TypedInt64(Int64 Value) : ProtoType("Int64");
12-
public sealed record class TypedUint32(UInt32 Value) : ProtoType("UInt32");
13-
public sealed record class TypedUint64(UInt64 Value) : ProtoType("UInt64");
14-
public sealed record class TypedSint32(Int32 Value) : ProtoType("SInt32");
15-
public sealed record class TypedSint64(Int64 Value) : ProtoType("SInt64");
16-
public sealed record class TypedFixed32(UInt32 Value) : ProtoType("Fixed32");
17-
public sealed record class TypedFixed64(UInt64 Value) : ProtoType("Fixed64");
18-
public sealed record class TypedSfixed32(Int32 Value) : ProtoType("Sfixed32");
19-
public sealed record class TypedSfixed64(Int64 Value) : ProtoType("Sfixed64");
20-
public sealed record class TypedBool(bool Value) : ProtoType("Bool");
21-
public sealed record class TypedString(string Value) : ProtoType("String");
22-
public sealed record class TypedBytes(ByteString Value) : ProtoType("Bytes");
23-
public sealed record class TypedUnknown(object Value) : ProtoType("Unknown");
24-
public sealed record class TypedMessage(IReadOnlyList<TypedField> Fields, MessageDefContext? MessageDef) : ProtoType("Message")
8+
public abstract record class ProtoType(string Type, ParserRuleContext? TypeDefinition)
9+
{
10+
public string Definition => TypeDefinition?.GetFullText() ?? "No definition";
11+
}
12+
public sealed record class TypedDouble(double Value, ParserRuleContext? TypeDefinition) : ProtoType("Double", TypeDefinition);
13+
public sealed record class TypedFloat(float Value, ParserRuleContext? TypeDefinition) : ProtoType("Float", TypeDefinition);
14+
public sealed record class TypedInt32(Int32 Value, ParserRuleContext? TypeDefinition) : ProtoType("Int32", TypeDefinition);
15+
public sealed record class TypedInt64(Int64 Value, ParserRuleContext? TypeDefinition) : ProtoType("Int64", TypeDefinition);
16+
public sealed record class TypedUint32(UInt32 Value, ParserRuleContext? TypeDefinition) : ProtoType("UInt32", TypeDefinition);
17+
public sealed record class TypedUint64(UInt64 Value, ParserRuleContext? TypeDefinition) : ProtoType("UInt64", TypeDefinition);
18+
public sealed record class TypedSint32(Int32 Value, ParserRuleContext? TypeDefinition) : ProtoType("SInt32", TypeDefinition);
19+
public sealed record class TypedSint64(Int64 Value, ParserRuleContext? TypeDefinition) : ProtoType("SInt64", TypeDefinition);
20+
public sealed record class TypedFixed32(UInt32 Value, ParserRuleContext? TypeDefinition) : ProtoType("Fixed32", TypeDefinition);
21+
public sealed record class TypedFixed64(UInt64 Value, ParserRuleContext? TypeDefinition) : ProtoType("Fixed64", TypeDefinition);
22+
public sealed record class TypedSfixed32(Int32 Value, ParserRuleContext? TypeDefinition) : ProtoType("Sfixed32", TypeDefinition);
23+
public sealed record class TypedSfixed64(Int64 Value, ParserRuleContext? TypeDefinition) : ProtoType("Sfixed64", TypeDefinition);
24+
public sealed record class TypedBool(bool Value, ParserRuleContext? TypeDefinition) : ProtoType("Bool", TypeDefinition);
25+
public sealed record class TypedString(string Value, ParserRuleContext? TypeDefinition) : ProtoType("String", TypeDefinition);
26+
public sealed record class TypedBytes(ByteString Value, ParserRuleContext? TypeDefinition) : ProtoType("Bytes", TypeDefinition);
27+
public sealed record class TypedUnknown(object Value) : ProtoType("Unknown", null);
28+
public sealed record class TypedMessage(IReadOnlyList<TypedField> Fields, ParserRuleContext? TypeDefinition, MessageDefContext? MessageDef) : ProtoType("Message", TypeDefinition)
2529
{
2630
public string MessageType => MessageDef == null ? "Unknown message type" : MessageDef.messageName().GetText();
2731

@@ -35,7 +39,7 @@ public static string GetFullText(MessageDefContext context)
3539
return context.Start.InputStream.GetText(Interval.Of(context.Start.StartIndex, context.Stop.StopIndex));
3640
}
3741
}
38-
public sealed record class TypedEnum(int Value, EnumDefContext? EnumDef) : ProtoType("Enum")
42+
public sealed record class TypedEnum(int Value, ParserRuleContext? TypeDefinition, EnumDefContext? EnumDef) : ProtoType("Enum", TypeDefinition)
3943
{
4044
public string EnumType => EnumDef == null ? "Unknown enum type" : EnumDef.enumName().GetText();
4145
public string EnumValue => EnumDef?.enumBody().enumElement().FirstOrDefault(
@@ -51,7 +55,7 @@ public static string GetFullText(EnumDefContext context)
5155
}
5256
}
5357

54-
public sealed record class TypedField(string Name, int Index, ProtoType Value): ProtoType(Value.Type);
58+
public sealed record class TypedField(string Name, int Index, ParserRuleContext? TypeDefinition, ProtoType Value): ProtoType(Value.Type, TypeDefinition);
5559

5660
public class TypedMessageDecoder
5761
{

ProtoBufViewer.Blazor/Pages/Index.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Google.Protobuf;
2+
using Microsoft.AspNetCore.Components.Forms;
3+
using ProtoBuf.Logic;
4+
using static ProtoBuf.Antlr.Protobuf3Parser;
5+
6+
namespace ProtoBufViewer.Blazor.Pages
7+
{
8+
public partial class Index
9+
{
10+
private const string DefaultDragClass = "relative rounded-lg border-2 border-dashed pa-4 mt-4 mud-width-full mud-height-full z-10";
11+
private IBrowserFile? ProtoFile;
12+
private IBrowserFile? ProtoBinFile;
13+
HashSet<MessageViewModel> Messages { get; } = new();
14+
MessageViewModel? SelectedMessage { get; set; }
15+
ProtoContext? ParseResult { get; set; }
16+
17+
List<ProtoType>? TypedMessages { get; set; }
18+
19+
private async Task MessageFileChanged(IBrowserFile file)
20+
{
21+
ProtoFile = file;
22+
Messages.Clear();
23+
using var ms = new MemoryStream();
24+
await file.OpenReadStream().CopyToAsync(ms);
25+
ms.Position = 0;
26+
ParseResult = ProtoParser.ParseFile(ms);
27+
var visitor = new MessageViewModel.Visitor();
28+
visitor.Visit(ParseResult);
29+
foreach (var m in visitor.Messages.Where(x => x.Parent == null))
30+
{
31+
Messages.Add(m);
32+
}
33+
}
34+
35+
private async Task BinFilesChanged(IBrowserFile file)
36+
{
37+
ProtoBinFile = file;
38+
using var ms = new MemoryStream();
39+
await file.OpenReadStream().CopyToAsync(ms);
40+
ms.Position = 0;
41+
using var coded = CodedInputStream.CreateWithLimits(ms, int.MaxValue, int.MaxValue);
42+
var decoder = new TypedMessageDecoder();
43+
var result = decoder.Parse(coded, ParseResult, SelectedMessage.MessageDefContext);
44+
TypedMessages = new(result);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)