Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52952 - in trunk/libs/spirit: example/lex test test/lex
From: hartmut.kaiser_at_[hidden]
Date: 2009-05-12 22:11:54


Author: hkaiser
Date: 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
New Revision: 52952
URL: http://svn.boost.org/trac/boost/changeset/52952

Log:
Spirit: More lexer regression tests
Added:
   trunk/libs/spirit/test/lex/matlib.h (contents, props changed)
   trunk/libs/spirit/test/lex/regression002.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/lex/word_count.cpp | 2
   trunk/libs/spirit/test/CMakeLists.txt | 41 +++++++++++++++++++++------------------
   trunk/libs/spirit/test/Jamfile | 1
   trunk/libs/spirit/test/lex/regression001.cpp | 7 +++++
   4 files changed, 30 insertions(+), 21 deletions(-)

Modified: trunk/libs/spirit/example/lex/word_count.cpp
==============================================================================
--- trunk/libs/spirit/example/lex/word_count.cpp (original)
+++ trunk/libs/spirit/example/lex/word_count.cpp 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
@@ -111,7 +111,7 @@
         using boost::phoenix::size;
 
         start = *( tok.word [++ref(w), ref(c) += size(_1)]
- | char_('\n') [++ref(c), ++ref(l)]
+ | lit('\n') [++ref(c), ++ref(l)]
                   | token(IDANY) [++ref(c)]
                   )
               ;

Modified: trunk/libs/spirit/test/CMakeLists.txt
==============================================================================
--- trunk/libs/spirit/test/CMakeLists.txt (original)
+++ trunk/libs/spirit/test/CMakeLists.txt 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
@@ -58,39 +58,39 @@
 # boost_test_compile_fail(qi_rule_fail qi/rule_fail.cpp COMPILE_FLAGS ${test_compile_flags})
 
 # run Karma tests
