token-resolver 2.0.19 → 2.0.20

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 528ccca8cd81b3442ac406d404a0c64210fd14026fae07c5e1ffa0d62dc5242b
4
- data.tar.gz: 367a85efda52a84b70631e2ec68c58789b30221454b0dada7f69eeb2d1b07572
3
+ metadata.gz: d6da58b8725f584f9490f880db85b80767641b51406a639b33d11a170bc32f7f
4
+ data.tar.gz: 6b4ce10e3d2e64bf23917b4a24deacd466088642af22e81ff6a40a355dbff67a
5
5
  SHA512:
6
- metadata.gz: f96385367bf597e3101957c2f1c52e2bb08ed3779116907c33863c67fb8499e5621d3aa9010a79cce6984d301fc485ebabc662ff3ad9d56d62e93c8d6c185b52
7
- data.tar.gz: b99e13b7b0f0b55c3325dd6f76dba42d44dd5ff7a7f18ee70f6ba7e792b599266b4cd93af39c443c35994f6237054e24a15112aae1fb483bf10cab49eb497026
6
+ metadata.gz: d3d6578a1ffeb5739e8fad4e85b32ba49575beeb75bba07cd2e41b7c6c61c5e02b094cb3436939cb8e082d2e94c22942ddcac5c7a253c0861fa1905b682cf983
7
+ data.tar.gz: 17f7529db7b62b75734c0c087592f086b88c6be5428dbc26d7c7a82264ff0b1fc155bf04ec1af00781c8192cdbd5836df694b6eb92c4575dbb6d3b43bf829189
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -30,6 +30,21 @@ Please file a bug if you notice a violation of semantic versioning.
30
30
 
31
31
  ### Security
32
32
 
33
+ ## [2.0.20] - 2026-12-03
34
+
35
+ - TAG: [v2.0.20][2.0.20t]
36
+ - COVERAGE: 99.70% -- 335/336 lines in 11 files
37
+ - BRANCH COVERAGE: 96.51% -- 83/86 branches in 11 files
38
+ - 98.48% documented
39
+
40
+ ### Added
41
+
42
+ - Selectable `parser:` option on `Token::Resolver::Config` (`:parslet` default, `:scanner` opt-in compiled `Regexp` scanner)
43
+
44
+ ### Changed
45
+
46
+ - Restore PEG (`parser: :parslet`) as the default backend with zero-copy `pre`-delimiter byte-index skipping so plain prose is skipped in C while candidate tokens are parsed by Parslet
47
+
33
48
  ## [2.0.19] - 2026-12-03
34
49
 
35
50
  - TAG: [v2.0.19][2.0.19t]
@@ -550,7 +565,9 @@ Please file a bug if you notice a violation of semantic versioning.
550
565
 
551
566
  ### Security
552
567
 
553
- [Unreleased]: https://github.com/kettle-dev/token-resolver/compare/v2.0.19...HEAD
568
+ [Unreleased]: https://github.com/kettle-dev/token-resolver/compare/v2.0.20...HEAD
569
+ [2.0.20]: https://github.com/kettle-dev/token-resolver/compare/v2.0.19...v2.0.20
570
+ [2.0.20t]: https://github.com/kettle-dev/token-resolver/releases/tag/v2.0.20
554
571
  [2.0.19]: https://github.com/kettle-dev/token-resolver/compare/v2.0.18...v2.0.19
555
572
  [2.0.19t]: https://github.com/kettle-dev/token-resolver/releases/tag/v2.0.19
556
573
  [2.0.18]: https://github.com/kettle-dev/token-resolver/compare/v2.0.17...v2.0.18
data/README.md CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  ## 🌻 Synopsis <a href="https://discord.gg/3qme4XHNKN"><img alt="Galtzo FLOSS Logo by Aboling0, CC BY-SA 4.0" src="https://logos.galtzo.com/assets/images/galtzo-floss/avatar-128px.svg" width="8%" align="right"/></a> <a href="https://ruby-toolbox.com"><img alt="ruby-lang Logo, Yukihiro Matsumoto, Ruby Visual Identity Team, CC BY-SA 2.5" src="https://logos.galtzo.com/assets/images/ruby-lang/avatar-128px.svg" width="8%" align="right"/></a>
18
18
 
19
- Token::Resolver is a configurable PEG-based token parser and resolver for structured token detection and replacement in arbitrary text.
19
+ Token::Resolver is a configurable PEG-first token parser and resolver for structured token detection and replacement in arbitrary text, with an optional high-throughput compiled `Regexp` scanner (`parser: :scanner`).
20
20
 
