Exceptions.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ?? "<anonymous.lua>"}:{(Position == null ? "" : $"{Position.Value}:")} {base.Message}";
  35. }
  36. public class LuaRuntimeException(Traceback traceback, string message) : LuaException(message)
  37. {
  38. public Traceback Traceback { 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 override string Message => $"{Traceback.RootChunkName}:{Traceback.LastPosition.Line}: {base.Message}{(Traceback.StackFrames.Length > 0 ? $"\n{Traceback}" : "")}";
  60. }
  61. public class LuaAssertionException(Traceback traceback, string message) : LuaRuntimeException(traceback, message)
  62. {
  63. public override string ToString()
  64. {
  65. return $"{Message}\n{StackTrace}";
  66. }
  67. }