
org.openrewrite.json.JsonParser Maven / Gradle / Ivy
/*
* Copyright 2021 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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.json;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
import org.antlr.v4.runtime.*;
import org.intellij.lang.annotations.Language;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.internal.MetricsHelper;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.json.internal.JsonParserVisitor;
import org.openrewrite.json.internal.grammar.JSON5Lexer;
import org.openrewrite.json.internal.grammar.JSON5Parser;
import org.openrewrite.json.tree.Json;
import org.openrewrite.tree.ParsingEventListener;
import org.openrewrite.tree.ParsingExecutionContextView;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import static java.util.stream.Collectors.toList;
public class JsonParser implements Parser {
@Override
public List parseInputs(Iterable sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles).stream()
.map(sourceFile -> {
Timer.Builder timer = Timer.builder("rewrite.parse")
.description("The time spent parsing an Json file")
.tag("file.type", "Json");
Timer.Sample sample = Timer.start();
try (InputStream sourceStream = sourceFile.getSource()) {
JSON5Parser parser = new JSON5Parser(new CommonTokenStream(new JSON5Lexer(
CharStreams.fromStream(sourceStream))));
parser.removeErrorListeners();
parser.addErrorListener(new ForwardingErrorListener(sourceFile.getPath(), ctx));
Json.Document document = new JsonParserVisitor(
sourceFile.getRelativePath(relativeTo),
sourceFile.getFileAttributes(),
sourceFile.getSource()
).visitJson5(parser.json5());
sample.stop(MetricsHelper.successTags(timer).register(Metrics.globalRegistry));
parsingListener.parsed(sourceFile, document);
return document;
} catch (Throwable t) {
sample.stop(MetricsHelper.errorTags(timer, t).register(Metrics.globalRegistry));
ctx.getOnError().accept(new IllegalStateException(sourceFile.getPath() + " " + t.getMessage(), t));
return null;
}
})
.filter(Objects::nonNull)
.collect(toList());
}
@Override
public List parse(@Language("Json") String... sources) {
return parse(new InMemoryExecutionContext(), sources);
}
@Override
public boolean accept(Path path) {
return path.toString().endsWith(".json");
}
@Override
public Path sourcePathFromSourceText(Path prefix, String sourceCode) {
return prefix.resolve("file.json");
}
private static class ForwardingErrorListener extends BaseErrorListener {
private final Path sourcePath;
private final ExecutionContext ctx;
private ForwardingErrorListener(Path sourcePath, ExecutionContext ctx) {
this.sourcePath = sourcePath;
this.ctx = ctx;
}
@Override
public void syntaxError(Recognizer, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg, RecognitionException e) {
ctx.getOnError().accept(new JsonParsingException(sourcePath,
String.format("Syntax error in %s at line %d:%d %s.", sourcePath, line, charPositionInLine, msg), e));
}
}
}