A library for working with phylogenetic and population genetic data.
v0.27.0
scanner.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_SCANNER_H_
2 #define GENESIS_UTILS_IO_SCANNER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2022 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
35 
36 #include <cassert>
37 #include <cctype>
38 #include <functional>
39 #include <stdexcept>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // Helper Classes and Enums
46 // =================================================================================================
47 
62 enum class SkipWhitespace : unsigned char
63 {
67  kNone = 0,
68 
72  kLeading = 1,
73 
77  kTrailing = 2,
78 
82  kSurrounding = 3
83 };
84 
97 inline constexpr bool operator & ( SkipWhitespace lhs, SkipWhitespace rhs ) noexcept
98 {
99  using T = std::underlying_type< SkipWhitespace >::type;
100  return static_cast< T >( lhs ) & static_cast< T >( rhs );
101 }
102 
103 // =================================================================================================
104 // Scanners
105 // =================================================================================================
106 
107 // -------------------------------------------------------------------------
108 // end of line
109 // -------------------------------------------------------------------------
110 
117 template< typename InputStream >
119  InputStream& source
120 ) {
121  while( source && *source != '\n' ) {
122  ++source;
123  }
124 }
125 
133 template< typename InputStream >
134 inline std::string read_to_end_of_line(
135  InputStream& source
136 ) {
137  std::string target;
138  while( source && *source != '\n' ) {
139  target += *source;
140  ++source;
141  }
142  return target;
143 }
144 
145 // -------------------------------------------------------------------------
146 // skip while
147 // -------------------------------------------------------------------------
148 
152 template< typename InputStream >
153 inline void skip_while(
154  InputStream& source,
155  char criterion
156 ) {
157  while( source && *source == criterion ) {
158  ++source;
159  }
160 }
161 
166 template< typename InputStream >
167 inline void skip_while(
168  InputStream& source,
169  std::function<bool (char)> criterion
170 ) {
171  while( source && criterion( *source )) {
172  ++source;
173  }
174 }
175 
176 // -------------------------------------------------------------------------
177 // skip until
178 // -------------------------------------------------------------------------
179 
183 template< typename InputStream >
184 inline void skip_until(
185  InputStream& source,
186  char criterion
187 ) {
188  while( source && *source != criterion ) {
189  ++source;
190  }
191 }
192 
197 template< typename InputStream >
198 inline void skip_until(
199  InputStream& source,
200  std::function<bool (char)> criterion
201 ) {
202  while( source && ! criterion( *source )) {
203  ++source;
204  }
205 }
206 
207 // -------------------------------------------------------------------------
208 // read while
209 // -------------------------------------------------------------------------
210 
215 template< typename InputStream >
216 inline std::string read_while(
217  InputStream& source,
218  char criterion
219 ) {
220  std::string target;
221  while( source && *source == criterion ) {
222  target += *source;
223  ++source;
224  }
225  return target;
226 }
227 
232 template< typename InputStream >
233 inline std::string read_while(
234  InputStream& source,
235  std::function<bool (char)> criterion
236 ) {
237  std::string target;
238  while( source && criterion( *source )) {
239  target += *source;
240  ++source;
241  }
242  return target;
243 }
244 
245 // -------------------------------------------------------------------------
246 // read until
247 // -------------------------------------------------------------------------
248 
253 template< typename InputStream >
254 inline std::string read_until(
255  InputStream& source,
256  char criterion
257 ) {
258  std::string target;
259  while( source && *source != criterion ) {
260  target += *source;
261  ++source;
262  }
263  return target;
264 }
265 
270 template< typename InputStream >
271 inline std::string read_until(
272  InputStream& source,
273  std::function<bool (char)> criterion
274 ) {
275  std::string target;
276  while( source && ! criterion( *source )) {
277  target += *source;
278  ++source;
279  }
280  return target;
281 }
282 
283 // -------------------------------------------------------------------------
284 // read char
285 // -------------------------------------------------------------------------
286 
298 template< typename InputStream >
299 inline char read_char_or_throw(
300  InputStream& source,
301  char criterion,
303 ) {
304  // Skip leading whitespace
305  if( skip_ws & SkipWhitespace::kLeading ) {
306  skip_while( source, ::isspace );
307  }
308 
309  // Check char and move to next.
310  if( !source || *source != criterion ) {
311  throw std::runtime_error(
312  std::string("In ") + source.source_name() + ": " +
313  "Expecting " + char_to_hex( criterion ) + " at " + source.at() + ", " +
314  "but received " + char_to_hex( *source ) + " instead."
315  );
316  }
317  assert( source && *source == criterion );
318  ++source;
319 
320  // Skip trailing whitespace
321  if( skip_ws & SkipWhitespace::kTrailing ) {
322  skip_while( source, ::isspace );
323  }
324 
325  return criterion;
326 }
327 
339 template< typename InputStream >
340 inline char read_char_or_throw(
341  InputStream& source,
342  std::function<bool (char)> criterion,
344 ) {
345  // Skip leading whitespace
346  if( skip_ws & SkipWhitespace::kLeading ) {
347  skip_while( source, ::isspace );
348  }
349 
350  // Check char and move to next.
351  if( !source || ! criterion( *source )) {
352  throw std::runtime_error(
353  std::string("In ") + source.source_name() + ": " +
354  "Unexpected char " + char_to_hex( *source ) + " at " + source.at() + "."
355  );
356  }
357  assert( source );
358  auto chr = *source;
359  ++source;
360 
361  // Skip trailing whitespace
362  if( skip_ws & SkipWhitespace::kTrailing ) {
363  skip_while( source, ::isspace );
364  }
365 
366  return chr;
367 }
368 
369 // -------------------------------------------------------------------------
370 // expect char
371 // -------------------------------------------------------------------------
372 
384 template< typename InputStream >
386  InputStream& source,
387  char criterion,
389 ) {
390  // Skip leading whitespace
391  if( skip_ws & SkipWhitespace::kLeading ) {
392  skip_while( source, ::isspace );
393  }
394 
395  // Check char.
396  if( !source || *source != criterion ) {
397  throw std::runtime_error(
398  std::string("In ") + source.source_name() + ": " +
399  "Expecting " + char_to_hex( criterion ) + " at " + source.at() + ", " +
400  "but received " + char_to_hex( *source ) + " instead."
401  );
402  }
403 
404  // Skip trailing whitespace
405  if( skip_ws & SkipWhitespace::kTrailing ) {
406  skip_while( source, ::isspace );
407  }
408 }
409 
421 template< typename InputStream >
423  InputStream& source,
424  std::function<bool (char)> criterion,
426 ) {
427  // Skip leading whitespace
428  if( skip_ws & SkipWhitespace::kLeading ) {
429  skip_while( source, ::isspace );
430  }
431 
432  // Check char.
433  if( !source || ! criterion( *source )) {
434  throw std::runtime_error(
435  std::string("In ") + source.source_name() + ": " +
436  "Unexpected char " + char_to_hex( *source ) + " at " + source.at() + "."
437  );
438  }
439 
440  // Skip trailing whitespace
441  if( skip_ws & SkipWhitespace::kTrailing ) {
442  skip_while( source, ::isspace );
443  }
444 }
445 
446 } // namespace utils
447 } // namespace genesis
448 
449 #endif // include guard
genesis::utils::InputStream::at
std::string at() const
Return a textual representation of the current input position in the form "line:column".
Definition: input_stream.hpp:481
genesis::utils::InputStream
Stream interface for reading data from an InputSource, that keeps track of line and column counters.
Definition: input_stream.hpp:81
genesis::utils::SkipWhitespace::kLeading
@ kLeading
Skip all whitespace in the input stream, then treat the next non-white char.
genesis::utils::InputStream::source_name
std::string source_name() const
Get the input source name where this stream reads from.
Definition: input_stream.hpp:522
genesis::utils::read_while
std::string read_while(InputStream &source, char criterion)
Lexing function that reads from the stream while its current char equals the provided one....
Definition: scanner.hpp:216
genesis::utils::skip_to_end_of_line
void skip_to_end_of_line(InputStream &source)
Lexing function that advances the stream to the end of the line, i.e., to the new line char.
Definition: scanner.hpp:118
genesis::utils::SkipWhitespace::kNone
@ kNone
Skip no whitespace. Thus, immediately treat the current input char.
genesis::utils::affirm_char_or_throw
void affirm_char_or_throw(InputStream &source, char criterion, SkipWhitespace skip_ws=SkipWhitespace::kNone)
Lexing function that checks whether the current char from the stream equals the provided one.
Definition: scanner.hpp:385
genesis::utils::read_char_or_throw
char read_char_or_throw(InputStream &source, char criterion, SkipWhitespace skip_ws=SkipWhitespace::kNone)
Lexing function that reads a single char from the stream and checks whether it equals the provided on...
Definition: scanner.hpp:299
genesis::utils::SkipWhitespace::kSurrounding
@ kSurrounding
Skip whitespace, treat the first non-white char, then skip all following whitespace.
genesis::utils::operator&
constexpr bool operator&(SkipWhitespace lhs, SkipWhitespace rhs) noexcept
And-operator to check whether a SkipWhitespace is set.
Definition: scanner.hpp:97
genesis::utils::skip_while
void skip_while(InputStream &source, char criterion)
Lexing function that advances the stream while its current char equals the provided one.
Definition: scanner.hpp:153
genesis::utils::read_until
std::string read_until(InputStream &source, char criterion)
Lexing function that reads from the stream until its current char equals the provided one....
Definition: scanner.hpp:254
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
char.hpp
genesis::utils::skip_until
void skip_until(InputStream &source, char criterion)
Lexing function that advances the stream until its current char equals the provided one.
Definition: scanner.hpp:184
genesis::utils::SkipWhitespace
SkipWhitespace
Option to determine how to treat surrounding whitespace when scanning an input stream.
Definition: scanner.hpp:62
genesis::utils::char_to_hex
std::string char_to_hex(char c, bool full)
Return the name and hex representation of a char.
Definition: char.cpp:118
genesis::utils::SkipWhitespace::kTrailing
@ kTrailing
Treat the current char in the input stream, then skip the following whitespace.
genesis::utils::read_to_end_of_line
std::string read_to_end_of_line(InputStream &source)
Lexing function that reads until the end of the line (i.e., to the new line char),...
Definition: scanner.hpp:134