Module: TokenKit::RegexConverter
Overview
Converts Ruby Regexp objects to Rust-compatible regex strings
Instance Method Summary collapse
-
#patterns_to_rust(patterns) ⇒ Array<String>
Convert an array of patterns to Rust regex strings.
-
#to_rust(pattern) ⇒ String
Convert a Ruby Regexp to Rust regex syntax.
-
#validate!(pattern) ⇒ Boolean
Validate a regex pattern.
Instance Method Details
#patterns_to_rust(patterns) ⇒ Array<String>
Convert an array of patterns to Rust 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
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
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.}" end |