13514
|
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.nodes.core;
|
|
11
|
|
12 import java.util.regex.*;
|
|
13
|
|
14 import com.oracle.truffle.api.*;
|
|
15 import com.oracle.truffle.api.dsl.*;
|
|
16 import com.oracle.truffle.api.frame.*;
|
|
17 import com.oracle.truffle.ruby.runtime.*;
|
|
18 import com.oracle.truffle.ruby.runtime.core.*;
|
|
19
|
|
20 @CoreClass(name = "Regexp")
|
|
21 public abstract class RegexpNodes {
|
|
22
|
|
23 @CoreMethod(names = {"=~", "==="}, minArgs = 1, maxArgs = 1)
|
|
24 public abstract static class MatchOperatorNode extends CoreMethodNode {
|
|
25
|
|
26 public MatchOperatorNode(RubyContext context, SourceSection sourceSection) {
|
|
27 super(context, sourceSection);
|
|
28 }
|
|
29
|
|
30 public MatchOperatorNode(MatchOperatorNode prev) {
|
|
31 super(prev);
|
|
32 }
|
|
33
|
|
34 @Specialization
|
|
35 public Object match(VirtualFrame frame, RubyRegexp regexp, RubyString string) {
|
|
36 return regexp.matchOperator(frame.getCaller().unpack(), string.toString());
|
|
37 }
|
|
38
|
|
39 }
|
|
40
|
|
41 @CoreMethod(names = "!~", minArgs = 1, maxArgs = 1)
|
|
42 public abstract static class NotMatchOperatorNode extends CoreMethodNode {
|
|
43
|
|
44 public NotMatchOperatorNode(RubyContext context, SourceSection sourceSection) {
|
|
45 super(context, sourceSection);
|
|
46 }
|
|
47
|
|
48 public NotMatchOperatorNode(NotMatchOperatorNode prev) {
|
|
49 super(prev);
|
|
50 }
|
|
51
|
|
52 @Specialization
|
|
53 public Object match(VirtualFrame frame, RubyRegexp regexp, RubyString string) {
|
|
54 return regexp.matchOperator(frame.getCaller().unpack(), string.toString()) == NilPlaceholder.INSTANCE;
|
|
55 }
|
|
56
|
|
57 }
|
|
58
|
|
59 @CoreMethod(names = "escape", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
|
|
60 public abstract static class EscapeNode extends CoreMethodNode {
|
|
61
|
|
62 public EscapeNode(RubyContext context, SourceSection sourceSection) {
|
|
63 super(context, sourceSection);
|
|
64 }
|
|
65
|
|
66 public EscapeNode(EscapeNode prev) {
|
|
67 super(prev);
|
|
68 }
|
|
69
|
|
70 @Specialization
|
|
71 public RubyString sqrt(RubyString pattern) {
|
|
72 return getContext().makeString(Pattern.quote(pattern.toString()));
|
|
73 }
|
|
74
|
|
75 }
|
|
76
|
|
77 @CoreMethod(names = "initialize", minArgs = 1, maxArgs = 1)
|
|
78 public abstract static class InitializeNode extends CoreMethodNode {
|
|
79
|
|
80 public InitializeNode(RubyContext context, SourceSection sourceSection) {
|
|
81 super(context, sourceSection);
|
|
82 }
|
|
83
|
|
84 public InitializeNode(InitializeNode prev) {
|
|
85 super(prev);
|
|
86 }
|
|
87
|
|
88 @Specialization
|
|
89 public NilPlaceholder initialize(RubyRegexp regexp, RubyString string) {
|
|
90 regexp.initialize(string.toString());
|
|
91 return NilPlaceholder.INSTANCE;
|
|
92 }
|
|
93
|
|
94 }
|
|
95
|
|
96 @CoreMethod(names = "match", minArgs = 1, maxArgs = 1)
|
|
97 public abstract static class MatchNode extends CoreMethodNode {
|
|
98
|
|
99 public MatchNode(RubyContext context, SourceSection sourceSection) {
|
|
100 super(context, sourceSection);
|
|
101 }
|
|
102
|
|
103 public MatchNode(MatchNode prev) {
|
|
104 super(prev);
|
|
105 }
|
|
106
|
|
107 @Specialization
|
|
108 public Object match(RubyRegexp regexp, RubyString string) {
|
|
109 return regexp.match(string.toString());
|
|
110 }
|
|
111
|
|
112 }
|
|
113
|
|
114 }
|