Click here to Skip to main content
15,885,161 members
Articles / Programming Languages / C++

Opening a door towards Spirit: a parser framework

Rate me:
Please Sign up or sign in to vote.
4.73/5 (44 votes)
23 Mar 20037 min read 200.8K   942   69  
A quick introduction to Spirit, a parser generator framework based
#pragma once

#include <boost/spirit/core.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <boost/spirit/utility/confix.hpp>
#include <iostream>
#include <vector>
#include <map>

namespace myparser
{
	using namespace std;
	using namespace boost::spirit;

    struct assign_string
    {
        assign_string(std::string& str_)
        : str(str_) {};

		template<typename IteratorT >
        void operator()(IteratorT first, IteratorT last) const
        {
            str.assign(first, last);
        }

        std::string& str;
    };

	struct keyvalue_grammar : public grammar<keyvalue_grammar>
	{
		keyvalue_grammar(std::string& str_key_, std::string& str_val_)
			: str_key(str_key_), str_val(str_val_){};

        template <typename ScannerT>
        struct definition
        {	
            definition(keyvalue_grammar const& self)  
			{ 
				equal = ch_p('=');

				key_value = key >> equal >> value;

				key_tag = ch_p('-') | ch_p('/');

				key = key_tag >> 
					(+alnum_p)[ assign_string(self.str_key) ];

				value = ( 
						confix_p( 
							'"',
							(+ c_escape_ch_p )[assign_string(self.str_val)] ,
							'"'
							)
					| (+ alnum_p )[assign_string(self.str_val)] );
			}

            rule<ScannerT>  key_tag, key, equal, value, key_value;
            rule<ScannerT> const& start() const { return key_value; };
		};

		std::string& str_key;
		std::string& str_val;
	};


	template<typename keyvalue_container> 
    class add_keyvalue_pair
    {
	public:
		add_keyvalue_pair( keyvalue_container& kvc_, std::string& key_, std::string& val_)
			: kvc( kvc_ ), key(key_), val(val_)
		{
		}

        template <typename IteratorT>
        void operator()(IteratorT first, IteratorT last) const
		{
			kvc.insert( keyvalue_container::value_type(key, val) );
		}
	private:
		std::string& key;
		std::string& val;
		keyvalue_container& kvc;
    };

	template<typename keyvalue_container> 
    struct cmdline_grammar : public grammar< cmdline_grammar >
    {
		cmdline_grammar( keyvalue_container& kvc_, std::string& str_command_)
			: kvc(kvc_), str_command(str_command_)
		{};

        template <typename ScannerT>
        struct definition
        {
            definition( cmdline_grammar<keyvalue_container> const& self )
				: key_value( key, value )
			{ 
				command = (+alnum_p)[assign_string(self.str_command)];
				line = command >> *( key_value[ add_keyvalue_pair<keyvalue_container>( self.kvc, key, value ) ] );
			}			

            rule<ScannerT> command, line;
			keyvalue_grammar key_value;
			std::string key;
			std::string value;
            rule<ScannerT> const& start() const { return line; }
        };

		keyvalue_container& kvc;
		std::string& str_command;
    };
};

class command_line_parser
{
public:
	typedef std::map< std::string, std::string> keyvalue_container;
	command_line_parser(){};
	~command_line_parser(){};

	bool parse(const std::string& str)
	{
		kvc.clear();

		myparser::cmdline_grammar<keyvalue_container> parser(kvc, command);
		info = boost::spirit::parse(str.c_str(), parser, boost::spirit::space_p);

		return info.full;
	};

	const char* stop() const	{	return info.stop;};

	const string& get_command() const				{	return command;};
	const keyvalue_container& get_keyvalues()const	{	return kvc;};

protected:
	std::string command;
	keyvalue_container kvc;
	parse_info<> info;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions