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"
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(
157 (
std::
string, m_name )
158 (
std::
string, m_value )
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_(
" " ) ) >>
'"';
196 comment %= qi::lexeme[ qi::char_(
"/" ) >> qi::char_(
"/" ) >> *qi::char_(
"a-zA-Z_0-9!\"#$%&'()*,:;<>?@\\^`{|}~/ .@=[]ยง!+-" ) ];
203 object.name(
"object" );
204 kvpair.name(
"key-value pair" );
206 value.name(
"value" );
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;
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 >
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 >
475 return *getValues< T >( key, std::vector< T >( 1, defaultValue ) ).begin();
478 template<
typename T >
481 std::vector< T > r = getValues< T >( key );
492 template<
typename T >
495 std::vector< T > r = getValues< T >( key );
502 throw WNotFound(
"The key \"" + key +
"\" was not found." );
528 template<
typename T >
557 template<
typename T >
588 template<
typename T >
591 return element.m_name;
595 template<
typename T >
599 std::vector< ObjectType > rObj;
600 std::vector< KeyValueType > 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.
qi::rule< Iterator, ValueType() > value
Value rule.
Indicates invalid value which could not be found.
size_t count(std::string key, bool valuesOnly=false) const
It is possible that there are multiple values matching a key.
std::vector< T > getValues(std::string key, const std::vector< T > &defaults) const
Queries the list of values matching the given path.
StructuredValueTree getSubTree(std::string key) const
Gets a subtree.
bool exists(std::string key, bool valuesOnly=false) const
Checks whether the given value or object exists.
FileType m_file
The named values.
std::string operator()(const T &element) const
Returns the m_name member of the specified object or key-valuev pair.
T operator[](std::string key) const
Queries the value with the given name.
StructuredValueTree()
Creates an empty tree.
std::string KeyType
we use these quite often, so define some short alias for them:
Visitor to identify whether the given variant of type WStructuredTextParser::MemberType is a object o...
std::string ValueType
The type used for values.
namespace WStructuredTextParser { /** The grammar describing the structured format.
qi::rule< Iterator, KeyValueType(), ascii::space_type > kvpair
Key-value pair rule.
This namespace contains the WStructuredTextParser data types and the parser.
void traverse(MemberType current, std::vector< std::string >::const_iterator keyIter, std::vector< std::string >::const_iterator keyEnd, std::vector< ObjectType > &resultObjects, std::vector< KeyValueType > &resultValues) const
Recursively fills a result vector using a given path iterator.
qi::rule< Iterator, ObjectType(), ascii::space_type > object
Rule for objects.
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.
static const std::string Separator
This char is used as separator for identifying values in the tree.
qi::rule< Iterator, FileType(), ascii::space_type > file
Rule for files.
Grammar(std::ostream &error)
Constructor and grammar description.
bool operator()(const T &) const
Returns always false as it is only called for objects.
KeyValueType - a tuple containing name and value.
An object is always a name and contains several further nodes.
std::string operator()(const CommentType &) const
Comments have no name.
Visitor to query the m_name member of WStructuredTextParser::ObjectType and WStructuredTextParser::Ke...
std::vector< StructuredValueTree > getSubTrees(std::string key) const
Gets all matching subtrees.
boost::variant< boost::recursive_wrapper< ObjectType >, KeyValueType, CommentType > MemberType
A node inside the AST is either another object or a key-value pair.
This simplifies working with a tree in a WStructuredTextParser::FileType instance.
virtual ~StructuredValueTree()
Cleanup.
T getValue(std::string key, const T &defaultValue) const
Queries the value with the given name.
Test parsing and query functionality.
bool operator()(const KeyValueType &) const
Returns always true as it is only called for key-value pairs.
std::string m_name
Name of the object.
qi::rule< Iterator, KeyType() > key
Key rule.
qi::rule< Iterator, CommentType(), ascii::space_type > comment
Rule for comments.