python: improve handling of formulas

* src/misc/escape.hh, src/misc/escape.cc (escape_latex): New function.
* src/ltlvisit/tostring.cc: Escape atomic proposition in LaTeX output.
* wrap/python/spot.py: Make it easy to output formulas in different
syntaxes.  Also allow the AST to be shown.
* wrap/python/spot_impl.i: Catch std::runtime_error.
* wrap/python/tests/formulas.ipynb: New file.
* wrap/python/tests/Makefile.am: Add it.
This commit is contained in:
Alexandre Duret-Lutz 2015-03-11 19:41:24 +01:00
parent a6dbf5cf5e
commit 2362b9ab68
7 changed files with 667 additions and 35 deletions

View file

@ -30,7 +30,7 @@
#include "lunabbrev.hh"
#include "wmunabbrev.hh"
#include "tostring.hh"
#include "misc/escape.hh"
namespace spot
{
@ -416,7 +416,9 @@ namespace spot
{
// Spin 6 supports atomic propositions such as (a == 0)
// as long as they are enclosed in parentheses.
if (kw_ != spin_kw)
if (kw_ == sclatex_kw || kw_ == sclatex_kw)
escape_latex(os_ << "``\\mathit{", str) << "\\textrm{''}}";
else if (kw_ != spin_kw)
os_ << '"' << str << '"';
else if (!full_parent_)
os_ << '(' << str << ')';
@ -431,11 +433,11 @@ namespace spot
while (str[s - 1] >= '0' && str[s - 1] <= '9')
{
--s;
assert(s != 0); // bare words cannot start with letters
assert(s != 0); // bare words cannot start with digits
}
if (s > 1)
os_ << "\\mathit{";
os_ << str.substr(0, s);
escape_latex(os_, str.substr(0, s));
if (s > 1)
os_ << '}';
if (s != str.size())

View file

@ -1,5 +1,5 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2012, 2013 Laboratoire de Recherche et Developpement de
// Copyright (C) 2012, 2013, 2015 Laboratoire de Recherche et Developpement de
// l'Epita (LRDE)
// Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
@ -34,14 +34,44 @@ namespace spot
std::ostream&
escape_rfc4180(std::ostream& os, const std::string& str)
{
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
switch (*i)
for (auto i: str)
switch (i)
{
case '"':
os << "\"\"";
break;
default:
os << *i;
os << i;
break;
}
return os;
}
std::ostream&
escape_latex(std::ostream& os, const std::string& str)
{
for (auto i: str)
switch (i)
{
case '~':
os << "\\textasciitilde";
break;
case '^':
os << "\\textasciicircum";
break;
case '\\':
os << "\\textbackslash";
break;
case '&':
case '%':
case '$':
case '#':
case '_':
case '{':
case '}':
os << '\\';
default:
os << i;
break;
}
return os;
@ -50,8 +80,8 @@ namespace spot
std::ostream&
escape_str(std::ostream& os, const std::string& str)
{
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
switch (*i)
for (auto i: str)
switch (i)
{
case '\\':
os << "\\\\";
@ -63,7 +93,7 @@ namespace spot
os << "\\n";
break;
default:
os << *i;
os << i;
break;
}
return os;

View file

@ -1,5 +1,5 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2011, 2012, 2013 Laboratoire de Recherche et
// Copyright (C) 2011, 2012, 2013, 2015 Laboratoire de Recherche et
// Developpement de l'Epita (LRDE).
// Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
@ -39,6 +39,13 @@ namespace spot
SPOT_API std::ostream&
escape_rfc4180(std::ostream& os, const std::string& str);
/// \brief Escape special LaTeX characters.
///
/// The following characters are rewritten:
/// <code>& % $ # _ { } ~ ^ \\</code>
SPOT_API std::ostream&
escape_latex(std::ostream& os, const std::string& str);
/// \brief Escape characters <code>"</code>, <code>\\</code>, and
/// <code>\\n</code> in \a str.
SPOT_API std::ostream&