Skip to content

Commit

Permalink
Parser no longer parameterized / parse exceptions held on `ParseError…
Browse files Browse the repository at this point in the history
…` rather than `PlainText` (openrewrite#3305)
  • Loading branch information
jkschneider authored Jun 9, 2023
1 parent c45abbb commit 6e19522
Show file tree
Hide file tree
Showing 57 changed files with 646 additions and 882 deletions.
100 changes: 100 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/ParseError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite;

import lombok.*;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.Markers;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
import java.util.UUID;

import static java.util.Collections.singletonList;

@Value
public class ParseError implements SourceFile {
@With
@EqualsAndHashCode.Include
@Getter
UUID id;

@With
@Getter
Markers markers;

@With
@Getter
Path sourcePath;

@With
@Getter
@Nullable
FileAttributes fileAttributes;

@Nullable // for backwards compatibility
@With(AccessLevel.PRIVATE)
String charsetName;

@Override
public Charset getCharset() {
return charsetName == null ? StandardCharsets.UTF_8 : Charset.forName(charsetName);
}

@SuppressWarnings("unchecked")
@Override
public SourceFile withCharset(Charset charset) {
return withCharsetName(charset.name());
}

@With
@Getter
boolean charsetBomMarked;

@With
@Getter
@Nullable
Checksum checksum;

@With
String text;

@Override
public <P> boolean isAcceptable(TreeVisitor<?, P> v, P p) {
return true;
}

public static ParseError build(Parser parser,
Parser.Input input,
@Nullable Path relativeTo,
ExecutionContext ctx,
Throwable t) {
EncodingDetectingInputStream is = input.getSource(ctx);
return new ParseError(
Tree.randomId(),
new Markers(Tree.randomId(), singletonList(ParseExceptionResult.build(parser, t))),
input.getRelativePath(relativeTo),
input.getFileAttributes(),
parser.getCharset(ctx).name(),
is.isCharsetBomMarked(),
null,
is.readFully()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ParseExceptionResult implements Marker {
UUID id;
String message;

public static ParseExceptionResult build(Parser<?> parser, Throwable t) {
public static ParseExceptionResult build(Parser parser, Throwable t) {
return new ParseExceptionResult(randomId(), ExceptionUtils.sanitizeStackTrace(t, parser.getClass()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ParseWarning implements Marker {
UUID id;
String message;

public static ParseWarning build(Parser<?> parser, Throwable t) {
public static ParseWarning build(Parser parser, Throwable t) {
return new ParseWarning(randomId(), ExceptionUtils.sanitizeStackTrace(t, parser.getClass()));
}
}
19 changes: 9 additions & 10 deletions rewrite-core/src/main/java/org/openrewrite/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

import static java.util.stream.Collectors.toList;

public interface Parser<S extends SourceFile> {
default Stream<S> parse(Iterable<Path> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
public interface Parser {
default Stream<SourceFile> parse(Iterable<Path> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
return parseInputs(StreamSupport
.stream(sourceFiles.spliterator(), false)
.map(sourceFile -> new Input(sourceFile, () -> {
Expand All @@ -54,11 +54,11 @@ default Stream<S> parse(Iterable<Path> sourceFiles, @Nullable Path relativeTo, E
);
}

default Stream<S> parse(String... sources) {
default Stream<SourceFile> parse(String... sources) {
return parse(new InMemoryExecutionContext(), sources);
}

default Stream<S> parse(ExecutionContext ctx, String... sources) {
default Stream<SourceFile> parse(ExecutionContext ctx, String... sources) {
return parseInputs(
Arrays.stream(sources).map(source ->
new Input(
Expand All @@ -79,21 +79,20 @@ default Stream<S> parse(ExecutionContext ctx, String... sources) {
* @param ctx The execution context
* @return A stream of {@link SourceFile}.
*/
Stream<S> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx);
Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx);

boolean accept(Path path);

default boolean accept(Input input) {
return input.isSynthetic() || accept(input.getPath());
}

default List<Input> acceptedInputs(Iterable<Input> input) {
default Stream<Input> acceptedInputs(Iterable<Input> input) {
return StreamSupport.stream(input.spliterator(), false)
.filter(this::accept)
.collect(toList());
.filter(this::accept);
}

default Parser<S> reset() {
default Parser reset() {
return this;
}

Expand Down Expand Up @@ -218,7 +217,7 @@ abstract class Builder implements Cloneable {
@Getter
private final Class<? extends SourceFile> sourceFileType;

public abstract Parser<?> build();
public abstract Parser build();

/**
* The name of the domain specific language this parser builder produces a parser for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package org.openrewrite.binary;

import org.openrewrite.ExecutionContext;
import org.openrewrite.ParseError;
import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.Markers;
import org.openrewrite.tree.ParsingExecutionContextView;

import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

Expand All @@ -32,10 +32,10 @@
/**
* Doesn't actually _parse_ anything, but if you want to wrap binary data into a SourceFile, this will do the trick
*/
public class BinaryParser implements Parser<Binary> {
public class BinaryParser implements Parser {

@Override
public Stream<Binary> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx) {
public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx) {
return StreamSupport.stream(sources.spliterator(), false)
.map(source -> {
Path path = source.getRelativePath(relativeTo);
Expand All @@ -46,13 +46,11 @@ public Stream<Binary> parseInputs(Iterable<Input> sources, @Nullable Path relati
source.getFileAttributes(),
null,
readAllBytes(source.getSource(ctx)));
} catch (Exception e) {
ParsingExecutionContextView.view(ctx).parseFailure(source, relativeTo, this, e);
ctx.getOnError().accept(e);
} catch (Throwable t) {
ctx.getOnError().accept(t);
return ParseError.build(this, source, relativeTo, ctx, t);
}
return null;
})
.filter(Objects::nonNull);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.openrewrite.Tree.randomId;

public class QuarkParser implements Parser<Quark> {
public class QuarkParser implements Parser {

public static List<Quark> parseAllOtherFiles(Path rootDir, List<SourceFile> sourceFiles) throws IOException {
public static Stream<SourceFile> parseAllOtherFiles(Path rootDir, List<SourceFile> sourceFiles) throws IOException {
Stack<List<PathMatcher>> gitignores = new Stack<>();
parseGitignore(new File(System.getProperty("user.home") + "/.gitignore"), gitignores);

Expand Down Expand Up @@ -85,8 +84,7 @@ private boolean isIgnored(Path path) {
}
});

return new QuarkParser().parse(quarks, rootDir, new InMemoryExecutionContext())
.collect(Collectors.toList());
return new QuarkParser().parse(quarks, rootDir, new InMemoryExecutionContext());
}

private static void parseGitignore(File gitignore, Stack<List<PathMatcher>> gitignores) throws IOException {
Expand All @@ -107,7 +105,7 @@ private static void parseGitignore(File gitignore, Stack<List<PathMatcher>> giti
}

@Override
public Stream<Quark> parseInputs(Iterable<Parser.Input> sources, @Nullable Path relativeTo,
public Stream<SourceFile> parseInputs(Iterable<Parser.Input> sources, @Nullable Path relativeTo,
ExecutionContext ctx) {
return StreamSupport.stream(sources.spliterator(), false).map(source ->
new Quark(randomId(),
Expand Down Expand Up @@ -138,14 +136,13 @@ public Builder() {
}

@Override
public Parser<Quark> build() {
public QuarkParser build() {
return new QuarkParser();
}

@Override
public String getDslName() {
return "other";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
}

@Override
public Collection<PlainText> generate(AtomicBoolean shouldCreate, ExecutionContext ctx) {
public Collection<SourceFile> generate(AtomicBoolean shouldCreate, ExecutionContext ctx) {
if(shouldCreate.get()) {
return new PlainTextParser().parse(fileContents)
.map(brandNewFile -> brandNewFile.withSourcePath(Paths.get(relativeFileName)))
.map(brandNewFile -> (SourceFile) brandNewFile.withSourcePath(Paths.get(relativeFileName)))
.collect(Collectors.toList());
}
return emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.text;

import org.openrewrite.ExecutionContext;
import org.openrewrite.ParseError;
import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.EncodingDetectingInputStream;
Expand All @@ -25,13 +26,12 @@
import org.openrewrite.tree.ParsingExecutionContextView;

import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.openrewrite.Tree.randomId;

public class PlainTextParser implements Parser<PlainText> {
public class PlainTextParser implements Parser {

/**
* Downcast a {@link SourceFile} to a {@link PlainText} if it isn't already one.
Expand Down Expand Up @@ -59,33 +59,32 @@ public static PlainText convert(SourceFile sourceFile) {
}

@Override
public Stream<PlainText> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo,
ExecutionContext ctx) {
public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo,
ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return StreamSupport.stream(sources.spliterator(), false)
.map(source -> {
Path path = source.getRelativePath(relativeTo);
try {
EncodingDetectingInputStream is = source.getSource(ctx);
String sourceStr = is.readFully();
PlainText plainText = new PlainText(randomId(),
path,
Markers.EMPTY,
is.getCharset().name(),
is.isCharsetBomMarked(),
source.getFileAttributes(),
null,
sourceStr,
null);
parsingListener.parsed(source, plainText);
return plainText;
} catch (Throwable t) {
ParsingExecutionContextView.view(ctx).parseFailure(source, relativeTo, this, t);
ctx.getOnError().accept(t);
}
return null;
})
.filter(Objects::nonNull);
return StreamSupport.stream(sources.spliterator(), false).map(source -> {
Path path = source.getRelativePath(relativeTo);
try {
EncodingDetectingInputStream is = source.getSource(ctx);
String sourceStr = is.readFully();
PlainText plainText = new PlainText(
randomId(),
path,
Markers.EMPTY,
is.getCharset().name(),
is.isCharsetBomMarked(),
source.getFileAttributes(),
null,
sourceStr,
null
);
parsingListener.parsed(source, plainText);
return plainText;
} catch (Throwable t) {
ctx.getOnError().accept(t);
return ParseError.build(this, source, relativeTo, ctx, t);
}
});
}

@Override
Expand All @@ -108,7 +107,7 @@ public Builder() {
}

@Override
public Parser<?> build() {
public PlainTextParser build() {
return new PlainTextParser();
}

Expand Down
Loading

0 comments on commit 6e19522

Please sign in to comment.