Flow123d  jenkins-Flow123d-linux-release-multijob-282
comment_filter.hh
Go to the documentation of this file.
1 /*
2  * comment_filter.hh
3  *
4  * Created on: May 7, 2012
5  * Author: jb
6  */
7 
8 #ifndef COMMENT_FILTER_HH_
9 #define COMMENT_FILTER_HH_
10 
11 #define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
12 #define BOOST_MPL_LIMIT_VECTOR_SIZE 30
13 #include <boost/mpl/vector.hpp>
15 
16 namespace Input {
17 
18 namespace io = boost::iostreams;
19 
20 /**
21  * This structure implements a finite state machine for filtering comments of the input file.
22  * The comment starts with '#' at any place save in quotas and after backslash. The comment ends be fors non-backslashed end of line.
23  * Backslashed end of line consists of the backslash, any sequence of whitespaces
24  *
25  * We assume, that json parser use backslash as an escape character, so we also have to escape any special meaning
26  * of the next character, but we keep backslash on the place.
27  *
28  * Don't ask me for syntactic bloat. This struct has to define \p transition_table of the FSM. Access to the rules is
29  * provided by methods derived from \p io::finite_state_machine. Then the FSM is used to implement filter.
30  */
31 struct uncommenting_fsm : public io::finite_state_machine<uncommenting_fsm> {
32  BOOST_IOSTREAMS_FSM(uncommenting_fsm) // Define skip and push event handlers
33  inline void slash_and_push(char ch) // Event handler for a slash that do not open an comment.
34  { push('/'); push(ch); }
35  typedef uncommenting_fsm self;
36 
37  /**
38  * Declaration of all states of FSM.
39  */
40  static const int no_comment = initial_state;
41  static const int one_slash = initial_state + 1;
42  static const int one_line_comment = initial_state + 2;
43  static const int multi_line_comment = initial_state + 3;
44  static const int star_in_comment = initial_state + 4;
45  static const int no_comment_bsl = initial_state + 5;
46  static const int in_quote = initial_state + 6;
47  static const int in_quote_bsl = initial_state + 7;
48 
49  /**
50  * Declaration of rules of the FSM.
51  */
52  typedef boost::mpl::vector<
53  // format of the table:
54  // actual state, input character set, next state, output i.e. pass or skip input character
55 
56  row<no_comment, is<'/'>, one_slash, &self::skip>,
57  row<no_comment, is<'"'>, in_quote, &self::push>,
58  row<no_comment, is<'\\'>, no_comment_bsl, &self::push>, // push backslash and any following character
59  row<no_comment, is_any, no_comment, &self::push>,
60 
61  row<one_slash, is<'/'>, one_line_comment, &self::skip>,
62  row<one_slash, is<'*'>, multi_line_comment, &self::skip>,
63  row<one_slash, is_any, no_comment, &self::slash_and_push>,
64 
65  row<one_line_comment, is<'\n'>, no_comment, &self::push>, // comment up to the end line, but do not remove EOL
66  row<one_line_comment, is<'\r'>, no_comment, &self::push>,
67  row<one_line_comment, is_any , one_line_comment, &self::skip>, // skip everything else in comment
68 
69  row<multi_line_comment, is<'*'>, star_in_comment, &self::skip>,
70  row<multi_line_comment, is<'\n'>, multi_line_comment, &self::push>, // preserve line numbers
71  row<multi_line_comment, is<'\r'>, multi_line_comment, &self::push>,
72  row<multi_line_comment, is_any, multi_line_comment, &self::skip>,
73 
74  row<star_in_comment, is<'/'>, no_comment, &self::skip>,
75  row<star_in_comment, is<'\n'>, multi_line_comment, &self::push>, // preserve line numbers
76  row<star_in_comment, is<'\r'>, multi_line_comment, &self::push>,
77  row<star_in_comment, is_any, multi_line_comment, &self::skip>,
78 
79  row<in_quote, is<'"'>, no_comment, &self::push>,
80  row<in_quote, is<'\\'>, in_quote_bsl, &self::push>,
81  row<in_quote, is_any, in_quote, &self::push>,
82 
83  row<in_quote_bsl, is_any, in_quote, &self::push>,
84 
85  row<no_comment_bsl, is_any, no_comment, &self::push>
87 };
88 
89 /**
90  * Declare an io filter based on FSM for filtering comments.
91  */
93 
94 
95 } // namespace Input
96 
97 
98 #endif /* COMMENT_FILTER_HH_ */
static const int in_quote
BOOST_IOSTREAMS_FSM(uncommenting_fsm) inline void slash_and_push(char ch)
static const int star_in_comment
io::finite_state_filter< uncommenting_fsm > uncommenting_filter
static const int no_comment_bsl
static const int one_line_comment
boost::mpl::vector< row< no_comment, is<'/'>, one_slash,&self::skip >, row< no_comment, is<'"'>, in_quote, &self::push>, row<no_comment, is<'\\'>, no_comment_bsl, &self::push>, row<no_comment, is_any, no_comment, &self::push>, row<one_slash, is<'/'>, one_line_comment, &self::skip>, row<one_slash, is<'*'>, multi_line_comment, &self::skip>, row<one_slash, is_any, no_comment, &self::slash_and_push>, row<one_line_comment, is<'\n'>, no_comment, &self::push>, row<one_line_comment, is<'\r'>, no_comment, &self::push>, row<one_line_comment, is_any , one_line_comment, &self::skip>, row<multi_line_comment, is<'*'>, star_in_comment, &self::skip>, row<multi_line_comment, is<'\n'>, multi_line_comment, &self::push>, row<multi_line_comment, is<'\r'>, multi_line_comment, &self::push>, row<multi_line_comment, is_any, multi_line_comment, &self::skip>, row<star_in_comment, is<'/'>, no_comment, &self::skip>, row<star_in_comment, is<'\n'>, multi_line_comment, &self::push>, row<star_in_comment, is<'\r'>, multi_line_comment, &self::push>, row<star_in_comment, is_any, multi_line_comment, &self::skip>, row<in_quote, is<'"'>, no_comment,&self::push >, row< in_quote, is<'\\'>, in_quote_bsl,&self::push >, row< in_quote, is_any, in_quote,&self::push >, row< in_quote_bsl, is_any, in_quote,&self::push >, row< no_comment_bsl, is_any, no_comment,&self::push > > transition_table
static const int one_slash
static const int in_quote_bsl
static const int no_comment
static const int multi_line_comment