NTP Analyzer  0.8.2
Analyze the operation of time servers
StatSyntaxNode.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;
24 using Ntp.Analyzer.Config.Node;
27 using Ntp.Analyzer.Config.Table;
28 
29 namespace Ntp.Analyzer.Config.Syntax
30 {
31  public sealed class StatSyntaxNode : SyntaxNode<StatConfiguration>
32  {
33  public StatSyntaxNode(Symbol symbol, string name, int line)
34  : base(symbol, name, line)
35  {
36  }
37 
39 
40  protected override StatConfiguration InternalCompile()
41  {
42  var initialRun = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordInitialRun) as BooleanSettingNode;
43  var fixedRun = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFixedRun) as BooleanSettingNode;
44  var stamp = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordTimeStamp) as TimeStampNode;
45 
46  var freq = bulk == null
47  ? Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFrequency) as IntegerSettingNode
48  : null;
49 
50  if (Symbol != Symbol.KeywordDriftStats)
51  return new StatConfiguration(
52  Name,
53  bulk?.Compile(),
54  stamp?.DateTimeKind,
55  freq?.Value,
56  initialRun?.Value,
57  fixedRun?.Value);
58 
59  var file = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFile) as StringSettingNode;
60 
61  return new DriftStatConfiguration(
62  Name,
63  file?.Value,
64  bulk?.Compile(),
65  stamp?.DateTimeKind,
66  freq?.Value,
67  initialRun?.Value,
68  fixedRun?.Value);
69  }
70 
71  protected override void InternalResolve(SymbolTable table)
72  {
73  var freq = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFrequency) as StringSettingNode;
74  if (freq != null)
75  bulk = table.Lookup(freq.Value) as ReadingSyntaxNode;
76  }
77 
78  protected override void ValidateMandatories()
79  {
80  CheckIsUnique(new List<Symbol>
81  {
82  Symbol.KeywordInitialRun,
83  Symbol.KeywordFixedRun,
84  Symbol.KeywordFrequency,
85  Symbol.KeywordTimeStamp,
86  Symbol.KeywordFile
87  });
88 
89  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordFrequency});
90 
91  if (Symbol == Symbol.KeywordDriftStats)
92  {
93  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordFile});
94  }
95  }
96 
97  protected override void ValidateReferences(SymbolTable table)
98  {
99  string keyword = Keyword.Find(Symbol.KeywordFrequency).Name;
100  string name = Keyword.Find(Symbol.KeywordReading).Name;
101 
102  var freq = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFrequency);
103  if (!(freq is IntegerSettingNode || freq is StringSettingNode))
104  {
105  AddError($"{keyword} must be either an integer value or the name of a {name} section.");
106  }
107 
108  var freqName = freq as StringSettingNode;
109  if (freqName == null)
110  return;
111 
112  var reference = table.Lookup(freqName.Value);
113  if (reference == null)
114  {
115  AddReferenceNameError(freqName, keyword, freqName.Value);
116  }
117  else if (!(reference is ReadingSyntaxNode))
118  {
119  AddReferenceTypeError(freqName, keyword, name, freqName.Value);
120  }
121 
122  if (Symbol == Symbol.KeywordDriftStats || Nodes.Count(n => n.Symbol == Symbol.KeywordFile) == 0)
123  return;
124 
125  string fileKeyword = Keyword.Find(Symbol.KeywordFile).Name;
126  string sectionKeyword = Keyword.Find(Symbol).Name;
127  AddError($"{fileKeyword} is not a valid setting in a {sectionKeyword} section.");
128  }
129 
130  protected override void ValidateTypes()
131  {
132  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordInitialRun);
133  CheckTypeIs<BooleanSettingNode>(Symbol.KeywordFixedRun);
134  CheckTypeIs<TimeStampNode>(Symbol.KeywordTimeStamp);
135  CheckTypeIs<IntegerSettingNode, StringSettingNode>(Symbol.KeywordFrequency);
136  CheckTypeIs<StringSettingNode>(Symbol.KeywordFile);
137  }
138  }
139 }
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
StatSyntaxNode(Symbol symbol, string name, int line)
override void ValidateMandatories()
Override to validates the mandatory types in this syntax node.
override void ValidateReferences(SymbolTable table)
override void InternalResolve(SymbolTable table)
override StatConfiguration InternalCompile()
override void ValidateTypes()
Override to validates the types in this syntax node.
ISyntaxNode Lookup(string name)
Definition: SymbolTable.cs:41