Module: JsRegex::SecondPass
- Defined in:
- lib/js_regex/second_pass.rb
Overview
After conversion of a full Regexp::Expression tree, this class checks for Node instances that need further processing.
E.g. subexpression calls (such as g<1>) can be look-ahead, so the full Regexp must have been processed first, and only then can they be substituted with the conversion result of their targeted group.
Class Method Summary collapse
- .alternate_conditional_permutations(tree) ⇒ Object
- .call(tree) ⇒ Object
- .captured_group_count(tree) ⇒ Object
- .condition_permutations(conditions) ⇒ Object
- .conditional_tree_permutations(tree) ⇒ Object
- .conditions(tree) ⇒ Object
- .crawl(node) {|node| ... } ⇒ Object
- .find_group_by_reference(ref, in_node: nil) ⇒ Object
- .min_quantify(node) ⇒ Object
- .null_quantify(node) ⇒ Object
- .substitute_subexp_calls(tree) ⇒ Object
Class Method Details
.alternate_conditional_permutations(tree) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/js_regex/second_pass.rb', line 36 def alternate_conditional_permutations(tree) permutations = conditional_tree_permutations(tree) return tree if permutations.empty? alternatives = permutations.map.with_index do |variant, i| Node.new((i.zero? ? '(?:' : '|(?:'), variant, ')') end tree.update(children: alternatives) end |
.call(tree) ⇒ Object
15 16 17 18 19 |
# File 'lib/js_regex/second_pass.rb', line 15 def call(tree) substitute_subexp_calls(tree) alternate_conditional_permutations(tree) tree end |
.captured_group_count(tree) ⇒ Object
92 93 94 95 96 |
# File 'lib/js_regex/second_pass.rb', line 92 def captured_group_count(tree) count = 0 crawl(tree) { |node| count += 1 if node.type.equal?(:captured_group)} count end |
.condition_permutations(conditions) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/js_regex/second_pass.rb', line 98 def condition_permutations(conditions) return [] if conditions.empty? condition_permutations = (0..(conditions.length)).inject([]) do |arr, n| arr += conditions.combination(n).to_a end end |
.conditional_tree_permutations(tree) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/js_regex/second_pass.rb', line 53 def conditional_tree_permutations(tree) all_conditions = conditions(tree) return [] if all_conditions.empty? captured_groups_per_branch = captured_group_count(tree) condition_permutations(all_conditions).map.with_index do |truthy_conds, i| tree_permutation = tree.clone # find referenced groups and conditionals and make one-sided crawl(tree_permutation) do |node| truthy = truthy_conds.include?(node.reference) if node.type.equal?(:captured_group) && all_conditions.include?(node.reference) truthy ? min_quantify(node) : null_quantify(node) elsif node.type.equal?(:conditional) branches = node.children[1...-1] if branches.count == 1 truthy || null_quantify(branches.first) else null_quantify(truthy ? branches.last : branches.first) end node.update(type: :plain) elsif node.type.equal?(:backref_num) new_num = node.children[0].to_i + captured_groups_per_branch * i node.update(children: [new_num.to_s]) end end end end |
.conditions(tree) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/js_regex/second_pass.rb', line 84 def conditions(tree) conditions = [] crawl(tree) do |node| conditions << node.reference if node.type.equal?(:conditional) end conditions end |
.crawl(node) {|node| ... } ⇒ Object
30 31 32 33 34 |
# File 'lib/js_regex/second_pass.rb', line 30 def crawl(node, &block) return if node.instance_of?(String) yield(node) node.children.each { |child| crawl(child, &block) } end |
.find_group_by_reference(ref, in_node: nil) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/js_regex/second_pass.rb', line 46 def find_group_by_reference(ref, in_node: nil) crawl(in_node) do |node| return node if node.type == :captured_group && node.reference == ref end Node.new('()') end |
.min_quantify(node) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/js_regex/second_pass.rb', line 106 def min_quantify(node) return if (qtf = node.quantifier).nil? || qtf.min > 0 if qtf.max.equal?(1) # any zero_or_one quantifier (?, ??, ?+) node.update(quantifier: nil) else node.update(quantifier: "{1,#{qtf.max}}#{'?' if qtf.reluctant?}") end end |
.null_quantify(node) ⇒ Object
116 117 118 |
# File 'lib/js_regex/second_pass.rb', line 116 def null_quantify(node) node.update(quantifier: '{0}') end |
.substitute_subexp_calls(tree) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/js_regex/second_pass.rb', line 21 def substitute_subexp_calls(tree) crawl(tree) do |node| if node.type == :subexp_call called_group = find_group_by_reference(node.reference, in_node: tree) node.update(children: called_group.children, type: :captured_group) end end end |