1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// 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
{
/// <summary>
/// Description of VBModifierToken.
/// </summary>
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<KeyValuePair<Modifiers, int>> lengthTable = new List<KeyValuePair<Modifiers, int>> () {
new KeyValuePair<Modifiers, int>(Modifiers.Public, "Public".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Protected, "Protected".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Private, "Private".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Friend, "Friend".Length),
new KeyValuePair<Modifiers, int>(Modifiers.MustInherit, "MustInherit".Length),
new KeyValuePair<Modifiers, int>(Modifiers.MustOverride, "MustOverride".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Overridable, "Overridable".Length),
new KeyValuePair<Modifiers, int>(Modifiers.NotInheritable, "NotInheritable".Length),
new KeyValuePair<Modifiers, int>(Modifiers.NotOverridable, "NotOverridable".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Dim, "Dim".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Const, "Const".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Shared, "Shared".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Static, "Static".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Overrides, "Overrides".Length),
new KeyValuePair<Modifiers, int>(Modifiers.ReadOnly, "ReadOnly".Length),
new KeyValuePair<Modifiers, int>(Modifiers.WriteOnly, "WriteOnly".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Shadows, "Shadows".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Partial, "Partial".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Overloads, "Overloads".Length),
new KeyValuePair<Modifiers, int>(Modifiers.WithEvents, "WithEvents".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Default, "Default".Length),
// parameter modifiers
new KeyValuePair<Modifiers, int>(Modifiers.Optional, "Optional".Length),
new KeyValuePair<Modifiers, int>(Modifiers.ByVal, "ByVal".Length),
new KeyValuePair<Modifiers, int>(Modifiers.ByRef, "ByRef".Length),
new KeyValuePair<Modifiers, int>(Modifiers.ParamArray, "ParamArray".Length),
// operator modifiers
new KeyValuePair<Modifiers, int>(Modifiers.Narrowing, "Narrowing".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Widening, "Widening".Length),
// VB 11 modifiers
new KeyValuePair<Modifiers, int>(Modifiers.Async, "Async".Length),
new KeyValuePair<Modifiers, int>(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, int>(Modifiers.Any, "Any".Length)
};
public static IEnumerable<Modifiers> 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);
}
}
}
}
|