25 #ifndef WSTRUCTUREDTEXTPARSER_H
26 #define WSTRUCTUREDTEXTPARSER_H
35 #include <boost/config/warning_disable.hpp>
36 #include <boost/spirit/include/qi.hpp>
37 #include <boost/spirit/include/phoenix_core.hpp>
38 #include <boost/spirit/include/phoenix_operator.hpp>
39 #include <boost/spirit/include/phoenix_fusion.hpp>
40 #include <boost/spirit/include/phoenix_stl.hpp>
41 #include <boost/spirit/include/phoenix_object.hpp>
42 #include <boost/fusion/include/adapt_struct.hpp>
43 #include <boost/variant/recursive_variant.hpp>
44 #include <boost/fusion/include/io.hpp>
45 #include <boost/filesystem/path.hpp>
47 #include "WStringUtils.h"
48 #include "exceptions/WTypeMismatch.h"
49 #include "exceptions/WNotFound.h"
55 namespace WStructuredTextParser
58 namespace qi = boost::spirit::qi;
61 namespace fusion = boost::fusion;
64 namespace ascii = boost::spirit::ascii;
67 namespace phoenix = boost::phoenix;
70 namespace spirit = boost::spirit;
112 boost::recursive_wrapper< ObjectType >,
146 BOOST_FUSION_ADAPT_STRUCT(
148 ( std::string, m_name )
149 ( std::vector< WStructuredTextParser::MemberType >, m_nodes )
155 BOOST_FUSION_ADAPT_STRUCT(
156 WStructuredTextParser::KeyValueType,
157 ( std::
string, m_name )
158 ( std::
string, m_value )
162 namespace WStructuredTextParser
178 template <
typename Iterator>
179 struct Grammar: qi::grammar<Iterator, FileType(), ascii::space_type >
186 explicit Grammar( std::ostream& error ): Grammar::base_type( file,
"WStructuredTextParser::Grammar" )
189 key %= qi::char_(
"a-zA-Z_" ) >> *qi::char_(
"a-zA-Z_0-9" );
191 value %=
'"' >> *( ~qi::char_(
"\"" ) | qi::char_(
" " ) ) >>
'"';
194 kvpair %= key >>
'=' >> value >>
';';
196 comment %= qi::lexeme[ qi::char_(
"/" ) >> qi::char_(
"/" ) >> *qi::char_(
"a-zA-Z_0-9!\"#$%&'()*,:;<>?@\\^`{|}~/ .@=[]ยง!+-" ) ];
198 object %= ( key | value ) >>
'{' >> *(
object | kvpair | comment ) >>
'}' >> *qi::char_(
";" );
200 file %= *(
object | kvpair | comment );
203 object.name(
"object" );
204 kvpair.name(
"key-value pair" );
206 value.name(
"value" );
208 comment.name(
"comment" );
212 qi::on_error< qi::fail >( object, error << phoenix::val(
"Error: " ) << qi::_4 );
213 qi::on_error< qi::fail >( kvpair, error << phoenix::val(
"Error: " ) << qi::_4 );
214 qi::on_error< qi::fail >( key, error << phoenix::val(
"Error: " ) << qi::_4 );
215 qi::on_error< qi::fail >( value, error << phoenix::val(
"Error: " ) << qi::_4 );
216 qi::on_error< qi::fail >( comment, error << phoenix::val(
"Error: " ) << qi::_4 );
217 qi::on_error< qi::fail >( file, error << phoenix::val(
"Error: " ) << qi::_4 );
225 qi::rule< Iterator, ObjectType(), ascii::space_type > object;
230 qi::rule< Iterator, FileType(), ascii::space_type > file;
235 qi::rule< Iterator, CommentType(), ascii::space_type > comment;
240 qi::rule< Iterator, KeyValueType(), ascii::space_type > kvpair;
245 qi::rule< Iterator, KeyType() > key;
250 qi::rule< Iterator, ValueType() > value;
267 class StructuredValueTree
274 static const std::string Separator;
281 explicit StructuredValueTree(
const FileType& file );
288 explicit StructuredValueTree(
const std::string& toParse );
295 explicit StructuredValueTree(
const boost::filesystem::path& file );
300 StructuredValueTree();
305 virtual ~StructuredValueTree();
317 bool exists( std::string key,
bool valuesOnly =
false )
const;
327 size_t count( std::string key,
bool valuesOnly =
false )
const;
342 template<
typename T >
343 T getValue( std::string key,
const T& defaultValue )
const;
358 template<
typename T >
359 std::vector< T > getValues( std::string key,
const std::vector< T >& defaults )
const;
373 template<
typename T >
374 std::vector< T > getValues( std::string key )
const;
387 template<
typename T >
388 T operator[]( std::string key )
const;
399 StructuredValueTree getSubTree( std::string key )
const;
410 std::vector< StructuredValueTree > getSubTrees( std::string key )
const;
429 void traverse( MemberType current, std::vector< std::string >::const_iterator keyIter,
430 std::vector< std::string >::const_iterator keyEnd,
431 std::vector< ObjectType >& resultObjects,
432 std::vector< KeyValueType >& resultValues )
const;
443 void traverse( FileType current, std::string key,
444 std::vector< ObjectType >& resultObjects,
445 std::vector< KeyValueType >& resultValues )
const;
457 FileType parseFromString( std::string input );
469 FileType parseFromFile( boost::filesystem::path path );
471 template<
typename T >
472 T StructuredValueTree::getValue( std::string key,
const T& defaultValue )
const
475 return *getValues< T >( key, std::vector< T >( 1, defaultValue ) ).begin();
478 template<
typename T >
479 std::vector< T > StructuredValueTree::getValues( std::string key,
const std::vector< T >& defaults )
const
481 std::vector< T > r = getValues< T >( key );
492 template<
typename T >
493 T StructuredValueTree::operator[]( std::string key )
const
495 std::vector< T > r = getValues< T >( key );
502 throw WNotFound(
"The key \"" + key +
"\" was not found." );
509 class IsLeafVisitor:
public boost::static_visitor< bool >
517 bool operator()(
const KeyValueType& )
const
528 template<
typename T >
529 bool operator()(
const T& )
const
538 class IsCommentVisitor:
public boost::static_visitor< bool >
546 bool operator()(
const CommentType& )
const
557 template<
typename T >
558 bool operator()(
const T& )
const
567 class NameQueryVisitor:
public boost::static_visitor< std::string >
575 std::string operator()(
const CommentType& )
const
588 template<
typename T >
589 std::string operator()(
const T& element )
const
591 return element.m_name;
595 template<
typename T >
596 std::vector< T > StructuredValueTree::getValues( std::string key )
const
599 std::vector< ObjectType > rObj;
600 std::vector< KeyValueType > rKV;
603 traverse( m_file, key, rObj, rKV );
607 for( std::vector< KeyValueType >::const_iterator i = rKV.begin(); i != rKV.end(); ++i )
611 r.push_back( string_utils::fromString< T >( ( *i ).m_value ) );
616 throw WTypeMismatch(
"Cannot convert element \"" + key +
"\" to desired type." );
625 #endif // WSTRUCTUREDTEXTPARSER_H
std::string m_name
Name string.
std::string m_value
Value string.
Indicates invalid type of something.
Indicates invalid value which could not be found.
std::string KeyType
we use these quite often, so define some short alias for them:
std::string ValueType
The type used for values.
std::string CommentType
The type used for comments.
std::vector< MemberType > m_nodes
Object's members.
std::vector< MemberType > FileType
An object representing all objects and comments on file level.
KeyValueType - a tuple containing name and value.
An object is always a name and contains several further nodes.
boost::variant< boost::recursive_wrapper< ObjectType >, KeyValueType, CommentType > MemberType
A node inside the AST is either another object or a key-value pair.
Test parsing and query functionality.
std::string m_name
Name of the object.