parsetl: change the interface to return a parsed_formula

This gets the interface of all the functions parsing formula in line
with the interface of the automaton parser: both return a "parsed_*"
object (parsed_formula or parsed_automaton) that contains the said
object and its list of errors.  Doing so avoid having to declare the
parse_error_list in advance.

* spot/tl/parse.hh, spot/parsetl/parsetl.yy: Do the change.
* spot/parsetl/fmterror.cc: Adjust the error printer.
* NEWS: Document it.
* bin/common_finput.cc, bin/common_finput.hh, bin/ltlcross.cc,
bin/ltldo.cc, bin/ltlfilt.cc, doc/org/tut01.org, doc/org/tut02.org,
doc/org/tut10.org, doc/org/tut20.org, python/ajax/spotcgi.in,
python/spot/impl.i, spot/parseaut/parseaut.yy, tests/core/checkpsl.cc,
tests/core/checkta.cc, tests/core/consterm.cc, tests/core/emptchk.cc,
tests/core/equalsf.cc, tests/core/ikwiad.cc, tests/core/kind.cc,
tests/core/length.cc, tests/core/ltlprod.cc, tests/core/ltlrel.cc,
tests/core/randtgba.cc, tests/core/readltl.cc, tests/core/reduc.cc,
tests/core/safra.cc, tests/core/syntimpl.cc, tests/core/tostring.cc,
tests/ltsmin/modelcheck.cc, tests/python/alarm.py,
tests/python/interdep.py, tests/python/ltl2tgba.py,
tests/python/ltlparse.py: Adjust all uses.
This commit is contained in:
Alexandre Duret-Lutz 2016-02-17 19:39:43 +01:00
parent cf4f58c34b
commit 22f442f758
37 changed files with 359 additions and 374 deletions

View file

@ -1633,10 +1633,9 @@ nc-formula: nc-formula-or-ident
auto i = res.fcache.find(*$1);
if (i == res.fcache.end())
{
spot::parse_error_list pel;
auto f = spot::parse_infix_boolean(*$1, pel, *res.env,
debug_level(), true);
for (auto& j: pel)
auto pf = spot::parse_infix_boolean(*$1, *res.env, debug_level(),
true);
for (auto& j: pf.errors)
{
// Adjust the diagnostic to the current position.
spot::location here = @1;
@ -1647,8 +1646,9 @@ nc-formula: nc-formula-or-ident
res.h->errors.emplace_back(here, j.second);
}
bdd cond = bddfalse;
if (f)
cond = spot::formula_to_bdd(f, res.h->aut->get_dict(), res.h->aut);
if (pf.f)
cond = spot::formula_to_bdd(pf.f,
res.h->aut->get_dict(), res.h->aut);
$$ = (res.fcache[*$1] = cond).id();
}
else
@ -1814,16 +1814,15 @@ lbtt-acc: { $$ = 0U; }
}
lbtt-guard: STRING
{
spot::parse_error_list pel;
auto f = spot::parse_prefix_ltl(*$1, pel, *res.env);
if (!f || !pel.empty())
auto pf = spot::parse_prefix_ltl(*$1, *res.env);
if (!pf.f || !pf.errors.empty())
{
std::string s = "failed to parse guard: ";
s += *$1;
error(@$, s);
}
if (!pel.empty())
for (auto& j: pel)
if (!pf.errors.empty())
for (auto& j: pf.errors)
{
// Adjust the diagnostic to the current position.
spot::location here = @1;
@ -1833,13 +1832,13 @@ lbtt-guard: STRING
here.begin.column += j.first.begin.column - 1;
res.h->errors.emplace_back(here, j.second);
}
if (!f)
if (!pf.f)
{
res.cur_label = bddtrue;
}
else
{
if (!f.is_boolean())
if (!pf.f.is_boolean())
{
error(@$,
"non-Boolean transition label (replaced by true)");
@ -1848,7 +1847,7 @@ lbtt-guard: STRING
else
{
res.cur_label =
formula_to_bdd(f, res.h->aut->get_dict(), res.h->aut);
formula_to_bdd(pf.f, res.h->aut->get_dict(), res.h->aut);
}
}
delete $1;

View file

@ -72,11 +72,10 @@ namespace spot
const parse_error_list& error_list)
{
bool printed = false;
parse_error_list::const_iterator it;
for (it = error_list.begin(); it != error_list.end(); ++it)
for (auto it: error_list)
{
os << ">>> " << ltl_string << std::endl;
const location& l = it->first;
os << ">>> " << ltl_string << '\n';
const location& l = it.first;
unsigned n = 1;
for (; n < 4 + l.begin.column; ++n)
@ -86,7 +85,7 @@ namespace spot
++n;
for (; n < 4 + l.end.column; ++n)
os << '^';
os << std::endl << it->second << std::endl << std::endl;
os << '\n' << it.second << "\n\n";
printed = true;
}
return printed;
@ -94,19 +93,17 @@ namespace spot
}
bool
format_parse_errors(std::ostream& os,
const std::string& ltl_string,
const parse_error_list& error_list)
parsed_formula::format_errors(std::ostream& os)
{
if (utf8::is_valid(ltl_string.begin(), ltl_string.end()))
if (utf8::is_valid(input.begin(), input.end()))
{
parse_error_list fixed = error_list;
fix_utf8_locations(ltl_string, fixed);
return format_parse_errors_aux(os, ltl_string, fixed);
parse_error_list fixed = errors;
fix_utf8_locations(input, fixed);
return format_parse_errors_aux(os, input, fixed);
}
else
{
return format_parse_errors_aux(os, ltl_string, error_list);
return format_parse_errors_aux(os, input, errors);
}
}
}

