lutaml-lml 0.3.5 → 0.3.7

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.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lutaml-lml.rb +5 -0
  3. data/lib/lutaml_lml/version.rb +6 -0
  4. metadata +12 -164
  5. data/exe/lutaml-lml +0 -6
  6. data/exe/lutaml-lml-lsp +0 -11
  7. data/lib/lutaml/lml/association_label_resolver.rb +0 -52
  8. data/lib/lutaml/lml/cli.rb +0 -489
  9. data/lib/lutaml/lml/data_processor/attribute_processing.rb +0 -133
  10. data/lib/lutaml/lml/data_processor/collection_processing.rb +0 -37
  11. data/lib/lutaml/lml/data_processor/instance_processing.rb +0 -77
  12. data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +0 -70
  13. data/lib/lutaml/lml/data_processor/value_processing.rb +0 -103
  14. data/lib/lutaml/lml/data_processor/view_processing.rb +0 -26
  15. data/lib/lutaml/lml/data_processor.rb +0 -59
  16. data/lib/lutaml/lml/diagnostic.rb +0 -149
  17. data/lib/lutaml/lml/document_builder.rb +0 -481
  18. data/lib/lutaml/lml/entity_types.rb +0 -30
  19. data/lib/lutaml/lml/executor/adapter_helpers.rb +0 -45
  20. data/lib/lutaml/lml/executor/condition_evaluator.rb +0 -169
  21. data/lib/lutaml/lml/executor/csv_adapter.rb +0 -88
  22. data/lib/lutaml/lml/executor/format_adapter.rb +0 -54
  23. data/lib/lutaml/lml/executor/xml_adapter.rb +0 -102
  24. data/lib/lutaml/lml/executor.rb +0 -96
  25. data/lib/lutaml/lml/format/adapter/document.rb +0 -11
  26. data/lib/lutaml/lml/format/adapter/mapping.rb +0 -31
  27. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +0 -162
  28. data/lib/lutaml/lml/format/adapter/transform.rb +0 -11
  29. data/lib/lutaml/lml/format.rb +0 -29
  30. data/lib/lutaml/lml/formatter/base.rb +0 -79
  31. data/lib/lutaml/lml/formatter/canonical.rb +0 -736
  32. data/lib/lutaml/lml/formatter/elk.rb +0 -45
  33. data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +0 -89
  34. data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +0 -72
  35. data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +0 -89
  36. data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +0 -130
  37. data/lib/lutaml/lml/formatter/graphviz.rb +0 -90
  38. data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +0 -134
  39. data/lib/lutaml/lml/formatter/svg_pdf.rb +0 -285
  40. data/lib/lutaml/lml/formatter.rb +0 -12
  41. data/lib/lutaml/lml/grammar/lml.artifact.json +0 -14993
  42. data/lib/lutaml/lml/grammar/lml.parg +0 -389
  43. data/lib/lutaml/lml/grammar.rb +0 -60
  44. data/lib/lutaml/lml/has_attributes.rb +0 -14
  45. data/lib/lutaml/lml/import_resolver.rb +0 -96
  46. data/lib/lutaml/lml/layout/elk_engine.rb +0 -15
  47. data/lib/lutaml/lml/layout/elk_graph_builder.rb +0 -129
  48. data/lib/lutaml/lml/layout/elk_svg_markers.rb +0 -45
  49. data/lib/lutaml/lml/layout/elk_svg_renderer.rb +0 -119
  50. data/lib/lutaml/lml/layout/engine.rb +0 -17
  51. data/lib/lutaml/lml/layout/graph_viz_engine.rb +0 -19
  52. data/lib/lutaml/lml/layout.rb +0 -12
  53. data/lib/lutaml/lml/literal_value.rb +0 -32
  54. data/lib/lutaml/lml/lsp/conversions.rb +0 -78
  55. data/lib/lutaml/lml/lsp/keywords.rb +0 -43
  56. data/lib/lutaml/lml/lsp/server.rb +0 -378
  57. data/lib/lutaml/lml/lsp/store.rb +0 -68
  58. data/lib/lutaml/lml/lsp/transport.rb +0 -53
  59. data/lib/lutaml/lml/lsp.rb +0 -16
  60. data/lib/lutaml/lml/model_compiler/enums.rb +0 -172
  61. data/lib/lutaml/lml/model_compiler/hydration.rb +0 -123
  62. data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +0 -188
  63. data/lib/lutaml/lml/model_compiler/string_formats.rb +0 -42
  64. data/lib/lutaml/lml/model_compiler/validation.rb +0 -65
  65. data/lib/lutaml/lml/model_compiler.rb +0 -323
  66. data/lib/lutaml/lml/models/action.rb +0 -10
  67. data/lib/lutaml/lml/models/association.rb +0 -29
  68. data/lib/lutaml/lml/models/cardinality.rb +0 -10
  69. data/lib/lutaml/lml/models/collection.rb +0 -11
  70. data/lib/lutaml/lml/models/constraint.rb +0 -23
  71. data/lib/lutaml/lml/models/data_type.rb +0 -41
  72. data/lib/lutaml/lml/models/derived_field.rb +0 -14
  73. data/lib/lutaml/lml/models/diagram.rb +0 -20
  74. data/lib/lutaml/lml/models/document.rb +0 -84
  75. data/lib/lutaml/lml/models/enum.rb +0 -38
  76. data/lib/lutaml/lml/models/fidelity.rb +0 -10
  77. data/lib/lutaml/lml/models/group.rb +0 -11
  78. data/lib/lutaml/lml/models/instance.rb +0 -26
  79. data/lib/lutaml/lml/models/instance_collection.rb +0 -16
  80. data/lib/lutaml/lml/models/instances_export.rb +0 -10
  81. data/lib/lutaml/lml/models/instances_import.rb +0 -11
  82. data/lib/lutaml/lml/models/mapping.rb +0 -15
  83. data/lib/lutaml/lml/models/mapping_rule.rb +0 -16
  84. data/lib/lutaml/lml/models/member_field.rb +0 -14
  85. data/lib/lutaml/lml/models/namespace.rb +0 -16
  86. data/lib/lutaml/lml/models/operation.rb +0 -26
  87. data/lib/lutaml/lml/models/operation_parameter.rb +0 -12
  88. data/lib/lutaml/lml/models/package.rb +0 -26
  89. data/lib/lutaml/lml/models/primitive_type.rb +0 -33
  90. data/lib/lutaml/lml/models/reference.rb +0 -23
  91. data/lib/lutaml/lml/models/serialization_mapping.rb +0 -19
  92. data/lib/lutaml/lml/models/string_format.rb +0 -16
  93. data/lib/lutaml/lml/models/top_element_attribute.rb +0 -116
  94. data/lib/lutaml/lml/models/uml_class.rb +0 -85
  95. data/lib/lutaml/lml/models/value.rb +0 -15
  96. data/lib/lutaml/lml/models/view_filter.rb +0 -11
  97. data/lib/lutaml/lml/models/view_import.rb +0 -11
  98. data/lib/lutaml/lml/parser.rb +0 -19
  99. data/lib/lutaml/lml/pipeline.rb +0 -73
  100. data/lib/lutaml/lml/positioned.rb +0 -13
  101. data/lib/lutaml/lml/preprocessor.rb +0 -130
  102. data/lib/lutaml/lml/quoting.rb +0 -60
  103. data/lib/lutaml/lml/source.rb +0 -49
  104. data/lib/lutaml/lml/source_position.rb +0 -20
  105. data/lib/lutaml/lml/symbol_index.rb +0 -200
  106. data/lib/lutaml/lml/transform.rb +0 -119
  107. data/lib/lutaml/lml/types/text_type.rb +0 -28
  108. data/lib/lutaml/lml/types.rb +0 -10
  109. data/lib/lutaml/lml/validator.rb +0 -458
  110. data/lib/lutaml/lml/version.rb +0 -7
  111. data/lib/lutaml/lml/view_resolution.rb +0 -32
  112. data/lib/lutaml/lml/view_resolver.rb +0 -48
  113. data/lib/lutaml/lml/yaml_parser.rb +0 -17
  114. data/lib/lutaml/lml.rb +0 -130
