All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
icarus::details::KeyValueParser Class Reference

Parser to fill a KeyValuesData structure out of a character buffer. More...

#include <KeyValueParser.h>

Classes

struct  FormatParams_t
 Parameters of the format. More...
 
struct  ParserError
 

Public Types

using Error = icarus::KeyValuesData::Error
 Base of all errors by KeyValueParser. More...
 
using ParsedData_t = icarus::KeyValuesData
 Type of returned data. More...
 

Public Member Functions

 KeyValueParser (FormatParams_t formatParams=DefaultFormatParameters, icarus::ParsingToolkit::Params_t parserParams=icarus::ParsingToolkit::DefaultParameters)
 Creates a parser with the specified parsing parameters. More...
 
ParsedData_t parse (std::string const &s) const
 Runs the parser on a string. More...
 
ParsedData_t parse (std::istream &stream) const
 Parses the stream and returns a data structure with the content. More...
 
ParsedData_t parse (std::istream &&stream) const
 
ParsedData_t operator() (std::istream &stream) const
 

Static Public Attributes

static const FormatParams_t DefaultFormatParameters
 

Private Types

enum  keyType { keyType::unsupported, keyType::create, keyType::add }
 

Private Member Functions

keyType highlightSeparator (std::vector< std::string_view > &tokens) const
 Modifies tokens placing the key/value separator in its own token. More...
 
template<typename Key >
keyType keySepType (Key const &key) const
 Returns the type of key. More...
 

Private Attributes

icarus::ParsingToolkit fPTK
 Parsing toolkit (and its parameters). More...
 
FormatParams_t fFmt
 Parser format parameters. More...
 
std::vector< std::string > fKeys
 Sorted keys cache. More...
 

Detailed Description

Parser to fill a KeyValuesData structure out of a character buffer.

The parser processes an input stream or string. Parsing produces a data structure of type icarus::KeyValuesData; it is not possible to reconstruct the input from that structure, since quotations, escapes, formatting and comments are lost.

The supported format is:

Example of format

This is the text from the unit test:

# SPR test input file
Description: "
This is a test for the key-values parser with default settings.
It is expected to be used to describe the Single Photoelectron Response.
"
Contact: Gianluca Petrillo (petrillo@slac.stanford.edu)
Gain: 9.7e6 # from amplitude 4 mV
Tick: '2 ns'
Samples: 0.0 1.0 2.5 \
4.5 3.0 2.5
Samples:+1.8 1.6 1.2 0.8 0.8 0.7 0.7 0.6

Definition at line 73 of file KeyValueParser.h.

Member Typedef Documentation

Base of all errors by KeyValueParser.

Definition at line 78 of file KeyValueParser.h.

Type of returned data.

Definition at line 81 of file KeyValueParser.h.

Member Enumeration Documentation

Enumerator
unsupported 
create 
add 

Definition at line 113 of file KeyValueParser.h.

113 { unsupported, create, add };

Constructor & Destructor Documentation

icarus::details::KeyValueParser::KeyValueParser ( FormatParams_t  formatParams = DefaultFormatParameters,
icarus::ParsingToolkit::Params_t  parserParams = icarus::ParsingToolkit::DefaultParameters 
)

Creates a parser with the specified parsing parameters.

Definition at line 56 of file KeyValueParser.cxx.

61  : fPTK{ std::move(parserParams) }
62  , fFmt{ std::move(formatParams) }
63  , fKeys{ sortKeys({ fFmt.newKey, fFmt.addKey }) }
icarus::ParsingToolkit fPTK
Parsing toolkit (and its parameters).
std::string newKey
Sequence starting the values.
FormatParams_t fFmt
Parser format parameters.
std::string addKey
Sequence appending the values.
std::vector< std::string > fKeys
Sorted keys cache.

Member Function Documentation

keyType icarus::details::KeyValueParser::highlightSeparator ( std::vector< std::string_view > &  tokens) const
private

Modifies tokens placing the key/value separator in its own token.

Parameters
[in,out]tokenslist of tokens
Returns
the type of separator found

On success, the first token is the key, the second the separator that was found, and the rest are values. The list of tokens is modified in place. In case of failure, where the separator is not found where expected, tokens are unchanged.

The separator is expected to be either unquoted and unescaped in the first of the tokens, or otherwise at the beginning of the second token.

template<typename Key >
keyType icarus::details::KeyValueParser::keySepType ( Key const &  key) const
private

Returns the type of key.

ParsedData_t icarus::details::KeyValueParser::operator() ( std::istream &  stream) const
inline

Definition at line 104 of file KeyValueParser.h.

104 { return parse(stream); }
ParsedData_t parse(std::istream &stream) const
Parses the stream and returns a data structure with the content.
ParsedData_t icarus::details::KeyValueParser::parse ( std::istream &  stream) const

Parses the stream and returns a data structure with the content.

ParsedData_t icarus::details::KeyValueParser::parse ( std::istream &&  stream) const
inline

Definition at line 102 of file KeyValueParser.h.

102 { return parse(stream); }
ParsedData_t parse(std::istream &stream) const
Parses the stream and returns a data structure with the content.
ParsedData_t icarus::details::KeyValueParser::parse ( std::string const &  s) const

Runs the parser on a string.

Member Data Documentation

const icarus::details::KeyValueParser::FormatParams_t icarus::details::KeyValueParser::DefaultFormatParameters
static

Definition at line 89 of file KeyValueParser.h.

FormatParams_t icarus::details::KeyValueParser::fFmt
private

Parser format parameters.

Definition at line 116 of file KeyValueParser.h.

std::vector<std::string> icarus::details::KeyValueParser::fKeys
private

Sorted keys cache.

Definition at line 118 of file KeyValueParser.h.

icarus::ParsingToolkit icarus::details::KeyValueParser::fPTK
private

Parsing toolkit (and its parameters).

Definition at line 115 of file KeyValueParser.h.


The documentation for this class was generated from the following files: