Module: Dhall

Defined in:
lib/dhall.rb,
lib/dhall/ast.rb,
lib/dhall/util.rb,
lib/dhall/coder.rb,
lib/dhall/types.rb,
lib/dhall/binary.rb,
lib/dhall/parser.rb,
lib/dhall/resolve.rb,
lib/dhall/as_dhall.rb,
lib/dhall/builtins.rb,
lib/dhall/normalize.rb,
lib/dhall/typecheck.rb

Defined Under Namespace

Modules: AsDhall, Builtins, Parser, Resolvers, TypeChecker, Types, Util Classes: Application, Assertion, Bool, Builtin, BuiltinFunction, Coder, Double, EmptyList, EmptyRecord, EmptyRecordProjection, EmptyRecordType, Enum, Expression, ExpressionResolver, ExpressionVisitor, Forall, Function, FunctionProxy, FunctionProxyRaw, If, Import, ImportBannedException, ImportFailedException, ImportLoopException, Integer, Let, LetBlock, LetIn, List, Merge, Natural, Operator, Optional, OptionalNone, Record, RecordProjection, RecordProjectionByExpression, RecordSelection, RecordType, RubyObjectRaw, Text, TextLiteral, TimeoutException, ToMap, TypeAnnotation, Union, UnionType, Variable

Constant Summary collapse

BINARY =
{
	::TrueClass    => ->(e) { Bool.new(value: e) },
	::FalseClass   => ->(e) { Bool.new(value: e) },
	::Float        => ->(e) { Double.new(value: e) },
	::String       => ->(e) { Builtins[e.to_sym] || (raise "Unknown builtin") },
	::Integer      => ->(e) { Variable.new(index: e) },
	::Array        => lambda { |e|
		e = e.map(&method(:handle_tag))
		if e.length == 2 && e.first.is_a?(::String)
			Variable.new(name: e[0], index: e[1])
		else
			tag, *body = e
			BINARY_TAGS[tag]&.decode(*body) ||
				(raise "Unknown expression: #{e.inspect}")
		end
	},
	::CBOR::Tagged => ->(e) { Dhall.decode(handle_tag(e)) }
}.freeze
BINARY_TAGS =
[
	Application,
	Function,
	Forall,
	Operator,
	List,
	Optional,
	Merge,
	RecordType,
	Record,
	RecordSelection,
	RecordProjection,
	UnionType,
	Union,
	nil,
	If,
	Natural,
	Integer,
	nil,
	TextLiteral,
	Assertion,
	nil,
	nil,
	nil,
	nil,
	Import,
	LetIn,
	TypeAnnotation,
	ToMap,
	EmptyList
].freeze

Class Method Summary collapse

Class Method Details

.decode(expression) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/dhall/binary.rb', line 16

def self.decode(expression)
	BINARY.each do |match, use|
		return use[expression] if expression.is_a?(match)
	end

	raise "Unknown expression: #{expression.inspect}"
end

.dump(o) ⇒ Object



46
47
48
# File 'lib/dhall.rb', line 46

def self.dump(o)
	CBOR.encode(o.as_dhall)
end

.from_binary(cbor_binary) ⇒ Object



12
13
14
# File 'lib/dhall/binary.rb', line 12

def self.from_binary(cbor_binary)
	decode(CBOR.decode(cbor_binary))
end

.handle_tag(e) ⇒ Object



298
299
300
301
302
303
# File 'lib/dhall/binary.rb', line 298

def self.handle_tag(e)
	return e unless e.is_a?(::CBOR::Tagged)
	return e.value if e.tag == 55799

	raise "Unknown tag: #{e.inspect}"
end

.load(source, resolver: Resolvers::Default.new, timeout: 10) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dhall.rb', line 17

def self.load(
	source,
	resolver: Resolvers::Default.new,
	timeout: 10
)
	deadline = Util::Deadline.for_timeout(timeout)
	Promise.resolve(nil).then {
		load_raw(source.to_s, timeout: timeout).resolve(
			resolver: resolver.with_deadline(deadline)
		)
	}.then do |resolved|
		deadline.timeout_block do
			TypeChecker.for(resolved).annotate(TypeChecker::Context.new).normalize
		end
	end
end

.load_raw(source, timeout: 10) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dhall.rb', line 34

def self.load_raw(source, timeout: 10)
	source = Util.text_or_binary(source)

	Util::Deadline.for_timeout(timeout).timeout_block do
		if source.encoding == Encoding::BINARY
			from_binary(source)
		else
			Parser.parse(source).value
		end
	end
end