PrettifierAnnotated.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_detail_PrettifierAnnotated_h
2 #define fhiclcpp_detail_PrettifierAnnotated_h
3 
4 /*
5  ======================================================================
6 
7  PrettifierAnnotated
8 
9  ======================================================================
10 
11  Class used when
12 
13  'ParameterSet::to_indented_string(unsigned,print_mode::annotated)'
14 
15  is called. This class provides a human-readable string representing
16  the entire (nested) contents of a ParameterSet object, as well as
17  annotations that describe in what FHiCL configuration file, and in
18  what line of that file, the particular parameter was set or
19  reassigned.
20 
21  Currently supported format:
22  ===========================
23 
24  This will provide a print out that looks like:
25 
26 
27  ^<-IND->
28  ^ p1: { # file:1$ p1: { # file:1
29  ^ }$ }
30  ^ p2: { # file:2$ p2: { # file:2
31  ^ a: something # ""$ a: something # ""
32  ^ }$ }
33  ^ p3: { # new_file:14$ p3: { # new_file:14
34  ^ a: else # new_file:15$ a: else # new_file:15
35  ^ b: [ # file:7$ b: [ # file:7
36  ^ ]$ ]
37  ^ c: [ # file:9$ Rendered c: [ # file:9
38  ^ 11 # ""$ ========> 11 # ""
39  ^ ]$ ]
40  ^ d: [ # file:14$ d: [ # file:14
41  ^ 11, # ""$ 11, # ""
42  ^ 12 # file:28$ 12 # file:28
43  ^ ]$ ]
44  ^ p4: { # new_file:16$ p4: { # new_file:16
45  ^ d: e # new_file:17$ d: e # new_file:17
46  ^ }$ }
47  ^ }$ }
48  ^<-IND->
49 
50  Note that the caret ^ (dollar-sign $) represents the beginning (end)
51  of the line and is not printed, nor is the <-IND-> string, which
52  represents a user-provided width value for the initial indentation
53  level. The double quotes "" are printed whenever the annotation
54  from the previous line is the same as the current.
55 
56  Maintenance notes:
57  ==================
58 
59  [1] A couple stack objects are used: the Indentation class, as well
60  as std::stack<std::size_t>, where the latter represents the
61  sizes of the stacked sequences. Keeping track of the sequence
62  is necessary so that the last sequence element does not have a
63  ',' character that follows it.
64 
65  To use these classes correctly, the Indentation must be updated
66  during each {enter,exit}_{table,sequence} call, and the
67  sequence-sizes stack must be updated during each
68  {enter,exit}_sequence call.
69 
70  [2] There are cases where the annotation information is not
71  available:
72 
73  (a) The parameter was introduced in the C++ code (either by the
74  user, or by the system) and, therefore, has no file source.
75 
76  (b) The parameter was introduced through a substitution:
77 
78  p1: { a: b }
79  p1.p2.c: d
80 
81  In this case, 'p1', 'a', and 'c' would each have source
82  annotations, but 'p2' would not.
83 
84  (c) The parameter originates from an external source (e.g. a
85  database), and no file source exists.
86 
87  In these cases, no annotation is provided.
88 
89  [3] To determine if the source annotation is the same as what has
90  been previously inserted into the buffer, we cache the source
91  information during the 'after_action' step.
92 
93 */
94 
97 #include "fhiclcpp/fwd.h"
98 
99 #include <sstream>
100 #include <stack>
101 #include <string>
102 
103 namespace fhicl::detail {
104 
106  public:
107  PrettifierAnnotated(unsigned initial_indent_level = 0);
108 
110  result() const
111  {
112  return buffer_.str();
113  }
114 
115  private:
116  void before_action(key_t const&,
117  any_t const&,
118  ParameterSet const*) override;
119  void after_action(key_t const&) override;
120 
121  void enter_table(key_t const&, any_t const&) override;
122  void enter_sequence(key_t const&, any_t const&) override;
123 
124  void exit_table(key_t const&, any_t const&) override;
125  void exit_sequence(key_t const&, any_t const&) override;
126 
127  void atom(key_t const&, any_t const&) override;
128 
129  void push_size_(any_t const&);
130  void pop_size_();
131 
132  std::ostringstream buffer_;
136  std::stack<std::size_t> sequence_sizes_;
137  std::size_t curr_size_;
138  };
139 }
140 
141 #endif /* fhiclcpp_detail_PrettifierAnnotated_h */
142 
143 // Local variables:
144 // mode: c++
145 // End:
std::string string
Definition: nybbler.cc:12
void before_action(key_t const &, any_t const &, ParameterSet const *) override
std::stack< std::size_t > sequence_sizes_
void enter_table(key_t const &, any_t const &) override
void after_action(key_t const &) override
void atom(key_t const &, any_t const &) override
void exit_table(key_t const &, any_t const &) override
void exit_sequence(key_t const &, any_t const &) override
void enter_sequence(key_t const &, any_t const &) override
PrettifierAnnotated(unsigned initial_indent_level=0)