Class: RegexpExamples::ChargroupParser
- Inherits:
-
Object
- Object
- RegexpExamples::ChargroupParser
- Includes:
- CharsetNegationHelper
- Defined in:
- lib/regexp-examples/chargroup_parser.rb
Overview
A “sub-parser”, for char groups in a regular expression Some examples of what this class needs to parse:
Instance Attribute Summary collapse
-
#current_position ⇒ Object
(also: #length)
readonly
Returns the value of attribute current_position.
-
#regexp_string ⇒ Object
readonly
Returns the value of attribute regexp_string.
Instance Method Summary collapse
-
#initialize(regexp_string, is_sub_group: false) ⇒ ChargroupParser
constructor
A new instance of ChargroupParser.
- #parse ⇒ Object
- #result ⇒ Object
Methods included from CharsetNegationHelper
Constructor Details
#initialize(regexp_string, is_sub_group: false) ⇒ ChargroupParser
Returns a new instance of ChargroupParser.
20 21 22 23 24 25 26 |
# File 'lib/regexp-examples/chargroup_parser.rb', line 20 def initialize(regexp_string, is_sub_group: false) @regexp_string = regexp_string @is_sub_group = is_sub_group @current_position = 0 @charset = [] @negative = false end |
Instance Attribute Details
#current_position ⇒ Object (readonly) Also known as: length
Returns the value of attribute current_position.
17 18 19 |
# File 'lib/regexp-examples/chargroup_parser.rb', line 17 def current_position @current_position end |
#regexp_string ⇒ Object (readonly)
Returns the value of attribute regexp_string.
17 18 19 |
# File 'lib/regexp-examples/chargroup_parser.rb', line 17 def regexp_string @regexp_string end |
Instance Method Details
#parse ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/regexp-examples/chargroup_parser.rb', line 28 def parse parse_first_chars until next_char == ']' case next_char when '[' parse_sub_group_concat when '-' parse_after_hyphen when '&' parse_after_ampersand else @charset.concat parse_checking_backlash @current_position += 1 end end @charset.uniq! @current_position += 1 # To account for final "]" end |
#result ⇒ Object
48 49 50 |
# File 'lib/regexp-examples/chargroup_parser.rb', line 48 def result negate_if(@charset, @negative) end |