/[pcre]/code/tags/pcre-6.7/pcre_scanner.h
ViewVC logotype

Contents of /code/tags/pcre-6.7/pcre_scanner.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 92 - (show annotations) (download)
Sat Feb 24 21:41:36 2007 UTC (7 years, 1 month ago) by nigel
File MIME type: text/plain
File size: 6042 byte(s)
Tag code/trunk as code/tags/pcre-6.7.

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.Skip("\\s+"); // Skip any white space we encounter
40 // while (scanner.Consume("(\\w+) = (\\d+)", &var, &number)) {
41 // ...;
42 // }
43
44 #ifndef _PCRE_SCANNER_H
45 #define _PCRE_SCANNER_H
46
47 #include <assert.h>
48 #include <string>
49 #include <vector>
50 #include <pcrecpp.h>
51 #include <pcre_stringpiece.h>
52
53 namespace pcrecpp {
54
55 class Scanner {
56 public:
57 Scanner();
58 explicit Scanner(const std::string& input);
59 ~Scanner();
60
61 // Return current line number. The returned line-number is
62 // one-based. I.e. it returns 1 + the number of consumed newlines.
63 //
64 // Note: this method may be slow. It may take time proportional to
65 // the size of the input.
66 int LineNumber() const;
67
68 // Return the byte-offset that the scanner is looking in the
69 // input data;
70 int Offset() const;
71
72 // Return true iff the start of the remaining input matches "re"
73 bool LookingAt(const RE& re) const;
74
75 // Return true iff all of the following are true
76 // a. the start of the remaining input matches "re",
77 // b. if any arguments are supplied, matched sub-patterns can be
78 // parsed and stored into the arguments.
79 // If it returns true, it skips over the matched input and any
80 // following input that matches the "skip" regular expression.
81 bool Consume(const RE& re,
82 const Arg& arg0 = no_arg,
83 const Arg& arg1 = no_arg,
84 const Arg& arg2 = no_arg
85 // TODO: Allow more arguments?
86 );
87
88 // Set the "skip" regular expression. If after consuming some data,
89 // a prefix of the input matches this RE, it is automatically
90 // skipped. For example, a programming language scanner would use
91 // a skip RE that matches white space and comments.
92 //
93 // scanner.Skip("(\\s|//.*|/[*](.|\n)*?[*]/)*");
94 //
95 // You can pass NULL for "re" if you do not want any data to be skipped.
96 void Skip(const char* re);
97
98 // Temporarily pause "skip"ing. This
99 // Skip("Foo"); code ; DisableSkip(); code; EnableSkip()
100 // is similar to
101 // Skip("Foo"); code ; Skip(NULL); code ; Skip("Foo");
102 // but avoids creating/deleting new RE objects.
103 void DisableSkip();
104
105 // Reenable previously paused skipping. Any prefix of the input
106 // that matches the skip pattern is immediately dropped.
107 void EnableSkip();
108
109 /***** Special wrappers around SetSkip() for some common idioms *****/
110
111 // Arranges to skip whitespace, C comments, C++ comments.
112 // The overall RE is a repeated disjunction of the following REs:
113 // \\s whitespace
114 // //.*\n C++ comment
115 // /[*](.|\n)*?[*]/ C comment (x*? means minimal repetitions of x)
116 void SkipCXXComments() {
117 Skip("((\\s|//.*\n|/[*](.|\n)*?[*]/)*)");
118 }
119
120 void set_save_comments(bool comments) {
121 save_comments_ = comments;
122 }
123
124 bool save_comments() {
125 return save_comments_;
126 }
127
128 // Append to vector ranges the comments found in the
129 // byte range [start,end] (inclusive) of the input data.
130 // Only comments that were extracted entirely within that
131 // range are returned: no range splitting of atomically-extracted
132 // comments is performed.
133 void GetComments(int start, int end, std::vector<StringPiece> *ranges);
134
135 // Append to vector ranges the comments added
136 // since the last time this was called. This
137 // functionality is provided for efficiency when
138 // interleaving scanning with parsing.
139 void GetNextComments(std::vector<StringPiece> *ranges);
140
141 private:
142 std::string data_; // All the input data
143 StringPiece input_; // Unprocessed input
144 RE* skip_; // If non-NULL, RE for skipping input
145 bool should_skip_; // If true, use skip_
146 bool save_comments_; // If true, aggregate the skip expression
147
148 // the skipped comments
149 // TODO: later consider requiring that the StringPieces be added
150 // in order by their start position
151 std::vector<StringPiece> *comments_;
152
153 // the offset into comments_ that has been returned by GetNextComments
154 int comments_offset_;
155
156 // helper function to consume *skip_ and honour
157 // save_comments_
158 void ConsumeSkip();
159 };
160
161 } // namespace pcrecpp
162
163 #endif /* _PCRE_SCANNER_H */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12