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 
96 #include "fhiclcpp/coding.h"
98 #include "fhiclcpp/fwd.h"
99 
100 #include <sstream>
101 #include <stack>
102 #include <string>
103 
104 namespace fhicl {
105  namespace detail {
106 
108  public:
109  PrettifierAnnotated(unsigned initial_indent_level = 0);
110 
112  result() const
113  {
114  return buffer_.str();
115  }
116 
117  private:
118  void before_action(key_t const&,
119  any_t const&,
120  ParameterSet const*) override;
121  void after_action() override;
122 
123  void enter_table(key_t const&, any_t const&) override;
124  void enter_sequence(key_t const&, any_t const&) override;
125 
126  void exit_table(key_t const&, any_t const&) override;
127  void exit_sequence(key_t const&, any_t const&) override;
128 
129  void atom(key_t const&, any_t const&) override;
130 
131  void push_size_(any_t const&);
132  void pop_size_();
133 
134  std::ostringstream buffer_;
138  std::stack<std::size_t> sequence_sizes_;
139  std::size_t curr_size_;
140  };
141  }
142 }
143 
144 #endif /* fhiclcpp_detail_PrettifierAnnotated_h */
145 
146 // Local variables:
147 // mode: c++
148 // End:
void atom(key_t const &, any_t const &) override
std::stack< std::size_t > sequence_sizes_
void before_action(key_t const &, any_t const &, ParameterSet const *) override
void exit_table(key_t const &, any_t const &) override
void enter_sequence(key_t const &, any_t const &) override
void exit_sequence(key_t const &, any_t const &) override
PrettifierAnnotated(unsigned initial_indent_level=0)
void enter_table(key_t const &, any_t const &) override
enum BeamMode string