field_struct-json_schema 1.0.0.pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +225 -0
- data/lib/field_struct/json_schema/builder.rb +195 -0
- data/lib/field_struct/json_schema/cache.rb +51 -0
- data/lib/field_struct/json_schema/copy.rb +38 -0
- data/lib/field_struct/json_schema/defaults.rb +72 -0
- data/lib/field_struct/json_schema/nullable.rb +61 -0
- data/lib/field_struct/json_schema/override.rb +20 -0
- data/lib/field_struct/json_schema/pattern/char_class.rb +203 -0
- data/lib/field_struct/json_schema/pattern/char_set.rb +51 -0
- data/lib/field_struct/json_schema/pattern/escape.rb +61 -0
- data/lib/field_struct/json_schema/pattern/item.rb +72 -0
- data/lib/field_struct/json_schema/pattern/safety/chains.rb +78 -0
- data/lib/field_struct/json_schema/pattern/safety/groups.rb +97 -0
- data/lib/field_struct/json_schema/pattern/safety/paths.rb +126 -0
- data/lib/field_struct/json_schema/pattern/safety.rb +29 -0
- data/lib/field_struct/json_schema/pattern/translator.rb +296 -0
- data/lib/field_struct/json_schema/pattern.rb +31 -0
- data/lib/field_struct/json_schema/scalar.rb +142 -0
- data/lib/field_struct/json_schema/stamp.rb +54 -0
- data/lib/field_struct/json_schema/version.rb +10 -0
- data/lib/field_struct/json_schema.rb +109 -0
- data/sig/field_struct-json_schema.rbs +56 -0
- metadata +89 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FieldStruct
|
|
4
|
+
module JSONSchema
|
|
5
|
+
# The optional (nullable) form of a schema (JS-B6, JS-B7). Never mutates its argument.
|
|
6
|
+
module Nullable
|
|
7
|
+
NULL = { 'type' => 'null' }.freeze
|
|
8
|
+
GUARDED_KEYS = %w[enum pattern format].freeze
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# Makes +schema+ also accept null.
|
|
13
|
+
#
|
|
14
|
+
# @param schema [::Hash{::String=>::Object}]
|
|
15
|
+
# @return [::Hash{::String=>::Object}]
|
|
16
|
+
def call(schema)
|
|
17
|
+
return schema if schema.empty? || schema.keys == ['x-ruby-type']
|
|
18
|
+
|
|
19
|
+
if schema['type'].is_a?(::String) && !schema.key?('$ref')
|
|
20
|
+
widen_type(schema)
|
|
21
|
+
elsif schema.key?('anyOf')
|
|
22
|
+
schema.merge('anyOf' => [*schema['anyOf'], NULL.dup])
|
|
23
|
+
else
|
|
24
|
+
{ 'anyOf' => [schema, NULL.dup] }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The optional form for a field of +type+: nullable, plus the blank-string alternative for String-family
|
|
29
|
+
# types whose blank strings count as missing and so skip +enum:+/+format:+.
|
|
30
|
+
#
|
|
31
|
+
# @param schema [::Hash{::String=>::Object}]
|
|
32
|
+
# @param type [FieldStruct::Types::Base]
|
|
33
|
+
# @return [::Hash{::String=>::Object}]
|
|
34
|
+
def field(schema, type)
|
|
35
|
+
nullable = call(schema)
|
|
36
|
+
return nullable unless type.is_a?(FieldStruct::Types::String) && GUARDED_KEYS.any? { |key| nullable.key?(key) }
|
|
37
|
+
|
|
38
|
+
blank = blank_alternative(type)
|
|
39
|
+
blank ? { 'anyOf' => [nullable, blank.dup] } : nullable
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @param type [FieldStruct::Types::String]
|
|
43
|
+
# @return [::Hash{::String=>::Object}, nil]
|
|
44
|
+
def blank_alternative(type)
|
|
45
|
+
if type.missing?(' ')
|
|
46
|
+
BLANK_STRING
|
|
47
|
+
elsif type.missing?('')
|
|
48
|
+
EMPTY_STRING
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# @param schema [::Hash{::String=>::Object}]
|
|
53
|
+
# @return [::Hash{::String=>::Object}]
|
|
54
|
+
def widen_type(schema)
|
|
55
|
+
widened = schema.merge('type' => [schema['type'], 'null'])
|
|
56
|
+
widened['enum'] = [*schema['enum'], nil] if schema.key?('enum')
|
|
57
|
+
widened
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FieldStruct
|
|
4
|
+
module JSONSchema
|
|
5
|
+
# Reads the value of a +json_schema:+ option or a +json_schema+ hook (JS-B13).
|
|
6
|
+
module Override
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
# @param value [::Object]
|
|
10
|
+
# @return [::Hash{::String=>::Object}, nil] the schema, or nil when +value+ is neither a Hash nor a boolean
|
|
11
|
+
def normalize(value)
|
|
12
|
+
case value
|
|
13
|
+
when ::Hash then value
|
|
14
|
+
when true then {}
|
|
15
|
+
when false then { 'not' => {} }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'char_set'
|
|
4
|
+
require_relative 'escape'
|
|
5
|
+
require_relative 'item'
|
|
6
|
+
|
|
7
|
+
module FieldStruct
|
|
8
|
+
module JSONSchema
|
|
9
|
+
module Pattern
|
|
10
|
+
# Reads a character class +[...]+ (JS-B53) and, under +i+, folds it (JS-B54). Throws +:refuse+ for anything with no
|
|
11
|
+
# exact equivalent. Internal.
|
|
12
|
+
class CharClass
|
|
13
|
+
# One thing in a class: +char+ is set when it is a single character (usable as a range end).
|
|
14
|
+
Member = Struct.new(:text, :set, :char, :escaped)
|
|
15
|
+
private_constant :Member
|
|
16
|
+
|
|
17
|
+
# @param source [::String] the whole pattern source
|
|
18
|
+
# @param pos [::Integer] the position right after the +[+
|
|
19
|
+
# @param fold_case [::Boolean]
|
|
20
|
+
def initialize(source, pos, fold_case:)
|
|
21
|
+
@src = source
|
|
22
|
+
@pos = pos
|
|
23
|
+
@fold = fold_case
|
|
24
|
+
@parts = []
|
|
25
|
+
@set = 0
|
|
26
|
+
@letters = []
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @return [Array(Item, ::Integer)] the class as an atom, and the position after its +]+
|
|
30
|
+
def call
|
|
31
|
+
negated = take?('^')
|
|
32
|
+
refuse if peek == ']'
|
|
33
|
+
read_element until take?(']')
|
|
34
|
+
set = negated ? CharSet.negate(@set) : @set
|
|
35
|
+
letter = negated || @letters.size != 1 ? nil : @letters.first
|
|
36
|
+
[Item.new(kind: :atom, body: "[#{'^' if negated}#{@parts.join}]", set: set, letter: letter), @pos]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def refuse
|
|
42
|
+
throw :refuse
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def peek(offset = 0)
|
|
46
|
+
@src[@pos + offset]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def advance
|
|
50
|
+
char = @src[@pos]
|
|
51
|
+
refuse if char.nil?
|
|
52
|
+
@pos += 1
|
|
53
|
+
char
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def take?(char)
|
|
57
|
+
return false unless peek == char
|
|
58
|
+
|
|
59
|
+
@pos += 1
|
|
60
|
+
true
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def read_element
|
|
64
|
+
first = read_member(@parts.empty?)
|
|
65
|
+
if peek == '-' && peek(1) && peek(1) != ']'
|
|
66
|
+
@pos += 1
|
|
67
|
+
add_range(first, read_member(true))
|
|
68
|
+
else
|
|
69
|
+
add_member(first)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def read_member(range_end)
|
|
74
|
+
char = advance
|
|
75
|
+
case char
|
|
76
|
+
when '[' then refuse
|
|
77
|
+
when '\\' then escape
|
|
78
|
+
when '-' then dash(range_end)
|
|
79
|
+
else plain(char)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# An unescaped - is literal only as the first or last member (or as the end of a range).
|
|
84
|
+
def dash(allowed)
|
|
85
|
+
refuse unless allowed || peek == ']'
|
|
86
|
+
plain('-')
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def plain(char)
|
|
90
|
+
refuse if char.ord > 0xFFFF || (char == '&' && peek == '&')
|
|
91
|
+
|
|
92
|
+
Member.new(char, CharSet.bit(char), char, false)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def escape
|
|
96
|
+
char = advance
|
|
97
|
+
case char
|
|
98
|
+
when 's' then Member.new('\t\n\v\f\r ', CharSet::SPACE, nil, true)
|
|
99
|
+
when 'h' then Member.new('0-9a-fA-F', CharSet::HEX, nil, true)
|
|
100
|
+
when 'd', 'w', 'D', 'W' then class_escape(char)
|
|
101
|
+
when 'b' then single('\b', "\b")
|
|
102
|
+
when *Escape::CONTROL.keys then single("\\#{char}", Escape::CONTROL.fetch(char))
|
|
103
|
+
when '-', *Escape::SYNTAX then single("\\#{char}", char)
|
|
104
|
+
else other_escape(char)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def class_escape(char)
|
|
109
|
+
refuse if @fold && %w[D W].include?(char)
|
|
110
|
+
|
|
111
|
+
set = char.downcase == 'd' ? CharSet::DIGIT : CharSet::WORD
|
|
112
|
+
Member.new("\\#{char}", char == char.downcase ? set : CharSet.negate(set), nil, true)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def single(text, char)
|
|
116
|
+
Member.new(text, CharSet.bit(char), char, true)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def other_escape(char)
|
|
120
|
+
if char == '0'
|
|
121
|
+
refuse if peek&.match?(/\A[0-9]\z/)
|
|
122
|
+
|
|
123
|
+
return single('\0', "\0")
|
|
124
|
+
end
|
|
125
|
+
return numeric(char) if %w[x u c].include?(char)
|
|
126
|
+
|
|
127
|
+
refuse if Escape.alphanumeric?(char) || char.ord > 127
|
|
128
|
+
|
|
129
|
+
single(char, char)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def numeric(letter)
|
|
133
|
+
refuse if @fold && letter != 'c'
|
|
134
|
+
text, char = Escape.numeric(letter, @src, @pos) || refuse
|
|
135
|
+
@pos += text.size - 2
|
|
136
|
+
single(text, char)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def add_member(member)
|
|
140
|
+
letter = member.char&.match?(/\A[A-Za-z]\z/) && !member.escaped ? member.char : nil
|
|
141
|
+
if @fold && letter
|
|
142
|
+
@parts << folded(letter)
|
|
143
|
+
@set |= folded_set(letter)
|
|
144
|
+
else
|
|
145
|
+
@parts << member.text
|
|
146
|
+
@set |= member.set
|
|
147
|
+
end
|
|
148
|
+
@letters << letter&.downcase
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def add_range(low, high)
|
|
152
|
+
refuse unless low.char && high.char && low.char.ord <= high.char.ord
|
|
153
|
+
if @fold
|
|
154
|
+
fold_range(low, high)
|
|
155
|
+
else
|
|
156
|
+
@parts << "#{low.text}-#{high.text}"
|
|
157
|
+
@set |= CharSet.range(low.char, high.char)
|
|
158
|
+
@letters << nil
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def fold_range(low, high)
|
|
163
|
+
refuse if low.escaped || high.escaped
|
|
164
|
+
|
|
165
|
+
unless covers_letters?(low.char, high.char)
|
|
166
|
+
@parts << "#{low.char}-#{high.char}"
|
|
167
|
+
@set |= CharSet.range(low.char, high.char)
|
|
168
|
+
@letters << nil
|
|
169
|
+
return
|
|
170
|
+
end
|
|
171
|
+
refuse unless same_case_letters?(low.char, high.char)
|
|
172
|
+
|
|
173
|
+
add_folded_range(low.char, high.char)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def covers_letters?(low, high)
|
|
177
|
+
[%w[A Z], %w[a z]].any? { |from, to| low.ord <= to.ord && high.ord >= from.ord }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def same_case_letters?(low, high)
|
|
181
|
+
[/\A[a-z]\z/, /\A[A-Z]\z/].any? { |kind| low.match?(kind) && high.match?(kind) }
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def add_folded_range(low, high)
|
|
185
|
+
extras = CharSet::EXTRAS.select { |letter, _| (low.downcase..high.downcase).cover?(letter) }.values
|
|
186
|
+
@parts << "#{low}-#{high}#{low.swapcase}-#{high.swapcase}#{extras.join}"
|
|
187
|
+
@set |= CharSet.range(low, high) | CharSet.range(low.swapcase, high.swapcase)
|
|
188
|
+
@set |= CharSet::NON_ASCII unless extras.empty?
|
|
189
|
+
@letters << (low == high ? low.downcase : nil)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def folded(letter)
|
|
193
|
+
"#{letter}#{letter.swapcase}#{CharSet::EXTRAS[letter.downcase]}"
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def folded_set(letter)
|
|
197
|
+
set = CharSet.bit(letter) | CharSet.bit(letter.swapcase)
|
|
198
|
+
CharSet::EXTRAS.key?(letter.downcase) ? set | CharSet::NON_ASCII : set
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FieldStruct
|
|
4
|
+
module JSONSchema
|
|
5
|
+
module Pattern
|
|
6
|
+
# Sets of characters as Integer bitmasks (JS-B60 model): one bit per ASCII character and one bit for "some
|
|
7
|
+
# non-ASCII character" (all of them counted as one, which can only add overlap). Internal.
|
|
8
|
+
module CharSet
|
|
9
|
+
NON_ASCII = 1 << 128
|
|
10
|
+
ALL = (1 << 129) - 1
|
|
11
|
+
# Ruby folds s to U+017F (long s) and k to U+212A (Kelvin sign) under +i+ (JS-B54).
|
|
12
|
+
EXTRAS = { 's' => 0x017F.chr(::Encoding::UTF_8), 'k' => 0x212A.chr(::Encoding::UTF_8) }.freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# @param char [::String] one character
|
|
17
|
+
# @return [::Integer] its bit
|
|
18
|
+
def bit(char)
|
|
19
|
+
char.ord < 128 ? 1 << char.ord : NON_ASCII
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @param chars [::String] characters
|
|
23
|
+
# @return [::Integer]
|
|
24
|
+
def of(chars)
|
|
25
|
+
chars.each_char.reduce(0) { |set, char| set | bit(char) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @param low [::String]
|
|
29
|
+
# @param high [::String]
|
|
30
|
+
# @return [::Integer] every character from +low+ to +high+
|
|
31
|
+
def range(low, high)
|
|
32
|
+
top = [high.ord, 127].min
|
|
33
|
+
set = low.ord <= top ? ((1 << (top - low.ord + 1)) - 1) << low.ord : 0
|
|
34
|
+
high.ord > 127 ? set | NON_ASCII : set
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @param set [::Integer]
|
|
38
|
+
# @return [::Integer] the complement; non-ASCII always stays in (a negated class matches other non-ASCII characters)
|
|
39
|
+
def negate(set)
|
|
40
|
+
(ALL & ~set) | NON_ASCII
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
DIGIT = range('0', '9')
|
|
44
|
+
WORD = range('a', 'z') | range('A', 'Z') | DIGIT | bit('_')
|
|
45
|
+
SPACE = of("\t\n\v\f\r ")
|
|
46
|
+
HEX = DIGIT | range('a', 'f') | range('A', 'F')
|
|
47
|
+
DOT = ALL & ~bit("\n")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FieldStruct
|
|
4
|
+
module JSONSchema
|
|
5
|
+
module Pattern
|
|
6
|
+
# Escape sequences shared by the translator and the character-class reader (JS-B52, JS-B53). Internal.
|
|
7
|
+
module Escape
|
|
8
|
+
# Characters whose escape (+\.+) means the same in Ruby and in an ECMA-262 pattern under the +u+ flag.
|
|
9
|
+
SYNTAX = %w[^ $ \\ . * + ? ( ) [ ] { } | /].freeze
|
|
10
|
+
# The letter escapes that stand for one control character.
|
|
11
|
+
CONTROL = { 'n' => "\n", 'r' => "\r", 't' => "\t", 'f' => "\f", 'v' => "\v" }.freeze
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# Reads the digits of +\x+, +\u+ or the letter of +\c+, which start at +pos+ in +source+.
|
|
16
|
+
#
|
|
17
|
+
# @param letter [::String] +x+, +u+ or +c+
|
|
18
|
+
# @param source [::String]
|
|
19
|
+
# @param pos [::Integer] the position right after the letter
|
|
20
|
+
# @return [Array(::String, ::String), nil] the escape text as written and the character it stands for; nil when the
|
|
21
|
+
# escape is malformed or has no exact equivalent (+\xHH+ at or above 0x80 is a raw byte in Ruby)
|
|
22
|
+
def numeric(letter, source, pos)
|
|
23
|
+
case letter
|
|
24
|
+
when 'x' then hex(letter, source[pos, 2], 0x7F)
|
|
25
|
+
when 'u' then hex(letter, source[pos, 4], 0xFFFF)
|
|
26
|
+
when 'c' then control(source[pos])
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @param letter [::String]
|
|
31
|
+
# @param digits [::String, nil]
|
|
32
|
+
# @param limit [::Integer]
|
|
33
|
+
# @return [Array(::String, ::String), nil]
|
|
34
|
+
def hex(letter, digits, limit)
|
|
35
|
+
return nil unless digits&.size == (letter == 'x' ? 2 : 4) && digits.match?(/\A\h+\z/)
|
|
36
|
+
|
|
37
|
+
code = digits.to_i(16)
|
|
38
|
+
return nil if code > limit
|
|
39
|
+
|
|
40
|
+
["\\#{letter}#{digits}", [code].pack('U')]
|
|
41
|
+
rescue ::ArgumentError, ::RangeError
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @param letter [::String, nil]
|
|
46
|
+
# @return [Array(::String, ::String), nil]
|
|
47
|
+
def control(letter)
|
|
48
|
+
return nil unless letter&.match?(/\A[A-Za-z]\z/)
|
|
49
|
+
|
|
50
|
+
["\\c#{letter}", (letter.ord & 0x1f).chr]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @param char [::String, nil]
|
|
54
|
+
# @return [::Boolean] whether +char+ is an ASCII letter or digit (an escape of one has a meaning of its own)
|
|
55
|
+
def alphanumeric?(char)
|
|
56
|
+
!char.nil? && char.match?(/\A[A-Za-z0-9]\z/)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FieldStruct
|
|
4
|
+
module JSONSchema
|
|
5
|
+
module Pattern
|
|
6
|
+
# One element of a translated sequence: an atom, an anchor, a group or a lookaround, with its quantifier. The
|
|
7
|
+
# translator builds these and renders them back to text; the safety analysis reads them (JS-B60..B63). Internal.
|
|
8
|
+
class Item
|
|
9
|
+
# @return [::Symbol] +:atom+, +:anchor+, +:start+ (+\A+), +:group+ or +:look+
|
|
10
|
+
attr_reader :kind
|
|
11
|
+
# @return [::Integer] the characters it can match (CharSet); 0 for an anchor or lookaround
|
|
12
|
+
attr_reader :set
|
|
13
|
+
# @return [::Array<::Array<Item>>, nil] a group's or lookaround's alternatives
|
|
14
|
+
attr_reader :alternatives
|
|
15
|
+
# @return [::String, nil] the lower-case ASCII letter of a one-letter atom (case folding bookkeeping)
|
|
16
|
+
attr_reader :letter
|
|
17
|
+
# @return [::Integer] the fewest repetitions
|
|
18
|
+
attr_reader :min
|
|
19
|
+
# @return [::Integer, ::Float] the most repetitions (Float::INFINITY when unbounded)
|
|
20
|
+
attr_reader :max
|
|
21
|
+
# @return [::String] the quantifier text as emitted, lazy mark included
|
|
22
|
+
attr_reader :quantifier
|
|
23
|
+
|
|
24
|
+
# @param kind [::Symbol]
|
|
25
|
+
# @param body [::String, nil] the atom text, or a group's opening such as +(?:+
|
|
26
|
+
# @param set [::Integer]
|
|
27
|
+
# @param alternatives [::Array<::Array<Item>>, nil]
|
|
28
|
+
# @param letter [::String, nil]
|
|
29
|
+
def initialize(kind:, body:, set: 0, alternatives: nil, letter: nil)
|
|
30
|
+
@kind = kind
|
|
31
|
+
@body = body
|
|
32
|
+
@set = set
|
|
33
|
+
@alternatives = alternatives
|
|
34
|
+
@letter = letter
|
|
35
|
+
@min = 1
|
|
36
|
+
@max = 1
|
|
37
|
+
@quantifier = ''
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @return [::Boolean] whether a quantifier may follow
|
|
41
|
+
def quantifiable?
|
|
42
|
+
%i[atom group].include?(@kind)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Applies a quantifier.
|
|
46
|
+
#
|
|
47
|
+
# @param min [::Integer]
|
|
48
|
+
# @param max [::Integer, ::Float]
|
|
49
|
+
# @param text [::String]
|
|
50
|
+
# @return [void]
|
|
51
|
+
def quantify(min, max, text)
|
|
52
|
+
@min = min
|
|
53
|
+
@max = max
|
|
54
|
+
@quantifier = text
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @return [::String] the translated text of this item
|
|
58
|
+
def text
|
|
59
|
+
return "#{@body}#{@quantifier}" unless @alternatives
|
|
60
|
+
|
|
61
|
+
"#{@body}#{Item.render(@alternatives)})#{@quantifier}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# @param alternatives [::Array<::Array<Item>>]
|
|
65
|
+
# @return [::String]
|
|
66
|
+
def self.render(alternatives)
|
|
67
|
+
alternatives.map { |sequence| sequence.map(&:text).join }.join('|')
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../char_set'
|
|
4
|
+
|
|
5
|
+
module FieldStruct
|
|
6
|
+
module JSONSchema
|
|
7
|
+
module Pattern
|
|
8
|
+
module Safety
|
|
9
|
+
# JS-B61: finds chains of two overlapping pairs of variable-count items in a path, which cost O(n^3) or worse. Internal.
|
|
10
|
+
module Chains
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# @param paths [::Array<Paths::Path>]
|
|
14
|
+
# @return [void] throws +:unsafe+ when some path holds a chain
|
|
15
|
+
def check(paths)
|
|
16
|
+
paths.each { |path| throw :unsafe if chain?(path.nodes) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# L(i) of JS-B61: 0 unless the node is variable, else the most over earlier nodes j paired with it of
|
|
20
|
+
# 1 + max(L(k), k <= j). A path with any L >= 2 is a chain.
|
|
21
|
+
def chain?(nodes)
|
|
22
|
+
levels = []
|
|
23
|
+
peak = []
|
|
24
|
+
nodes.each_with_index do |node, index|
|
|
25
|
+
level = variable?(node) ? level_of(nodes, index, peak) : 0
|
|
26
|
+
return true if level >= 2
|
|
27
|
+
|
|
28
|
+
levels << level
|
|
29
|
+
peak << [peak.last || 0, level].max
|
|
30
|
+
end
|
|
31
|
+
false
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def level_of(nodes, index, peak)
|
|
35
|
+
node = nodes[index]
|
|
36
|
+
through = CharSet::ALL
|
|
37
|
+
best = 0
|
|
38
|
+
(index - 1).downto(0) do |back|
|
|
39
|
+
other = nodes[back]
|
|
40
|
+
best = [best, 1 + peak[back]].max if variable?(other) && pair?(other, node, through)
|
|
41
|
+
through &= pass(other)
|
|
42
|
+
break if through.zero?
|
|
43
|
+
end
|
|
44
|
+
best
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def variable?(node)
|
|
48
|
+
node.low != node.high
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# What a node lets through to the pair spanning it: everything when it can match nothing, else its own set.
|
|
52
|
+
def pass(node)
|
|
53
|
+
node.low.zero? ? CharSet::ALL : node.set
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Whether characters can move between two variable nodes, given what the nodes between let through.
|
|
57
|
+
def pair?(earlier, later, through)
|
|
58
|
+
if earlier.kind == :whole && later.kind == :whole
|
|
59
|
+
(earlier.starts & later.starts & through).nonzero? && (earlier.ends & later.ends & through).nonzero?
|
|
60
|
+
elsif earlier.kind == :whole
|
|
61
|
+
met?(earlier, later.set & through)
|
|
62
|
+
elsif later.kind == :whole
|
|
63
|
+
met?(later, earlier.set & through)
|
|
64
|
+
else
|
|
65
|
+
(earlier.set & later.set & through).nonzero?
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# One alternative of the group whose required items can each be met by a character in +carried+. An alternative
|
|
70
|
+
# with nothing required is an empty iteration, which moves nothing.
|
|
71
|
+
def met?(whole, carried)
|
|
72
|
+
whole.needs.any? { |needs| !needs.empty? && needs.all? { |need| (need & carried).nonzero? } }
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../char_set'
|
|
4
|
+
|
|
5
|
+
module FieldStruct
|
|
6
|
+
module JSONSchema
|
|
7
|
+
module Pattern
|
|
8
|
+
module Safety
|
|
9
|
+
# JS-B60: a quantifier whose maximum is more than 1 on a group that can split one input many ways is refused,
|
|
10
|
+
# unless the group is unambiguous. Internal.
|
|
11
|
+
module Groups
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# @param alternatives [::Array<::Array<Item>>]
|
|
15
|
+
# @return [void]
|
|
16
|
+
def check(alternatives)
|
|
17
|
+
alternatives.each { |sequence| sequence.each { |item| visit(item) } }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @param item [Item]
|
|
21
|
+
# @return [void]
|
|
22
|
+
def visit(item)
|
|
23
|
+
return unless item.alternatives
|
|
24
|
+
|
|
25
|
+
throw :unsafe if item.max > 1 && repeating?(item) && !unambiguous?(item)
|
|
26
|
+
check(item.alternatives)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A group is repeating when it holds a variable-count quantifier or an alternation, directly or in a nested group.
|
|
30
|
+
#
|
|
31
|
+
# @param group [Item]
|
|
32
|
+
# @return [::Boolean]
|
|
33
|
+
def repeating?(group)
|
|
34
|
+
group.alternatives.size > 1 || group.alternatives.any? do |sequence|
|
|
35
|
+
sequence.any? { |item| item.min != item.max || (item.alternatives && repeating?(item)) }
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @param group [Item]
|
|
40
|
+
# @return [::Boolean]
|
|
41
|
+
def unambiguous?(group)
|
|
42
|
+
alternatives = group.alternatives
|
|
43
|
+
return false unless alternatives.all? { |sequence| plain?(sequence) }
|
|
44
|
+
|
|
45
|
+
firsts = alternatives.map { |sequence| first_set(sequence) }
|
|
46
|
+
return false unless disjoint_all?(firsts)
|
|
47
|
+
|
|
48
|
+
every = firsts.reduce(0, :|)
|
|
49
|
+
alternatives.all? { |sequence| steady?(sequence, every) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Not empty, not made only of items that match nothing, and no group or lookaround inside.
|
|
53
|
+
def plain?(sequence)
|
|
54
|
+
!sequence.all? { |item| nullable?(item) } && sequence.none?(&:alternatives)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Every variable-count item must be disjoint from what can follow it.
|
|
58
|
+
def steady?(sequence, every)
|
|
59
|
+
sequence.each_with_index.all? do |item, index|
|
|
60
|
+
item.min == item.max || item.set.nobits?(follow_first(sequence, index, every))
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# @return [::Boolean] whether the item can match nothing (an empty count, or a zero-width item)
|
|
65
|
+
def nullable?(item)
|
|
66
|
+
item.min.zero? || %i[anchor start look].include?(item.kind)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# What a sequence can start with, looking past leading nullable items.
|
|
70
|
+
def first_set(sequence)
|
|
71
|
+
set = 0
|
|
72
|
+
sequence.each do |item|
|
|
73
|
+
set |= item.set
|
|
74
|
+
break unless nullable?(item)
|
|
75
|
+
end
|
|
76
|
+
set
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# What can come right after sequence[index]: the items after it up to the first required one, plus, when all of
|
|
80
|
+
# them can match nothing, whatever the next iteration of the group can start with.
|
|
81
|
+
def follow_first(sequence, index, every)
|
|
82
|
+
set = 0
|
|
83
|
+
sequence[(index + 1)..].each do |item|
|
|
84
|
+
set |= item.set
|
|
85
|
+
return set unless nullable?(item)
|
|
86
|
+
end
|
|
87
|
+
set | every
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def disjoint_all?(sets)
|
|
91
|
+
sets.combination(2).all? { |a, b| a.nobits?(b) }
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|