21
21
  Detects structured tokens like `{KJ|GEM_NAME}` in any file format and resolves them against a replacement map. The token structure (delimiters, separators, segment count) is fully configurable.
22
22
 
@@ -55,6 +55,24 @@ gem install token-resolver
55
55
  | `min_segments` | `2` | Minimum segments for a valid token |
56
56
  | `max_segments` | `nil` | Maximum segments (`nil` = unlimited) |
57
57
  | `segment_pattern` | `"[A-Za-z0-9_]"` | Parslet character class for valid segment content |
58
+ | `parser` | `:parslet` | Parser backend: `:parslet` (PEG-first with `pre`-index skipping) or `:scanner` (compiled `Regexp` scanner) |
59
+
60
+ ### Choosing a Parser Backend (`:parslet` vs `:scanner`)
61
+
62
+ `Token::Resolver` offers two parser backends selectable via `Config.new(parser: ...)`:
63
+
64
+ | Backend | How it works | Strengths | Trade-offs |
65
+ |---------------------|--------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
66
+ | `:parslet` (default)| Uses `String#byteindex` to jump straight to `pre` delimiters in C, then parses each candidate token with the Parslet PEG grammar | Genuine non-backtracking ordered-choice PEG semantics (immune to catastrophic regex backtracking / ReDoS on complex custom `segment_pattern`s) and full Parslet `match(...)` compatibility | ~2–5× slower than `:scanner` on large files with hundreds of non-token `pre` occurrences (still ~10–100× faster than naive character-by-character Parslet) |
67
+ | `:scanner` | Jumps to `pre` offsets and matches a cached compiled `Regexp` (falling back to `:parslet` on `RegexpError`) | Maximum throughput and lowest allocations (~100× faster than character-by-character Parslet) on large documents with many delimiter occurrences | Relies on Ruby's `Regexp` engine (equivalent match set for standard character-class configs, without PEG's non-backtracking guarantee on exotic custom patterns) |
68
+
69
+ ```ruby
70
+ # Default: PEG-first (Parslet with pre-delimiter index skipping)
71
+ peg_config = Token::Resolver::Config.new(separators: ["|", ":"])
72
+
73
+ # Opt-in: pure compiled Regexp scanner for maximum throughput
74
+ fast_config = Token::Resolver::Config.new(separators: ["|", ":"], parser: :scanner)
75
+ ```
58
76
 
59
77
  ### Segment Character Constraints
60
78
 
@@ -502,7 +520,7 @@ Thanks for RTFM. ☺️
502
520
  [📌gitmoji]: https://gitmoji.dev
503
521
  [📌gitmoji-img]: https://img.shields.io/badge/gitmoji_commits-%20%F0%9F%98%9C%20%F0%9F%98%8D-34495e.svg?style=flat-square
504
522
  [🧮kloc]: https://www.youtube.com/watch?v=dQw4w9WgXcQ
505
- [🧮kloc-img]: https://img.shields.io/badge/KLOC-0.305-FFDD67.svg?style=for-the-badge&logo=YouTube&logoColor=blue
523
+ [🧮kloc-img]: https://img.shields.io/badge/KLOC-0.336-FFDD67.svg?style=for-the-badge&logo=YouTube&logoColor=blue
506
524
  [🔐security]: https://github.com/kettle-dev/token-resolver/blob/main/SECURITY.md
507
525
  [🔐security-img]: https://img.shields.io/badge/security-policy-259D6C.svg?style=flat
508
526
  [📄copyright-notice-explainer]: https://opensource.stackexchange.com/questions/5778/why-do-licenses-such-as-the-mit-license-specify-a-single-year
@@ -530,7 +548,7 @@ Thanks for RTFM. ☺️
530
548
  | Field | Value |
531
549
  |---|---|
532
550
  | Package | token-resolver |
533
- | Description | 🪙 Token::Resolver provides configurable PEG-based (parslet) parsing and resolution of structured tokens (e.g., {KJ\|GEM_NAME}) in arbitrary text. Useful for template ETL pipelines where tokens in template files must be resolved before format-specific merging. |
551
+ | Description | 🪙 Token::Resolver provides configurable PEG-first (parslet) parsing and resolution of structured tokens (e.g., {KJ\|GEM_NAME}) in arbitrary text, with an optional high-throughput compiled Regexp scanner (`parser: :scanner`). Useful for template ETL pipelines where tokens in template files must be resolved before format-specific merging. |
534
552
  | Homepage | https://github.com/kettle-dev/token-resolver |
