Browse Source

[Ast] Getting the end location of token nodes is now faster.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
5d20faf0c5
  1. 55
      ICSharpCode.NRefactory.CSharp/Ast/CSharpModifierToken.cs
  2. 20
      ICSharpCode.NRefactory.CSharp/Ast/CSharpTokenNode.cs
  3. 6
      ICSharpCode.NRefactory.CSharp/Ast/ComposedType.cs
  4. 32
      ICSharpCode.NRefactory.CSharp/Ast/TokenRole.cs
  5. 784
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  6. 6
      ICSharpCode.NRefactory.CSharp/QueryExpressionExpander.cs
  7. 6
      ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs

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

@ -40,13 +40,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -40,13 +40,13 @@ namespace ICSharpCode.NRefactory.CSharp
this.modifier = value;
}
}
protected override int TokenLength {
public override TextLocation EndLocation {
get {
return GetModifierName (modifier).Length;
return new TextLocation (StartLocation.Line, StartLocation.Column + GetModifierLength (Modifier));
}
}
public override string GetText (CSharpFormattingOptions formattingOptions = null)
{
return GetModifierName (Modifier);
@ -75,7 +75,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -75,7 +75,7 @@ namespace ICSharpCode.NRefactory.CSharp
get { return allModifiers; }
}
public CSharpModifierToken (TextLocation location, Modifiers modifier) : base (location)
public CSharpModifierToken (TextLocation location, Modifiers modifier) : base (location, null)
{
this.Modifier = modifier;
}
@ -124,5 +124,50 @@ namespace ICSharpCode.NRefactory.CSharp @@ -124,5 +124,50 @@ namespace ICSharpCode.NRefactory.CSharp
throw new NotSupportedException("Invalid value for Modifiers");
}
}
public static int GetModifierLength(Modifiers modifier)
{
switch (modifier) {
case Modifiers.Private:
return "private".Length;
case Modifiers.Internal:
return "internal".Length;
case Modifiers.Protected:
return "protected".Length;
case Modifiers.Public:
return "public".Length;
case Modifiers.Abstract:
return "abstract".Length;
case Modifiers.Virtual:
return "virtual".Length;
case Modifiers.Sealed:
return "sealed".Length;
case Modifiers.Static:
return "static".Length;
case Modifiers.Override:
return "override".Length;
case Modifiers.Readonly:
return "readonly".Length;
case Modifiers.Const:
return "const".Length;
case Modifiers.New:
return "new".Length;
case Modifiers.Partial:
return "partial".Length;
case Modifiers.Extern:
return "extern".Length;
case Modifiers.Volatile:
return "volatile".Length;
case Modifiers.Unsafe:
return "unsafe".Length;
case Modifiers.Async:
return "async".Length;
case Modifiers.Any:
// even though it's used for pattern matching only, 'any' needs to be in this list to be usable in the AST
return "any".Length;
default:
throw new NotSupportedException("Invalid value for Modifiers");
}
}
}
}

20
ICSharpCode.NRefactory.CSharp/Ast/CSharpTokenNode.cs

@ -44,7 +44,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -44,7 +44,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
public NullCSharpTokenNode () : base (TextLocation.Empty)
public NullCSharpTokenNode () : base (TextLocation.Empty, null)
{
}
@ -80,12 +80,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -80,12 +80,10 @@ namespace ICSharpCode.NRefactory.CSharp
return startLocation;
}
}
protected virtual int TokenLength {
int TokenLength {
get {
if (!(Role is TokenRole))
return 0;
return ((TokenRole)Role).Length;
return TokenRole.TokenLengths [(int)(this.flags >> AstNodeFlagsUsedBits)];
}
}
@ -94,17 +92,17 @@ namespace ICSharpCode.NRefactory.CSharp @@ -94,17 +92,17 @@ namespace ICSharpCode.NRefactory.CSharp
return new TextLocation (StartLocation.Line, StartLocation.Column + TokenLength);
}
}
public CSharpTokenNode (TextLocation location)
public CSharpTokenNode (TextLocation location, TokenRole role)
{
this.startLocation = location;
if (role != null)
this.flags |= role.TokenIndex << AstNodeFlagsUsedBits;
}
public override string GetText (CSharpFormattingOptions formattingOptions = null)
{
if (!(Role is TokenRole))
return null;
return ((TokenRole)Role).Token;
return TokenRole.Tokens [(int)(this.flags >> AstNodeFlagsUsedBits)];
}
public override void AcceptVisitor (IAstVisitor visitor)

6
ICSharpCode.NRefactory.CSharp/Ast/ComposedType.cs

