|
54 | 54 | import org.truffleruby.core.binding.SetBindingFrameForEvalNode;
|
55 | 55 | import org.truffleruby.core.encoding.Encodings;
|
56 | 56 | 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; |
61 | 57 | import org.truffleruby.core.string.StringOperations;
|
62 | 58 | import org.truffleruby.language.DataNode;
|
63 | 59 | import org.truffleruby.language.EmitWarningsNode;
|
|
71 | 67 | import org.truffleruby.language.arguments.ReadPreArgumentNode;
|
72 | 68 | import org.truffleruby.language.arguments.RubyArguments;
|
73 | 69 | import org.truffleruby.language.control.RaiseException;
|
74 |
| -import org.truffleruby.language.control.WhileNode; |
75 |
| -import org.truffleruby.language.control.WhileNodeFactory; |
76 | 70 | import org.truffleruby.language.locals.FrameDescriptorNamesIterator;
|
77 | 71 | import org.truffleruby.language.locals.WriteLocalVariableNode;
|
78 | 72 | import org.truffleruby.language.methods.Arity;
|
79 | 73 | import org.truffleruby.language.methods.SharedMethodInfo;
|
| 74 | +import org.truffleruby.options.Options; |
80 | 75 | import org.truffleruby.shared.Metrics;
|
81 | 76 | import org.prism.Nodes;
|
82 | 77 | import org.prism.ParseResult;
|
@@ -160,14 +155,21 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
|
160 | 155 | // Parse to the YARP AST
|
161 | 156 | final RubyDeferredWarnings rubyWarnings = new RubyDeferredWarnings();
|
162 | 157 | 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 | + } |
163 | 165 |
|
164 | 166 | if (parseResult == null) {
|
165 | 167 | printParseTranslateExecuteMetric("before-parsing", context, source);
|
166 | 168 | parseResult = context.getMetricsProfiler().callWithMetrics(
|
167 | 169 | "parsing",
|
168 | 170 | source.getName(),
|
169 | 171 | () -> parseToYARPAST(rubySource, sourcePath, sourceBytes, localsInScopes,
|
170 |
| - language.options.FROZEN_STRING_LITERALS)); |
| 172 | + language.options.FROZEN_STRING_LITERALS, options)); |
171 | 173 | printParseTranslateExecuteMetric("after-parsing", context, source);
|
172 | 174 | }
|
173 | 175 |
|
@@ -257,21 +259,6 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
|
257 | 259 | truffleNode = YARPTranslator.sequence(YARPTranslator.initFlipFlopStates(environment), truffleNode);
|
258 | 260 | }
|
259 | 261 |
|
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 |
| - |
275 | 262 | RubyNode[] beginBlocks = translator.getBeginBlocks();
|
276 | 263 |
|
277 | 264 | // add BEGIN {} blocks at the very beginning of the program
|
@@ -354,14 +341,39 @@ private String getMethodName(ParserContext parserContext, MaterializedFrame pare
|
354 | 341 | }
|
355 | 342 |
|
356 | 343 | 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) { |
358 | 345 | TruffleSafepoint.poll(DummyNode.INSTANCE);
|
359 | 346 |
|
360 | 347 | final byte[] filepath = sourcePath.getBytes(Encodings.FILESYSTEM_CHARSET);
|
361 | 348 | int line = rubySource.getLineOffset() + 1;
|
362 | 349 | byte[] encoding = StringOperations.encodeAsciiBytes(rubySource.getEncoding().toString()); // encoding name is supposed to contain only ASCII characters
|
363 | 350 | var version = ParsingOptions.SyntaxVersion.V3_3_0;
|
364 | 351 |
|
| 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 | + |
365 | 377 | byte[][][] scopes;
|
366 | 378 |
|
367 | 379 | if (!localsInScopes.isEmpty()) {
|
@@ -389,7 +401,8 @@ public static ParseResult parseToYARPAST(RubySource rubySource, String sourcePat
|
389 | 401 | scopes = new byte[0][][];
|
390 | 402 | }
|
391 | 403 |
|
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, |
393 | 406 | scopes);
|
394 | 407 | byte[] serializedBytes = Parser.parseAndSerialize(sourceBytes, parsingOptions);
|
395 | 408 |
|
|
0 commit comments