@@ -1,378 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Lml
5
- module Lsp
6
- # The stdio JSON-RPC server. One message at a time; every document
7
- # change reparses the full text. Errors are contained per message
8
- # so a broken document never crashes the server.
9
- class Server
10
- WORD = /[A-Za-z0-9_:.-]/
11
-
12
- CAPABILITIES = {
13
- textDocumentSync: 1,
14
- documentSymbolProvider: true,
15
- definitionProvider: true,
16
- referencesProvider: true,
17
- hoverProvider: true,
18
- completionProvider: { triggerCharacters: [' ', ':'] },
19
- documentFormattingProvider: true,
20
- diagnosticProvider: {
21
- interFileDependencies: false,
22
- workspaceDiagnostics: false,
23
- },
24
- }.freeze
25
-
26
- def initialize(transport: Transport.new, store: Store.new)
27
- @transport = transport
28
- @store = store
29
- @shutdown = false
30
- @stopped = false
31
- end
32
-
33
- # Runs the message loop; returns the process exit code.
34
- def run
35
- until @stopped
36
- message = @transport.read_message
37
- break if message.nil?
38
-
39
- dispatch(message)
40
- end
41
- @shutdown ? 0 : 1
42
- end
43
-
44
- private
45
-
46
- def dispatch(message)
47
- id = message['id']
48
- method = message['method']
49
- params = message['params'] || {}
50
-
51
- case method
52
- when 'initialize' then respond(id, initialize_result)
53
- when 'initialized' then nil
54
- when 'shutdown'
55
- @shutdown = true
56
- respond(id, nil)
57
- when 'exit'
58
- @stopped = true
59
- when 'textDocument/didOpen' then did_open(params)
60
- when 'textDocument/didChange' then did_change(params)
61
- when 'textDocument/didClose' then did_close(params)
62
- when 'textDocument/diagnostic' then respond(id, diagnostic_report(params))
63
- when 'textDocument/documentSymbol' then respond(id, document_symbols(params))
64
- when 'textDocument/definition' then respond(id, definition(params))
65
- when 'textDocument/references' then respond(id, references(params))
66
- when 'textDocument/hover' then respond(id, hover(params))
67
- when 'textDocument/completion' then respond(id, completion(params))
68
- when 'textDocument/formatting' then respond(id, formatting(params))
69
- else
70
- respond_error(id, -32_601, "Method not found: #{method}") unless id.nil?
71
- end
72
- rescue StandardError => e
73
- respond_error(id, -32_603, e.message) unless id.nil?
74
- end
75
-
76
- def initialize_result
77
- {
78
- capabilities: CAPABILITIES,
79
- serverInfo: { name: 'lutaml-lml-lsp', version: Lutaml::Lml::VERSION },
80
- }
81
- end
82
-
83
- # ---- documents ----
84
-
85
- def did_open(params)
86
- document = params['textDocument'] || {}
87
- entry = @store.open(
88
- uri: document['uri'], text: document['text'].to_s,
89
- version: document['version']
90
- )
91
- publish(entry)
92
- end
93
-
94
- def did_change(params)
95
- document = params['textDocument'] || {}
96
- changes = params['contentChanges'] || []
97
- text = changes.last.is_a?(Hash) ? changes.last['text'].to_s : ''
98
- entry = @store.change(
99
- uri: document['uri'], text: text, version: document['version'],
100
- )
101
- publish(entry) if entry
102
- end
103
-
104
- def did_close(params)
105
- document = params['textDocument'] || {}
106
- uri = document['uri']
107
- @store.close(uri)
108
- @transport.write_message(
109
- jsonrpc: '2.0', method: 'textDocument/publishDiagnostics',
110
- params: { uri: uri, diagnostics: [] }
111
- )
112
- end
113
-
114
- def publish(entry)
115
- @transport.write_message(
116
- jsonrpc: '2.0', method: 'textDocument/publishDiagnostics',
117
- params: {
118
- uri: entry.uri,
119
- diagnostics: entry.diagnostics.map { |record| Conversions.diagnostic(record) },
120
- }
121
- )
122
- end
123
-
124
- def diagnostic_report(params)
125
- entry = entry_for(params)
126
- return { kind: 'full', items: [] } unless entry
127
-
128
- {
129
- kind: 'full',
130
- items: entry.diagnostics.map { |record| Conversions.diagnostic(record) },
131
- }
132
- end
133
-
134
- # ---- symbols ----
135
-
136
- def document_symbols(params)
137
- entry = entry_for(params)
138
- return [] unless entry&.model
139
-
140
- SymbolIndex.new(entry.model).entries.map do |symbol|
141
- {
142
- name: symbol.name,
143
- kind: SymbolIndex::KINDS.fetch(symbol.kind),
144
- location: location(entry.uri, symbol.position),
145
- containerName: symbol.container,
146
- }.compact
147
- end
148
- end
149
-
150
- def definition(params)
151
- entry = entry_for(params)
152
- return nil unless entry&.model
153
-
154
- token = token_at(entry, params['position'])
155
- return nil if token.empty?
156
-
157
- index = SymbolIndex.new(entry.model)
158
- locations = resolve(index, token).map { |symbol| location(entry.uri, symbol.position) }
159
- locations.empty? ? nil : locations
160
- end
161
-
162
- def references(params)
163
- entry = entry_for(params)
164
- return [] unless entry&.model
165
-
166
- token = token_at(entry, params['position'])
167
- return [] if token.empty?
168
-
169
- index = SymbolIndex.new(entry.model)
170
- symbols = index.uses(token)
171
- symbols += index.definitions(token) if params.dig('context', 'includeDeclaration')
172
-
173
- symbols.map { |symbol| location(entry.uri, symbol.position) }.uniq
174
- end
175
-
176
- def hover(params)
177
- entry = entry_for(params)
178
- return nil unless entry&.model
179
-
180
- position = params['position']
181
- token = token_at(entry, position)
182
- return nil if token.empty?
183
-
184
- symbol = resolve(SymbolIndex.new(entry.model), token).first
185
- return nil unless symbol
186
-
187
- {
188
- contents: { kind: 'markdown', value: hover_markdown(symbol) },
189
- range: token_range(entry, position, token),
190
- }
191
- end
192
-
193
- def hover_markdown(symbol)
194
- case symbol.kind
195
- when :class then class_markdown(symbol.record)
196
- when :enum then enum_markdown(symbol.record)
197
- when :field then field_markdown(symbol.record)
198
- when :instance then instance_markdown(symbol.record)
199
- when :enum_member then enum_member_markdown(symbol.record)
200
- else "**#{symbol.name}**"
201
- end
202
- end
203
-
204
- def class_markdown(klass)
205
- text = "```lml\nclass #{klass.name}"
206
- text << " < #{klass.parent_class}" unless klass.parent_class.to_s.empty?
207
- text << "\n```"
208
- append_definition(text, klass.definition)
209
- end
210
-
211
- def enum_markdown(enum)
212
- members = Array(enum.attributes).map { |member| member.name.to_s }
213
- text = "```lml\nenum #{enum.name}\n```"
214
- text << "\n\nMembers: #{members.join(', ')}" unless members.empty?
215
- append_definition(text, enum.definition)
216
- end
217
-
218
- def field_markdown(attribute)
219
- label = attribute.name.to_s
220
- type = attribute.type.to_s
221
- text = "```lml\n#{label}"
222
- text << ": #{type}" unless type.empty?
223
- cardinality = attribute.cardinality
224
- if cardinality
225
- max = cardinality.max.to_s
226
- text << " [#{cardinality.min}#{"..#{max}" unless max.empty?}]"
227
- end
228
- text << "\n```"
229
- text << "\n\ndefault: `#{attribute.default}`" if attribute.default
230
- text << "\npattern: `#{attribute.pattern}`" if attribute.pattern
231
- text << "\nvalues: #{attribute.value_set.join(', ')}" if Array(attribute.value_set).any?
232
- append_definition(text, attribute.definition)
233
- text << "\n\n`null`, `\"\"`, and an unassigned attribute are distinct." if type == 'String'
234
- text
235
- end
236
-
237
- def instance_markdown(instance)
238
- label = instance.type.to_s.empty? ? instance.name.to_s : instance.type.to_s
239
- text = "```lml\ninstance #{label}\n```"
240
- text << "\n\n#{Array(instance.attributes).size} assignment(s)"
241
- text
242
- end
243
-
244
- def enum_member_markdown(member)
245
- text = "**value #{member.name}**"
246
- description = Array(member.attributes).find { |attribute| attribute.name == 'description' }
247
- text << "\n\n#{description.type}" if description
248
- text
249
- end
250
-
251
- def append_definition(text, definition)
252
- value = definition.to_s
253
- text << "\n\n#{value}" unless value.empty?
254
- text
255
- end
256
-
257
- # ---- completion ----
258
-
259
- def completion(params)
260
- entry = entry_for(params)
261
- prefix = completion_prefix(entry, params['position'])
262
- items = completion_items(entry)
263
- items = items.select { |item| item[:label].start_with?(prefix) } unless prefix.empty?
264
-
265
- { isIncomplete: false, items: items }
266
- end
267
-
268
- def completion_items(entry)
269
- items = Keywords.all.map do |keyword|
270
- { label: keyword, kind: 14, detail: 'LML keyword' }
271
- end
272
- return dedupe(items) unless entry&.model
273
-
274
- index = SymbolIndex.new(entry.model)
275
- index.entity_entries.each do |symbol|
276
- items << {
277
- label: symbol.name,
278
- kind: SymbolIndex::KINDS.fetch(symbol.kind),
279
- detail: symbol.detail || symbol.container,
280
- }
281
- end
282
- index.attribute_entries.each do |symbol|
283
- items << { label: symbol.name, kind: 8, detail: symbol.detail }
284
- end
285
- dedupe(items)
286
- end
287
-
288
- def dedupe(items)
289
- seen = {}
290
- items.each_with_object([]) do |item, result|
291
- next if seen[item[:label]]
292
-
293
- seen[item[:label]] = true
294
- result << item.compact
295
- end
296
- end
297
-
298
- # ---- formatting ----
299
-
300
- def formatting(params)
301
- entry = entry_for(params)
302
- return nil unless entry&.model
303
-
304
- canonical = Lutaml::Formatter::Canonical.new(entry.model).render
305
- return [] if canonical == entry.text
306
-
307
- [{ range: full_range(entry.text), newText: canonical }]
308
- end
309
-
310
- def full_range(text)
311
- {
312
- start: { line: 0, character: 0 },
313
- end: { line: text.lines.length, character: 0 },
314
- }
315
- end
316
-
317
- # ---- helpers ----
318
-
319
- def entry_for(params)
320
- document = params['textDocument'] || {}
321
- @store[document['uri']]
322
- end
323
-
324
- def location(uri, position)
325
- { uri: uri, range: Conversions.range(position) }
326
- end
327
-
328
- def resolve(index, token)
329
- symbols = index.definitions(token)
330
- return symbols unless symbols.empty?
331
-
332
- token.split('.').flat_map { |segment| index.definitions(segment) }.uniq
333
- end
334
-
335
- def token_at(entry, position)
336
- line = entry.text.lines[position['line'].to_i].to_s
337
- index = [position['character'].to_i, line.length].min
338
- start = index
339
- start -= 1 while start.positive? && line[start - 1].match?(WORD)
340
- finish = index
341
- finish += 1 while finish < line.length && line[finish].match?(WORD)
342
- line[start...finish].to_s
343
- end
344
-
345
- def token_range(entry, position, token)
346
- line = entry.text.lines[position['line'].to_i].to_s
347
- finish = [position['character'].to_i, line.length].min
348
- start = [finish - token.length, 0].max
349
- {
350
- start: { line: position['line'].to_i, character: start },
351
- end: { line: position['line'].to_i, character: start + token.length },
352
- }
353
- end
354
-
355
- def completion_prefix(entry, position)
356
- return '' unless entry
357
-
358
- line = entry.text.lines[position['line'].to_i].to_s
359
- before = line[0...[position['character'].to_i, line.length].min].to_s
360
- before[/[A-Za-z0-9_]*\z/].to_s
361
- end
362
-
363
- def respond(id, result)
364
- return if id.nil?
365
-
366
- @transport.write_message(jsonrpc: '2.0', id: id, result: result)
367
- end
368
-
369
- def respond_error(id, code, message)
370
- @transport.write_message(
371
- jsonrpc: '2.0', id: id,
372
- error: { code: code, message: message }
373
- )
374
- end
375
- end
376
- end
377
- end
378
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Lml
5
- module Lsp
6
- # The open-document store. Every change reparses the full text
7
- # (full-document sync) and recomputes diagnostics.
8
- class Store
9
- Entry = Struct.new(:uri, :text, :version, :model, :diagnostics,
10
- keyword_init: true)
11
-
12
- def initialize
13
- @entries = {}
14
- end
15
-
16
- def open(uri:, text:, version: nil)
17
- entry = Entry.new(uri: uri, text: text, version: version)
18
- parse(entry)
19
- @entries[uri] = entry
20
- end
21
-
22
- def change(uri:, text:, version: nil)
23
- entry = @entries[uri]
24
- return nil unless entry
25
-
26
- entry.text = text
27
- entry.version = version
28
- parse(entry)
29
- entry
30
- end
31
-
32
- def close(uri)
33
- @entries.delete(uri)
34
- end
35
-
36
- def [](uri)
37
- @entries[uri]
38
- end
39
-
40
- private
41
-
42
- def parse(entry)
43
- path = Conversions.uri_to_path(entry.uri)
44
- entry.model = Lutaml::Lml.parse_document(entry.text)
45
- entry.model.source_path = path
46
- entry.diagnostics = Validator.diagnostics(entry.model)
47
- rescue Lutaml::Lml::Error => e
48
- entry.model = nil
49
- entry.diagnostics = [diagnostic_for(e, path)]
50
- end
51
-
52
- def diagnostic_for(error, path)
53
- record = error.diagnostic
54
- return record if record&.file
55
-
56
- Diagnostic.build(
57
- record&.code || 'parse/LML-001',
58
- message: record&.message || error.message.lines.first.to_s.strip,
59
- file: path,
60
- line: record&.line,
61
- column: record&.column,
62
- length: record&.length,
63
- )
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
-
5
- module Lutaml
6
- module Lml
7
- module Lsp
8
- # LSP base protocol framing: `Content-Length: N\r\n\r\n<json>`.
9
- # Reads one message at a time and writes responses to the output
10
- # stream. stdout is the protocol channel; stderr is for logging.
11
- class Transport
12
- def initialize(input: $stdin, output: $stdout)
13
- @input = input
14
- @output = output
15
- end
16
-
17
- def read_message
18
- headers = read_headers
19
- return nil unless headers
20
-
21
- length = headers["content-length"]
22
- return nil unless length
23
-
24
- body = @input.read(length.to_i)
25
- body.nil? || body.empty? ? nil : JSON.parse(body)
26
- end
27
-
28
- def write_message(payload)
29
- body = JSON.generate(payload)
30
- @output.write("Content-Length: #{body.bytesize}\r\n\r\n#{body}")
31
- @output.flush
32
- end
33
-
34
- private
35
-
36
- def read_headers
37
- headers = {}
38
- loop do
39
- line = @input.gets
40
- return nil if line.nil?
41
-
42
- line = line.chomp
43
- break if line.empty?
44
-
45
- key, value = line.split(":", 2)
46
- headers[key.to_s.strip.downcase] = value.to_s.strip
47
- end
48
- headers
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Lml
5
- # Language Server Protocol support: a stdio JSON-RPC server exposing
6
- # diagnostics, symbols, navigation, hover, completion, and canonical
7
- # formatting over the parsed document model.
8
- module Lsp
9
- autoload :Transport, "lutaml/lml/lsp/transport"
10
- autoload :Server, "lutaml/lml/lsp/server"
11
- autoload :Store, "lutaml/lml/lsp/store"
12
- autoload :Keywords, "lutaml/lml/lsp/keywords"
13
- autoload :Conversions, "lutaml/lml/lsp/conversions"
14
- end
15
- end
16
- end
@@ -1,172 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lutaml
4
- module Lml
5
- class ModelCompiler
6
- # Compiles LML enums into Serializable subclasses with member
7
- # payloads, table-driven expansion, and value accessors.
8
- module Enums
9
- EnumValue = Struct.new(:name, :description, :payload)
10
-
11
- private
12
-
13
- def compile_enum(enum_def)
14
- name = enum_def.name.to_s
15
- expand_from_table(enum_def)
16
- values = extract_enum_values(enum_def)
17
-
18
- payload_fields = Array(enum_def.member_fields).map(&:name).map(&:to_s)
19
- value_names = values.map { |v| v.name.to_s }
20
- compiled_klass = Class.new(Lutaml::Model::Serializable) do
21
- attribute :value, :string, values: value_names, default: values.first.name
22
- attribute :description, :string
23
- payload_fields.each do |field|
24
- attribute field.to_sym, :string
25
- end
26
-
27
- define_method(:to_s) { value }
28
- end
29
-
30
- values.each do |value|
31
- accessor = value.name.to_s.gsub(/[^a-zA-Z0-9_]/, '_')
32
- compiled_klass.define_singleton_method(accessor) do
33
- new({ value: value.name, description: value.description }.merge(value.payload || {}))
34
- end
35
- end
36
- compiled_klass.define_singleton_method(:values) { values }
37
-
38
- @enum_names << name
39
- @enum_classes[name] = compiled_klass
40
- register(name, compiled_klass)
41
- end
42
-
43
- # Builds a compiled Serializable subclass from a class or data-type
44
- # definition: declares attributes and stashes forward-referenced
45
- # attributes for post-registration resolution.
46
-
47
- # RS 3001 §Attribute: `values { v1, v2 }` expands into an anonymous
48
- # enum baked from the inline set; the attribute is typed with it.
49
- def resolve_values_type(klass_def, attr)
50
- return attr.type.to_s unless attr.value_set&.any?
51
-
52
- enum_name = anonymous_enum_name(klass_def.name, attr.name)
53
- members = attr.value_set.map { |v| ::Lutaml::Lml::TopElementAttribute.new(name: v.to_s) }
54
- compile_enum(::Lutaml::Lml::Enum.new(name: enum_name, attributes: members))
55
- enum_name
56
- end
57
-
58
- def anonymous_enum_name(class_name, attr_name)
59
- base = class_name.to_s.split('::').last
60
- attr_part = attr_name.to_s.split('_').map(&:capitalize).join
61
- "#{base}#{attr_part}Values"
62
- end
63
-
64
- # L7a point 3: from_table members are baked at compile time — the
65
- # artifact table's rows become the values, validated against the
66
- # declared member_fields (unknown field = error; missing = nil).
67
- # The member name comes from the row's "name" column.
68
- def expand_from_table(enum_def)
69
- return unless enum_def.from_table_name
70
-
71
- rows = artifact_table_rows(enum_def)
72
- known = Array(enum_def.member_fields).map { |f| f.name.to_s }
73
- rows.each do |row|
74
- row = row.transform_keys(&:to_s)
75
- member_name = row.fetch('name')
76
- unknown = row.keys - known - ['name']
77
- if unknown.any?
78
- raise ArgumentError,
79
- "enum #{enum_def.name}: table row #{member_name.inspect} has " \
80
- "undeclared fields #{unknown.inspect} (declared: #{known.inspect})"
81
- end
82
- payload = row.slice(*known).compact
83
- enum_def.attributes << TopElementAttribute.new(
84
- name: member_name,
85
- properties: payload.map do |field, value|
86
- TopElementAttribute.new(name: field, value: value)
87
- end,
88
- )
89
- end
90
- end
91
-
92
- def artifact_table_rows(enum_def)
93
- reference = enum_def.from_table_artifact || @default_artifact
94
- path = artifact_path_for(reference.to_s)
95
- unless path
96
- raise ArgumentError,
97
- "enum #{enum_def.name}: from_table artifact #{reference.inspect} not found"
98
- end
99
-
100
- envelope = JSON.parse(File.read(path))
101
- table = envelope['tables'].to_h[enum_def.from_table_name]
102
- unless table
103
- raise ArgumentError,
104
- "enum #{enum_def.name}: table #{enum_def.from_table_name.inspect} " \
105
- "not found in artifact #{reference.inspect}"
106
- end
107
-
108
- table['rows'].to_a
109
- end
110
-
111
- def extract_enum_values(enum_def)
112
- member_fields = Array(enum_def.member_fields).map do |f|
113
- [f.name.to_s, f.type.to_s]
114
- end
115
- enum_def.attributes.map do |attr|
116
- description = member_description(attr)
117
- payload = extract_member_payload(attr)
118
- validate_member_payload(enum_def, attr, payload, member_fields)
119
- EnumValue.new(attr.name.to_s, description, payload)
120
- end
121
- end
122
-
123
- # The member description rides a nested attribute named `description`
124
- # (bare-member form) or `definition` (RS 3001 `value` blocks); both
125
- # spellings are accepted.
126
-
127
- # The member description rides a nested attribute named `description`
128
- # (bare-member form) or `definition` (RS 3001 `value` blocks); both
129
- # spellings are accepted.
130
- def member_description(attr)
131
- nested = Array(attr.attributes).find do |n|
132
- %w[description definition].include?(n.name.to_s)
133
- end
134
- text = nested&.type || nested&.definition || nested&.value
135
- text.is_a?(Hash) && text.key?(:string) ? text[:string] : text
136
- end
137
-
138
- # L7a: per-member payload lines ride the keyword-attribute
139
- # properties (`abbreviation = "WD"`)
140
-
141
- # L7a: per-member payload lines ride the keyword-attribute
142
- # properties (`abbreviation = "WD"`)
143
- def extract_member_payload(attr)
144
- Array(attr.properties).to_h { |p| [p.name.to_s, p.value&.to_s] }
145
- end
146
-
147
- def validate_member_payload(enum_def, attr, payload, member_fields)
148
- known = member_fields.map(&:first)
149
- unknown = payload.keys - known
150
- return unless unknown.any?
151
-
152
- raise ArgumentError,
153
- "enum #{enum_def.name}: member #{attr.name} has undeclared " \
154
- "payload fields #{unknown.inspect} (declared: #{known.inspect})"
155
- end
156
-
157
- def enum_class_for(type)
158
- type.is_a?(Class) && @enum_classes.value?(type) and return type
159
- end
160
-
161
- def enum_value_name(raw)
162
- raw.split('::').last
163
- end
164
-
165
- # L7b: derived fields declare existence + type; computation is
166
- # bound by the binder from artifact derive specs. The attribute
167
- # participates in mappings like any other (relaton consumes URNs
168
- # from model JSON).
169
- end
170
- end
171
- end
172
- end