-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathRegExpPatternValue.java
More file actions
188 lines (153 loc) · 5.47 KB
/
RegExpPatternValue.java
File metadata and controls
188 lines (153 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
* Copyright (c) 2009-2013 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* * Emilie Balland - (CWI)
* * Paul Klint - Paul.Klint@cwi.nl - CWI
* * Arnold Lankamp - Arnold.Lankamp@cwi.nl
*******************************************************************************/
package org.rascalmpl.interpreter.matching;
import static org.rascalmpl.interpreter.result.ResultFactory.makeResult;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.rascalmpl.ast.AbstractAST;
import org.rascalmpl.exceptions.ImplementationError;
import org.rascalmpl.interpreter.IEvaluatorContext;
import org.rascalmpl.interpreter.env.Environment;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.result.ResultFactory;
import org.rascalmpl.interpreter.staticErrors.RedeclaredVariable;
import org.rascalmpl.interpreter.staticErrors.SyntaxError;
import org.rascalmpl.semantics.dynamic.RegExpLiteral;
import org.rascalmpl.semantics.dynamic.RegExpLiteral.InterpolationElement;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.type.Type;
public class RegExpPatternValue extends AbstractMatchingResult {
private final List<InterpolationElement> regexp;
private Pattern pat; // The Pattern resulting from compiling the regexp
private List<String> patternVars; // The variables occurring in the regexp
private Matcher matcher; // The actual regexp matcher
String subject; // Subject string to be matched
private boolean initialized = false; // Has matcher been initialized?
private boolean firstMatch; // Is this the first match?
private boolean hasNext; // Are there more matches?
private int start; // start of last match in current subject
private int end; // end of last match in current subject
private boolean iWroteItMySelf;
// private static HashMap<String,Matcher> matcherCache =
// new HashMap<String,Matcher>();
public RegExpPatternValue(IEvaluatorContext ctx, AbstractAST x, java.util.List<RegExpLiteral.InterpolationElement> regexp, List<String> patternVars) {
super(ctx, x);
this.regexp = regexp;
this.patternVars = patternVars;
initialized = false;
this.iWroteItMySelf = false;
}
private String interpolate(IEvaluatorContext env) {
StringBuilder b = new StringBuilder();
for (RegExpLiteral.InterpolationElement elem : regexp) {
b.append(elem.getString(env));
}
return b.toString();
}
@Override
public Type getType(Environment ev, HashMap<String,IVarPattern> patternVars) {
return tf.stringType();
}
@Override
public void initMatch(Result<IValue> subject) {
super.initMatch(subject);
Type runType = subject.getValue().getType();
if (runType.isSubtypeOf(tf.stringType())) {
this.subject = ((IString) subject.getValue()).getValue();
}
else {
hasNext = false;
return;
}
initialized = firstMatch = hasNext = true;
try {
String RegExpAsString = interpolate(ctx);
this.pat = Pattern.compile(RegExpAsString, Pattern.UNICODE_CHARACTER_CLASS);
matcher = this.pat.matcher(((IString) subject.getValue()).getValue());
IString empty = ctx.getValueFactory().string("");
// Initialize all pattern variables to ""
for (String name : patternVars) {
ctx.getCurrentEnvt().declareAndStoreInferredInnerScopeVariable(name, ResultFactory.makeResult(tf.stringType(), empty, ctx));
}
} catch (PatternSyntaxException e) {
throw new SyntaxError(e.getMessage(), ctx.getCurrentAST().getLocation());
}
}
@Override
public boolean hasNext() {
return initialized && (firstMatch || hasNext);
}
@Override
public boolean mayMatch(Type subjectType, Environment env) {
return subjectType.equivalent(tf.stringType());
}
public int getStart(){
return start;
}
public int getEnd(){
return end;
}
private boolean findMatch(){
while(matcher.find()){
for (int nVar = 0; nVar < patternVars.size(); nVar++){
java.lang.String binding = matcher.group(1+nVar);
if(binding != null){
java.lang.String name = patternVars.get(nVar);
ctx.getCurrentEnvt().storeVariable(name, makeResult(tf.stringType(), ctx.getValueFactory().string(binding), ctx));
}
}
start = matcher.start(0);
end = matcher.end(0);
return true;
}
hasNext = false;
return false;
}
@Override
public boolean next(){
if (firstMatch){
firstMatch = false;
}
try {
return findMatch();
}
catch (IndexOutOfBoundsException e) {
throw new ImplementationError("Unexpected error in mapping to Java regex:" + interpolate(ctx), ctx.getCurrentAST().getLocation());
}
}
@Override
public List<IVarPattern> getVariables(){
List<IVarPattern> res = new LinkedList<>();
for(String name : patternVars){
res.add(new RegExpVar(name));
}
return res;
}
@Override
public String toString(){
return "RegExpPatternValue(" + regexp + ", " + patternVars + ")";
}
@Override
public AbstractAST getAST() {
return ctx.getCurrentAST();
}
public boolean bindingInstance() {
return this.iWroteItMySelf;
}
}