Class: Ruby::Rego::Builtins::Glob::GlobCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/rego/builtins/glob/compiler.rb

Overview

Translates a glob pattern into a Ruby regex fragment. A fresh instance is used per compile so the scan position is not shared state. rubocop:disable Metrics/ClassLength

Constant Summary collapse

MAX_BRACE_NESTING =

Bounds brace-group nesting so a pathological pattern (e.g. deeply nested {{{...) cannot exhaust the stack during compilation; exceeding it is treated as a malformed pattern (undefined result).

100
MAX_COMPILED_SIZE =

Bounds the generated regex source so a pattern cannot force a super-linear build at compile time. Each */? re-emits the (delimiter-sized) non-delimiter class, so a long pattern over many delimiters would otherwise produce an O(pattern x delimiters) source before the match timeout applies.

1 << 20

Instance Method Summary collapse

Constructor Details

#initialize(pattern, seps) ⇒ GlobCompiler

Returns a new instance of GlobCompiler.

Parameters:

  • pattern (String)
  • seps (Array[String])

    delimiter characters (empty means “no delimiters”)



25
26
27
28
29
30
# File 'lib/ruby/rego/builtins/glob/compiler.rb', line 25

def initialize(pattern, seps)
  @chars = pattern.chars
  @nonsep = non_delimiter_class(seps)
  @brace_depth = 0
  @size = 0
end

Instance Method Details

#compileString

Returns regex source (unanchored).

Returns:

  • (String)

    regex source (unanchored)



33
34
35
36
37
38
# File 'lib/ruby/rego/builtins/glob/compiler.rb', line 33

def compile
  source, pos = translate(0, top_level: true)
  raise_malformed if pos < @chars.length

  source
end