Class: Picoglob::Compiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/picoglob/compiler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Compiles a bash-style glob pattern into a Ruby Regexp source string.

Supported syntax:

*            any run of non-separator characters
**           any run of characters, including separators (globstar)
?            any single non-separator character
[abc] [a-z]  character class; [!...] or [^...] negates
{a,b,c}      brace alternation (one of the comma-separated alternatives)
{1..3}       numeric brace range expansion -> {1,2,3}
@(a|b)       exactly one of the patterns (extglob)
?(a|b)       zero or one of the patterns (extglob)
*(a|b)       zero or more of the patterns (extglob)
+(a|b)       one or more of the patterns (extglob)
!(a|b)       anything except the patterns (extglob)
\x           escape the next character (literal)

Instance Method Summary collapse

Constructor Details

#initialize(separator: "/", dot: false, extglob: true, nocase: false) ⇒ Compiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Compiler.

Parameters:

  • separator (String) (defaults to: "/")

    the path separator that ‘*`/`?` will not cross

  • dot (Boolean) (defaults to: false)

    when false, a leading ‘.` must be matched explicitly (a leading `*`/`?`/`[` will not match it), mirroring shell globbing

  • extglob (Boolean) (defaults to: true)

    enable extglob constructs (@/?/*/+/! followed by ‘(`)

  • nocase (Boolean) (defaults to: false)

    case-insensitive matching



30
31
32
33
34
35
# File 'lib/picoglob/compiler.rb', line 30

def initialize(separator: "/", dot: false, extglob: true, nocase: false)
  @sep = separator
  @dot = dot
  @extglob = extglob
  @nocase = nocase
end

Instance Method Details

#compile(pattern) ⇒ Regexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • pattern (String)

Returns:

  • (Regexp)

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/picoglob/compiler.rb', line 39

def compile(pattern)
  @chars = pattern.chars
  @pos = 0
  body = parse_sequence(top_level: true)
  raise ParseError, "unexpected #{current.inspect}" unless eof?

  src = "\\A#{body}\\z"
  Regexp.new(src, @nocase ? Regexp::IGNORECASE : nil)
end