Browse Source

Fixed snippet parser bugs.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2522 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
97342e3d49
  1. 10
      samples/HtmlSyntaxColorizer/HtmlWriter.cs
  2. 5
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/CSharpParser.cs
  3. 639
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  4. 5
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  5. 4
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNetParser.cs
  6. 8
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/AbstractOutputFormatter.cs
  7. 12
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs
  8. 23
      src/Libraries/NRefactory/Project/Src/SnippetParser.cs
  9. 1
      src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs
  10. 55
      src/Libraries/NRefactory/Test/Output/SnippetConversion.cs
  11. 6
      src/Libraries/NRefactory/Test/Parser/Expressions/PrimitiveExpressionTests.cs
  12. 1
      src/Libraries/NRefactory/Test/Parser/ParseUtilCSharp.cs

10
samples/HtmlSyntaxColorizer/HtmlWriter.cs

@ -82,15 +82,15 @@ namespace ICSharpCode.HtmlSyntaxColorizer @@ -82,15 +82,15 @@ namespace ICSharpCode.HtmlSyntaxColorizer
string myMainStyle = MainStyle;
currentDefaultTextColor = document.HighlightingStrategy.GetColorFor("Default");
myMainStyle += " color: " + ColorToString(currentDefaultTextColor.Color) + ";"
+ " background-color: " + ColorToString(currentDefaultTextColor.BackgroundColor);
+ " background-color: " + ColorToString(currentDefaultTextColor.BackgroundColor) + ";";
string LineNumberStyle;
HighlightColor lineNumbersColor = document.HighlightingStrategy.GetColorFor("LineNumbers");
if (lineNumbersColor != null) {
LineNumberStyle = " color: " + ColorToString(lineNumbersColor.Color) + ";"
+ " background-color: " + ColorToString(lineNumbersColor.BackgroundColor);
LineNumberStyle = "color: " + ColorToString(lineNumbersColor.Color) + ";"
+ " background-color: " + ColorToString(lineNumbersColor.BackgroundColor) + ";";
} else {
LineNumberStyle = " color: #606060";
LineNumberStyle = "color: #606060;";
}
StringBuilder b = new StringBuilder();
@ -222,7 +222,7 @@ namespace ICSharpCode.HtmlSyntaxColorizer @@ -222,7 +222,7 @@ namespace ICSharpCode.HtmlSyntaxColorizer
return style;
}
string ColorToString(System.Drawing.Color color)
static string ColorToString(System.Drawing.Color color)
{
return "#" + color.R.ToString("x2") + color.G.ToString("x2") + color.B.ToString("x2");
}

5
src/Libraries/NRefactory/Project/Src/Parser/CSharp/CSharpParser.cs

@ -51,6 +51,9 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -51,6 +51,9 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
lexer.NextToken();
Expression expr;
Expr(out expr);
// SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
if (la.kind == Tokens.Semicolon) lexer.NextToken();
Expect(Tokens.EOF);
return expr;
}
@ -73,6 +76,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -73,6 +76,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
}
compilationUnit.BlockEnd();
Expect(Tokens.EOF);
return blockStmt;
}
@ -85,6 +89,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -85,6 +89,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
compilationUnit.BlockStart(newType);
ClassBody();
compilationUnit.BlockEnd();
Expect(Tokens.EOF);
return newType.Children;
}

639
src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs

File diff suppressed because it is too large Load Diff

5
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG

@ -2186,7 +2186,8 @@ Block<out Statement stmt> @@ -2186,7 +2186,8 @@ Block<out Statement stmt>
=
(.
BlockStatement blockStmt = new BlockStatement();
blockStmt.StartLocation = t.Location;
/* in snippet parsing mode, t might be null */
if (t != null) blockStmt.StartLocation = t.Location;
compilationUnit.BlockStart(blockStmt);
.)
{
@ -2196,7 +2197,7 @@ Block<out Statement stmt> @@ -2196,7 +2197,7 @@ Block<out Statement stmt>
}
(.
stmt = blockStmt;
blockStmt.EndLocation = t.EndLocation;
if (t != null) blockStmt.EndLocation = t.EndLocation;
compilationUnit.BlockEnd();
.)
.

4
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNetParser.cs