535
553
  | Source | https://github.com/kettle-dev/token-resolver |
536
554
  | License | `AGPL-3.0-only` OR `PolyForm-Small-Business-1.0.0` |
@@ -43,11 +43,23 @@ module Token
43
43
  # @return [Integer, nil] Maximum number of segments (nil = unlimited)
44
44
  attr_reader :max_segments
45
45
 
46
+ # Supported parser backends.
47
+ #
48
+ # - `:parslet` (default): PEG-based Parslet parser with `pre`-delimiter
49
+ # index skipping. Non-backtracking ordered-choice PEG semantics and full
50
+ # Parslet `match(...)` compatibility.
51
+ # - `:scanner`: Compiled `Regexp` scanner anchored at `pre` offsets.
52
+ # Fastest option on large documents with many `pre` delimiter occurrences.
53
+ VALID_PARSERS = %i[parslet scanner].freeze
54
+
46
55
  # @return [String] Parslet-compatible character class for segment content.
47
56
  # Only characters matching this pattern are valid inside a token segment.
48
57
  # Default: `"[A-Za-z0-9_]"` (word characters — no spaces, no operators).
49
58
  attr_reader :segment_pattern
50
59
 
60
+ # @return [Symbol] Parser backend (`:parslet` or `:scanner`, default: `:parslet`)
61
+ attr_reader :parser
62
+
51
63
  # Create a new Config.
52
64
  #
53
65
  # @param pre [String] Opening delimiter (default: "{")
@@ -57,10 +69,12 @@ module Token
57
69
  # @param max_segments [Integer, nil] Maximum segment count (default: nil)
58
70
  # @param segment_pattern [String] Parslet match() character class for valid segment
59
71
  # characters (default: "[A-Za-z0-9_]")
72
+ # @param parser [Symbol] Parser backend: `:parslet` (default, PEG-first with pre-index
73
+ # skipping) or `:scanner` (compiled Regexp scanner for maximum throughput)
60
74
  #
61
75
  # @raise [ArgumentError] If any delimiter is empty or constraints are invalid
62
- def initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]")
63
- validate!(pre, post, separators, min_segments, max_segments)
76
+ def initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]", parser: :parslet)
77
+ validate!(pre, post, separators, min_segments, max_segments, parser)
64
78
 
65
79
  @pre = pre.dup.freeze
66
80
  @post = post.dup.freeze
@@ -68,6 +82,7 @@ module Token
68
82
  @min_segments = min_segments
69
83
  @max_segments = max_segments
70
84
  @segment_pattern = segment_pattern.dup.freeze
85
+ @parser = parser
71
86
 
72
87
  freeze
73
88
  end
@@ -93,7 +108,8 @@ module Token
93
108
  separators == other.separators &&
94
109
  min_segments == other.min_segments &&
95
110
  max_segments == other.max_segments &&
96
- segment_pattern == other.segment_pattern
111
+ segment_pattern == other.segment_pattern &&
112
+ parser == other.parser
97
113
  end
98
114
 
99
115
  alias_method :==, :eql?
@@ -102,7 +118,7 @@ module Token
102
118
  #
103
119
  # @return [Integer]
104
120
  def hash
105
- [pre, post, separators, min_segments, max_segments, segment_pattern].hash
121
+ [pre, post, separators, min_segments, max_segments, segment_pattern, parser].hash
106
122
  end
107
123
 
108
124
  # Get the separator for a given boundary index.
@@ -121,7 +137,7 @@ module Token
121
137
 
122
138
  private
123
139
 
124
- def validate!(pre, post, separators, min_segments, max_segments)
140
+ def validate!(pre, post, separators, min_segments, max_segments, parser)
125
141
  raise ArgumentError, "pre must be a non-empty String" unless pre.is_a?(String) && !pre.empty?
126
142
  raise ArgumentError, "post must be a non-empty String" unless post.is_a?(String) && !post.empty?
127
143
  raise ArgumentError, "separators must be a non-empty Array" unless separators.is_a?(Array) && !separators.empty?
@@ -136,6 +152,10 @@ module Token
136
152
  raise ArgumentError, "max_segments must be a positive Integer" unless max_segments.is_a?(Integer) && max_segments >= 1
137
153
  raise ArgumentError, "max_segments (#{max_segments}) must be >= min_segments (#{min_segments})" if max_segments < min_segments
138
154
  end
155
+
156
+ unless VALID_PARSERS.include?(parser)
157
+ raise ArgumentError, "parser must be one of #{VALID_PARSERS.inspect}, got #{parser.inspect}"
158
+ end
139
159
  end
