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
#&, #*, #+, #annotate, #as_dhall, #cache_key, #call, #concat, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #resolve, #shift, #slice, #substitute, #to_binary, #to_cbor, #to_proc, #to_s, #|
Class Method Details
.decode(*chunks) ⇒ Object
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/dhall/binary.rb', line 204
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
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
|
# File 'lib/dhall/ast.rb', line 1301
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
1321
1322
1323
|
# File 'lib/dhall/ast.rb', line 1321
def as_json
[18, *chunks.map { |chunk| chunk.is_a?(Text) ? chunk.value : chunk.as_json }]
end
|
#end_empty? ⇒ Boolean
1317
1318
1319
|
# File 'lib/dhall/ast.rb', line 1317
def end_empty?
chunks.last.empty?
end
|
#flatten ⇒ Object
427
428
429
430
431
|
# File 'lib/dhall/normalize.rb', line 427
def flatten
with(chunks: chunks.flat_map do |chunk|
chunk.is_a?(TextLiteral) ? chunk.chunks : chunk
end)
end
|
#normalize ⇒ Object
416
417
418
419
420
421
422
423
424
425
|
# File 'lib/dhall/normalize.rb', line 416
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
1313
1314
1315
|
# File 'lib/dhall/ast.rb', line 1313
def start_empty?
chunks.first.empty?
end
|