OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/tools/profile_reset/jtl_parser.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "third_party/re2/re2/re2.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // RegEx that matches the first line of a text. Will throw away any potential |
| 15 // double-slash-introduced comments and the potential trailing EOL character. |
| 16 // Note: will fail in case the first line contains an unmatched double-quote |
| 17 // outside of comments. |
| 18 const char kSingleLineWithMaybeCommentsRE[] = |
| 19 // Non-greedily match and capture sequences of 1.) string literals inside |
| 20 // correctly matched double-quotes, or 2.) any other character. |
| 21 "^((?:\"[^\"\\n]*\"|[^\"\\n])*?)" |
| 22 // Greedily match and throw away the potential comment. |
| 23 "(?://.*)?" |
| 24 // Match and throw away EOL, or match end-of-string. |
| 25 "(?:\n|$)"; |
| 26 |
| 27 // RegEx to match either a double-quote-enclosed string literal or a whitespace. |
| 28 // Applied repeatedly and without overlapping, can be used to remove whitespace |
| 29 // outside of string literals. |
| 30 const char kRemoveWhitespaceRE[] = "(\"[^\"]*\")|\\s"; |
| 31 |
| 32 // The substitution pattern to use together with the above when replacing. As |
| 33 // the whitespace is not back-referenced here, it will get removed. |
| 34 const char kRemoveWhitespaceRewrite[] = "\\1"; |
| 35 |
| 36 // Separator to terminate a sentence. |
| 37 const char kEndOfSentenceSeparator[] = ";"; |
| 38 |
| 39 // Separator to continue a sentence with another operation. |
| 40 const char kContinueSentenceSeparator[] = "/"; |
| 41 |
| 42 // The 'true' Boolean keyword. |
| 43 const char kTrueKeyword[] = "true"; |
| 44 |
| 45 // The 'false' Boolean keyword. |
| 46 const char kFalseKeyword[] = "false"; |
| 47 |
| 48 // RegEx that matches and captures one argument, which is either a double-quote |
| 49 // enclosed string, or a Boolean value. Will throw away a trailing comma. |
| 50 const char kSingleArgumentRE[] = "(?:(?:\"([^\"]*)\"|(true|false))(?:,|$))"; |
| 51 |
| 52 // RegEx-es that, when concatenated, will match a single operation, and capture |
| 53 // the: operation name, the optional arguments, and the separator that follows. |
| 54 const char kOperationNameRE[] = "([[:word:]]+)"; |
| 55 const char kMaybeArgumentListRE[] = |
| 56 "(?:\\(" // Opening parenthesis. |
| 57 "((?:\"[^\"]*\"|[^\")])*)" // Capture: anything inside, quote-aware. |
| 58 "\\))?"; // Closing parenthesis + everything optional. |
| 59 const char kOperationSeparatorRE[] = "(;|/)"; |
| 60 |
| 61 } // namespace |
| 62 |
| 63 struct JtlParser::ParsingState { |
| 64 explicit ParsingState(const re2::StringPiece& compacted_source) |
| 65 : single_operation_regex(std::string(kOperationNameRE) + |
| 66 kMaybeArgumentListRE + |
| 67 kOperationSeparatorRE), |
| 68 single_argument_regex(kSingleArgumentRE), |
| 69 remaining_compacted_source(compacted_source), |
| 70 last_line_number(0) {} |
| 71 |
| 72 RE2 single_operation_regex; |
| 73 RE2 single_argument_regex; |
| 74 re2::StringPiece remaining_compacted_source; |
| 75 re2::StringPiece last_context; |
| 76 size_t last_line_number; |
| 77 }; |
| 78 |
| 79 JtlParser::JtlParser(const std::string& compacted_source_code, |
| 80 const std::vector<size_t>& newline_indices) |
| 81 : compacted_source_(compacted_source_code), |
| 82 newline_indices_(newline_indices) { |
| 83 state_.reset(new ParsingState(compacted_source_)); |
| 84 } |
| 85 |
| 86 JtlParser::~JtlParser() {} |
| 87 |
| 88 // static |
| 89 bool JtlParser::RemoveCommentsAndAllWhitespace( |
| 90 const std::string& verbose_text, |
| 91 std::string* compacted_text, |
| 92 std::vector<size_t>* newline_indices, |
| 93 size_t* error_line_number) { |
| 94 DCHECK(compacted_text); |
| 95 DCHECK(newline_indices); |
| 96 std::string line; |
| 97 RE2 single_line_regex(kSingleLineWithMaybeCommentsRE); |
| 98 RE2 remove_whitespace_regex(kRemoveWhitespaceRE); |
| 99 re2::StringPiece verbose_text_piece(verbose_text); |
| 100 compacted_text->clear(); |
| 101 newline_indices->clear(); |
| 102 while (!verbose_text_piece.empty()) { |
| 103 if (!RE2::Consume(&verbose_text_piece, single_line_regex, &line)) { |
| 104 if (error_line_number) |
| 105 *error_line_number = newline_indices->size(); |
| 106 return false; |
| 107 } |
| 108 RE2::GlobalReplace( |
| 109 &line, remove_whitespace_regex, kRemoveWhitespaceRewrite); |
| 110 *compacted_text += line; |
| 111 newline_indices->push_back(compacted_text->size()); |
| 112 } |
| 113 return true; |
| 114 } |
| 115 |
| 116 bool JtlParser::HasFinished() { |
| 117 return state_->remaining_compacted_source.empty(); |
| 118 } |
| 119 |
| 120 bool JtlParser::ParseNextOperation(std::string* name, |
| 121 base::ListValue* argument_list, |
| 122 bool* ends_sentence) { |
| 123 DCHECK(name); |
| 124 DCHECK(argument_list); |
| 125 DCHECK(ends_sentence); |
| 126 |
| 127 state_->last_context = state_->remaining_compacted_source; |
| 128 state_->last_line_number = GetOriginalLineNumber( |
| 129 compacted_source_.size() - state_->remaining_compacted_source.length()); |
| 130 |
| 131 std::string arguments, separator; |
| 132 if (!RE2::Consume(&state_->remaining_compacted_source, |
| 133 state_->single_operation_regex, |
| 134 name, |
| 135 &arguments, |
| 136 &separator)) |
| 137 return false; |
| 138 |
| 139 *ends_sentence = (separator == kEndOfSentenceSeparator); |
| 140 state_->last_context.remove_suffix(state_->remaining_compacted_source.size()); |
| 141 |
| 142 re2::StringPiece arguments_piece(arguments); |
| 143 std::string string_value, boolean_value; |
| 144 while (!arguments_piece.empty()) { |
| 145 if (!RE2::Consume(&arguments_piece, |
| 146 state_->single_argument_regex, |
| 147 &string_value, |
| 148 &boolean_value)) |
| 149 return false; |
| 150 |
| 151 if (!boolean_value.empty()) { |
| 152 argument_list->Append( |
| 153 new base::FundamentalValue(boolean_value == kTrueKeyword)); |
| 154 } else { |
| 155 // |string_value| might be empty for an empty string |
| 156 argument_list->Append(new StringValue(string_value)); |
| 157 } |
| 158 } |
| 159 return true; |
| 160 } |
| 161 |
| 162 size_t JtlParser::GetOriginalLineNumber(size_t compacted_index) const { |
| 163 return static_cast<size_t>(std::upper_bound(newline_indices_.begin(), |
| 164 newline_indices_.end(), |
| 165 compacted_index) - |
| 166 newline_indices_.begin()); |
| 167 } |
| 168 |
| 169 size_t JtlParser::GetLastLineNumber() const { return state_->last_line_number; } |
| 170 |
| 171 std::string JtlParser::GetLastContext() const { |
| 172 return state_->last_context.ToString(); |
| 173 } |
OLD | NEW |