// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.NRefactory.VB.Ast
{
///
/// Description of VBModifierToken.
///
public class VBModifierToken : VBTokenNode
{
Modifiers modifier;
public Modifiers Modifier {
get { return modifier; }
set {
for (int i = 0; i < lengthTable.Count; i++) {
if (lengthTable[i].Key == value) {
this.modifier = value;
this.tokenLength = lengthTable[i].Value;
return;
}
}
throw new ArgumentException ("Modifier " + value + " is invalid.");
}
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
VBModifierToken o = other as VBModifierToken;
return o != null && this.modifier == o.modifier;
}
// Not worth using a dictionary for such few elements.
// This table is sorted in the order that modifiers should be output when generating code.
static readonly List> lengthTable = new List> () {
new KeyValuePair(Modifiers.Public, "Public".Length),
new KeyValuePair(Modifiers.Protected, "Protected".Length),
new KeyValuePair(Modifiers.Private, "Private".Length),
new KeyValuePair(Modifiers.Friend, "Friend".Length),
new KeyValuePair(Modifiers.MustInherit, "MustInherit".Length),
new KeyValuePair(Modifiers.MustOverride, "MustOverride".Length),
new KeyValuePair(Modifiers.Overridable, "Overridable".Length),
new KeyValuePair(Modifiers.NotInheritable, "NotInheritable".Length),
new KeyValuePair(Modifiers.NotOverridable, "NotOverridable".Length),
new KeyValuePair(Modifiers.Dim, "Dim".Length),
new KeyValuePair(Modifiers.Const, "Const".Length),
new KeyValuePair(Modifiers.Shared, "Shared".Length),
new KeyValuePair(Modifiers.Static, "Static".Length),
new KeyValuePair(Modifiers.Overrides, "Overrides".Length),
new KeyValuePair(Modifiers.ReadOnly, "ReadOnly".Length),
new KeyValuePair(Modifiers.WriteOnly, "WriteOnly".Length),
new KeyValuePair(Modifiers.Shadows, "Shadows".Length),
new KeyValuePair(Modifiers.Partial, "Partial".Length),
new KeyValuePair(Modifiers.Overloads, "Overloads".Length),
new KeyValuePair(Modifiers.WithEvents, "WithEvents".Length),
new KeyValuePair(Modifiers.Default, "Default".Length),
// parameter modifiers
new KeyValuePair(Modifiers.Optional, "Optional".Length),
new KeyValuePair(Modifiers.ByVal, "ByVal".Length),
new KeyValuePair(Modifiers.ByRef, "ByRef".Length),
new KeyValuePair(Modifiers.ParamArray, "ParamArray".Length),
// operator modifiers
new KeyValuePair(Modifiers.Narrowing, "Narrowing".Length),
new KeyValuePair(Modifiers.Widening, "Widening".Length),
// VB 11 modifiers
new KeyValuePair(Modifiers.Async, "Async".Length),
new KeyValuePair(Modifiers.Iterator, "Iterator".Length),
// even though it's used for patterns only, it needs to be in this table to be usable in the AST
new KeyValuePair(Modifiers.Any, "Any".Length)
};
public static IEnumerable AllModifiers {
get { return lengthTable.Select(p => p.Key); }
}
public VBModifierToken(TextLocation location, Modifiers modifier) : base (location, 0)
{
this.Modifier = modifier;
}
public static string GetModifierName(Modifiers modifier)
{
switch (modifier) {
case Modifiers.Private:
return "Private";
case Modifiers.Friend:
return "Friend";
case Modifiers.Protected:
return "Protected";
case Modifiers.Public:
return "Public";
case Modifiers.MustInherit:
return "MustInherit";
case Modifiers.MustOverride:
return "MustOverride";
case Modifiers.Overridable:
return "Overridable";
case Modifiers.NotInheritable:
return "NotInheritable";
case Modifiers.NotOverridable:
return "NotOverridable";
case Modifiers.Const:
return "Const";
case Modifiers.Shared:
return "Shared";
case Modifiers.Static:
return "Static";
case Modifiers.Overrides:
return "Overrides";
case Modifiers.ReadOnly:
return "ReadOnly";
case Modifiers.Shadows:
return "Shadows";
case Modifiers.Partial:
return "Partial";
case Modifiers.Overloads:
return "Overloads";
case Modifiers.WithEvents:
return "WithEvents";
case Modifiers.Default:
return "Default";
case Modifiers.Dim:
return "Dim";
case Modifiers.WriteOnly:
return "WriteOnly";
case Modifiers.Optional:
return "Optional";
case Modifiers.ByVal:
return "ByVal";
case Modifiers.ByRef:
return "ByRef";
case Modifiers.ParamArray:
return "ParamArray";
case Modifiers.Widening:
return "Widening";
case Modifiers.Narrowing:
return "Narrowing";
case Modifiers.Async:
return "Async";
case Modifiers.Iterator:
return "Iterator";
default:
throw new NotSupportedException("Invalid value for Modifiers: " + modifier);
}
}
}
}