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 the two version-1 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[untyped]

RBS:

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

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Array[untyped])


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

def array(value, path)
  invalid(path, "must be an array") unless value.is_a?(Array)
  value
end

#boolean(value, path) ⇒ Boolean

RBS:

  • (untyped value, String path) -> bool

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Boolean)


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

def boolean(value, path)
  invalid(path, "must be a boolean") unless [true, false].include?(value)
  value
end

#child_path(path, key) ⇒ String

RBS:

  • (String path, String key) -> String

Parameters:

  • path (String)
  • key (String)

Returns:

  • (String)


118
119
120
# File 'lib/ibex/ir/validator/base.rb', line 118

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:

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

Parameters:

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

Returns:

  • (String)


72
73
74
75
76
# File 'lib/ibex/ir/validator/base.rb', line 72

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

#field(value, key, path) ⇒ Object

RBS:

  • (Hash[String, untyped] value, String key, String path) -> untyped

Parameters:

  • value (Hash[String, untyped])
  • key (String)
  • path (String)

Returns:

  • (Object)


113
114
115
# File 'lib/ibex/ir/validator/base.rb', line 113

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

#integer(value, path) ⇒ Integer

RBS:

  • (untyped value, String path) -> Integer

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Integer)


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

def integer(value, path)
  invalid(path, "must be an integer") unless value.is_a?(Integer)
  value
end

#invalid(path, message) ⇒ bot

RBS:

  • (String path, String message) -> bot

Parameters:

  • path (String)
  • message (String)

Returns:

  • (bot)


123
124
125
# File 'lib/ibex/ir/validator/base.rb', line 123

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

#literal(value, path, expected) ⇒ void

This method returns an undefined value.

RBS:

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

Parameters:

  • value (Object)
  • path (String)
  • expected (Object)


67
68
69
# File 'lib/ibex/ir/validator/base.rb', line 67

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:

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

Parameters:

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


79
80
81
82
83
84
85
86
# File 'lib/ibex/ir/validator/base.rb', line 79

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

  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:

  • (untyped value, String path) -> void

Parameters:

  • value (Object)
  • path (String)


103
104
105
106
107
108
109
110
# File 'lib/ibex/ir/validator/base.rb', line 103

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

  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:

  • (untyped value, String path) -> String

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (String)


41
42
43
44
45
# File 'lib/ibex/ir/validator/base.rb', line 41

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

#nonnegative_integer(value, path) ⇒ Integer

RBS:

  • (untyped value, String path) -> Integer

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Integer)


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

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

#nullable_string(value, path) ⇒ String?

RBS:

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

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (String, nil)


96
97
98
99
100
# File 'lib/ibex/ir/validator/base.rb', line 96

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

  string(value, path)
end

#object(value, path) ⇒ Hash[String, untyped]

RBS:

  • (untyped value, String path) -> Hash[String, untyped]

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Hash[String, untyped])


23
24
25
26
# File 'lib/ibex/ir/validator/base.rb', line 23

def object(value, path)
  invalid(path, "must be an object") unless value.is_a?(Hash)
  value
end

#positive_integer(value, path) ⇒ Integer

RBS:

  • (untyped value, String path) -> Integer

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Integer)


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

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

#record(value, path, required, optional = []) ⇒ Hash[String, untyped]

RBS:

  • (untyped value, String path, Array[String] required, ?Array[String] optional) -> Hash[String, untyped]

Parameters:

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

Returns:

  • (Hash[String, untyped])


13
14
15
16
17
18
19
20
# File 'lib/ibex/ir/validator/base.rb', line 13

def record(value, path, required, optional = [])
  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:

  • (untyped value, String path) -> String

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (String)


35
36
37
38
# File 'lib/ibex/ir/validator/base.rb', line 35

def string(value, path)
  invalid(path, "must be a string") unless value.is_a?(String)
  value
end