pcre_scanner.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Sanjay Ghemawat
  31. //
  32. // Regular-expression based scanner for parsing an input stream.
  33. //
  34. // Example 1: parse a sequence of "var = number" entries from input:
  35. //
  36. // Scanner scanner(input);
  37. // string var;
  38. // int number;
  39. // scanner.SetSkipExpression("\\s+"); // Skip any white space we encounter
  40. // while (scanner.Consume("(\\w+) = (\\d+)", &var, &number)) {
  41. // ...;
  42. // }
  43. #ifndef _PCRE_SCANNER_H
  44. #define _PCRE_SCANNER_H
  45. #include <assert.h>
  46. #include <string>
  47. #include <vector>
  48. #include "pcrecpp.h"
  49. #include "pcre_stringpiece.h"
  50. namespace pcrecpp {
  51. class PCRECPP_EXP_DEFN Scanner {
  52. public:
  53. Scanner();
  54. explicit Scanner(const std::string& input);
  55. ~Scanner();
  56. // Return current line number. The returned line-number is
  57. // one-based. I.e. it returns 1 + the number of consumed newlines.
  58. //
  59. // Note: this method may be slow. It may take time proportional to
  60. // the size of the input.
  61. int LineNumber() const;
  62. // Return the byte-offset that the scanner is looking in the
  63. // input data;
  64. int Offset() const;
  65. // Return true iff the start of the remaining input matches "re"
  66. bool LookingAt(const RE& re) const;
  67. // Return true iff all of the following are true
  68. // a. the start of the remaining input matches "re",
  69. // b. if any arguments are supplied, matched sub-patterns can be
  70. // parsed and stored into the arguments.
  71. // If it returns true, it skips over the matched input and any
  72. // following input that matches the "skip" regular expression.
  73. bool Consume(const RE& re,
  74. const Arg& arg0 = RE::no_arg,
  75. const Arg& arg1 = RE::no_arg,
  76. const Arg& arg2 = RE::no_arg
  77. // TODO: Allow more arguments?
  78. );
  79. // Set the "skip" regular expression. If after consuming some data,
  80. // a prefix of the input matches this RE, it is automatically
  81. // skipped. For example, a programming language scanner would use
  82. // a skip RE that matches white space and comments.
  83. //
  84. // scanner.SetSkipExpression("\\s+|//.*|/[*](.|\n)*?[*]/");
  85. //
  86. // Skipping repeats as long as it succeeds. We used to let people do
  87. // this by writing "(...)*" in the regular expression, but that added
  88. // up to lots of recursive calls within the pcre library, so now we
  89. // control repetition explicitly via the function call API.
  90. //
  91. // You can pass NULL for "re" if you do not want any data to be skipped.
  92. void Skip(const char* re); // DEPRECATED; does *not* repeat
  93. void SetSkipExpression(const char* re);
  94. // Temporarily pause "skip"ing. This
  95. // Skip("Foo"); code ; DisableSkip(); code; EnableSkip()
  96. // is similar to
  97. // Skip("Foo"); code ; Skip(NULL); code ; Skip("Foo");
  98. // but avoids creating/deleting new RE objects.
  99. void DisableSkip();
  100. // Reenable previously paused skipping. Any prefix of the input
  101. // that matches the skip pattern is immediately dropped.
  102. void EnableSkip();
  103. /***** Special wrappers around SetSkip() for some common idioms *****/
  104. // Arranges to skip whitespace, C comments, C++ comments.
  105. // The overall RE is a disjunction of the following REs:
  106. // \\s whitespace
  107. // //.*\n C++ comment
  108. // /[*](.|\n)*?[*]/ C comment (x*? means minimal repetitions of x)
  109. // We get repetition via the semantics of SetSkipExpression, not by using *
  110. void SkipCXXComments() {
  111. SetSkipExpression("\\s|//.*\n|/[*](?:\n|.)*?[*]/");
  112. }
  113. void set_save_comments(bool comments) {
  114. save_comments_ = comments;
  115. }
  116. bool save_comments() {
  117. return save_comments_;
  118. }
  119. // Append to vector ranges the comments found in the
  120. // byte range [start,end] (inclusive) of the input data.
  121. // Only comments that were extracted entirely within that
  122. // range are returned: no range splitting of atomically-extracted
  123. // comments is performed.
  124. void GetComments(int start, int end, std::vector<StringPiece> *ranges);
  125. // Append to vector ranges the comments added
  126. // since the last time this was called. This
  127. // functionality is provided for efficiency when
  128. // interleaving scanning with parsing.
  129. void GetNextComments(std::vector<StringPiece> *ranges);
  130. private:
  131. std::string data_; // All the input data
  132. StringPiece input_; // Unprocessed input
  133. RE* skip_; // If non-NULL, RE for skipping input
  134. bool should_skip_; // If true, use skip_
  135. bool skip_repeat_; // If true, repeat skip_ as long as it works
  136. bool save_comments_; // If true, aggregate the skip expression
  137. // the skipped comments
  138. // TODO: later consider requiring that the StringPieces be added
  139. // in order by their start position
  140. std::vector<StringPiece> *comments_;
  141. // the offset into comments_ that has been returned by GetNextComments
  142. int comments_offset_;
  143. // helper function to consume *skip_ and honour
  144. // save_comments_
  145. void ConsumeSkip();
  146. };
  147. } // namespace pcrecpp
  148. #endif /* _PCRE_SCANNER_H */