Skip to content

Commit 4255ff6

Browse files
author
Tom Lord
committed
Replaced SingleCharGroup.new('') with PlaceHolderGroup.new
This makes the intention of these slightly-hacky "placeholders" more obvious. For example, /\K/, /\A/, /(?i)/, /a b/x all require a "placeholder" for these groups, due to how the regexp parser works.
1 parent baf8b36 commit 4255ff6

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

coverage/coverage-badge.png

8 Bytes
Loading

lib/regexp-examples/groups.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def result
4848
end
4949
end
5050

51+
# Used as a workaround for when a grep is expected to be returned,
52+
# but there are no results for the group.
53+
# i.e. PlaceHolderGroup.new.result == '' == SingleCharGroup.new('').result
54+
# (But using PlaceHolderGroup makes it clearer what the intention is!)
55+
class PlaceHolderGroup
56+
def result
57+
[GroupResult.new('')]
58+
end
59+
end
60+
5161
class CharGroup
5262
prepend GroupWithIgnoreCase
5363
def initialize(chars, ignorecase)

lib/regexp-examples/parser.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ def parse_group(repeaters)
4646
group = parse_after_backslash_group
4747
when '^'
4848
if @current_position == 0
49-
group = parse_single_char_group('') # Ignore the "illegal" character
49+
group = PlaceHolderGroup.new # Ignore the "illegal" character
5050
else
5151
raise IllegalSyntaxError, "Anchors ('#{next_char}') cannot be supported, as they are not regular"
5252
end
5353
when '$'
5454
if @current_position == (regexp_string.length - 1)
55-
group = parse_single_char_group('') # Ignore the "illegal" character
55+
group = PlaceHolderGroup.new # Ignore the "illegal" character
5656
else
5757
raise IllegalSyntaxError, "Anchors ('#{next_char}') cannot be supported, as they are not regular"
5858
end
5959
when /[#\s]/
6060
if @extended
6161
parse_extended_whitespace
62-
group = parse_single_char_group('') # Ignore the whitespace/comment
62+
group = PlaceHolderGroup.new # Ignore the whitespace/comment
6363
else
6464
group = parse_single_char_group(next_char)
6565
end
@@ -104,7 +104,7 @@ def parse_after_backslash_group
104104
@current_position += ($1.length + 2)
105105
raise UnsupportedSyntaxError, "Named properties ({\\p#{$1}}) are not yet supported"
106106
when next_char == 'K' # Keep (special lookbehind that CAN be supported safely!)
107-
group = parse_single_char_group('') # Ignore it
107+
group = PlaceHolderGroup.new
108108
when next_char == 'R' # Linebreak
109109
group = CharGroup.new(["\r\n", "\n", "\v", "\f", "\r"], @ignorecase) # A bit hacky...
110110
when next_char == 'g' # Subexpression call
@@ -114,13 +114,13 @@ def parse_after_backslash_group
114114
raise IllegalSyntaxError, "Anchors ('\\#{next_char}') cannot be supported, as they are not regular"
115115
when next_char =~ /[AG]/ # Start of string
116116
if @current_position == 1
117-
group = parse_single_char_group('') # Ignore the "illegal" character
117+
group = PlaceHolderGroup.new
118118
else
119119
raise IllegalSyntaxError, "Anchors ('\\#{next_char}') cannot be supported, as they are not regular"
120120
end
121121
when next_char =~ /[zZ]/ # End of string
122122
if @current_position == (regexp_string.length - 1)
123-
group = parse_single_char_group('') # Ignore the "illegal" character
123+
group = PlaceHolderGroup.new
124124
else
125125
raise IllegalSyntaxError, "Anchors ('\\#{next_char}') cannot be supported, as they are not regular"
126126
end
@@ -186,7 +186,7 @@ def parse_multi_group
186186
if next_char == ':' # e.g. /(?i:subexpr)/
187187
@current_position += 1
188188
else
189-
return parse_single_char_group('')
189+
return PlaceHolderGroup.new
190190
end
191191
when %w(! =).include?(match[2]) # e.g. /(?=lookahead)/, /(?!neglookahead)/
192192
raise IllegalSyntaxError, "Lookaheads are not regular; cannot generate examples"

0 commit comments

Comments
 (0)