Symbol.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Created by SharpDevelop.
  3. * User: lextm
  4. * Date: 2008/5/17
  5. * Time: 17:14
  6. *
  7. * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8. */
  9. using System;
  10. using System.Configuration;
  11. using System.Globalization;
  12. using System.Text;
  13. namespace Lextm.SharpSnmpLib.Mib
  14. {
  15. /// <summary>
  16. /// Description of Symbol.
  17. /// </summary>
  18. public sealed class Symbol : IEquatable<Symbol>
  19. {
  20. private readonly string _text;
  21. private readonly int _row;
  22. private readonly int _column;
  23. private readonly string _file;
  24. private Symbol(string text) : this(null, text, -1, -1)
  25. {
  26. }
  27. /// <summary>
  28. /// Creates a <see cref="Symbol"/>.
  29. /// </summary>
  30. /// <param name="file">File</param>
  31. /// <param name="text">Text</param>
  32. /// <param name="row">Row number</param>
  33. /// <param name="column">column number</param>
  34. public Symbol(string file, string text, int row, int column)
  35. {
  36. _file = file;
  37. _text = text;
  38. _row = row;
  39. _column = column;
  40. }
  41. /// <summary>
  42. /// File.
  43. /// </summary>
  44. public string File
  45. {
  46. get
  47. {
  48. return _file;
  49. }
  50. }
  51. /// <summary>
  52. /// Row number.
  53. /// </summary>
  54. public int Row
  55. {
  56. get
  57. {
  58. return _row;
  59. }
  60. }
  61. /// <summary>
  62. /// Column number.
  63. /// </summary>
  64. public int Column
  65. {
  66. get
  67. {
  68. return _column;
  69. }
  70. }
  71. /// <summary>
  72. /// Returns a <see cref="String"/> that represents this <see cref="Symbol"/>.
  73. /// </summary>
  74. /// <returns></returns>
  75. public override string ToString()
  76. {
  77. return _text;
  78. }
  79. /// <summary>
  80. /// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="Symbol"/>.
  81. /// </summary>
  82. /// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="Symbol"/>. </param>
  83. /// <returns><value>true</value> if the specified <see cref="Object"/> is equal to the current <see cref="Symbol"/>; otherwise, <value>false</value>.
  84. /// </returns>
  85. public override bool Equals(object obj)
  86. {
  87. if (obj == null)
  88. {
  89. return false;
  90. }
  91. if (ReferenceEquals(this, obj))
  92. {
  93. return true;
  94. }
  95. return GetType() == obj.GetType() && Equals((Symbol)obj);
  96. }
  97. /// <summary>
  98. /// Serves as a hash function for a particular type.
  99. /// </summary>
  100. /// <returns>A hash code for the current <see cref="Symbol"/>.</returns>
  101. public override int GetHashCode()
  102. {
  103. return _text.GetHashCode();
  104. }
  105. /// <summary>
  106. /// The equality operator.
  107. /// </summary>
  108. /// <param name="left">Left <see cref="Symbol"/> object</param>
  109. /// <param name="right">Right <see cref="Symbol"/> object</param>
  110. /// <returns>
  111. /// Returns <c>true</c> if the values of its operands are equal, <c>false</c> otherwise.</returns>
  112. public static bool operator ==(Symbol left, Symbol right)
  113. {
  114. return Equals(left, right);
  115. }
  116. /// <summary>
  117. /// Determines whether the specified <see cref="Symbol"/> is equal to the current <see cref="Symbol"/>.
  118. /// </summary>
  119. /// <param name="left">Left <see cref="Symbol"/> object</param>
  120. /// <param name="right">Right <see cref="Symbol"/> object</param>
  121. /// <returns>
  122. /// Returns <c>true</c> if the values of its operands are equal, <c>false</c> otherwise.</returns>
  123. public static bool Equals(Symbol left, Symbol right)
  124. {
  125. object l = left;
  126. object r = right;
  127. if (l == r)
  128. {
  129. return true;
  130. }
  131. if (l == null || r == null)
  132. {
  133. return false;
  134. }
  135. return left._text.Equals(right._text);
  136. }
  137. /// <summary>
  138. /// The inequality operator.
  139. /// </summary>
  140. /// <param name="left">Left <see cref="Symbol"/> object</param>
  141. /// <param name="right">Right <see cref="Symbol"/> object</param>
  142. /// <returns>
  143. /// Returns <c>true</c> if the values of its operands are not equal, <c>false</c> otherwise.</returns>
  144. public static bool operator !=(Symbol left, Symbol right)
  145. {
  146. return !(left == right);
  147. }
  148. #region IEquatable<Symbol> Members
  149. /// <summary>
  150. /// Indicates whether the current object is equal to another object of the same type.
  151. /// </summary>
  152. /// <param name="other">An object to compare with this object.</param>
  153. /// <returns><value>true</value> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <value>false</value>.
  154. /// </returns>
  155. public bool Equals(Symbol other)
  156. {
  157. return Equals(this, other);
  158. }
  159. #endregion
  160. public static readonly Symbol Definitions = new Symbol("DEFINITIONS");
  161. public static readonly Symbol Begin = new Symbol("BEGIN");
  162. public static readonly Symbol Object = new Symbol("OBJECT");
  163. public static readonly Symbol Identifier = new Symbol("IDENTIFIER");
  164. public static readonly Symbol Assign = new Symbol("::=");
  165. public static readonly Symbol OpenBracket = new Symbol("{");
  166. public static readonly Symbol CloseBracket = new Symbol("}");
  167. public static readonly Symbol Comment = new Symbol("--");
  168. public static readonly Symbol Imports = new Symbol("IMPORTS");
  169. public static readonly Symbol Semicolon = new Symbol(";");
  170. public static readonly Symbol From = new Symbol("FROM");
  171. public static readonly Symbol ModuleIdentity = new Symbol("MODULE-IDENTITY");
  172. public static readonly Symbol ObjectType = new Symbol("OBJECT-TYPE");
  173. public static readonly Symbol ObjectGroup = new Symbol("OBJECT-GROUP");
  174. public static readonly Symbol NotificationGroup = new Symbol("NOTIFICATION-GROUP");
  175. public static readonly Symbol ModuleCompliance = new Symbol("MODULE-COMPLIANCE");
  176. public static readonly Symbol Sequence = new Symbol("SEQUENCE");
  177. public static readonly Symbol NotificationType = new Symbol("NOTIFICATION-TYPE");
  178. public static readonly Symbol EOL = new Symbol(Environment.NewLine);
  179. public static readonly Symbol ObjectIdentity = new Symbol("OBJECT-IDENTITY");
  180. public static readonly Symbol End = new Symbol("END");
  181. public static readonly Symbol Macro = new Symbol("MACRO");
  182. public static readonly Symbol Choice = new Symbol("CHOICE");
  183. public static readonly Symbol TrapType = new Symbol("TRAP-TYPE");
  184. public static readonly Symbol AgentCapabilities = new Symbol("AGENT-CAPABILITIES");
  185. public static readonly Symbol Comma = new Symbol(",");
  186. public static readonly Symbol TextualConvention = new Symbol("TEXTUAL-CONVENTION");
  187. public static readonly Symbol Syntax = new Symbol("SYNTAX");
  188. public static readonly Symbol Bits = new Symbol("BITS");
  189. public static readonly Symbol Octet = new Symbol("OCTET");
  190. public static readonly Symbol String = new Symbol("STRING");
  191. public static readonly Symbol OpenParentheses = new Symbol("(");
  192. public static readonly Symbol CloseParentheses = new Symbol(")");
  193. public static readonly Symbol Exports = new Symbol("EXPORTS");
  194. public static readonly Symbol DisplayHint = new Symbol("DISPLAY-HINT");
  195. public static readonly Symbol Status = new Symbol("STATUS");
  196. public static readonly Symbol Description = new Symbol("DESCRIPTION");
  197. public static readonly Symbol Reference = new Symbol("REFERENCE");
  198. public static readonly Symbol DoubleDot = new Symbol("..");
  199. public static readonly Symbol Pipe = new Symbol("|");
  200. public static readonly Symbol Size = new Symbol("SIZE");
  201. public static readonly Symbol Units = new Symbol("UNITS");
  202. public static readonly Symbol MaxAccess = new Symbol("MAX-ACCESS");
  203. public static readonly Symbol Access = new Symbol("ACCESS");
  204. public static readonly Symbol Index = new Symbol("INDEX");
  205. public static readonly Symbol Augments = new Symbol("AUGMENTS");
  206. public static readonly Symbol DefVal = new Symbol("DEFVAL");
  207. public static readonly Symbol Of = new Symbol("OF");
  208. public static readonly Symbol Integer = new Symbol("INTEGER");
  209. public static readonly Symbol Integer32 = new Symbol("Integer32");
  210. public static readonly Symbol IpAddress = new Symbol("IpAddress");
  211. public static readonly Symbol Counter32 = new Symbol("Counter32");
  212. public static readonly Symbol Counter = new Symbol("Counter");
  213. public static readonly Symbol TimeTicks = new Symbol("TimeTicks");
  214. public static readonly Symbol Opaque = new Symbol("Opaque");
  215. public static readonly Symbol Counter64 = new Symbol("Counter64");
  216. public static readonly Symbol Unsigned32 = new Symbol("Unsigned32");
  217. public static readonly Symbol Gauge32 = new Symbol("Gauge32");
  218. public static readonly Symbol Gauge = new Symbol("Gauge");
  219. public static readonly Symbol TruthValue = new Symbol("TruthValue");
  220. public static readonly Symbol Implied = new Symbol("IMPLIED");
  221. internal void Expect(Symbol expected, params Symbol[] orExpected)
  222. {
  223. bool isExpected = (this == expected);
  224. if (!isExpected && (orExpected != null) && (orExpected.Length > 0))
  225. {
  226. // check the alternatives
  227. for (int i=0; i<orExpected.Length; i++)
  228. {
  229. if (this == orExpected[i])
  230. {
  231. isExpected = true;
  232. break;
  233. }
  234. }
  235. }
  236. if (!isExpected)
  237. {
  238. if ((orExpected == null) || (orExpected.Length == 0))
  239. {
  240. Assert(false, "Unexpected symbol found! Expected '" + expected.ToString() + "'");
  241. }
  242. else
  243. {
  244. StringBuilder msg = new StringBuilder("Unexpected symbol found! Expected one of the following: '");
  245. msg.Append(expected);
  246. // check the alternatives
  247. for (int i=0; i<orExpected.Length; i++)
  248. {
  249. msg.Append("', '");
  250. msg.Append(expected);
  251. }
  252. msg.Append("'");
  253. Assert(false, msg.ToString());
  254. }
  255. }
  256. }
  257. internal void Assert(bool condition, string message)
  258. {
  259. if (!condition)
  260. {
  261. throw MibException.Create(message, this);
  262. }
  263. }
  264. internal void AssertIsValidIdentifier()
  265. {
  266. string message;
  267. bool isValid = IsValidIdentifier(ToString(), out message);
  268. Assert(isValid, message);
  269. }
  270. internal bool IsValidIdentifier()
  271. {
  272. string message;
  273. return IsValidIdentifier(ToString(), out message);
  274. }
  275. private static bool IsValidIdentifier(string name, out string message)
  276. {
  277. if (UseStricterValidation && (name.Length < 1 || name.Length > 64))
  278. {
  279. message = "an identifier must consist of 1 to 64 letters, digits, and hyphens";
  280. return false;
  281. }
  282. if (!Char.IsLetter(name[0]))
  283. {
  284. message = "the initial character must be a letter";
  285. return false;
  286. }
  287. if (name.EndsWith("-", StringComparison.Ordinal))
  288. {
  289. message = "a hyphen cannot be the last character of an identifier";
  290. return false;
  291. }
  292. if (name.Contains("--"))
  293. {
  294. message = "a hyphen cannot be immediately followed by another hyphen in an identifier";
  295. return false;
  296. }
  297. if (UseStricterValidation && name.Contains("_"))
  298. {
  299. message = "underscores are not allowed in identifiers";
  300. return false;
  301. }
  302. // TODO: SMIv2 forbids "-" except in module names and keywords
  303. message = null;
  304. return true;
  305. }
  306. private static bool? _useStricterValidation;
  307. private static bool UseStricterValidation
  308. {
  309. get
  310. {
  311. if (_useStricterValidation == null)
  312. {
  313. object setting = ConfigurationManager.AppSettings["StricterValidationEnabled"];
  314. _useStricterValidation = setting != null && Convert.ToBoolean(setting.ToString(), CultureInfo.InvariantCulture);
  315. }
  316. return _useStricterValidation.Value;
  317. }
  318. }
  319. }
  320. }