| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/parser.h" | 5 #include "tools/gn/parser.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 TraverseOrder(list->End(), pre, post); | 796 TraverseOrder(list->End(), pre, post); |
| 797 } else if (root->AsLiteral()) { | 797 } else if (root->AsLiteral()) { |
| 798 // Nothing. | 798 // Nothing. |
| 799 } else if (const UnaryOpNode* unaryop = root->AsUnaryOp()) { | 799 } else if (const UnaryOpNode* unaryop = root->AsUnaryOp()) { |
| 800 TraverseOrder(unaryop->operand(), pre, post); | 800 TraverseOrder(unaryop->operand(), pre, post); |
| 801 } else if (root->AsBlockComment()) { | 801 } else if (root->AsBlockComment()) { |
| 802 // Nothing. | 802 // Nothing. |
| 803 } else if (root->AsEnd()) { | 803 } else if (root->AsEnd()) { |
| 804 // Nothing. | 804 // Nothing. |
| 805 } else { | 805 } else { |
| 806 CHECK(false) << "Unhandled case in TraverseOrder."; | 806 // Unhandled case in TraverseOrder. |
| 807 CHECK(false); |
| 807 } | 808 } |
| 808 | 809 |
| 809 post->push_back(root); | 810 post->push_back(root); |
| 810 } | 811 } |
| 811 } | 812 } |
| 812 | 813 |
| 813 void Parser::AssignComments(ParseNode* file) { | 814 void Parser::AssignComments(ParseNode* file) { |
| 814 // Start by generating a pre- and post- order traversal of the tree so we | 815 // Start by generating a pre- and post- order traversal of the tree so we |
| 815 // can determine what's before and after comments. | 816 // can determine what's before and after comments. |
| 816 std::vector<const ParseNode*> pre; | 817 std::vector<const ParseNode*> pre; |
| 817 std::vector<const ParseNode*> post; | 818 std::vector<const ParseNode*> post; |
| 818 TraverseOrder(file, &pre, &post); | 819 TraverseOrder(file, &pre, &post); |
| 819 | 820 |
| 820 // Assign line comments to syntax immediately following. | 821 // Assign line comments to syntax immediately following. |
| 821 int cur_comment = 0; | 822 int cur_comment = 0; |
| 822 for (auto* node : pre) { | 823 for (auto* node : pre) { |
| 823 if (node->GetRange().is_null()) { | 824 if (node->GetRange().is_null()) { |
| 824 CHECK_EQ(node, file) << "Only expected on top file node"; | 825 // Only expected on top file node |
| 826 CHECK_EQ(node, file); |
| 825 continue; | 827 continue; |
| 826 } | 828 } |
| 827 const Location& start = node->GetRange().begin(); | 829 const Location& start = node->GetRange().begin(); |
| 828 while (cur_comment < static_cast<int>(line_comment_tokens_.size())) { | 830 while (cur_comment < static_cast<int>(line_comment_tokens_.size())) { |
| 829 if (start.byte() >= line_comment_tokens_[cur_comment].location().byte()) { | 831 if (start.byte() >= line_comment_tokens_[cur_comment].location().byte()) { |
| 830 const_cast<ParseNode*>(node)->comments_mutable()->append_before( | 832 const_cast<ParseNode*>(node)->comments_mutable()->append_before( |
| 831 line_comment_tokens_[cur_comment]); | 833 line_comment_tokens_[cur_comment]); |
| 832 ++cur_comment; | 834 ++cur_comment; |
| 833 } else { | 835 } else { |
| 834 break; | 836 break; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 873 break; | 875 break; |
| 874 } | 876 } |
| 875 } | 877 } |
| 876 | 878 |
| 877 // Suffix comments were assigned in reverse, so if there were multiple on | 879 // Suffix comments were assigned in reverse, so if there were multiple on |
| 878 // the same node, they need to be reversed. | 880 // the same node, they need to be reversed. |
| 879 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) | 881 if ((*i)->comments() && !(*i)->comments()->suffix().empty()) |
| 880 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); | 882 const_cast<ParseNode*>(*i)->comments_mutable()->ReverseSuffix(); |
| 881 } | 883 } |
| 882 } | 884 } |
| OLD | NEW |