Class: Token::Resolver::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/token/resolver/config.rb

Overview

Configuration object defining the token structure for parsing.

A Config describes what tokens look like: their opening/closing delimiters, segment separators, and segment count constraints. Configs are frozen after initialization and implement #hash/#eql? for grammar caching.

Examples:

Default config (tokens like X|Y)

config = Token::Resolver::Config.default
config.pre          # => "{"
config.post         # => "}"
config.separators   # => ["|"]
config.min_segments # => 2

Custom config (tokens like <<X:Y>>)

config = Token::Resolver::Config.new(
  pre: "<<",
  post: ">>",
  separators: [":"],
)

Multi-separator config (tokens like KJ|SECTION:NAME)

config = Token::Resolver::Config.new(
  separators: ["|", ":"],
)
# First boundary uses "|", second uses ":", rest repeat ":"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]") ⇒ Config

Create a new Config.

Parameters:

  • pre (String) (defaults to: "{")

    Opening delimiter (default: “{”)

  • post (String) (defaults to: "}")

    Closing delimiter (default: “}”)

  • separators (Array<String>) (defaults to: ["|"])

    Segment separators (default: [“|”])

  • min_segments (Integer) (defaults to: 2)

    Minimum segment count (default: 2)

  • max_segments (Integer, nil) (defaults to: nil)

    Maximum segment count (default: nil)

  • segment_pattern (String) (defaults to: "[A-Za-z0-9_]")

    Parslet match() character class for valid segment characters (default: “[A-Za-z0-9_]”)

Raises:

  • (ArgumentError)

    If any delimiter is empty or constraints are invalid



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/token/resolver/config.rb', line 63

def initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]")
  validate!(pre, post, separators, min_segments, max_segments)

  @pre = pre.dup.freeze
  @post = post.dup.freeze
  @separators = separators.map { |s| s.dup.freeze }.freeze
  @min_segments = min_segments
  @max_segments = max_segments
  @segment_pattern = segment_pattern.dup.freeze

  freeze
end

Instance Attribute Details

#max_segmentsInteger? (readonly)

Returns Maximum number of segments (nil = unlimited).

Returns:

  • (Integer, nil)

    Maximum number of segments (nil = unlimited)



45
46
47
# File 'lib/token/resolver/config.rb', line 45

def max_segments
  @max_segments
end

#min_segmentsInteger (readonly)

Returns Minimum number of segments for a valid token.

Returns:

  • (Integer)

    Minimum number of segments for a valid token



42
43
44
# File 'lib/token/resolver/config.rb', line 42

def min_segments
  @min_segments
end

#postString (readonly)

Returns Closing delimiter for tokens.

Returns:

  • (String)

    Closing delimiter for tokens



36
37
38
# File 'lib/token/resolver/config.rb', line 36

def post
  @post
end

#preString (readonly)

Returns Opening delimiter for tokens.

Returns:

  • (String)

    Opening delimiter for tokens



33
34
35
# File 'lib/token/resolver/config.rb', line 33

def pre
  @pre
end

#segment_patternString (readonly)

Returns Parslet-compatible character class for segment content. Only characters matching this pattern are valid inside a token segment. Default: ‘“[A-Za-z0-9_]”` (word characters — no spaces, no operators).

Returns:

  • (String)

    Parslet-compatible character class for segment content. Only characters matching this pattern are valid inside a token segment. Default: ‘“[A-Za-z0-9_]”` (word characters — no spaces, no operators).



50
51
52
# File 'lib/token/resolver/config.rb', line 50

def segment_pattern
  @segment_pattern
end

#separatorsArray<String> (readonly)

Returns Separators between segments (used sequentially; last repeats).

Returns:

  • (Array<String>)

    Separators between segments (used sequentially; last repeats)



39
40
41
# File 'lib/token/resolver/config.rb', line 39

def separators
  @separators
end

Class Method Details

.defaultConfig

Default config suitable for kettle-jem style tokens like KJ|GEM_NAME.

Returns:



80
81
82
# File 'lib/token/resolver/config.rb', line 80

def default
  @default ||= new # rubocop:disable ThreadSafety/ClassInstanceVariable
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Equality based on all attributes.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
# File 'lib/token/resolver/config.rb', line 89

def eql?(other)
  return false unless other.is_a?(Config)

  pre == other.pre &&
    post == other.post &&
    separators == other.separators &&
    min_segments == other.min_segments &&
    max_segments == other.max_segments &&
    segment_pattern == other.segment_pattern
end

#hashInteger

Hash based on all attributes (for use as Hash key / grammar cache).

Returns:

  • (Integer)


105
106
107
# File 'lib/token/resolver/config.rb', line 105

def hash
  [pre, post, separators, min_segments, max_segments, segment_pattern].hash
end

#separator_at(index) ⇒ String

Get the separator for a given boundary index.

When there are more segment boundaries than separators, the last separator repeats.

Parameters:

  • index (Integer)

    Zero-based boundary index

Returns:

  • (String)

    The separator to use



115
116
117
118
119
120
121
# File 'lib/token/resolver/config.rb', line 115

def separator_at(index)
  if index < separators.length
    separators[index]
  else
    separators.last
  end
end