Class: Dhall::TextLiteral
- Inherits:
-
Expression
show all
- Defined in:
- lib/dhall/ast.rb,
lib/dhall/binary.rb,
lib/dhall/normalize.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Expression
#&, #*, #+, #as_dhall, #cache_key, #call, #concat, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #resolve, #shift, #slice, #substitute, #to_cbor, #to_proc, #to_s, #|
Class Method Details
.decode(*chunks) ⇒ Object
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/dhall/binary.rb', line 180
def self.decode(*chunks)
lead_text, *pairs = chunks
chunks =
[Text.new(value: lead_text)] +
pairs.each_slice(2).flat_map do |(e, t)|
[Dhall.decode(e), Text.new(value: t)]
end
chunks.length == 1 ? chunks.first : TextLiteral.new(chunks: chunks)
end
|
.for(*chunks) ⇒ Object
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
|
# File 'lib/dhall/ast.rb', line 1164
def self.for(*chunks)
fixed =
([""] + chunks)
.flat_map { |c| ["", c, ""] }
.map { |c| c.is_a?(Expression) ? c : Text.new(value: c.to_s) }
.chunk { |x| x.is_a?(Text) }.flat_map do |(is_text, group)|
is_text ? group.reduce(&:<<) : group
end
fixed.length == 1 ? fixed.first : new(chunks: fixed)
end
|
Instance Method Details
#as_json ⇒ Object
1184
1185
1186
|
# File 'lib/dhall/ast.rb', line 1184
def as_json
[18, *chunks.map { |chunk| chunk.is_a?(Text) ? chunk.value : chunk.as_json }]
end
|
#end_empty? ⇒ Boolean
1180
1181
1182
|
# File 'lib/dhall/ast.rb', line 1180
def end_empty?
chunks.last.empty?
end
|
#flatten ⇒ Object
385
386
387
388
389
|
# File 'lib/dhall/normalize.rb', line 385
def flatten
with(chunks: chunks.flat_map do |chunk|
chunk.is_a?(TextLiteral) ? chunk.chunks : chunk
end)
end
|
#normalize ⇒ Object
374
375
376
377
378
379
380
381
382
383
|
# File 'lib/dhall/normalize.rb', line 374
def normalize
lit = TextLiteral.for(*super.flatten.chunks)
if lit.is_a?(TextLiteral) && lit.chunks.length == 3 &&
lit.start_empty? && lit.end_empty?
lit.chunks[1]
else
lit
end
end
|
#start_empty? ⇒ Boolean
1176
1177
1178
|
# File 'lib/dhall/ast.rb', line 1176
def start_empty?
chunks.first.empty?
end
|