~singpolyma/dhall-ruby

ref: 1026a6408714727835e512e7cf12d2ec882ddd9e dhall-ruby/lib/dhall/parser.rb -rw-r--r-- 12.0 KiB
1026a640Stephen Paul Weber Fix rubocop except for parser metrics 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# frozen_string_literal: true

require "dhall/ast"
require "dhall/builtins"

module Dhall
	module Parser
		def self.parse(*args)
			CitrusParser.parse(*args)
		end

		def self.parse_file(*args)
			CitrusParser.parse_file(*args)
		end

		module CompleteExpression
			def value
				capture(:expression).value
			end
		end

		def self.operator_expression(capture, ast_class)
			Module.new do
				define_method(:value) do
					captures(capture).map(&:value).reduce do |lhs, rhs|
						Operator.const_get(ast_class).new(lhs: lhs, rhs: rhs)
					end
				end
			end
		end

		ImportAltExpression = operator_expression(:or_expression, :ImportFallback)
		OrExpression = operator_expression(:plus_expression, :Or)
		PlusExpression = operator_expression(:text_append_expression, :Plus)
		TextAppendExpression = operator_expression(:list_append_expression, :TextConcatenate)
		ListAppendExpression = operator_expression(:and_expression, :ListConcatenate)
		AndExpression = operator_expression(:combine_expression, :And)
		CombineExpression = operator_expression(:prefer_expression, :RecursiveRecordMerge)
		PreferExpression = operator_expression(:combine_types_expression, :RightBiasedRecordMerge)
		CombineTypesExpression = operator_expression(:times_expression, :RecursiveRecordTypeMerge)
		TimesExpression = operator_expression(:equal_expression, :Times)
		EqualExpression = operator_expression(:not_equal_expression, :Equal)
		NotEqualExpression = operator_expression(:application_expression, :NotEqual)

		module ApplicationExpression
			def value
				some = capture(:some) ? [Variable["Some"]] : []
				els = some + captures(:import_expression).map(&:value)
				els.reduce do |f, arg|
					Application.for(function: f, argument: arg)
				end
			end
		end

		module SelectorExpression
			def value
				record = first.value
				selectors = matches[1].matches
				selectors.reduce(record) do |rec, sel|
					if sel.captures.key?(:labels)
						sels = sel.capture(:labels).captures(:any_label).map(&:value)
						return EmptyRecordProjection.new(record: rec) if sels.empty?
						RecordProjection.new(record: rec, selectors: sels)
					else
						RecordSelection.new(
							record:   rec,
							selector: sel.capture(:any_label).value
						)
					end
				end
			end
		end

		module Label
			def value
				if first.string == "`"
					matches[1].string
				else
					string
				end
			end
		end

		module NonreservedLabel
			def value
				if captures.key?(:label)
					capture(:label).value
				else
					string
				end
			end
		end

		module NaturalLiteral
			def value
				Natural.new(value: string.to_i)
			end
		end

		module IntegerLiteral
			def value
				Integer.new(value: string.to_i)
			end
		end

		module DoubleLiteral
			def value
				key = captures.keys.select { |k| k.is_a?(Symbol) }.first
				Double.new(value: case key
					when :infinity
						string == "-Infinity" ? -Float::INFINITY : Float::INFINITY
					when :nan
						Float::NAN
					else
						float = string.to_f
						raise Citrus::ParseError, input if float.nan? || float.infinite?
						float
					end)
			end
		end

		module DoubleQuoteLiteral
			def value
				TextLiteral.for(
					*captures(:double_quote_chunk)
					.map(&:value)
					.chunk { |s| s.is_a?(String) }
					.flat_map do |(is_string, group)|
						is_string ? group.join : group
					end
				)
			end
		end

		module DoubleQuoteChunk
			ESCAPES = {
				"\"" => "\"",
				"$"  => "$",
				"\\" => "\\",
				"/"  => "/",
				"b"  => "\b",
				"f"  => "\f",
				"n"  => "\n",
				"r"  => "\r",
				"t"  => "\t"
			}.freeze

			def value
				if first&.string == "\\" && matches[1].string =~ /\Au\h+\Z/i
					[matches[1].string[1..-1]].pack("H*").force_encoding("UTF-16BE")
				elsif first&.string == "\\"
					ESCAPES.fetch(matches[1].string) {
						raise "Invalid escape: #{string}"
					}.encode("UTF-16BE")
				elsif first&.string == "${"
					matches[1].value
				else
					string.encode("UTF-16BE")
				end
			end
		end

		module SingleQuoteLiteral
			def value
				chunks = capture(:single_quote_continue).value.flatten
				indent = chunks.join.split(/\n/, -1).map { |line|
					line.match(/^( *|\t*)/).to_s.length
				}.min

				TextLiteral.for(
					*chunks
					.chunk { |c| c != "\n" }
					.flat_map { |(line, chunk)| line ? chunk[indent..-1] : chunk }
				)
			end
		end

		module SingleQuoteContinue
			ESCAPES = {
				"'''"  => "''",
				"''${" => "${"
			}.freeze

			def value
				if matches.length == 2
					[ESCAPES.fetch(first.string, first.string), matches[1].value]
				elsif matches.empty?
					[]
				else
					[
						capture(:complete_expression).value,
						capture(:single_quote_continue).value
					]
				end
			end
		end

		module NonEmptyListLiteral
			def value
				List.new(elements: captures(:expression).map(&:value))
			end
		end

		module Identifier
			def value
				name = capture(:any_label).value

				return Dhall::Bool.new(value: true) if name == "True"
				return Dhall::Bool.new(value: false) if name == "False"

				Dhall::Builtins::ALL[name]&.new ||
					Variable.new(
						name:  name,
						index: capture(:natural_literal)&.string.to_i
					)
			end
		end

		module PrimitiveExpression
			def value
				if first&.string == "("
					capture(:expression).value
				elsif first&.string == "{"
					capture(:record_type_or_literal).value
				elsif first&.string == "<"
					capture(:union_type_or_literal).value
				else
					super
				end
			end
		end

		module UnionTypeOrLiteral
			def value
				if captures[0].string == ""
					UnionType.new(alternatives: {})
				else
					super
				end
			end
		end

		module NonEmptyUnionTypeOrLiteral
			def value
				cont = matches[1].first

				if cont && cont.matches[1].first.string == "="
					Union.new(
						tag:          captures(:any_label).first.value,
						value:        captures(:expression).first.value,
						alternatives: UnionType.new(alternatives: ::Hash[
							captures(:any_label)[1..-1].map(&:value).zip(
								captures(:expression)[1..-1].map(&:value)
							)
						])
					)
				else
					type = UnionType.new(alternatives: ::Hash[
						captures(:any_label).map(&:value).zip(
							captures(:expression).map(&:value)
						)
					])
					rest = cont && cont.matches[1].capture(:non_empty_union_type_or_literal)&.value
					if rest.is_a?(Union)
						rest.with(alternatives: type.merge(rest.alternatives))
					elsif rest
						type.merge(rest)
					else
						type
					end
				end
			end
		end

		module RecordTypeOrLiteral
			def value
				if captures[0].string == "="
					EmptyRecord.new
				elsif captures[0].string == ""
					EmptyRecordType.new
				else
					super
				end
			end
		end

		module NonEmptyRecordTypeOrLiteral
			def value
				if captures.key?(:non_empty_record_literal)
					capture(:non_empty_record_literal).value(
						capture(:any_label).value
					)
				else
					capture(:non_empty_record_type).value(
						capture(:any_label).value
					)
				end
			end
		end

		module NonEmptyRecordLiteral
			def value(first_key)
				keys = [first_key] + captures(:any_label).map(&:value)
				values = captures(:expression).map(&:value)
				Record.new(record: ::Hash[keys.zip(values)])
			end
		end

		module NonEmptyRecordType
			def value(first_key)
				keys = [first_key] + captures(:any_label).map(&:value)
				values = captures(:expression).map(&:value)
				RecordType.new(record: ::Hash[keys.zip(values)])
			end
		end

		module EmptyCollection
			def value
				if captures.key?(:list)
					EmptyList.new(element_type: capture(:import_expression).value)
				else
					OptionalNone.new(value_type: capture(:import_expression).value)
				end
			end
		end

		module NonEmptyOptional
			def value
				Optional.new(
					value:      capture(:expression).value,
					value_type: capture(:import_expression).value
				)
			end
		end

		module AnnotatedExpression
			def value
				if captures.key?(:empty_collection)
					capture(:empty_collection).value
				elsif captures.key?(:non_empty_optional)
					capture(:non_empty_optional).value
				elsif matches.length == 2
					TypeAnnotation.new(
						value: first.value,
						type:  matches[1].capture(:expression).value
					)
				else
					super
				end
			end
		end

		module Expression
			def value
				keys = captures.keys.select { |k| k.is_a?(Symbol) }
				if keys.length == 1
					capture(keys.first).value
				elsif captures.key?(:let)
					lets = first.matches.map do |let_match|
						exprs = let_match.captures(:expression)
						Let.new(
							var:    let_match.capture(:nonreserved_label).value,
							assign: exprs.last.value,
							type:   exprs.length > 1 ? exprs.first.value : nil
						)
					end

					if lets.length == 1
						LetIn.new(let: lets.first, body: matches.last.value)
					else
						LetBlock.new(lets: lets, body: matches.last.value)
					end
				elsif captures.key?(:lambda)
					Function.new(
						var:  capture(:nonreserved_label).value,
						type: captures(:expression)[0].value,
						body: captures(:expression)[1].value
					)
				elsif captures.key?(:forall)
					Forall.new(
						var:  capture(:nonreserved_label).value,
						type: captures(:expression)[0].value,
						body: captures(:expression)[1].value
					)
				elsif captures.key?(:arrow)
					Forall.of_arguments(
						capture(:operator_expression).value,
						body: capture(:expression).value
					)
				elsif captures.key?(:if)
					If.new(
						predicate: captures(:expression)[0].value,
						then:      captures(:expression)[1].value,
						else:      captures(:expression)[2].value
					)
				elsif captures.key?(:merge)
					Merge.new(
						record: captures(:import_expression)[0].value,
						input:  captures(:import_expression)[1].value,
						type:   capture(:application_expression)&.value
					)
				else
					super
				end
			end
		end

		module Import
			def value
				import_type = if captures.key?(:text)
					Dhall::Import::Text
				else
					Dhall::Import::Expression
				end

				capture(:import_hashed).value(import_type)
			end
		end

		module ImportHashed
			def value(import_type)
				integrity_check = capture(:hash)&.value
				path = capture(:import_type).value
				Dhall::Import.new(integrity_check, import_type, path)
			end
		end

		module Hash
			def value
				protocol, data = string.split(/:/, 2)
				Dhall::Import::IntegrityCheck.new(protocol, data)
			end
		end

		module Http
			SCHEME = {
				"http"  => Dhall::Import::Http,
				"https" => Dhall::Import::Https
			}.freeze

			def self.escape(s)
				URI.encode_www_form_component(s).gsub("+", "%20")
			end

			def value
				http = capture(:http_raw)
				SCHEME.fetch(http.capture(:scheme).value).new(
					if captures.key?(:import_hashed)
						capture(:import_hashed).value(Dhall::Import::Expression)
					end,
					http.capture(:authority).value,
					*http.capture(:path).captures(:path_component).map(&:value),
					http.capture(:query)&.value
				)
			end
		end

		module Env
			def value
				Dhall::Import::EnvironmentVariable.new(
					if captures.key?(:bash_environment_variable)
						capture(:bash_environment_variable).string
					else
						capture(:posix_environment_variable).value.encode("utf-8")
					end
				)
			end
		end

		module PosixEnvironmentVariable
			def value
				matches.map(&:value).join
			end
		end

		module PosixEnvironmentVariableCharacter
			ESCAPES = Dhall::Import::EnvironmentVariable::ESCAPES

			def value
				if first&.string == "\\"
					ESCAPES.fetch(matches[1].string) {
						raise "Invalid escape: #{string}"
					}.encode("UTF-16BE")
				else
					string
				end
			end
		end

		module Local
			KLASS = {
				"/"  => Dhall::Import::AbsolutePath,
				"."  => Dhall::Import::RelativePath,
				".." => Dhall::Import::RelativeToParentPath,
				"~"  => Dhall::Import::RelativeToHomePath
			}.freeze

			def value
				path = capture(:path).captures(:path_component).map(&:value)
				klass = KLASS.find { |prefix, _| string.start_with?(prefix) }.last
				klass.new(*path)
			end
		end

		module PathComponent
			def value(escaper=:itself.to_proc)
				if captures.key?(:quoted_path_character)
					escaper.call(matches[1].matches[1].value)
				else
					matches[1].value
				end
			end
		end

		module Missing
			def value
				Dhall::Import::MissingImport.new
			end
		end
	end
end

require "citrus"
Citrus.require "dhall/parser"