/ lib / graphlyte / errors.rb
errors.rb
 1  # frozen_string_literal: true
 2  
 3  module Graphlyte
 4    IllegalValue = Class.new(ArgumentError)
 5  
 6    ParseError = Class.new(StandardError)
 7  
 8    TooDeep = Class.new(StandardError) do
 9      def initialize(location)
10        super("Max parse depth exceeded at #{location}")
11      end
12    end
13  
14    Unexpected = Class.new(ParseError) do
15      def initialize(token)
16        super("Unexpected token at #{token.location}: #{token.lexeme.inspect}")
17      end
18    end
19  
20    Expected = Class.new(ParseError) do
21      def initialize(token, expected:)
22        super("Unexpected token at #{token.location}: #{token.lexeme.inspect}, expected #{expected}")
23      end
24    end
25  
26    Illegal = Class.new(ParseError) do
27      def initialize(token, reason = nil)
28        msg = "Illegal token at #{token.location}: #{token.lexeme}"
29        msg << ", #{reason}" if reason
30        super(msg)
31      end
32    end
33  end