Skip to content

Commit

Permalink
Use string interpolations
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jan 11, 2024
1 parent 2295c81 commit ffacf28
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
20 changes: 10 additions & 10 deletions src/Abc.Zebus.MessageDsl/Analysis/AstCreationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public AstCreationVisitor(ParsedContracts contracts)
{
if (!_definedContractOptions.Add(pragmaName))
{
_contracts.AddError(context.name, "Duplicate file-level pragma: {0}", pragmaName);
_contracts.AddError(context.name, $"Duplicate file-level pragma: {pragmaName}");
return null;
}

if (_hasDefinitions)
_contracts.AddError(context, "File-level pragma {0} should be set at the top of the file", pragmaName);
_contracts.AddError(context, $"File-level pragma {pragmaName} should be set at the top of the file");
}
else
{
Expand All @@ -58,7 +58,7 @@ public AstCreationVisitor(ParsedContracts contracts)

if (optionDescriptor == null)
{
_contracts.AddError(context.name, "Unknown pragma: '{0}'", pragmaName);
_contracts.AddError(context.name, $"Unknown pragma: '{pragmaName}'");
return null;
}
}
Expand All @@ -77,13 +77,13 @@ public AstCreationVisitor(ParsedContracts contracts)
}
else
{
_contracts.AddError(context, "Pragma {0} expects a value", pragmaName);
_contracts.AddError(context, $"Pragma {pragmaName} expects a value");
return null;
}
}

if (!optionDescriptor.SetValue(value))
_contracts.AddError(context, "Invalid option value: '{0}' for {1}", value, pragmaName);
_contracts.AddError(context, $"Invalid option value: '{value}' for {pragmaName}");

return null;
}
Expand Down Expand Up @@ -240,19 +240,19 @@ public AstCreationVisitor(ParsedContracts contracts)

if (!int.TryParse(context.tagNumber?.Text, out var tag))
{
_contracts.AddError(context, "Invalid tag value for parameter '{0}': {1}", _currentParameter.Name, context.tagNumber?.Text);
_contracts.AddError(context, $"Invalid tag value for parameter '{_currentParameter.Name}': {context.tagNumber?.Text}");
return null;
}

if (!AstValidator.IsValidTag(tag))
{
_contracts.AddError(context, "Tag for parameter '{0}' is not within the valid range ({1})", _currentParameter.Name, context.tagNumber?.Text);
_contracts.AddError(context, $"Tag for parameter '{_currentParameter.Name}' is not within the valid range ({context.tagNumber?.Text})");
return null;
}

if (_currentParameter.Tag != 0)
{
_contracts.AddError(context, "The parameter '{0}' already has an explicit tag ({1})", _currentParameter.Name, _currentParameter.Tag);
_contracts.AddError(context, $"The parameter '{_currentParameter.Name}' already has an explicit tag ({_currentParameter.Tag})");
return null;
}

Expand Down Expand Up @@ -322,7 +322,7 @@ public AstCreationVisitor(ParsedContracts contracts)
{
var typeName = new TypeName(typeClause.typeName().GetText());
if (!constraint.Types.Add(typeName))
_contracts.AddError(clause, "Duplicate type constraint: '{0}'", typeName);
_contracts.AddError(clause, $"Duplicate type constraint: '{typeName}'");
}
}

Expand Down Expand Up @@ -352,7 +352,7 @@ private void ProcessMessage(MessageDefinition message, MessageDefinitionContext
var paramId = GetId(typeParamToken);

if (message.GenericParameters.Contains(paramId))
_contracts.AddError(typeParamToken, "Duplicate generic parameter: '{0}'", paramId);
_contracts.AddError(typeParamToken, $"Duplicate generic parameter: '{paramId}'");

message.GenericParameters.Add(paramId);
}
Expand Down
30 changes: 15 additions & 15 deletions src/Abc.Zebus.MessageDsl/Analysis/AstValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void ValidateMessage(MessageDefinition message)
if (message.Options.Proto)
{
if (message.GenericParameters.Count > 0)
_contracts.AddError(message.ParseContext, "Cannot generate .proto for generic message {0}", message.Name);
_contracts.AddError(message.ParseContext, $"Cannot generate .proto for generic message {message.Name}");
}

ValidateAttributes(message.Attributes);
Expand All @@ -52,7 +52,7 @@ private void ValidateMessage(MessageDefinition message)
var errorContext = param.ParseContext ?? message.ParseContext;

if (!paramNames.Add(param.Name))
_contracts.AddError(errorContext, "Duplicate parameter name: {0}", param.Name);
_contracts.AddError(errorContext, $"Duplicate parameter name: {param.Name}");

