File: validation_error.rb

package info (click to toggle)
ruby-regexp-parser 2.11.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,092 kB
  • sloc: ruby: 6,891; makefile: 6; sh: 3
file content (65 lines) | stat: -rw-r--r-- 1,929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

class Regexp::Scanner
  # Base for all scanner validation errors
  class ValidationError < ScannerError
    # Centralizes and unifies the handling of validation related errors.
    def self.for(type, problem, reason = nil)
      types.fetch(type).new(problem, reason)
    end

    def self.types
      @types ||= {
        backref:      InvalidBackrefError,
        group:        InvalidGroupError,
        group_option: InvalidGroupOption,
        posix_class:  UnknownPosixClassError,
        property:     UnknownUnicodePropertyError,
        sequence:     InvalidSequenceError,
      }
    end
  end

  # Invalid sequence format. Used for escape sequences, mainly.
  class InvalidSequenceError < ValidationError
    def initialize(what = 'sequence', where = '')
      super "Invalid #{what} at #{where}"
    end
  end

  # Invalid group. Used for named groups.
  class InvalidGroupError < ValidationError
    def initialize(what, reason)
      super "Invalid #{what}, #{reason}."
    end
  end

  # Invalid groupOption. Used for inline options.
  # TODO: should become InvalidGroupOptionError in v3.0.0 for consistency
  class InvalidGroupOption < ValidationError
    def initialize(option, text)
      super "Invalid group option #{option} in #{text}"
    end
  end

  # Invalid back reference. Used for name a number refs/calls.
  class InvalidBackrefError < ValidationError
    def initialize(what, reason)
      super "Invalid back reference #{what}, #{reason}"
    end
  end

  # The property name was not recognized by the scanner.
  class UnknownUnicodePropertyError < ValidationError
    def initialize(name, _)
      super "Unknown unicode character property name #{name}"
    end
  end

  # The POSIX class name was not recognized by the scanner.
  class UnknownPosixClassError < ValidationError
    def initialize(text, _)
      super "Unknown POSIX class #{text}"
    end
  end
end