1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System.Collections.Generic;
9 using System.Linq;
10 using Sprache;
11 
12 namespace Antmicro.Renode.PlatformDescription.Syntax
13 {
14     // the class is there because we are not able to have position aware IEnumerable
15     public sealed class SingleOrMultiIrqEnd : IPositionAware<SingleOrMultiIrqEnd>, IWithPosition, IVisitable
16     {
SingleOrMultiIrqEnd(IEnumerable<IrqEnd> ends)17         public SingleOrMultiIrqEnd(IEnumerable<IrqEnd> ends)
18         {
19             Ends = ends;
20         }
21 
SetPos(Position startPos, int length)22         public SingleOrMultiIrqEnd SetPos(Position startPos, int length)
23         {
24             var copy = SerializationProvider.Instance.DeepClone(this);
25             copy.StartPosition = startPos;
26             copy.Length = length;
27             return copy;
28         }
29 
WithEnds(IEnumerable<IrqEnd> ends)30         public SingleOrMultiIrqEnd WithEnds(IEnumerable<IrqEnd> ends)
31         {
32             var copy = SerializationProvider.Instance.DeepClone(this);
33             copy.Ends = ends;
34             return copy;
35         }
36 
ToString()37         public override string ToString()
38         {
39             return PrettyPrintEnds(Ends);
40         }
41 
Visit()42         public IEnumerable<object> Visit()
43         {
44             return Enumerable.Empty<object>();
45         }
46 
47         public Position StartPosition { get; private set; }
48         public int Length { get; private set; }
49         public IEnumerable<IrqEnd> Ends { get; private set; }
50 
PrettyPrintEnds(IEnumerable<IrqEnd> ends)51         private static string PrettyPrintEnds(IEnumerable<IrqEnd> ends)
52         {
53             var endsAsArray = ends.ToArray();
54             if(endsAsArray.Length < 2)
55             {
56                 return endsAsArray[0].ToShortString();
57             }
58             return $"[{endsAsArray.Select(x => x.ToShortString()).Aggregate((x, y) => x + ',' + y)}]";
59         }
60     }
61 }
62