Exceptions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Lua.CodeAnalysis;
  2. using Lua.CodeAnalysis.Syntax;
  3. using Lua.Runtime;
  4. namespace Lua;
  5. public class LuaException(string message) : Exception(message);
  6. public class LuaParseException(string? chunkName, SourcePosition position, string message) : LuaException(message)
  7. {
  8. public string? ChunkName { get; } = chunkName;
  9. public SourcePosition? Position { get; } = position;
  10. public static void UnexpectedToken(string? chunkName, SourcePosition position, SyntaxToken token)
  11. {
  12. throw new LuaParseException(chunkName, position, $"unexpected symbol <{token.Type}> near '{token.Text}'");
  13. }
  14. public static void ExpectedToken(string? chunkName, SourcePosition position, SyntaxTokenType token)
  15. {
  16. throw new LuaParseException(chunkName, position, $"'{token}' expected");
  17. }
  18. public static void UnfinishedLongComment(string? chunkName, SourcePosition position)
  19. {
  20. throw new LuaParseException(chunkName, position, $"unfinished long comment (starting at line {position.Line})");
  21. }
  22. public static void SyntaxError(string? chunkName, SourcePosition position, SyntaxToken? token)
  23. {
  24. throw new LuaParseException(chunkName, position, $"syntax error {(token == null ? "" : $"near '{token.Value.Text}'")}");
  25. }
  26. public static void NoVisibleLabel(string label, string? chunkName, SourcePosition position)
  27. {
  28. throw new LuaParseException(chunkName, position, $"no visible label '{label}' for <goto>");
  29. }
  30. public static void BreakNotInsideALoop(string? chunkName, SourcePosition position)
  31. {
  32. throw new LuaParseException(chunkName, position, "<break> not inside a loop");
  33. }
  34. public override string Message => $"{ChunkName}:{(Position == null ? "" : $"{Position.Value}:")} {base.Message}";
  35. }
  36. public class LuaRuntimeException(Traceback traceback, string message) : LuaException(message)
  37. {
  38. public Traceback LuaTraceback { get; } = traceback;
  39. public static void AttemptInvalidOperation(Traceback traceback, string op, LuaValue a, LuaValue b)
  40. {
  41. throw new LuaRuntimeException(traceback, $"attempt to {op} a '{a.Type}' with a '{b.Type}'");
  42. }
  43. public static void AttemptInvalidOperation(Traceback traceback, string op, LuaValue a)
  44. {
  45. throw new LuaRuntimeException(traceback, $"attempt to {op} a '{a.Type}' value");
  46. }
  47. public static void BadArgument(Traceback traceback, int argumentId, string functionName)
  48. {
  49. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' (value expected)");
  50. }
  51. public static void BadArgument(Traceback traceback, int argumentId, string functionName, LuaValueType[] expected)
  52. {
  53. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' ({string.Join(" or ", expected)} expected)");
  54. }
  55. public static void BadArgument(Traceback traceback, int argumentId, string functionName, string expected, string actual)
  56. {
  57. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' ({expected} expected, got {actual})");
  58. }
  59. public static void ThrowBadArgumentIfNumberIsNotInteger(LuaState state, LuaFunction function, int argumentId, double value)
  60. {
  61. if (!MathEx.IsInteger(value))
  62. {
  63. throw new LuaRuntimeException(state.GetTraceback(), $"bad argument #{argumentId} to '{function.Name}' (number has no integer representation)");
  64. }
  65. }
  66. public override string Message => $"{LuaTraceback.RootChunkName}:{LuaTraceback.LastPosition.Line}: {base.Message}";
  67. public override string ToString()
  68. {
  69. return $"{Message}\n{(LuaTraceback.StackFrames.Length > 0 ? $"{LuaTraceback}\n" : "")}{StackTrace}";
  70. }
  71. }
  72. public class LuaAssertionException(Traceback traceback, string message) : LuaRuntimeException(traceback, message)
  73. {
  74. public override string ToString()
  75. {
  76. return $"{Message}\n{StackTrace}";
  77. }
  78. }
  79. public class LuaModuleNotFoundException(string moduleName) : LuaException($"module '{moduleName}' not found");