Module: TokenKit::RegexConverter

Extended by:
RegexConverter
Included in:
RegexConverter
Defined in:
lib/tokenkit/regex_converter.rb

Overview

Converts Ruby Regexp objects to Rust-compatible regex strings

Instance Method Summary collapse

Instance Method Details

#patterns_to_rust(patterns) ⇒ Array<String>

Convert an array of patterns to Rust regex strings

Parameters:

  • patterns (Array<Regexp, String>)

    The patterns to convert

Returns:

  • (Array<String>)

    Rust-compatible regex strings



27
28
29
30
31
# File 'lib/tokenkit/regex_converter.rb', line 27

def patterns_to_rust(patterns)
  return [] unless patterns

  patterns.map { |p| to_rust(p) }
end

#to_rust(pattern) ⇒ String

Convert a Ruby Regexp to Rust regex syntax

Parameters:

  • pattern (Regexp, String)

    The pattern to convert

Returns:

  • (String)

    Rust-compatible regex string



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tokenkit/regex_converter.rb', line 11

def to_rust(pattern)
  return pattern.to_s unless pattern.is_a?(Regexp)

  flags = extract_flags(pattern)
  source = pattern.source

  if flags.empty?
    source
  else
    "(?#{flags})#{source}"
  end
end

#validate!(pattern) ⇒ Boolean

Validate a regex pattern

Parameters:

  • pattern (String)

    The regex pattern to validate

Returns:

  • (Boolean)

    true if valid

Raises:



37
38
39
40
41
42
43
# File 'lib/tokenkit/regex_converter.rb', line 37

def validate!(pattern)
  # Try to compile it in Ruby first
  Regexp.new(pattern)
  true
rescue RegexpError => e
  raise Error, "Invalid regex pattern '#{pattern}': #{e.message}"
end