Chromium Code Reviews| Index: chrome/tools/profile_reset/jtl_parser.cc |
| diff --git a/chrome/tools/profile_reset/jtl_parser.cc b/chrome/tools/profile_reset/jtl_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..55b45ab7bea5b2cfe7f03f84c6a000abf7366cce |
| --- /dev/null |
| +++ b/chrome/tools/profile_reset/jtl_parser.cc |
| @@ -0,0 +1,152 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/tools/profile_reset/jtl_parser.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/values.h" |
| +#include "third_party/re2/re2/re2.h" |
| + |
| +namespace { |
| + |
| +// RegEx that matches the first line of a text. Will throw away any potential |
| +// double-slash introduced comments and the potential trailing EOL character. |
| +// Gracefully handles slashes inside quotes, and even mismatched quotes. See |
| +// unit tests for details. |
| +const char kSingleLineWithMaybeCommentsRE[] = |
| + "((?:\"[^\"]*\"|.)*?)(?://.*)?(?:\n|$)"; |
|
battre
2013/09/27 15:00:01
How about splitting this into something like
"
engedy
2013/10/01 10:48:10
Done.
|
| + |
| +// Separator to terminate a sentence. |
| +const char kEndOfSentenceSeparator[] = ";"; |
| + |
| +// Separator to continue a sentence with another operation. |
| +const char kContinueSentenceSeparator[] = "/"; |
| + |
| +// The 'true' Boolean keyword. |
| +const char kTrueKeyword[] = "true"; |
| + |
| +// The 'false' Boolean keyword. |
| +const char kFalseKeyword[] = "false"; |
| + |
| +// RegEx that matches and captures one argument, which is either a double-quote |
| +// enclosed string, or a Boolean value. Will throw away a trailing comma. |
| +const char kSingleArgumentRE[] = "(?:(?:(?:\"([^\"]*)\")|(true|false))(?:,|$))"; |
| + |
| +// RegEx-es that, when concatenated, will match a single operation, and capture |
| +// the: operation name, the optional arguments, and the separator that follows. |
| +const char kOperationNameRE[] = "([[:word:]]+)"; |
| +const char kMaybeArgumentListRE[] = "(?:\\(((?:\"[^\"]*\"|[^\")])*)\\))?"; |
| +const char kOperationSeparatorRE[] = "(;|/)"; |
| + |
| +} // namespace |
| + |
| +struct JtlParser::ParsingState { |
| + explicit ParsingState(const std::string& compacted_source) |
| + : single_operation_regex(std::string(kOperationNameRE) + |
| + kMaybeArgumentListRE + |
| + kOperationSeparatorRE), |
| + single_argument_regex(kSingleArgumentRE), |
| + remaining_compacted_source(compacted_source), |
| + last_line_number(0) {} |
| + |
| + RE2 single_operation_regex; |
| + RE2 single_argument_regex; |
| + re2::StringPiece remaining_compacted_source; |
| + re2::StringPiece last_context; |
| + size_t last_line_number; |
| +}; |
| + |
| +JtlParser::JtlParser(const std::string& compacted_source, |
| + const std::vector<size_t>& newline_indices) |
| + : compacted_source_(compacted_source), |
| + newline_indices_(newline_indices), |
| + state_(new ParsingState(compacted_source)) {} |
| + |
| +JtlParser::~JtlParser() {} |
| + |
| +JtlParser* JtlParser::Create(const std::string& source_code) { |
| + std::string compacted_source; |
| + std::vector<size_t> newline_indices; |
| + RemoveCommentsAndAllWhitespace( |
| + source_code, &compacted_source, &newline_indices); |
| + return new JtlParser(compacted_source, newline_indices); |
| +} |
| + |
| +void JtlParser::RemoveCommentsAndAllWhitespace( |
| + const std::string& verbose_text, |
| + std::string* compacted_text, |
| + std::vector<size_t>* newline_indices) { |
| + DCHECK(compacted_text); |
| + DCHECK(newline_indices); |
| + std::string line_without_comments; |
| + RE2 single_line_regex(kSingleLineWithMaybeCommentsRE); |
| + re2::StringPiece verbose_text_piece(verbose_text); |
| + while (!verbose_text_piece.empty() && |
| + RE2::Consume( |
| + &verbose_text_piece, single_line_regex, &line_without_comments)) { |
| + std::string compacted_line; |
| + RemoveChars(line_without_comments, kWhitespaceASCII, &compacted_line); |
| + *compacted_text += compacted_line; |
| + newline_indices->push_back(compacted_text->size()); |
| + } |
| + DCHECK(verbose_text_piece.empty()); |
| +} |
| + |
| +bool JtlParser::HasNextOperation() { |
| + return !state_->remaining_compacted_source.empty(); |
| +} |
| + |
| +bool JtlParser::ParseNextOperation(std::string* name, |
| + base::ListValue* argument_list, |
| + bool* ends_sentence) { |
| + DCHECK(name); |
| + DCHECK(argument_list); |
| + DCHECK(ends_sentence); |
| + |
| + state_->last_context = state_->remaining_compacted_source; |
| + state_->last_line_number = GetOriginalLineNumber( |
| + compacted_source_.size() - state_->remaining_compacted_source.length()); |
| + |
| + std::string arguments, separator; |
| + if (!RE2::Consume(&state_->remaining_compacted_source, |
| + state_->single_operation_regex, |
| + name, |
| + &arguments, |
| + &separator)) |
| + return false; |
| + |
| + *ends_sentence = (separator == kEndOfSentenceSeparator); |
| + state_->last_context.remove_suffix(state_->remaining_compacted_source.size()); |
| + |
| + re2::StringPiece arguments_piece(arguments); |
| + std::string string_value, boolean_value; |
| + while (!arguments_piece.empty()) { |
| + if (!RE2::Consume(&arguments_piece, |
| + state_->single_argument_regex, |
| + &string_value, |
| + &boolean_value)) |
| + return false; |
| + |
| + if (!boolean_value.empty()) |
| + argument_list->Append( |
| + new base::FundamentalValue(boolean_value == kTrueKeyword)); |
| + else // |string_value| might be empty for an empty string |
| + argument_list->Append(new StringValue(string_value)); |
| + } |
| + return true; |
| +} |
| + |
| +size_t JtlParser::GetOriginalLineNumber(size_t compacted_index) const { |
| + return static_cast<size_t>(std::upper_bound(newline_indices_.begin(), |
| + newline_indices_.end(), |
| + compacted_index) - |
| + newline_indices_.begin()); |
| +} |
| + |
| +size_t JtlParser::last_line_number() const { return state_->last_line_number; } |
| + |
| +std::string JtlParser::last_context() const { |
| + return state_->last_context.ToString(); |
| +} |