Class: Ibex::IR::Validator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/ir/validator/base.rb,
sig/ibex/ir/validator/base.rbs

Overview

Shared JSON-shape checks for current IR documents.

Direct Known Subclasses

AutomatonDocument, GrammarDocument, LexerDocument

Constant Summary collapse

POSITION =

Returns:

  • (::String)
"(ir):1:1"

Instance Method Summary collapse

Instance Method Details

#array(value, path) ⇒ Array[json_value]

RBS:

  • (json_value value, String path) -> Array[json_value]

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Array[json_value])


37
38
39
40
41
42
43
44
# File 'lib/ibex/ir/validator/base.rb', line 37

def array(value, path)
  if value.is_a?(Array)
    array = value #: Array[json_value]
    return array
  end

  invalid(path, "must be an array")
end

#boolean(value, path) ⇒ Boolean

RBS:

  • (json_value value, String path) -> bool

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'lib/ibex/ir/validator/base.rb', line 75

def boolean(value, path)
  if [true, false].include?(value)
    result = value #: bool
    return result
  end

  invalid(path, "must be a boolean")
end

#child_path(path, key) ⇒ String

RBS:

  • (String path, String key) -> String

Parameters:

  • path (String)
  • key (String)

Returns:

  • (String)


136
137
138
# File 'lib/ibex/ir/validator/base.rb', line 136

def child_path(path, key)
  key.match?(/\A[$A-Za-z_][$A-Za-z0-9_]*\z/) ? "#{path}.#{key}" : "#{path}[#{key.inspect}]"
end

#enum(value, path, values) ⇒ String

RBS:

  • (json_value value, String path, Array[String] values) -> String

Parameters:

  • value (json_value)
  • path (String)
  • values (Array[String])

Returns:

  • (String)


90
91
92
93
94
# File 'lib/ibex/ir/validator/base.rb', line 90

def enum(value, path, values)
  value = string(value, path)
  invalid(path, "must be one of #{values.join(', ')}") unless values.include?(value)
  value
end

#field(value, key, path) ⇒ json_value

RBS:

  • (json_object value, String key, String path) -> json_value

Parameters:

  • value (json_object)
  • key (String)
  • path (String)

Returns:

  • (json_value)


131
132
133
# File 'lib/ibex/ir/validator/base.rb', line 131

def field(value, key, path)
  value.fetch(key) { invalid(path, "is missing required field #{key.inspect}") }
end

#integer(value, path) ⇒ Integer

RBS:

  • (json_value value, String path) -> Integer

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Integer)


61
62
63
64
65
# File 'lib/ibex/ir/validator/base.rb', line 61

def integer(value, path)
  return value if value.is_a?(Integer)

  invalid(path, "must be an integer")
end

#invalid(path, message) ⇒ bot

RBS:

  • (String path, String message) -> bot

Parameters:

  • path (String)
  • message (String)

Returns:

  • (bot)


141
142
143
# File 'lib/ibex/ir/validator/base.rb', line 141

def invalid(path, message)
  raise Ibex::Error, "#{POSITION}: #{path} #{message}"
end

#literal(value, path, expected) ⇒ void

This method returns an undefined value.

RBS:

  • (json_value value, String path, json_value expected) -> void

Parameters:

  • value (json_value)
  • path (String)
  • expected (json_value)


85
86
87
# File 'lib/ibex/ir/validator/base.rb', line 85

def literal(value, path, expected)
  invalid(path, "must be #{expected.inspect}") unless value == expected
end

#location(value, path, nullable: true) ⇒ void

This method returns an undefined value.

RBS:

  • (json_value value, String path, ?nullable: bool) -> void

Parameters:

  • value (json_value)
  • path (String)
  • nullable: (Boolean) (defaults to: true)


97
98
99
100
101
102
103
104
# File 'lib/ibex/ir/validator/base.rb', line 97

def location(value, path, nullable: true)
  return if nullable && value.nil?

  value = record(value, path, %w[file line column])
  string(value["file"], "#{path}.file")
  positive_integer(value["line"], "#{path}.line")
  positive_integer(value["column"], "#{path}.column")
end

#metadata(value, path) ⇒ void

This method returns an undefined value.

RBS:

  • (json_value value, String path) -> void

Parameters:

  • value (json_value)
  • path (String)


121
122
123
124
125
126
127
128
# File 'lib/ibex/ir/validator/base.rb', line 121

def (value, path)
  return if value.nil?

  value = string(value, path)
  invalid(path, "must not be empty") if value.strip.empty?
  invalid(path, "must be a single line") if value.match?(/[\r\n]/)
  invalid(path, "must not contain control characters") if value.match?(/[[:cntrl:]]/)
end

#nonempty_string(value, path) ⇒ String

RBS:

  • (json_value value, String path) -> String

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (String)


54
55
56
57
58
# File 'lib/ibex/ir/validator/base.rb', line 54

def nonempty_string(value, path)
  value = string(value, path)
  invalid(path, "must not be empty") if value.empty?
  value
end

#nonnegative_integer(value, path) ⇒ Integer

RBS:

  • (json_value value, String path) -> Integer

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Integer)


68
69
70
71
72
# File 'lib/ibex/ir/validator/base.rb', line 68

def nonnegative_integer(value, path)
  value = integer(value, path)
  invalid(path, "must be greater than or equal to 0") if value.negative?
  value
end

#nullable_string(value, path) ⇒ String?

RBS:

  • (json_value value, String path) -> String?

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (String, nil)


114
115
116
117
118
# File 'lib/ibex/ir/validator/base.rb', line 114

def nullable_string(value, path)
  return nil if value.nil?

  string(value, path)
end

#object(value, path) ⇒ json_object

RBS:

  • (json_value value, String path) -> json_object

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (json_object)


27
28
29
30
31
32
33
34
# File 'lib/ibex/ir/validator/base.rb', line 27

def object(value, path)
  if value.is_a?(Hash)
    object = value #: json_object
    return object
  end

  invalid(path, "must be an object")
end

#positive_integer(value, path) ⇒ Integer

RBS:

  • (json_value value, String path) -> Integer

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (Integer)


107
108
109
110
111
# File 'lib/ibex/ir/validator/base.rb', line 107

def positive_integer(value, path)
  value = integer(value, path)
  invalid(path, "must be greater than or equal to 1") unless value.positive?
  value
end

#record(value, path, required, optional = []) ⇒ json_object

RBS:

  • (json_value value, String path, Array[String] required, ?Array[String] optional) -> json_object

Parameters:

  • value (json_value)
  • path (String)
  • required (Array[String])
  • optional (Array[String]) (defaults to: [])

Returns:

  • (json_object)


17
18
19
20
21
22
23
24
# File 'lib/ibex/ir/validator/base.rb', line 17

def record(value, path, required, optional = [])
  value = object(value, path)
  missing = required.reject { |key| value.key?(key) }
  invalid(path, "is missing required field #{missing.first.inspect}") unless missing.empty?
  unknown = value.keys - required - optional
  invalid(path, "has unsupported field #{unknown.first.inspect}") unless unknown.empty?
  value
end

#string(value, path) ⇒ String

RBS:

  • (json_value value, String path) -> String

Parameters:

  • value (json_value)
  • path (String)

Returns:

  • (String)


47
48
49
50
51
# File 'lib/ibex/ir/validator/base.rb', line 47

def string(value, path)
  return value if value.is_a?(String)

  invalid(path, "must be a string")
end