Class: Cucumber::CucumberExpressions::TreeRegexp
- Inherits:
-
Object
- Object
- Cucumber::CucumberExpressions::TreeRegexp
- Defined in:
- lib/cucumber/cucumber_expressions/tree_regexp.rb
Instance Attribute Summary collapse
-
#group_builder ⇒ Object
readonly
Returns the value of attribute group_builder.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
Instance Method Summary collapse
-
#initialize(regexp) ⇒ TreeRegexp
constructor
A new instance of TreeRegexp.
- #match(s) ⇒ Object
Constructor Details
#initialize(regexp) ⇒ TreeRegexp
Returns a new instance of TreeRegexp.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cucumber/cucumber_expressions/tree_regexp.rb', line 8 def initialize(regexp) @regexp = regexp.is_a?(Regexp) ? regexp : Regexp.new(regexp) stack = [GroupBuilder.new] group_start_stack = [] last = nil non_capturing_maybe = false @regexp.source.split('').each_with_index do |c, n| if c == '(' && last != '\\' stack.push(GroupBuilder.new) group_start_stack.push(n+1) non_capturing_maybe = false elsif c == ')' && last != '\\' gb = stack.pop group_start = group_start_stack.pop if gb.capturing? gb.source = @regexp.source[group_start...n] stack.last.add(gb) else gb.move_children_to(stack.last) end non_capturing_maybe = false elsif c == '?' && last == '(' non_capturing_maybe = true elsif c == ':' && non_capturing_maybe stack.last.set_non_capturing! non_capturing_maybe = false end last = c end @group_builder = stack.pop end |
Instance Attribute Details
#group_builder ⇒ Object (readonly)
Returns the value of attribute group_builder.
6 7 8 |
# File 'lib/cucumber/cucumber_expressions/tree_regexp.rb', line 6 def group_builder @group_builder end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
6 7 8 |
# File 'lib/cucumber/cucumber_expressions/tree_regexp.rb', line 6 def regexp @regexp end |
Instance Method Details
#match(s) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/cucumber/cucumber_expressions/tree_regexp.rb', line 41 def match(s) match = @regexp.match(s) return nil if match.nil? group_indices = (0..match.length).to_a.to_enum @group_builder.build(match, group_indices) end |