rhoella 0.1.0 → 0.1.2
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 +391 -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: f4268ebe7c3d7f85cd4d23ccc5df3229ded41799f8e09b0e0b9113186520cfb2
|
|
4
|
+
data.tar.gz: 232c9978bd4c082c80274ca1711cbfba525060e22f3d6eb779a8186cdfa56131
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ae894840496f0c5f2c7e387b182332aed5c510d398d3b208e98346ffc55d045ae2d0439e341bfbb5eed6531e45b9cf16526b8a2565d15941bfddfe607cc8e01e
|
|
7
|
+
data.tar.gz: 0e31d8b619ef7412ec99d1b6a7d41cd1b0402b892ae4fa523e6ecd45fc1cea07972e44f7201868c0c559dd256637582ba5f32508fb570f699c537f6210e0e7ff
|
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,13 +195,51 @@ 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
|
|
175
215
|
@pos = 0
|
|
176
216
|
end
|
|
177
217
|
|
|
218
|
+
# Static analysis: the message and term ids this pattern references
|
|
219
|
+
# (message ids bare, term ids with their leading dash, member
|
|
220
|
+
# attributes stripped), including selectors. Strict hosts walk this
|
|
221
|
+
# against their catalog to fail loudly on unknown references — the
|
|
222
|
+
# engine itself renders unknown references softly, per the upstream
|
|
223
|
+
# fallback fixtures.
|
|
224
|
+
def references
|
|
225
|
+
refs = []
|
|
226
|
+
walk_references = lambda do |elements|
|
|
227
|
+
elements.each do |element|
|
|
228
|
+
case element[0]
|
|
229
|
+
when :message_ref then refs << element[1]
|
|
230
|
+
when :message_attr, :term_attr then refs << element[1]
|
|
231
|
+
when :term_ref then refs << element[1]
|
|
232
|
+
when :select
|
|
233
|
+
walk_references.call([element[1]])
|
|
234
|
+
element[2].each { |(_, arm_elements)| walk_references.call(arm_elements) }
|
|
235
|
+
walk_references.call(element[3])
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
walk_references.call(parse_elements(stop: nil))
|
|
240
|
+
refs.uniq
|
|
241
|
+
end
|
|
242
|
+
|
|
178
243
|
def parse_elements(stop:)
|
|
179
244
|
elements = []
|
|
180
245
|
buffer = +""
|
|
@@ -193,9 +258,6 @@ module Rhoella
|
|
|
193
258
|
buffer = +""
|
|
194
259
|
@pos += 1
|
|
195
260
|
elements << parse_placeable(stop)
|
|
196
|
-
elsif char == "\\" && @raw[@pos + 1] && "{}\\".include?(@raw[@pos + 1])
|
|
197
|
-
buffer << @raw[@pos + 1]
|
|
198
|
-
@pos += 2
|
|
199
261
|
else
|
|
200
262
|
buffer << char
|
|
201
263
|
@pos += 1
|
|
@@ -240,7 +302,9 @@ module Rhoella
|
|
|
240
302
|
out << (0x10000 + ((code - 0xD800) << 10) +
|
|
241
303
|
(units[i + 1] - 0xDC00)).chr(Encoding::UTF_8)
|
|
242
304
|
i += 2
|
|
243
|
-
elsif code.between?(0xD800, 0xDFFF)
|
|
305
|
+
elsif code.between?(0xD800, 0xDFFF) || code > 0x10FFFF
|
|
306
|
+
# Lone surrogates and out-of-range code points decode to
|
|
307
|
+
# U+FFFD (a raw \U escape can exceed the Unicode range).
|
|
244
308
|
out << "\u{FFFD}"
|
|
245
309
|
i += 1
|
|
246
310
|
else
|
|
@@ -251,6 +315,10 @@ module Rhoella
|
|
|
251
315
|
out
|
|
252
316
|
end
|
|
253
317
|
|
|
318
|
+
def self.decode_units(units)
|
|
319
|
+
new("").send(:decode_units, units)
|
|
320
|
+
end
|
|
321
|
+
|
|
254
322
|
private
|
|
255
323
|
|
|
256
324
|
def flush(buffer, elements)
|
|
@@ -261,15 +329,37 @@ module Rhoella
|
|
|
261
329
|
@pos += 1 while @raw[@pos] =~ /[ \t\n]/
|
|
262
330
|
end
|
|
263
331
|
|
|
332
|
+
# Placeable := "{" Expression "}"
|
|
264
333
|
def parse_placeable(stop)
|
|
265
334
|
skip_placeable_spaces
|
|
335
|
+
if @raw[@pos] == "{"
|
|
336
|
+
@pos += 1
|
|
337
|
+
inner = parse_placeable(stop)
|
|
338
|
+
skip_placeable_spaces
|
|
339
|
+
expect!("}")
|
|
340
|
+
return inner
|
|
341
|
+
end
|
|
266
342
|
if @raw[@pos] == '"'
|
|
267
|
-
|
|
343
|
+
element = parse_string_literal(skip_close: true)
|
|
344
|
+
skip_placeable_spaces
|
|
345
|
+
if @raw[@pos, 2] == "->"
|
|
346
|
+
@pos += 2
|
|
347
|
+
return parse_select(element, stop)
|
|
348
|
+
end
|
|
349
|
+
expect!("}")
|
|
350
|
+
return element
|
|
268
351
|
end
|
|
269
|
-
if @raw[@pos] =~ /[0-9-]/ && @raw[@pos..] =~ /\A-?[0-9]+(\.[0-9]+)
|
|
270
|
-
match = /\A(-?[0-9]+(?:\.[0-9]+)?)
|
|
352
|
+
if @raw[@pos] =~ /[0-9-]/ && @raw[@pos..] =~ /\A-?[0-9]+(\.[0-9]+)?/
|
|
353
|
+
match = /\A(-?[0-9]+(?:\.[0-9]+)?)/.match(@raw[@pos..])
|
|
271
354
|
@pos += match[0].length
|
|
272
|
-
|
|
355
|
+
element = [:text, match[1]]
|
|
356
|
+
skip_placeable_spaces
|
|
357
|
+
if @raw[@pos, 2] == "->"
|
|
358
|
+
@pos += 2
|
|
359
|
+
return parse_select(element, stop)
|
|
360
|
+
end
|
|
361
|
+
expect!("}")
|
|
362
|
+
return element
|
|
273
363
|
end
|
|
274
364
|
if @raw[@pos] == "$"
|
|
275
365
|
@pos += 1
|
|
@@ -277,25 +367,131 @@ module Rhoella
|
|
|
277
367
|
skip_placeable_spaces
|
|
278
368
|
if @raw[@pos, 2] == "->"
|
|
279
369
|
@pos += 2
|
|
280
|
-
return parse_select(name, stop)
|
|
370
|
+
return parse_select([:var, name], stop)
|
|
281
371
|
end
|
|
282
372
|
expect!("}")
|
|
283
373
|
[:var, name]
|
|
284
|
-
elsif
|
|
285
|
-
|
|
374
|
+
elsif (m = /\A([A-Za-z-][A-Za-z0-9_-]*)/.match(@raw[@pos..]))
|
|
375
|
+
name = m[1]
|
|
376
|
+
@pos += name.length
|
|
377
|
+
parse_reference(name, stop)
|
|
378
|
+
else
|
|
379
|
+
raise ParseError, "unsupported placeable at #{context}"
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# Reference expressions: message `{ id }`, member `{ id.attr }`,
|
|
384
|
+
# term `{ -term }` / `{ -term.attr }` (attributes only as
|
|
385
|
+
# selectors), functions `{ FN(...) }` (uppercase callee; any callee
|
|
386
|
+
# with parentheses and a lowercase/message/term-attr shape is Junk
|
|
387
|
+
# per the grammar), with optional call arguments on terms and
|
|
388
|
+
# functions.
|
|
389
|
+
def parse_reference(name, stop)
|
|
390
|
+
attribute = nil
|
|
391
|
+
if @raw[@pos] == "."
|
|
392
|
+
@pos += 1
|
|
393
|
+
attribute = ident!
|
|
394
|
+
end
|
|
395
|
+
skip_placeable_spaces
|
|
396
|
+
has_call = @raw[@pos] == "("
|
|
397
|
+
if name.start_with?("-")
|
|
398
|
+
# Term attributes may carry call arguments (upstream:
|
|
399
|
+
# {-ship.gender(style: "chicago") -> ...}); term values may not
|
|
400
|
+
# be selectors.
|
|
401
|
+
args = has_call ? parse_call_arguments : nil
|
|
402
|
+
skip_placeable_spaces
|
|
403
|
+
if @raw[@pos, 2] == "->"
|
|
404
|
+
raise ParseError, "term value may not be a selector" unless attribute
|
|
405
|
+
|
|
406
|
+
@pos += 2
|
|
407
|
+
return parse_select([:term_attr, name, attribute, args], stop)
|
|
408
|
+
end
|
|
409
|
+
expect!("}")
|
|
410
|
+
return attribute ? [:term_attr, name, attribute, args] : [:term_ref, name, args]
|
|
411
|
+
elsif UPPERCASE.match?(name)
|
|
412
|
+
unless has_call
|
|
413
|
+
raise ParseError, "function reference requires call arguments"
|
|
414
|
+
end
|
|
415
|
+
args = parse_call_arguments
|
|
286
416
|
skip_placeable_spaces
|
|
417
|
+
if @raw[@pos, 2] == "->"
|
|
418
|
+
@pos += 2
|
|
419
|
+
return parse_select([:func_ref, name, args], stop)
|
|
420
|
+
end
|
|
287
421
|
expect!("}")
|
|
288
|
-
[:
|
|
422
|
+
return [:func_ref, name, args]
|
|
289
423
|
else
|
|
290
|
-
|
|
424
|
+
# Message callees are Junk per the grammar.
|
|
425
|
+
raise ParseError, "message is not callable" if has_call
|
|
426
|
+
raise ParseError, "message attribute is not callable" if has_call && attribute
|
|
427
|
+
|
|
428
|
+
skip_placeable_spaces
|
|
429
|
+
if @raw[@pos, 2] == "->"
|
|
430
|
+
@pos += 2
|
|
431
|
+
element = attribute ? [:message_attr, name, attribute] : [:message_ref, name]
|
|
432
|
+
return parse_select(element, stop)
|
|
433
|
+
end
|
|
434
|
+
expect!("}")
|
|
435
|
+
return attribute ? [:message_attr, name, attribute] : [:message_ref, name]
|
|
436
|
+
end
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
# CallArguments := "(" (positional | named)* ")"
|
|
440
|
+
# Returns { named: {name => element}, positional: [elements] }.
|
|
441
|
+
def parse_call_arguments
|
|
442
|
+
expect!("(")
|
|
443
|
+
named = {}
|
|
444
|
+
positional = []
|
|
445
|
+
loop do
|
|
446
|
+
skip_placeable_spaces
|
|
447
|
+
break if @raw[@pos] == ")"
|
|
448
|
+
|
|
449
|
+
if (m = /\A([A-Za-z][A-Za-z0-9_-]*)[ \t]*:[ \t]*/.match(@raw[@pos..]))
|
|
450
|
+
@pos += m[0].length
|
|
451
|
+
named[m[1]] = parse_arg_expression
|
|
452
|
+
else
|
|
453
|
+
positional << parse_arg_expression
|
|
454
|
+
end
|
|
455
|
+
skip_placeable_spaces
|
|
456
|
+
if @raw[@pos] == ","
|
|
457
|
+
@pos += 1
|
|
458
|
+
elsif @raw[@pos] != ")"
|
|
459
|
+
raise ParseError, "expected , or ) in call arguments at #{context}"
|
|
460
|
+
end
|
|
461
|
+
end
|
|
462
|
+
expect!(")")
|
|
463
|
+
{ named: named, positional: positional }
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# Argument values: string/number literals, variables and
|
|
467
|
+
# references — parsed to the same element shape as placeables.
|
|
468
|
+
def parse_arg_expression
|
|
469
|
+
skip_placeable_spaces
|
|
470
|
+
if @raw[@pos] == '"'
|
|
471
|
+
return parse_string_literal(skip_close: true)
|
|
472
|
+
end
|
|
473
|
+
if @raw[@pos] =~ /[0-9-]/ && @raw[@pos..] =~ /\A-?[0-9]+(\.[0-9]+)?/
|
|
474
|
+
match = /\A(-?[0-9]+(?:\.[0-9]+)?)/.match(@raw[@pos..])
|
|
475
|
+
@pos += match[0].length
|
|
476
|
+
return [:text, match[1]]
|
|
477
|
+
end
|
|
478
|
+
if @raw[@pos] == "$"
|
|
479
|
+
@pos += 1
|
|
480
|
+
[:var, ident!]
|
|
481
|
+
elsif (m = /\A[A-Za-z-][A-Za-z0-9_-]*/.match(@raw[@pos..]))
|
|
482
|
+
@pos += m[0].length
|
|
483
|
+
[:message_ref, m[0]]
|
|
484
|
+
else
|
|
485
|
+
raise ParseError, "unsupported argument at #{context}"
|
|
291
486
|
end
|
|
292
487
|
end
|
|
293
488
|
|
|
294
489
|
# String literals decode as UTF-16 code units — Fluent semantics:
|
|
295
490
|
# a surrogate pair may straddle two adjacent string literals
|
|
296
491
|
# ({"\uD83D"}{"\uDE02"} is the astral emoji), so the literal keeps
|
|
297
|
-
# its units and adjacent text merges below.
|
|
298
|
-
|
|
492
|
+
# its units and adjacent text merges below. Escapes: \" \\ \uXXXX
|
|
493
|
+
# and \UXXXXXX (six hex digits).
|
|
494
|
+
def parse_string_literal(skip_close: false)
|
|
299
495
|
@pos += 1 # opening quote
|
|
300
496
|
units = []
|
|
301
497
|
loop do
|
|
@@ -312,6 +508,11 @@ module Rhoella
|
|
|
312
508
|
raise ParseError, "bad unicode escape" unless hex =~ /\A[0-9A-Fa-f]{4}\z/
|
|
313
509
|
units << hex.to_i(16)
|
|
314
510
|
@pos += 6
|
|
511
|
+
elsif esc == "U"
|
|
512
|
+
hex = @raw[@pos + 2, 6]
|
|
513
|
+
raise ParseError, "bad unicode escape" unless hex =~ /\A[0-9A-Fa-f]{6}\z/
|
|
514
|
+
units << hex.to_i(16)
|
|
515
|
+
@pos += 8
|
|
315
516
|
elsif @raw[@pos + 1] && "{}\\\"".include?(esc)
|
|
316
517
|
units << esc.ord
|
|
317
518
|
@pos += 2
|
|
@@ -324,11 +525,13 @@ module Rhoella
|
|
|
324
525
|
end
|
|
325
526
|
end
|
|
326
527
|
skip_placeable_spaces
|
|
327
|
-
expect!("}")
|
|
528
|
+
expect!("}") unless skip_close
|
|
328
529
|
[:units, units]
|
|
329
530
|
end
|
|
330
531
|
|
|
331
|
-
|
|
532
|
+
# SelectExpression := expression "->" variants; the default arm is
|
|
533
|
+
# mandatory. Variant keys are matched as raw strings.
|
|
534
|
+
def parse_select(selector, _stop)
|
|
332
535
|
skip_placeable_spaces
|
|
333
536
|
variants = []
|
|
334
537
|
default = nil
|
|
@@ -355,7 +558,7 @@ module Rhoella
|
|
|
355
558
|
end
|
|
356
559
|
raise ParseError, "selector without default arm" if default.nil?
|
|
357
560
|
|
|
358
|
-
[:select,
|
|
561
|
+
[:select, selector, variants, default]
|
|
359
562
|
end
|
|
360
563
|
|
|
361
564
|
def ident!
|
|
@@ -377,45 +580,151 @@ module Rhoella
|
|
|
377
580
|
end
|
|
378
581
|
end
|
|
379
582
|
|
|
583
|
+
# `messages` maps message ids to Bundle::Message-like pairs
|
|
584
|
+
# [pattern_elements, {attr => elements}]; `Bundle.format` provides
|
|
585
|
+
# its own resources to it, and `Rhoella.format` wraps ad-hoc
|
|
586
|
+
# catalog values in the same shape.
|
|
587
|
+
# Budget bounds the number of placeable resolutions per top-level
|
|
588
|
+
# format — the fluent-rs TooManyPlaceables defense (bomb.yaml).
|
|
589
|
+
class Budget
|
|
590
|
+
LIMIT = 1000
|
|
591
|
+
|
|
592
|
+
def initialize
|
|
593
|
+
@spent = 0
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
def spend
|
|
597
|
+
@spent += 1
|
|
598
|
+
raise ResolutionError, "too many placeables" if @spent > LIMIT
|
|
599
|
+
end
|
|
600
|
+
end
|
|
601
|
+
|
|
380
602
|
class Formatter
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
def initialize(messages, vars)
|
|
603
|
+
attr_reader :budget
|
|
604
|
+
|
|
605
|
+
def initialize(messages, vars, budget = nil)
|
|
384
606
|
@messages = messages
|
|
385
607
|
@vars = vars || {}
|
|
608
|
+
@budget = budget || Budget.new
|
|
386
609
|
end
|
|
387
610
|
|
|
388
|
-
def format_pattern(elements,
|
|
389
|
-
raise ResolutionError, "message nesting too deep" if depth > Bundle::MAX_DEPTH
|
|
390
|
-
|
|
611
|
+
def format_pattern(elements, stack)
|
|
391
612
|
elements.map do |element|
|
|
392
613
|
case element[0]
|
|
393
614
|
when :text then element[1]
|
|
615
|
+
when :units then decode_units(element[1])
|
|
394
616
|
when :var then format_var(element[1])
|
|
395
|
-
when :
|
|
396
|
-
when :
|
|
617
|
+
when :message_ref then format_ref(element[1], stack)
|
|
618
|
+
when :message_attr then format_attr_ref(element[1], element[2], stack)
|
|
619
|
+
when :term_ref then format_term(element[1], element[2], stack)
|
|
620
|
+
when :term_attr then format_term_attr(element[1], element[2], element[3], stack)
|
|
621
|
+
when :func_ref then format_function(element[1], element[2])
|
|
622
|
+
when :select then format_select(element, stack)
|
|
397
623
|
end
|
|
398
624
|
end.join
|
|
399
625
|
end
|
|
400
626
|
|
|
401
627
|
private
|
|
402
628
|
|
|
629
|
+
def decode_units(units)
|
|
630
|
+
Parser.decode_units(units)
|
|
631
|
+
end
|
|
632
|
+
|
|
633
|
+
def entry_pattern(entry)
|
|
634
|
+
entry.is_a?(Bundle::Message) ? entry.pattern : entry[0]
|
|
635
|
+
end
|
|
636
|
+
|
|
637
|
+
def entry_attrs(entry)
|
|
638
|
+
entry.is_a?(Bundle::Message) ? entry.attributes : entry[1]
|
|
639
|
+
end
|
|
640
|
+
|
|
403
641
|
def format_var(name)
|
|
642
|
+
@budget.spend
|
|
404
643
|
value = @vars[name]
|
|
405
|
-
value.nil? ? "{
|
|
644
|
+
value.nil? ? "{$#{name}}" : value.to_s
|
|
406
645
|
end
|
|
407
646
|
|
|
408
|
-
def format_ref(id,
|
|
409
|
-
|
|
410
|
-
|
|
647
|
+
def format_ref(id, stack)
|
|
648
|
+
@budget.spend
|
|
649
|
+
entry = @messages[id]
|
|
650
|
+
return "{#{id}}" unless entry
|
|
651
|
+
return "{#{id}}" if stack.include?(id)
|
|
652
|
+
return "{#{id}}" if entry_pattern(entry).nil?
|
|
411
653
|
|
|
412
|
-
format_pattern(
|
|
654
|
+
format_pattern(entry_pattern(entry), stack + [id])
|
|
413
655
|
end
|
|
414
656
|
|
|
415
|
-
def
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
657
|
+
def format_attr_ref(id, attr, stack)
|
|
658
|
+
@budget.spend
|
|
659
|
+
entry = @messages[id]
|
|
660
|
+
return "{#{id}.#{attr}}" unless entry
|
|
661
|
+
|
|
662
|
+
cycle_key = "#{id}.#{attr}"
|
|
663
|
+
return "{#{cycle_key}}" if stack.include?(cycle_key)
|
|
664
|
+
|
|
665
|
+
elements = entry_attrs(entry)[attr]
|
|
666
|
+
return "{#{cycle_key}}" unless elements
|
|
667
|
+
|
|
668
|
+
format_pattern(elements, stack + [cycle_key])
|
|
669
|
+
end
|
|
670
|
+
|
|
671
|
+
def format_term(id, args, stack)
|
|
672
|
+
@budget.spend
|
|
673
|
+
entry = @messages[id]
|
|
674
|
+
return "{#{id}}" unless entry
|
|
675
|
+
return "{#{id}}" if stack.include?(id)
|
|
676
|
+
return "{#{id}}" if entry_pattern(entry).nil?
|
|
677
|
+
|
|
678
|
+
# Term parameters bind WITHOUT the outer variables: upstream
|
|
679
|
+
# semantics — externals are not inherited into term resolution,
|
|
680
|
+
# only the call arguments are visible inside.
|
|
681
|
+
named = args ? args[:named] : {}
|
|
682
|
+
bound = named.transform_values { |element| render_arg(element, stack) }
|
|
683
|
+
Formatter.new(@messages, bound, @budget)
|
|
684
|
+
.format_pattern(entry_pattern(entry), stack + [id])
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
def format_term_attr(id, attr, args, stack)
|
|
688
|
+
@budget.spend
|
|
689
|
+
entry = @messages[id]
|
|
690
|
+
return "{#{id}.#{attr}}" unless entry
|
|
691
|
+
|
|
692
|
+
elements = entry_attrs(entry)[attr]
|
|
693
|
+
cycle_key = "#{id}.#{attr}"
|
|
694
|
+
return "{#{cycle_key}}" unless elements
|
|
695
|
+
return "{#{cycle_key}}" if stack.include?(cycle_key)
|
|
696
|
+
|
|
697
|
+
# Term-attribute parameters bind without outer variables, like
|
|
698
|
+
# term references (upstream: {-ship.gender(style: "chicago")}).
|
|
699
|
+
named = args ? args[:named] : {}
|
|
700
|
+
bound = named.transform_values { |element| render_arg(element, stack) }
|
|
701
|
+
Formatter.new(@messages, bound, @budget)
|
|
702
|
+
.format_pattern(elements, stack + [cycle_key])
|
|
703
|
+
end
|
|
704
|
+
|
|
705
|
+
# A minimal engine registers no builtins: an unknown function
|
|
706
|
+
# renders its name and empty parentheses, per the upstream
|
|
707
|
+
# fallback shape ({MISSING()}, arguments not shown).
|
|
708
|
+
def format_function(_name, _args)
|
|
709
|
+
"{#{_name}()}"
|
|
710
|
+
end
|
|
711
|
+
|
|
712
|
+
def format_select((_tag, selector, variants, default), stack)
|
|
713
|
+
value = render_arg(selector, stack)
|
|
714
|
+
arm = variants.find { |(key, _)| key == value }
|
|
715
|
+
format_pattern(arm ? arm[1] : default, stack)
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
def render_arg(element, stack)
|
|
719
|
+
case element[0]
|
|
720
|
+
when :var then format_var(element[1])
|
|
721
|
+
when :text then element[1]
|
|
722
|
+
when :message_ref
|
|
723
|
+
entry = @messages[element[1]]
|
|
724
|
+
entry ? format_pattern(entry_pattern(entry), stack + [element[1]]) : "{#{element[1]}}"
|
|
725
|
+
else
|
|
726
|
+
format_pattern([element], stack)
|
|
727
|
+
end
|
|
419
728
|
end
|
|
420
729
|
end
|
|
421
730
|
end
|