lib/regexp-examples/parser.rb in regexp-examples-0.6.0 vs lib/regexp-examples/parser.rb in regexp-examples-0.7.0
- old
+ new
@@ -1,6 +1,7 @@
module RegexpExamples
+ IllegalSyntaxError = Class.new(StandardError)
class Parser
attr_reader :regexp_string
def initialize(regexp_string, regexp_options, config_options={})
@regexp_string = regexp_string
@ignorecase = !(regexp_options & Regexp::IGNORECASE).zero?
@@ -83,12 +84,10 @@
when rest_of_string =~ /\Ak<([^>]+)>/ # Named capture group
@current_position += ($1.length + 2)
group = parse_backreference_group($1)
when BackslashCharMap.keys.include?(next_char)
group = CharGroup.new(
- # Note: The `.dup` is important, as it prevents modifying the constant, in
- # CharGroup#init_ranges (where the '-' is moved to the front)
BackslashCharMap[next_char].dup,
@ignorecase
)
when rest_of_string =~ /\A(c|C-)(.)/ # Control character
@current_position += $1.length
@@ -98,19 +97,25 @@
group = parse_single_char_group( parse_escape_sequence($1) )
when rest_of_string =~ /\Au(\h{4}|\{\h{1,4}\})/ # Unicode sequence
@current_position += $1.length
sequence = $1.match(/\h{1,4}/)[0] # Strip off "{" and "}"
group = parse_single_char_group( parse_unicode_sequence(sequence) )
- when rest_of_string =~ /\Ap\{([^}]+)\}/ # Named properties
- @current_position += ($1.length + 2)
- raise UnsupportedSyntaxError, "Named properties ({\\p#{$1}}) are not yet supported"
+ when rest_of_string =~ /\Ap\{(\^?)([^}]+)\}/ # Named properties
+ @current_position += ($1.length + $2.length + 2)
+ group = CharGroup.new(
+ if($1 == "^")
+ CharSets::Any.dup - NamedPropertyCharMap[$2]
+ else
+ NamedPropertyCharMap[$2]
+ end,
+ @ignorecase
+ )
when next_char == 'K' # Keep (special lookbehind that CAN be supported safely!)
group = PlaceHolderGroup.new
when next_char == 'R' # Linebreak
group = CharGroup.new(["\r\n", "\n", "\v", "\f", "\r"], @ignorecase) # A bit hacky...
when next_char == 'g' # Subexpression call
- # TODO: Should this be IllegalSyntaxError ?
- raise UnsupportedSyntaxError, "Subexpression calls (\g) are not yet supported"
+ raise IllegalSyntaxError, "Subexpression calls (\g) are not yet supported"
when next_char =~ /[bB]/ # Anchors
raise IllegalSyntaxError, "Anchors ('\\#{next_char}') cannot be supported, as they are not regular"
when next_char =~ /[AG]/ # Start of string
if @current_position == 1
group = PlaceHolderGroup.new