MatchDataNodes.java

  1. /*
  2.  * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
  3.  * code is released under a tri EPL/GPL/LGPL license. You can use it,
  4.  * redistribute it and/or modify it under the terms of the:
  5.  *
  6.  * Eclipse Public License version 1.0
  7.  * GNU General Public License version 2
  8.  * GNU Lesser General Public License version 2.1
  9.  */
  10. package org.jruby.truffle.nodes.core;

  11. import com.oracle.truffle.api.dsl.Specialization;
  12. import com.oracle.truffle.api.source.SourceSection;
  13. import org.jruby.truffle.runtime.RubyContext;
  14. import org.jruby.truffle.runtime.core.RubyArray;
  15. import org.jruby.truffle.runtime.core.RubyMatchData;
  16. import org.jruby.truffle.runtime.core.RubyString;

  17. @CoreClass(name = "MatchData")
  18. public abstract class MatchDataNodes {

  19.     @CoreMethod(names = "[]", required = 1, lowerFixnumParameters = 0)
  20.     public abstract static class GetIndexNode extends CoreMethodNode {

  21.         public GetIndexNode(RubyContext context, SourceSection sourceSection) {
  22.             super(context, sourceSection);
  23.         }

  24.         public GetIndexNode(GetIndexNode prev) {
  25.             super(prev);
  26.         }

  27.         @Specialization
  28.         public Object getIndex(RubyMatchData matchData, int index) {
  29.             notDesignedForCompilation();

  30.             if (index >= matchData.getValues().length) {
  31.                 return getContext().getCoreLibrary().getNilObject();
  32.             } else {
  33.                 return matchData.getValues()[index];
  34.             }
  35.         }

  36.     }

  37.     @CoreMethod(names = "pre_match")
  38.     public abstract static class PreMatchNode extends CoreMethodNode {

  39.         public PreMatchNode(RubyContext context, SourceSection sourceSection) {
  40.             super(context, sourceSection);
  41.         }

  42.         public PreMatchNode(PreMatchNode prev) {
  43.             super(prev);
  44.         }

  45.         @Specialization
  46.         public RubyString preMatch(RubyMatchData matchData) {
  47.             return matchData.getPre();
  48.         }

  49.     }

  50.     @CoreMethod(names = "post_match")
  51.     public abstract static class PostMatchNode extends CoreMethodNode {

  52.         public PostMatchNode(RubyContext context, SourceSection sourceSection) {
  53.             super(context, sourceSection);
  54.         }

  55.         public PostMatchNode(PostMatchNode prev) {
  56.             super(prev);
  57.         }

  58.         @Specialization
  59.         public RubyString postMatch(RubyMatchData matchData) {
  60.             return matchData.getPost();
  61.         }

  62.     }

  63.     @CoreMethod(names = {"to_a", "captures"})
  64.     public abstract static class ToANode extends CoreMethodNode {

  65.         public ToANode(RubyContext context, SourceSection sourceSection) {
  66.             super(context, sourceSection);
  67.         }

  68.         public ToANode(ToANode prev) {
  69.             super(prev);
  70.         }

  71.         @Specialization
  72.         public RubyArray toA(RubyMatchData matchData) {
  73.             notDesignedForCompilation();

  74.             return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), matchData.getValues());
  75.         }

  76.     }

  77.     @CoreMethod(names = "values_at", argumentsAsArray = true)
  78.     public abstract static class ValuesAtNode extends CoreMethodNode {

  79.         public ValuesAtNode(RubyContext context, SourceSection sourceSection) {
  80.             super(context, sourceSection);
  81.         }

  82.         public ValuesAtNode(ValuesAtNode prev) {
  83.             super(prev);
  84.         }

  85.         @Specialization
  86.         public RubyArray valuesAt(RubyMatchData matchData, Object[] args) {
  87.             notDesignedForCompilation();

  88.             final int[] indicies = new int[args.length];

  89.             for (int n = 0; n < args.length; n++) {
  90.                 indicies[n] = (int) args[n];
  91.             }

  92.             return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), matchData.valuesAt(indicies));
  93.         }

  94.     }

  95. }