You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

2 months ago
  1. from . import _config
  2. if not _config.CARL_WITH_PARSER:
  3. raise ImportError("Parser is not available in the configured carl library! Did you configure carl with '-DBUILD_ADDONS=ON -DBUILD_ADDON_PARSER=ON'?")
  4. from . import parse
  5. from .parse import *
  6. class ParserError(Exception):
  7. """
  8. Error which is meant to be raised when the parser throws an error.
  9. """
  10. def __init__(self, message):
  11. self.message = message
  12. def deserialize(input, package):
  13. """
  14. :param input:
  15. :return:
  16. """
  17. error = None
  18. try:
  19. res = package.parse.parse._deserialize(input)
  20. except RuntimeError as e:
  21. error = str(e)
  22. # ugly save and rethrow yields improved error messages in pytest.
  23. if error:
  24. raise ParserError(error + " when parsing '" + input + "'")
  25. res_type = res.get_type()
  26. if res_type == parse._ParserReturnType.Rational:
  27. return res.as_rational()
  28. elif res_type == parse._ParserReturnType.Variable:
  29. return res.as_variable()
  30. elif res_type == parse._ParserReturnType.Monomial:
  31. return res.as_monomial()
  32. elif res_type == parse._ParserReturnType.Term:
  33. return res.as_term()
  34. elif res_type == parse._ParserReturnType.Polynomial:
  35. return res.as_polynomial()
  36. elif res_type == parse._ParserReturnType.RationalFunction:
  37. return res.as_rational_function()
  38. elif res_type == parse._ParserReturnType.Constraint:
  39. return res.as_constraint()
  40. elif res_type == parse._ParserReturnType.Formula:
  41. return res.as_formula()
  42. assert False, "Internal error."