summaryrefslogtreecommitdiff
path: root/src/generic/apt/matching/parse.h
blob: 0f9825619c82d4db363f365df2fd22ccfcc372cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// parse.h  -*-c++-*-
//
//  Copyright 2000-2001, 2005, 2007-2008 Daniel Burrows
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#ifndef PARSE_H
#define PARSE_H

#include <cwidget/generic/util/ref_ptr.h>

/** \file parse.h */

/** \brief Code to parse search patterns. */

namespace aptitude
{
  namespace matching
  {
    class pattern;

    /** \brief Parse a string region as a search pattern.
     *
     *  Parses [start, end), treating strings from terminators as
     *  ending the parse if they are encountered at the top syntactic
     *  level.
     *
     *  \throw MatchingException if the parse fails.
     *
     *  \param start   The beginning of the range to parse.
     *                 Will be moved past the parsed expression.
     *  \param end     The end of the range to parse.
     *  \param terminators  A list of strings whose (unescaped)
     *                      presence at the top syntactic level
     *                      signals the end of a search pattern.
     *  \param require_full_parse  If \b true, an error will be
     *                             signalled if part of the region
     *                             is left after parsing (i.e.,
     *                             if start != end).
     *  \param partial      If \b true, if the last word is an
     *                      unadorned Xapian term, it will be
     *                      treated as a wildcard representing
     *                      all the terms in the database.
     *
     *  \return  The parsed expression, or NULL if an
     *  error occurred or if the input range was empty.
     */
    cwidget::util::ref_ptr<pattern>
      parse_with_errors(std::string::const_iterator &start,
			const std::string::const_iterator &end,
			const std::vector<const char *> &terminators,
			bool require_full_parse,
			bool partial);

    /** \brief Parse a string region as a search pattern.
     *
     *  Parses [start, end), treating strings from terminators as
     *  ending the parse if they are encountered at the top syntactic
     *  level.
     *
     *  \param start   The beginning of the range to parse.
     *                 Will be moved past the parsed expression.
     *  \param end     The end of the range to parse.
     *  \param terminators  A list of strings whose (unescaped)
     *                      presence at the top syntactic level
     *                      signals the end of a search pattern.
     *  \param flag_errors  If \b true, apt errors (in _error)
     *                      will be generated for any parsing
     *                      errors that are encountered.
     *  \param require_full_parse  If \b true, an error will be
     *                             signalled if part of the region
     *                             is left after parsing (i.e.,
     *                             if start != end).
     *  \param partial      If \b true, if the last word is an
     *                      unadorned Xapian term, it will be
     *                      treated as a wildcard representing
     *                      all the terms in the database.
     *
     *  \return  The parsed expression, or NULL if an
     *  error occurred or if the input range was empty.
     */
    cwidget::util::ref_ptr<pattern>
      parse(std::string::const_iterator &start,
	    const std::string::const_iterator &end,
	    const std::vector<const char *> &terminators,
	    bool flag_errors,
	    bool require_full_parse,
	    bool partial);


    /** \brief Parse a string as a search pattern.
     *
     *  Parses the given string, treating strings from terminators as
     *  ending the parse if they are encountered at the top syntactic
     *  level.
     *
     *  \param s       The string to parse.
     *  \param terminators  A list of strings whose (unescaped)
     *                      presence at the top syntactic level
     *                      signals the end of a search pattern.
     *  \param flag_errors  If \b true, apt errors (in _error)
     *                      will be generated for any parsing
     *                      errors that are encountered.
     *  \param require_full_parse  If \b true, an error will be
     *                             signalled if part of the string
     *                             is left after parsing.
     *
     *  \return  The parsed expression, or NULL if an
     *  error occurred or if the input string was empty.
     */
    inline cwidget::util::ref_ptr<pattern>
      parse(const std::string &s,
	    const std::vector<const char *> &terminators,
	    bool flag_errors,
	    bool require_full_parse)
    {
      std::string::const_iterator start = s.begin();
      return parse(start, s.end(),
		   terminators,
		   flag_errors,
		   require_full_parse,
		   false);
    }

    inline cwidget::util::ref_ptr<pattern>
      parse(const std::string &s,
	    const std::vector<const char *> &terminators,
	    bool flag_errors,
	    bool require_full_parse,
	    bool partial)
    {
      std::string::const_iterator start = s.begin();
      return parse(start, s.end(),
		   terminators,
		   flag_errors,
		   require_full_parse,
		   partial);
    }

    /** \brief Parse a string as a search pattern. */
    inline cwidget::util::ref_ptr<pattern>
      parse(const std::string &s,
	    bool flag_errors,
	    bool require_full_parse)
    {
      return parse(s, std::vector<const char *>(),
		   flag_errors,
		   require_full_parse);
    }

    /** \brief Parse a string as a search pattern. */
    inline cwidget::util::ref_ptr<pattern>
      parse(const std::string &s,
	    bool flag_errors,
	    bool require_full_parse,
	    bool partial)
    {
      return parse(s, std::vector<const char *>(),
		   flag_errors,
		   require_full_parse,
		   partial);
    }

    /** \brief Parse a string as a search pattern.
     *
     *  Errors will be flagged by generating apt errors, and it is an
     *  error if part of the input string is left over after parsing.
     *
     *  \param s       The string to parse.
     *
     *  \return  The parsed expression, or NULL if an
     *  error occurred or if the input string was empty.
     */
    inline cwidget::util::ref_ptr<pattern>
      parse(const std::string &s)
    {
      return parse(s, true, true);
    }

    /** \brief Parse a string as a search pattern.
     *
     *  It is an error if part of the input string is left over after
     *  parsing.
     *
     *  \throw MatchingException if the pattern cannot be parsed.
     *
     *  \param s       The string to parse.
     *
     *  \return  The parsed expression, or NULL if an
     *  error occurred or if the input string was empty.
     */
    inline cwidget::util::ref_ptr<pattern>
      parse_with_errors(const std::string &s)
    {
      std::string::const_iterator begin(s.begin());
      return parse_with_errors(begin, s.end(),
			       std::vector<const char *>(),
			       true, false);
    }

    /** \brief Parse a string as a possibly incomplete search pattern.
     *
     *  Errors will be flagged by generating apt errors, and it is an
     *  error if part of the input string is left over after parsing.
     *
     *  \param s       The string to parse.
     *
     *  \return  The parsed expression, or NULL if an
     *  error occurred or if the input string was empty.
     */
    inline cwidget::util::ref_ptr<pattern>
      parse_with_extension(const std::string &s)
    {
      return parse(s, true, true, true);
    }
  }
}

#endif