Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.ruby.runtime/src/com/oracle/truffle/ruby/runtime/core/RubyRegexp.java @ 13514:0fbee3eb71f0
Ruby: import project.
author | Chris Seaton <chris.seaton@oracle.com> |
---|---|
date | Mon, 06 Jan 2014 17:12:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13513:64a23ce736a0 | 13514:0fbee3eb71f0 |
---|---|
1 /* | |
2 * Copyright (c) 2013 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 com.oracle.truffle.ruby.runtime.core; | |
11 | |
12 import java.util.regex.*; | |
13 | |
14 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; | |
15 import com.oracle.truffle.api.frame.*; | |
16 import com.oracle.truffle.ruby.runtime.*; | |
17 import com.oracle.truffle.ruby.runtime.objects.*; | |
18 | |
19 /** | |
20 * Represents the Ruby {@code Regexp} class. | |
21 */ | |
22 public class RubyRegexp extends RubyObject { | |
23 | |
24 /** | |
25 * The class from which we create the object that is {@code Regexp}. A subclass of | |
26 * {@link RubyClass} so that we can override {@link #newInstance} and allocate a | |
27 * {@link RubyRegexp} rather than a normal {@link RubyBasicObject}. | |
28 */ | |
29 public static class RubyRegexpClass extends RubyClass { | |
30 | |
31 public RubyRegexpClass(RubyClass objectClass) { | |
32 super(null, objectClass, "Regexp"); | |
33 } | |
34 | |
35 @Override | |
36 public RubyBasicObject newInstance() { | |
37 return new RubyRegexp(getContext().getCoreLibrary().getRegexpClass()); | |
38 } | |
39 | |
40 } | |
41 | |
42 @CompilationFinal private Pattern pattern; | |
43 | |
44 public RubyRegexp(RubyClass regexpClass) { | |
45 super(regexpClass); | |
46 } | |
47 | |
48 public RubyRegexp(RubyClass regexpClass, String pattern) { | |
49 this(regexpClass); | |
50 initialize(compile(pattern)); | |
51 } | |
52 | |
53 public RubyRegexp(RubyClass regexpClass, Pattern pattern) { | |
54 this(regexpClass); | |
55 initialize(pattern); | |
56 } | |
57 | |
58 public void initialize(String setPattern) { | |
59 pattern = compile(setPattern); | |
60 } | |
61 | |
62 public void initialize(Pattern setPattern) { | |
63 pattern = setPattern; | |
64 } | |
65 | |
66 public Object matchOperator(Frame frame, String string) { | |
67 final RubyContext context = getRubyClass().getContext(); | |
68 | |
69 final Matcher matcher = pattern.matcher(string); | |
70 | |
71 if (matcher.find()) { | |
72 for (int n = 1; n < matcher.groupCount() + 1; n++) { | |
73 final FrameSlot slot = frame.getFrameDescriptor().findFrameSlot("$" + n); | |
74 | |
75 if (slot != null) { | |
76 frame.setObject(slot, context.makeString(matcher.group(n))); | |
77 } | |
78 } | |
79 | |
80 return matcher.start(); | |
81 } else { | |
82 return NilPlaceholder.INSTANCE; | |
83 } | |
84 } | |
85 | |
86 public Pattern getPattern() { | |
87 return pattern; | |
88 } | |
89 | |
90 public Object match(String string) { | |
91 final RubyContext context = getRubyClass().getContext(); | |
92 | |
93 final Matcher matcher = pattern.matcher(string); | |
94 | |
95 if (!matcher.find()) { | |
96 return NilPlaceholder.INSTANCE; | |
97 } | |
98 | |
99 final Object[] values = new Object[matcher.groupCount() + 1]; | |
100 | |
101 for (int n = 0; n < matcher.groupCount() + 1; n++) { | |
102 final String group = matcher.group(n); | |
103 | |
104 if (group == null) { | |
105 values[n] = NilPlaceholder.INSTANCE; | |
106 } else { | |
107 values[n] = context.makeString(group); | |
108 } | |
109 } | |
110 | |
111 return new RubyMatchData(context.getCoreLibrary().getMatchDataClass(), values); | |
112 } | |
113 | |
114 @Override | |
115 public int hashCode() { | |
116 return pattern.pattern().hashCode(); | |
117 } | |
118 | |
119 @Override | |
120 public boolean equals(Object obj) { | |
121 if (this == obj) { | |
122 return true; | |
123 } | |
124 if (obj == null) { | |
125 return false; | |
126 } | |
127 if (!(obj instanceof RubyRegexp)) { | |
128 return false; | |
129 } | |
130 RubyRegexp other = (RubyRegexp) obj; | |
131 if (pattern == null) { | |
132 if (other.pattern != null) { | |
133 return false; | |
134 } | |
135 } else if (!pattern.pattern().equals(other.pattern.pattern())) { | |
136 return false; | |
137 } | |
138 return true; | |
139 } | |
140 | |
141 public static Pattern compile(String pattern) { | |
142 return Pattern.compile(pattern, Pattern.MULTILINE | Pattern.UNIX_LINES); | |
143 } | |
144 } |