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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (hide annotations) (download)
Sat Feb 24 21:41:42 2007 UTC (7 years, 9 months ago) by nigel
Original Path: code/trunk/pcre_scanner.h
File MIME type: text/plain
File size: 6570 byte(s)
Load pcre-7.0 into code/trunk.

1 nigel 77 // 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 nigel 93 // scanner.SetSkipExpression("\\s+"); // Skip any white space we encounter
40 nigel 77 // 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 nigel 93 // scanner.SetSkipExpression("\\s+|//.*|/[*](.|\n)*?[*]/");
94 nigel 77 //
95 nigel 93 // Skipping repeats as long as it succeeds. We used to let people do
96     // this by writing "(...)*" in the regular expression, but that added
97     // up to lots of recursive calls within the pcre library, so now we
98     // control repetition explicitly via the function call API.
99     //
100 nigel 77 // You can pass NULL for "re" if you do not want any data to be skipped.
101 nigel 93 void Skip(const char* re); // DEPRECATED; does *not* repeat
102     void SetSkipExpression(const char* re);
103 nigel 77
104     // Temporarily pause "skip"ing. This
105     // Skip("Foo"); code ; DisableSkip(); code; EnableSkip()
106     // is similar to
107     // Skip("Foo"); code ; Skip(NULL); code ; Skip("Foo");
108     // but avoids creating/deleting new RE objects.
109     void DisableSkip();
110    
111     // Reenable previously paused skipping. Any prefix of the input
112     // that matches the skip pattern is immediately dropped.
113     void EnableSkip();
114    
115     /***** Special wrappers around SetSkip() for some common idioms *****/
116    
117     // Arranges to skip whitespace, C comments, C++ comments.
118 nigel 93 // The overall RE is a disjunction of the following REs:
119 nigel 77 // \\s whitespace
120     // //.*\n C++ comment
121     // /[*](.|\n)*?[*]/ C comment (x*? means minimal repetitions of x)
122 nigel 93 // We get repetition via the semantics of SetSkipExpression, not by using *
123 nigel 77 void SkipCXXComments() {
124 nigel 93 SetSkipExpression("\\s|//.*\n|/[*](?:\n|.)*?[*]/");
125 nigel 77 }
126    
127     void set_save_comments(bool comments) {
128     save_comments_ = comments;
129     }
130    
131     bool save_comments() {
132     return save_comments_;
133     }
134    
135     // Append to vector ranges the comments found in the
136     // byte range [start,end] (inclusive) of the input data.
137     // Only comments that were extracted entirely within that
138     // range are returned: no range splitting of atomically-extracted
139     // comments is performed.
140     void GetComments(int start, int end, std::vector<StringPiece> *ranges);
141    
142     // Append to vector ranges the comments added
143     // since the last time this was called. This
144     // functionality is provided for efficiency when
145     // interleaving scanning with parsing.
146     void GetNextComments(std::vector<StringPiece> *ranges);
147    
148     private:
149     std::string data_; // All the input data
150     StringPiece input_; // Unprocessed input
151     RE* skip_; // If non-NULL, RE for skipping input
152     bool should_skip_; // If true, use skip_
153 nigel 93 bool skip_repeat_; // If true, repeat skip_ as long as it works
154 nigel 77 bool save_comments_; // If true, aggregate the skip expression
155    
156     // the skipped comments
157     // TODO: later consider requiring that the StringPieces be added
158     // in order by their start position
159     std::vector<StringPiece> *comments_;
160    
161     // the offset into comments_ that has been returned by GetNextComments
162     int comments_offset_;
163    
164     // helper function to consume *skip_ and honour
165     // save_comments_
166     void ConsumeSkip();
167     };
168    
169     } // namespace pcrecpp
170    
171     #endif /* _PCRE_SCANNER_H */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12