ladl 0.1.0

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.
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # MN 112 "Label context condition" — a context condition for a labeller
5
+ # to determine which label template to use, based on the number of
6
+ # similar elements within the same label context.
7
+ #
8
+ # Condition types: `element_count_gt`, `element_count_eq`,
9
+ # `element_count_lt`.
10
+ class LabelContextCondition < Lutaml::Model::Serializable
11
+ attribute :type, :string
12
+ attribute :threshold, :integer
13
+
14
+ hsh do
15
+ map "type", to: :type
16
+ map "threshold", to: :threshold
17
+ end
18
+
19
+ # Evaluates the condition against the element count of similar
20
+ # elements within the same label context.
21
+ #
22
+ # @param count [Integer] the total number of elements labeled by the
23
+ # same labeller within the same context instance
24
+ # @return [Boolean]
25
+ # @raise [Ladl::Error] if the condition type is unknown
26
+ def evaluate(count)
27
+ case type
28
+ when "element_count_gt"
29
+ count > threshold
30
+ when "element_count_eq"
31
+ count == threshold
32
+ when "element_count_lt"
33
+ count < threshold
34
+ else
35
+ raise Ladl::Error, "unknown label context condition type: #{type.inspect}"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # MN 112 "Label style" — a full set of label models sufficient to
5
+ # implement a coherent labeling system for a publisher.
6
+ #
7
+ # The collection attributes hold references to the instances bound to
8
+ # this style, exactly as written in the `.lml` profile: labellers are
9
+ # referenced by id (`"id:clause_labeller"`), the other collections by
10
+ # instance name (`"ClauseLabelTemplate"`).
11
+ class LabelStyle < Lutaml::Model::Serializable
12
+ attribute :name, :string
13
+ attribute :definition, :string
14
+ attribute :labellers, :string, collection: true
15
+ attribute :sequences, :string, collection: true
16
+ attribute :templates, :string, collection: true
17
+ attribute :contexts, :string, collection: true
18
+
19
+ hsh do
20
+ map "name", to: :name
21
+ map "definition", to: :definition
22
+ map "labellers", to: :labellers
23
+ map "sequences", to: :sequences
24
+ map "templates", to: :templates
25
+ map "contexts", to: :contexts
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # MN 112 "Label template" — defines how string labels are constructed
5
+ # from labeller values and fixed text.
6
+ #
7
+ # Supported placeholders: `%n` (labeller value), `%pn` (parent labeller
8
+ # value), `%parent` (rendered global label of the parent context
9
+ # element), `%self` (rendered local label of the element itself).
10
+ class LabelTemplate < Lutaml::Model::Serializable
11
+ attribute :pattern, :string
12
+ attribute :direction, :string, default: "ltr"
13
+ attribute :condition, Ladl::LabelContextCondition
14
+ attribute :definition, :string
15
+
16
+ hsh do
17
+ map "pattern", to: :pattern
18
+ map "direction", to: :direction
19
+ map "condition", to: :condition
20
+ map "definition", to: :definition
21
+ end
22
+
23
+ def conditional?
24
+ !condition.nil?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # MN 112 "Labeller" — assigns labels to content elements based on their
5
+ # position in the document structure and labeling context, and generates
6
+ # a human-readable label string from a label object.
7
+ #
8
+ # Labellers form the labeller tree: one of the three model trees of
9
+ # MN 112, linked via the `parent` attribute.
10
+ #
11
+ # The `sequence`, `context` and `parent` attributes hold references to
12
+ # profile instances (by instance name) as written in the `.lml`
13
+ # profile; the runtime resolver (Ladl::Runtime::Resolver) wires them to
14
+ # the referenced models. The `template` and `global_template`
15
+ # collections are hydrated into nested Ladl::LabelTemplate objects by
16
+ # the profile loader.
17
+ class Labeller < Lutaml::Model::Serializable
18
+ attribute :id, :string
19
+ attribute :sequence, :string
20
+ attribute :context, :string
21
+ attribute :type, :string
22
+ attribute :parent, :string
23
+ attribute :template, Ladl::LabelTemplate, collection: true
24
+ attribute :global_template, Ladl::LabelTemplate, collection: true
25
+ attribute :definition, :string
26
+
27
+ hsh do
28
+ map "id", to: :id
29
+ map "sequence", to: :sequence
30
+ map "context", to: :context
31
+ map "type", to: :type
32
+ map "parent", to: :parent
33
+ map "template", to: :template
34
+ map "global_template", to: :global_template
35
+ map "definition", to: :definition
36
+ end
37
+
38
+ # First template of the local template collection; used as the
39
+ # fallback when no template condition matches.
40
+ def first_template
41
+ Array(template).first
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ module Lml
5
+ # The parse result of a `.lml` document: the ordered list of its
6
+ # instance declarations.
7
+ class Document
8
+ attr_reader :instances
9
+
10
+ def initialize(instances: [])
11
+ @instances = instances
12
+ end
13
+
14
+ def add_instance(instance)
15
+ @instances << instance
16
+ instance
17
+ end
18
+
19
+ # The first instance with the given name, if any.
20
+ def instance_named(name)
21
+ @instances.find { |i| i.name == name }
22
+ end
23
+
24
+ # All instances whose base class name matches `base`.
25
+ def instances_of_base(base)
26
+ @instances.select { |i| i.base == base }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ module Lml
5
+ # A parsed `instance` declaration from a `.lml` document: its name,
6
+ # its base class (after `<`), and its attribute map. Attribute values
7
+ # are plain Ruby data: String, Integer, true/false, nil, Array, or a
8
+ # nested Hash (from `{ ... }` blocks).
9
+ class Instance
10
+ attr_reader :name, :base, :attributes
11
+
12
+ def initialize(name:, base: nil, attributes: {})
13
+ @name = name
14
+ @base = base
15
+ @attributes = attributes
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,345 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ module Lml
5
+ # Parser for the subset of the LutaML instance syntax that MN 112
6
+ # profiles are written in:
7
+ #
8
+ # // comment
9
+ # instance Name < BaseClass {
10
+ # attr = "string"
11
+ # ref = BareReference
12
+ # count = 3
13
+ # list = ["a", OtherRef, 3]
14
+ # condition = { type = "x" threshold = 1 }
15
+ # definition { free text
16
+ # * with bullets }
17
+ # definition "one line"
18
+ # }
19
+ #
20
+ # The MN 112 profile syntax uses unquoted instance names and `<`
21
+ # inheritance, which the released LutaML grammar (lutaml-lml) does
22
+ # not accept; this parser covers that draft syntax plus the generic
23
+ # value forms above.
24
+ class Parser
25
+ def self.parse(text)
26
+ new(text).parse
27
+ end
28
+
29
+ def initialize(text)
30
+ @tokens = Lexer.new(text).tokens
31
+ @pos = 0
32
+ @pending_isa = nil
33
+ end
34
+
35
+ def parse
36
+ document = Document.new
37
+ until (@pos >= @tokens.size || peek.nil? || peek == [:punct, ""])
38
+ document.add_instance(parse_instance)
39
+ end
40
+ document
41
+ end
42
+
43
+ private
44
+
45
+ def peek
46
+ @tokens[@pos]
47
+ end
48
+
49
+ def advance
50
+ token = @tokens[@pos]
51
+ @pos += 1
52
+ token
53
+ end
54
+
55
+ def expect(type, value = nil)
56
+ token = advance
57
+ unless token && token[0] == type && (value.nil? || token[1] == value)
58
+ raise ParseError,
59
+ "expected #{value || type} at token #{@pos - 1}, got " \
60
+ "#{token ? token.inspect : "end of input"}"
61
+ end
62
+ token
63
+ end
64
+
65
+ def parse_instance
66
+ expect(:ident, "instance")
67
+ name = parse_name
68
+ base = nil
69
+ if peek && peek == [:punct, "<"]
70
+ advance
71
+ base = expect(:ident)[1]
72
+ end
73
+ expect(:punct, "{")
74
+ attributes = parse_body
75
+ expect(:punct, "}")
76
+ # lutaml-lml spells inheritance as a body statement (`isa Base;`)
77
+ # rather than the MN 112 `< Base` form; both are accepted.
78
+ base ||= @pending_isa
79
+ @pending_isa = nil
80
+ Instance.new(name: name, base: base, attributes: attributes)
81
+ end
82
+
83
+ def parse_name
84
+ token = peek
85
+ case token&.first
86
+ when :ident
87
+ advance[1]
88
+ when :string
89
+ advance[1]
90
+ else
91
+ raise ParseError, "expected instance name, got #{token.inspect}"
92
+ end
93
+ end
94
+
95
+ def parse_body
96
+ attributes = {}
97
+ until (@pos >= @tokens.size || peek == [:punct, "}"])
98
+ parse_body_entry(attributes)
99
+ end
100
+ attributes
101
+ end
102
+
103
+ def parse_body_entry(attributes)
104
+ token = advance
105
+ raise ParseError, "unexpected token #{token.inspect} in instance body" unless token
106
+
107
+ if token == [:ident, "definition"]
108
+ attributes["definition"] = parse_definition
109
+ elsif token == [:ident, "isa"]
110
+ @pending_isa = parse_name
111
+ advance if peek && peek == [:punct, ";"]
112
+ else
113
+ raise ParseError, "expected attribute name, got #{token.inspect}" unless token[0] == :ident
114
+
115
+ attributes[token[1]] = parse_assignment_value
116
+ end
117
+ end
118
+
119
+ def parse_assignment_value
120
+ expect(:punct, "=")
121
+ parse_value
122
+ end
123
+
124
+ def parse_definition
125
+ token = peek
126
+ case token&.first
127
+ when :raw_block
128
+ advance[1]
129
+ when :string
130
+ advance[1]
131
+ else
132
+ expect(:string)[1]
133
+ end
134
+ end
135
+
136
+ def parse_value
137
+ token = peek
138
+ case token&.first
139
+ when :string
140
+ advance[1]
141
+ when :number
142
+ advance[1]
143
+ when :ident
144
+ ident = advance[1]
145
+ case ident
146
+ when "true" then true
147
+ when "false" then false
148
+ when "null" then nil
149
+ else ident
150
+ end
151
+ when :punct
152
+ if token == [:punct, "["]
153
+ parse_list
154
+ elsif token == [:punct, "{"]
155
+ parse_map
156
+ else
157
+ raise ParseError, "unexpected token #{token.inspect} in value position"
158
+ end
159
+ else
160
+ raise ParseError, "expected value, got #{token.inspect}"
161
+ end
162
+ end
163
+
164
+ def parse_list
165
+ expect(:punct, "[")
166
+ items = []
167
+ until (peek == [:punct, "]"])
168
+ items << parse_value
169
+ if peek == [:punct, ","]
170
+ advance
171
+ else
172
+ break
173
+ end
174
+ end
175
+ expect(:punct, "]")
176
+ items
177
+ end
178
+
179
+ def parse_map
180
+ expect(:punct, "{")
181
+ attributes = parse_body
182
+ expect(:punct, "}")
183
+ attributes
184
+ end
185
+
186
+ # Turns `.lml` source text into a flat token stream. `//` line
187
+ # comments and `/* */` block comments are dropped.
188
+ class Lexer
189
+ def initialize(text)
190
+ @text = text.to_s
191
+ @pos = 0
192
+ @size = @text.length
193
+ @pending = []
194
+ end
195
+
196
+ def tokens
197
+ result = []
198
+ until eos?
199
+ result << next_token
200
+ end
201
+ result
202
+ end
203
+
204
+ private
205
+
206
+ def eos?
207
+ @pos >= @size
208
+ end
209
+
210
+ def next_token
211
+ return @pending.shift unless @pending.empty?
212
+
213
+ skip_whitespace_and_comments
214
+ return [:punct, ""] if eos?
215
+
216
+ ch = @text[@pos]
217
+ case ch
218
+ when "{", "}", "[", "]", "=", ",", "<", ";"
219
+ @pos += 1
220
+ [:punct, ch]
221
+ when '"'
222
+ lex_string
223
+ when /\d/
224
+ lex_number
225
+ when /[A-Za-z_]/
226
+ lex_ident
227
+ when "-"
228
+ lex_number
229
+ else
230
+ raise ParseError,
231
+ "unexpected character #{ch.inspect} at offset #{@pos}"
232
+ end
233
+ end
234
+
235
+ def skip_whitespace_and_comments
236
+ until eos?
237
+ ch = @text[@pos]
238
+ if ch =~ /\s/
239
+ @pos += 1
240
+ elsif @text[@pos, 2] == "//"
241
+ skip_until_newline
242
+ elsif @text[@pos, 2] == "/*"
243
+ skip_block_comment
244
+ else
245
+ break
246
+ end
247
+ end
248
+ end
249
+
250
+ def skip_until_newline
251
+ while @pos < @size && @text[@pos] != "\n"
252
+ @pos += 1
253
+ end
254
+ end
255
+
256
+ def skip_block_comment
257
+ @pos += 2
258
+ until eos? || @text[@pos, 2] == "*/"
259
+ @pos += 1
260
+ end
261
+ @pos += 2 unless eos?
262
+ end
263
+
264
+ def lex_string
265
+ @pos += 1
266
+ buffer = +""
267
+ until eos? || @text[@pos] == '"'
268
+ ch = @text[@pos]
269
+ if ch == "\\"
270
+ @pos += 1
271
+ buffer << unescape
272
+ else
273
+ buffer << ch
274
+ @pos += 1
275
+ end
276
+ end
277
+ raise ParseError, "unterminated string at offset #{@pos}" if eos?
278
+
279
+ @pos += 1
280
+ [:string, buffer]
281
+ end
282
+
283
+ def unescape
284
+ raise ParseError, "unterminated escape at offset #{@pos}" if eos?
285
+
286
+ ch = @text[@pos]
287
+ @pos += 1
288
+ { "n" => "\n", "t" => "\t", '"' => '"', "\\" => "\\" }.fetch(ch, ch)
289
+ end
290
+
291
+ def lex_number
292
+ start = @pos
293
+ @pos += 1 if @text[@pos] == "-"
294
+ @pos += 1 while !eos? && @text[@pos] =~ /\d/
295
+ [:number, Integer(@text[start...@pos])]
296
+ end
297
+
298
+ def lex_ident
299
+ start = @pos
300
+ @pos += 1 while !eos? && @text[@pos] =~ /[A-Za-z0-9_]/
301
+ ident = @text[start...@pos]
302
+ if ident == "definition" && next_nonspace_char == "{"
303
+ @pending << [:raw_block, scan_raw_block]
304
+ [:ident, ident]
305
+ else
306
+ [:ident, ident]
307
+ end
308
+ end
309
+
310
+ def next_nonspace_char
311
+ i = @pos
312
+ i += 1 while i < @size && @text[i] =~ /\s/
313
+ @text[i]
314
+ end
315
+
316
+ # `definition { ... }` blocks contain free text (bullets, nested
317
+ # braces, slashes, newlines); it is consumed verbatim up to the
318
+ # matching closing brace.
319
+ def scan_raw_block
320
+ i = @pos
321
+ i += 1 while i < @size && @text[i] =~ /\s/
322
+ raise ParseError, "unterminated definition block" if i >= @size || @text[i] != "{"
323
+
324
+ i += 1
325
+ depth = 1
326
+ start = i
327
+ while i < @size
328
+ ch = @text[i]
329
+ if ch == "{"
330
+ depth += 1
331
+ elsif ch == "}"
332
+ depth -= 1
333
+ break if depth.zero?
334
+ end
335
+ i += 1
336
+ end
337
+ raise ParseError, "unterminated definition block" if depth.positive?
338
+
339
+ @pos = i + 1
340
+ @text[start...i].strip
341
+ end
342
+ end
343
+ end
344
+ end
345
+ end
data/lib/ladl/lml.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # Parsing of LADL profile instances written in the LutaML instance
5
+ # syntax used by MN 112 (`.lml` documents).
6
+ module Lml
7
+ autoload :Parser, "ladl/lml/parser"
8
+ autoload :Document, "ladl/lml/document"
9
+ autoload :Instance, "ladl/lml/instance"
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # MN 112 "NumericSequence" — defines a monotonic sequence of numerical
5
+ # values.
6
+ #
7
+ # The `format` attribute is a runtime extension of this engine: it
8
+ # names the labeling format used to render a position (one of the
9
+ # kinds registered in Ladl::Sequences::Formatters). It carries the
10
+ # MN 112 default labeling sequences (`DecimalNumericSequence`,
11
+ # `RomanUpperNumericSequence`, ...) as profile-loadable data.
12
+ #
13
+ # The `base_sequence` attribute holds the instance name of the base
14
+ # sequence this sequence builds upon; it is resolved at runtime
15
+ # against the registry and the profile-defined sequences.
16
+ class NumericSequence < Lutaml::Model::Serializable
17
+ attribute :char_direction, :string, default: "ltr"
18
+ attribute :initial_value, :integer, default: 1
19
+ attribute :increment, :integer, default: 1
20
+ attribute :cycle_length, :integer
21
+ attribute :base_sequence, :string
22
+ attribute :format, :string
23
+ attribute :definition, :string
24
+
25
+ hsh do
26
+ map "char_direction", to: :char_direction
27
+ map "initial_value", to: :initial_value
28
+ map "increment", to: :increment
29
+ map "cycle_length", to: :cycle_length
30
+ map "base_sequence", to: :base_sequence
31
+ map "format", to: :format
32
+ map "definition", to: :definition
33
+ end
34
+
35
+ # MN 112 Sequence#value_for_position — provides the value for a given
36
+ # order in the sequence. Delegates the computation to
37
+ # Ladl::Sequences.value_for; `lookup` resolves `base_sequence`
38
+ # references against the runtime registry.
39
+ def value_for_position(position, lookup: nil)
40
+ Ladl::Sequences.value_for(self, position, lookup: lookup)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ladl
4
+ # A loaded LADL profile: the LabelStyle model described by the profile
5
+ # document, together with the instance tables of its labellers,
6
+ # contexts, templates, sequences, content elements and labels, keyed by
7
+ # instance name.
8
+ class Profile
9
+ attr_reader :style
10
+
11
+ def initialize(style:, labellers: {}, contexts: {}, templates: {},
12
+ sequences: {}, elements: {}, labels: {})
13
+ @style = style
14
+ @labellers = labellers
15
+ @contexts = contexts
16
+ @templates = templates
17
+ @sequences = sequences
18
+ @elements = elements
19
+ @labels = labels
20
+ end
21
+
22
+ def labeller(name)
23
+ @labellers[name]
24
+ end
25
+
26
+ def labeller_by_id(id)
27
+ @labellers.values.find { |l| l.id == id }
28
+ end
29
+
30
+ def context(name)
31
+ @contexts[name]
32
+ end
33
+
34
+ def template(name)
35
+ @templates[name]
36
+ end
37
+
38
+ def sequence(name)
39
+ @sequences[name]
40
+ end
41
+
42
+ def element(name)
43
+ @elements[name]
44
+ end
45
+
46
+ def label(name)
47
+ @labels[name]
48
+ end
49
+
50
+ def labeller_names
51
+ @labellers.keys
52
+ end
53
+
54
+ def context_names
55
+ @contexts.keys
56
+ end
57
+
58
+ def template_names
59
+ @templates.keys
60
+ end
61
+
62
+ def sequence_names
63
+ @sequences.keys
64
+ end
65
+ end
66
+ end