PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/CSharpModifierToken.cs

http://github.com/icsharpcode/ILSpy
C# | 218 lines | 177 code | 11 blank | 30 comment | 6 complexity | 890d55312c4af90a8dabbee174f68cde MD5 | raw file
  1// 
  2// CSharpModifierToken.cs
  3//
  4// Author:
  5//       Mike Kr├╝ger <mkrueger@novell.com>
  6// 
  7// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
  8// 
  9// Permission is hereby granted, free of charge, to any person obtaining a copy
 10// of this software and associated documentation files (the "Software"), to deal
 11// in the Software without restriction, including without limitation the rights
 12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 13// copies of the Software, and to permit persons to whom the Software is
 14// furnished to do so, subject to the following conditions:
 15// 
 16// The above copyright notice and this permission notice shall be included in
 17// all copies or substantial portions of the Software.
 18// 
 19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 25// THE SOFTWARE.
 26using System;
 27using System.Collections.Generic;
 28using System.Linq;
 29
 30namespace ICSharpCode.NRefactory.CSharp
 31{
 32	public class CSharpModifierToken : CSharpTokenNode
 33	{
 34		Modifiers modifier;
 35		
 36		public Modifiers Modifier {
 37			get { return modifier; }
 38			set { 
 39				ThrowIfFrozen();
 40				this.modifier = value; 
 41			}
 42		}
 43
 44		public override TextLocation EndLocation {
 45			get {
 46				return new TextLocation (StartLocation.Line, StartLocation.Column + GetModifierLength (Modifier));
 47			}
 48		}
 49
 50		public override string ToString(CSharpFormattingOptions formattingOptions)
 51		{
 52			return GetModifierName (Modifier);
 53		}
 54		
 55		protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 56		{
 57			CSharpModifierToken o = other as CSharpModifierToken;
 58			return o != null && this.modifier == o.modifier;
 59		}
 60		
 61		// Not worth using a dictionary for such few elements.
 62		// This table is sorted in the order that modifiers should be output when generating code.
 63		static readonly Modifiers[] allModifiers = {
 64			Modifiers.Public, Modifiers.Protected, Modifiers.Private, Modifiers.Internal,
 65			Modifiers.New,
 66			Modifiers.Unsafe,
 67			Modifiers.Abstract, Modifiers.Virtual, Modifiers.Sealed, Modifiers.Static, Modifiers.Override,
 68			Modifiers.Readonly, Modifiers.Volatile,
 69			Modifiers.Extern, Modifiers.Partial, Modifiers.Const,
 70			Modifiers.Async,
 71			Modifiers.Any
 72		};
 73		
 74		public static IEnumerable<Modifiers> AllModifiers {
 75			get { return allModifiers; }
 76		}
 77		
 78		public CSharpModifierToken (TextLocation location, Modifiers modifier) : base (location, null)
 79		{
 80			this.Modifier = modifier;
 81		}
 82		
 83		public static string GetModifierName(Modifiers modifier)
 84		{
 85			switch (modifier) {
 86				case Modifiers.Private:
 87					return "private";
 88				case Modifiers.Internal:
 89					return "internal";
 90				case Modifiers.Protected:
 91					return "protected";
 92				case Modifiers.Public:
 93					return "public";
 94				case Modifiers.Abstract:
 95					return "abstract";
 96				case Modifiers.Virtual:
 97					return "virtual";
 98				case Modifiers.Sealed:
 99					return "sealed";
100				case Modifiers.Static:
101					return "static";
102				case Modifiers.Override:
103					return "override";
104				case Modifiers.Readonly:
105					return "readonly";
106				case Modifiers.Const:
107					return "const";
108				case Modifiers.New:
109					return "new";
110				case Modifiers.Partial:
111					return "partial";
112				case Modifiers.Extern:
113					return "extern";
114				case Modifiers.Volatile:
115					return "volatile";
116				case Modifiers.Unsafe:
117					return "unsafe";
118				case Modifiers.Async:
119					return "async";
120				case Modifiers.Any:
121					// even though it's used for pattern matching only, 'any' needs to be in this list to be usable in the AST
122					return "any";
123				default:
124					throw new NotSupportedException("Invalid value for Modifiers");
125			}
126		}
127
128		public static int GetModifierLength(Modifiers modifier)
129		{
130			switch (modifier) {
131				case Modifiers.Private:
132					return "private".Length;
133				case Modifiers.Internal:
134					return "internal".Length;
135				case Modifiers.Protected:
136					return "protected".Length;
137				case Modifiers.Public:
138					return "public".Length;
139				case Modifiers.Abstract:
140					return "abstract".Length;
141				case Modifiers.Virtual:
142					return "virtual".Length;
143				case Modifiers.Sealed:
144					return "sealed".Length;
145				case Modifiers.Static:
146					return "static".Length;
147				case Modifiers.Override:
148					return "override".Length;
149				case Modifiers.Readonly:
150					return "readonly".Length;
151				case Modifiers.Const:
152					return "const".Length;
153				case Modifiers.New:
154					return "new".Length;
155				case Modifiers.Partial:
156					return "partial".Length;
157				case Modifiers.Extern:
158					return "extern".Length;
159				case Modifiers.Volatile:
160					return "volatile".Length;
161				case Modifiers.Unsafe:
162					return "unsafe".Length;
163				case Modifiers.Async:
164					return "async".Length;
165				case Modifiers.Any:
166					// even though it's used for pattern matching only, 'any' needs to be in this list to be usable in the AST
167					return "any".Length;
168				default:
169					throw new NotSupportedException("Invalid value for Modifiers");
170			}
171		}
172		
173		public static Modifiers GetModifierValue(string modifier)
174		{
175			switch (modifier) {
176				case "private":
177					return Modifiers.Private;
178				case "internal":
179					return Modifiers.Internal;
180				case "protected":
181					return Modifiers.Protected;
182				case "public":
183					return Modifiers.Public;
184				case "abstract":
185					return Modifiers.Abstract;
186				case "virtual":
187					return Modifiers.Virtual;
188				case "sealed":
189					return Modifiers.Sealed;
190				case "static":
191					return Modifiers.Static;
192				case "override":
193					return Modifiers.Override;
194				case "readonly":
195					return Modifiers.Readonly;
196				case "const":
197					return Modifiers.Const;
198				case "new":
199					return Modifiers.New;
200				case "partial":
201					return Modifiers.Partial;
202				case "extern":
203					return Modifiers.Extern;
204				case "volatile":
205					return Modifiers.Volatile;
206				case "unsafe":
207					return Modifiers.Unsafe;
208				case "async":
209					return Modifiers.Async;
210				case "any":
211					// even though it's used for pattern matching only, 'any' needs to be in this list to be usable in the AST
212					return Modifiers.Any;
213				default:
214					throw new NotSupportedException("Invalid value for Modifiers");
215			}
216		}
217	}
218}