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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/strling/nodes.rb', line 21
def self.from_json(hash)
return nil if hash.nil?
case hash['type']
when 'Literal'
Literal.new(value: hash['value'])
when 'Quantifier'
Quantifier.new(
target: from_json(hash['target']),
min: hash['min'],
max: hash['max'],
greedy: hash['greedy'],
lazy: hash['lazy'],
possessive: hash['possessive']
)
when 'CharacterClass'
CharacterClass.new(
negated: hash['negated'],
members: hash['members'].map { |m| from_json(m) }
)
when 'Group'
Group.new(
capturing: hash['capturing'],
body: from_json(hash['body'] || hash['expression']),
name: hash['name'],
atomic: hash['atomic']
)
when 'Alternation'
Alternation.new(
alternatives: hash['alternatives'].map { |a| from_json(a) }
)
when 'UnicodeProperty'
UnicodeProperty.new(
name: hash['name'],
value: hash['value'],
negated: hash['negated']
)
when 'Sequence'
Sequence.new(
parts: hash['parts'].map { |p| from_json(p) }
)
when 'Lookahead'
Lookahead.new(
body: from_json(hash['body']),
negative: hash['negative'] || false
)
when 'NegativeLookahead'
Lookahead.new(
body: from_json(hash['body']),
negative: true
)
when 'Lookbehind'
Lookbehind.new(
body: from_json(hash['body']),
negative: hash['negative'] || false
)
when 'NegativeLookbehind'
Lookbehind.new(
body: from_json(hash['body']),
negative: true
)
when 'Anchor'
Anchor.new(at: hash['at'])
when 'Escape'
Escape.new(kind: hash['kind'])
when 'BackReference', 'Backreference'
BackReference.new(name: hash['name'], index: hash['index'])
when 'Dot'
Dot.new(value: nil)
when 'Range'
Range.new(from: hash['from'], to: hash['to'])
else
Data.define(:type, :raw).new(type: hash['type'], raw: hash)
end
end
|