All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KeyValueParser.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/PMT/Algorithms/KeyValueParser.h
3  * @brief Simple parser for "key: value" text.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date May 13, 2022
6  * @see icaruscode/PMT/Algorithms/KeyValueParser.cxx
7  */
8 
9 #ifndef ICARUSCODE_PMT_ALGORITHMS_KEYVALUEPARSER_H
10 #define ICARUSCODE_PMT_ALGORITHMS_KEYVALUEPARSER_H
11 
12 // ICARUS libraries
15 
16 // C++ standard libraries
17 #include <iosfwd> // std::istream
18 #include <string_view>
19 #include <vector>
20 #include <string>
21 
22 
23 // -----------------------------------------------------------------------------
24 namespace icarus::details { class KeyValueParser; }
25 /**
26  * @class icarus::details::KeyValueParser
27  * @brief Parser to fill a `KeyValuesData` structure out of a character buffer.
28  *
29  * The parser processes an input stream or string.
30  * Parsing produces a data structure of type `icarus::KeyValuesData`; it is not
31  * possible to reconstruct the input from that structure, since quotations,
32  * escapes, formatting and comments are lost.
33  *
34  * The supported format is:
35  * * one key/value pair per line
36  * * all keys and values are stripped of trailing and ending blank characters
37  * * key is everything before the key separator (by default, `:` to
38  * create/overwrite, `:+` to create/append values)
39  * * array values are separated by spaces
40  * * quotation supports both single and double quotes
41  * * comments start with a word beginning with `#` and extend to the end of the
42  * line
43  * * escape character (backslash by default) drops the special meaning of
44  * comment, key and quote symbols
45  *
46  *
47  *
48  *
49  *
50  * Example of format
51  * ------------------
52  *
53  * This is the text from the unit test:
54  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55  *
56  * # SPR test input file
57  *
58  * Description: "
59  * This is a test for the key-values parser with default settings.
60  * It is expected to be used to describe the Single Photoelectron Response.
61  * "
62  *
63  * Contact: Gianluca Petrillo (petrillo@slac.stanford.edu)
64  *
65  * Gain: 9.7e6 # from amplitude 4 mV
66  * Tick: '2 ns'
67  * Samples: 0.0 1.0 2.5 \
68  * 4.5 3.0 2.5
69  * Samples:+1.8 1.6 1.2 0.8 0.8 0.7 0.7 0.6
70  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71  *
72  */
74 
75  public:
76 
77  /// Base of all errors by KeyValueParser.
79  struct ParserError; ///< Generic error: base of all errors by KeyValueParser.
80 
81  using ParsedData_t = icarus::KeyValuesData; ///< Type of returned data.
82 
83  /// Parameters of the format.
84  struct FormatParams_t {
85  std::string newKey { ":" }; ///< Sequence starting the values.
86  std::string addKey { ":+" }; ///< Sequence appending the values.
87  };
88 
90 
91  /// Creates a parser with the specified parsing parameters.
96  );
97 
98 
99  //@{
100  /// Parses the `stream` and returns a data structure with the content.
101  ParsedData_t parse(std::istream& stream) const;
102  ParsedData_t parse(std::istream&& stream) const { return parse(stream); }
103 
104  ParsedData_t operator() (std::istream& stream) const { return parse(stream); }
105  //@}
106 
107  /// Runs the parser on a string.
108  ParsedData_t parse(std::string const& s) const;
109 
110 
111  private:
112 
113  enum class keyType { unsupported, create, add };
114 
115  icarus::ParsingToolkit fPTK; ///< Parsing toolkit (and its parameters).
116  FormatParams_t fFmt; ///< Parser format parameters.
117 
118  std::vector<std::string> fKeys; ///< Sorted keys cache.
119 
120 
121  /**
122  * @brief Modifies `tokens` placing the key/value separator in its own token.
123  * @param[in,out] tokens list of tokens
124  * @return the type of separator found
125  *
126  * On success, the first token is the key, the second the separator that was
127  * found, and the rest are values. The list of `tokens` is modified in place.
128  * In case of failure, where the separator is not found where expected,
129  * `tokens` are unchanged.
130  *
131  * The separator is expected to be either unquoted and unescaped in the first
132  * of the tokens, or otherwise at the beginning of the second token.
133  */
134  keyType highlightSeparator(std::vector<std::string_view>& tokens) const;
135 
136 
137  /// Returns the type of `key`.
138  template <typename Key>
139  keyType keySepType(Key const& key) const;
140 
141 }; // icarus::details::KeyValueParser
142 
143 
144 // -----------------------------------------------------------------------------
145 // --- Exception class definitions
146 // -----------------------------------------------------------------------------
148 
150  (unsigned int iLine, std::string const& line, std::string const& msg);
151 
152 }; // icarus::details::KeyValueParser::Error()
153 
154 
155 // -----------------------------------------------------------------------------
156 
157 #endif // ICARUSCODE_PMT_ALGORITHMS_KEYVALUEPARSER_H
KeyValueParser(FormatParams_t formatParams=DefaultFormatParameters, icarus::ParsingToolkit::Params_t parserParams=icarus::ParsingToolkit::DefaultParameters)
Creates a parser with the specified parsing parameters.
All parsing parameters.
keyType keySepType(Key const &key) const
Returns the type of key.
icarus::ParsingToolkit fPTK
Parsing toolkit (and its parameters).
ParsedData_t operator()(std::istream &stream) const
Simple parsed data format.
Collection of items with key/values structure.
static const FormatParams_t DefaultFormatParameters
keyType highlightSeparator(std::vector< std::string_view > &tokens) const
Modifies tokens placing the key/value separator in its own token.
Parser to fill a KeyValuesData structure out of a character buffer.
std::string newKey
Sequence starting the values.
FormatParams_t fFmt
Parser format parameters.
Utilities for text parsing.
static Params_t const DefaultParameters
Simple text parsing utilities.
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
ParsedData_t parse(std::istream &stream) const
Parses the stream and returns a data structure with the content.
ParserError(unsigned int iLine, std::string const &line, std::string const &msg)
icarus::KeyValuesData ParsedData_t
Type of returned data.
std::string addKey
Sequence appending the values.
std::vector< std::string > fKeys
Sorted keys cache.
ParsedData_t parse(std::istream &&stream) const