Skip to content

Commit fa59235

Browse files
committed
Refactor so there is no need to pass any context to parseToYARPAST()
1 parent 69d56d7 commit fa59235

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

src/main/java/org/truffleruby/aot/ParserCache.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import org.truffleruby.language.loader.ResourceLoader;
2424
import org.truffleruby.parser.ParseEnvironment;
2525
import org.truffleruby.parser.ParserContext;
26-
import org.truffleruby.parser.RubyDeferredWarnings;
2726
import org.truffleruby.parser.RubySource;
2827
import org.truffleruby.parser.YARPTranslatorDriver;
29-
import org.truffleruby.parser.parser.ParserConfiguration;
3028
import org.truffleruby.shared.options.OptionsCatalog;
3129

3230
import com.oracle.truffle.api.TruffleOptions;
@@ -65,14 +63,13 @@ private static RubySource loadSource(String feature) {
6563

6664
private static Pair<ParseResult, Source> parse(RubySource source) {
6765
var language = RubyLanguage.getCurrentLanguage();
68-
var parserConfiguration = new ParserConfiguration(null, false, true, false);
69-
var rubyWarnings = new RubyDeferredWarnings();
70-
var parseEnvironment = new ParseEnvironment(language, source,
71-
YARPTranslatorDriver.createYARPSource(source.getBytes()), ParserContext.TOP_LEVEL, null);
66+
var yarpSource = YARPTranslatorDriver.createYARPSource(source.getBytes());
67+
var parseEnvironment = new ParseEnvironment(language, source, yarpSource, ParserContext.TOP_LEVEL, null);
7268

73-
var parseResult = YARPTranslatorDriver.parseToYARPAST(null, language, source, Collections.emptyList(),
74-
parserConfiguration,
75-
rubyWarnings, parseEnvironment);
69+
var parseResult = YARPTranslatorDriver.parseToYARPAST(language, source, Collections.emptyList(),
70+
parseEnvironment);
71+
72+
YARPTranslatorDriver.handleWarningsErrorsNoContext(language, parseResult, source, yarpSource);
7673

7774
return Pair.create(parseResult, source.getSource());
7875
}

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,13 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
188188
parseResult = context.getMetricsProfiler().callWithMetrics(
189189
"parsing",
190190
source.getName(),
191-
() -> parseToYARPAST(context, language, rubySource, localVariableNames,
192-
parserConfiguration, rubyWarnings, parseEnvironment));
191+
() -> parseToYARPAST(language, rubySource, localVariableNames,
192+
parseEnvironment));
193193
printParseTranslateExecuteMetric("after-parsing", context, source);
194194
}
195+
196+
handleWarningsErrorsPrimitives(context, language, parseResult, rubySource, parserConfiguration, rubyWarnings);
197+
195198
var node = parseResult.value;
196199

197200
// Needs the magic comment to be parsed
@@ -388,9 +391,8 @@ private String getMethodName(ParserContext parserContext, MaterializedFrame pare
388391
}
389392
}
390393

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) {
394396
TruffleSafepoint.poll(DummyNode.INSTANCE);
395397

396398
byte[] sourceBytes = rubySource.getBytes();
@@ -437,7 +439,15 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
437439
byte[] serializedBytes = Parser.parseAndSerialize(sourceBytes, parsingOptions);
438440

439441
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();
441451

442452
final ParseResult.Error[] errors = parseResult.errors;
443453

@@ -446,12 +456,7 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
446456
Nodes.Location location = warning.location;
447457
SourceSection section = rubySource.getSource().createSection(location.startOffset, location.length);
448458

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();
455460

456461
switch (warning.level) {
457462
case WARNING_DEFAULT -> rubyWarnings.warn(sourcePath, lineNumber, warning.message);
@@ -462,7 +467,7 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
462467
if (errors.length != 0) {
463468
// print warnings immediately
464469
// if there is no syntax error they will be printed in runtime
465-
if (!rubyWarnings.warnings.isEmpty() && context != null) {
470+
if (!rubyWarnings.warnings.isEmpty()) {
466471
EmitWarningsNode.printWarnings(context, rubyWarnings);
467472
}
468473

@@ -474,11 +479,6 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
474479
Nodes.Location location = error.location;
475480
SourceSection section = rubySource.getSource().createSection(location.startOffset, location.length);
476481

477-
if (context == null) {
478-
throw CompilerDirectives.shouldNotReachHere(
479-
"Parse error in " + RubyLanguage.filenameLine(section) + ": " + error.message);
480-
}
481-
482482
String message = context.fileLine(section) + ": " + error.message;
483483
throw new RaiseException(
484484
context,
@@ -498,8 +498,25 @@ public static org.prism.ParseResult parseToYARPAST(RubyContext context, RubyLang
498498
configuration.allowTruffleRubyPrimitives = value.equalsIgnoreCase("true");
499499
}
500500
}
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+
}
501516

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);
503520
}
504521

505522
public static RubySource createRubySource(Object code) {

0 commit comments

Comments
 (0)