Class: JsRegex::Converter::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/js_regex/converter/context.rb

Overview

Passed among Converters to globalize basic status data.

The Converters themselves are stateless.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_regex) ⇒ Context

Returns a new instance of Context.



19
20
21
22
23
24
25
26
27
# File 'lib/js_regex/converter/context.rb', line 19

def initialize(ruby_regex)
  self.added_capturing_groups_after_group = Hash.new(0)
  self.capturing_group_count = 0
  self.named_group_positions = {}
  self.warnings = []

  self.root_options = {}
  root_options[:m] = !(ruby_regex.options & Regexp::MULTILINE).equal?(0)
end

Instance Attribute Details

#buffered_set_extractionsObject

Returns the value of attribute buffered_set_extractions.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def buffered_set_extractions
  @buffered_set_extractions
end

#buffered_set_membersObject

Returns the value of attribute buffered_set_members.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def buffered_set_members
  @buffered_set_members
end

#in_atomic_groupObject

Returns the value of attribute in_atomic_group.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def in_atomic_group
  @in_atomic_group
end

#named_group_positionsObject

Returns the value of attribute named_group_positions.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def named_group_positions
  @named_group_positions
end

#negative_base_setObject

Returns the value of attribute negative_base_set.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def negative_base_set
  @negative_base_set
end

#root_optionsObject

Returns the value of attribute root_options.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def root_options
  @root_options
end

#warningsObject

Returns the value of attribute warnings.



11
12
13
# File 'lib/js_regex/converter/context.rb', line 11

def warnings
  @warnings
end

Instance Method Details

#capture_groupObject

group context



49
50
51
# File 'lib/js_regex/converter/context.rb', line 49

def capture_group
  self.capturing_group_count = capturing_group_count + 1
end

#end_atomic_groupObject



57
58
59
# File 'lib/js_regex/converter/context.rb', line 57

def end_atomic_group
  self.in_atomic_group = false
end

#multiline?Boolean

option context

Returns:

  • (Boolean)


31
32
33
# File 'lib/js_regex/converter/context.rb', line 31

def multiline?
  root_options.fetch(:m)
end

#negate_base_setObject

set context



37
38
39
# File 'lib/js_regex/converter/context.rb', line 37

def negate_base_set
  self.negative_base_set = true
end

#new_capturing_group_position(old_position) ⇒ Object

takes and returns 1-indexed group positions. new is different from old if capturing groups were added in between.



72
73
74
75
76
77
78
# File 'lib/js_regex/converter/context.rb', line 72

def new_capturing_group_position(old_position)
  increment = 0
  added_capturing_groups_after_group.each do |after_n_groups, count|
    increment += count if after_n_groups < old_position
  end
  old_position + increment
end

#original_capturing_group_countObject



80
81
82
# File 'lib/js_regex/converter/context.rb', line 80

def original_capturing_group_count
  capturing_group_count - total_added_capturing_groups
end

#reset_set_contextObject



41
42
43
44
45
# File 'lib/js_regex/converter/context.rb', line 41

def reset_set_context
  self.buffered_set_extractions = []
  self.buffered_set_members = []
  self.negative_base_set = false
end

#start_atomic_groupObject



53
54
55
# File 'lib/js_regex/converter/context.rb', line 53

def start_atomic_group
  self.in_atomic_group = true
end

#store_named_group_position(name) ⇒ Object



88
89
90
# File 'lib/js_regex/converter/context.rb', line 88

def store_named_group_position(name)
  named_group_positions[name] = capturing_group_count + 1
end

#total_added_capturing_groupsObject



84
85
86
# File 'lib/js_regex/converter/context.rb', line 84

def total_added_capturing_groups
  added_capturing_groups_after_group.values.inject(0, &:+)
end

#wrap_in_backrefed_lookahead(content) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/js_regex/converter/context.rb', line 61

def wrap_in_backrefed_lookahead(content)
  new_backref_num = capturing_group_count + 1
  # an empty passive group (?:) is appended as literal digits may follow
  result = "(?=(#{content}))\\#{new_backref_num}(?:)"
  added_capturing_groups_after_group[original_capturing_group_count] += 1
  capture_group
  result
end