tools: Add a --format option

* src/bin/common_output.cc: Add option --format and implement
it.
* src/bin/ltlfilt.cc, src/bin/randltl.cc: Document the
supported %-sequences.
* src/bin/genltl.cc: Document the %-sequences, and supply
the name of the pattern to output_formula().
* doc/org/genltl.org, doc/org/ioltl.org, doc/org/ltlfilt.org,
NEWS: Document it.
* src/ltltest/latex.test: Use it.
This commit is contained in:
Alexandre Duret-Lutz 2013-09-22 22:02:10 +02:00
parent 983feb5290
commit ce5ea829bd
9 changed files with 297 additions and 77 deletions

View file

@ -20,14 +20,17 @@
#include "common_sys.hh"
#include "common_output.hh"
#include <iostream>
#include <sstream>
#include "ltlvisit/tostring.hh"
#include "ltlvisit/lbt.hh"
#include "misc/formater.hh"
#include "common_cout.hh"
#include "error.h"
#define OPT_SPOT 1
#define OPT_WRING 2
#define OPT_LATEX 3
#define OPT_FORMAT 4
output_format_t output_format = spot_output;
bool full_parenth = false;
@ -42,13 +45,126 @@ static const argp_option options[] =
{ "wring", OPT_WRING, 0, 0, "output in Wring's syntax", -20 },
{ "utf8", '8', 0, 0, "output using UTF-8 characters", -20 },
{ "latex", OPT_LATEX, 0, 0, "output using LaTeX macros", -20 },
{ "format", OPT_FORMAT, "FORMAT", 0,
"specify how each line should be output (default: \"%f\")", -20 },
{ 0, 0, 0, 0, 0, 0 }
};
const struct argp output_argp = { options, parse_opt_output, 0, 0, 0, 0, 0 };
static
void
report_not_ltl(const spot::ltl::formula* f,
const char* filename, int linenum, const char* syn)
{
std::string s = spot::ltl::to_string(f);
static const char msg[] =
"formula '%s' cannot be written %s's syntax because it is not LTL";
if (filename)
error_at_line(2, 0, filename, linenum, msg, s.c_str(), syn);
else
error(2, 0, msg, s.c_str(), syn);
}
static void
stream_formula(std::ostream& out,
const spot::ltl::formula* f, const char* filename, int linenum)
{
switch (output_format)
{
case lbt_output:
if (f->is_ltl_formula())
spot::ltl::to_lbt_string(f, out);
else
report_not_ltl(f, filename, linenum, "LBT");
break;
case spot_output:
spot::ltl::to_string(f, out, full_parenth);
break;
case spin_output:
if (f->is_ltl_formula())
spot::ltl::to_spin_string(f, out, full_parenth);
else
report_not_ltl(f, filename, linenum, "Spin");
break;
case wring_output:
if (f->is_ltl_formula())
spot::ltl::to_wring_string(f, out);
else
report_not_ltl(f, filename, linenum, "Wring");
break;
case utf8_output:
spot::ltl::to_utf8_string(f, out, full_parenth);
break;
case latex_output:
spot::ltl::to_latex_string(f, out, full_parenth);
break;
}
}
namespace
{
struct formula_with_location
{
const spot::ltl::formula* f;
const char* filename;
int line;
};
class printable_formula:
public spot::printable_value<const formula_with_location*>
{
public:
printable_formula&
operator=(const formula_with_location* new_val)
{
val_ = new_val;
return *this;
}
virtual void
print(std::ostream& os, const char*) const
{
stream_formula(os,
val_->f,
val_->filename,
val_->line);
}
};
class formula_printer: protected spot::formater
{
public:
formula_printer(std::ostream& os, const char* format)
: format_(format)
{
declare('f', &fl_);
declare('F', &filename_);
declare('L', &line_);
set_output(os);
}
std::ostream&
print(const formula_with_location& fl)
{
fl_ = &fl;
filename_ = fl.filename ? fl.filename : "";
line_ = fl.line;
return format(format_);
}
private:
const char* format_;
printable_formula fl_;
spot::printable_value<const char*> filename_;
spot::printable_value<int> line_;
};
}
static formula_printer* format = 0;
int
parse_opt_output(int key, char*, struct argp_state*)
parse_opt_output(int key, char* arg, struct argp_state*)
{
// This switch is alphabetically-ordered.
switch (key)
@ -74,60 +190,30 @@ parse_opt_output(int key, char*, struct argp_state*)
case OPT_WRING:
output_format = wring_output;
break;
case OPT_FORMAT:
delete format;
format = new formula_printer(std::cout, arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static
void
report_not_ltl(const spot::ltl::formula* f,
const char* filename, int linenum, const char* syn)
{
std::string s = spot::ltl::to_string(f);
static const char msg[] =
"formula '%s' cannot be written %s's syntax because it is not LTL";
if (filename)
error_at_line(2, 0, filename, linenum, msg, s.c_str(), syn);
else
error(2, 0, msg, s.c_str(), syn);
}
void
output_formula(const spot::ltl::formula* f, const char* filename, int linenum)
{
switch (output_format)
if (!format)
{
case lbt_output:
if (f->is_ltl_formula())
spot::ltl::to_lbt_string(f, std::cout);
else
report_not_ltl(f, filename, linenum, "LBT");
break;
case spot_output:
spot::ltl::to_string(f, std::cout, full_parenth);
break;
case spin_output:
if (f->is_ltl_formula())
spot::ltl::to_spin_string(f, std::cout, full_parenth);
else
report_not_ltl(f, filename, linenum, "Spin");
break;
case wring_output:
if (f->is_ltl_formula())
spot::ltl::to_wring_string(f, std::cout);
else
report_not_ltl(f, filename, linenum, "Wring");
break;
case utf8_output:
spot::ltl::to_utf8_string(f, std::cout, full_parenth);
break;
case latex_output:
spot::ltl::to_latex_string(f, std::cout, full_parenth);
break;
stream_formula(std::cout, f, filename, linenum);
}
else
{
formula_with_location fl = { f, filename, linenum };
format->print(fl);
}
// Make sure we abort if we can't write to std::cout anymore
// (like disk full or broken pipe with SIGPIPE ignored).
std::cout << std::endl;

View file

@ -96,26 +96,52 @@ using namespace spot::ltl;
const char argp_program_doc[] ="\
Generate temporal logic formulas from predefined scalable patterns.";
#define OPT_AND_F 2
#define OPT_AND_FG 3
#define OPT_AND_GF 4
#define OPT_CCJ_ALPHA 5
#define OPT_CCJ_BETA 6
#define OPT_CCJ_BETA_PRIME 7
#define OPT_GH_Q 8
#define OPT_GH_R 9
#define OPT_GO_THETA 10
#define OPT_OR_FG 11
#define OPT_OR_G 12
#define OPT_OR_GF 13
#define OPT_R_LEFT 14
#define OPT_R_RIGHT 15
#define OPT_RV_COUNTER 16
#define OPT_RV_COUNTER_CARRY 17
#define OPT_RV_COUNTER_CARRY_LINEAR 18
#define OPT_RV_COUNTER_LINEAR 19
#define OPT_U_LEFT 20
#define OPT_U_RIGHT 21
#define OPT_AND_F 1
#define OPT_AND_FG 2
#define OPT_AND_GF 3
#define OPT_CCJ_ALPHA 4
#define OPT_CCJ_BETA 5
#define OPT_CCJ_BETA_PRIME 6
#define OPT_GH_Q 7
#define OPT_GH_R 8
#define OPT_GO_THETA 9
#define OPT_OR_FG 10
#define OPT_OR_G 11
#define OPT_OR_GF 12
#define OPT_R_LEFT 13
#define OPT_R_RIGHT 14
#define OPT_RV_COUNTER 15
#define OPT_RV_COUNTER_CARRY 16
#define OPT_RV_COUNTER_CARRY_LINEAR 17
#define OPT_RV_COUNTER_LINEAR 18
#define OPT_U_LEFT 19
#define OPT_U_RIGHT 20
#define LAST_CLASS 20
const char* const class_name[LAST_CLASS] =
{
"and-f",
"and-fg",
"and-gf",
"ccj-alpha",
"ccj-beta",
"ccj-beta-prime",
"gh-q",
"gh-r",
"go-theta",
"or-fg",
"or-g",
"or-gf",
"or-r-left",
"or-r-right",
"rv-counter",
"rv-counter-carry",
"rv-counter-carry-linear",
"rv-counter-linear",
"u-left",
"u-right",
};
#define OPT_ALIAS(o) { #o, 0, 0, OPTION_ALIAS, 0, 0 }
@ -168,6 +194,16 @@ static const argp_option options[] =
RANGE_DOC,
/**************************************************/
{ 0, 0, 0, 0, "Output options:", -20 },
{ 0, 0, 0, 0, "The FORMAT string passed to --format may use "\
"the following interpreted sequences:", -19 },
{ "%f", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the formula (in the selected syntax)", 0 },
{ "%F", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the name of the pattern", 0 },
{ "%L", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the argument of the pattern", 0 },
{ "%%", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"a single %", 0 },
{ 0, 0, 0, 0, "Miscellaneous options:", -1 },
{ 0, 0, 0, 0, 0, 0 }
};
@ -810,7 +846,7 @@ output_pattern(int pattern, int n)
f = r;
}
output_formula(f);
output_formula(f, class_name[pattern - 1], n);
f->destroy();
}

View file

@ -153,6 +153,16 @@ static const argp_option options[] =
"drop formulas that have already been output (not affected by -v)", 0 },
/**************************************************/
{ 0, 0, 0, 0, "Output options:", -20 },
{ 0, 0, 0, 0, "The FORMAT string passed to --format may use "\
"the following interpreted sequences:", -19 },
{ "%f", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the formula (in the selected syntax)", 0 },
{ "%F", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the name of the input file", 0 },
{ "%L", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the original line number in the input file", 0 },
{ "%%", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"a single %", 0 },
{ 0, 0, 0, 0, "Miscellaneous options:", -1 },
{ 0, 0, 0, 0, 0, 0 }
};

View file

@ -1,6 +1,6 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2012 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
// Copyright (C) 2012, 2013 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
@ -107,6 +107,14 @@ static const argp_option options[] =
"listed by --dump-priorities.", 0 },
/**************************************************/
{ 0, 0, 0, 0, "Output options:", -20 },
{ 0, 0, 0, 0, "The FORMAT string passed to --format may use "\
"the following interpreted sequences:", -19 },
{ "%f", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the formula (in the selected syntax)", 0 },
{ "%L", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"the (serial) number of the formula", 0 },
{ "%%", 0, 0, OPTION_DOC | OPTION_NO_USAGE,
"a single %", 0 },
{ 0, 0, 0, 0, "Miscellaneous options:", -1 },
{ 0, 0, 0, 0, 0, 0 }
};
@ -387,7 +395,8 @@ main(int argc, char** argv)
if (trials == 0)
error(2, 0, "failed to generate a new unique formula after %d trials",
MAX_TRIALS);
output_formula(f);
static int count = 0;
output_formula(f, 0, ++count);
f->destroy();
};