@ -47,7 +47,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -47,7 +47,7 @@ namespace ICSharpCode.NRefactory.CSharp
return !GetChildByRole(NullableRole).IsNull;
}
set {
SetChildByRole(NullableRole, value ? new CSharpTokenNode(TextLocation.Empty) : null);
SetChildByRole(NullableRole, value ? new CSharpTokenNode(TextLocation.Empty, null) : null);
}
}
@ -64,7 +64,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -64,7 +64,7 @@ namespace ICSharpCode.NRefactory.CSharp
d--;
}
while (d < value) {
InsertChildBefore(GetChildByRole(PointerRole), new CSharpTokenNode(TextLocation.Empty), PointerRole);
InsertChildBefore(GetChildByRole(PointerRole), new CSharpTokenNode(TextLocation.Empty, PointerRole), PointerRole);
d++;
}
}
@ -178,7 +178,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -178,7 +178,7 @@ namespace ICSharpCode.NRefactory.CSharp
d--;
}
while (d < value) {
InsertChildBefore(GetChildByRole(Roles.Comma), new CSharpTokenNode(TextLocation.Empty), Roles.Comma);
InsertChildBefore(GetChildByRole(Roles.Comma), new CSharpTokenNode(TextLocation.Empty, Roles.Comma), Roles.Comma);
d++;
}
}

32
ICSharpCode.NRefactory.CSharp/Ast/TokenRole.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp
{
@ -7,6 +8,17 @@ namespace ICSharpCode.NRefactory.CSharp @@ -7,6 +8,17 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
public sealed class TokenRole : Role<CSharpTokenNode>
{
internal readonly static List<string> Tokens = new List<string> ();
internal readonly static List<int> TokenLengths = new List<int> ();
internal readonly uint TokenIndex;
static TokenRole ()
{
// null token
Tokens.Add ("");
TokenLengths.Add (0);
}
/// <summary>
/// Gets the token as string. Note that the token Name and Token value may differ.
/// </summary>
@ -22,11 +34,27 @@ namespace ICSharpCode.NRefactory.CSharp @@ -22,11 +34,27 @@ namespace ICSharpCode.NRefactory.CSharp
get;
private set;
}
public TokenRole (string token) : base (token, CSharpTokenNode.Null)
public TokenRole(string token) : base (token, CSharpTokenNode.Null)
{
this.Token = token;
this.Length = token.Length;
bool found = false;
for (int i = 0; i < Tokens.Count; i++) {
var existingToken = Tokens [i];
if (existingToken == token) {
TokenIndex = (uint)i;
found = true;
break;
}
}
if (!found) {
TokenIndex = (uint)Tokens.Count;
Tokens.Add (token);
TokenLengths.Add (this.Length);
}
}
}
}

784
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

File diff suppressed because it is too large Load Diff

6
ICSharpCode.NRefactory.CSharp/QueryExpressionExpander.cs

@ -122,14 +122,14 @@ namespace ICSharpCode.NRefactory.CSharp { @@ -122,14 +122,14 @@ namespace ICSharpCode.NRefactory.CSharp {
LambdaExpression CreateLambda(IList<ParameterDeclaration> parameters, Expression body) {
var result = new LambdaExpression();
if (parameters.Count > 1)
result.AddChild(new CSharpTokenNode(TextLocation.Empty), Roles.LPar);
result.AddChild(new CSharpTokenNode(TextLocation.Empty, Roles.LPar), Roles.LPar);
result.AddChild(parameters[0], Roles.Parameter);
for (int i = 1; i < parameters.Count; i++) {
result.AddChild(new CSharpTokenNode(TextLocation.Empty), Roles.Comma);
result.AddChild(new CSharpTokenNode(TextLocation.Empty, Roles.Comma), Roles.Comma);
result.AddChild(parameters[i], Roles.Parameter);
}
if (parameters.Count > 1)
result.AddChild(new CSharpTokenNode(TextLocation.Empty), Roles.RPar);
result.AddChild(new CSharpTokenNode(TextLocation.Empty, Roles.RPar), Roles.RPar);
result.AddChild(body, LambdaExpression.BodyRole);
return result;

6
ICSharpCode.NRefactory.Tests/CSharp/CSharpOutputVisitorTests.cs

@ -86,11 +86,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -86,11 +86,11 @@ namespace ICSharpCode.NRefactory.CSharp
public void InlineCommentAtEndOfCondition()
{
IfElseStatement condition = new IfElseStatement();
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 1)), IfElseStatement.IfKeywordRole);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 4)), Roles.LPar);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 1), IfElseStatement.IfKeywordRole), IfElseStatement.IfKeywordRole);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 4), Roles.LPar), Roles.LPar);
condition.AddChild(new IdentifierExpression("cond", new TextLocation(1, 5)), IfElseStatement.ConditionRole);
condition.AddChild(new Comment(CommentType.MultiLine, new TextLocation(1, 9), new TextLocation(1, 14)) { Content = "a" }, Roles.Comment);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 14)), Roles.RPar);
condition.AddChild(new CSharpTokenNode(new TextLocation(1, 14), Roles.RPar), Roles.RPar);
condition.AddChild(new ReturnStatement(), IfElseStatement.TrueRole);
AssertOutput("if (cond/*a*/)\n$return;\n", condition);

Loading…
Cancel
Save