Skip to content

Commit edff900

Browse files
committed
Handle yarp.ParseResult with errors and warnings
1 parent d9730ca commit edff900

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ protected Object parse(Object code,
292292
byte[] serialized = yarpSerialize(getLanguage(), source);
293293

294294
var yarpSource = YARPTranslatorDriver.createYARPSource(source, YARPTranslatorDriver.createRubySource(code));
295-
var ast = Loader.load(serialized, yarpSource);
295+
var parseResult = Loader.load(serialized, yarpSource);
296+
var ast = parseResult.getValue();
296297

297298
return createString(fromJavaStringNode, ast.toString(), Encodings.UTF_8);
298299
}

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.truffleruby.language.arguments.MissingArgumentBehavior;
7171
import org.truffleruby.language.arguments.ReadPreArgumentNode;
7272
import org.truffleruby.language.arguments.RubyArguments;
73+
import org.truffleruby.language.control.RaiseException;
7374
import org.truffleruby.language.control.WhileNode;
7475
import org.truffleruby.language.control.WhileNodeFactory;
7576
import org.truffleruby.language.library.RubyStringLibrary;
@@ -84,6 +85,7 @@
8485
import org.truffleruby.shared.Metrics;
8586
import org.yarp.Loader;
8687
import org.yarp.Nodes;
88+
import org.yarp.ParseResult;
8789
import org.yarp.Parser;
8890

8991
import java.util.ArrayList;
@@ -405,10 +407,52 @@ public static org.yarp.Nodes.Node parseToYARPAST(RubyContext context, RubyLangua
405407
byte[] serializedBytes = Parser.parseAndSerialize(sourceBytes);
406408

407409
var yarpSource = createYARPSource(sourceBytes, rubySource);
408-
return Loader.load(serializedBytes, yarpSource);
409-
// YARP end
410+
var parseResult = Loader.load(serializedBytes, yarpSource);
411+
412+
final String filename = rubySource.getSourcePath();
413+
final ParseResult.Error[] errors = parseResult.getErrors();
414+
415+
// collect warnings generated by the parser
416+
for (ParseResult.Warning warning : parseResult.getWarnings()) {
417+
Nodes.Location location = warning.getLocation();
418+
SourceSection section = rubySource.getSource().createSection(location.startOffset, location.length);
419+
int lineNumber = RubySource.getStartLineAdjusted(context, section);
420+
421+
rubyWarnings.warn(filename, lineNumber, warning.getMessage());
422+
}
423+
424+
if (errors.length != 0) {
425+
// print warnings immediately
426+
// if there is no syntax error they will be printed in runtime
427+
if (!rubyWarnings.warnings.isEmpty()) {
428+
EmitWarningsNode.printWarnings(context, rubyWarnings);
429+
}
430+
431+
// Handle only the first reported syntax error.
432+
// The order of errors should be deterministic,
433+
// but it isn't guarantied that the first error is the most specific/relevant to user
434+
ParseResult.Error error = errors[0];
410435

411-
// TODO: handle syntax errors
436+
Nodes.Location location = error.getLocation();
437+
SourceSection section = rubySource.getSource().createSection(location.startOffset, location.length);
438+
String message = context.fileLine(section) + ": " + error.getMessage();
439+
440+
if (context != null) {
441+
int lineNumber = RubySource.getStartLineAdjusted(context, section);
442+
443+
throw new RaiseException(
444+
context,
445+
context.getCoreExceptions().syntaxErrorAlreadyWithFileLine(
446+
message,
447+
null,
448+
rubySource.getSource().createSection(lineNumber)));
449+
} else {
450+
throw new UnsupportedOperationException(message);
451+
}
452+
}
453+
454+
return parseResult.getValue();
455+
// YARP end
412456

413457
// RubyParser parser = new RubyParser(lexerSource, rubyWarnings);
414458
// TruffleSafepoint.poll(DummyNode.INSTANCE); // RubyParser <clinit> takes a while

0 commit comments

Comments
 (0)