Skip to content

Commit 0a024ad

Browse files
committed
Rely on Prism in handling command line options (-n, -l, -a, and -p)
1 parent 0a81ab5 commit 0a024ad

File tree

6 files changed

+38
-186
lines changed

6 files changed

+38
-186
lines changed

src/main/java/org/truffleruby/core/kernel/AutoSplitNode.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/java/org/truffleruby/core/kernel/ChompLoopNode.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/org/truffleruby/core/kernel/KernelGetsNode.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/org/truffleruby/core/kernel/KernelPrintLastLineNode.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private static ParseResult getParseResult(RubyLanguage language, RubySource ruby
275275
String sourcePath = rubySource.getSourcePath(language).intern();
276276

277277
return YARPTranslatorDriver.parseToYARPAST(rubySource, sourcePath, rubySource.getBytes(),
278-
Collections.emptyList(), language.options.FROZEN_STRING_LITERALS);
278+
Collections.emptyList(), language.options.FROZEN_STRING_LITERALS, null);
279279
}
280280
}
281281

src/main/java/org/truffleruby/parser/YARPTranslatorDriver.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@
5454
import org.truffleruby.core.binding.SetBindingFrameForEvalNode;
5555
import org.truffleruby.core.encoding.Encodings;
5656
import org.truffleruby.core.encoding.TStringUtils;
57-
import org.truffleruby.core.kernel.AutoSplitNode;
58-
import org.truffleruby.core.kernel.ChompLoopNode;
59-
import org.truffleruby.core.kernel.KernelGetsNode;
60-
import org.truffleruby.core.kernel.KernelPrintLastLineNode;
6157
import org.truffleruby.core.string.StringOperations;
6258
import org.truffleruby.language.DataNode;
6359
import org.truffleruby.language.EmitWarningsNode;
@@ -71,12 +67,11 @@
7167
import org.truffleruby.language.arguments.ReadPreArgumentNode;
7268
import org.truffleruby.language.arguments.RubyArguments;
7369
import org.truffleruby.language.control.RaiseException;
74-
import org.truffleruby.language.control.WhileNode;
75-
import org.truffleruby.language.control.WhileNodeFactory;
7670
import org.truffleruby.language.locals.FrameDescriptorNamesIterator;
7771
import org.truffleruby.language.locals.WriteLocalVariableNode;
7872
import org.truffleruby.language.methods.Arity;
7973
import org.truffleruby.language.methods.SharedMethodInfo;
74+
import org.truffleruby.options.Options;
8075
import org.truffleruby.shared.Metrics;
8176
import org.prism.Nodes;
8277
import org.prism.ParseResult;
@@ -160,14 +155,21 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
160155
// Parse to the YARP AST
161156
final RubyDeferredWarnings rubyWarnings = new RubyDeferredWarnings();
162157
final String sourcePath = rubySource.getSourcePath(language).intern();
158+
final Options options;
159+
160+
if (parserContext == ParserContext.TOP_LEVEL_FIRST) {
161+
options = context.getOptions();
162+
} else {
163+
options = null;
164+
}
163165

164166
if (parseResult == null) {
165167
printParseTranslateExecuteMetric("before-parsing", context, source);
166168
parseResult = context.getMetricsProfiler().callWithMetrics(
167169
"parsing",
168170
source.getName(),
169171
() -> parseToYARPAST(rubySource, sourcePath, sourceBytes, localsInScopes,
170-
language.options.FROZEN_STRING_LITERALS));
172+
language.options.FROZEN_STRING_LITERALS, options));
171173
printParseTranslateExecuteMetric("after-parsing", context, source);
172174
}
173175

@@ -257,21 +259,6 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
257259
truffleNode = YARPTranslator.sequence(YARPTranslator.initFlipFlopStates(environment), truffleNode);
258260
}
259261

260-
if (parserContext == ParserContext.TOP_LEVEL_FIRST && context.getOptions().GETS_LOOP) {
261-
if (context.getOptions().PRINT_LOOP) {
262-
truffleNode = YARPTranslator.sequence(truffleNode, new KernelPrintLastLineNode());
263-
}
264-
if (context.getOptions().SPLIT_LOOP) {
265-
truffleNode = YARPTranslator.sequence(new AutoSplitNode(), truffleNode);
266-
}
267-
268-
if (context.getOptions().CHOMP_LOOP) {
269-
truffleNode = YARPTranslator.sequence(new ChompLoopNode(), truffleNode);
270-
}
271-
truffleNode = new WhileNode(
272-
WhileNodeFactory.WhileRepeatingNodeGen.create(new KernelGetsNode(), truffleNode));
273-
}
274-
275262
RubyNode[] beginBlocks = translator.getBeginBlocks();
276263

277264
// add BEGIN {} blocks at the very beginning of the program
@@ -354,14 +341,39 @@ private String getMethodName(ParserContext parserContext, MaterializedFrame pare
354341
}
355342

356343
public static ParseResult parseToYARPAST(RubySource rubySource, String sourcePath, byte[] sourceBytes,
357-
List<List<String>> localsInScopes, boolean frozenStringLiteral) {
344+
List<List<String>> localsInScopes, boolean frozenStringLiteral, Options cliOptions) {
358345
TruffleSafepoint.poll(DummyNode.INSTANCE);
359346

360347
final byte[] filepath = sourcePath.getBytes(Encodings.FILESYSTEM_CHARSET);
361348
int line = rubySource.getLineOffset() + 1;
362349
byte[] encoding = StringOperations.encodeAsciiBytes(rubySource.getEncoding().toString()); // encoding name is supposed to contain only ASCII characters
363350
var version = ParsingOptions.SyntaxVersion.V3_3_0;
364351

352+
// Prism handles command line options (-n, -l, -a, -p) on its own
353+
final EnumSet<ParsingOptions.CommandLine> commandline;
354+
355+
if (cliOptions != null && cliOptions.GETS_LOOP) {
356+
List<ParsingOptions.CommandLine> yarpCliOptions = new ArrayList<>();
357+
yarpCliOptions.add(ParsingOptions.CommandLine.N);
358+
359+
if (cliOptions.PRINT_LOOP) {
360+
yarpCliOptions.add(ParsingOptions.CommandLine.P);
361+
}
362+
363+
if (cliOptions.SPLIT_LOOP) {
364+
yarpCliOptions.add(ParsingOptions.CommandLine.A);
365+
}
366+
367+
if (cliOptions.CHOMP_LOOP) {
368+
yarpCliOptions.add(ParsingOptions.CommandLine.L);
369+
}
370+
371+
commandline = EnumSet.copyOf(yarpCliOptions);
372+
} else {
373+
// EnumSet.copyOf method requires a collection to be non-empty
374+
commandline = EnumSet.noneOf(ParsingOptions.CommandLine.class);
375+
}
376+
365377
byte[][][] scopes;
366378

367379
if (!localsInScopes.isEmpty()) {
@@ -389,7 +401,8 @@ public static ParseResult parseToYARPAST(RubySource rubySource, String sourcePat
389401
scopes = new byte[0][][];
390402
}
391403

392-
byte[] parsingOptions = ParsingOptions.serialize(filepath, line, encoding, frozenStringLiteral, EnumSet.noneOf(ParsingOptions.CommandLine.class), version,
404+
byte[] parsingOptions = ParsingOptions.serialize(filepath, line, encoding, frozenStringLiteral, commandline,
405+
version,
393406
scopes);
394407
byte[] serializedBytes = Parser.parseAndSerialize(sourceBytes, parsingOptions);
395408

0 commit comments

Comments
 (0)