From 763074102bc76cb84344929c46f8854e61bc526c Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 7 Aug 2008 23:40:09 +0200 Subject: [PATCH 1/1] Test variants. * tests/c++.at (AT_CHECK_VARIANTS): New. Use it with and without %define assert. --- ChangeLog | 6 ++ tests/c++.at | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 159 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 757e1fd0..d7921b14 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-11-10 Akim Demaille + + Test variants. + * tests/c++.at (AT_CHECK_VARIANTS): New. + Use it with and without %define assert. + 2008-11-10 Akim Demaille Add %precedence support. diff --git a/tests/c++.at b/tests/c++.at index 3e742ee2..d351d41b 100644 --- a/tests/c++.at +++ b/tests/c++.at @@ -1,5 +1,5 @@ # Checking the output filenames. -*- Autotest -*- -# Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc. +# Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,6 +17,155 @@ AT_BANNER([[C++ Features.]]) +## ---------- ## +## Variants. ## +## ---------- ## + +# AT_CHECK_VARIANTS([DIRECTIVES]) +# ------------------------------- +# Check the support of variants in C++, with the additional DIRECTIVES. +m4_define([AT_CHECK_VARIANTS], +[AT_SETUP([Variants $1]) + +# Store strings and integers in a list of strings. +AT_DATA([list.yy], +[[%debug +%skeleton "lalr1.cc" +%defines +%define variant + +%code requires // code for the .hh file +{ +#include +#include +typedef std::list strings_type; +} + +%code // code for the .cc file +{ +#include +#include +#include +#include + + // Prototype of the yylex function providing subsequent tokens. + static yy::parser::token_type yylex(yy::parser::semantic_type* yylval); + + // Printing a list of strings. + // Koening look up will look into std, since that's an std::list. + namespace std + { + std::ostream& + operator<<(std::ostream& o, const strings_type& s) + { + std::copy(s.begin(), s.end(), + std::ostream_iterator(o, "\n")); + return o; + } + } + + // Conversion to string. + template + inline + std::string + string_cast (const T& t) + { + std::ostringstream o; + o << t; + return o.str(); + } +} + +%token TEXT; +%token NUMBER; +%printer { debug_stream() << $][$; } ; +%token END_OF_FILE 0; + +%type item; +%type list result; + +%% + +result: + list { std::cout << $][1; } +; + +list: + /* nothing */ { /* Generates an empty string list */ } +| list item { std::swap($][$,$][1); $$.push_back($][2); } +; + +item: + TEXT { std::swap($][$,$][1); } +| NUMBER { $][$ = string_cast($][1); } +; +%% + +static +yy::parser::token_type +yylex(yy::parser::semantic_type* yylval) +{ + static int stage = 0; + yy::parser::token_type result; + + switch (stage) + { + case 0: + yylval->build() = std::string("BEGIN"); + result = yy::parser::token::TEXT; + break; + case 1: + case 2: + case 3: + yylval->build() = stage; + result = yy::parser::token::NUMBER; + break; + case 4: + yylval->build() = std::string("END"); + result = yy::parser::token::TEXT; + break; + default: + result = yy::parser::token::END_OF_FILE; + break; + } + + ++stage; + return result; +} + +// Mandatory error function +void +yy::parser::error(const yy::parser::location_type& yylloc, + const std::string& message) +{ + std::cerr << yylloc << ": " << message << std::endl; +} + +int main(int argc, char *argv[]) +{ + yy::parser p; + p.set_debug_level(!!getenv("YYDEBUG")); + return p.parse(); +} +]]) + +AT_BISON_CHECK([-o list.cc list.yy], 0) +AT_COMPILE_CXX([list]) +AT_CHECK([./list], 0, +[BEGIN +1 +2 +3 +END +]) + +AT_CLEANUP +]) + +AT_CHECK_VARIANTS([]) +AT_CHECK_VARIANTS([%define assert]) + + ## ----------------------- ## ## Doxygen Documentation. ## ## ----------------------- ## @@ -100,6 +249,9 @@ m4_popdef([AT_DOXYGEN_PRIVATE]) AT_CHECK_DOXYGEN([Public]) AT_CHECK_DOXYGEN([Private]) + + + ## ------------ ## ## Namespaces. ## ## ------------ ## -- 2.45.2