NTP Analyzer  0.8.2
Analyze the operation of time servers
GraphSetSyntaxNode.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System.Collections.Generic;
23 using System.Linq;
27 using Ntp.Analyzer.Config.Table;
28 
29 namespace Ntp.Analyzer.Config.Syntax
30 {
31  public sealed class GraphSetSyntaxNode : SyntaxNode<GraphSetConfiguration>
32  {
33  public GraphSetSyntaxNode(string name, Symbol owner, int line)
34  : base(Symbol.KeywordGraphSet, name, line)
35  {
36  graphs = new List<ISyntaxNode>();
37  this.owner = owner;
38  }
39 
40  private readonly List<ISyntaxNode> graphs;
41  private readonly Symbol owner;
42 
44  {
45  var title = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordTitle) as StringSettingNode;
46  var linkIndex = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordLinkIndex) as IntegerSettingNode;
47 
48  var list = new List<GraphBaseConfiguration>();
49  foreach (var graph in graphs)
50  {
51  if (graph is HostGraphSyntaxNode)
52  {
53  list.Add((graph as HostGraphSyntaxNode).Compile());
54  }
55  else if (graph is TrafficGraphSyntaxNode)
56  {
57  list.Add((graph as TrafficGraphSyntaxNode).Compile());
58  }
59  else if (graph is PeerGraphSyntaxNode)
60  {
61  list.Add((graph as PeerGraphSyntaxNode).Compile());
62  }
63  else
64  {
65  AddError("Internal error in configuration compiler: GraphSetConfiguration");
66  }
67  }
68 
69  return new GraphSetConfiguration(
70  Name,
71  title?.Value,
72  linkIndex?.Value,
73  list
74  );
75  }
76 
77  protected override void InternalResolve(SymbolTable table)
78  {
79  graphs.AddRange(
80  Nodes.
81  Where(n => n.Symbol == Symbol.KeywordGraph).
82  Cast<StringSettingNode>().
83  Select(graph => table.Lookup(graph.Value))
84  );
85  }
86 
87  protected override void ValidateMandatories()
88  {
89  CheckIsUnique(new List<Symbol>
90  {
91  Symbol.KeywordLinkIndex,
92  Symbol.KeywordTitle
93  });
94 
95  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordGraph});
96  }
97 
98  protected override void ValidateReferences(SymbolTable table)
99  {
100  string keyword = Keyword.Find(Symbol.KeywordGraph).Name;
101  string peerGraphWord = Keyword.Find(Symbol.KeywordPeerGraph).Name;
102  var graphNames = Nodes.Where(n => n.Symbol == Symbol.KeywordGraph).Cast<StringSettingNode>();
103 
104  foreach (var graph in graphNames)
105  {
106  var reference = table.Lookup(graph.Value);
107  if (reference == null)
108  {
109  AddReferenceNameError(graph, keyword, graph.Value);
110  }
111  else if (owner == Symbol.KeywordHostPage &&
112  !(reference is HostGraphSyntaxNode || reference is TrafficGraphSyntaxNode))
113  {
114  AddReferenceTypeError(graph, keyword, keyword, graph.Value);
115  }
116  else if (owner == Symbol.KeywordPeerPage && !(reference is PeerGraphSyntaxNode))
117  {
118  AddReferenceTypeError(graph, keyword, peerGraphWord, graph.Value);
119  }
120  }
121  }
122 
123  protected override void ValidateTypes()
124  {
125  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordLinkIndex);
126  CheckTypeIs<StringSettingNode>(Symbol.KeywordTitle);
127  CheckTypeIs<StringSettingNode>(Symbol.KeywordGraph);
128  }
129  }
130 }
override GraphSetConfiguration InternalCompile()
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
override void InternalResolve(SymbolTable table)
override void ValidateReferences(SymbolTable table)
void Add(ISyntaxNode node)
Definition: SyntaxNode.cs:66
override void ValidateMandatories()
Override to validates the mandatory types in this syntax node.
override void ValidateTypes()
Override to validates the types in this syntax node.
GraphSetSyntaxNode(string name, Symbol owner, int line)
ISyntaxNode Lookup(string name)
Definition: SymbolTable.cs:41