From 9d3c0767b550222e84419208aede4a31944c1016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Tue, 6 Sep 2011 12:27:24 +0200 Subject: [PATCH] Fixed some issues with comment insertion. --- .../Parser/CSharpParser.cs | 5 ++ .../Parser/mcs/cs-tokenizer.cs | 61 +++++++++++-------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs b/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs index 0d8a55ffda..c5633f3466 100644 --- a/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs +++ b/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs @@ -3194,6 +3194,7 @@ namespace ICSharpCode.NRefactory.CSharp var leaf = GetOuterLeft(conversionVisitor.Unit); foreach (var special in top.SpecialsBag.Specials) { + var comment = special as SpecialsBag.Comment; if (comment == null) continue; @@ -3211,6 +3212,10 @@ namespace ICSharpCode.NRefactory.CSharp // insert comment at begin if (domComment.StartLocation < leaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; + while (node.Parent != null && node.FirstChild == leaf) { + leaf = node; + node = node.Parent; + } node.InsertChildBefore (leaf, domComment, AstNode.Roles.Comment); leaf = domComment; break; diff --git a/ICSharpCode.NRefactory.CSharp/Parser/mcs/cs-tokenizer.cs b/ICSharpCode.NRefactory.CSharp/Parser/mcs/cs-tokenizer.cs index 8862e71ae1..7c9c599d83 100644 --- a/ICSharpCode.NRefactory.CSharp/Parser/mcs/cs-tokenizer.cs +++ b/ICSharpCode.NRefactory.CSharp/Parser/mcs/cs-tokenizer.cs @@ -1866,7 +1866,8 @@ namespace Mono.CSharp var cmd = GetPreprocessorDirective (id_builder, TokenizePreprocessorIdentifier (out c, out endLine, out endCol)); if ((cmd & PreprocessorDirective.CustomArgumentsParsing) != 0) { - sbag.AddPreProcessorDirective (startLine, startCol, line, col, cmd, null); + if (position_stack.Count == 0) + sbag.AddPreProcessorDirective (startLine, startCol, line, col, cmd, null); return cmd; } @@ -1932,7 +1933,8 @@ namespace Mono.CSharp // Eat any trailing whitespaces arg = arg.Trim (simple_whitespaces); } - sbag.AddPreProcessorDirective (startLine, startCol, endLine, endCol, cmd, arg); + if (position_stack.Count == 0) + sbag.AddPreProcessorDirective (startLine, startCol, endLine, endCol, cmd, arg); return cmd; } @@ -2229,14 +2231,16 @@ namespace Mono.CSharp { if (peek_char () != '/') Report.Warning (1696, 1, Location, "Single-line comment or end-of-line expected"); - sbag.StartComment (SpecialsBag.CommentType.Single, startsLine, line, col - 1); + if (position_stack.Count == 0) + sbag.StartComment (SpecialsBag.CommentType.Single, startsLine, line, col - 1); // Read everything till the end of the line or file int c; do { c = get_char (); - sbag.PushCommentChar (c); + if (position_stack.Count == 0) + sbag.PushCommentChar (c); var pc = peek_char (); - if (pc == '\n' || pc == -1) + if (pc == '\n' || pc == -1 && position_stack.Count == 0) sbag.EndComment (line, col + 1); } while (c != -1 && c != '\n'); } @@ -3182,17 +3186,17 @@ namespace Mono.CSharp get_char (); return Token.OP_DIV_ASSIGN; } - // Handle double-slash comments. if (d == '/') { + Console.WriteLine (line + "/" + col); get_char (); if (doc_processing) { if (peek_char () == '/') { - sbag.StartComment (SpecialsBag.CommentType.Documentation, startsLine, line, col - 1); get_char (); // Don't allow ////. if ((d = peek_char ()) != '/') { - sbag.PushCommentChar (d); + if (position_stack.Count == 0) + sbag.PushCommentChar (d); if (doc_state == XmlCommentState.Allowed) handle_one_line_xml_comment (); else if (doc_state == XmlCommentState.NotAllowed) @@ -3204,39 +3208,41 @@ namespace Mono.CSharp } } else { bool isDoc = peek_char () == '/'; - sbag.StartComment (isDoc ? SpecialsBag.CommentType.Documentation : SpecialsBag.CommentType.Single, startsLine, line, col - 1); + if (position_stack.Count == 0) + sbag.StartComment (isDoc ? SpecialsBag.CommentType.Documentation : SpecialsBag.CommentType.Single, startsLine, line, col - 1); if (isDoc) get_char (); } d = peek_char (); - if (d == '\n' || d == '\r') - sbag.EndComment (line, col + 1); while ((d = get_char ()) != -1 && (d != '\n') && d != '\r') { - sbag.PushCommentChar (d); - var pc = peek_char (); - if (pc == -1 || pc == '\n' || pc == '\r') { - sbag.EndComment (line, col + 1); - } + if (position_stack.Count == 0) + sbag.PushCommentChar (d); } + if (position_stack.Count == 0) + sbag.EndComment (line, col + 1); any_token_seen |= tokens_seen; tokens_seen = false; comments_seen = false; continue; } else if (d == '*'){ - sbag.StartComment (SpecialsBag.CommentType.Multi, startsLine, line, col); + if (position_stack.Count == 0) + sbag.StartComment (SpecialsBag.CommentType.Multi, startsLine, line, col); get_char (); bool docAppend = false; if (doc_processing && peek_char () == '*') { int ch = get_char (); - sbag.PushCommentChar (ch); + if (position_stack.Count == 0) + sbag.PushCommentChar (ch); // But when it is /**/, just do nothing. if (peek_char () == '/') { ch = get_char (); - sbag.PushCommentChar (ch); - sbag.EndComment (line, col + 1); + if (position_stack.Count == 0) { + sbag.PushCommentChar (ch); + sbag.EndComment (line, col + 1); + } continue; } if (doc_state == XmlCommentState.Allowed) @@ -3253,11 +3259,14 @@ namespace Mono.CSharp } while ((d = get_char ()) != -1){ - sbag.PushCommentChar (d); + if (position_stack.Count == 0) + sbag.PushCommentChar (d); if (d == '*' && peek_char () == '/'){ - sbag.PushCommentChar ('/'); + if (position_stack.Count == 0) + sbag.PushCommentChar ('/'); get_char (); - sbag.EndComment (line, col + 1); + if (position_stack.Count == 0) + sbag.EndComment (line, col + 1); comments_seen = true; break; } @@ -3513,11 +3522,13 @@ namespace Mono.CSharp { int c; while ((c = peek_char ()) == ' ') { - sbag.PushCommentChar (c); + if (position_stack.Count == 0) + sbag.PushCommentChar (c); get_char (); // skip heading whitespaces. } while ((c = peek_char ()) != -1 && c != '\n' && c != '\r') { - sbag.PushCommentChar (c); + if (position_stack.Count == 0) + sbag.PushCommentChar (c); xml_comment_buffer.Append ((char) get_char ()); } if (c == '\r' || c == '\n')