@ -63,6 +63,8 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -63,6 +63,8 @@ namespace ICSharpCode.NRefactory.Parser.VB
lexer.NextToken();
Expression expr;
Expr(out expr);
while (la.kind == Tokens.EOL) lexer.NextToken();
Expect(Tokens.EOF);
return expr;
}
@ -73,6 +75,7 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -73,6 +75,7 @@ namespace ICSharpCode.NRefactory.Parser.VB
Statement st;
Block(out st);
Expect(Tokens.EOF);
return st as BlockStatement;
}
@ -85,6 +88,7 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -85,6 +88,7 @@ namespace ICSharpCode.NRefactory.Parser.VB
compilationUnit.BlockStart(newType);
ClassBody(newType);
compilationUnit.BlockEnd();
Expect(Tokens.EOF);
return newType.Children;
}

8
src/Libraries/NRefactory/Project/Src/PrettyPrinter/AbstractOutputFormatter.cs

@ -16,7 +16,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -16,7 +16,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
/// </summary>
public abstract class AbstractOutputFormatter : IOutputFormatter
{
internal StringBuilder text = new StringBuilder();
StringBuilder text = new StringBuilder();
int indentationLevel = 0;
bool indent = true;
@ -38,6 +38,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -38,6 +38,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
}
}
public int TextLength {
get {
return text.Length;
}
}
public bool DoIndent {
get {

12
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs

@ -930,6 +930,18 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -930,6 +930,18 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public object VisitBlockStatement(BlockStatement blockStatement, object data)
{
if (outputFormatter.TextLength == 0) {
// we are outputting only a code block:
// do not output braces, just the block's contents
foreach (Statement stmt in blockStatement.Children) {
outputFormatter.Indent();
nodeTracker.TrackedVisit(stmt, null);
if (!outputFormatter.LastCharacterIsNewLine)
outputFormatter.NewLine();
}
return null;
}
if (data is BraceStyle)
OutputBlock(blockStatement, (BraceStyle)data);
else

23
src/Libraries/NRefactory/Project/Src/SnippetParser.cs

@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory @@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory
}
/// <summary>
/// Parse the code. The result may be a CompilationUnit, an Expression, a BlockStatement or a list of class
/// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
/// members.
/// </summary>
public INode Parse(string code)
@ -55,7 +55,12 @@ namespace ICSharpCode.NRefactory @@ -55,7 +55,12 @@ namespace ICSharpCode.NRefactory
INode result = parser.CompilationUnit;
if (errors.Count > 0) {
parser = ParserFactory.CreateParser(language, new StringReader(code));
if (language == SupportedLanguage.CSharp) {
// SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
parser = ParserFactory.CreateParser(language, new StringReader(code + ";"));
} else {
parser = ParserFactory.CreateParser(language, new StringReader(code));
}
Expression expression = parser.ParseExpression();
if (expression != null && parser.Errors.Count < errors.Count) {
errors = parser.Errors;
@ -78,19 +83,19 @@ namespace ICSharpCode.NRefactory @@ -78,19 +83,19 @@ namespace ICSharpCode.NRefactory
if (members != null && members.Count > 0 && parser.Errors.Count < errors.Count) {
errors = parser.Errors;
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
result = new MemberListNode(members);
result = new NodeListNode(members);
}
}
return result;
}
sealed class MemberListNode : INode
sealed class NodeListNode : INode
{
List<INode> members;
List<INode> nodes;
public MemberListNode(List<INode> members)
public NodeListNode(List<INode> nodes)
{
this.members = members;
this.nodes = nodes;
}
public INode Parent {
@ -99,7 +104,7 @@ namespace ICSharpCode.NRefactory @@ -99,7 +104,7 @@ namespace ICSharpCode.NRefactory
}
public List<INode> Children {
get { return members; }
get { return nodes; }
}
public Location StartLocation {
@ -114,7 +119,7 @@ namespace ICSharpCode.NRefactory @@ -114,7 +119,7 @@ namespace ICSharpCode.NRefactory
public object AcceptChildren(IAstVisitor visitor, object data)
{
foreach (INode n in members) {
foreach (INode n in nodes) {
n.AcceptVisitor(visitor, data);
}
return null;

1
src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs

@ -45,6 +45,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -45,6 +45,7 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
void TestExpression(string expression)
{
// SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(expression + ";"));
Expression e = parser.ParseExpression();
Assert.AreEqual("", parser.Errors.ErrorOutput);

55
src/Libraries/NRefactory/Test/Output/SnippetConversion.cs

@ -39,6 +39,29 @@ namespace ICSharpCode.NRefactory.Tests.Output @@ -39,6 +39,29 @@ namespace ICSharpCode.NRefactory.Tests.Output
Assert.AreEqual(expectedOutput, output.Text);
}
void VB2CS(string input, string expectedOutput)
{
SnippetParser parser = new SnippetParser(SupportedLanguage.VBNet);
INode node = parser.Parse(input);
// parser.Errors.ErrorOutput contains syntax errors, if any
Assert.IsNotNull(node);
Assert.AreEqual("", parser.Errors.ErrorOutput);
// parser.Specials is the list of comments, preprocessor directives etc.
PreprocessingDirective.VBToCSharp(parser.Specials);
// Convert VB.NET constructs to C#:
node.AcceptVisitor(new VBNetConstructsConvertVisitor(), null);
node.AcceptVisitor(new ToCSharpConvertVisitor(), null);
CSharpOutputVisitor output = new CSharpOutputVisitor();
using (SpecialNodesInserter.Install(parser.Specials, output)) {
node.AcceptVisitor(output, null);
}
// output.Errors.ErrorOutput contains conversion errors/warnings, if any
// output.Text contains the converted code
Assert.AreEqual("", output.Errors.ErrorOutput);
Assert.AreEqual(expectedOutput, output.Text);
}
[Test]
public void CompilationUnitCS2VB()
{
@ -108,5 +131,37 @@ a += 1 @@ -108,5 +131,37 @@ a += 1
"
);
}
[Test]
public void TypeMembersVB2CS()
{
VB2CS(
@"Sub Test()
End Sub
Sub Test2()
End Sub
",
@"public void Test()
{
}
public void Test2()
{
}
"
);
}
[Test]
public void StatementsVB2CS()
{
VB2CS(
@"Dim a As Integer = 3
a += 1
",
"int a = 3;\r\n" +
"a += 1;\r\n"
);
}
}
}

6
src/Libraries/NRefactory/Test/Parser/Expressions/PrimitiveExpressionTests.cs

@ -37,7 +37,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast @@ -37,7 +37,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
[Test]
public void CSharpDoubleTest1()
{
PrimitiveExpression pe = ParseUtilCSharp.ParseExpression<PrimitiveExpression>(".5e-06;");
PrimitiveExpression pe = ParseUtilCSharp.ParseExpression<PrimitiveExpression>(".5e-06");
Assert.AreEqual(".5e-06", pe.StringValue);
Assert.AreEqual(.5e-06, (double)pe.Value);
}
@ -45,7 +45,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast @@ -45,7 +45,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
[Test]
public void CSharpCharTest1()
{
PrimitiveExpression pe = ParseUtilCSharp.ParseExpression<PrimitiveExpression>("'\\u0356';");
PrimitiveExpression pe = ParseUtilCSharp.ParseExpression<PrimitiveExpression>("'\\u0356'");
Assert.AreEqual("'\\u0356'", pe.StringValue);
Assert.AreEqual('\u0356', (char)pe.Value);
}
@ -53,7 +53,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast @@ -53,7 +53,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
[Test]
public void CSharpStringTest1()
{
PrimitiveExpression pe = ParseUtilCSharp.ParseExpression<PrimitiveExpression>("\"\\n\\t\\u0005 Hello World !!!\";");
PrimitiveExpression pe = ParseUtilCSharp.ParseExpression<PrimitiveExpression>("\"\\n\\t\\u0005 Hello World !!!\"");
Assert.AreEqual("\"\\n\\t\\u0005 Hello World !!!\"", pe.StringValue);
Assert.AreEqual("\n\t\u0005 Hello World !!!", (string)pe.Value);
}

1
src/Libraries/NRefactory/Test/Parser/ParseUtilCSharp.cs

@ -76,6 +76,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast @@ -76,6 +76,7 @@ namespace ICSharpCode.NRefactory.Tests.Ast
public static T ParseExpression<T>(string expr, bool expectErrors) where T : INode
{
// SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(expr + ";"));
object parsedExpression = parser.ParseExpression();
if (expectErrors)

Loading…
Cancel
Save