@@ -174,29 +174,25 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
174
174
}
175
175
176
176
// Parse to the YARP AST
177
-
178
- final org .prism .Nodes .Node node ;
179
-
180
177
final RubyDeferredWarnings rubyWarnings = new RubyDeferredWarnings ();
181
178
182
179
// Only use the cache while loading top-level core library files, as eval() later could use
183
180
// the same Source name but should not use the cache. For instance,
184
181
// TOPLEVEL_BINDING.eval("self") would use the cache which is wrong.
185
-
186
- // TODO: use cache
187
- // if (ParserCache.INSTANCE != null && parserContext == ParserContext.TOP_LEVEL &&
188
- // ParserCache.INSTANCE.containsKey(source.getName())) {
189
- // node = ParserCache.INSTANCE.get(source.getName());
190
- // } else {
191
- printParseTranslateExecuteMetric ("before-parsing" , context , source );
192
- ParseResult parseResult = context .getMetricsProfiler ().callWithMetrics (
193
- "parsing" ,
194
- source .getName (),
195
- () -> parseToYARPAST (context , language , rubySource , staticScope , localVariableNames ,
196
- parserConfiguration , rubyWarnings , parseEnvironment ));
197
- printParseTranslateExecuteMetric ("after-parsing" , context , source );
198
- // }
199
- node = parseResult .value ;
182
+ final ParseResult parseResult ;
183
+ if (ParserCache .INSTANCE != null && parserContext == ParserContext .TOP_LEVEL &&
184
+ ParserCache .INSTANCE .containsKey (source .getName ())) {
185
+ parseResult = ParserCache .INSTANCE .get (source .getName ()).getLeft ();
186
+ } else {
187
+ printParseTranslateExecuteMetric ("before-parsing" , context , source );
188
+ parseResult = context .getMetricsProfiler ().callWithMetrics (
189
+ "parsing" ,
190
+ source .getName (),
191
+ () -> parseToYARPAST (context , language , rubySource , localVariableNames ,
192
+ parserConfiguration , rubyWarnings , parseEnvironment ));
193
+ printParseTranslateExecuteMetric ("after-parsing" , context , source );
194
+ }
195
+ var node = parseResult .value ;
200
196
201
197
// Needs the magic comment to be parsed
202
198
parseEnvironment .allowTruffleRubyPrimitives = parserConfiguration .allowTruffleRubyPrimitives ;
@@ -397,7 +393,6 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
397
393
ParserConfiguration configuration , RubyDeferredWarnings rubyWarnings , ParseEnvironment parseEnvironment ) {
398
394
TruffleSafepoint .poll (DummyNode .INSTANCE );
399
395
400
- // YARP begin
401
396
byte [] sourceBytes = rubySource .getBytes ();
402
397
403
398
byte [] filepath ;
@@ -442,15 +437,20 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
442
437
byte [] serializedBytes = Parser .parseAndSerialize (sourceBytes , parsingOptions );
443
438
444
439
Nodes .Source yarpSource = parseEnvironment .yarpSource ;
445
- ParseResult parseResult = YARPLoader .load (serializedBytes , yarpSource , context .getEncodingManager (),
446
- rubySource );
440
+ ParseResult parseResult = YARPLoader .load (serializedBytes , yarpSource , rubySource );
447
441
448
442
final ParseResult .Error [] errors = parseResult .errors ;
449
443
450
444
// collect warnings generated by the parser
451
445
for (ParseResult .Warning warning : parseResult .warnings ) {
452
446
Nodes .Location location = warning .location ;
453
447
SourceSection section = rubySource .getSource ().createSection (location .startOffset , location .length );
448
+
449
+ if (context == null ) {
450
+ throw CompilerDirectives .shouldNotReachHere (
451
+ "Warning in " + RubyLanguage .filenameLine (section ) + ": " + warning .message );
452
+ }
453
+
454
454
int lineNumber = RubySource .getStartLineAdjusted (context , section );
455
455
456
456
switch (warning .level ) {
@@ -462,7 +462,7 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
462
462
if (errors .length != 0 ) {
463
463
// print warnings immediately
464
464
// if there is no syntax error they will be printed in runtime
465
- if (!rubyWarnings .warnings .isEmpty ()) {
465
+ if (!rubyWarnings .warnings .isEmpty () && context != null ) {
466
466
EmitWarningsNode .printWarnings (context , rubyWarnings );
467
467
}
468
468
@@ -473,8 +473,13 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
473
473
474
474
Nodes .Location location = error .location ;
475
475
SourceSection section = rubySource .getSource ().createSection (location .startOffset , location .length );
476
- String message = context .fileLine (section ) + ": " + error .message ;
477
476
477
+ if (context == null ) {
478
+ throw CompilerDirectives .shouldNotReachHere (
479
+ "Parse error in " + RubyLanguage .filenameLine (section ) + ": " + error .message );
480
+ }
481
+
482
+ String message = context .fileLine (section ) + ": " + error .message ;
478
483
throw new RaiseException (
479
484
context ,
480
485
context .getCoreExceptions ().syntaxErrorAlreadyWithFileLine (message , null , section ));
@@ -495,47 +500,6 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
495
500
}
496
501
497
502
return parseResult ;
498
- // YARP end
499
-
500
- // RubyParser parser = new RubyParser(lexerSource, rubyWarnings);
501
- // TruffleSafepoint.poll(DummyNode.INSTANCE); // RubyParser <clinit> takes a while
502
- // RubyParserResult result;
503
- // try {
504
- // result = parser.parse(configuration);
505
- // } catch (SyntaxException e) {
506
- // if (!rubyWarnings.warnings.isEmpty()) {
507
- // EmitWarningsNode.printWarnings(context, rubyWarnings);
508
- // }
509
- // switch (e.getPid()) {
510
- // case UNKNOWN_ENCODING:
511
- // case NOT_ASCII_COMPATIBLE:
512
- // if (context != null) {
513
- // throw new RaiseException(
514
- // context,
515
- // context.getCoreExceptions().argumentError(e.getMessage(), null));
516
- // } else {
517
- // throw e;
518
- // }
519
- // default:
520
- // StringBuilder buffer = new StringBuilder(100);
521
- // buffer.append(e.getFile()).append(':');
522
- // buffer.append(e.getLine() + rubySource.getLineOffset()).append(": ");
523
- // buffer.append(e.getMessage());
524
- //
525
- // if (context != null) {
526
- // throw new RaiseException(
527
- // context,
528
- // context.getCoreExceptions().syntaxErrorAlreadyWithFileLine(
529
- // buffer.toString(),
530
- // null,
531
- // rubySource.getSource().createSection(e.getLine())));
532
- // } else {
533
- // throw new UnsupportedOperationException(buffer.toString(), e);
534
- // }
535
- // }
536
- // }
537
-
538
- // return (RootParseNode) result.getAST();
539
503
}
540
504
541
505
public static RubySource createRubySource (Object code ) {
0 commit comments