rhoella 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +35 -15
- data/reference/ruby/rhoella.rb +366 -82
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6cabe842828afe35586f02d32d80a7b7e5a5eefcfe9da35176c191ccf1a2e59f
|
|
4
|
+
data.tar.gz: ce4987fac00cfcc1d95af79b23d274599489a33d14dd48d4bbe86abc89c51c79
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f773001af95380cd77757d4ba035078e64797c3e65c0b6ac9c763cce1b3c8f83009b6ea616dc4cc90c8af04f720a937e2df482ff03bf64c559d720447ef6b15d
|
|
7
|
+
data.tar.gz: bb61cf65f79cbebca2e40187d2543bb29f9937876d2174604df81ad2723e40206d02ada389a45d5226566a69c31c61e9658b6ddb34667054ab485c6b462e3045
|
data/README.md
CHANGED
|
@@ -18,29 +18,48 @@ semantic, four runtimes:
|
|
|
18
18
|
## Supported syntax (the Fluent subset Metanorma needs)
|
|
19
19
|
|
|
20
20
|
- messages `id = pattern`; ids `[A-Za-z][A-Za-z0-9_-]*`; terms carry a
|
|
21
|
-
leading
|
|
21
|
+
leading `-`; message attributes `id.attr = pattern` (indented
|
|
22
|
+
`.attr = value` lines)
|
|
22
23
|
- multiline patterns: indented continuation lines, common indent
|
|
23
24
|
stripped, lines joined with "\n"
|
|
24
|
-
- placeables: `{ $var }`, message references `{ id }
|
|
25
|
-
|
|
25
|
+
- placeables: `{ $var }`, message references `{ id }`, member
|
|
26
|
+
references `{ id.attr }`, term references `{ -term }` with named
|
|
27
|
+
parameters `{ -term(key: $v) }`, term member references
|
|
28
|
+
`{ -term.attr }`, function references `{ FN($v) }` (uppercase callee),
|
|
29
|
+
selectors `{ expr -> [key] pattern ... *[other] pattern }` with
|
|
26
30
|
exact string matching on the selector value and a mandatory default
|
|
27
31
|
arm
|
|
28
|
-
-
|
|
32
|
+
- string literals `{"text"}` with `\"`, `\\`, `\uXXXX` and `\UXXXXXX`
|
|
33
|
+
escapes, decoded as UTF-16 code units (surrogate pairs may straddle
|
|
34
|
+
adjacent literals); text has no escape sequences — backslash is
|
|
35
|
+
literal, per the Fluent grammar
|
|
29
36
|
|
|
30
37
|
## Fluent semantics preserved
|
|
31
38
|
|
|
32
|
-
- a missing variable renders as
|
|
33
|
-
- message
|
|
34
|
-
|
|
39
|
+
- a missing variable renders as `{$name}`
|
|
40
|
+
- a reference to an unknown message or attribute renders the literal
|
|
41
|
+
source `{id}` / `{id.attr}`; an unknown function renders `{FN()}`
|
|
42
|
+
(name only, per upstream)
|
|
43
|
+
- term parameters bind as variables inside the term; outer variables
|
|
44
|
+
are NOT inherited into term resolution — only the call arguments are
|
|
45
|
+
- cyclic references fall back to their source form and resolution
|
|
46
|
+
continues; the number of placeable resolutions per format is bounded
|
|
47
|
+
(`ResolutionError`); there is no depth cap — deep acyclic chains
|
|
48
|
+
resolve
|
|
35
49
|
- a selector without a default arm is a parse error
|
|
36
50
|
|
|
37
51
|
## Contract
|
|
38
52
|
|
|
39
|
-
`
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
`testsuite/` holds the conformance layers: `upstream/` (the
|
|
54
|
+
cross-implementation parser fixtures of the Fluent spec, classified in
|
|
55
|
+
`manifest.json`, and the fluent-rs resolution fixtures under
|
|
56
|
+
`upstream/behavior/`, run by `tools/run_behavior.rb` with an explicit
|
|
57
|
+
out-of-subset skip list) and `semantics/` (resolution vectors: FTL
|
|
58
|
+
resources plus the expected resolutions generated by the Ruby
|
|
59
|
+
reference,
|
|
60
|
+
`tools/gen_vectors.rb`). Every runtime must reproduce the parse
|
|
61
|
+
classification and every resolution vector exactly. Each runtime runs
|
|
62
|
+
both layers in CI.
|
|
44
63
|
|
|
45
64
|
Regenerating the vectors after an intentional semantic change:
|
|
46
65
|
|
|
@@ -49,6 +68,7 @@ Regenerating the vectors after an intentional semantic change:
|
|
|
49
68
|
## Runtimes
|
|
50
69
|
|
|
51
70
|
ruby -I reference/ruby -e 'require "rhoella"; puts Rhoella::Bundle.new.add_resource(File.read("testsuite/semantics/resources/core.ftl")).format("hello", "name" => "TSE")'
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
71
|
+
# and in the sibling repositories:
|
|
72
|
+
# rhoella-py: python3 test_parity.py
|
|
73
|
+
# rhoella-rs: cargo test
|
|
74
|
+
# rhoella-ts: npm test
|
data/reference/ruby/rhoella.rb
CHANGED
|
@@ -9,33 +9,40 @@ module Rhoella
|
|
|
9
9
|
#
|
|
10
10
|
# Supported syntax (the Fluent subset Metanorma needs):
|
|
11
11
|
# * messages: `id = pattern`, ids are [A-Za-z][A-Za-z0-9_-]*, terms are
|
|
12
|
-
# ids with a leading
|
|
12
|
+
# ids with a leading `-`; message attributes `id.attr = pattern`
|
|
13
13
|
# * multiline patterns: continuation lines indented relative to the id
|
|
14
14
|
# line; the common indent is stripped and lines join with "\n"
|
|
15
|
-
# * placeables: `{ $var }`, message references `{ id }
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
15
|
+
# * placeables: `{ $var }`, message references `{ id }`, member
|
|
16
|
+
# references `{ id.attr }`, term references `{ -term }` and
|
|
17
|
+
# `{ -term(key: $v) }`, function references `{ FN($v) }`, string
|
|
18
|
+
# literals `{"text"}`, number literals `{1.5}`, and selectors
|
|
19
|
+
# `{ $var -> [key] pattern ... *[other] pattern }` with exact
|
|
20
|
+
# string matching on the selector value and a mandatory default arm
|
|
21
|
+
# * escapes in string literals: `\"`, `\\`, `\uXXXX` and `\UXXXXXX`;
|
|
22
|
+
# text has no escape sequences (backslash is literal), matching the
|
|
23
|
+
# Fluent grammar
|
|
20
24
|
#
|
|
21
|
-
# Fluent semantics preserved
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
25
|
+
# Fluent semantics preserved (per the upstream resolution fixtures):
|
|
26
|
+
# a missing variable renders as {$name}; a reference to an unknown
|
|
27
|
+
# message or attribute renders as its own literal {id} / {id.attr};
|
|
28
|
+
# unknown functions render as {FN()} (name only, per upstream);
|
|
29
|
+
# term parameters bind WITHOUT the outer variables (only call
|
|
30
|
+
# arguments are visible inside a term); cyclic references fall back
|
|
31
|
+
# to their source form and resolution continues; the number of
|
|
32
|
+
# placeable resolutions per format is bounded. Formats a raw pattern
|
|
33
|
+
# string directly: parses it as a Fluent pattern and resolves it
|
|
34
|
+
# against the variable bag. The entry point for hosts whose catalogs
|
|
35
|
+
# live outside FTL resources (e.g. label vocabularies carrying
|
|
36
|
+
# message values).
|
|
28
37
|
def self.format(pattern, vars = {}, messages = {})
|
|
29
38
|
table = messages.transform_values do |value|
|
|
30
|
-
Parser.new(value).parse_elements(stop: nil)
|
|
39
|
+
[Parser.new(value).parse_elements(stop: nil), {}]
|
|
31
40
|
end
|
|
32
41
|
Formatter.new(table, vars).format_pattern(
|
|
33
|
-
Parser.new(pattern).parse_elements(stop: nil),
|
|
42
|
+
Parser.new(pattern).parse_elements(stop: nil), [])
|
|
34
43
|
end
|
|
35
44
|
|
|
36
45
|
class Bundle
|
|
37
|
-
MAX_DEPTH = 5
|
|
38
|
-
|
|
39
46
|
attr_reader :messages
|
|
40
47
|
|
|
41
48
|
def initialize
|
|
@@ -47,38 +54,47 @@ module Rhoella
|
|
|
47
54
|
self
|
|
48
55
|
end
|
|
49
56
|
|
|
50
|
-
def format(id, vars = {})
|
|
57
|
+
def format(id, vars = {}, attribute = nil)
|
|
51
58
|
message = @messages[id]
|
|
52
59
|
raise ResolutionError, "unknown message #{id.inspect}" unless message
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
pattern = attribute ? message.attributes[attribute] : message.pattern
|
|
62
|
+
if attribute && pattern.nil?
|
|
63
|
+
raise ResolutionError, "unknown attribute #{attribute.inspect} on #{id.inspect}"
|
|
64
|
+
end
|
|
65
|
+
raise ResolutionError, "message #{id.inspect} has no value" if pattern.nil?
|
|
66
|
+
|
|
67
|
+
stack = attribute ? [] : [id]
|
|
68
|
+
Formatter.new(@messages, vars).format_pattern(pattern, stack)
|
|
56
69
|
end
|
|
57
70
|
|
|
58
|
-
Message = Struct.new(:id, :pattern, keyword_init: true)
|
|
71
|
+
Message = Struct.new(:id, :pattern, :attributes, keyword_init: true)
|
|
59
72
|
end
|
|
60
73
|
|
|
61
|
-
# Fluent parser
|
|
74
|
+
# Fluent parser v3: spec-shaped line machine with junk recovery.
|
|
62
75
|
#
|
|
63
76
|
# Line terminators are CR, LF and CRLF. A message block is its id
|
|
64
77
|
# line plus the lines that follow it: indented lines, blank lines
|
|
65
78
|
# and — while the pattern has unbalanced braces — any line at all
|
|
66
|
-
# (the placeable stays open across unindented lines).
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
79
|
+
# (the placeable stays open across unindented lines). Lines whose
|
|
80
|
+
# first character (after indent) begins a `.attr =` construct open a
|
|
81
|
+
# message attribute section. The block ends at a comment or an id
|
|
82
|
+
# line; an unindented non-blank line with balanced braces ends the
|
|
83
|
+
# block and becomes Junk. Entries whose joined pattern fails to parse
|
|
84
|
+
# become Junk; parsing continues.
|
|
70
85
|
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
# `{
|
|
75
|
-
#
|
|
76
|
-
#
|
|
86
|
+
# Placeable grammar per the Fluent spec: `{ $var }` (no calls on
|
|
87
|
+
# variables), `{ id }` and `{ id.attr }`, `{ -term }` and
|
|
88
|
+
# `{ -term.attr }` (term attrs in selectors), function references
|
|
89
|
+
# `{ UPPERCASE(args) }` (lowercase callees with arguments are Junk),
|
|
90
|
+
# string literals with \" \\ \uXXXX \UXXXXXX escapes, number
|
|
91
|
+
# literals, and selectors over any of these expressions. Placeable
|
|
92
|
+
# interiors skip newlines: a placeable may span lines.
|
|
77
93
|
class Parser
|
|
78
94
|
ID_LINE = /\A(-?[A-Za-z][A-Za-z0-9_-]*)[ \t]*=[ \t]?(.*)\z/.freeze
|
|
95
|
+
ATTR_LINE = /\A\.([A-Za-z][A-Za-z0-9_-]*)[ \t]*=[ \t]?(.*)\z/.freeze
|
|
79
96
|
COMMENT = /\A\#{1,3}/.freeze
|
|
80
|
-
|
|
81
|
-
Entry = Struct.new(:message, keyword_init: true)
|
|
97
|
+
UPPERCASE = /\A[A-Z][A-Z0-9_-]*\z/.freeze
|
|
82
98
|
|
|
83
99
|
class << self
|
|
84
100
|
def parse(source)
|
|
@@ -89,7 +105,8 @@ module Rhoella
|
|
|
89
105
|
lines.each_with_index do |line, i|
|
|
90
106
|
if (match = ID_LINE.match(line))
|
|
91
107
|
messages.concat(close_block(block))
|
|
92
|
-
block = Block.new(id: match[1],
|
|
108
|
+
block = Block.new(id: match[1], value: [match[2]], attrs: {})
|
|
109
|
+
block.current = :value
|
|
93
110
|
elsif COMMENT.match?(line) || (line.strip.empty? && block.nil?)
|
|
94
111
|
messages.concat(close_block(block))
|
|
95
112
|
block = nil
|
|
@@ -103,16 +120,22 @@ module Rhoella
|
|
|
103
120
|
j += 1 while j < lines.length && lines[j].strip.empty?
|
|
104
121
|
nxt = lines[j]
|
|
105
122
|
if nxt&.match?(/\A[ \t]/) || (nxt && unbalanced?(block))
|
|
106
|
-
block.
|
|
123
|
+
block.current_section << ""
|
|
107
124
|
else
|
|
108
125
|
messages.concat(close_block(block))
|
|
109
126
|
block = nil
|
|
110
127
|
end
|
|
128
|
+
elsif block && line =~ /\A[ \t]/ &&
|
|
129
|
+
(attr = ATTR_LINE.match(line.strip))
|
|
130
|
+
# An indented `.attr = value` line opens an attribute
|
|
131
|
+
# section of the current message.
|
|
132
|
+
block.attrs[attr[1]] = [attr[2]]
|
|
133
|
+
block.current = attr[1]
|
|
111
134
|
elsif block && (line =~ /\A[ \t]/ || unbalanced?(block))
|
|
112
135
|
# Continuation: indented lines, and — while the pattern has
|
|
113
136
|
# unbalanced braces — unindented lines too (a placeable
|
|
114
137
|
# stays open across them).
|
|
115
|
-
block.
|
|
138
|
+
block.current_section << line
|
|
116
139
|
elsif block
|
|
117
140
|
# An unindented, brace-balanced line ends the block; the
|
|
118
141
|
# line itself is junk outside any message.
|
|
@@ -126,7 +149,7 @@ module Rhoella
|
|
|
126
149
|
messages.compact
|
|
127
150
|
end
|
|
128
151
|
|
|
129
|
-
# A block closes into a Message when its joined
|
|
152
|
+
# A block closes into a Message when its joined patterns parse;
|
|
130
153
|
# a pattern that fails to parse is Junk — the entry is dropped
|
|
131
154
|
# and parsing continues.
|
|
132
155
|
def close_block(block)
|
|
@@ -136,22 +159,26 @@ module Rhoella
|
|
|
136
159
|
end
|
|
137
160
|
|
|
138
161
|
def build(block)
|
|
139
|
-
|
|
162
|
+
pattern = parse_section(block.value)
|
|
163
|
+
# An attributes-only message has no value (upstream NoValue).
|
|
164
|
+
pattern = nil if block.value.all? { |l| l.strip.empty? }
|
|
165
|
+
attributes = block.attrs.transform_values { |lines| parse_section(lines) }
|
|
166
|
+
Bundle::Message.new(id: block.id, pattern: pattern, attributes: attributes)
|
|
167
|
+
rescue Rhoella::Error
|
|
168
|
+
nil
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def parse_section(lines)
|
|
140
172
|
prefix = lines.reject(&:empty?)
|
|
141
173
|
.map { |l| l[/\A[ \t]+/, 0]&.length || 0 }.min || 0
|
|
142
174
|
stripped = lines.map { |l| l[prefix..] || "" }
|
|
143
175
|
stripped.shift while stripped.first&.strip&.empty? && stripped.size > 1
|
|
144
|
-
|
|
145
|
-
pattern = new(joined).parse_elements(stop: nil)
|
|
146
|
-
Bundle::Message.new(id: block.id, pattern: pattern)
|
|
147
|
-
rescue Rhoella::Error
|
|
148
|
-
nil
|
|
176
|
+
new(stripped.join("\n")).parse_elements(stop: nil)
|
|
149
177
|
end
|
|
150
178
|
|
|
151
179
|
def unbalanced?(block)
|
|
152
|
-
raw = (
|
|
180
|
+
raw = (block.value + block.attrs.values.flatten).join("\n")
|
|
153
181
|
open = 0
|
|
154
|
-
escaped_run = false
|
|
155
182
|
in_string = false
|
|
156
183
|
raw.each_char do |ch|
|
|
157
184
|
if in_string
|
|
@@ -168,7 +195,20 @@ module Rhoella
|
|
|
168
195
|
end
|
|
169
196
|
end
|
|
170
197
|
|
|
171
|
-
Block
|
|
198
|
+
class Block
|
|
199
|
+
attr_accessor :id, :value, :attrs, :current
|
|
200
|
+
|
|
201
|
+
def initialize(id:, value:, attrs:)
|
|
202
|
+
@id = id
|
|
203
|
+
@value = value
|
|
204
|
+
@attrs = attrs
|
|
205
|
+
@current = :value
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def current_section
|
|
209
|
+
@current == :value ? @value : @attrs[@current]
|
|
210
|
+
end
|
|
211
|
+
end
|
|
172
212
|
|
|
173
213
|
def initialize(raw)
|
|
174
214
|
@raw = raw
|
|
@@ -193,9 +233,6 @@ module Rhoella
|
|
|
193
233
|
buffer = +""
|
|
194
234
|
@pos += 1
|
|
195
235
|
elements << parse_placeable(stop)
|
|
196
|
-
elsif char == "\\" && @raw[@pos + 1] && "{}\\".include?(@raw[@pos + 1])
|
|
197
|
-
buffer << @raw[@pos + 1]
|
|
198
|
-
@pos += 2
|
|
199
236
|
else
|
|
200
237
|
buffer << char
|
|
201
238
|
@pos += 1
|
|
@@ -240,7 +277,9 @@ module Rhoella
|
|
|
240
277
|
out << (0x10000 + ((code - 0xD800) << 10) +
|
|
241
278
|
(units[i + 1] - 0xDC00)).chr(Encoding::UTF_8)
|
|
242
279
|
i += 2
|
|
243
|
-
elsif code.between?(0xD800, 0xDFFF)
|
|
280
|
+
elsif code.between?(0xD800, 0xDFFF) || code > 0x10FFFF
|
|
281
|
+
# Lone surrogates and out-of-range code points decode to
|
|
282
|
+
# U+FFFD (a raw \U escape can exceed the Unicode range).
|
|
244
283
|
out << "\u{FFFD}"
|
|
245
284
|
i += 1
|
|
246
285
|
else
|
|
@@ -251,6 +290,10 @@ module Rhoella
|
|
|
251
290
|
out
|
|
252
291
|
end
|
|
253
292
|
|
|
293
|
+
def self.decode_units(units)
|
|
294
|
+
new("").send(:decode_units, units)
|
|
295
|
+
end
|
|
296
|
+
|
|
254
297
|
private
|
|
255
298
|
|
|
256
299
|
def flush(buffer, elements)
|
|
@@ -261,15 +304,37 @@ module Rhoella
|
|
|
261
304
|
@pos += 1 while @raw[@pos] =~ /[ \t\n]/
|
|
262
305
|
end
|
|
263
306
|
|
|
307
|
+
# Placeable := "{" Expression "}"
|
|
264
308
|
def parse_placeable(stop)
|
|
265
309
|
skip_placeable_spaces
|
|
310
|
+
if @raw[@pos] == "{"
|
|
311
|
+
@pos += 1
|
|
312
|
+
inner = parse_placeable(stop)
|
|
313
|
+
skip_placeable_spaces
|
|
314
|
+
expect!("}")
|
|
315
|
+
return inner
|
|
316
|
+
end
|
|
266
317
|
if @raw[@pos] == '"'
|
|
267
|
-
|
|
318
|
+
element = parse_string_literal(skip_close: true)
|
|
319
|
+
skip_placeable_spaces
|
|
320
|
+
if @raw[@pos, 2] == "->"
|
|
321
|
+
@pos += 2
|
|
322
|
+
return parse_select(element, stop)
|
|
323
|
+
end
|
|
324
|
+
expect!("}")
|
|
325
|
+
return element
|
|
268
326
|
end
|
|
269
|
-
if @raw[@pos] =~ /[0-9-]/ && @raw[@pos..] =~ /\A-?[0-9]+(\.[0-9]+)
|
|
270
|
-
match = /\A(-?[0-9]+(?:\.[0-9]+)?)
|
|
327
|
+
if @raw[@pos] =~ /[0-9-]/ && @raw[@pos..] =~ /\A-?[0-9]+(\.[0-9]+)?/
|
|
328
|
+
match = /\A(-?[0-9]+(?:\.[0-9]+)?)/.match(@raw[@pos..])
|
|
271
329
|
@pos += match[0].length
|
|
272
|
-
|
|
330
|
+
element = [:text, match[1]]
|
|
331
|
+
skip_placeable_spaces
|
|
332
|
+
if @raw[@pos, 2] == "->"
|
|
333
|
+
@pos += 2
|
|
334
|
+
return parse_select(element, stop)
|
|
335
|
+
end
|
|
336
|
+
expect!("}")
|
|
337
|
+
return element
|
|
273
338
|
end
|
|
274
339
|
if @raw[@pos] == "$"
|
|
275
340
|
@pos += 1
|
|
@@ -277,25 +342,131 @@ module Rhoella
|
|
|
277
342
|
skip_placeable_spaces
|
|
278
343
|
if @raw[@pos, 2] == "->"
|
|
279
344
|
@pos += 2
|
|
280
|
-
return parse_select(name, stop)
|
|
345
|
+
return parse_select([:var, name], stop)
|
|
281
346
|
end
|
|
282
347
|
expect!("}")
|
|
283
348
|
[:var, name]
|
|
284
|
-
elsif
|
|
285
|
-
|
|
349
|
+
elsif (m = /\A([A-Za-z-][A-Za-z0-9_-]*)/.match(@raw[@pos..]))
|
|
350
|
+
name = m[1]
|
|
351
|
+
@pos += name.length
|
|
352
|
+
parse_reference(name, stop)
|
|
353
|
+
else
|
|
354
|
+
raise ParseError, "unsupported placeable at #{context}"
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# Reference expressions: message `{ id }`, member `{ id.attr }`,
|
|
359
|
+
# term `{ -term }` / `{ -term.attr }` (attributes only as
|
|
360
|
+
# selectors), functions `{ FN(...) }` (uppercase callee; any callee
|
|
361
|
+
# with parentheses and a lowercase/message/term-attr shape is Junk
|
|
362
|
+
# per the grammar), with optional call arguments on terms and
|
|
363
|
+
# functions.
|
|
364
|
+
def parse_reference(name, stop)
|
|
365
|
+
attribute = nil
|
|
366
|
+
if @raw[@pos] == "."
|
|
367
|
+
@pos += 1
|
|
368
|
+
attribute = ident!
|
|
369
|
+
end
|
|
370
|
+
skip_placeable_spaces
|
|
371
|
+
has_call = @raw[@pos] == "("
|
|
372
|
+
if name.start_with?("-")
|
|
373
|
+
# Term attributes may carry call arguments (upstream:
|
|
374
|
+
# {-ship.gender(style: "chicago") -> ...}); term values may not
|
|
375
|
+
# be selectors.
|
|
376
|
+
args = has_call ? parse_call_arguments : nil
|
|
377
|
+
skip_placeable_spaces
|
|
378
|
+
if @raw[@pos, 2] == "->"
|
|
379
|
+
raise ParseError, "term value may not be a selector" unless attribute
|
|
380
|
+
|
|
381
|
+
@pos += 2
|
|
382
|
+
return parse_select([:term_attr, name, attribute, args], stop)
|
|
383
|
+
end
|
|
384
|
+
expect!("}")
|
|
385
|
+
return attribute ? [:term_attr, name, attribute, args] : [:term_ref, name, args]
|
|
386
|
+
elsif UPPERCASE.match?(name)
|
|
387
|
+
unless has_call
|
|
388
|
+
raise ParseError, "function reference requires call arguments"
|
|
389
|
+
end
|
|
390
|
+
args = parse_call_arguments
|
|
286
391
|
skip_placeable_spaces
|
|
392
|
+
if @raw[@pos, 2] == "->"
|
|
393
|
+
@pos += 2
|
|
394
|
+
return parse_select([:func_ref, name, args], stop)
|
|
395
|
+
end
|
|
287
396
|
expect!("}")
|
|
288
|
-
[:
|
|
397
|
+
return [:func_ref, name, args]
|
|
289
398
|
else
|
|
290
|
-
|
|
399
|
+
# Message callees are Junk per the grammar.
|
|
400
|
+
raise ParseError, "message is not callable" if has_call
|
|
401
|
+
raise ParseError, "message attribute is not callable" if has_call && attribute
|
|
402
|
+
|
|
403
|
+
skip_placeable_spaces
|
|
404
|
+
if @raw[@pos, 2] == "->"
|
|
405
|
+
@pos += 2
|
|
406
|
+
element = attribute ? [:message_attr, name, attribute] : [:message_ref, name]
|
|
407
|
+
return parse_select(element, stop)
|
|
408
|
+
end
|
|
409
|
+
expect!("}")
|
|
410
|
+
return attribute ? [:message_attr, name, attribute] : [:message_ref, name]
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
# CallArguments := "(" (positional | named)* ")"
|
|
415
|
+
# Returns { named: {name => element}, positional: [elements] }.
|
|
416
|
+
def parse_call_arguments
|
|
417
|
+
expect!("(")
|
|
418
|
+
named = {}
|
|
419
|
+
positional = []
|
|
420
|
+
loop do
|
|
421
|
+
skip_placeable_spaces
|
|
422
|
+
break if @raw[@pos] == ")"
|
|
423
|
+
|
|
424
|
+
if (m = /\A([A-Za-z][A-Za-z0-9_-]*)[ \t]*:[ \t]*/.match(@raw[@pos..]))
|
|
425
|
+
@pos += m[0].length
|
|
426
|
+
named[m[1]] = parse_arg_expression
|
|
427
|
+
else
|
|
428
|
+
positional << parse_arg_expression
|
|
429
|
+
end
|
|
430
|
+
skip_placeable_spaces
|
|
431
|
+
if @raw[@pos] == ","
|
|
432
|
+
@pos += 1
|
|
433
|
+
elsif @raw[@pos] != ")"
|
|
434
|
+
raise ParseError, "expected , or ) in call arguments at #{context}"
|
|
435
|
+
end
|
|
436
|
+
end
|
|
437
|
+
expect!(")")
|
|
438
|
+
{ named: named, positional: positional }
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# Argument values: string/number literals, variables and
|
|
442
|
+
# references — parsed to the same element shape as placeables.
|
|
443
|
+
def parse_arg_expression
|
|
444
|
+
skip_placeable_spaces
|
|
445
|
+
if @raw[@pos] == '"'
|
|
446
|
+
return parse_string_literal(skip_close: true)
|
|
447
|
+
end
|
|
448
|
+
if @raw[@pos] =~ /[0-9-]/ && @raw[@pos..] =~ /\A-?[0-9]+(\.[0-9]+)?/
|
|
449
|
+
match = /\A(-?[0-9]+(?:\.[0-9]+)?)/.match(@raw[@pos..])
|
|
450
|
+
@pos += match[0].length
|
|
451
|
+
return [:text, match[1]]
|
|
452
|
+
end
|
|
453
|
+
if @raw[@pos] == "$"
|
|
454
|
+
@pos += 1
|
|
455
|
+
[:var, ident!]
|
|
456
|
+
elsif (m = /\A[A-Za-z-][A-Za-z0-9_-]*/.match(@raw[@pos..]))
|
|
457
|
+
@pos += m[0].length
|
|
458
|
+
[:message_ref, m[0]]
|
|
459
|
+
else
|
|
460
|
+
raise ParseError, "unsupported argument at #{context}"
|
|
291
461
|
end
|
|
292
462
|
end
|
|
293
463
|
|
|
294
464
|
# String literals decode as UTF-16 code units — Fluent semantics:
|
|
295
465
|
# a surrogate pair may straddle two adjacent string literals
|
|
296
466
|
# ({"\uD83D"}{"\uDE02"} is the astral emoji), so the literal keeps
|
|
297
|
-
# its units and adjacent text merges below.
|
|
298
|
-
|
|
467
|
+
# its units and adjacent text merges below. Escapes: \" \\ \uXXXX
|
|
468
|
+
# and \UXXXXXX (six hex digits).
|
|
469
|
+
def parse_string_literal(skip_close: false)
|
|
299
470
|
@pos += 1 # opening quote
|
|
300
471
|
units = []
|
|
301
472
|
loop do
|
|
@@ -312,6 +483,11 @@ module Rhoella
|
|
|
312
483
|
raise ParseError, "bad unicode escape" unless hex =~ /\A[0-9A-Fa-f]{4}\z/
|
|
313
484
|
units << hex.to_i(16)
|
|
314
485
|
@pos += 6
|
|
486
|
+
elsif esc == "U"
|
|
487
|
+
hex = @raw[@pos + 2, 6]
|
|
488
|
+
raise ParseError, "bad unicode escape" unless hex =~ /\A[0-9A-Fa-f]{6}\z/
|
|
489
|
+
units << hex.to_i(16)
|
|
490
|
+
@pos += 8
|
|
315
491
|
elsif @raw[@pos + 1] && "{}\\\"".include?(esc)
|
|
316
492
|
units << esc.ord
|
|
317
493
|
@pos += 2
|
|
@@ -324,11 +500,13 @@ module Rhoella
|
|
|
324
500
|
end
|
|
325
501
|
end
|
|
326
502
|
skip_placeable_spaces
|
|
327
|
-
expect!("}")
|
|
503
|
+
expect!("}") unless skip_close
|
|
328
504
|
[:units, units]
|
|
329
505
|
end
|
|
330
506
|
|
|
331
|
-
|
|
507
|
+
# SelectExpression := expression "->" variants; the default arm is
|
|
508
|
+
# mandatory. Variant keys are matched as raw strings.
|
|
509
|
+
def parse_select(selector, _stop)
|
|
332
510
|
skip_placeable_spaces
|
|
333
511
|
variants = []
|
|
334
512
|
default = nil
|
|
@@ -355,7 +533,7 @@ module Rhoella
|
|
|
355
533
|
end
|
|
356
534
|
raise ParseError, "selector without default arm" if default.nil?
|
|
357
535
|
|
|
358
|
-
[:select,
|
|
536
|
+
[:select, selector, variants, default]
|
|
359
537
|
end
|
|
360
538
|
|
|
361
539
|
def ident!
|
|
@@ -377,45 +555,151 @@ module Rhoella
|
|
|
377
555
|
end
|
|
378
556
|
end
|
|
379
557
|
|
|
558
|
+
# `messages` maps message ids to Bundle::Message-like pairs
|
|
559
|
+
# [pattern_elements, {attr => elements}]; `Bundle.format` provides
|
|
560
|
+
# its own resources to it, and `Rhoella.format` wraps ad-hoc
|
|
561
|
+
# catalog values in the same shape.
|
|
562
|
+
# Budget bounds the number of placeable resolutions per top-level
|
|
563
|
+
# format — the fluent-rs TooManyPlaceables defense (bomb.yaml).
|
|
564
|
+
class Budget
|
|
565
|
+
LIMIT = 1000
|
|
566
|
+
|
|
567
|
+
def initialize
|
|
568
|
+
@spent = 0
|
|
569
|
+
end
|
|
570
|
+
|
|
571
|
+
def spend
|
|
572
|
+
@spent += 1
|
|
573
|
+
raise ResolutionError, "too many placeables" if @spent > LIMIT
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
|
|
380
577
|
class Formatter
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
def initialize(messages, vars)
|
|
578
|
+
attr_reader :budget
|
|
579
|
+
|
|
580
|
+
def initialize(messages, vars, budget = nil)
|
|
384
581
|
@messages = messages
|
|
385
582
|
@vars = vars || {}
|
|
583
|
+
@budget = budget || Budget.new
|
|
386
584
|
end
|
|
387
585
|
|
|
388
|
-
def format_pattern(elements,
|
|
389
|
-
raise ResolutionError, "message nesting too deep" if depth > Bundle::MAX_DEPTH
|
|
390
|
-
|
|
586
|
+
def format_pattern(elements, stack)
|
|
391
587
|
elements.map do |element|
|
|
392
588
|
case element[0]
|
|
393
589
|
when :text then element[1]
|
|
590
|
+
when :units then decode_units(element[1])
|
|
394
591
|
when :var then format_var(element[1])
|
|
395
|
-
when :
|
|
396
|
-
when :
|
|
592
|
+
when :message_ref then format_ref(element[1], stack)
|
|
593
|
+
when :message_attr then format_attr_ref(element[1], element[2], stack)
|
|
594
|
+
when :term_ref then format_term(element[1], element[2], stack)
|
|
595
|
+
when :term_attr then format_term_attr(element[1], element[2], element[3], stack)
|
|
596
|
+
when :func_ref then format_function(element[1], element[2])
|
|
597
|
+
when :select then format_select(element, stack)
|
|
397
598
|
end
|
|
398
599
|
end.join
|
|
399
600
|
end
|
|
400
601
|
|
|
401
602
|
private
|
|
402
603
|
|
|
604
|
+
def decode_units(units)
|
|
605
|
+
Parser.decode_units(units)
|
|
606
|
+
end
|
|
607
|
+
|
|
608
|
+
def entry_pattern(entry)
|
|
609
|
+
entry.is_a?(Bundle::Message) ? entry.pattern : entry[0]
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
def entry_attrs(entry)
|
|
613
|
+
entry.is_a?(Bundle::Message) ? entry.attributes : entry[1]
|
|
614
|
+
end
|
|
615
|
+
|
|
403
616
|
def format_var(name)
|
|
617
|
+
@budget.spend
|
|
404
618
|
value = @vars[name]
|
|
405
|
-
value.nil? ? "{
|
|
619
|
+
value.nil? ? "{$#{name}}" : value.to_s
|
|
620
|
+
end
|
|
621
|
+
|
|
622
|
+
def format_ref(id, stack)
|
|
623
|
+
@budget.spend
|
|
624
|
+
entry = @messages[id]
|
|
625
|
+
return "{#{id}}" unless entry
|
|
626
|
+
return "{#{id}}" if stack.include?(id)
|
|
627
|
+
return "{#{id}}" if entry_pattern(entry).nil?
|
|
628
|
+
|
|
629
|
+
format_pattern(entry_pattern(entry), stack + [id])
|
|
630
|
+
end
|
|
631
|
+
|
|
632
|
+
def format_attr_ref(id, attr, stack)
|
|
633
|
+
@budget.spend
|
|
634
|
+
entry = @messages[id]
|
|
635
|
+
return "{#{id}.#{attr}}" unless entry
|
|
636
|
+
|
|
637
|
+
cycle_key = "#{id}.#{attr}"
|
|
638
|
+
return "{#{cycle_key}}" if stack.include?(cycle_key)
|
|
639
|
+
|
|
640
|
+
elements = entry_attrs(entry)[attr]
|
|
641
|
+
return "{#{cycle_key}}" unless elements
|
|
642
|
+
|
|
643
|
+
format_pattern(elements, stack + [cycle_key])
|
|
406
644
|
end
|
|
407
645
|
|
|
408
|
-
def
|
|
409
|
-
|
|
410
|
-
|
|
646
|
+
def format_term(id, args, stack)
|
|
647
|
+
@budget.spend
|
|
648
|
+
entry = @messages[id]
|
|
649
|
+
return "{#{id}}" unless entry
|
|
650
|
+
return "{#{id}}" if stack.include?(id)
|
|
651
|
+
return "{#{id}}" if entry_pattern(entry).nil?
|
|
652
|
+
|
|
653
|
+
# Term parameters bind WITHOUT the outer variables: upstream
|
|
654
|
+
# semantics — externals are not inherited into term resolution,
|
|
655
|
+
# only the call arguments are visible inside.
|
|
656
|
+
named = args ? args[:named] : {}
|
|
657
|
+
bound = named.transform_values { |element| render_arg(element, stack) }
|
|
658
|
+
Formatter.new(@messages, bound, @budget)
|
|
659
|
+
.format_pattern(entry_pattern(entry), stack + [id])
|
|
660
|
+
end
|
|
661
|
+
|
|
662
|
+
def format_term_attr(id, attr, args, stack)
|
|
663
|
+
@budget.spend
|
|
664
|
+
entry = @messages[id]
|
|
665
|
+
return "{#{id}.#{attr}}" unless entry
|
|
666
|
+
|
|
667
|
+
elements = entry_attrs(entry)[attr]
|
|
668
|
+
cycle_key = "#{id}.#{attr}"
|
|
669
|
+
return "{#{cycle_key}}" unless elements
|
|
670
|
+
return "{#{cycle_key}}" if stack.include?(cycle_key)
|
|
671
|
+
|
|
672
|
+
# Term-attribute parameters bind without outer variables, like
|
|
673
|
+
# term references (upstream: {-ship.gender(style: "chicago")}).
|
|
674
|
+
named = args ? args[:named] : {}
|
|
675
|
+
bound = named.transform_values { |element| render_arg(element, stack) }
|
|
676
|
+
Formatter.new(@messages, bound, @budget)
|
|
677
|
+
.format_pattern(elements, stack + [cycle_key])
|
|
678
|
+
end
|
|
411
679
|
|
|
412
|
-
|
|
680
|
+
# A minimal engine registers no builtins: an unknown function
|
|
681
|
+
# renders its name and empty parentheses, per the upstream
|
|
682
|
+
# fallback shape ({MISSING()}, arguments not shown).
|
|
683
|
+
def format_function(_name, _args)
|
|
684
|
+
"{#{_name}()}"
|
|
413
685
|
end
|
|
414
686
|
|
|
415
|
-
def format_select((_tag,
|
|
416
|
-
|
|
417
|
-
arm = variants.find { |(key, _)| key ==
|
|
418
|
-
format_pattern(arm ? arm[1] : default,
|
|
687
|
+
def format_select((_tag, selector, variants, default), stack)
|
|
688
|
+
value = render_arg(selector, stack)
|
|
689
|
+
arm = variants.find { |(key, _)| key == value }
|
|
690
|
+
format_pattern(arm ? arm[1] : default, stack)
|
|
691
|
+
end
|
|
692
|
+
|
|
693
|
+
def render_arg(element, stack)
|
|
694
|
+
case element[0]
|
|
695
|
+
when :var then format_var(element[1])
|
|
696
|
+
when :text then element[1]
|
|
697
|
+
when :message_ref
|
|
698
|
+
entry = @messages[element[1]]
|
|
699
|
+
entry ? format_pattern(entry_pattern(entry), stack + [element[1]]) : "{#{element[1]}}"
|
|
700
|
+
else
|
|
701
|
+
format_pattern([element], stack)
|
|
702
|
+
end
|
|
419
703
|
end
|
|
420
704
|
end
|
|
421
705
|
end
|