Browse Source

Fixed end location of preprocessing directives.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2987 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
abbd911bc1
  1. 7
      src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs
  2. 29
      src/Libraries/NRefactory/Test/Lexer/CSharp/PreprocessingTests.cs

7
src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs

@ -1061,6 +1061,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -1061,6 +1061,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
PPWhitespace();
if (parseIfExpression && directive == "#if" || parseElifExpression && directive == "#elif") {
Ast.Expression expr = PPExpression();
Location endLocation = new Location(Col, Line);
int c = ReaderRead();
if (c >= 0 && !HandleLineEnd((char)c)) {
if (c == '/' && ReaderRead() == '/') {
@ -1070,14 +1071,16 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -1070,14 +1071,16 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
}
SkipToEndOfLine(); // skip comment
}
return new PreprocessingDirective(directive, null, start, new Location(Col, Line)) { Expression = expr };
return new PreprocessingDirective(directive, null, start, endLocation) { Expression = expr };
} else {
Location endLocation = new Location(Col, Line);
string arg = ReadToEndOfLine();
endLocation.Column += arg.Length;
int pos = arg.IndexOf("//");
if (pos >= 0)
arg = arg.Substring(0, pos);
arg = arg.Trim();
return new PreprocessingDirective(directive, arg, start, new Location(Col, Line));
return new PreprocessingDirective(directive, arg, start, endLocation);
}
}

29
src/Libraries/NRefactory/Test/Lexer/CSharp/PreprocessingTests.cs

@ -133,10 +133,37 @@ namespace ICSharpCode.NRefactory.Tests.Lexer.CSharp @@ -133,10 +133,37 @@ namespace ICSharpCode.NRefactory.Tests.Lexer.CSharp
/*
#else
/* */ class
#endif
#endif
";
Assert.AreEqual(new int[] { Tokens.Class, Tokens.EOF }, GetTokenKinds(program));
Assert.AreEqual(new int[] { Tokens.Struct, Tokens.Class, Tokens.EOF }, GetTokenKinds("#define X\n" + program));
}
[Test]
public void Region()
{
string program = @"
#region Region Title
;
#endregion
,";
Assert.AreEqual(new int[] { Tokens.Semicolon, Tokens.Comma, Tokens.EOF }, GetTokenKinds(program));
ILexer lexer = GenerateLexer(program);
while (lexer.NextToken().kind != Tokens.EOF);
List<ISpecial> specials = lexer.SpecialTracker.RetrieveSpecials();
Assert.IsTrue(specials[0] is BlankLine);
Assert.AreEqual(new Location(2, 1), specials[0].StartPosition);
Assert.AreEqual(new Location(2, 1), specials[0].EndPosition);
Assert.AreEqual("#region", (specials[1] as PreprocessingDirective).Cmd);
Assert.AreEqual("Region Title", (specials[1] as PreprocessingDirective).Arg);
Assert.AreEqual(new Location(2, 2), specials[1].StartPosition);
Assert.AreEqual(new Location(22, 2), specials[1].EndPosition);
Assert.AreEqual("#endregion", (specials[2] as PreprocessingDirective).Cmd);
Assert.AreEqual(new Location(2, 4), specials[2].StartPosition);
Assert.AreEqual(new Location(12, 4), specials[2].EndPosition);
}
}
}

Loading…
Cancel
Save