ValidateType(param.Type, param.ParseContext);
ValidateAttributes(param.Attributes);
Expand All @@ -71,21 +71,21 @@ private void ValidateMessage(MessageDefinition message)
if (string.IsNullOrEmpty(param.DefaultValue))
requiredParameterSeen = true;
else if (requiredParameterSeen)
_contracts.AddError(errorContext, "Optional parameter {0} cannot appear before a required parameter", param.Name);
_contracts.AddError(errorContext, $"Optional parameter {param.Name} cannot appear before a required parameter");
}

foreach (var constraint in message.GenericConstraints)
{
var errorContext = constraint.ParseContext ?? message.ParseContext;

if (!genericConstraints.Add(constraint.GenericParameterName))
_contracts.AddError(errorContext, "Duplicate generic constraint: '{0}'", constraint.GenericParameterName);
_contracts.AddError(errorContext, $"Duplicate generic constraint: '{constraint.GenericParameterName}'");

if (!message.GenericParameters.Contains(constraint.GenericParameterName))
_contracts.AddError(errorContext, "Undefined generic parameter: '{0}'", constraint.GenericParameterName);
_contracts.AddError(errorContext, $"Undefined generic parameter: '{constraint.GenericParameterName}'");

if (constraint.IsClass && constraint.IsStruct)
_contracts.AddError(errorContext, "Constraint on '{0}' cannot require both class and struct", constraint.GenericParameterName);
_contracts.AddError(errorContext, $"Constraint on '{constraint.GenericParameterName}' cannot require both class and struct");

foreach (var constraintType in constraint.Types)
ValidateType(constraintType, message.ParseContext);
Expand All @@ -106,10 +106,10 @@ private void ValidateTags(MessageDefinition message)
var errorContext = param.ParseContext ?? message.ParseContext;

if (!IsValidTag(param.Tag))
_contracts.AddError(errorContext, "Tag for parameter '{0}' is not within the valid range ({1})", param.Name, param.Tag);
_contracts.AddError(errorContext, $"Tag for parameter '{param.Name}' is not within the valid range ({param.Tag})");

if (!tags.Add(param.Tag))
_contracts.AddError(errorContext, "Duplicate tag {0} on parameter {1}", param.Tag, param.Name);
_contracts.AddError(errorContext, $"Duplicate tag {param.Tag} on parameter {param.Name}");
}

foreach (var attr in message.Attributes)
Expand All @@ -121,22 +121,22 @@ private void ValidateTags(MessageDefinition message)

if (!AttributeInterpreter.TryParseProtoInclude(attr, out var tag, out _))
{
_contracts.AddError(errorContext, "Invalid [{0}] parameters", KnownTypes.ProtoIncludeAttribute);
_contracts.AddError(errorContext, $"Invalid [{KnownTypes.ProtoIncludeAttribute}] parameters");
continue;
}

if (!IsValidTag(tag))
_contracts.AddError(errorContext, "Tag for [{0}] is not within the valid range ({1})", KnownTypes.ProtoIncludeAttribute, tag);
_contracts.AddError(errorContext, $"Tag for [{KnownTypes.ProtoIncludeAttribute}] is not within the valid range ({tag})");

if (!tags.Add(tag))
_contracts.AddError(errorContext, "Duplicate tag {0} on [{1}]", tag, KnownTypes.ProtoIncludeAttribute);
_contracts.AddError(errorContext, $"Duplicate tag {tag} on [{KnownTypes.ProtoIncludeAttribute}]");
}
}

private void ValidateEnum(EnumDefinition enumDef)
{
if (!enumDef.IsValidUnderlyingType())
_contracts.AddError(enumDef.ParseContext, "Invalid underlying type: {0}", enumDef.UnderlyingType);
_contracts.AddError(enumDef.ParseContext, $"Invalid underlying type: {enumDef.UnderlyingType}");

if (enumDef.Options.Proto && enumDef.UnderlyingType.NetType != "int")
_contracts.AddError(enumDef.ParseContext, "An enum used in a proto file must have an underlying type of int");
Expand All @@ -148,7 +148,7 @@ private void ValidateEnum(EnumDefinition enumDef)
foreach (var member in enumDef.Members)
{
if (!definedMembers.Add(member.Name))
_contracts.AddError(member.ParseContext, "Duplicate enum member: {0}", member.Name);
_contracts.AddError(member.ParseContext, $"Duplicate enum member: {member.Name}");

ValidateAttributes(member.Attributes);
}
Expand All @@ -163,7 +163,7 @@ private void ValidateAttributes(AttributeSet attributes)
private void ValidateType(TypeName type, ParserRuleContext? context)
{
if (type.NetType.Contains("??"))
_contracts.AddError(context, "Invalid type: {0}", type.NetType);
_contracts.AddError(context, $"Invalid type: {type.NetType}");
}

private void ValidateInheritance(MessageDefinition message)
Expand Down Expand Up @@ -218,7 +218,7 @@ private void DetectDuplicateTypes()
var nameWithGenericArity = GetNameWithGenericArity(typeNode);

if (duplicates.Contains(nameWithGenericArity))
_contracts.AddError(typeNode.ParseContext, "Duplicate type name: {0}", nameWithGenericArity);
_contracts.AddError(typeNode.ParseContext, $"Duplicate type name: {nameWithGenericArity}");
}

