Skip to content

Commit

Permalink
Auto-cleaning of silly bounds checks
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 30, 2024
1 parent cf99537 commit dfdf399
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion YangParser/SemanticModel/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,47 @@ public Range(YangStatement statement) : base(statement)

public string GetConstructorValidation()
{
var qualifiers = Bounds.Select(bound => double.IsNaN(bound.Item2) ? $"input >= {bound.Item1}" : $"input is >= {bound.Item1} and <= {bound.Item2}");
foreach (var bound in Bounds.ToArray())
{
var max = Parent!.Argument switch
{
"int8" => sbyte.MaxValue,
"uint8" => byte.MaxValue,
"int16" => short.MaxValue,
"uint16" => ushort.MaxValue,
"int32" => int.MaxValue,
"uint32" => uint.MaxValue,
"int64" => long.MaxValue,
"uint64" => ulong.MaxValue,
"decimal64" => double.MaxValue,
_ => 0,
};
var min = Parent!.Argument switch
{
"int8" => sbyte.MinValue,
"uint8" => byte.MinValue,
"int16" => short.MinValue,
"uint16" => ushort.MinValue,
"int32" => int.MinValue,
"uint32" => uint.MinValue,
"int64" => long.MinValue,
"uint64" => ulong.MinValue,
"decimal64" => double.MinValue,
_ => 0,
};
if (bound.Item2 >= max && bound.Item1 <= min)
{
Bounds.Remove(bound);
}
}

if (Bounds.Count == 0)
{
return string.Empty;
}

var qualifiers = Bounds.Select(bound =>
double.IsNaN(bound.Item2) ? $"input >= {bound.Item1}" : $"input is >= {bound.Item1} and <= {bound.Item2}");
var all = string.Join("||", qualifiers);
var hasError = this.TryGetChild<ErrorMessage>(out var errorMessage);
var hasTag = this.TryGetChild<ErrorAppTag>(out var appTag);
Expand Down

0 comments on commit dfdf399

Please sign in to comment.