Commit 8fb7b279 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

ltlvisit: rename tostring.hh as print.hh and rename printer functions

This actually performs three related changes, but separating them
would be quite inconvenient.

1) rename tostring.hh to print.hh a welcome side-effect is that
I could fix several files that included this file for not reason.

2) de-overload some of the to_string functions, and rename them
as follow:

  to_string -> print_psl, print_sere, str_psl, str_sere
  to_utf8_string -> print_utf8_psl, print_utf8_sere,
                    str_utf8_psl, str_utf8_sere
  to_spin_string -> print_spin_ltl, str_spin_ltl
  to_wring_string -> print_wring_ltl, str_wing_ltl
  to_lbt_string -> print_lbt_ltl, str_lbt_ltl
  to_latex_string -> print_latex_psl, str_latex_psl
  to_sclatex_string -> print_sclatex_psl, str_sclatex_psl

Now it is clearer what these functions do, and their restrictions.

3) all those print_* functions now take the stream to write onto
as their first argument.  This fixes #88.

* src/ltlvisit/tostring.cc, src/ltlvisit/tostring.hh: Rename into...
* src/ltlvisit/print.cc, src/ltlvisit/print.hh: ... those, and make
the changes listed above.
* doc/org/tut01.org, src/bin/common_output.cc,
src/bin/common_trans.cc, src/bin/ltl2tgba.cc, src/bin/ltl2tgta.cc,
src/bin/ltlcross.cc, src/bin/ltldo.cc, src/bin/ltlfilt.cc,
src/bin/randltl.cc, src/ltlparse/ltlparse.yy,
src/ltlvisit/Makefile.am, src/ltlvisit/mark.cc,
src/ltlvisit/relabel.cc, src/ltlvisit/simplify.cc,
src/ltlvisit/snf.cc, src/ta/taexplicit.cc, src/ta/tgtaexplicit.cc,
src/taalgos/tgba2ta.cc, src/tests/equalsf.cc, src/tests/ltl2tgba.cc,
src/tests/ltlrel.cc, src/tests/randtgba.cc, src/tests/reduc.cc,
src/tests/syntimpl.cc, src/tests/tostring.cc, src/twa/bdddict.cc,
src/twa/bddprint.cc, src/twa/taatgba.cc, src/twa/taatgba.hh,
src/twa/twagraph.cc, src/twaalgos/compsusp.cc, src/twaalgos/lbtt.cc,
src/twaalgos/ltl2taa.cc, src/twaalgos/ltl2tgba_fm.cc,
src/twaalgos/neverclaim.cc, src/twaalgos/remprop.cc,
src/twaalgos/stats.cc, wrap/python/ajax/spot.in, wrap/python/spot.py,
wrap/python/spot_impl.i: Adjust.
parent 0cf952e7
......@@ -65,14 +65,15 @@ exceptions.
#+BEGIN_SRC C++ :results verbatim :exports both
#include <iostream>
#include "ltlparse/public.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
int main()
{
const spot::ltl::formula* f = spot::ltl::parse_formula("[]<>p0 || <>[]p1");
to_lbt_string(f, std::cout) << '\n';
to_spin_string(f, std::cout, true) << '\n';
to_latex_string(spot::ltl::parse_formula("& & G p0 p1 p2"), std::cout);
print_lbt_ltl(std::cout, f) << '\n';
print_spin_ltl(std::cout, f, true) << '\n';
print_latex_psl(std::cout, spot::ltl::parse_formula("& & G p0 p1 p2"));
f->destroy();
}
#+END_SRC
......@@ -81,6 +82,19 @@ exceptions.
: ([](<>(p0))) || (<>([](p1)))
: p_{1} \land p_{2} \land \G p_{0}
Notice that the different output routines specify in their name the
syntax the output, and the type of formula they expect. Here we are
only using LTL formulas for demonstration, so those three functions
are OK with that (LTL is a subset of PSL as far as Spot is concerned).
However if we input PSL formula, it's clear that the above code will
be a problem. If you want to add additional checks, you can easily
tests whether =f->is_ltl_formula()= returns true.
Did you notice the calls to =f->destroy()= at the end? The LTL
formula objects are implemented as DAG with sharing of subformulas.
Each (sub)formula is therefore reference counted, and currently this
is done manually by calling =f->clone()= and =f->destroy()= (do not
ever =delete= a formula, always call =f->destroy()=).
We do not recommend using this =parse_formula()= interface because of
the potential formulas (like =f= or =t=) that have different meanings
......@@ -96,7 +110,7 @@ parser. Additionally, this give you control over how to print errors.
#include <string>
#include <iostream>
#include "ltlparse/public.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
int main()
{
......@@ -109,8 +123,8 @@ parser. Additionally, this give you control over how to print errors.
f->destroy();
return 1;
}
to_lbt_string(f, std::cout) << '\n';
to_spin_string(f, std::cout, true) << '\n';
print_lbt_ltl(std::cout, f) << '\n';
print_spin_ltl(std::cout, f, true) << '\n';
f->destroy();
}
#+END_SRC
......@@ -144,7 +158,7 @@ with the "fixed" formula if you wish. Here is an example:
#include <string>
#include <iostream>
#include "ltlparse/public.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
int main()
{
......@@ -156,8 +170,8 @@ with the "fixed" formula if you wish. Here is an example:
(void) spot::ltl::format_parse_errors(std::cout, input, pel);
if (f == nullptr)
return 1;
to_lbt_string(f, std::cout) << '\n';
to_spin_string(f, std::cout, true) << '\n';
print_lbt_ltl(std::cout, f) << '\n';
print_spin_ltl(std::cout, f, true) << '\n';
f->destroy();
}
#+END_SRC
......@@ -180,13 +194,6 @@ U "a" "b"
The formula =f= is only returned as null when the parser really cannot
recover anything.
Did you notice the calls to =f->destroy()= in these two examples? The
LTL formula objects are implemented as DAG with sharing of
subformulas. Each (sub)formula is therefore reference counted, and
currently this is done manually by calling =f->clone()= and
=f->destroy()= (do not ever =delete= a formula, always call
=f->destroy()=).
** Calling the prefix parser explicitly
The only difference here is the call to =parse_prefix_ltl()= instead of
......@@ -196,7 +203,7 @@ The only difference here is the call to =parse_prefix_ltl()= instead of
#include <string>
#include <iostream>
#include "ltlparse/public.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
int main()
{
......@@ -209,7 +216,7 @@ The only difference here is the call to =parse_prefix_ltl()= instead of
f->destroy();
return 1;
}
to_latex_string(f, std::cout) << '\n';
print_latex_psl(std::cout, f) << '\n';
f->destroy();
}
#+END_SRC
......
......@@ -21,7 +21,7 @@
#include "common_output.hh"
#include <iostream>
#include <sstream>
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
#include "misc/formater.hh"
#include "misc/escape.hh"
#include "common_cout.hh"
......@@ -71,7 +71,7 @@ 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);
std::string s = spot::ltl::str_psl(f);
static const char msg[] =
"formula '%s' cannot be written %s's syntax because it is not LTL";
if (filename)
......@@ -88,30 +88,30 @@ stream_formula(std::ostream& out,
{
case lbt_output:
if (f->is_ltl_formula())
spot::ltl::to_lbt_string(f, out);
spot::ltl::print_lbt_ltl(out, f);
else
report_not_ltl(f, filename, linenum, "LBT");
break;
case spot_output:
spot::ltl::to_string(f, out, full_parenth);
spot::ltl::print_psl(out, f, full_parenth);
break;
case spin_output:
if (f->is_ltl_formula())
spot::ltl::to_spin_string(f, out, full_parenth);
spot::ltl::print_spin_ltl(out, f, 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);
spot::ltl::print_wring_ltl(out, f);
else
report_not_ltl(f, filename, linenum, "Wring");
break;
case utf8_output:
spot::ltl::to_utf8_string(f, out, full_parenth);
spot::ltl::print_utf8_psl(out, f, full_parenth);
break;
case latex_output:
spot::ltl::to_latex_string(f, out, full_parenth);
spot::ltl::print_latex_psl(out, f, full_parenth);
break;
case count_output:
case quiet_output:
......
......@@ -28,7 +28,7 @@
#include "error.h"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
#include "common_conv.hh"
// A set of tools for which we know the correct output
......@@ -288,13 +288,13 @@ void
translator_runner::round_formula(const spot::ltl::formula* f, unsigned serial)
{
if (has('f') || has('F'))
string_ltl_spot = spot::ltl::to_string(f, true);
string_ltl_spot = spot::ltl::str_psl(f, true);
if (has('s') || has('S'))
string_ltl_spin = spot::ltl::to_spin_string(f, true);
string_ltl_spin = spot::ltl::str_spin_ltl(f, true);
if (has('l') || has('L'))
string_ltl_lbt = spot::ltl::to_lbt_string(f);
string_ltl_lbt = spot::ltl::str_lbt_ltl(f);
if (has('w') || has('W'))
string_ltl_wring = spot::ltl::to_wring_string(f);
string_ltl_wring = spot::ltl::str_wring_ltl(f);
if (has('F'))
string_to_tmp(string_ltl_spot, serial, filename_ltl_spot);
if (has('S'))
......
......@@ -34,7 +34,7 @@
#include "common_post.hh"
#include "ltlast/formula.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
#include "twaalgos/translate.hh"
#include "misc/optionmap.hh"
#include "misc/timer.hh"
......@@ -147,7 +147,7 @@ namespace
// future-proof.
if (!f->is_psl_formula())
{
std::string s = spot::ltl::to_string(f);
std::string s = spot::ltl::str_psl(f);
error_at_line(2, 0, filename, linenum,
"formula '%s' is not an LTL or PSL formula",
s.c_str());
......
......@@ -33,7 +33,7 @@
#include "common_post.hh"
#include "ltlparse/public.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
#include "ltlvisit/simplify.hh"
#include "twaalgos/dotty.hh"
#include "twaalgos/ltl2tgba_fm.hh"
......@@ -181,7 +181,7 @@ namespace
// future-proof.
if (!f->is_psl_formula())
{
std::string s = spot::ltl::to_string(f);
std::string s = spot::ltl::str_psl(f);
error_at_line(2, 0, filename, linenum,
"formula '%s' is not an LTL or PSL formula",
s.c_str());
......
......@@ -40,7 +40,7 @@
#include "dstarparse/public.hh"
#include "hoaparse/public.hh"
#include "ltlast/unop.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
#include "ltlvisit/apcollect.hh"
#include "ltlvisit/mutation.hh"
#include "ltlvisit/relabel.hh"
......@@ -971,9 +971,9 @@ namespace
if (res)
{
if (lbt_input)
bogus = spot::ltl::to_lbt_string(f);
bogus = spot::ltl::str_lbt_ltl(f);
else
bogus = spot::ltl::to_string(f);
bogus = spot::ltl::str_psl(f);
if (bogus_output)
bogus_output->ostream() << bogus << std::endl;
}
......
......@@ -35,7 +35,6 @@
#include "common_post.hh"
#include "common_trans.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/relabel.hh"
#include "misc/bareword.hh"
#include "misc/timer.hh"
......
......@@ -42,7 +42,7 @@
#include "ltlvisit/remove_x.hh"
#include "ltlvisit/apcollect.hh"
#include "ltlvisit/exclusive.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
#include "ltlast/unop.hh"
#include "ltlast/multop.hh"
#include "twaalgos/ltl2tgba_fm.hh"
......@@ -648,7 +648,7 @@ namespace
// Sort the formulas alphabetically.
std::map<std::string, const spot::ltl::formula*> m;
for (auto& p: relmap)
m.emplace(to_string(p.first), p.second);
m.emplace(str_psl(p.first), p.second);
for (auto& p: m)
stream_formula(output_define->ostream()
<< "#define " << p.first << " (",
......
......@@ -36,7 +36,6 @@
#include "ltlast/multop.hh"
#include "ltlast/unop.hh"
#include "ltlvisit/randomltl.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/length.hh"
#include "ltlvisit/simplify.hh"
#include "ltlenv/defaultenv.hh"
......
......@@ -36,7 +36,7 @@
#include <sstream>
#include "public.hh"
#include "ltlast/allnodes.hh"
#include "ltlvisit/tostring.hh"
#include "ltlvisit/print.hh"
struct minmax_t { unsigned min, max; };
}
......@@ -240,8 +240,8 @@ using namespace spot::ltl;
%destructor { $$->destroy(); } <ltl>
%printer { debug_stream() << *$$; } <str>
%printer { spot::ltl::to_string($$, debug_stream()); } <ltl>
%printer { spot::ltl::to_string($$, debug_stream(), false, true); } sere bracedsere
%printer { spot::ltl::print_psl(debug_stream(), $$); } <ltl>
%printer { spot::ltl::print_sere(debug_stream(), $$); } sere bracedsere
%printer { debug_stream() << $$; } <num>
%printer { debug_stream() << $$.min << ".." << $$.max; } <minmax>
......
......@@ -36,6 +36,7 @@ ltlvisit_HEADERS = \
lunabbrev.hh \
mutation.hh \
nenoform.hh \
print.hh \
postfix.hh \
randomltl.hh \
relabel.hh \
......@@ -43,7 +44,6 @@ ltlvisit_HEADERS = \
simpfg.hh \
simplify.hh \
snf.hh \
tostring.hh \
tunabbrev.hh \
wmunabbrev.hh
......@@ -61,6 +61,7 @@ libltlvisit_la_SOURCES = \
mark.hh \
mutation.cc \
nenoform.cc \
print.cc \
postfix.cc \
randomltl.cc \
relabel.cc \
......@@ -68,6 +69,5 @@ libltlvisit_la_SOURCES = \
simpfg.cc \
simplify.cc \
snf.cc \
tostring.cc \
tunabbrev.cc \
wmunabbrev.cc
// -*- coding: utf-8 -*-
// Copyright (C) 2010, 2012, 2014 Laboratoire de Recherche et
// Copyright (C) 2010, 2012, 2014, 2015 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
......@@ -23,7 +23,6 @@
#include <algorithm>
#include <set>
#include <vector>
#include "ltlvisit/tostring.hh"
#include "misc/casts.hh"
namespace spot
......
......@@ -29,7 +29,7 @@
#include "ltlast/visitor.hh"
#include "lunabbrev.hh"
#include "wmunabbrev.hh"
#include "tostring.hh"
#include "print.hh"
#include "misc/escape.hh"
namespace spot
......@@ -912,44 +912,78 @@ namespace spot
const char** kw_;
};
std::ostream&
printer_(std::ostream& os, const formula* f, bool full_parent,
bool ratexp, const char** kw)
{
to_string_visitor v(os, full_parent, ratexp, kw);
f->accept(v);
return os;
}
std::string
str_(const formula* f, bool full_parent, bool ratexp, const char** kw)
{
std::ostringstream os;
printer_(os, f, full_parent, ratexp, kw);
return os.str();
}
} // anonymous
std::ostream&
to_string(const formula* f, std::ostream& os, bool full_parent,
bool ratexp)
print_psl(std::ostream& os, const formula* f, bool full_parent)
{
to_string_visitor v(os, full_parent, ratexp, spot_kw);
f->accept(v);
return os;
return printer_(os, f, full_parent, false, spot_kw);
}
std::string
to_string(const formula* f, bool full_parent, bool ratexp)
str_psl(const formula* f, bool full_parent)
{
std::ostringstream os;
to_string(f, os, full_parent, ratexp);
return os.str();
return str_(f, full_parent, false, spot_kw);
}
std::ostream&
to_utf8_string(const formula* f, std::ostream& os, bool full_parent,
bool ratexp)
print_sere(std::ostream& os, const formula* f, bool full_parent)
{
to_string_visitor v(os, full_parent, ratexp, utf8_kw);
f->accept(v);
return os;
return printer_(os, f, full_parent, true, spot_kw);
}
std::string
to_utf8_string(const formula* f, bool full_parent, bool ratexp)
str_sere(const formula* f, bool full_parent)
{
std::ostringstream os;
to_utf8_string(f, os, full_parent, ratexp);
return os.str();
return str_(f, full_parent, false, spot_kw);
}
std::ostream&
print_utf8_psl(std::ostream& os, const formula* f, bool full_parent)
{
return printer_(os, f, full_parent, false, utf8_kw);
}
std::string
str_utf8_psl(const formula* f, bool full_parent)
{
return str_(f, full_parent, false, utf8_kw);
}
std::ostream&
print_utf8_sere(std::ostream& os, const formula* f, bool full_parent)
{
return printer_(os, f, full_parent, true, utf8_kw);
}
std::string
str_utf8_sere(const formula* f, bool full_parent)
{
return str_(f, full_parent, false, utf8_kw);
}
std::ostream&
to_spin_string(const formula* f, std::ostream& os, bool full_parent)
print_spin_ltl(std::ostream& os, const formula* f, bool full_parent)
{
// Remove xor, ->, and <-> first.
const formula* fu = unabbreviate_logic(f);
......@@ -963,15 +997,15 @@ namespace spot
}
std::string
to_spin_string(const formula* f, bool full_parent)
str_spin_ltl(const formula* f, bool full_parent)
{
std::ostringstream os;
to_spin_string(f, os, full_parent);
print_spin_ltl(os, f, full_parent);
return os.str();
}
std::ostream&
to_wring_string(const formula* f, std::ostream& os)
print_wring_ltl(std::ostream& os, const formula* f)
{
// Remove W and M.
f = unabbreviate_wm(f);
......@@ -982,47 +1016,60 @@ namespace spot
}
std::string
to_wring_string(const formula* f)
str_wring_ltl(const formula* f)
{
std::ostringstream os;
to_wring_string(f, os);
print_wring_ltl(os, f);
return os.str();
}
std::ostream&
to_latex_string(const formula* f, std::ostream& os,
bool full_parent, bool ratexp)
print_latex_psl(std::ostream& os, const formula* f, bool full_parent)
{
to_string_visitor v(os, full_parent, ratexp, latex_kw);
f->accept(v);
return os;
return printer_(os, f, full_parent, false, latex_kw);
}
std::string
to_latex_string(const formula* f,
bool full_parent, bool ratexp)
str_latex_psl(const formula* f, bool full_parent)
{
std::ostringstream os;
to_latex_string(f, os, full_parent, ratexp);
return os.str();
return str_(f, full_parent, false, latex_kw);
}
std::ostream&
to_sclatex_string(const formula* f, std::ostream& os,
bool full_parent, bool ratexp)
print_latex_sere(std::ostream& os, const formula* f, bool full_parent)
{
to_string_visitor v(os, full_parent, ratexp, sclatex_kw);
f->accept(v);
return os;
return printer_(os, f, full_parent, true, latex_kw);
}
std::string
to_sclatex_string(const formula* f,
bool full_parent, bool ratexp)
str_latex_sere(const formula* f, bool full_parent)
{
std::ostringstream os;
to_sclatex_string(f, os, full_parent, ratexp);
return os.str();
return str_(f, full_parent, true, latex_kw);
}
std::ostream&
print_sclatex_psl(std::ostream& os, const formula* f, bool full_parent)
{
return printer_(os, f, full_parent, false, sclatex_kw);
}
std::string
str_sclatex_psl(const formula* f, bool full_parent)
{
return str_(f, full_parent, false, sclatex_kw);
}
std::ostream&
print_sclatex_sere(std::ostream& os, const formula* f, bool full_parent)
{
return printer_(os, f, full_parent, true, sclatex_kw);
}
std::string
str_sclatex_sere(const formula* f, bool full_parent)
{
return str_(f, full_parent, true, sclatex_kw);
}
namespace
......@@ -1199,7 +1246,7 @@ namespace spot
} // anonymous
std::ostream&
to_lbt_string(const formula* f, std::ostream& os)
print_lbt_ltl(std::ostream& os, const formula* f)
{
assert(f->is_ltl_formula());
lbt_visitor v(os);
......@@ -1208,10 +1255,10 @@ namespace spot
}
std::string
to_lbt_string(const formula* f)
str_lbt_ltl(const formula* f)
{
std::ostringstream os;
to_lbt_string(f, os);
print_lbt_ltl(os, f);
return os.str();
}
......
......@@ -32,117 +32,168 @@ namespace spot
/// \addtogroup ltl_io
/// @{
/// \brief Output a formula as a string which is parsable unless the formula
/// contains automaton operators (used in ELTL formulae).
/// \brief Output a PSL formula as a string which is parsable.
/// \param os The stream where it should be output.
/// \param f The formula to translate.
/// \param full_parent Whether or not the string should by fully
/// parenthesized.
SPOT_API std::ostream&
print_psl(std::ostream& os, const formula* f, bool full_parent = false);
/// \brief Convert a PSL formula into a string which is parsable
/// \param f The formula to translate.
/// \param full_parent Whether or not the string should by fully
/// parenthesized.
SPOT_API std::string
str_psl(const formula* f, bool full_parent = false);
/// \brief Output a PSL formula as an utf-8 string which is parsable.
/// \param os The stream where it should be output.
/// \param f The formula to translate.
/// \param full_parent Whether or not the string should by fully
/// parenthesized.
/// \param ratexp Whether we are printing a SERE.
SPOT_API std::ostream&
to_string(const formula* f, std::ostream& os, bool full_parent = false,
bool ratexp = false);
print_utf8_psl(std::ostream& os, const formula* f,
bool full_parent = false);
/// \brief Output a formula as a string which is parsable unless the formula
/// contains automaton operators (used in ELTL formulae).
/// \brief Convert a PSL formula into a utf-8 string which is parsable
/// \param f The formula to translate.
/// \param full_parent Whether or not the string should by fully
/// parenthesized.
/// \param ratexp Whether we are printing a SERE.
SPOT_API std::string
to_string(const formula* f, bool full_parent = false, bool ratexp = false);