PrologException.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2007-2008, Jeff Thompson
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of the copyright holder nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. using System;
  31. namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
  32. {
  33. /// <summary>
  34. /// A PrologException is used as the exception thrown by YP.throw(Term).
  35. /// </summary>
  36. public class PrologException : Exception
  37. {
  38. public readonly object _term;
  39. /// <summary>
  40. /// Create a PrologException with the given Term. The printable exception message is the full Term.
  41. /// </summary>
  42. /// <param name="Term">the term of the exception</param>
  43. public PrologException(object Term)
  44. : base(YP.getValue(Term).ToString())
  45. {
  46. _term = YP.makeCopy(Term, new Variable.CopyStore());
  47. }
  48. /// <summary>
  49. /// Create a PrologException where the Term is error(ErrorTerm, Message).
  50. /// This uses YP.makeCopy to copy the ErrorTerm and Message so that they are valid after unbinding.
  51. /// </summary>
  52. /// <param name="ErrorTerm">the error term of the error</param>
  53. /// <param name="Messsage">the message term of the error. If this is a string, it is converted to an
  54. /// Atom so it can be used by Prolog code.
  55. /// Message, converted to a string, is use as the printable exception message.
  56. /// </param>
  57. public PrologException(object ErrorTerm, object Message)
  58. : base(YP.getValue(Message).ToString())
  59. {
  60. if (Message is string)
  61. Message = Atom.a((string)Message);
  62. _term = YP.makeCopy(new Functor2(Atom.a("error"), ErrorTerm, Message), new Variable.CopyStore());
  63. }
  64. public class TypeErrorInfo
  65. {
  66. public readonly Atom _Type;
  67. public readonly object _Culprit;
  68. public readonly object _Message;
  69. public TypeErrorInfo(Atom Type, object Culprit, object Message)
  70. {
  71. _Type = Type;
  72. _Culprit = Culprit;
  73. _Message = Message;
  74. }
  75. }
  76. /// <summary>
  77. /// Return the TypeErrorInfo for this exception, or null if _term does not match
  78. /// error(type_error(Type, Culprit), Message).
  79. /// </summary>
  80. /// <returns></returns>
  81. public TypeErrorInfo getTypeErrorInfo()
  82. {
  83. if (!(_term is Functor2 && ((Functor2)_term)._name._name == "error"))
  84. return null;
  85. object errorTerm = ((Functor2)_term)._arg1;
  86. if (!(errorTerm is Functor2 && ((Functor2)errorTerm)._name._name == "type_error"))
  87. return null;
  88. if (!(((Functor2)errorTerm)._arg1 is Atom))
  89. return null;
  90. return new TypeErrorInfo
  91. ((Atom)((Functor2)errorTerm)._arg1, ((Functor2)errorTerm)._arg2, ((Functor2)_term)._arg2);
  92. }
  93. public class ExistenceErrorInfo
  94. {
  95. public readonly Atom _Type;
  96. public readonly object _Culprit;
  97. public readonly object _Message;
  98. public ExistenceErrorInfo(Atom Type, object Culprit, object Message)
  99. {
  100. _Type = Type;
  101. _Culprit = Culprit;
  102. _Message = Message;
  103. }
  104. /// <summary>
  105. /// If _Type is procedure and _Culprit is name/artity, return the name. Otherwise return null.
  106. /// </summary>
  107. /// <returns></returns>
  108. public object getProcedureName()
  109. {
  110. if (!(_Type._name == "procedure" &&
  111. _Culprit is Functor2 && ((Functor2)_Culprit)._name == Atom.SLASH))
  112. return null;
  113. return ((Functor2)_Culprit)._arg1;
  114. }
  115. /// <summary>
  116. /// If _Type is procedure and _Culprit is name/arity and arity is an integer, return the arity.
  117. /// Otherwise return -1.
  118. /// </summary>
  119. /// <returns></returns>
  120. public int getProcedureArity()
  121. {
  122. if (!(_Type._name == "procedure" &&
  123. _Culprit is Functor2 && ((Functor2)_Culprit)._name == Atom.SLASH))
  124. return -1;
  125. if (!(((Functor2)_Culprit)._arg2 is int))
  126. return -1;
  127. return (int)((Functor2)_Culprit)._arg2;
  128. }
  129. }
  130. /// <summary>
  131. /// Return the ExistenceErrorInfo for this exception, or null if _term does not match
  132. /// error(existence_error(Type, Culprit), Message). If the returned ExistenceErrorInfo _Culprit is
  133. /// procedure, you can use its getProcedureName and getProcedureArity.
  134. /// </summary>
  135. /// <returns></returns>
  136. public ExistenceErrorInfo getExistenceErrorInfo()
  137. {
  138. if (!(_term is Functor2 && ((Functor2)_term)._name._name == "error"))
  139. return null;
  140. object errorTerm = ((Functor2)_term)._arg1;
  141. if (!(errorTerm is Functor2 && ((Functor2)errorTerm)._name._name == "existence_error"))
  142. return null;
  143. if (!(((Functor2)errorTerm)._arg1 is Atom))
  144. return null;
  145. return new ExistenceErrorInfo
  146. ((Atom)((Functor2)errorTerm)._arg1, ((Functor2)errorTerm)._arg2, ((Functor2)_term)._arg2);
  147. }
  148. }
  149. }