/Project/Src/StyleCop.CSharp/CsToken.cs

https://github.com/StyleCop/StyleCop · C# · 439 lines · 170 code · 47 blank · 222 comment · 8 complexity · a1388e6990de7fe0d2e01f6dc336a347 MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="CsToken.cs" company="https://github.com/StyleCop">
  3. // MS-PL
  4. // </copyright>
  5. // <license>
  6. // This source code is subject to terms and conditions of the Microsoft
  7. // Public License. A copy of the license can be found in the License.html
  8. // file at the root of this distribution. If you cannot locate the
  9. // Microsoft Public License, please send an email to dlr@microsoft.com.
  10. // By using this source code in any fashion, you are agreeing to be bound
  11. // by the terms of the Microsoft Public License. You must not remove this
  12. // notice, or any other, from this software.
  13. // </license>
  14. // <summary>
  15. // Describes a single token within a C# document.
  16. // </summary>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. namespace StyleCop.CSharp
  19. {
  20. using System.Diagnostics.CodeAnalysis;
  21. /// <summary>
  22. /// Describes a single token within a C# document.
  23. /// </summary>
  24. /// <subcategory>token</subcategory>
  25. [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Camel case better serves in this case.")]
  26. public class CsToken : ICodePart
  27. {
  28. #region Fields
  29. /// <summary>
  30. /// True if the token is part of a generated code block.
  31. /// </summary>
  32. private readonly bool generated;
  33. /// <summary>
  34. /// The location of this token in the code document.
  35. /// </summary>
  36. private readonly CodeLocation location;
  37. /// <summary>
  38. /// THe class of the token.
  39. /// </summary>
  40. private readonly CsTokenClass tokenClass;
  41. /// <summary>
  42. /// The type of this token.
  43. /// </summary>
  44. private readonly CsTokenType tokenType;
  45. /// <summary>
  46. /// The parent of the token.
  47. /// </summary>
  48. private Reference<ICodePart> parent;
  49. /// <summary>
  50. /// The token text.
  51. /// </summary>
  52. private string text;
  53. #endregion
  54. #region Constructors and Destructors
  55. /// <summary>
  56. /// Initializes a new instance of the CsToken class.
  57. /// </summary>
  58. /// <param name="text">
  59. /// The token string.
  60. /// </param>
  61. /// <param name="tokenType">
  62. /// The token type.
  63. /// </param>
  64. /// <param name="location">
  65. /// The location of the token within the code document.
  66. /// </param>
  67. /// <param name="parent">
  68. /// References the parent code part.
  69. /// </param>
  70. /// <param name="generated">
  71. /// True if the token is inside of a block of generated code.
  72. /// </param>
  73. internal CsToken(string text, CsTokenType tokenType, CodeLocation location, Reference<ICodePart> parent, bool generated)
  74. : this(text, tokenType, CsTokenClass.Token, location, parent, generated)
  75. {
  76. Param.Ignore(text, tokenType, location, parent, generated);
  77. }
  78. /// <summary>
  79. /// Initializes a new instance of the CsToken class.
  80. /// </summary>
  81. /// <param name="tokenType">
  82. /// The token type.
  83. /// </param>
  84. /// <param name="tokenClass">
  85. /// The token class.
  86. /// </param>
  87. /// <param name="location">
  88. /// The location of the token within the code document.
  89. /// </param>
  90. /// <param name="parent">
  91. /// References the parent code part.
  92. /// </param>
  93. /// <param name="generated">
  94. /// True if the token is inside of a block of generated code.
  95. /// </param>
  96. internal CsToken(CsTokenType tokenType, CsTokenClass tokenClass, CodeLocation location, Reference<ICodePart> parent, bool generated)
  97. {
  98. Param.Ignore(tokenType);
  99. Param.Ignore(tokenClass);
  100. Param.AssertNotNull(location, "location");
  101. Param.AssertNotNull(parent, "parent");
  102. Param.Ignore(generated);
  103. this.tokenType = tokenType;
  104. this.tokenClass = tokenClass;
  105. this.location = location;
  106. this.parent = parent;
  107. this.generated = generated;
  108. }
  109. /// <summary>
  110. /// Initializes a new instance of the CsToken class.
  111. /// </summary>
  112. /// <param name="text">
  113. /// The token string.
  114. /// </param>
  115. /// <param name="tokenType">
  116. /// The token type.
  117. /// </param>
  118. /// <param name="tokenClass">
  119. /// The token class.
  120. /// </param>
  121. /// <param name="location">
  122. /// The location of the token within the code document.
  123. /// </param>
  124. /// <param name="parent">
  125. /// References the parent code part.
  126. /// </param>
  127. /// <param name="generated">
  128. /// True if the token is inside of a block of generated code.
  129. /// </param>
  130. internal CsToken(string text, CsTokenType tokenType, CsTokenClass tokenClass, CodeLocation location, Reference<ICodePart> parent, bool generated)
  131. {
  132. Param.AssertNotNull(text, "text");
  133. Param.Ignore(tokenType);
  134. Param.Ignore(tokenClass);
  135. Param.Ignore(location);
  136. Param.AssertNotNull(parent, "parent");
  137. Param.Ignore(generated);
  138. this.text = text;
  139. this.tokenType = tokenType;
  140. this.tokenClass = tokenClass;
  141. this.location = location;
  142. this.parent = parent;
  143. this.generated = generated;
  144. }
  145. #endregion
  146. #region Public Properties
  147. /// <summary>
  148. /// Gets the type of this code part.
  149. /// </summary>
  150. public CodePartType CodePartType
  151. {
  152. get
  153. {
  154. return CodePartType.Token;
  155. }
  156. }
  157. /// <summary>
  158. /// Gets the token class.
  159. /// </summary>
  160. [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", MessageId = "Member", Justification = "Camel case better serves in this case.")]
  161. public CsTokenClass CsTokenClass
  162. {
  163. get
  164. {
  165. return this.tokenClass;
  166. }
  167. }
  168. /// <summary>
  169. /// Gets the token type.
  170. /// </summary>
  171. [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", MessageId = "Member", Justification = "Camel case better serves in this case.")]
  172. public CsTokenType CsTokenType
  173. {
  174. get
  175. {
  176. return this.tokenType;
  177. }
  178. }
  179. /// <summary>
  180. /// Gets a value indicating whether the token is within a block of generated code.
  181. /// </summary>
  182. public bool Generated
  183. {
  184. get
  185. {
  186. return this.generated;
  187. }
  188. }
  189. /// <summary>
  190. /// Gets the line number that the token appears on in the document.
  191. /// </summary>
  192. public virtual int LineNumber
  193. {
  194. get
  195. {
  196. return this.location.LineNumber;
  197. }
  198. }
  199. /// <summary>
  200. /// Gets the location of the token in the code document.
  201. /// </summary>
  202. public virtual CodeLocation Location
  203. {
  204. get
  205. {
  206. return this.location;
  207. }
  208. }
  209. /// <summary>
  210. /// Gets the parent of the token.
  211. /// </summary>
  212. public ICodePart Parent
  213. {
  214. get
  215. {
  216. return this.parent.Target;
  217. }
  218. }
  219. /// <summary>
  220. /// Gets or sets the token string.
  221. /// </summary>
  222. public virtual string Text
  223. {
  224. get
  225. {
  226. if (this.text == null)
  227. {
  228. this.CreateTextString();
  229. if (this.text == null)
  230. {
  231. this.text = string.Empty;
  232. }
  233. }
  234. return this.text;
  235. }
  236. protected set
  237. {
  238. this.text = value;
  239. }
  240. }
  241. #endregion
  242. #region Properties
  243. /// <summary>
  244. /// Gets or sets the parent reference.
  245. /// </summary>
  246. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Properties must always have a get.")]
  247. internal Reference<ICodePart> ParentRef
  248. {
  249. get
  250. {
  251. return this.parent;
  252. }
  253. set
  254. {
  255. Param.Ignore(value);
  256. this.parent = value;
  257. }
  258. }
  259. #endregion
  260. #region Public Methods and Operators
  261. /// <summary>
  262. /// Returns the contents of the token as a string.
  263. /// </summary>
  264. /// <returns>Returns the token string.</returns>
  265. public override string ToString()
  266. {
  267. return this.Text;
  268. }
  269. #endregion
  270. #region Methods
  271. /// <summary>
  272. /// Joins the locations of the two tokens.
  273. /// </summary>
  274. /// <param name="location1">
  275. /// The first location.
  276. /// </param>
  277. /// <param name="token2">
  278. /// The second token.
  279. /// </param>
  280. /// <returns>
  281. /// Returns the joined locations.
  282. /// </returns>
  283. internal static CodeLocation JoinLocations(CodeLocation location1, CsToken token2)
  284. {
  285. Param.Ignore(location1, token2);
  286. return token2 == null ? CodeLocation.Join(location1, null) : CodeLocation.Join(location1, token2.Location);
  287. }
  288. /////// <summary>
  289. /////// Joins the locations of the two tokens.
  290. /////// </summary>
  291. /////// <param name="token1">The first token.</param>
  292. /////// <param name="location2">The second location.</param>
  293. /////// <returns>Returns the joined locations.</returns>
  294. ////internal static CodeLocation JoinLocations(CsToken token1, CodeLocation location2)
  295. ////{
  296. //// Param.Ignore(token1, location2);
  297. //// return CodeLocation.Join(token1 == null ? null : token1.Location, location2);
  298. ////}
  299. /// <summary>
  300. /// Joins the locations of the two tokens.
  301. /// </summary>
  302. /// <param name="location1">
  303. /// The first location.
  304. /// </param>
  305. /// <param name="token2">
  306. /// The second token.
  307. /// </param>
  308. /// <returns>
  309. /// Returns the joined locations.
  310. /// </returns>
  311. internal static CodeLocation JoinLocations(CodeLocation location1, Node<CsToken> token2)
  312. {
  313. Param.Ignore(location1, token2);
  314. return CsToken.JoinLocations(location1, token2 == null ? null : token2.Value);
  315. }
  316. /////// <summary>
  317. /////// Joins the locations of the two tokens.
  318. /////// </summary>
  319. /////// <param name="token1">The first token.</param>
  320. /////// <param name="location2">The second location.</param>
  321. /////// <returns>Returns the joined locations.</returns>
  322. ////internal static CodeLocation JoinLocations(Node<CsToken> token1, CodeLocation location2)
  323. ////{
  324. //// Param.Ignore(token1, location2);
  325. //// return CsToken.JoinLocations(token1 == null ? null : token1.Value, location2);
  326. ////}
  327. /// <summary>
  328. /// Joins the locations of the two tokens.
  329. /// </summary>
  330. /// <param name="token1">
  331. /// The first token.
  332. /// </param>
  333. /// <param name="token2">
  334. /// The second token.
  335. /// </param>
  336. /// <returns>
  337. /// Returns the joined locations.
  338. /// </returns>
  339. internal static CodeLocation JoinLocations(CsToken token1, CsToken token2)
  340. {
  341. Param.AssertNotNull(token1, "token1");
  342. Param.AssertNotNull(token2, "token2");
  343. return CodeLocation.Join(token1.Location, token2.Location);
  344. }
  345. /////// <summary>
  346. /////// Joins the locations of the two tokens.
  347. /////// </summary>
  348. /////// <param name="token1">The first token.</param>
  349. /////// <param name="token2">The second token.</param>
  350. /////// <returns>Returns the joined locations.</returns>
  351. ////internal static CodeLocation JoinLocations(Node<CsToken> token1, CsToken token2)
  352. ////{
  353. //// Param.Ignore(token1, token2);
  354. //// return CsToken.JoinLocations(token1 == null ? null : token1.Value, token2);
  355. ////}
  356. /////// <summary>
  357. /////// Joins the locations of the two tokens.
  358. /////// </summary>
  359. /////// <param name="token1">The first token.</param>
  360. /////// <param name="token2">The second token.</param>
  361. /////// <returns>Returns the joined locations.</returns>
  362. ////internal static CodeLocation JoinLocations(CsToken token1, Node<CsToken> token2)
  363. ////{
  364. //// Param.Ignore(token1, token2);
  365. //// return CsToken.JoinLocations(token1, token2 == null ? null : token2.Value);
  366. ////}
  367. /// <summary>
  368. /// Joins the locations of the two tokens.
  369. /// </summary>
  370. /// <param name="token1">
  371. /// The first token.
  372. /// </param>
  373. /// <param name="token2">
  374. /// The second token.
  375. /// </param>
  376. /// <returns>
  377. /// Returns the joined locations.
  378. /// </returns>
  379. internal static CodeLocation JoinLocations(Node<CsToken> token1, Node<CsToken> token2)
  380. {
  381. Param.Ignore(token1, token2);
  382. return CsToken.JoinLocations(token1 == null ? null : token1.Value, token2 == null ? null : token2.Value);
  383. }
  384. /// <summary>
  385. /// Creates the text string for the token.
  386. /// </summary>
  387. protected virtual void CreateTextString()
  388. {
  389. }
  390. #endregion
  391. }
  392. }