Repository navigation
Expand file tree
/
Copy pathFunctionParametersKeywordPlugin.java
More file actions
330 lines (319 loc) · 18 KB
/
Copy pathFunctionParametersKeywordPlugin.java
File metadata and controls
330 lines (319 loc) · 18 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package org.perlonjava.frontend.parser;
import org.perlonjava.frontend.astnode.Node;
import org.perlonjava.frontend.lexer.LexerToken;
import org.perlonjava.frontend.lexer.LexerTokenType;
import org.perlonjava.frontend.parser.SubroutineParser;
import org.perlonjava.runtime.runtimetypes.RuntimeScalar;
import org.perlonjava.runtime.runtimetypes.MortalList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** Parses Function::Parameters declarations using core signature lowering. */
final class FunctionParametersKeywordPlugin implements ParserKeywordPlugin {
static final FunctionParametersKeywordPlugin FUNCTION =
new FunctionParametersKeywordPlugin(false, 0, true, true, false, null, false,
List.of(), "auto", null, true);
static final FunctionParametersKeywordPlugin METHOD =
new FunctionParametersKeywordPlugin(true, 0, true, true, false, null, false,
List.of("$self"), "auto", null, true);
private final boolean method;
private final int optionalInvocantCount;
private final boolean strict;
private final boolean checkArgumentTypes;
private final boolean runtime;
private final String installSub;
private final RuntimeScalar installSubCallback;
private final boolean requiresName;
private final boolean prohibitsName;
private final boolean defaultArguments;
private final boolean types;
private final String typeReifier;
private final RuntimeScalar typeReifierCallback;
private final List<String> invocants;
private final Map<String, RuntimeScalar> invocantTypes;
private final List<String> defaultAttributes;
private FunctionParametersKeywordPlugin(
boolean method, int optionalInvocantCount, boolean strict,
boolean checkArgumentTypes, boolean runtime, String installSub,
boolean requiresName, List<String> invocants, String typeReifier,
RuntimeScalar typeReifierCallback, boolean types) {
this(method, optionalInvocantCount, strict, checkArgumentTypes, runtime,
installSub, requiresName, false, true, invocants, List.of(), typeReifier,
typeReifierCallback, types, Map.of(), null);
}
private FunctionParametersKeywordPlugin(
boolean method, int optionalInvocantCount, boolean strict,
boolean checkArgumentTypes, boolean runtime, String installSub, boolean requiresName,
boolean prohibitsName, boolean defaultArguments,
List<String> invocants,
List<String> defaultAttributes,
String typeReifier,
RuntimeScalar typeReifierCallback,
boolean types,
Map<String, RuntimeScalar> invocantTypes,
RuntimeScalar installSubCallback) {
this.method = method;
this.optionalInvocantCount = optionalInvocantCount;
this.strict = strict;
this.checkArgumentTypes = checkArgumentTypes;
this.runtime = runtime;
this.installSub = installSub;
this.installSubCallback = installSubCallback;
this.requiresName = requiresName;
this.prohibitsName = prohibitsName;
this.defaultArguments = defaultArguments;
this.types = types;
this.typeReifier = typeReifier;
this.typeReifierCallback = typeReifierCallback;
this.invocants = List.copyOf(invocants);
this.invocantTypes = Map.copyOf(invocantTypes);
this.defaultAttributes = List.copyOf(defaultAttributes);
}
static FunctionParametersKeywordPlugin configured(
String defaults, List<String> shift, int optionalInvocantCount,
boolean strict, boolean checkArgumentTypes, boolean runtime, String installSub,
List<String> defaultAttributes, String namePolicy, boolean defaultArguments,
String typeReifier, RuntimeScalar typeReifierCallback, boolean types,
Map<String, RuntimeScalar> invocantTypes, RuntimeScalar installSubCallback) {
boolean method = Set.of("method", "classmethod", "before", "after", "around",
"augment", "override").contains(defaults);
boolean requiresName = Set.of("before", "after", "around", "augment", "override")
.contains(defaults);
if (method) {
List<String> invocants = shift;
if (invocants == null || invocants.isEmpty()) {
invocants = "around".equals(defaults)
? List.of("$orig", "$self")
: List.of("classmethod".equals(defaults) ? "$class" : "$self");
}
return new FunctionParametersKeywordPlugin(
true, 0, strict, checkArgumentTypes, runtime, installSub,
"required".equals(namePolicy) || requiresName,
"prohibited".equals(namePolicy), defaultArguments, invocants, defaultAttributes,
typeReifier, typeReifierCallback, types, invocantTypes, installSubCallback);
}
if (optionalInvocantCount == 0 && strict && checkArgumentTypes && !runtime && installSub == null
&& defaultAttributes.isEmpty() && "optional".equals(namePolicy) && defaultArguments
&& "auto".equals(typeReifier) && typeReifierCallback == null && types) {
return FUNCTION;
}
return new FunctionParametersKeywordPlugin(
false, optionalInvocantCount, strict, checkArgumentTypes, runtime, installSub,
"required".equals(namePolicy) || requiresName,
"prohibited".equals(namePolicy), defaultArguments,
shift == null ? List.of() : shift, defaultAttributes,
typeReifier, typeReifierCallback, types, invocantTypes, installSubCallback);
}
@Override
public Node parse(Parser parser, String keyword) {
int declarationIndex = parser.tokenIndex;
LexerToken keywordToken = parser.tokens.get(declarationIndex);
String originalText = keywordToken.text;
int implicitInvocantInsertIndex = -1;
int implicitMethodSignatureInsertIndex = -1;
boolean previousFunctionParametersDeclaration = parser.parsingFunctionParametersDeclaration;
boolean previousFunctionParametersMethod = parser.parsingFunctionParametersMethod;
boolean previousOptionalInvocant = parser.parsingFunctionParametersOptionalInvocant;
int previousInvocantCount = parser.functionParametersInvocantCount;
Map<String, RuntimeScalar> previousInvocantTypes = parser.functionParametersInvocantTypeObjects;
boolean previousStrict = parser.functionParametersStrict;
boolean previousCheckArgumentTypes = parser.functionParametersCheckArgumentTypes;
boolean previousTypes = parser.functionParametersTypes;
String previousKeyword = parser.functionParametersKeyword;
String previousInstallSub = parser.functionParametersInstallSub;
RuntimeScalar previousInstallSubCallback = parser.functionParametersInstallSubCallback;
String previousTypeReifier = parser.functionParametersTypeReifier;
RuntimeScalar previousTypeReifierCallback = parser.functionParametersTypeReifierCallback;
List<String> previousAttributes = parser.functionParametersAttributes;
boolean previousRuntime = parser.functionParametersRuntime;
boolean previousDefaultArguments = parser.functionParametersDefaultArguments;
parser.tokenIndex++;
boolean startsWithApostrophe = "'".equals(TokenUtils.peek(parser).text);
if ("{".equals(TokenUtils.peek(parser).text) && !method) {
parser.throwError("In " + keyword
+ " (anon): I was expecting a parameter list, not \"{\"");
}
int declarationNameIndex = parser.tokenIndex;
String candidateName = null;
if (!"(".equals(TokenUtils.peek(parser).text) && !startsWithApostrophe) {
candidateName = IdentifierParser.parseSubroutineIdentifier(parser);
}
boolean validCandidateName = isValidCandidateName(candidateName);
parser.tokenIndex = declarationIndex;
String afterName = parser.tokens.get(
Whitespace.skipWhitespace(parser, declarationNameIndex, parser.tokens)).text;
boolean anonymousMethodBlock = method && "{".equals(afterName);
if (startsWithApostrophe || (!"(".equals(afterName)
&& !validCandidateName && !anonymousMethodBlock)) {
parser.throwError(originalText + " expects a parameter list");
}
parser.tokens.set(declarationIndex, LexerToken.of(keywordToken.type, "sub"));
parser.tokenIndex++;
parser.parsingFunctionParametersDeclaration = true;
parser.parsingFunctionParametersMethod = method;
parser.parsingFunctionParametersOptionalInvocant = optionalInvocantCount > 0;
parser.functionParametersInvocantCount = method ? invocants.size() : optionalInvocantCount;
parser.functionParametersInvocantTypeObjects = invocantTypes;
parser.functionParametersStrict = strict;
parser.functionParametersCheckArgumentTypes = checkArgumentTypes;
parser.functionParametersTypes = types;
parser.functionParametersKeyword = keyword;
parser.functionParametersAttributes = defaultAttributes;
parser.functionParametersRuntime = runtime;
parser.functionParametersDefaultArguments = defaultArguments;
parser.functionParametersInstallSub = installSub;
parser.functionParametersInstallSubCallback = installSubCallback;
parser.functionParametersTypeReifier = typeReifier;
parser.functionParametersTypeReifierCallback = typeReifierCallback;
try {
LexerToken declarationStart = TokenUtils.peek(parser);
boolean named = declarationStart.type == LexerTokenType.IDENTIFIER
|| declarationStart.text.equals("::")
|| declarationStart.text.equals("'");
if (named && parser.expressionParseDepth > 0) {
parser.throwError("expecting a parameter list");
}
if (requiresName && !named) {
parser.throwError("expecting a function name");
}
if (prohibitsName && named) parser.throwError("expecting a parameter list");
if (method || !this.invocants.isEmpty()) {
int openParen = parser.tokenIndex;
while (openParen < parser.tokens.size()
&& !"(".equals(parser.tokens.get(openParen).text)
&& !"{".equals(parser.tokens.get(openParen).text)) {
openParen++;
}
if (openParen < parser.tokens.size()
&& "{".equals(parser.tokens.get(openParen).text)) {
// Syntax::Feature::Method permits both `method name {}`
// and anonymous `method {}` forms. Normalize them to the
// same explicit implicit-invocant signature used by the
// Function::Parameters method provider.
implicitMethodSignatureInsertIndex = openParen;
List<String> configuredInvocants = method
? invocants : this.invocants;
String[] signature = {"(", "$", configuredInvocants.getFirst().substring(1), ":", ")"};
for (int i = 0; i < signature.length; i++) {
LexerTokenType type = i == 2
? LexerTokenType.IDENTIFIER : LexerTokenType.OPERATOR;
parser.tokens.add(openParen + i,
new LexerToken(type, signature[i]));
}
} else if (openParen < parser.tokens.size()
&& "(".equals(parser.tokens.get(openParen).text)
&& !hasExplicitInvocant(parser, openParen)) {
implicitInvocantInsertIndex = openParen + 1;
int insertAt = implicitInvocantInsertIndex;
List<String> configuredInvocants = method
? invocants : this.invocants;
for (int i = 0; i < configuredInvocants.size(); i++) {
String invocant = configuredInvocants.get(i);
if (i > 0) {
parser.tokens.add(insertAt++,
new LexerToken(LexerTokenType.OPERATOR, ","));
}
parser.tokens.add(insertAt++,
new LexerToken(LexerTokenType.OPERATOR, "$"));
parser.tokens.add(insertAt++,
new LexerToken(LexerTokenType.IDENTIFIER,
invocant.startsWith("$") ? invocant.substring(1) : invocant));
}
parser.tokens.add(insertAt,
new LexerToken(LexerTokenType.OPERATOR, ":"));
}
}
Node declaration = SubroutineParser.parseSubroutineDefinition(parser, named, "our");
if (installSub != null && !runtime) {
SpecialBlockParser.runSpecialBlock(parser, "BEGIN", declaration);
// The generated CV is only a temporary argument to the
// compile-time installer. Complete its lifetime cleanup at
// the end of the synthetic BEGIN, before parsing the next
// source statement can observe stale weak references.
MortalList.flushAboveMark();
return new org.perlonjava.frontend.astnode.ListNode(parser.tokenIndex);
}
return declaration;
} finally {
if (implicitMethodSignatureInsertIndex >= 0) {
for (int i = 0; i < 5; i++) {
parser.tokens.remove(implicitMethodSignatureInsertIndex);
}
parser.tokenIndex -= 5;
}
if (implicitInvocantInsertIndex >= 0) {
List<String> configuredInvocants = method ? invocants : this.invocants;
int inserted = configuredInvocants.size() * 2 + (configuredInvocants.size() - 1) + 1;
for (int i = 0; i < inserted; i++) parser.tokens.remove(implicitInvocantInsertIndex);
parser.tokenIndex -= inserted;
}
parser.parsingFunctionParametersDeclaration = previousFunctionParametersDeclaration;
parser.parsingFunctionParametersMethod = previousFunctionParametersMethod;
parser.parsingFunctionParametersOptionalInvocant = previousOptionalInvocant;
parser.functionParametersInvocantCount = previousInvocantCount;
parser.functionParametersInvocantTypeObjects = previousInvocantTypes;
parser.functionParametersStrict = previousStrict;
parser.functionParametersCheckArgumentTypes = previousCheckArgumentTypes;
parser.functionParametersTypes = previousTypes;
parser.functionParametersKeyword = previousKeyword;
parser.functionParametersInstallSub = previousInstallSub;
parser.functionParametersInstallSubCallback = previousInstallSubCallback;
parser.functionParametersTypeReifier = previousTypeReifier;
parser.functionParametersTypeReifierCallback = previousTypeReifierCallback;
parser.functionParametersAttributes = previousAttributes;
parser.functionParametersRuntime = previousRuntime;
parser.functionParametersDefaultArguments = previousDefaultArguments;
parser.tokens.set(declarationIndex, LexerToken.of(parser.tokens.get(declarationIndex).type, originalText));
}
}
private static boolean hasExplicitInvocant(Parser parser, int openParen) {
int first = openParen + 1;
while (first < parser.tokens.size()
&& (parser.tokens.get(first).type == LexerTokenType.WHITESPACE
|| parser.tokens.get(first).type == LexerTokenType.NEWLINE)) {
first++;
}
if (first >= parser.tokens.size()) return false;
// An empty signature has no explicit invocant separator. Do not scan
// past its closing parenthesis into following attributes, where a
// colon could otherwise suppress insertion of the implicit invocant.
if (")".equals(parser.tokens.get(first).text)) return false;
for (int i = first + 1; i < parser.tokens.size(); i++) {
String text = parser.tokens.get(i).text;
if (":".equals(text)) return true;
if (")".equals(text) || "{".equals(text)) return false;
}
return false;
}
private static boolean isValidCandidateName(String name) {
if (name == null || name.isEmpty()) return false;
String[] components = name.split("::", -1);
int firstComponent = name.startsWith("::") ? 1 : 0;
for (int componentIndex = firstComponent;
componentIndex < components.length; componentIndex++) {
String component = components[componentIndex];
if (component.isEmpty()) return false;
int firstCodePoint = component.codePointAt(0);
if (!IdentifierParser.isPerlIdentifierStart(firstCodePoint)) return false;
for (int offset = Character.charCount(firstCodePoint); offset < component.length();) {
int codePoint = component.codePointAt(offset);
if (!Character.isUnicodeIdentifierPart(codePoint) && codePoint != '_') return false;
offset += Character.charCount(codePoint);
}
}
return true;
}
@Override
public Node parseExpression(Parser parser, String keyword) {
int keywordIndex = parser.tokenIndex;
parser.tokenIndex++;
LexerToken declarationStart = TokenUtils.peek(parser);
parser.tokenIndex = keywordIndex;
if (declarationStart.type == LexerTokenType.IDENTIFIER
|| Set.of("(", "::", "'").contains(declarationStart.text)
|| (method && "{".equals(declarationStart.text))) {
return parse(parser, keyword);
}
return null;
}
}