class Mustermann::AST::Compiler::Capture
Capture
compilation is complex. :( @!visibility private
Public Instance Methods
pattern(capture: nil, **options)
click to toggle source
@return [String] regexp without the named capture @!visibility private
# File lib/mustermann/ast/compiler.rb, line 49 def pattern(capture: nil, **options) case capture when Symbol then from_symbol(capture, **options) when Array then from_array(capture, **options) when Hash then from_hash(capture, **options) when String then from_string(capture, **options) when nil then from_nil(**options) else capture end end
translate(**options)
click to toggle source
@!visibility private
# File lib/mustermann/ast/compiler.rb, line 42 def translate(**options) return pattern(**options) if options[:no_captures] "(?<#{name}>#{translate(no_captures: true, **options)})" end
Private Instance Methods
default(**options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 68 def default(**options) constraint || "[^/\\?#]" end
from_array(array, **options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 64 def from_array(array, **options) Regexp.union(*array.map { |e| pattern(capture: e, **options) }) end
from_hash(hash, **options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 63 def from_hash(hash, **options) pattern(capture: hash[name.to_sym], **options) end
from_nil(**options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 67 def from_nil(**options) qualified(with_lookahead(default(**options), **options), **options) end
from_string(string, **options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 66 def from_string(string, **options) Regexp.new(string.chars.map { |c| t.encoded(c, **options) }.join) end
from_symbol(symbol, **options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 65 def from_symbol(symbol, **options) qualified(with_lookahead("[[:#{symbol}:]]", **options), **options) end
qualified(string, greedy: true, **options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 61 def qualified(string, greedy: true, **options) "#{string}#{qualifier || "+#{?? unless greedy}"}" end
with_lookahead(string, lookahead: nil, **options)
click to toggle source
# File lib/mustermann/ast/compiler.rb, line 62 def with_lookahead(string, lookahead: nil, **options) lookahead ? "(?:(?!#{lookahead})#{string})" : string end