• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_TOOLS_PROFILE_RESET_JTL_PARSER_H_
6 #define CHROME_TOOLS_PROFILE_RESET_JTL_PARSER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 
15 // Parses text-based JTL source code into a stream of operation names, arguments
16 // and separator kinds.
17 class JtlParser {
18  public:
19   // Creates a new parser to parse |compacted_source_code|, which should already
20   // be stripped of all comments and whitespace (except inside string literals).
21   // Use RemoveCommentsAndAllWhitespace() to manufacture these arguments, also
22   // see its comments for a description of |newline_indices|.
23   JtlParser(const std::string& compacted_source_code,
24             const std::vector<size_t>& newline_indices);
25   ~JtlParser();
26 
27   // Removes comments from |verbose_text| and compacts it into whitespace-free
28   // format (except inside string literals). Elements in |newline_indices| will
29   // be monotonically increasing and will refer to positions in |compacted_text|
30   // such that a new line has been removed before that position.
31   // Example:
32   //   verbose_text = "H e l l o // my\n"
33   //                  " dear \n"
34   //                  "\n"
35   //                  "world\" ! \""
36   //   compacted_text = "Hellodearworld\" ! \""
37   //                     01234567890123...
38   //   newline_indices = {5, 9, 9}
39   // Returns true on success, false if there were unmatched quotes in a line, in
40   // which case |error_line_number| will be set accordingly if it is non-NULL.
41   static bool RemoveCommentsAndAllWhitespace(
42       const std::string& verbose_text,
43       std::string* compacted_text,
44       std::vector<size_t>* newline_indices,
45       size_t* error_line_number);
46 
47   // Returns true if the entire input has been successfully consumed. Note that
48   // even when this returns false, a subsequent call to ParseNextOperation()
49   // might still fail if the next operation cannot be parsed.
50   bool HasFinished();
51 
52   // Fetches the |name| and the |argument_list| of the next operation, and also
53   // whether or not it |ends_the_sentence|, i.e. it is followed by the
54   // end-of-sentence separator.
55   // Returns false if there is a parsing error, in which case the values for the
56   // output parameters are undefined, and |this| parser shall no longer be used.
57   bool ParseNextOperation(std::string* name,
58                           base::ListValue* argument_list,
59                           bool* ends_the_sentence);
60 
61   // Returns the compacted source code that was passed in to the constructor.
compacted_source()62   const std::string& compacted_source() const { return compacted_source_; }
63 
64   // Returns at which line the character at position |compacted_index| in the
65   // |compacted_source()| was originally located.
66   size_t GetOriginalLineNumber(size_t compacted_index) const;
67 
68   size_t GetLastLineNumber() const;
69   std::string GetLastContext() const;
70 
71  private:
72   // Contains pre-compiled regular expressions and related state. Factored out
73   // to avoid this header depending on RE2 headers.
74   struct ParsingState;
75 
76   std::string compacted_source_;
77   std::vector<size_t> newline_indices_;
78   scoped_ptr<ParsingState> state_;
79 
80   DISALLOW_COPY_AND_ASSIGN(JtlParser);
81 };
82 
83 #endif  // CHROME_TOOLS_PROFILE_RESET_JTL_PARSER_H_
84