Skip to content

Commit

Permalink
[Truffle] Support named captures in MatchData.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Feb 3, 2015
1 parent a10fccc commit ea2584a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
8 changes: 0 additions & 8 deletions spec/truffle/tags/core/matchdata/element_reference_tags.txt
@@ -1,10 +1,2 @@
fails:MatchData#[] supports accessors [start, length]
fails:MatchData#[] supports ranges [start..end]
fails:MatchData#[Symbol] returns the corresponding named match when given a Symbol
fails:MatchData#[Symbol] returns the corresponding named match when given a String
fails:MatchData#[Symbol] returns the matching version of multiple corresponding named match
fails:MatchData#[Symbol] returns the last match when multiple named matches exist with the same name
fails:MatchData#[Symbol] returns nil on non-matching named matches
fails:MatchData#[Symbol] raises an IndexError if there is no named match corresponding to the Symbol
fails:MatchData#[Symbol] raises an IndexError if there is no named match corresponding to the String
fails:MatchData#[Symbol] returns matches in the String's encoding
Expand Up @@ -13,11 +13,13 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.joni.exception.ValueException;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyMatchData;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.core.RubySymbol;
import org.jruby.util.ByteList;

@CoreClass(name = "MatchData")
Expand Down Expand Up @@ -45,6 +47,39 @@ public Object getIndex(RubyMatchData matchData, int index) {
}
}

@Specialization
public Object getIndex(RubyMatchData matchData, RubySymbol index) {
notDesignedForCompilation();

try {
final int i = matchData.getBackrefNumber(index.getSymbolBytes());

return getIndex(matchData, i);
} catch (final ValueException e) {
CompilerDirectives.transferToInterpreter();

throw new RaiseException(
getContext().getCoreLibrary().indexError(String.format("undefined group name reference: %s", index.toString()), this));
}
}

@Specialization
public Object getIndex(RubyMatchData matchData, RubyString index) {
notDesignedForCompilation();

try {
final int i = matchData.getBackrefNumber(index.getByteList());

return getIndex(matchData, i);
}
catch (final ValueException e) {
CompilerDirectives.transferToInterpreter();

throw new RaiseException(
getContext().getCoreLibrary().indexError(String.format("undefined group name reference: %s", index.toString()), this));
}
}

}

@CoreMethod(names = "begin", required = 1, lowerFixnumParameters = 1)
Expand Down
Expand Up @@ -137,7 +137,7 @@ public Object matchCommon(ByteList bytes, boolean operator, boolean setNamedCapt
final RubyString post = new RubyString(context.getCoreLibrary().getStringClass(), bytes.makeShared(region.end[0], bytes.length() - region.end[0]).dup());
final RubyString global = new RubyString(context.getCoreLibrary().getStringClass(), bytes.makeShared(region.beg[0], region.end[0] - region.beg[0]).dup());

final RubyMatchData matchObject = new RubyMatchData(context.getCoreLibrary().getMatchDataClass(), region, values, pre, post, global);
final RubyMatchData matchObject = new RubyMatchData(context.getCoreLibrary().getMatchDataClass(), regex, region, values, pre, post, global);

if (operator) {
if (values.length > 0) {
Expand Down

0 comments on commit ea2584a

Please sign in to comment.