Flow123d  3.9.0-895a22dee
comment_filter.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file comment_filter.hh
15  * @brief
16  */
17 
18 #ifndef COMMENT_FILTER_HH_
19 #define COMMENT_FILTER_HH_
20 
21 #define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
22 #define BOOST_MPL_LIMIT_VECTOR_SIZE 30
23 #include <boost/mpl/vector.hpp>
25 
26 namespace Input {
27 
28 namespace io = boost::iostreams;
29 
30 /**
31  * This structure implements a finite state machine for filtering comments of the input file.
32  * The comment starts with '#' at any place save in quotas and after backslash. The comment ends be fors non-backslashed end of line.
33  * Backslashed end of line consists of the backslash, any sequence of whitespaces
34  *
35  * We assume, that json parser use backslash as an escape character, so we also have to escape any special meaning
36  * of the next character, but we keep backslash on the place.
37  *
38  * Don't ask me for syntactic bloat. This struct has to define \p transition_table of the FSM. Access to the rules is
39  * provided by methods derived from \p io::finite_state_machine. Then the FSM is used to implement filter.
40  */
41 struct uncommenting_fsm : public io::finite_state_machine<uncommenting_fsm> {
42  BOOST_IOSTREAMS_FSM(uncommenting_fsm) // Define skip and push event handlers
43  inline void slash_and_push(char ch) // Event handler for a slash that do not open an comment.
44  { push('/'); push(ch); }
45  typedef uncommenting_fsm self;
46 
47  /**
48  * Declaration of all states of FSM.
49  */
50  static const int no_comment = initial_state;
51  static const int one_slash = initial_state + 1;
52  static const int one_line_comment = initial_state + 2;
53  static const int multi_line_comment = initial_state + 3;
54  static const int star_in_comment = initial_state + 4;
55  static const int no_comment_bsl = initial_state + 5;
56  static const int in_quote = initial_state + 6;
57  static const int in_quote_bsl = initial_state + 7;
58 
59  /**
60  * Declaration of rules of the FSM.
61  */
62  typedef boost::mpl::vector<
63  // format of the table:
64  // actual state, input character set, next state, output i.e. pass or skip input character
65 
66  row<no_comment, is<'/'>, one_slash, &self::skip>,
67  row<no_comment, is<'"'>, in_quote, &self::push>,
68  row<no_comment, is<'\\'>, no_comment_bsl, &self::push>, // push backslash and any following character
69  row<no_comment, is_any, no_comment, &self::push>,
70 
71  row<one_slash, is<'/'>, one_line_comment, &self::skip>,
72  row<one_slash, is<'*'>, multi_line_comment, &self::skip>,
73  row<one_slash, is_any, no_comment, &self::slash_and_push>,
74 
75  row<one_line_comment, is<'\n'>, no_comment, &self::push>, // comment up to the end line, but do not remove EOL
76  row<one_line_comment, is<'\r'>, no_comment, &self::push>,
77  row<one_line_comment, is_any , one_line_comment, &self::skip>, // skip everything else in comment
78 
79  row<multi_line_comment, is<'*'>, star_in_comment, &self::skip>,
80  row<multi_line_comment, is<'\n'>, multi_line_comment, &self::push>, // preserve line numbers
81  row<multi_line_comment, is<'\r'>, multi_line_comment, &self::push>,
82  row<multi_line_comment, is_any, multi_line_comment, &self::skip>,
83 
84  row<star_in_comment, is<'/'>, no_comment, &self::skip>,
85  row<star_in_comment, is<'\n'>, multi_line_comment, &self::push>, // preserve line numbers
86  row<star_in_comment, is<'\r'>, multi_line_comment, &self::push>,
87  row<star_in_comment, is_any, multi_line_comment, &self::skip>,
88 
89  row<in_quote, is<'"'>, no_comment, &self::push>,
90  row<in_quote, is<'\\'>, in_quote_bsl, &self::push>,
91  row<in_quote, is_any, in_quote, &self::push>,
92 
93  row<in_quote_bsl, is_any, in_quote, &self::push>,
94 
95  row<no_comment_bsl, is_any, no_comment, &self::push>
97 };
98 
99 /**
100  * Declare an io filter based on FSM for filtering comments.
101  */
103 
104 
105 } // namespace Input
106 
107 
108 #endif /* COMMENT_FILTER_HH_ */
Input::uncommenting_fsm::star_in_comment
static const int star_in_comment
Definition: comment_filter.hh:54
boost::iostreams
Definition: finite_state_filter.hpp:34
Input
Abstract linear system class.
Definition: balance.hh:40
Input::uncommenting_fsm::BOOST_IOSTREAMS_FSM
BOOST_IOSTREAMS_FSM(uncommenting_fsm) inline void slash_and_push(char ch)
Definition: comment_filter.hh:42
Input::uncommenting_fsm::in_quote_bsl
static const int in_quote_bsl
Definition: comment_filter.hh:57
Input::uncommenting_fsm::one_line_comment
static const int one_line_comment
Definition: comment_filter.hh:52
Input::uncommenting_filter
io::finite_state_filter< uncommenting_fsm > uncommenting_filter
Definition: comment_filter.hh:102
boost::iostreams::finite_state_machine_base::initial_state
static const int initial_state
Definition: finite_state_filter.hpp:40
Input::uncommenting_fsm::transition_table
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
Definition: comment_filter.hh:96
Input::uncommenting_fsm
Definition: comment_filter.hh:41
boost::iostreams::finite_state_machine< uncommenting_fsm >::push
void push(char c)
Definition: finite_state_filter.hpp:126
boost::iostreams::finite_state_machine
Definition: finite_state_filter.hpp:95
boost::iostreams::finite_state_filter
Definition: finite_state_filter.hpp:272
Input::uncommenting_fsm::no_comment
static const int no_comment
Definition: comment_filter.hh:50
Input::uncommenting_fsm::multi_line_comment
static const int multi_line_comment
Definition: comment_filter.hh:53
Input::uncommenting_fsm::one_slash
static const int one_slash
Definition: comment_filter.hh:51
boost::iostreams::finite_state_machine< uncommenting_fsm >::skip
void skip(char_type)
Definition: finite_state_filter.hpp:144
Input::uncommenting_fsm::in_quote
static const int in_quote
Definition: comment_filter.hh:56
Input::uncommenting_fsm::no_comment_bsl
static const int no_comment_bsl
Definition: comment_filter.hh:55
finite_state_filter.hpp