$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r57278 - in trunk/libs/spirit/test: . lex
From: hartmut.kaiser_at_[hidden]
Date: 2009-11-01 12:51:02
Author: hkaiser
Date: 2009-11-01 12:51:02 EST (Sun, 01 Nov 2009)
New Revision: 57278
URL: http://svn.boost.org/trac/boost/changeset/57278
Log:
Spirit: added new lexer test
Added:
   trunk/libs/spirit/test/lex/regression005.cpp   (contents, props changed)
Text files modified: 
   trunk/libs/spirit/test/Jamfile |     1 +                                       
   1 files changed, 1 insertions(+), 0 deletions(-)
Modified: trunk/libs/spirit/test/Jamfile
==============================================================================
--- trunk/libs/spirit/test/Jamfile	(original)
+++ trunk/libs/spirit/test/Jamfile	2009-11-01 12:51:02 EST (Sun, 01 Nov 2009)
@@ -128,6 +128,7 @@
     [ run lex/regression003_generate.cpp    : $(LEX_DIR)/matlib_static.h : : : lex_regression003_generate ]
     [ run lex/regression003.cpp             : : : <dependency>.//lex_regression003_generate : lex_regression003 ]
     [ run lex/regression004.cpp             : : : : lex_regression004 ]
+    [ run lex/regression005.cpp             : : : : lex_regression005 ]
 
     ;
 
Added: trunk/libs/spirit/test/lex/regression005.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/test/lex/regression005.cpp	2009-11-01 12:51:02 EST (Sun, 01 Nov 2009)
@@ -0,0 +1,92 @@
+//  Copyright (c) 2001-2009 Hartmut Kaiser
+//  Copyright (c) 2009 Jean-Francois Ostiguy
+//
+//  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/detail/lightweight_test.hpp>
+#include <boost/config/warning_disable.hpp>
+
+#include <boost/spirit/include/lex_lexertl.hpp>
+#include <boost/spirit/include/qi.hpp>
+
+#include <string>
+#include <iostream>
+#include <sstream>
+
+namespace lex = boost::spirit::lex;
+namespace qi = boost::spirit::qi;
+namespace mpl = boost::mpl;
+
+template <typename Lexer>
+struct my_lexer : lex::lexer<Lexer>
+{
+    my_lexer() 
+    {
+        delimiter = "BEGIN|END";
+        identifier = "[a-zA-Z][_\\.a-zA-Z0-9]*";
+        ws = "[ \\t\\n]+";
+        real = "([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)|([-+]?[1-9]+\\.?([eE][-+]?[0-9]+))";
+        integer = "[0-9]+"; 
+
+        this->self += ws[lex::_pass = lex::pass_flags::pass_ignore];
+        this->self += delimiter;
+        this->self += identifier;
+        this->self += real;
+        this->self += integer;
+        this->self += '=';
+        this->self += ';';
+    }
+
+    lex::token_def<> ws;
+    lex::token_def<std::string> identifier;
+    lex::token_def<int> integer;
+    lex::token_def<double> real;
+    lex::token_def<> delimiter;
+};
+
+template <typename Iterator>
+struct my_grammar : qi::grammar<Iterator> 
+{
+    template <typename TokenDef>
+    my_grammar( TokenDef const& tok )
+      :  my_grammar::base_type(statement) 
+    {
+        statement 
+            =   qi::eoi
+            |  *(delimiter | declaration)
+            ;
+
+        delimiter = tok.delimiter >> tok.identifier;
+        declaration = tok.identifier >> option >> ';';
+        option = *(tok.identifier >> '=' >> (tok.real | tok.integer));
+    }
+
+    qi::rule<Iterator> statement, delimiter, declaration, option;
+};
+
+typedef lex::lexertl::token<char const*
+  , mpl::vector<std::string, double, int> > token_type;
+typedef lex::lexertl::actor_lexer<token_type> lexer_type;
+typedef my_lexer<lexer_type>::iterator_type iterator_type;
+
+int main(int argc, char* argv[])
+{
+    std::string test_string ("BEGIN section\n");
+    // we introduce a syntax error: ";;" instead of ";" as a terminator.
+    test_string += "Identity;;\n";      // this will make the parser fail 
+    test_string += "END section\n" ;
+
+    char const* first = &test_string[0];
+    char const* last  = &first[test_string.size()];
+
+    my_lexer<lexer_type> lexer;
+    my_grammar<iterator_type> grammar(lexer);
+
+    BOOST_TEST(!lex::tokenize_and_parse(first, last, lexer, grammar));
+    BOOST_TEST(first != last);
+
+    return boost::report_errors();
+}
+
+