STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
MibModule.cs
Go to the documentation of this file.
1 /*
2  * Created by SharpDevelop.
3  * User: lextm
4  * Date: 2008/5/17
5  * Time: 17:38
6  *
7  * To change this template use Tools | Options | Coding | Edit Standard Headers.
8  */
9 
10 using System;
11 using System.Collections.Generic;
15 
16 namespace Lextm.SharpSnmpLib.Mib
17 {
21  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Mib")]
22  public sealed class MibModule : IModule
23  {
24  private readonly string _name;
25  private readonly Imports _imports;
26  private readonly Exports _exports;
27  private readonly List<IElement> _tokens = new List<IElement>();
28 
34  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "lexer")]
35  public MibModule(ISymbolEnumerator symbols)
36  {
37  if (symbols == null)
38  {
39  throw new ArgumentNullException("lexer");
40  }
41 
42  Symbol temp = symbols.NextNonEOLSymbol();
43  temp.AssertIsValidIdentifier();
44  _name = temp.ToString().ToUpperInvariant(); // all module names are uppercase
45 
46  temp = symbols.NextNonEOLSymbol();
47  temp.Expect(Symbol.Definitions);
48 
49  temp = symbols.NextNonEOLSymbol();
50  temp.Expect(Symbol.Assign);
51 
52  temp = symbols.NextSymbol();
53  temp.Expect(Symbol.Begin);
54 
55  temp = symbols.NextNonEOLSymbol();
56  if (temp == Symbol.Imports)
57  {
58  _imports = ParseDependents(symbols);
59  }
60  else if (temp == Symbol.Exports)
61  {
62  _exports = ParseExports(symbols);
63  }
64  else
65  {
66  symbols.PutBack(temp);
67  }
68 
69  ParseEntities(symbols);
70  }
71 
72  #region Accessors
73 
77  public string Name
78  {
79  get { return _name; }
80  }
81 
82  public Exports Exports
83  {
84  get { return _exports; }
85  }
86 
87  public Imports Imports
88  {
89  get { return _imports; }
90  }
91 
92  public List<IElement> Tokens
93  {
94  get { return this._tokens; }
95  }
96 
100  public IList<IDeclaration> Declarations
101  {
102  get
103  {
104  IList<IDeclaration> result = new List<IDeclaration>();
105  foreach (IElement e in _tokens)
106  {
107  IDeclaration decl = e as IDeclaration;
108  if (decl != null)
109  {
110  result.Add(decl);
111  }
112  }
113 
114  return result;
115  }
116  }
117 
121  public IList<IEntity> Entities
122  {
123  get
124  {
125  IList<IEntity> result = new List<IEntity>();
126  foreach (IElement e in _tokens)
127  {
128  IEntity entity = e as IEntity;
129  if (entity != null)
130  {
131  result.Add(entity);
132  }
133  }
134 
135  return result;
136  }
137  }
138 
139  public IList<ITypeAssignment> Types
140  {
141  get
142  {
143  IList<ITypeAssignment> result = new List<ITypeAssignment>();
144  foreach (IElement e in _tokens)
145  {
146  ITypeAssignment type = e as ITypeAssignment;
147  if (type != null)
148  {
149  result.Add(type);
150  }
151  }
152 
153  return result;
154  }
155  }
156 
157  #endregion
158 
159  #region Parsing of Symbols
160 
161  private Exports ParseExports(ISymbolEnumerator symbols)
162  {
163  return new Exports(this, symbols);
164  }
165 
166  private Imports ParseDependents(ISymbolEnumerator symbols)
167  {
168  return new Imports(this, symbols);
169  }
170 
171  private void ParseEntities(ISymbolEnumerator symbols)
172  {
173  Symbol temp = symbols.NextNonEOLSymbol();
174  SymbolList buffer = new SymbolList();
175 
176  while (temp != Symbol.End)
177  {
178  if (temp == Symbol.Assign)
179  {
180  ParseEntity(buffer, symbols);
181  buffer.Clear();
182  // skip linebreaks behind an entity
183  temp = symbols.NextNonEOLSymbol();
184  }
185  else
186  {
187  buffer.Add(temp);
188  temp = symbols.NextSymbol();
189  }
190  }
191  }
192 
193  private void ParseEntity(SymbolList preAssignSymbols, ISymbolEnumerator symbols)
194  {
195  if ((preAssignSymbols == null) || (preAssignSymbols.Count == 0))
196  {
197  Symbol s = symbols.NextSymbol();
198  if (s != null)
199  {
200  s.Assert(false, "Invalid Entitiy declaration");
201  }
202  else
203  {
204  throw new MibException("Invalid Entitiy declaration");
205  }
206  }
207 
208  // check for a valid identifier
209  preAssignSymbols[0].AssertIsValidIdentifier();
210 
211  if (preAssignSymbols.Count == 1)
212  {
213  // its a typedef
214  _tokens.Add(Lexer.ParseBasicTypeDef(this, preAssignSymbols[0].ToString(), symbols, isMacroSyntax: false));
215  return;
216  }
217 
218  ISymbolEnumerator preAssignSymbolsEnumerator = preAssignSymbols.GetSymbolEnumerator();
219  preAssignSymbolsEnumerator.NextNonEOLSymbol(); // returns identifier
220  Symbol type = preAssignSymbolsEnumerator.NextNonEOLSymbol();
221 
222  // parse declarations
223  if (type == Symbol.Object)
224  {
225  Symbol next = preAssignSymbolsEnumerator.NextNonEOLSymbol();
226 
227  if (next == Symbol.Identifier)
228  {
229  _tokens.Add(new OidValueAssignment(this, preAssignSymbols, symbols));
230  return;
231  }
232  else if (next != null)
233  {
234  preAssignSymbolsEnumerator.PutBack(next);
235  }
236  }
237  if (type == Symbol.ModuleIdentity)
238  {
239  _tokens.Add(new ModuleIdentity(this, preAssignSymbols, symbols));
240  return;
241  }
242  if (type == Symbol.ObjectType)
243  {
244  _tokens.Add(new ObjectType(this, preAssignSymbols, symbols));
245  return;
246  }
247  if (type == Symbol.ObjectGroup)
248  {
249  _tokens.Add(new ObjectGroup(this, preAssignSymbols, symbols));
250  return;
251  }
252  if (type == Symbol.NotificationGroup)
253  {
254  _tokens.Add(new NotificationGroup(this, preAssignSymbols, symbols));
255  return;
256  }
257  if (type == Symbol.ModuleCompliance)
258  {
259  _tokens.Add(new ModuleCompliance(this, preAssignSymbols, symbols));
260  return;
261  }
262  if (type == Symbol.NotificationType)
263  {
264  _tokens.Add(new NotificationType(this, preAssignSymbols, symbols));
265  return;
266  }
267  if (type == Symbol.ObjectIdentity)
268  {
269  _tokens.Add(new ObjectIdentity(this, preAssignSymbols, symbols));
270  return;
271  }
272  if (type == Symbol.Macro)
273  {
274  _tokens.Add(new Macro(this, preAssignSymbols, symbols));
275  return;
276  }
277  if (type == Symbol.TrapType)
278  {
279  _tokens.Add(new TrapType(this, preAssignSymbols, symbols));
280  return;
281  }
282  if (type == Symbol.AgentCapabilities)
283  {
284  _tokens.Add(new AgentCapabilities(this, preAssignSymbols, symbols));
285  return;
286  }
287 
288  preAssignSymbols[1].Assert(false, "Unknown/Invalid declaration");
289  }
290 
291  #endregion
292 
293  }
294 }
static readonly Symbol ModuleIdentity
Definition: Symbol.cs:191
Description of Exports.
Definition: Exports.cs:17
static readonly Symbol Imports
Definition: Symbol.cs:188
static readonly Symbol ModuleCompliance
Definition: Symbol.cs:195
ISymbolEnumerator GetSymbolEnumerator()
Definition: SymbolList.cs:94
MIB Module interface.
Definition: IModule.cs:36
static readonly Symbol Object
Definition: Symbol.cs:182
Description of MibException.
Definition: MibException.cs:24
static readonly Symbol Definitions
Definition: Symbol.cs:180
The AGENT-CAPABILITIES construct is used to specify implementation characteristics of an SNMP agent s...
static readonly Symbol NotificationType
Definition: Symbol.cs:197
static readonly Symbol ObjectIdentity
Definition: Symbol.cs:199
MibModule(ISymbolEnumerator symbols)
Creates a MibModule with a specific Lexer.
Definition: MibModule.cs:35
static readonly Symbol ObjectType
Definition: Symbol.cs:192
static readonly Symbol Exports
Definition: Symbol.cs:213
static readonly Symbol Identifier
Definition: Symbol.cs:183
static readonly Symbol End
Definition: Symbol.cs:200
static readonly Symbol Macro
Definition: Symbol.cs:201
Lexer class that parses MIB files into symbol list.
Definition: Lexer.cs:21
static readonly Symbol AgentCapabilities
Definition: Symbol.cs:204
The IMPORTS construct is used to specify items used in the current MIB module which are defined in an...
Definition: Imports.cs:17
static readonly Symbol ObjectGroup
Definition: Symbol.cs:193
static readonly Symbol Assign
Definition: Symbol.cs:184
static readonly Symbol NotificationGroup
Definition: Symbol.cs:194
Basic interface for all elements building up the MIB tree, thus having an OID as value.
Definition: IEntity.cs:34
static readonly Symbol TrapType
Definition: Symbol.cs:203
static ITypeAssignment ParseBasicTypeDef(IModule module, string name, ISymbolEnumerator symbols, bool isMacroSyntax=false)
Definition: Lexer.cs:263
Description of Symbol.
Definition: Symbol.cs:20
override string ToString()
Returns a String that represents this Symbol.
Definition: Symbol.cs:83
static readonly Symbol Begin
Definition: Symbol.cs:181