Check-in [8eabe53b52]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:8eabe53b528f4ff7106bcb11794cee8366eaa5ad
Date: 2008-09-03 20:35:49
User: stephan
Comment:latest fixes/additions from the QBoard tree
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to parsepp.hpp

Old (fde3b53ed62d8cea) New (b7440c804d4244fc)
1 #ifndef s11n_net_PARSEPP_HPP_INCLUDED 1 #ifndef s11n_net_PARSEPP_HPP_INCLUDED
2 #define s11n_net_PARSEPP_HPP_INCLUDED 2 #define s11n_net_PARSEPP_HPP_INCLUDED
3 3
4 #include <map> 4 #include <map>
5 #include <string> 5 #include <string>
506 hidden lines
512 parse_iterator const & begin() const { return m_head; } 512 parse_iterator const & begin() const { return m_head; }
513 513
514 /** 514 /**
515 Returns the after-the-end iterator. 515 Returns the after-the-end iterator.
516 */ 516 */
517 parse_iterator const & end() const { return m_head; } | 517 //parse_iterator const & end() const { return m_head; }
518 518
519 /** 519 /**
520 Sets current position and returns 520 Sets current position and returns
521 a copy of the old position. 521 a copy of the old position.
522 */ 522 */
736 hidden lines
1259 matches in the input. 1259 matches in the input.
1260 */ 1260 */
1261 template< typename Rule, unsigned int Min, unsigned int Max = Min > 1261 template< typename Rule, unsigned int Min, unsigned int Max = Min >
1262 struct r_repeat 1262 struct r_repeat
1263 { 1263 {
> 1264 typedef r_repeat type;
1264 template <typename ClientState> 1265 template <typename ClientState>
1265 static bool matches( parser_state & in, ClientState & st ) 1266 static bool matches( parser_state & in, ClientState & st )
1266 { 1267 {
1267 assert( (Min<=Max) && "r_repeat<Min,Max>: Min>Max" ); 1268 assert( (Min<=Max) && "r_repeat<Min,Max>: Min>Max" );
1268 assert( (Min!=0) && "r_repeat<Min,Max>: Min==0" ); 1269 assert( (Min!=0) && "r_repeat<Min,Max>: Min==0" );
127 hidden lines
1396 ? (++in,true) 1397 ? (++in,true)
1397 : false; 1398 : false;
1398 } 1399 }
1399 else 1400 else
1400 { 1401 {
> 1402 int ch = *in;
1401 assert( (((CH>='a')&&(CH<='z')) 1403 assert( (((CH>='a')&&(CH<='z'))
1402 || ((CH>='A')&&(CH<='Z'))) 1404 || ((CH>='A')&&(CH<='Z')))
1403 && "r_ch_base<false,char> CH value is not alphabetic." ); 1405 && "r_ch_base<false,char> CH value is not alphabetic." );
1404 return ( (*in==CH) || (*in == ((CH>='a') ? (CH-32) : (CH+32))) ) | 1406 return ( (ch==CH) || (ch == ((CH>='a') ? (CH-32) : (CH+32))) )
1405 ? (++in,true) 1407 ? (++in,true)
1406 : false; 1408 : false;
1407 } 1409 }
1408 } 1410 }
1409 }; 1411 };
348 hidden lines
1758 col = st.col; 1760 col = st.col;
1759 } 1761 }
1760 } // namespace 1762 } // namespace
1761 1763
1762 #endif // s11n_net_PARSEPP_HPP_INCLUDED 1764 #endif // s11n_net_PARSEPP_HPP_INCLUDED

Changes to parsepp_strings.hpp

