Class: JsRegex::Converter::Context
- Inherits:
-
Object
- Object
- JsRegex::Converter::Context
- 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
-
#capturing_group_count ⇒ Object
readonly
Returns the value of attribute capturing_group_count.
-
#case_insensitive_root ⇒ Object
readonly
Returns the value of attribute case_insensitive_root.
-
#fail_fast ⇒ Object
readonly
Returns the value of attribute fail_fast.
-
#in_atomic_group ⇒ Object
readonly
Returns the value of attribute in_atomic_group.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
-
#capture_group ⇒ Object
group context.
- #count_recursion(exp) ⇒ Object
-
#enable_u_option ⇒ Object
these methods allow appending options to the final Conversion output.
- #end_atomic_group ⇒ Object
-
#es_2015_or_higher? ⇒ Boolean
target context.
- #es_2018_or_higher? ⇒ Boolean
- #increment_local_capturing_group_count ⇒ Object
-
#initialize(case_insensitive_root: false, fail_fast: false, target: nil) ⇒ Context
constructor
A new instance of Context.
-
#new_capturing_group_position(old_position) ⇒ Object
takes and returns 1-indexed group positions.
- #original_capturing_group_count ⇒ Object
- #recursion_id(exp) ⇒ Object
- #recursions(exp) ⇒ Object
- #required_options ⇒ Object
- #start_atomic_group ⇒ Object
- #u? ⇒ Boolean
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. = {} self.warnings = [] self.case_insensitive_root = case_insensitive_root self.target = target end |
Instance Attribute Details
#capturing_group_count ⇒ Object
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_root ⇒ Object
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_fast ⇒ Object
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_group ⇒ Object
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 |
#warnings ⇒ Object
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_group ⇒ Object
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_option ⇒ Object
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? ['u'] = true end |
#end_atomic_group ⇒ Object
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
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
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_count ⇒ Object
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_count ⇒ Object
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_options ⇒ Object
49 50 51 |
# File 'lib/js_regex/converter/context.rb', line 49 def .keys end |
#start_atomic_group ⇒ Object
59 60 61 |
# File 'lib/js_regex/converter/context.rb', line 59 def start_atomic_group self.in_atomic_group = true end |
#u? ⇒ Boolean
45 46 47 |
# File 'lib/js_regex/converter/context.rb', line 45 def u? ['u'] end |