Module: Toml::Merge::NodeTypeNormalizer

Extended by:
Ast::Merge::NodeTyping::Normalizer
Defined in:
lib/toml/merge/node_type_normalizer.rb

Overview

Normalizes backend-specific node types to canonical TOML types.

Uses Ast::Merge::NodeTyping::Wrapper to wrap nodes with canonical
merge_type, allowing portable merge rules across backends.

Thread Safety

All backend registration and lookup operations are thread-safe via
the shared Ast::Merge::NodeTyping::Normalizer module.

Backends

Currently supports:

  • :tree_sitter - tree-sitter-toml grammar (via ruby_tree_sitter, tree_stump, FFI)
  • :citrus - toml-rb gem with Citrus parser
  • :parslet - toml gem with Parslet parser

Extensibility

New backends can be registered at runtime:

Canonical Types

The following canonical types are used for portable merge rules:

Document Structure

  • :document - Root document node
  • :table - Table/section header [section]
  • :array_of_tables - Array of tables [[section]]
  • :pair - Key-value pair key = value

Key Types

  • :bare_key - Unquoted key
  • :quoted_key - Quoted key "key" or 'key'
  • :dotted_key - Dotted key a.b.c

Value Types

  • :string - String values (basic or literal)
  • :integer - Integer values
  • :float - Floating point values
  • :boolean - Boolean true/false
  • :array - Array values [1, 2, 3]
  • :inline_table - Inline table { key = value }

Date/Time Types

  • :datetime - Date/time values (offset, local, date-only, time-only)

Other

  • :comment - Comment lines

Examples:

Registering a new backend

NodeTypeNormalizer.register_backend(:my_toml_parser, {
  key_value: :pair,
  section: :table,
  section_array: :array_of_tables,
})

See Also:

  • Ast::Merge::NodeTyping::Wrapper
  • Ast::Merge::NodeTyping::Normalizer

Constant Summary collapse

DEFAULT_BACKEND =

Default backend for TOML normalization

:tree_sitter

Class Method Summary collapse

Class Method Details

.canonical_type(backend_type, backend = DEFAULT_BACKEND) ⇒ Symbol?

Get the canonical type for a backend-specific type.
Overrides the shared Normalizer to default to :tree_sitter backend.

Parameters:

  • backend_type (Symbol, String, nil)

    The backend’s node type

  • backend (Symbol) (defaults to: DEFAULT_BACKEND)

    The backend identifier (defaults to :tree_sitter)

Returns:

  • (Symbol, nil)

    Canonical type (or original if no mapping)



245
246
247
# File 'lib/toml/merge/node_type_normalizer.rb', line 245

def canonical_type(backend_type, backend = DEFAULT_BACKEND)
  super(backend_type, backend)
end

.container_type?(type) ⇒ Boolean

Check if a type is a container type (can have children)

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


290
291
292
293
# File 'lib/toml/merge/node_type_normalizer.rb', line 290

def container_type?(type)
  canonical = type.to_sym
  %i[document table array_of_tables array inline_table].include?(canonical)
end

.key_type?(type) ⇒ Boolean

Check if a type is a key type

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


281
282
283
284
# File 'lib/toml/merge/node_type_normalizer.rb', line 281

def key_type?(type)
  canonical = type.to_sym
  %i[bare_key quoted_key dotted_key].include?(canonical)
end

.table_type?(type) ⇒ Boolean

Check if a type is a table type (regular or array of tables)

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


263
264
265
266
# File 'lib/toml/merge/node_type_normalizer.rb', line 263

def table_type?(type)
  canonical = type.to_sym
  %i[table array_of_tables].include?(canonical)
end

.value_type?(type) ⇒ Boolean

Check if a type is a value type (string, integer, etc.)

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


272
273
274
275
# File 'lib/toml/merge/node_type_normalizer.rb', line 272

def value_type?(type)
  canonical = type.to_sym
  %i[string integer float boolean array inline_table datetime].include?(canonical)
end

.wrap(node, backend = DEFAULT_BACKEND) ⇒ Ast::Merge::NodeTyping::Wrapper

Wrap a node with its canonical type as merge_type.
Overrides the shared Normalizer to default to :tree_sitter backend.

Parameters:

  • node (Object)

    The backend node to wrap (must respond to #type)

  • backend (Symbol) (defaults to: DEFAULT_BACKEND)

    The backend identifier (defaults to :tree_sitter)

Returns:

  • (Ast::Merge::NodeTyping::Wrapper)

    Wrapped node with canonical merge_type



255
256
257
# File 'lib/toml/merge/node_type_normalizer.rb', line 255

def wrap(node, backend = DEFAULT_BACKEND)
  super(node, backend)
end