View file

@ -1,5 +1,5 @@
/* -*- coding: utf-8 -*-
** Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Laboratoire
** Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Laboratoire
** de Recherche et Développement de l'Epita (LRDE).
** Copyright (C) 2003, 2004, 2005, 2006 Laboratoire d'Informatique de
** Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
@ -120,25 +120,24 @@ using namespace spot;
return nullptr;
}
spot::parse_error_list suberror;
formula f;
spot::parsed_formula pf;
switch (type)
{
case parser_sere:
f = spot::parse_infix_sere(str, suberror, env, debug, true);
pf = spot::parse_infix_sere(str, env, debug, true);
break;
case parser_bool:
f = spot::parse_infix_boolean(str, suberror, env, debug, true);
pf = spot::parse_infix_boolean(str, env, debug, true);
break;
case parser_ltl:
f = spot::parse_infix_psl(str, suberror, env, debug, true);
pf = spot::parse_infix_psl(str, env, debug, true);
break;
}
if (suberror.empty())
return f;
if (pf.errors.empty())
return pf.f;
f = env.require(str);
auto f = env.require(str);
if (!f)
{
std::string s = "atomic proposition `";
@ -993,69 +992,65 @@ tlyy::parser::error(const location_type& location, const std::string& message)
namespace spot
{
formula
parsed_formula
parse_infix_psl(const std::string& ltl_string,
parse_error_list& error_list,
environment& env,
bool debug, bool lenient)
{
formula result = nullptr;
parsed_formula result(ltl_string);
flex_set_buffer(ltl_string,
tlyy::parser::token::START_LTL,
lenient);
tlyy::parser parser(error_list, env, result);
tlyy::parser parser(result.errors, env, result.f);
parser.set_debug_level(debug);
parser.parse();
flex_unset_buffer();
return result;
}
formula
parsed_formula
parse_infix_boolean(const std::string& ltl_string,
parse_error_list& error_list,
environment& env,
bool debug, bool lenient)
{
formula result = nullptr;
parsed_formula result(ltl_string);
flex_set_buffer(ltl_string,
tlyy::parser::token::START_BOOL,
lenient);
tlyy::parser parser(error_list, env, result);
tlyy::parser parser(result.errors, env, result.f);
parser.set_debug_level(debug);
parser.parse();
flex_unset_buffer();
return result;
}
formula
parsed_formula
parse_prefix_ltl(const std::string& ltl_string,
parse_error_list& error_list,
environment& env,
bool debug)
{
formula result = nullptr;
parsed_formula result(ltl_string);
flex_set_buffer(ltl_string,
tlyy::parser::token::START_LBT,
false);
tlyy::parser parser(error_list, env, result);
tlyy::parser parser(result.errors, env, result.f);
parser.set_debug_level(debug);
parser.parse();
flex_unset_buffer();
return result;
}
formula
parsed_formula
parse_infix_sere(const std::string& sere_string,
parse_error_list& error_list,
environment& env,
bool debug,
bool lenient)
{
formula result = nullptr;
parsed_formula result(sere_string);
flex_set_buffer(sere_string,
tlyy::parser::token::START_SERE,
lenient);
tlyy::parser parser(error_list, env, result);
tlyy::parser parser(result.errors, env, result.f);
parser.set_debug_level(debug);
parser.parse();
flex_unset_buffer();
@ -1065,19 +1060,17 @@ namespace spot
formula
parse_formula(const std::string& ltl_string, environment& env)
{
parse_error_list pel;
formula f = parse_infix_psl(ltl_string, pel, env);
parsed_formula pf = parse_infix_psl(ltl_string, env);
std::ostringstream s;
if (format_parse_errors(s, ltl_string, pel))
if (pf.format_errors(s))
{
parse_error_list pel2;
formula g = parse_prefix_ltl(ltl_string, pel2, env);
if (pel2.empty())
return g;
parsed_formula pg = parse_prefix_ltl(ltl_string, env);
if (pg.errors.empty())
return pg.f;
else
throw parse_error(s.str());
}
return f;
return pf.f;
}
}

View file

@ -1,6 +1,6 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 Laboratoire de
// Recherche et Développement de l'Epita (LRDE).
// Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Laboratoire
// de Recherche et Développement de l'Epita (LRDE).
// Copyright (C) 2003, 2004, 2005, 2006 Laboratoire d'Informatique de
// Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
@ -45,10 +45,40 @@ namespace spot
struct parse_error_list {};
#endif
/// \brief The result of a formula parser.
struct SPOT_API parsed_formula final
{
/// \brief The parsed formula.
///
/// This could be formula(nullptr) in case of a serious parse error.
formula f = nullptr;
/// The input text, before parsing.
std::string input;
/// \brief Syntax errors that occurred during parsing.
///
/// Note that the parser does not print any diagnostic.
/// Deciding how to output those errors is up to you.
///
/// \see format_errors
parse_error_list errors;
parsed_formula(const std::string& str = "")
: input(str)
{
}
/// \brief Format diagnostics.
///
/// \param os Where diagnostics should be output.
/// \return \c true iff any diagnostic was output.
bool format_errors(std::ostream& os);
};
/// \brief Build a formula from an LTL string.
/// \param ltl_string The string to parse.
/// \param error_list A list that will be filled with
/// parse errors that occured during parsing.
/// \param env The environment into which parsing should take place.
/// \param debug When true, causes the parser to trace its execution.
/// \param lenient When true, parenthesized blocks that cannot be
@ -57,59 +87,56 @@ namespace spot
/// \return A formula built from \a ltl_string, or
/// formula(nullptr) if the input was unparsable.
///
/// Note that the parser usually tries to recover from errors. It can
/// return a non zero value even if it encountered error during the
/// parsing of \a ltl_string. If you want to make sure \a ltl_string
/// was parsed succesfully, check \a error_list for emptiness.
///
/// Note that the parser usually tries to recover from errors. The
/// field parsed_formula::f in the returned object can be a non-zero
/// value even if it encountered error during the parsing of \a
/// ltl_string. If you want to make sure \a ltl_string was parsed
/// succesfully, check \a parsed_formula::errors for emptiness.
///
/// \warning This function is not reentrant.
SPOT_API
formula parse_infix_psl(const std::string& ltl_string,
parse_error_list& error_list,
environment& env =
default_environment::instance(),
bool debug = false,
bool lenient = false);
parsed_formula parse_infix_psl(const std::string& ltl_string,
environment& env =
default_environment::instance(),
bool debug = false,
bool lenient = false);
/// \brief Build a Boolean formula from a string.
/// \param ltl_string The string to parse.
/// \param error_list A list that will be filled with
/// parse errors that occured during parsing.
/// \param env The environment into which parsing should take place.
/// \param debug When true, causes the parser to trace its execution.
/// \param lenient When true, parenthesized blocks that cannot be
/// parsed as subformulas will be considered as
/// atomic propositions.
/// \return A formula built from \a ltl_string, or
/// formula(nullptr) if the input was unparsable.
/// \return A parsed_formula
///
/// Note that the parser usually tries to recover from errors. It can
/// return a non zero value even if it encountered error during the
/// parsing of \a ltl_string. If you want to make sure \a ltl_string
/// was parsed succesfully, check \a error_list for emptiness.
/// Note that the parser usually tries to recover from errors. The
/// field parsed_formula::f in the returned object can be a non-zero
/// value even if it encountered error during the parsing of \a
/// ltl_string. If you want to make sure \a ltl_string was parsed
/// succesfully, check \a parsed_formula::errors for emptiness.
///
/// \warning This function is not reentrant.
SPOT_API
formula parse_infix_boolean(const std::string& ltl_string,
parse_error_list& error_list,
environment& env =
default_environment::instance(),
bool debug = false,
bool lenient = false);
parsed_formula parse_infix_boolean(const std::string& ltl_string,
environment& env =
default_environment::instance(),
bool debug = false,
bool lenient = false);
/// \brief Build a formula from an LTL string in LBT's format.
/// \param ltl_string The string to parse.
/// \param error_list A list that will be filled with
/// parse errors that occured during parsing.
/// \param env The environment into which parsing should take place.
/// \param debug When true, causes the parser to trace its execution.
/// \return A formula built from \a ltl_string, or
/// formula(nullptr) if the input was unparsable.
///
/// Note that the parser usually tries to recover from errors. It can
/// return an non zero value even if it encountered error during the
/// parsing of \a ltl_string. If you want to make sure \a ltl_string
/// was parsed succesfully, check \a error_list for emptiness.
/// Note that the parser usually tries to recover from errors. The
/// field parsed_formula::f in the returned object can be a non-zero
/// value even if it encountered error during the parsing of \a
/// ltl_string. If you want to make sure \a ltl_string was parsed
/// succesfully, check \a parsed_formula::errors for emptiness.
///
/// The LBT syntax, also used by the lbtt and scheck tools, is
/// extended to support W, and M operators (as done in lbtt), and
@ -117,11 +144,10 @@ namespace spot
///
/// \warning This function is not reentrant.
SPOT_API
formula parse_prefix_ltl(const std::string& ltl_string,
parse_error_list& error_list,
environment& env =
default_environment::instance(),
bool debug = false);
parsed_formula parse_prefix_ltl(const std::string& ltl_string,
environment& env =
default_environment::instance(),
bool debug = false);
/// \brief A simple wrapper to parse_infix_psl() and parse_prefix_ltl().
///
@ -135,8 +161,6 @@ namespace spot
/// \brief Build a formula from a string representing a SERE.
/// \param sere_string The string to parse.
/// \param error_list A list that will be filled with
/// parse errors that occured during parsing.
/// \param env The environment into which parsing should take place.
/// \param debug When true, causes the parser to trace its execution.
/// \param lenient When true, parenthesized blocks that cannot be
@ -145,37 +169,19 @@ namespace spot
/// \return A formula built from \a sere_string, or
/// formula(0) if the input was unparsable.
///
/// Note that the parser usually tries to recover from errors. It can
/// return an non zero value even if it encountered error during the
/// parsing of \a ltl_string. If you want to make sure \a ltl_string
/// was parsed succesfully, check \a error_list for emptiness.
/// Note that the parser usually tries to recover from errors. The
/// field parsed_formula::f in the returned object can be a non-zero
/// value even if it encountered error during the parsing of \a
/// ltl_string. If you want to make sure \a ltl_string was parsed
/// succesfully, check \a parsed_formula::errors for emptiness.
///
/// \warning This function is not reentrant.
SPOT_API
formula parse_infix_sere(const std::string& sere_string,
parse_error_list& error_list,
environment& env =
default_environment::instance(),
bool debug = false,
bool lenient = false);
/// \brief Format diagnostics produced by spot::parse
/// or spot::ratexp
///
/// If the string is utf8 encoded, spot::fix_utf8_locations()
/// will be used to report correct utf8 locations (assuming the
/// output is utf8 aware). Nonetheless, the supplied \a
/// error_list will not be modified.
///
/// \param os Where diagnostics should be output.
/// \param input_string The string that were parsed.
/// \param error_list The error list filled by spot::parse
/// or spot::parse_sere while parsing \a input_string.
/// \return \c true iff any diagnostic was output.
SPOT_API
bool format_parse_errors(std::ostream& os,
const std::string& input_string,
const parse_error_list& error_list);
parsed_formula parse_infix_sere(const std::string& sere_string,
environment& env =
default_environment::instance(),
bool debug = false,
bool lenient = false);
/// \brief Fix location of diagnostics assuming the input is utf8.
///