140
160
  end
141
161
  end
@@ -4,8 +4,11 @@ module Token
4
4
  module Resolver
5
5
  # Parses input text and provides access to the resulting text and token nodes.
6
6
  #
7
- # Document is the primary public API for parsing. It uses Grammar to parse
8
- # the input and Transform to convert the parse tree into node objects.
7
+ # Document is the primary public API for parsing. By default (`parser: :parslet`)
8
+ # it uses a PEG-first Parslet Grammar with `pre`-delimiter byte-index skipping so
9
+ # plain prose is skipped in C while every candidate token is parsed by Parslet's
10
+ # PEG rules. Set `Config.new(parser: :scanner)` to use the pure compiled `Regexp`
11
+ # scanner for maximum throughput on large documents with many delimiter occurrences.
9
12
  #
10
13
  # @example Basic usage
11
14
  # doc = Token::Resolver::Document.new("Hello {KJ|NAME}!")
@@ -101,6 +104,14 @@ module Token
101
104
  # Fast-path: if input doesn't contain the pre delimiter, no tokens possible
102
105
  return [Node::Text.new(input)] if input.nil? || input.empty? || !input.include?(@config.pre)
103
106
 
107
+ if @config.parser == :scanner
108
+ parse_with_scanner(input)
109
+ else
110
+ parse_with_parslet(input)
111
+ end
112
+ end
113
+
114
+ def parse_with_scanner(input)
104
115
  scan(input)
105
116
  rescue RegexpError
106
117
  # Fallback to Parslet if a custom segment_pattern isn't directly embeddable
@@ -108,14 +119,44 @@ module Token
108
119
  end
109
120
 
110
121
  def parse_with_parslet(input)
111
- parser_class = Grammar.build(@config)
112
- tree = parser_class.new.parse(input)
122
+ parser = Grammar.build(@config).new
123
+ return parse_indexed_parslet(input, parser.token) if parser.respond_to?(:token)
124
+
125
+ tree = parser.parse(input)
113
126
  Transform.apply(tree, @config)
114
127
  rescue Parslet::ParseFailed
115
128
  # Grammar should never fail, but if it does, treat entire input as text
116
129
  [Node::Text.new(input)]
117
130
  end
118
131
 
132
+ def parse_indexed_parslet(input, token_atom)
133
+ pre = @config.pre
134
+ pre_step = pre.chr.bytesize
135
+ nodes = []
136
+ text_start = 0
137
+ search_pos = 0
138
+ len = input.bytesize
139
+ source = Parslet::Source.new(input)
140
+ context = Parslet::Atoms::Context.new
141
+
142
+ while search_pos < len && (hit = input.byteindex(pre, search_pos))
143
+ source.bytepos = hit
144
+ success, value = token_atom.apply(source, context, false)
145
+ if success
146
+ nodes << Node::Text.new(input.byteslice(text_start, hit - text_start)) if hit > text_start
147
+ segments = Transform.extract_segments(token_atom.flatten(value))
148
+ nodes << Node::Token.new(segments, @config)
149
+ text_start = source.bytepos
150
+ search_pos = text_start
151
+ else
152
+ search_pos = hit + pre_step
153
+ end
154
+ end
155
+
156
+ nodes << Node::Text.new(input.byteslice(text_start, len - text_start)) if text_start < len
157
+ nodes
158
+ end
159
+
119
160
  def scan(input)
120
161
  token_re, sep_split_re = self.class.compiled_patterns(@config)
121
162
  pre = @config.pre
@@ -32,26 +32,10 @@ module Token
32
32
  coalesce(raw_nodes)
33
33
  end
34
34
 
35
- private
36
-
37
- def convert_entry(entry, config)
38
- if entry.key?(:token)
39
- convert_token(entry[:token], config)
40
- elsif entry.key?(:text)
41
- Node::Text.new(slice_to_s(entry[:text]))
42
- else
43
- # Shouldn't happen with our grammar, but be safe
44
- Node::Text.new(entry.to_s)
45
- end
46
- end
47
-
48
- def convert_token(token_data, config)
49
- # token_data contains :seg captures
50
- # It can be a single seg hash or an array of seg hashes
51
- segments = extract_segments(token_data)
52
- Node::Token.new(segments, config)
53
- end
54
-
35
+ # Extract segment strings from a flattened parslet token capture.
36
+ #
37
+ # @param token_data [Hash, Array, Object] Flattened token capture from Parslet
38
+ # @return [Array<String>] Extracted segment strings
55
39
  def extract_segments(token_data)
