Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66560 - trunk/libs/spirit/example/qi
From: hartmut.kaiser_at_[hidden]
Date: 2010-11-13 15:26:27


Author: hkaiser
Date: 2010-11-13 15:26:22 EST (Sat, 13 Nov 2010)
New Revision: 66560
URL: http://svn.boost.org/trac/boost/changeset/66560

Log:
Spirit: Adding new example
Added:
   trunk/libs/spirit/example/qi/unescaped_string.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/qi/Jamfile | 2 ++
   1 files changed, 2 insertions(+), 0 deletions(-)

Modified: trunk/libs/spirit/example/qi/Jamfile
==============================================================================
--- trunk/libs/spirit/example/qi/Jamfile (original)
+++ trunk/libs/spirit/example/qi/Jamfile 2010-11-13 15:26:22 EST (Sat, 13 Nov 2010)
@@ -37,6 +37,8 @@
 exe display_attribute_type : display_attribute_type.cpp ;
 exe adapt_template_struct : adapt_template_struct.cpp ;
 
+exe unescaped_string : unescaped_string.cpp ;
+
 exe calculator1 : calc1.cpp ;
 exe calculator2 : calc2.cpp ;
 exe calculator2_ast : calc2_ast.cpp ;

Added: trunk/libs/spirit/example/qi/unescaped_string.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/unescaped_string.cpp 2010-11-13 15:26:22 EST (Sat, 13 Nov 2010)
@@ -0,0 +1,72 @@
+// Copyright (c) 2010 Jeroen Habraken
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#include <boost/spirit/include/qi.hpp>
+
+#include <iostream>
+#include <ostream>
+#include <string>
+
+namespace client
+{
+ namespace qi = boost::spirit::qi;
+
+ template <typename InputIterator>
+ struct unescaped_string
+ : qi::grammar<InputIterator, std::string(char const*)>
+ {
+ unescaped_string()
+ : unescaped_string::base_type(unesc_str)
+ {
+ unesc_char.add("\\a", '\a')("\\b", '\b')("\\f", '\f')("\\n", '\n')
+ ("\\r", '\r')("\\t", '\t')("\\v", '\v')("\\\\", '\\')
+ ("\\\'", '\'')("\\\"", '\"')
+ ;
+
+ unesc_str = qi::lit(qi::_r1)
+ >> *(unesc_char | qi::alnum | "\\x" >> qi::hex)
+ >> qi::lit(qi::_r1)
+ ;
+ }
+
+ qi::rule<InputIterator, std::string(char const*)> unesc_str;
+ qi::symbols<char const, char const> unesc_char;
+ };
+
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ namespace qi = boost::spirit::qi;
+
+ typedef std::string::const_iterator iterator_type;
+
+ std::string parsed;
+
+ std::string str("'''string\\x20to\\x20unescape\\x3a\\x20\\n\\r\\t\\\"\\'\\x41'''");
+ char const* quote = "'''";
+
+ iterator_type iter = str.begin();
+ iterator_type end = str.end();
+
+ client::unescaped_string<iterator_type> p;
+ if (!qi::parse(iter, end, p(quote), parsed))
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------\n";
+ }
+ else
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsed: " << parsed << "\n";
+ std::cout << "-------------------------\n";
+ }
+
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk