Skip to content

Commit

Permalink
Merge pull request #28 from openrewrite/release-fixes-03
Browse files Browse the repository at this point in the history
proper error handling via ctx
  • Loading branch information
garyolsen authored Mar 21, 2023
2 parents 3ab0769 + dd62ed8 commit cc64895
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
32 changes: 22 additions & 10 deletions src/main/java/org/openrewrite/python/PythonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.python.internal.IntelliJUtils;
import org.openrewrite.python.internal.PsiPythonMapper;
import org.openrewrite.python.tree.Py;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.tree.ParsingExecutionContextView;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -66,18 +68,28 @@ public List<Py.CompilationUnit> parse(String... sources) {
}

@Override
public List<Py.CompilationUnit> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx) {
return StreamSupport.stream(sources.spliterator(), false).map(sourceFile -> {
EncodingDetectingInputStream is = sourceFile.getSource(ctx);
public List<Py.CompilationUnit> parseInputs(Iterable<Input> inputs, @Nullable Path relativeTo, ExecutionContext ogCtx) {
ParsingExecutionContextView ctx = ParsingExecutionContextView.view(ogCtx);
List<Py.CompilationUnit> mapped = new ArrayList<>();

for (Input input : inputs) {
EncodingDetectingInputStream is = input.getSource(ctx);
String sourceText = is.readFully();

return new PsiPythonMapper().mapSource(
sourceText,
sourceFile.getPath(),
is.getCharset().toString(),
is.isCharsetBomMarked()
);
}).collect(toList());
try {
mapped.add(new PsiPythonMapper().mapSource(
sourceText,
input.getPath(),
is.getCharset().toString(),
is.isCharsetBomMarked()
));
} catch (Throwable t) {
ctx.parseFailure(input, relativeTo, this, t);
ctx.getOnError().accept(t);
}
}

return mapped;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Py.CompilationUnit mapSource(String sourceText, Path path, String charset

public Py.CompilationUnit mapFile(PyFile element, Path path, String charset, boolean isCharsetBomMarked) {
// Uncomment when doing development if you want a PSI tree to print out
new IntelliJUtils.PsiPrinter().print(element.getNode());
// new IntelliJUtils.PsiPrinter().print(element.getNode());
BlockContext ctx = BlockContext.root(element);
List<Statement> statements = singletonList(
mapBlock(element, null, element.getStatements(), ctx)
Expand Down Expand Up @@ -1732,8 +1732,9 @@ private Py.MatchCase.Pattern mapPattern(PyPattern pattern) {
kind = Py.MatchCase.Pattern.Kind.WILDCARD;
children = JContainer.empty();
} else {
System.err.println("WARNING: unhandled pattern of type " + pattern.getClass().getSimpleName());
return null;
throw new IllegalArgumentException(
String.format("unhandled case pattern of type %s", pattern.getClass().getSimpleName())
);
}

return new Py.MatchCase.Pattern(
Expand Down Expand Up @@ -2236,8 +2237,9 @@ private Expression mapPrefixExpression(PyPrefixExpression element) {
} else if (op == PyTokenTypes.TILDE) {
ot = J.Unary.Type.Complement;
} else {
System.err.println("WARNING: unhandled prefix expression of ot " + op);
return null;
throw new IllegalArgumentException(
String.format("unhandled prefix expression with type %s", op)
);
}
return new J.Unary(
randomId(),
Expand Down

0 comments on commit cc64895

Please sign in to comment.