public override UnionDefinition VisitUnion_decl([NotNull] FlatBuffersParser.Union_declContext context) { this.unionDef = new UnionDefinition(context.IDENT().GetText(), this.parent); var metadata = new MetadataVisitor().VisitMetadata(context.metadata()); if (metadata.ContainsKey("NoCustomType")) { this.unionDef.GenerateCustomUnionType = false; } ErrorContext.Current.WithScope(this.unionDef.Name, () => { base.VisitUnion_decl(context); }); return(this.unionDef); }
public override RpcDefinition VisitRpc_method([NotNull] FlatBuffersParser.Rpc_methodContext context) { var idents = context.IDENT(); string name = idents[0].GetText(); string requestType = idents[1].GetText(); string responseType = idents[2].GetText(); Dictionary <string, string> metadata = new MetadataVisitor().Visit(context.metadata()); var streamingType = RpcStreamingType.Unary; if (metadata.TryGetValue("streaming", out string value)) { streamingType = ParseStreamingType(value); } this.rpcDefinition.AddRpcMethod(name, requestType, responseType, streamingType); return(null); }
public override RpcDefinition?VisitRpc_method([NotNull] FlatBuffersParser.Rpc_methodContext context) { var definition = this.rpcDefinition ?? throw new InvalidOperationException($"FlatSharp.Internal: Failed to initialize RPC definition"); var idents = context.IDENT(); string name = idents[0].GetText(); string requestType = idents[1].GetText(); string responseType = idents[2].GetText(); Dictionary <string, string?> metadata = new MetadataVisitor().Visit(context.metadata()); var streamingType = RpcStreamingType.Unary; if (metadata.TryGetValue("streaming", out string?value)) { streamingType = ParseStreamingType(value); } definition.AddRpcMethod(name, requestType, responseType, streamingType); return(null); }
public override TableOrStructDefinition VisitType_decl([NotNull] FlatBuffersParser.Type_declContext context) { Dictionary <string, string> metadata = new MetadataVisitor().Visit(context.metadata()); TableOrStructDefinition definition = new TableOrStructDefinition( context.IDENT().GetText(), ParseSerailizerFlags(metadata), this.parent); ErrorContext.Current.WithScope(definition.Name, () => { definition.IsTable = context.GetChild(0).GetText() == "table"; if (metadata.TryGetValue("virtual", out string virtualValue)) { if (!bool.TryParse(virtualValue, out bool isVirtual)) { ErrorContext.Current?.RegisterError($"The 'virtual' attribute must have a boolean value."); } definition.Virtual = isVirtual; } if (!definition.IsTable && definition.RequestedSerializer != null) { ErrorContext.Current.RegisterError("Structs may not have precompiled serializers."); } var fields = context.field_decl(); if (fields != null) { definition.Fields = fields.Select(x => new FieldVisitor().VisitField_decl(x)).ToList(); } }); return(definition); }
public override FieldDefinition VisitField_decl([NotNull] FlatBuffersParser.Field_declContext context) { this.definition.Name = context.IDENT().GetText(); ErrorContext.Current.WithScope(this.definition.Name, () => { Dictionary <string, string> metadata = new MetadataVisitor().VisitMetadata(context.metadata()); string fbsFieldType = context.type().GetText(); this.definition.VectorType = VectorType.None; if (fbsFieldType.StartsWith("[")) { this.definition.VectorType = VectorType.IList; // Trim the starting and ending square brackets. fbsFieldType = fbsFieldType.Substring(1, fbsFieldType.Length - 2); this.definition.VectorType = VectorType.IList; if (metadata.TryGetValue("vectortype", out string vectorTypeString)) { if (!Enum.TryParse <VectorType>(vectorTypeString, true, out var vectorType)) { ErrorContext.Current?.RegisterError($"Unable to parse '{vectorTypeString}' as a vector type. Valid choices are: {string.Join(", ", Enum.GetNames(typeof(VectorType)))}."); } this.definition.VectorType = vectorType; } } else if (metadata.ContainsKey("vectortype")) { ErrorContext.Current?.RegisterError($"Non-vectors may not have the 'vectortype' attribute. Field = '{this.definition.Name}'"); } this.definition.FbsFieldType = fbsFieldType; string defaultValue = context.scalar()?.GetText(); if (!string.IsNullOrEmpty(defaultValue)) { this.definition.DefaultValue = defaultValue; } if (metadata.ContainsKey("deprecated")) { this.definition.Deprecated = true; } if (metadata.ContainsKey("key")) { this.definition.IsKey = true; } if (metadata.ContainsKey("sortedvector")) { this.definition.SortedVector = true; } // Attributes from FlatBuffers that we don't support. string[] unsupportedAttributes = { "id", "required", "force_align", "bit_flags", "flexbuffer", "hash", "original_order" }; foreach (var unsupportedAttribute in unsupportedAttributes) { if (metadata.ContainsKey(unsupportedAttribute)) { ErrorContext.Current?.RegisterError($"FlatSharpCompiler does not support the '{unsupportedAttribute}' attribute in FBS files."); } } }); return(this.definition); }
public override bool VisitField_decl([NotNull] FlatBuffersParser.Field_declContext context) { string name = context.IDENT().GetText(); ErrorContext.Current.WithScope(name, () => { Dictionary <string, string?> metadata = new MetadataVisitor().VisitMetadata(context.metadata()); var(fieldType, vectorType, structVectorLength) = GetFbsFieldType(context, metadata); var definition = new FieldDefinition(this.parent, name, fieldType) { VectorType = vectorType, }; string?defaultValue = context.defaultValue_decl()?.GetText(); if (defaultValue == "null") { definition.IsOptionalScalar = true; } else if (!string.IsNullOrEmpty(defaultValue)) { definition.DefaultValue = defaultValue; } // standard metadata definition.Deprecated = metadata.ParseBooleanMetadata(MetadataKeys.Deprecated); definition.IsKey = metadata.ParseBooleanMetadata(MetadataKeys.Key); // Flatsharp custom metadata definition.SortedVector = metadata.ParseBooleanMetadata(MetadataKeys.SortedVector, MetadataKeys.SortedVectorLegacy); definition.SharedString = metadata.ParseBooleanMetadata(MetadataKeys.SharedString, MetadataKeys.SharedStringLegacy); definition.NonVirtual = metadata.ParseNullableBooleanMetadata(MetadataKeys.NonVirtualProperty, MetadataKeys.NonVirtualPropertyLegacy); definition.ForceWrite = metadata.ParseNullableBooleanMetadata(MetadataKeys.ForceWrite); this.ParseIdMetadata(definition, metadata); definition.SetterKind = metadata.ParseMetadata( new[] { MetadataKeys.Setter, MetadataKeys.SetterLegacy }, ParseSetterKind, SetterKind.Public, SetterKind.Public); if (structVectorLength == null) { this.parent.Fields.Add(definition); } else if (this.parent.IsTable) { ErrorContext.Current.RegisterError("Only structs may contain fixed-length vectors"); } else { List <string> groupNames = new List <string>(); for (int i = 0; i < structVectorLength.Value; ++i) { string name = $"__flatsharp__{definition.Name}_{i}"; this.parent.Fields.Add(definition with { Name = name, SetterKind = SetterKind.Protected, GetterModifier = AccessModifier.Protected, CustomGetter = $"{definition.Name}[{i}]" }); groupNames.Add(name); }
public override TableOrStructDefinition VisitType_decl([NotNull] FlatBuffersParser.Type_declContext context) { Dictionary <string, string?> metadata = new MetadataVisitor().Visit(context.metadata()); TableOrStructDefinition definition = new TableOrStructDefinition( context.IDENT().GetText(), this.parent); ErrorContext.Current.WithScope(definition.Name, () => { definition.IsTable = context.GetChild(0).GetText() == "table"; definition.NonVirtual = metadata.ParseNullableBooleanMetadata(MetadataKeys.NonVirtualProperty, MetadataKeys.NonVirtualPropertyLegacy); definition.ForceWrite = metadata.ParseNullableBooleanMetadata(MetadataKeys.ForceWrite); definition.DefaultConstructorKind = metadata.ParseMetadata <DefaultConstructorKind?>( new[] { MetadataKeys.DefaultConstructorKind }, ParseDefaultConstructorKind, DefaultConstructorKind.Public, DefaultConstructorKind.Public); definition.RequestedSerializer = metadata.ParseMetadata <FlatBufferDeserializationOption?>( new[] { MetadataKeys.SerializerKind, MetadataKeys.PrecompiledSerializerLegacy }, ParseSerializerKind, FlatBufferDeserializationOption.Default, null); if (!definition.IsTable && definition.RequestedSerializer is not null) { ErrorContext.Current.RegisterError("Structs may not have serializers."); } if (!definition.IsTable && definition.ForceWrite is not null) { ErrorContext.Current.RegisterError($"Structs may not use the '{MetadataKeys.ForceWrite}' attribute."); } if (metadata.ContainsKey(MetadataKeys.ObsoleteDefaultConstructorLegacy)) { ErrorContext.Current.RegisterError($"The '{MetadataKeys.ObsoleteDefaultConstructorLegacy}' metadata attribute has been deprecated. Please use the '{MetadataKeys.DefaultConstructorKind}' attribute instead."); } if (metadata.TryGetValue(MetadataKeys.FileIdentifier, out var fileId)) { if (!definition.IsTable) { ErrorContext.Current.RegisterError("Structs may not have file identifiers."); } definition.FileIdentifier = fileId; } var fields = context.field_decl(); if (fields != null) { foreach (var f in fields) { new FieldVisitor(definition).VisitField_decl(f); } } }); return(definition); }