static string GetNameWithGenericArity(AstNode node)
Expand Down
10 changes: 5 additions & 5 deletions src/Abc.Zebus.MessageDsl/Analysis/AttributeInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void CheckIfRoutable(MessageDefinition message)
if (int.TryParse(attr.Parameters, out var routingPosition))
param.RoutingPosition = routingPosition;
else
_contracts.AddError(attr.ParseContext, "Invalid routing position: {0}", attr.Parameters);
_contracts.AddError(attr.ParseContext, $"Invalid routing position: {attr.Parameters}");
break;

case [var first, ..]:
Expand Down Expand Up @@ -93,26 +93,26 @@ private void ProcessProtoMemberAttribute(ParameterDefinition param)

if (string.IsNullOrWhiteSpace(attr.Parameters))
{
_contracts.AddError(attr.ParseContext, "The [{0}] attribute must have parameters", KnownTypes.ProtoMemberAttribute);
_contracts.AddError(attr.ParseContext, $"The [{KnownTypes.ProtoMemberAttribute}] attribute must have parameters");
return;
}

var match = Regex.Match(attr.Parameters, @"^\s*(?<nb>[0-9]+)\s*(?:,|$)");
if (!match.Success || !int.TryParse(match.Groups["nb"].Value, out var tagNb))
{
_contracts.AddError(attr.ParseContext, "Invalid [{0}] parameters", KnownTypes.ProtoMemberAttribute);
_contracts.AddError(attr.ParseContext, $"Invalid [{KnownTypes.ProtoMemberAttribute}] parameters");
return;
}

if (param.Tag != 0)
{
_contracts.AddError(attr.ParseContext, "The parameter '{0}' already has an explicit tag ({1})", param.Name, param.Tag);
_contracts.AddError(attr.ParseContext, $"The parameter '{param.Name}' already has an explicit tag ({param.Tag})");
return;
}

if (!AstValidator.IsValidTag(tagNb))
{
_contracts.AddError(attr.ParseContext, "Tag for parameter '{0}' is not within the valid range ({1})", param.Name, tagNb);
_contracts.AddError(attr.ParseContext, $"Tag for parameter '{param.Name}' is not within the valid range ({tagNb})");
return;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Abc.Zebus.MessageDsl/Ast/ParsedContracts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Abc.Zebus.MessageDsl.Analysis;
using Abc.Zebus.MessageDsl.Dsl;
using Antlr4.Runtime;
Expand Down Expand Up @@ -97,15 +98,15 @@ public void AddError(IToken? token, string message)
public void AddError(ParserRuleContext? context, string message)
=> AddError(context?.Start, message);

[StringFormatMethod("format")]
[StringFormatMethod("format"), Obsolete("Use a string interpolation")]
public void AddError(ParserRuleContext? context, string format, params object?[] args)
=> AddError(context, string.Format(format, args));

[StringFormatMethod("format")]
[StringFormatMethod("format"), Obsolete("Use a string interpolation")]
public void AddError(IToken? token, string format, params object?[] args)
=> AddError(token, string.Format(format, args));

[StringFormatMethod("format")]
[StringFormatMethod("format"), Obsolete("Use a string interpolation")]
public void AddError(string format, params object?[] args)
=> AddError(string.Format(format, args));
}
2 changes: 1 addition & 1 deletion src/Abc.Zebus.MessageDsl/Dsl/MessageContracts.g4.parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public string GetValidatedId(ParsedContracts contracts)
return string.Empty;

if (CSharpSyntax.IsCSharpKeyword(id) && escape == null)
contracts.AddError(this, "'{0}' is a C# keyword and has to be escaped with '@'", id);
contracts.AddError(this, $"'{id}' is a C# keyword and has to be escaped with '@'");

return id;
}
Expand Down

0 comments on commit ffacf28

Please sign in to comment.