56
40
  # Parslet returns different structures depending on repetition:
57
41
  # - Single segment match: {:seg => "value"}
@@ -76,6 +60,26 @@ module Token
76
60
  end
77
61
  end
78
62
 
63
+ private
64
+
65
+ def convert_entry(entry, config)
66
+ if entry.key?(:token)
67
+ convert_token(entry[:token], config)
68
+ elsif entry.key?(:text)
69
+ Node::Text.new(slice_to_s(entry[:text]))
70
+ else
71
+ # Shouldn't happen with our grammar, but be safe
72
+ Node::Text.new(entry.to_s)
73
+ end
74
+ end
75
+
76
+ def convert_token(token_data, config)
77
+ # token_data contains :seg captures
78
+ # It can be a single seg hash or an array of seg hashes
79
+ segments = extract_segments(token_data)
80
+ Node::Token.new(segments, config)
81
+ end
82
+
79
83
  def slice_to_s(value)
80
84
  value.to_s
81
85
  end
@@ -5,7 +5,7 @@ module Token
5
5
  # Version namespace for this gem.
6
6
  module Version
7
7
  # Current gem version.
8
- VERSION = "2.0.19"
8
+ VERSION = "2.0.20"
9
9
  end
10
10
  # Current gem version exposed at the traditional constant location.
11
11
  VERSION = Version::VERSION # Traditional Constant Location
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: token-resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.19
4
+ version: 2.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter H. Boling
@@ -317,7 +317,7 @@ dependencies:
317
317
  - ">="
318
318
  - !ruby/object:Gem::Version
319
319
  version: 2.0.17
320
- description: "🪙 Token::Resolver provides configurable PEG-based (parslet) parsing and resolution of structured tokens (e.g., {KJ|GEM_NAME}) in arbitrary text. Useful for template ETL pipelines where tokens in template files must be resolved before format-specific merging."
320
+ description: "🪙 Token::Resolver provides configurable PEG-first (parslet) parsing and resolution of structured tokens (e.g., {KJ|GEM_NAME}) in arbitrary text, with an optional high-throughput compiled Regexp scanner (`parser: :scanner`). Useful for template ETL pipelines where tokens in template files must be resolved before format-specific merging."
321
321
  email:
322
322
  - floss@galtzo.com
323
323
  executables: []
@@ -345,10 +345,10 @@ licenses:
345
345
  - PolyForm-Small-Business-1.0.0
346
346
  metadata:
347
347
  homepage_uri: "https://token-resolver.galtzo.com/"
348
- source_code_uri: "https://github.com/kettle-dev/token-resolver/tree/v2.0.19"
349
- changelog_uri: "https://github.com/kettle-dev/token-resolver/blob/v2.0.19/CHANGELOG.md"
348
+ source_code_uri: "https://github.com/kettle-dev/token-resolver/tree/v2.0.20"
349
+ changelog_uri: "https://github.com/kettle-dev/token-resolver/blob/v2.0.20/CHANGELOG.md"
350
350
  bug_tracker_uri: "https://github.com/kettle-dev/token-resolver/issues"
351
- documentation_uri: "https://www.rubydoc.info/gems/token-resolver/2.0.19"
351
+ documentation_uri: "https://www.rubydoc.info/gems/token-resolver/2.0.20"
352
352
  funding_uri: "https://github.com/sponsors/pboling"
353
353
  wiki_uri: "https://github.com/kettle-dev/token-resolver/wiki"
354
354
  news_uri: "https://www.railsbling.com/tags/token-resolver"
@@ -357,7 +357,7 @@ metadata:
357
357
  rubygems_mfa_required: "true"
358
358
  rdoc_options:
359
359
  - "--title"
360
- - token-resolver - 🪙 Configurable PEG-based token parser and resolver for structured token detection and replacement in arbitrary text
360
+ - token-resolver - 🪙 Configurable PEG-first token parser and resolver with optional compiled Regexp scanner
361
361
  - "--main"
362
362
  - README.md
363
363
  - "--exclude"
@@ -382,5 +382,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
382
382
  requirements: []
383
383
  rubygems_version: 4.1.1
384
384
  specification_version: 4
385
- summary: 🪙 Configurable PEG-based token parser and resolver for structured token detection and replacement in arbitrary text
385
+ summary: 🪙 Configurable PEG-first token parser and resolver with optional compiled Regexp scanner
386
386
  test_files: []
metadata.gz.sig CHANGED
Binary file