token-resolver 1.0.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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +48 -0
- data/CITATION.cff +20 -0
- data/CODE_OF_CONDUCT.md +134 -0
- data/CONTRIBUTING.md +227 -0
- data/FUNDING.md +74 -0
- data/LICENSE.txt +21 -0
- data/README.md +645 -0
- data/REEK +0 -0
- data/RUBOCOP.md +71 -0
- data/SECURITY.md +21 -0
- data/lib/token/resolver/config.rb +134 -0
- data/lib/token/resolver/document.rb +82 -0
- data/lib/token/resolver/grammar.rb +122 -0
- data/lib/token/resolver/node/text.rb +63 -0
- data/lib/token/resolver/node/token.rb +107 -0
- data/lib/token/resolver/node.rb +11 -0
- data/lib/token/resolver/resolve.rb +93 -0
- data/lib/token/resolver/transform.rb +100 -0
- data/lib/token/resolver/version.rb +12 -0
- data/lib/token/resolver.rb +90 -0
- data/lib/token-resolver.rb +4 -0
- data/sig/token/resolver.rbs +6 -0
- data.tar.gz.sig +2 -0
- metadata +314 -0
- metadata.gz.sig +0 -0
data/REEK
ADDED
|
File without changes
|
data/RUBOCOP.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# RuboCop Usage Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
A tale of two RuboCop plugin gems.
|
|
6
|
+
|
|
7
|
+
### RuboCop Gradual
|
|
8
|
+
|
|
9
|
+
This project uses `rubocop_gradual` instead of vanilla RuboCop for code style checking. The `rubocop_gradual` tool allows for gradual adoption of RuboCop rules by tracking violations in a lock file.
|
|
10
|
+
|
|
11
|
+
### RuboCop LTS
|
|
12
|
+
|
|
13
|
+
This project uses `rubocop-lts` to ensure, on a best-effort basis, compatibility with Ruby >= 1.9.2.
|
|
14
|
+
RuboCop rules are meticulously configured by the `rubocop-lts` family of gems to ensure that a project is compatible with a specific version of Ruby. See: https://rubocop-lts.gitlab.io for more.
|
|
15
|
+
|
|
16
|
+
## Checking RuboCop Violations
|
|
17
|
+
|
|
18
|
+
To check for RuboCop violations in this project, always use:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bundle exec rake rubocop_gradual:check
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Do not use** the standard RuboCop commands like:
|
|
25
|
+
- `bundle exec rubocop`
|
|
26
|
+
- `rubocop`
|
|
27
|
+
|
|
28
|
+
## Understanding the Lock File
|
|
29
|
+
|
|
30
|
+
The `.rubocop_gradual.lock` file tracks all current RuboCop violations in the project. This allows the team to:
|
|
31
|
+
|
|
32
|
+
1. Prevent new violations while gradually fixing existing ones
|
|
33
|
+
2. Track progress on code style improvements
|
|
34
|
+
3. Ensure CI builds don't fail due to pre-existing violations
|
|
35
|
+
|
|
36
|
+
## Common Commands
|
|
37
|
+
|
|
38
|
+
- **Check violations**
|
|
39
|
+
- `bundle exec rake rubocop_gradual`
|
|
40
|
+
- `bundle exec rake rubocop_gradual:check`
|
|
41
|
+
- **(Safe) Autocorrect violations, and update lockfile if no new violations**
|
|
42
|
+
- `bundle exec rake rubocop_gradual:autocorrect`
|
|
43
|
+
- **Force update the lock file (w/o autocorrect) to match violations present in code**
|
|
44
|
+
- `bundle exec rake rubocop_gradual:force_update`
|
|
45
|
+
|
|
46
|
+
## Workflow
|
|
47
|
+
|
|
48
|
+
1. Before submitting a PR, run `bundle exec rake rubocop_gradual:autocorrect`
|
|
49
|
+
a. or just the default `bundle exec rake`, as autocorrection is a pre-requisite of the default task.
|
|
50
|
+
2. If there are new violations, either:
|
|
51
|
+
- Fix them in your code
|
|
52
|
+
- Run `bundle exec rake rubocop_gradual:force_update` to update the lock file (only for violations you can't fix immediately)
|
|
53
|
+
3. Commit the updated `.rubocop_gradual.lock` file along with your changes
|
|
54
|
+
|
|
55
|
+
## Never add inline RuboCop disables
|
|
56
|
+
|
|
57
|
+
Do not add inline `rubocop:disable` / `rubocop:enable` comments anywhere in the codebase (including specs, except when following the few existing `rubocop:disable` patterns for a rule already being disabled elsewhere in the code). We handle exceptions in two supported ways:
|
|
58
|
+
|
|
59
|
+
- Permanent/structural exceptions: prefer adjusting the RuboCop configuration (e.g., in `.rubocop.yml`) to exclude a rule for a path or file pattern when it makes sense project-wide.
|
|
60
|
+
- Temporary exceptions while improving code: record the current violations in `.rubocop_gradual.lock` via the gradual workflow:
|
|
61
|
+
- `bundle exec rake rubocop_gradual:autocorrect` (preferred; will autocorrect what it can and update the lock only if no new violations were introduced)
|
|
62
|
+
- If needed, `bundle exec rake rubocop_gradual:force_update` (as a last resort when you cannot fix the newly reported violations immediately)
|
|
63
|
+
|
|
64
|
+
In general, treat the rules as guidance to follow; fix violations rather than ignore them. For example, RSpec conventions in this project expect `described_class` to be used in specs that target a specific class under test.
|
|
65
|
+
|
|
66
|
+
## Benefits of rubocop_gradual
|
|
67
|
+
|
|
68
|
+
- Allows incremental adoption of code style rules
|
|
69
|
+
- Prevents CI failures due to pre-existing violations
|
|
70
|
+
- Provides a clear record of code style debt
|
|
71
|
+
- Enables focused efforts on improving code quality over time
|
data/SECURITY.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
| Version | Supported |
|
|
6
|
+
|----------|-----------|
|
|
7
|
+
| 1.latest | ✅ |
|
|
8
|
+
|
|
9
|
+
## Security contact information
|
|
10
|
+
|
|
11
|
+
To report a security vulnerability, please use the
|
|
12
|
+
[Tidelift security contact](https://tidelift.com/security).
|
|
13
|
+
Tidelift will coordinate the fix and disclosure.
|
|
14
|
+
|
|
15
|
+
## Additional Support
|
|
16
|
+
|
|
17
|
+
If you are interested in support for versions older than the latest release,
|
|
18
|
+
please consider sponsoring the project / maintainer @ https://liberapay.com/pboling/donate,
|
|
19
|
+
or find other sponsorship links in the [README].
|
|
20
|
+
|
|
21
|
+
[README]: README.md
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Token
|
|
4
|
+
module Resolver
|
|
5
|
+
# Configuration object defining the token structure for parsing.
|
|
6
|
+
#
|
|
7
|
+
# A Config describes what tokens look like: their opening/closing delimiters,
|
|
8
|
+
# segment separators, and segment count constraints. Configs are frozen after
|
|
9
|
+
# initialization and implement #hash/#eql? for grammar caching.
|
|
10
|
+
#
|
|
11
|
+
# @example Default config (tokens like {X|Y})
|
|
12
|
+
# config = Token::Resolver::Config.default
|
|
13
|
+
# config.pre # => "{"
|
|
14
|
+
# config.post # => "}"
|
|
15
|
+
# config.separators # => ["|"]
|
|
16
|
+
# config.min_segments # => 2
|
|
17
|
+
#
|
|
18
|
+
# @example Custom config (tokens like <<X:Y>>)
|
|
19
|
+
# config = Token::Resolver::Config.new(
|
|
20
|
+
# pre: "<<",
|
|
21
|
+
# post: ">>",
|
|
22
|
+
# separators: [":"],
|
|
23
|
+
# )
|
|
24
|
+
#
|
|
25
|
+
# @example Multi-separator config (tokens like {KJ|SECTION:NAME})
|
|
26
|
+
# config = Token::Resolver::Config.new(
|
|
27
|
+
# separators: ["|", ":"],
|
|
28
|
+
# )
|
|
29
|
+
# # First boundary uses "|", second uses ":", rest repeat ":"
|
|
30
|
+
#
|
|
31
|
+
class Config
|
|
32
|
+
# @return [String] Opening delimiter for tokens
|
|
33
|
+
attr_reader :pre
|
|
34
|
+
|
|
35
|
+
# @return [String] Closing delimiter for tokens
|
|
36
|
+
attr_reader :post
|
|
37
|
+
|
|
38
|
+
# @return [Array<String>] Separators between segments (used sequentially; last repeats)
|
|
39
|
+
attr_reader :separators
|
|
40
|
+
|
|
41
|
+
# @return [Integer] Minimum number of segments for a valid token
|
|
42
|
+
attr_reader :min_segments
|
|
43
|
+
|
|
44
|
+
# @return [Integer, nil] Maximum number of segments (nil = unlimited)
|
|
45
|
+
attr_reader :max_segments
|
|
46
|
+
|
|
47
|
+
# Create a new Config.
|
|
48
|
+
#
|
|
49
|
+
# @param pre [String] Opening delimiter (default: "{")
|
|
50
|
+
# @param post [String] Closing delimiter (default: "}")
|
|
51
|
+
# @param separators [Array<String>] Segment separators (default: ["|"])
|
|
52
|
+
# @param min_segments [Integer] Minimum segment count (default: 2)
|
|
53
|
+
# @param max_segments [Integer, nil] Maximum segment count (default: nil)
|
|
54
|
+
#
|
|
55
|
+
# @raise [ArgumentError] If any delimiter is empty or constraints are invalid
|
|
56
|
+
def initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil)
|
|
57
|
+
validate!(pre, post, separators, min_segments, max_segments)
|
|
58
|
+
|
|
59
|
+
@pre = pre.dup.freeze
|
|
60
|
+
@post = post.dup.freeze
|
|
61
|
+
@separators = separators.map { |s| s.dup.freeze }.freeze
|
|
62
|
+
@min_segments = min_segments
|
|
63
|
+
@max_segments = max_segments
|
|
64
|
+
|
|
65
|
+
freeze
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class << self
|
|
69
|
+
# Default config suitable for kettle-jem style tokens like {KJ|GEM_NAME}.
|
|
70
|
+
#
|
|
71
|
+
# @return [Config]
|
|
72
|
+
def default
|
|
73
|
+
@default ||= new # rubocop:disable ThreadSafety/ClassInstanceVariable
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Equality based on all attributes.
|
|
78
|
+
#
|
|
79
|
+
# @param other [Object]
|
|
80
|
+
# @return [Boolean]
|
|
81
|
+
def eql?(other)
|
|
82
|
+
return false unless other.is_a?(Config)
|
|
83
|
+
|
|
84
|
+
pre == other.pre &&
|
|
85
|
+
post == other.post &&
|
|
86
|
+
separators == other.separators &&
|
|
87
|
+
min_segments == other.min_segments &&
|
|
88
|
+
max_segments == other.max_segments
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
alias_method :==, :eql?
|
|
92
|
+
|
|
93
|
+
# Hash based on all attributes (for use as Hash key / grammar cache).
|
|
94
|
+
#
|
|
95
|
+
# @return [Integer]
|
|
96
|
+
def hash
|
|
97
|
+
[pre, post, separators, min_segments, max_segments].hash
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Get the separator for a given boundary index.
|
|
101
|
+
#
|
|
102
|
+
# When there are more segment boundaries than separators, the last separator repeats.
|
|
103
|
+
#
|
|
104
|
+
# @param index [Integer] Zero-based boundary index
|
|
105
|
+
# @return [String] The separator to use
|
|
106
|
+
def separator_at(index)
|
|
107
|
+
if index < separators.length
|
|
108
|
+
separators[index]
|
|
109
|
+
else
|
|
110
|
+
separators.last
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def validate!(pre, post, separators, min_segments, max_segments)
|
|
117
|
+
raise ArgumentError, "pre must be a non-empty String" unless pre.is_a?(String) && !pre.empty?
|
|
118
|
+
raise ArgumentError, "post must be a non-empty String" unless post.is_a?(String) && !post.empty?
|
|
119
|
+
raise ArgumentError, "separators must be a non-empty Array" unless separators.is_a?(Array) && !separators.empty?
|
|
120
|
+
|
|
121
|
+
separators.each_with_index do |sep, i|
|
|
122
|
+
raise ArgumentError, "separators[#{i}] must be a non-empty String" unless sep.is_a?(String) && !sep.empty?
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
raise ArgumentError, "min_segments must be a positive Integer" unless min_segments.is_a?(Integer) && min_segments >= 1
|
|
126
|
+
|
|
127
|
+
if max_segments
|
|
128
|
+
raise ArgumentError, "max_segments must be a positive Integer" unless max_segments.is_a?(Integer) && max_segments >= 1
|
|
129
|
+
raise ArgumentError, "max_segments (#{max_segments}) must be >= min_segments (#{min_segments})" if max_segments < min_segments
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Token
|
|
4
|
+
module Resolver
|
|
5
|
+
# Parses input text and provides access to the resulting text and token nodes.
|
|
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.
|
|
9
|
+
#
|
|
10
|
+
# @example Basic usage
|
|
11
|
+
# doc = Token::Resolver::Document.new("Hello {KJ|NAME}!")
|
|
12
|
+
# doc.nodes # => [Text("Hello "), Token(["KJ", "NAME"]), Text("!")]
|
|
13
|
+
# doc.tokens # => [Token(["KJ", "NAME"])]
|
|
14
|
+
# doc.token_keys # => ["KJ|NAME"]
|
|
15
|
+
# doc.to_s # => "Hello {KJ|NAME}!"
|
|
16
|
+
# doc.text_only? # => false
|
|
17
|
+
#
|
|
18
|
+
# @example Fast-path for text without tokens
|
|
19
|
+
# doc = Token::Resolver::Document.new("No tokens here")
|
|
20
|
+
# doc.text_only? # => true
|
|
21
|
+
#
|
|
22
|
+
class Document
|
|
23
|
+
# @return [Array<Node::Text, Node::Token>] Parsed nodes
|
|
24
|
+
attr_reader :nodes
|
|
25
|
+
|
|
26
|
+
# @return [Config] The config used for parsing
|
|
27
|
+
attr_reader :config
|
|
28
|
+
|
|
29
|
+
# Parse input text into a Document.
|
|
30
|
+
#
|
|
31
|
+
# @param input [String] Text to parse for tokens
|
|
32
|
+
# @param config [Config] Token configuration (default: Config.default)
|
|
33
|
+
def initialize(input, config: Config.default)
|
|
34
|
+
@config = config
|
|
35
|
+
@input = input
|
|
36
|
+
@nodes = parse(input)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Return only the Token nodes.
|
|
40
|
+
#
|
|
41
|
+
# @return [Array<Node::Token>]
|
|
42
|
+
def tokens
|
|
43
|
+
@tokens ||= @nodes.select(&:token?)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Return the unique token keys found in the input.
|
|
47
|
+
#
|
|
48
|
+
# @return [Array<String>]
|
|
49
|
+
def token_keys
|
|
50
|
+
@token_keys ||= tokens.map(&:key).uniq
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Reconstruct the original input from nodes (roundtrip fidelity).
|
|
54
|
+
#
|
|
55
|
+
# @return [String]
|
|
56
|
+
def to_s
|
|
57
|
+
@nodes.map(&:to_s).join
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Whether the input contains no tokens.
|
|
61
|
+
#
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
def text_only?
|
|
64
|
+
tokens.empty?
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def parse(input)
|
|
70
|
+
# Fast-path: if input doesn't contain the pre delimiter, no tokens possible
|
|
71
|
+
return [Node::Text.new(input)] if input.nil? || input.empty? || !input.include?(@config.pre)
|
|
72
|
+
|
|
73
|
+
parser_class = Grammar.build(@config)
|
|
74
|
+
tree = parser_class.new.parse(input)
|
|
75
|
+
Transform.apply(tree, @config)
|
|
76
|
+
rescue Parslet::ParseFailed
|
|
77
|
+
# Grammar should never fail, but if it does, treat entire input as text
|
|
78
|
+
[Node::Text.new(input)]
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "parslet"
|
|
4
|
+
|
|
5
|
+
module Token
|
|
6
|
+
module Resolver
|
|
7
|
+
# Dynamically builds a Parslet::Parser subclass from a Config.
|
|
8
|
+
#
|
|
9
|
+
# The grammar recognizes structured tokens (e.g., `{KJ|GEM_NAME}`) within
|
|
10
|
+
# arbitrary text. It is designed to **never fail** — any input is valid.
|
|
11
|
+
# Unrecognized content (including incomplete tokens) becomes text nodes.
|
|
12
|
+
#
|
|
13
|
+
# @example Building and using a grammar
|
|
14
|
+
# parser_class = Token::Resolver::Grammar.build(Config.default)
|
|
15
|
+
# tree = parser_class.new.parse("Hello {KJ|NAME}!")
|
|
16
|
+
# # => [{:text=>"H"@0}, {:text=>"e"@1}, ..., {:token=>{:segments=>[...]}}, ...]
|
|
17
|
+
#
|
|
18
|
+
# @note The raw parslet tree contains one :text entry per character.
|
|
19
|
+
# Use Transform to coalesce these into proper Text nodes.
|
|
20
|
+
#
|
|
21
|
+
class Grammar
|
|
22
|
+
# Cache of built parser classes, keyed by Config.
|
|
23
|
+
# Config is frozen and implements #hash/#eql?, so this is safe.
|
|
24
|
+
# NOTE: This hash is mutated under mutex in .build; it cannot actually be frozen.
|
|
25
|
+
@cache = {} # rubocop:disable ThreadSafety/MutableClassInstanceVariable
|
|
26
|
+
@cache_mutex = Mutex.new
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
# Build (or retrieve from cache) a Parslet::Parser subclass for the given Config.
|
|
30
|
+
#
|
|
31
|
+
# @param config [Config] Token configuration
|
|
32
|
+
# @return [Class] A Parslet::Parser subclass
|
|
33
|
+
def build(config)
|
|
34
|
+
@cache_mutex.synchronize do
|
|
35
|
+
@cache[config] ||= build_parser_class(config)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Clear the grammar cache. Mostly useful for testing.
|
|
40
|
+
#
|
|
41
|
+
# @return [void]
|
|
42
|
+
def clear_cache!
|
|
43
|
+
@cache_mutex.synchronize do
|
|
44
|
+
@cache.clear
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def build_parser_class(config)
|
|
51
|
+
pre_str = config.pre
|
|
52
|
+
post_str = config.post
|
|
53
|
+
separators = config.separators
|
|
54
|
+
min_segs = config.min_segments
|
|
55
|
+
max_segs = config.max_segments
|
|
56
|
+
|
|
57
|
+
Class.new(Parslet::Parser) do
|
|
58
|
+
# A segment is one or more characters that are not a separator or post delimiter.
|
|
59
|
+
# We need to exclude ALL separators and the post delimiter.
|
|
60
|
+
define_method(:_config) { config }
|
|
61
|
+
|
|
62
|
+
# Build the set of strings that terminate a segment
|
|
63
|
+
terminators = ([post_str] + separators).uniq
|
|
64
|
+
|
|
65
|
+
# segment: one or more chars that aren't any terminator
|
|
66
|
+
rule(:segment) {
|
|
67
|
+
terminator_absent = terminators.map { |t| str(t).absent? }.reduce(:>>)
|
|
68
|
+
(terminator_absent >> any).repeat(1)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
# token: pre + segment + (sep + segment).repeat + post
|
|
72
|
+
# with min/max segment constraints
|
|
73
|
+
rule(:token) {
|
|
74
|
+
# Build the repeating part: (separator + segment)
|
|
75
|
+
# For sequential separators, we'd need to handle them specially.
|
|
76
|
+
# However, parslet rules are declarative, so we handle sequential seps
|
|
77
|
+
# by building a chain: first_sep >> segment >> second_sep >> segment >> ...
|
|
78
|
+
# For the general case with repeating last separator, we use a dynamic approach.
|
|
79
|
+
|
|
80
|
+
# Simple case: build "pre segment (sep segment)* post" and validate segment count after
|
|
81
|
+
# We use the first separator for the first boundary, second for the second, etc.
|
|
82
|
+
# Last separator repeats.
|
|
83
|
+
|
|
84
|
+
# For parslet, we need to build this statically. The simplest approach:
|
|
85
|
+
# Match pre + segment + (any_sep + segment)* + post, capture all segments,
|
|
86
|
+
# then validate count in the Transform step.
|
|
87
|
+
|
|
88
|
+
# Build alternation of all separators
|
|
89
|
+
sep_match = if separators.length == 1
|
|
90
|
+
str(separators[0])
|
|
91
|
+
else
|
|
92
|
+
separators.map { |s| str(s) }.reduce(:|)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
base = str(pre_str) >>
|
|
96
|
+
segment.as(:seg) >>
|
|
97
|
+
(sep_match >> segment.as(:seg)).repeat(min_segs - 1, max_segs ? max_segs - 1 : nil) >>
|
|
98
|
+
str(post_str)
|
|
99
|
+
|
|
100
|
+
base
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
# text_char: any single character that doesn't start a valid token
|
|
104
|
+
rule(:text_char) {
|
|
105
|
+
# If we see pre_str, try to match a token. If it fails, consume pre_str as text.
|
|
106
|
+
# Parslet's ordered choice handles this: token is tried first in document.
|
|
107
|
+
# Here we just need to match any single character.
|
|
108
|
+
any
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
# document: sequence of tokens and text characters
|
|
112
|
+
rule(:document) {
|
|
113
|
+
(token.as(:token) | text_char.as(:text)).repeat
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
root(:document)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Token
|
|
4
|
+
module Resolver
|
|
5
|
+
module Node
|
|
6
|
+
# Represents plain text content (not a token).
|
|
7
|
+
#
|
|
8
|
+
# Text nodes hold the literal string content between (or outside of) tokens.
|
|
9
|
+
# They are frozen after creation.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# text = Token::Resolver::Node::Text.new("Hello ")
|
|
13
|
+
# text.to_s # => "Hello "
|
|
14
|
+
# text.content # => "Hello "
|
|
15
|
+
#
|
|
16
|
+
class Text
|
|
17
|
+
# @return [String] The text content
|
|
18
|
+
attr_reader :content
|
|
19
|
+
|
|
20
|
+
# @param content [String] The text content
|
|
21
|
+
def initialize(content)
|
|
22
|
+
@content = content.frozen? ? content : content.dup.freeze
|
|
23
|
+
freeze
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @return [String] The text content
|
|
27
|
+
def to_s
|
|
28
|
+
@content
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @return [Boolean]
|
|
32
|
+
def token?
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @return [Boolean]
|
|
37
|
+
def text?
|
|
38
|
+
true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Equality based on content.
|
|
42
|
+
#
|
|
43
|
+
# @param other [Object]
|
|
44
|
+
# @return [Boolean]
|
|
45
|
+
def eql?(other)
|
|
46
|
+
other.is_a?(Text) && content == other.content
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
alias_method :==, :eql?
|
|
50
|
+
|
|
51
|
+
# @return [Integer]
|
|
52
|
+
def hash
|
|
53
|
+
[self.class, content].hash
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @return [String]
|
|
57
|
+
def inspect
|
|
58
|
+
"#<#{self.class} #{content.inspect}>"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Token
|
|
4
|
+
module Resolver
|
|
5
|
+
module Node
|
|
6
|
+
# Represents a structured token found in the input.
|
|
7
|
+
#
|
|
8
|
+
# A token consists of segments separated by configured separators,
|
|
9
|
+
# wrapped in pre/post delimiters. For example, with default config,
|
|
10
|
+
# `{KJ|GEM_NAME}` has segments `["KJ", "GEM_NAME"]`.
|
|
11
|
+
#
|
|
12
|
+
# Token nodes are frozen after creation.
|
|
13
|
+
#
|
|
14
|
+
# @example Single separator
|
|
15
|
+
# token = Token::Resolver::Node::Token.new(["KJ", "GEM_NAME"], config)
|
|
16
|
+
# token.key # => "KJ|GEM_NAME"
|
|
17
|
+
# token.prefix # => "KJ"
|
|
18
|
+
# token.segments # => ["KJ", "GEM_NAME"]
|
|
19
|
+
# token.to_s # => "{KJ|GEM_NAME}"
|
|
20
|
+
#
|
|
21
|
+
# @example Sequential separators
|
|
22
|
+
# config = Config.new(separators: ["|", ":"])
|
|
23
|
+
# token = Token::Resolver::Node::Token.new(["KJ", "SECTION", "NAME"], config)
|
|
24
|
+
# token.key # => "KJ|SECTION:NAME"
|
|
25
|
+
# token.to_s # => "{KJ|SECTION:NAME}"
|
|
26
|
+
#
|
|
27
|
+
class Token
|
|
28
|
+
# @return [Array<String>] The token segments
|
|
29
|
+
attr_reader :segments
|
|
30
|
+
|
|
31
|
+
# @return [Config] The config used to parse this token
|
|
32
|
+
attr_reader :config
|
|
33
|
+
|
|
34
|
+
# @param segments [Array<String>] The token segments
|
|
35
|
+
# @param config [Config] The config that defined this token's structure
|
|
36
|
+
def initialize(segments, config)
|
|
37
|
+
@segments = segments.map { |s| s.frozen? ? s : s.dup.freeze }.freeze
|
|
38
|
+
@config = config
|
|
39
|
+
freeze
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The canonical key for this token, suitable for use as a replacement map key.
|
|
43
|
+
#
|
|
44
|
+
# Joins segments using the actual separators in order. For `separators: ["|", ":"]`
|
|
45
|
+
# and segments `["KJ", "SECTION", "NAME"]`, returns `"KJ|SECTION:NAME"`.
|
|
46
|
+
#
|
|
47
|
+
# @return [String]
|
|
48
|
+
def key
|
|
49
|
+
return @segments[0] if @segments.length == 1
|
|
50
|
+
|
|
51
|
+
result = +""
|
|
52
|
+
@segments.each_with_index do |seg, i|
|
|
53
|
+
if i > 0
|
|
54
|
+
result << @config.separator_at(i - 1)
|
|
55
|
+
end
|
|
56
|
+
result << seg
|
|
57
|
+
end
|
|
58
|
+
result.freeze
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The first segment (typically a prefix/namespace like "KJ").
|
|
62
|
+
#
|
|
63
|
+
# @return [String]
|
|
64
|
+
def prefix
|
|
65
|
+
@segments[0]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Reconstruct the original token string with delimiters.
|
|
69
|
+
#
|
|
70
|
+
# @return [String]
|
|
71
|
+
def to_s
|
|
72
|
+
"#{@config.pre}#{key}#{@config.post}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# @return [Boolean]
|
|
76
|
+
def token?
|
|
77
|
+
true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# @return [Boolean]
|
|
81
|
+
def text?
|
|
82
|
+
false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Equality based on segments and config.
|
|
86
|
+
#
|
|
87
|
+
# @param other [Object]
|
|
88
|
+
# @return [Boolean]
|
|
89
|
+
def eql?(other)
|
|
90
|
+
other.is_a?(Token) && segments == other.segments && config == other.config
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
alias_method :==, :eql?
|
|
94
|
+
|
|
95
|
+
# @return [Integer]
|
|
96
|
+
def hash
|
|
97
|
+
[self.class, segments, config].hash
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# @return [String]
|
|
101
|
+
def inspect
|
|
102
|
+
"#<#{self.class} #{to_s.inspect} segments=#{segments.inspect}>"
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|