~singpolyma/dhall-ruby

ref: c979c0c40060439b06d039a133a38d2c564123b7 dhall-ruby/scripts/generate_citrus_parser.rb -rw-r--r-- 3.2 KiB
c979c0c4Stephen Paul Weber Working parser 3 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
require "abnf"

require "dhall/parser"
require "dhall/util"

class RegexpTree::CharClass
	def encode_elt(e)
		case e
		when 0x09; '\t'
		when 0x0a; '\n'
		when 0x0d; '\r'
		when 0x0c; '\f'
		when 0x0b; '\v'
		when 0x07; '\a'
		when 0x1b; '\e'
		when 0x21, 0x22, 0x25, 0x26, 0x27, 0x2c, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x2f, 0x30..0x39, 0x40, 0x41..0x5a, 0x5f, 0x60, 0x61..0x7a, 0x7e
			sprintf("%c", e)
		else
			sprintf("\\u{%02x}", e)
		end
	end
end

class Sequence
	attr_reader :seq

	def initialize(*seq)
		@seq = seq
	end

	def +(other)
		if other.is_a?(Sequence)
			self.class.new(*seq, *other.seq)
		else
			self.class.new(*seq, other)
		end
	end

	def to_s
		@seq.join(' ')
	end
end

class Terminal
	SAFE = /\A[\w_:']+\Z/

	def initialize(regex)
		@regex = regex
	end

	def +(other)
		if options == other.options
			self.class.new(Regexp.compile("#{source}#{other.source}", options))
		else
			Sequence.new(self, other)
		end
	end

	def options
		@regex.options
	end

	def source
		if SAFE =~ @regex.source
			@regex.source
		else
			"(?:#{@regex.source})"
		end
	end

	def to_s
		if SAFE =~ @regex.source
			if @regex.casefold?
				"`#{@regex.source}`"
			else
				@regex.source.inspect
			end
		else
			@regex.inspect
		end
	end
end

class RuleFormatter
	def initialize(abnf)
		@abnf = abnf
		@bogus = 0
	end

	def bogus_name
		"____#{@bogus += 1}".intern
	end

	def format_anon_rule(rule)
		name = bogus_name
		@abnf[name] = rule
		formatted = format_rule(name, rule)
		formatted.is_a?(Terminal) ? formatted : "(#{formatted})"
	end

	def format_rule(name, rule)
		if name == :"simple-label"
			return "keyword simple_label_next_char+ | !keyword (simple_label_first_char simple_label_next_char*)"
		end

		if name == :"nonreserved-label"
			return "reserved_identifier simple_label_next_char+ | !reserved_identifier label"
		end

		case rule
		when ABNF::Term
			Terminal.new(@abnf.regexp(name))
		when ABNF::Var
			rule.name.to_s.gsub(/-/, '_')
		when ABNF::Seq
			if rule.elts.empty?
				'""'
			else
				rule.elts.map(&method(:format_anon_rule)).chunk { |x| x.is_a?(Terminal) }.flat_map { |(terminal, chunk)|
					terminal ? chunk.reduce(:+) : Sequence.new(chunk)
				}.join(' ')
			end
		when ABNF::Alt
			rule.elts.map(&method(:format_anon_rule)).join(' | ')
		when ABNF::Rep
			base = format_anon_rule(rule.elt)
			if rule.min == 0 && rule.max.nil?
				"#{base}*"
			elsif rule.min == 1 && rule.max.nil?
				"#{base}+"
			elsif rule.min == 0 && rule.max == 1
				"#{base}?"
			else
				"#{base} #{rule.min}*#{rule.max}"
			end
		else
			raise "Unknown rule type: #{rule.inspect}"
		end

	end
end

puts "grammar Dhall::Parser::CitrusParser"
puts "\troot complete_expression"

abnf = ABNF.parse(STDIN.read)
formatter = RuleFormatter.new(abnf)
abnf.each do |name, rule|
	next if name.to_s.start_with?("____")
	puts "rule #{name.to_s.gsub(/-/, '_')}"
	print "\t(#{formatter.format_rule(name, rule)})"
	extension = name.to_s.split(/-/).map(&:capitalize).join
	if Dhall::Parser.const_defined?(extension)
		puts " <Dhall::Parser::#{extension}>"
	else
		puts
	end
	puts "end"
end

puts "rule reserved_identifier"
print "\t"
puts Dhall::Util::BuiltinName::NAMES.map { |name| "\"#{name}\"" }.join(" |\n\t")
puts "end"

puts "end"