@@ -188,10 +188,13 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
188
188
parseResult = context .getMetricsProfiler ().callWithMetrics (
189
189
"parsing" ,
190
190
source .getName (),
191
- () -> parseToYARPAST (context , language , rubySource , localVariableNames ,
192
- parserConfiguration , rubyWarnings , parseEnvironment ));
191
+ () -> parseToYARPAST (language , rubySource , localVariableNames ,
192
+ parseEnvironment ));
193
193
printParseTranslateExecuteMetric ("after-parsing" , context , source );
194
194
}
195
+
196
+ handleWarningsErrorsPrimitives (context , language , parseResult , rubySource , parserConfiguration , rubyWarnings );
197
+
195
198
var node = parseResult .value ;
196
199
197
200
// Needs the magic comment to be parsed
@@ -388,9 +391,8 @@ private String getMethodName(ParserContext parserContext, MaterializedFrame pare
388
391
}
389
392
}
390
393
391
- public static org .prism .ParseResult parseToYARPAST (RubyContext context , RubyLanguage language ,
392
- RubySource rubySource , List <List <String >> localVariableNames ,
393
- ParserConfiguration configuration , RubyDeferredWarnings rubyWarnings , ParseEnvironment parseEnvironment ) {
394
+ public static ParseResult parseToYARPAST (RubyLanguage language , RubySource rubySource ,
395
+ List <List <String >> localVariableNames , ParseEnvironment parseEnvironment ) {
394
396
TruffleSafepoint .poll (DummyNode .INSTANCE );
395
397
396
398
byte [] sourceBytes = rubySource .getBytes ();
@@ -437,7 +439,15 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
437
439
byte [] serializedBytes = Parser .parseAndSerialize (sourceBytes , parsingOptions );
438
440
439
441
Nodes .Source yarpSource = parseEnvironment .yarpSource ;
440
- ParseResult parseResult = YARPLoader .load (serializedBytes , yarpSource , rubySource );
442
+ return YARPLoader .load (serializedBytes , yarpSource , rubySource );
443
+ }
444
+
445
+ public static void handleWarningsErrorsPrimitives (RubyContext context , RubyLanguage language ,
446
+ ParseResult parseResult , RubySource rubySource , ParserConfiguration configuration ,
447
+ RubyDeferredWarnings rubyWarnings ) {
448
+
449
+ // intern() to improve footprint
450
+ String sourcePath = rubySource .getSourcePath (language ).intern ();
441
451
442
452
final ParseResult .Error [] errors = parseResult .errors ;
443
453
@@ -446,12 +456,7 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
446
456
Nodes .Location location = warning .location ;
447
457
SourceSection section = rubySource .getSource ().createSection (location .startOffset , location .length );
448
458
449
- if (context == null ) {
450
- throw CompilerDirectives .shouldNotReachHere (
451
- "Warning in " + RubyLanguage .filenameLine (section ) + ": " + warning .message );
452
- }
453
-
454
- int lineNumber = RubySource .getStartLineAdjusted (context , section );
459
+ int lineNumber = section .getStartLine () + rubySource .getLineOffset ();
455
460
456
461
switch (warning .level ) {
457
462
case WARNING_DEFAULT -> rubyWarnings .warn (sourcePath , lineNumber , warning .message );
@@ -462,7 +467,7 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
462
467
if (errors .length != 0 ) {
463
468
// print warnings immediately
464
469
// if there is no syntax error they will be printed in runtime
465
- if (!rubyWarnings .warnings .isEmpty () && context != null ) {
470
+ if (!rubyWarnings .warnings .isEmpty ()) {
466
471
EmitWarningsNode .printWarnings (context , rubyWarnings );
467
472
}
468
473
@@ -474,11 +479,6 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
474
479
Nodes .Location location = error .location ;
475
480
SourceSection section = rubySource .getSource ().createSection (location .startOffset , location .length );
476
481
477
- if (context == null ) {
478
- throw CompilerDirectives .shouldNotReachHere (
479
- "Parse error in " + RubyLanguage .filenameLine (section ) + ": " + error .message );
480
- }
481
-
482
482
String message = context .fileLine (section ) + ": " + error .message ;
483
483
throw new RaiseException (
484
484
context ,
@@ -498,8 +498,25 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
498
498
configuration .allowTruffleRubyPrimitives = value .equalsIgnoreCase ("true" );
499
499
}
500
500
}
501
+ }
502
+
503
+ public static void handleWarningsErrorsNoContext (RubyLanguage language , ParseResult parseResult ,
504
+ RubySource rubySource , Nodes .Source yarpSource ) {
505
+ if (parseResult .errors .length > 0 ) {
506
+ var error = parseResult .errors [0 ];
507
+ throw CompilerDirectives .shouldNotReachHere ("Parse error in " +
508
+ fileLineYARPSource (error .location , language , rubySource , yarpSource ) + ": " + error .message );
509
+ }
510
+
511
+ for (var warning : parseResult .warnings ) {
512
+ throw CompilerDirectives .shouldNotReachHere ("Warning in " +
513
+ fileLineYARPSource (warning .location , language , rubySource , yarpSource ) + ": " + warning .message );
514
+ }
515
+ }
501
516
502
- return parseResult ;
517
+ private static String fileLineYARPSource (Nodes .Location location , RubyLanguage language , RubySource rubySource ,
518
+ Nodes .Source yarpSource ) {
519
+ return rubySource .getSourcePath (language ) + ":" + yarpSource .line (location .startOffset );
503
520
}
504
521
505
522
public static RubySource createRubySource (Object code ) {
0 commit comments