bibtexreader.hpp
10.7 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) 2012 Sergiu Dotenco
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/**
* @brief Boost.Spirit based BibTeX parser.
* @file bibtexreader.hpp
* @author Sergiu Dotenco
* @version 0.1
*/
#ifndef BIBTEXREADER_HPP
#define BIBTEXREADER_HPP
#pragma once
#include <istream>
#include <string>
#include <boost/fusion/include/vector.hpp>
#include <boost/io/ios_state.hpp>
#include <boost/mpl/and.hpp>
#include <boost/range.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/utility/enable_if.hpp>
#include "bibtexentry.hpp"
namespace bibtex {
/**
* @brief Single BibTeX entry reader.
*
* @tparam ForwardIterator Input sequence iterator type.
* @tparam Skipper Skipper type.
*/
template <class ForwardIterator, class Skipper>
class BibTeXReader
: public boost::spirit::qi::grammar<ForwardIterator, BibTeXEntry(), Skipper>
{
public:
BibTeXReader()
: BibTeXReader::base_type(start_, "single BibTeX entry")
{
using namespace boost::spirit;
namespace ph = boost::phoenix;
namespace sn = detail::encoding;
escapedBrace.add
("\\{", '{')
("\\}", '}')
;
escapedQuote.add
("\\\"", '"')
;
tag_ = +detail::encoding::alnum;
entryKey_ = qi::lexeme[+(~qi::char_(',') - sn::space)];
key_ = qi::lexeme[+(~qi::char_("=,})") - sn::space)];
escapedText_
= !qi::lit('{') >> +(escapedBrace | ~qi::char_("{}"))
;
quoteText_
= +(escapedQuote | ~qi::char_("\"{}"))
;
innerBraceText_
%=
qi::as_string
[
qi::char_('{')
>>
*(innerBraceText_ | escapedText_)
>>
qi::char_('}')
]
|
escapedText_
;
innerQuoteText_
%=
qi::as_string
[
qi::char_('{')
>>
*(innerQuoteText_ | escapedText_)
>>
qi::char_('}')
]
|
quoteText_
;
quoted_
= qi::lexeme
[
( '"' >> *innerQuoteText_ >> '"' )
|
( '{' >> *innerBraceText_ >> '}' )
]
[
_val = ph::accumulate(boost::spirit::_1, ph::construct<std::string>())
]
;
value_
= quoted_
| +~qi::char_(",})#")
;
values_
= value_ % '#'
;
field_
= key_ >> '=' >> values_
;
fields_ = -(field_ % ',');
body_
= -entryKey_ >> ',' // tolerate an empty key
>> fields_
>> -qi::lit(',') // accept a trailing comma
;
generic_
=
'@' >> tag_
[
ph::at_c<0>(_val) = boost::spirit::_1
]
>>
(
('{' >> body_ >> '}')
|
('(' >> body_ >> ')')
)
[
ph::at_c<1>(_val) = ph::at_c<0>(boost::spirit::_1),
ph::at_c<2>(_val) = ph::at_c<1>(boost::spirit::_1)
]
;
string_
=
'@' >> sn::no_case
[
qi::string("string")
[
ph::at_c<0>(_val) = boost::spirit::_1
]
]
>>
(
('{' >> field_ >> '}')
|
('(' >> field_ >> ')')
)
[
ph::assign(ph::bind(&BibTeXEntry::fields, _val), 1, boost::spirit::_1)
]
;
simple_
=
'@' >> sn::no_case
[
(
sn::string("comment")
|
sn::string("include")
|
sn::string("preamble")
)
[
ph::at_c<0>(_val) = boost::spirit::_1
]
]
>>
(
('{' >> values_ >> '}')
|
('(' >> values_ >> ')')
)
[
ph::assign(ph::bind(&BibTeXEntry::fields, _val), 1,
ph::construct<KeyValue>
(ph::construct<KeyValue::first_type>(), boost::spirit::_1))
]
;
entry_
= string_
| simple_
| generic_
;
junk_
= *~qi::lit('@')
;
start_
= junk_ >> entry_
;
}
private:
typedef boost::spirit::qi::rule<ForwardIterator, std::string(), Skipper>
StringRule;
typedef boost::spirit::qi::rule<ForwardIterator, std::string()>
SimpleStringRule;
boost::spirit::qi::rule<ForwardIterator, BibTeXEntry(), Skipper> generic_;
boost::spirit::qi::rule<ForwardIterator, BibTeXEntry(), Skipper> include_;
boost::spirit::qi::rule<ForwardIterator, BibTeXEntry(), Skipper> simple_;
boost::spirit::qi::rule<ForwardIterator, BibTeXEntry(), Skipper> start_;
boost::spirit::qi::rule<ForwardIterator, BibTeXEntry(), Skipper> string_;
boost::spirit::qi::rule<ForwardIterator, BibTeXEntry(), Skipper> entry_;
boost::spirit::qi::rule<ForwardIterator, ValueVector(), Skipper> values_;
boost::spirit::qi::rule<ForwardIterator, Skipper> junk_;
boost::spirit::qi::rule<ForwardIterator,
boost::fusion::vector<boost::optional<std::string>,
KeyValueVector>(), Skipper> body_;
boost::spirit::qi::rule<ForwardIterator, KeyValue(), Skipper> field_;
boost::spirit::qi::rule<ForwardIterator, KeyValueVector(), Skipper>
fields_;
boost::spirit::qi::symbols<char, char> escapedBrace;
boost::spirit::qi::symbols<char, char> escapedQuote;
StringRule quoted_;
StringRule entryKey_;
StringRule key_;
StringRule tag_;
StringRule value_;
SimpleStringRule innerBraceText_;
SimpleStringRule escapedText_;
SimpleStringRule innerQuoteText_;
SimpleStringRule quoteText_;
};
template<class ForwardIterator, class Skipper>
inline bool read(ForwardIterator first, ForwardIterator last, Skipper& skipper,
BibTeXEntry& entry)
{
BibTeXReader<ForwardIterator, Skipper> parser;
return boost::spirit::qi::phrase_parse(first, last, parser, skipper, entry);
}
template<class ForwardIterator, class Skipper, class Container>
inline bool read(ForwardIterator first, ForwardIterator last, Skipper& skipper,
Container& entries, boost::enable_if<boost::is_same<
typename Container::value_type, BibTeXEntry> >* /*dummy*/ = NULL)
{
BibTeXReader<ForwardIterator, Skipper> parser;
return boost::spirit::qi::phrase_parse(first, last, *parser, skipper,
entries);
}
template<class ForwardRange, class Skipper>
inline bool read(const ForwardRange& range, Skipper& skipper,
BibTeXEntry& entry)
{
return read(boost::const_begin(range), boost::const_end(range), entry);
}
template<class ForwardRange, class Skipper, class Container>
inline bool read(const ForwardRange& range, Skipper& skipper,
Container& entries, boost::enable_if<boost::is_same<
typename Container::value_type, BibTeXEntry> >* /*dummy*/ = NULL)
{
return read(boost::const_begin(range), boost::const_end(range), entries);
}
template<class ForwardIterator>
inline bool read(ForwardIterator first, ForwardIterator last,
BibTeXEntry& entry)
{
return read(first, last, bibtex::space, entry);
}
template<class ForwardIterator, class Container>
inline bool read(ForwardIterator first, ForwardIterator last,
Container& entries, boost::enable_if<boost::is_same<
typename Container::value_type, BibTeXEntry> >* /*dummy*/ = NULL)
{
return read(first, last, bibtex::space, entries);
}
template<class ForwardRange>
inline bool read(const ForwardRange& range, BibTeXEntry& entry,
typename boost::enable_if<
boost::has_range_iterator<ForwardRange> >::type* /*dummy*/ = NULL)
{
return read(range, bibtex::space, entry);
}
template<class ForwardRange, class Container>
inline bool read(const ForwardRange& range, Container& entries,
typename boost::enable_if<
boost::has_range_iterator<ForwardRange> >::type* /*dummy*/ = NULL)
{
return read(range, bibtex::space, entries);
}
template<class E, class T>
inline bool read(std::basic_istream<E, T>& in, BibTeXEntry& e)
{
boost::io::ios_flags_saver saver(in);
in.unsetf(std::ios_base::skipws);
typedef boost::spirit::basic_istream_iterator<E> istream_iterator;
return read(istream_iterator(in), istream_iterator(), e);
}
template<class E, class T, class Container>
inline bool read(std::basic_istream<E, T>& in, Container& entries)
{
boost::io::ios_flags_saver saver(in);
in.unsetf(std::ios_base::skipws);
typedef boost::spirit::basic_istream_iterator<E> istream_iterator;
return read(istream_iterator(in), istream_iterator(), entries);
}
template<class E, class T>
inline std::basic_istream<E, T>& operator>>(std::basic_istream<E, T>& in,
BibTeXEntry& entry)
{
read(in, entry);
return in;
}
template<class E, class T, class Range>
inline typename boost::enable_if<
boost::mpl::and_<
boost::has_range_iterator<Range>,
boost::is_same<typename Range::value_type, BibTeXEntry>
>, std::basic_istream<E, T>
>::type& operator>>(std::basic_istream<E, T>& in, Range& entries)
{
read(in, entries);
return in;
}
} // namespace bibtex
#endif // BIBTEXREADER_HPP