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(case_insensitive_root: false, fail_fast: false, target: nil) ⇒ Context

Returns a new instance of Context.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/js_regex/converter/context.rb', line 15

def initialize(case_insensitive_root: false, fail_fast: false, target: nil)
  self.added_capturing_groups_after_group = Hash.new(0)
  self.capturing_group_count = 0
  self.fail_fast = fail_fast
  self.recursions_per_expression = {}
  self.required_options_hash = {}
  self.warnings = []

  self.case_insensitive_root = case_insensitive_root
  self.target = target
end

Instance Attribute Details

#capturing_group_countObject

Returns the value of attribute capturing_group_count.



9
10
11
# File 'lib/js_regex/converter/context.rb', line 9

def capturing_group_count
  @capturing_group_count
end

#case_insensitive_rootObject

Returns the value of attribute case_insensitive_root.



9
10
11
# File 'lib/js_regex/converter/context.rb', line 9

def case_insensitive_root
  @case_insensitive_root
end

#fail_fastObject

Returns the value of attribute fail_fast.



9
10
11
# File 'lib/js_regex/converter/context.rb', line 9

def fail_fast
  @fail_fast
end

#in_atomic_groupObject

Returns the value of attribute in_atomic_group.



9
10
11
# File 'lib/js_regex/converter/context.rb', line 9

def in_atomic_group
  @in_atomic_group
end

#warningsObject

Returns the value of attribute warnings.



9
10
11
# File 'lib/js_regex/converter/context.rb', line 9

def warnings
  @warnings
end

Instance Method Details

#capture_groupObject

group context



55
56
57
# File 'lib/js_regex/converter/context.rb', line 55

def capture_group
  self.capturing_group_count = capturing_group_count + 1
end

#count_recursion(exp) ⇒ Object



76
77
78
# File 'lib/js_regex/converter/context.rb', line 76

def count_recursion(exp)
  recursions_per_expression[recursion_id(exp)] = recursions(exp) + 1
end

#enable_u_optionObject

these methods allow appending options to the final Conversion output



39
40
41
42
43
# File 'lib/js_regex/converter/context.rb', line 39

def enable_u_option
  return false unless es_2015_or_higher?

  required_options_hash['u'] = true
end

#end_atomic_groupObject



63
64
65
# File 'lib/js_regex/converter/context.rb', line 63

def end_atomic_group
  self.in_atomic_group = false
end

#es_2015_or_higher?Boolean

target context

Returns:

  • (Boolean)


29
30
31
# File 'lib/js_regex/converter/context.rb', line 29

def es_2015_or_higher?
  target >= Target::ES2015
end

#es_2018_or_higher?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/js_regex/converter/context.rb', line 33

def es_2018_or_higher?
  target >= Target::ES2018
end

#increment_local_capturing_group_countObject



67
68
69
70
# File 'lib/js_regex/converter/context.rb', line 67

def increment_local_capturing_group_count
  added_capturing_groups_after_group[original_capturing_group_count] += 1
  capture_group
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.



86
87
88
89
90
91
92
# File 'lib/js_regex/converter/context.rb', line 86

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



94
95
96
# File 'lib/js_regex/converter/context.rb', line 94

def original_capturing_group_count
  capturing_group_count - total_added_capturing_groups
end

#recursion_id(exp) ⇒ Object



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

def recursion_id(exp)
  [exp.class, exp.starts_at]
end

#recursions(exp) ⇒ Object



72
73
74
# File 'lib/js_regex/converter/context.rb', line 72

def recursions(exp)
  recursions_per_expression[recursion_id(exp)] || 0
end

#required_optionsObject



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

def required_options
  required_options_hash.keys
end

#start_atomic_groupObject



59
60
61
# File 'lib/js_regex/converter/context.rb', line 59

def start_atomic_group
  self.in_atomic_group = true
end

#u?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/js_regex/converter/context.rb', line 45

def u?
  required_options_hash['u']
end