-boost_test_run(karma_actions karma/actions.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_actions ]
-boost_test_run(karma_and_attribute karma/and_attribute.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_actions ]
-boost_test_run(karma_alternative karma/alternative.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_alternative ]
-boost_test_run(karma_binary karma/binary.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_binary ]
+boost_test_run(karma_actions karma/actions.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_and_attribute karma/and_attribute.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_alternative karma/alternative.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_binary karma/binary.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_case_handling karma/case_handling.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_center_alignment karma/center_alignment.cpp COMPILE_FLAGS ${test_compile_flags})
-boost_test_run(karma_char karma/char.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_char ]
+boost_test_run(karma_char karma/char.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_delimiter karma/delimiter.cpp COMPILE_FLAGS ${test_compile_flags})
-boost_test_run(karma_eol karma/eol.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_eol ]
-boost_test_run(karma_eps karma/eps.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_eps ]
+boost_test_run(karma_eol karma/eol.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_eps karma/eps.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_format_manip karma/format_manip.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_format_manip_attr karma/format_manip_attr.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_generate_attr karma/generate_attr.cpp COMPILE_FLAGS ${test_compile_flags})
-boost_test_run(karma_grammar karma/grammar.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_grammar ]
+boost_test_run(karma_grammar karma/grammar.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_int_numerics karma/int_numerics.cpp COMPILE_FLAGS ${test_compile_flags})
-boost_test_run(karma_kleene karma/kleene.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_kleene ]
-boost_test_run(karma_lazy karma/lazy.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_lazy ]
+boost_test_run(karma_kleene karma/kleene.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_lazy karma/lazy.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_left_alignment karma/left_alignment.cpp COMPILE_FLAGS ${test_compile_flags})
-boost_test_run(karma_list karma/list.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_list ]
-boost_test_run(karma_lit karma/lit.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_lit ]
-boost_test_run(karma_maxwidth karma/maxwidth.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_lit ]
-boost_test_run(karma_optional karma/optional.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_optional ]
+boost_test_run(karma_list karma/list.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_lit karma/lit.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_maxwidth karma/maxwidth.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_optional karma/optional.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_pattern karma/pattern.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_pattern2 karma/pattern2.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_plus karma/plus.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_real_numerics karma/real_numerics.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(karma_right_alignment karma/right_alignment.cpp COMPILE_FLAGS ${test_compile_flags})
-boost_test_run(karma_sequence karma/sequence.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_sequence ]
-boost_test_run(karma_stream karma/stream.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_sequence ]
-boost_test_run(karma_tricky_alignment karma/tricky_alignment.cpp COMPILE_FLAGS ${test_compile_flags}) # : : : : karma_sequence ]
+boost_test_run(karma_sequence karma/sequence.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_stream karma/stream.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(karma_tricky_alignment karma/tricky_alignment.cpp COMPILE_FLAGS ${test_compile_flags})
 
-boost_test_compile_fail(karma_grammar_fail karma/grammar_fail.cpp COMPILE_FLAGS ${test_compile_flags}) # : : karma_grammar_fail ]
-boost_test_compile_fail(karma_rule_fail karma/rule_fail.cpp COMPILE_FLAGS ${test_compile_flags}) # : : karma_rule_fail ]
+boost_test_compile_fail(karma_grammar_fail karma/grammar_fail.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_compile_fail(karma_rule_fail karma/rule_fail.cpp COMPILE_FLAGS ${test_compile_flags})
 
 # run lexer tests
 boost_test_run(lex_lexertl1 lex/lexertl1.cpp COMPILE_FLAGS ${test_compile_flags})
@@ -100,6 +100,9 @@
 boost_test_run(lex_lexertl5 lex/lexertl5.cpp COMPILE_FLAGS ${test_compile_flags})
 boost_test_run(lex_state_switcher_test lex/state_switcher_test.cpp COMPILE_FLAGS ${test_compile_flags})
 
+boost_test_run(lex_regression001 lex/lex_regression001.cpp COMPILE_FLAGS ${test_compile_flags})
+boost_test_run(lex_regression002 lex/lex_regression002.cpp COMPILE_FLAGS ${test_compile_flags})
+
 # run support tests
 # boost_test_run(support_hold_any support/hold_any.cpp COMPILE_FLAGS ${test_compile_flags})
 # the multi_pass tests are not completed yet

Modified: trunk/libs/spirit/test/Jamfile
==============================================================================
--- trunk/libs/spirit/test/Jamfile (original)
+++ trunk/libs/spirit/test/Jamfile 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
@@ -107,6 +107,7 @@
     [ run lex/state_switcher_test.cpp : : : : ]
 
     [ run lex/regression001.cpp : : : : lex_regression001 ]
+ [ run lex/regression002.cpp : : : : lex_regression002 ]
 
     ;
 

Added: trunk/libs/spirit/test/lex/matlib.h
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/test/lex/matlib.h 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
@@ -0,0 +1,91 @@
+/*
+ * matlib.h
+ * a2
+ *
+ * Created by Carl Barron on 5/10/09.
+ * Copyright 2009 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#ifndef MATLIB_H_05102009
+#define MATLIB_H_05102009
+#include <boost/spirit/include/lex.hpp>
+#include <vector>
+#include <string>
+
+struct set_lexer_state
+{
+ std::string state;
+ set_lexer_state(const std::string &a):state(a){}
+ template <class Range,class Context>
+ void operator () (Range const &,std::size_t,bool &,Context &ctx) const
+ {
+ ctx.set_state_name(state.c_str());
+ }
+};
+
+struct store_double
+{
+ std::vector<double> &out;
+ store_double(std::vector<double> &a):out(a){}
+ template <class Range,class LexerContext>
+ void operator () (Range const & r,std::size_t,bool &,LexerContext &)const
+ {
+ std::string work(r.begin(),r.end());
+ out.push_back(std::atof(work.c_str()));
+ }
+};
+
+struct add_row
+{
+ std::vector<std::vector<double> > &matrix;
+ std::vector<double> &row;
+
+ add_row(std::vector<std::vector<double> > &a,std::vector<double> &b)
+ :matrix(a),row(b) {}
+ template <class Range,class Context>
+ void operator () (Range const &,std::size_t ,bool &,Context &ctx) const
+ {
+ matrix.push_back(std::vector<double>());
+ matrix.back().swap(row);
+ ctx.set_state_name("A");
+ }
+};
+
+template <class Lexer>
+struct matlib_tokens:boost::spirit::lex::lexer<Lexer>
+{
+ matlib_tokens(std::vector<std::vector<double> > &a)
+ : matrix(a)
+ {
+ typedef boost::spirit::lex::token_def<> token_def_;
+
+ this->self.add_pattern("REAL1", "[0-9]+(\\.[0-9]*)?");
+ this->self.add_pattern("REAL2", "\\.[0-9]+");
+
+ number = "[-+]?({REAL1}|{REAL2})([eE][-+]?[0-9]+)?";
+
+ this->self
+ = token_def_('[') [set_lexer_state("A")]
+ ;
+
+ this->self("A")
+ = token_def_('[') [set_lexer_state("B")]
+ | ','
+ | token_def_(']') [ set_lexer_state("INITIAL")]
+ ;
+
+ this->self("B")
+ = number [store_double(row)]
+ | ','
+ | token_def_(']') [add_row(matrix,row)]
+ ;
+ }
+
+ boost::spirit::lex::token_def<> number;
+ std::vector<std::vector<double> > &matrix;
+ std::vector<double> row;
+};
+
+#endif
+

Modified: trunk/libs/spirit/test/lex/regression001.cpp
==============================================================================
--- trunk/libs/spirit/test/lex/regression001.cpp (original)
+++ trunk/libs/spirit/test/lex/regression001.cpp 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
@@ -1,4 +1,9 @@
+// Copyright (c) 2001-2009 Hartmut Kaiser
+//
+// 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/spirit/include/lex_lexertl.hpp>
 
 #include <iostream>
@@ -53,6 +58,6 @@
         i != position_helper_lexer.end(); i++ )
     {
     }
- return 0;
+ return boost::report_errors();
 }
 

Added: trunk/libs/spirit/test/lex/regression002.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/test/lex/regression002.cpp 2009-05-12 22:11:53 EDT (Tue, 12 May 2009)
@@ -0,0 +1,42 @@
+// Copyright (c) 2001-2009 Hartmut Kaiser
+//
+// 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/spirit/include/lex_lexertl.hpp>
+#include <iostream>
+#include <algorithm>
+#include "matlib.h"
+
+void test_matrix(std::vector<std::vector<double> > const& x)
+{
+ BOOST_TEST(x.size() == 3);
+ BOOST_TEST(x[0].size() == 2 && x[0][0] == 1 && x[0][1] == 2);
+ BOOST_TEST(x[1].size() == 1 && x[1][0] == 3);
+ BOOST_TEST(x[2].size() == 3 && x[2][0] == 4 && x[2][1] == 5 && x[2][2] == 6);
+}
+
+int main ()
+{
+ std::string input("[[1,2][3][4,5,6]]");
+
+ std::vector<std::vector<double> > results;
+ typedef std::string::iterator iter;
+ typedef boost::spirit::lex::lexertl::actor_lexer<
+ boost::spirit::lex::lexertl::token<iter> > lexer_type;
+
+ typedef matlib_tokens<lexer_type> matlib_type;
+ matlib_type matrix(results);
+ iter first = input.begin();
+
+ try {
+ BOOST_TEST(boost::spirit::lex::tokenize(first, input.end(), matrix));
+ test_matrix(results);
+ }
+ catch (std::runtime_error const& e) {
+ std::cerr << "caught exception: " << e.what() << std::endl;
+ return -1;
+ }
+ return boost::report_errors();
+}


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