PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/YieldProlog/Modules/PrologException.cs

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