Old (23eb7bbda4c8ed05) New (af1f282d8085000b)
1 #ifndef S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED 1 #ifndef S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED
2 #define S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED 2 #define S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED
3 3
4 #define RL rule_list 4 #define RL rule_list
5 #define CL char_list 5 #define CL char_list
6 namespace Ps { 6 namespace Ps {
7 namespace strings { <
8 using namespace Ps; <
9 7
10 //! Starts reading of a string. | 8 /**
11 struct op_startstring | 9 StringsForwarder is a Concept type which exists for
| 10 demonstrating the interface required by the strings<> class.
| 11 See StringsForwarderString for a sample implementation.
| 12 */
| 13 template <typename ClientState>
| 14 struct StringsForwarder
12 { 15 {
13 template< typename State > | 16 /**
14 static void matched( parser_state &, const std::string &, State & s) | 17 Signals the start of a read-quoted-string operation.
15 { | 18 The client should normally empty any existing string buffer
16 //COUT << "start "<<CH<<"-style string\n"; | 19 at this point, to prepare for more input.
17 s = std::string(); | 20 */
18 } | 21 void start_string( ClientState & );
| 22 /**
| 23 Signals the appending of one or more chars to a parsed string.
| 24 The client should simply append m to his input buffer.
| 25 */
| 26 void append( ClientState &st, std::string const & m );
| 27 /**
| 28 Signals the end of a string parse. The client is not normally
| 29 required to do anything here, but can use this to trigger
| 30 actions which are waiting on the end of the string.
| 31 */
| 32 void end_string( ClientState & );
19 }; 33 };
20 34
21 //! Appends a char to a string. | 35 /**
22 struct op_appendchar | 36 A StringsForwarder type which uses a std::string as its State.
23 { | 37 */
24 template< typename State > | 38 struct StringsForwarderString
25 static void matched( parser_state &, const std::string & m, State & s ) |
26 { |
27 if( m.empty() ) return; |
28 s += m; |
29 //COUT << "appendchar"<<m<<". state == [" << s << "]\n"; |
30 } |
31 }; |
32 |
33 //! Appends an escaped char (unescaped) to a string. |
34 template <char CH> |
35 struct op_appendesc |
36 { 39 {
37 template< typename State > | 40 /**
38 static void matched( parser_state &, const std::string &, State & s ) | 41 Signals the start of a read-quoted-string operation.
| 42 The client should normally empty any existing string buffer
| 43 at this point.
| 44 */
| 45 inline void start_string( std::string & st )
| 46 {
| 47 st.clear();
| 48 }
| 49 /**
| 50 Signals the appending of one or more chars to a parsed string.
| 51 The client should simply append m to his input buffer.
| 52 */
| 53 inline void append( std::string &st, std::string const & m )
39 { 54 {
40 s.push_back(CH); | 55 st += m;
| 56 }
| 57 /**
| 58 Signals the end of a string parse. The client is not normally
| 59 required to do anything here, but can use this to trigger
| 60 actions which are waiting on the end of the string.
| 61 */
| 62 inline void end_string( std::string & )
| 63 {
41 } 64 }
42 }; 65 };
43 66
44 //! Ends reading of a string. <
45 struct op_endstring <
46 { <
47 template< typename State > <
48 static void matched( parser_state &, const std::string &, State & ) <
49 { <
50 //s.str = s.buf; <
51 //s.buf = ""; <
52 } <
53 }; <
54 <
55 <
56 <
57 //! Reads an escape sequence. QC == QuoteChar (either " or ') <
58 template <char QC> <
59 struct read_escchar : <
60 r_and< RL< <
61 r_ch<'\\'>, <
62 r_or< RL< r_action< r_ch<QC>, op_appendesc<QC> >, <
63 r_action< r_ch<'\\'>, op_appendesc<'\\'> >, <
64 r_action< r_ch<'t'>, op_appendesc<'\t'> >, <
65 r_action< r_ch<'b'>, op_appendesc<'\b'> >, <
66 r_action< r_ch<'n'>, op_appendesc<'\n'> >, <
67 r_action< r_ch<'r'>, op_appendesc<'\r'> >, <
68 r_action< r_ch<'v'>, op_appendesc<'\v'> > > > <
69 > > <
70 {}; <
71 <
72 <
73 <
74 //! Reads a single char or known escape sequence for a string. <
75 template <char QC> <
76 struct read_char : <
77 r_or< RL< <
78 read_escchar<QC>, r_action< r_notch<QC>, op_appendchar > <
79 > > <
80 //r_action< r_action<r_not<r_ch<QC>>,op_dump>, op_appendchar > <
81 //r_action< r_action<r_any,op_dump>, op_appendchar > <
82 //r_action< r_action<r_not<r_oneof<'x','y','n'>>,op_dump>, op_appendchar > <
83 //r_action< r_action<r_notch<QC>,op_dump>, op_appendchar > <
84 {}; <
85 <
86 /** 67 /**
87 Reads a string enclosed in QC characters. QC should be one of ' or " | 68 Rules for parsing conventional quoted strings (single- or
88 and if that character is in the string content, it must be escaped | 69 double-quoted). It supports common C-style escape sequences in
89 with a backslash character. | 70 its input. TransFunc must conform to the StringsForwarder
90 | 71 interface.
91 At the opening quote the State is cleared. Characters are |
92 appended until an unescaped quote char is found, then the State |
93 is "finalized". If the rule does not match, State.str will not |
94 be modified, but State.buf might be (containing content up until |
95 the point of the parse error). |
96 */ 72 */
97 template <char QC> | 73 template <typename TransFunc>
98 struct read_qstring : | 74 struct strings {
99 r_and< RL< | 75 //! Starts reading of a string.
100 r_action< r_ch<QC>, op_startstring >, | 76 struct op_startstring
101 r_star< read_char<QC> >, | 77 {
102 r_action< r_ch<QC>, op_endstring > | 78 template< typename State >
103 > > | 79 static void matched( parser_state &, const std::string &, State & s)
104 {}; | 80 {
| 81 //COUT << "start "<<CH<<"-style string\n";
| 82 TransFunc().start_string(s);
| 83 }
| 84 };
| 85
| 86 //! Appends a char to a string.
| 87 struct op_appendchar
| 88 {
| 89 template< typename State >
| 90 static void matched( parser_state &, const std::string & m, State & s )
| 91 {
| 92 if( m.empty() ) return; // happens on non-consuming actions
| 93 TransFunc().append(s,m);
| 94 }
| 95 };
| 96
| 97 //! Appends an escaped char (unescaped) to a string.
| 98 template <char CH>
| 99 struct op_appendesc
| 100 {
| 101 template< typename State >
| 102 static void matched( parser_state &, const std::string &, State & st )
| 103 {
| 104 std::string ch;
| 105 ch.push_back(CH);
| 106 TransFunc().append(st,ch);
| 107 }
| 108 };
| 109
| 110 //! Ends reading of a string.
| 111 struct op_endstring
| 112 {
| 113 template< typename State >
| 114 static void matched( parser_state &, const std::string &, State & st )
| 115 {
| 116 TransFunc().end_string(st);
| 117 }
| 118 };
| 119
| 120
| 121
| 122 //! Reads an escape sequence. QC == QuoteChar (either " or ')
| 123 template <char QC>
| 124 struct read_escchar :
| 125 r_and< RL<
| 126 r_ch<'\\'>,
| 127 r_or< RL< r_action< r_ch<QC>, op_appendesc<QC> >,
| 128 r_action< r_ch<'\\'>, op_appendesc<'\\'> >,
| 129 r_action< r_ch<'t'>, op_appendesc<'\t'> >,
| 130 r_action< r_ch<'b'>, op_appendesc<'\b'> >,
| 131 r_action< r_ch<'n'>, op_appendesc<'\n'> >,
| 132 r_action< r_ch<'r'>, op_appendesc<'\r'> >,
| 133 r_action< r_ch<'v'>, op_appendesc<'\v'> > > >
| 134 > >
| 135 {};
| 136
| 137
| 138
| 139 //! Reads a single char or known escape sequence for a string.
| 140 template <char QC>
| 141 struct read_char :
| 142 r_or< RL<
| 143 read_escchar<QC>, r_action< r_notch<QC>, op_appendchar >
| 144 > >
| 145 //r_action< r_action<r_not<r_ch<QC>>,op_dump>, op_appendchar >
| 146 //r_action< r_action<r_any,op_dump>, op_appendchar >
| 147 //r_action< r_action<r_not<r_oneof<'x','y','n'>>,op_dump>, op_appendchar >
| 148 //r_action< r_action<r_notch<QC>,op_dump>, op_appendchar >
| 149 {};
| 150
| 151 /**
| 152 Reads a string enclosed in QC characters. QC should be one of ' or "
| 153 and if that character is in the string content, it must be escaped
| 154 with a backslash character.
| 155
| 156 At the opening quote the State is cleared. Characters are
| 157 appended until an unescaped quote char is found, then the State
| 158 is "finalized". If the rule does not match, State.str will not
| 159 be modified, but State.buf might be (containing content up until
| 160 the point of the parse error).
| 161 */
| 162 template <char QC>
| 163 struct read_qstring :
| 164 r_and< RL<
| 165 r_action< r_ch<QC>, op_startstring >,
| 166 r_star< read_char<QC> >,
| 167 r_action< r_ch<QC>, op_endstring >
| 168 > >
| 169 {};
| 170
| 171 //! Matches a double-quoted string.
| 172 struct read_dqstring : read_qstring<'"'> {};
| 173 //! Matches a single-quoted string.
| 174 struct read_sqstring : read_qstring<'\''> {};
| 175
| 176 /**
| 177 This is the parser starting point for this parser.
| 178
| 179 Matches single- or double-quoted strings. It expects a
| 180 std::string State object, to which it writes the output
| 181 (overwriting any existing content). This routine unescapes
| 182 common C-style escape constructs when building the target
| 183 string.
| 184 */
| 185 struct start :
| 186 r_or< RL< read_dqstring, read_sqstring > >
| 187 {};
105 188
106 //! Matches a double-quoted string. | 189 }; // strings
107 struct read_dqstring : read_qstring<'"'> {}; |
108 //! Matches a single-quoted string. |
109 struct read_sqstring : read_qstring<'\''> {}; |
110 190
111 /** 191 /**
112 Matches single- or double-quoted strings. It expects a | 192 A convenience function to use the strings<> parser to parse a
113 std::string State object, to which it writes the output | 193 quoted string. It reads from src, which is expected to be
114 (overwriting any existing content). This routine unescapes | 194 string data (possibly escaped using C-style escaped) enclosed
115 common C-style escape constructs when building the target | 195 in either single or double quotes. If the parse is successful
116 string. | 196 true is returned. If it fails
117 */ | 197 to parse then false is returned.
118 struct parse_string : |
119 r_or< RL< read_dqstring, read_sqstring > > |
120 {}; |
121 198
122 /** | 199 tgt is overwritten with the unescaped content of the string
123 A convenience form of parse<parse_string>(). It reads from | 200 (minus the enclosing quotes). In the case of a parse failure,
124 src, which is expected to be string data (possibly escaped | 201 the state of tgt is undefined - it will likely hold the chars
125 using C-style escaped) enclosed in either single or double | 202 of the string up until the point where the parse failed, which
126 quotes. If the parse is successful tgt is overwritten with the | 203 might be useful in debugging the problem.
127 unescaped content of the string (minus the enclosing quotes) |
128 and true is returned. If it fails to parse then false is |
129 returned and tgt is not modified. |
130 */ 204 */
131 inline bool parse_quoted_string( std::string const & src, | 205 inline bool parse_qstring( std::string const & src, std::string & tgt )
132 std::string & tgt ) |
133 { 206 {
134 std::string ss; | 207 return parse< strings<StringsForwarderString>::start >( src, tgt );
135 return parse<parse_string>( src, ss ) | 208 }
136 ? (tgt = ss,true) |
137 : false; |
138 }; |
139 209
140 }} // namespaces | 210 } // namespaces
141 #undef RL 211 #undef RL
142 #undef CL 212 #undef CL
143 #endif // S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED 213 #endif // S11N_NET_PARSEPP_STRINGS_HPP_INCLUDED