|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved. This |
| 4 | +# code is released under a tri EPL/GPL/LGPL license. You can use it, |
| 5 | +# redistribute it and/or modify it under the terms of the: |
| 6 | +# |
| 7 | +# Eclipse Public License version 2.0, or |
| 8 | +# GNU General Public License version 2, or |
| 9 | +# GNU Lesser General Public License version 2.1. |
| 10 | + |
| 11 | +# Copyright (c) 2007-2015, Evan Phoenix and contributors |
| 12 | +# All rights reserved. |
| 13 | +# |
| 14 | +# Redistribution and use in source and binary forms, with or without |
| 15 | +# modification, are permitted provided that the following conditions are met: |
| 16 | +# |
| 17 | +# * Redistributions of source code must retain the above copyright notice, this |
| 18 | +# list of conditions and the following disclaimer. |
| 19 | +# * Redistributions in binary form must reproduce the above copyright notice |
| 20 | +# this list of conditions and the following disclaimer in the documentation |
| 21 | +# and/or other materials provided with the distribution. |
| 22 | +# * Neither the name of Rubinius nor the names of its contributors |
| 23 | +# may be used to endorse or promote products derived from this software |
| 24 | +# without specific prior written permission. |
| 25 | +# |
| 26 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 27 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 30 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 32 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 33 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 34 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 35 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | + |
| 37 | +class MatchData |
| 38 | + class << self |
| 39 | + # Prevent allocating MatchData, like MRI 2.7+, so we don't need to check if it's initialized |
| 40 | + undef_method :allocate |
| 41 | + end |
| 42 | + |
| 43 | + def offset(idx) |
| 44 | + [self.begin(idx), self.end(idx)] |
| 45 | + end |
| 46 | + |
| 47 | + def ==(other) |
| 48 | + return true if equal?(other) |
| 49 | + |
| 50 | + Primitive.object_kind_of?(other, MatchData) && |
| 51 | + string == other.string && |
| 52 | + regexp == other.regexp && |
| 53 | + captures == other.captures |
| 54 | + end |
| 55 | + alias_method :eql?, :== |
| 56 | + |
| 57 | + def string |
| 58 | + Primitive.match_data_get_source(self).dup.freeze |
| 59 | + end |
| 60 | + |
| 61 | + def captures |
| 62 | + to_a[1..-1] |
| 63 | + end |
| 64 | + |
| 65 | + def names |
| 66 | + regexp.names |
| 67 | + end |
| 68 | + |
| 69 | + def named_captures |
| 70 | + names.collect { |name| [name, self[name]] }.to_h |
| 71 | + end |
| 72 | + |
| 73 | + def begin(index) |
| 74 | + backref = if String === index || Symbol === index |
| 75 | + names_to_backref = Hash[Primitive.regexp_names(self.regexp)] |
| 76 | + names_to_backref[index.to_sym].last |
| 77 | + else |
| 78 | + Truffle::Type.coerce_to(index, Integer, :to_int) |
| 79 | + end |
| 80 | + |
| 81 | + |
| 82 | + Primitive.match_data_begin(self, backref) |
| 83 | + end |
| 84 | + |
| 85 | + def end(index) |
| 86 | + backref = if String === index || Symbol === index |
| 87 | + names_to_backref = Hash[Primitive.regexp_names(self.regexp)] |
| 88 | + names_to_backref[index.to_sym].last |
| 89 | + else |
| 90 | + Truffle::Type.coerce_to(index, Integer, :to_int) |
| 91 | + end |
| 92 | + |
| 93 | + |
| 94 | + Primitive.match_data_end(self, backref) |
| 95 | + end |
| 96 | + |
| 97 | + def inspect |
| 98 | + str = "#<MatchData \"#{self[0]}\"" |
| 99 | + idx = 0 |
| 100 | + captures.zip(names) do |capture, name| |
| 101 | + idx += 1 |
| 102 | + str << " #{name || idx}:#{capture.inspect}" |
| 103 | + end |
| 104 | + "#{str}>" |
| 105 | + end |
| 106 | + |
| 107 | + def values_at(*indexes) |
| 108 | + out = [] |
| 109 | + size = self.size |
| 110 | + |
| 111 | + indexes.each do |elem| |
| 112 | + if Primitive.object_kind_of?(elem, String) || Primitive.object_kind_of?(elem, Symbol) |
| 113 | + out << self[elem] |
| 114 | + elsif Primitive.object_kind_of?(elem, Range) |
| 115 | + start, length = Primitive.range_normalized_start_length(elem, size) |
| 116 | + finish = start + length - 1 |
| 117 | + |
| 118 | + raise RangeError, "#{elem} out of range" if start < 0 |
| 119 | + next if finish < start # ignore empty ranges |
| 120 | + |
| 121 | + finish_in_bounds = [finish, size - 1].min |
| 122 | + start.upto(finish_in_bounds) do |index| |
| 123 | + out << self[index] |
| 124 | + end |
| 125 | + |
| 126 | + (finish_in_bounds + 1).upto(finish) { out << nil } |
| 127 | + else |
| 128 | + index = Primitive.rb_num2int(elem) |
| 129 | + if index >= size || index < -size |
| 130 | + out << nil |
| 131 | + else |
| 132 | + out << self[index] |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + out |
| 138 | + end |
| 139 | + |
| 140 | + def match(n) |
| 141 | + # Similar, but #match accepts only single index/name, but not a range or an optional length. |
| 142 | + number = Truffle::Type.rb_check_convert_type(n, Integer, :to_int) |
| 143 | + return self[number] if number |
| 144 | + # To convert the last type (String) we used rb_convert_type instead of rb_check_convert_type which throws an exception |
| 145 | + name = Truffle::Type.rb_check_convert_type(n, Symbol, :to_sym) || Truffle::Type.rb_convert_type(n, String, :to_str) |
| 146 | + self[name] |
| 147 | + end |
| 148 | + |
| 149 | + def match_length(n) |
| 150 | + match(n)&.length |
| 151 | + end |
| 152 | + |
| 153 | + def to_s |
| 154 | + self[0] |
| 155 | + end |
| 156 | +end |
| 157 | + |
| 158 | +Truffle::KernelOperations.define_hooked_variable( |
| 159 | + :$~, |
| 160 | + -> s { Primitive.regexp_last_match_get(s) }, |
| 161 | + Truffle::RegexpOperations::LAST_MATCH_SET) |
| 162 | + |
| 163 | +Truffle::KernelOperations.define_hooked_variable( |
| 164 | + :'$`', |
| 165 | + -> s { match = Primitive.regexp_last_match_get(s) |
| 166 | + match.pre_match if match }, |
| 167 | + -> { raise SyntaxError, "Can't set variable $`" }, |
| 168 | + -> s { 'global-variable' if Primitive.regexp_last_match_get(s) }) |
| 169 | + |
| 170 | +Truffle::KernelOperations.define_hooked_variable( |
| 171 | + :"$'", |
| 172 | + -> s { match = Primitive.regexp_last_match_get(s) |
| 173 | + match.post_match if match }, |
| 174 | + -> { raise SyntaxError, "Can't set variable $'" }, |
| 175 | + -> s { 'global-variable' if Primitive.regexp_last_match_get(s) }) |
| 176 | + |
| 177 | +Truffle::KernelOperations.define_hooked_variable( |
| 178 | + :'$&', |
| 179 | + -> s { match = Primitive.regexp_last_match_get(s) |
| 180 | + match[0] if match }, |
| 181 | + -> { raise SyntaxError, "Can't set variable $&" }, |
| 182 | + -> s { 'global-variable' if Primitive.regexp_last_match_get(s) }) |
| 183 | + |
| 184 | +Truffle::KernelOperations.define_hooked_variable( |
| 185 | + :'$+', |
| 186 | + -> s { match = Primitive.regexp_last_match_get(s) |
| 187 | + match.captures.reverse.find { |m| !Primitive.nil?(m) } if match }, |
| 188 | + -> { raise SyntaxError, "Can't set variable $+" }, |
| 189 | + -> s { 'global-variable' if Primitive.regexp_last_match_get(s) }) |
0 commit comments