diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-03-21 11:01:28 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-03-21 11:01:28 +0000 |
| commit | 48a0d074425e5105e92fd3f518c9d80ae8f6dd9a (patch) | |
| tree | 568ab7f215fb5e68149024bd36a0ff387c304435 /src/main/java | |
| parent | 6561e50d28066b0913fb5395f238111c3b0479d6 (diff) | |
Add deserialization for sources
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java | 11 | ||||
| -rw-r--r-- | src/main/java/org/shedlang/compiler/sources/protobuf/SourceToProtobuf.java | 68 |
2 files changed, 65 insertions, 14 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java b/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java index e3479ed4..8ceb9019 100644 --- a/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java +++ b/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java @@ -2,10 +2,11 @@ package org.shedlang.compiler.patio.protobuf; import org.shedlang.compiler.patio.*; import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.protobuf.SourceToProtobuf; import java.util.Optional; -import static org.shedlang.compiler.sources.protobuf.SourceToProtobuf.sourceToProtobuf; +import static org.shedlang.compiler.sources.protobuf.SourceToProtobuf.serializeSource; public class PatioToProtobuf { public PatioProtobuf.PatioCompilationUnit compilationUnitToProtobuf( @@ -13,7 +14,7 @@ public class PatioToProtobuf { ) { var builder = PatioProtobuf.PatioCompilationUnit.newBuilder(); - builder.setSource(sourceToProtobuf(compilationUnit.source(), Optional.empty())); + builder.setSource(serializeSource(compilationUnit.source(), Optional.empty())); for (var element : compilationUnit.elements()) { builder.addElements(compilationUnitElementToProtobuf(element, compilationUnit.source())); @@ -63,7 +64,7 @@ public class PatioToProtobuf { Source parentSource ) { var builder = PatioProtobuf.PatioDataElementImmutableDefinition.newBuilder(); - builder.setSource(sourceToProtobuf(element.source(), parentSource)); + builder.setSource(SourceToProtobuf.serializeSource(element.source(), parentSource)); builder.setName(element.name()); builder.setVisibility(visibilityToProtobuf(element.visibility())); return builder.build(); @@ -74,7 +75,7 @@ public class PatioToProtobuf { Source parentSource ) { var builder = PatioProtobuf.PatioDataElementMutableDeclaration.newBuilder(); - builder.setSource(sourceToProtobuf(element.source(), parentSource)); + builder.setSource(SourceToProtobuf.serializeSource(element.source(), parentSource)); builder.setName(element.name()); return builder.build(); } @@ -84,7 +85,7 @@ public class PatioToProtobuf { Source parentSource ) { var builder = PatioProtobuf.PatioDataElementMutableDefinition.newBuilder(); - builder.setSource(sourceToProtobuf(element.source(), parentSource)); + builder.setSource(SourceToProtobuf.serializeSource(element.source(), parentSource)); builder.setName(element.name()); builder.setVisibility(visibilityToProtobuf(element.visibility())); return builder.build(); diff --git a/src/main/java/org/shedlang/compiler/sources/protobuf/SourceToProtobuf.java b/src/main/java/org/shedlang/compiler/sources/protobuf/SourceToProtobuf.java index 2495557b..b8a390a0 100644 --- a/src/main/java/org/shedlang/compiler/sources/protobuf/SourceToProtobuf.java +++ b/src/main/java/org/shedlang/compiler/sources/protobuf/SourceToProtobuf.java @@ -1,10 +1,9 @@ package org.shedlang.compiler.sources.protobuf; -import org.shedlang.compiler.sources.FileFragmentSource; -import org.shedlang.compiler.sources.NullSource; -import org.shedlang.compiler.sources.RuntimeSource; -import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.*; +import org.zwobble.sourcetext.SourcePosition; import org.zwobble.sourcetext.SourceRange; +import org.zwobble.sourcetext.SourceText; import java.util.Optional; @@ -12,14 +11,14 @@ public class SourceToProtobuf { private SourceToProtobuf() { } - public static SourcesProtobuf.Source sourceToProtobuf( + public static SourcesProtobuf.Source serializeSource( Source source, Source parentSource ) { - return sourceToProtobuf(source, Optional.of(parentSource)); + return serializeSource(source, Optional.of(parentSource)); } - public static SourcesProtobuf.Source sourceToProtobuf( + public static SourcesProtobuf.Source serializeSource( Source source, Optional<Source> parentSource ) { @@ -38,7 +37,7 @@ public class SourceToProtobuf { } case FileFragmentSource fileFragmentSource -> { builder.setFileFragmentSource( - fileFragmentSourceToProtobuf(fileFragmentSource, parentSource) + serializeFileFragmentSource(fileFragmentSource, parentSource) ); } default -> { @@ -49,7 +48,37 @@ public class SourceToProtobuf { return builder.build(); } - private static SourcesProtobuf.FileFragmentSource fileFragmentSourceToProtobuf( + public static Source deserializeSource( + SourcesProtobuf.Source sourceProtobuf, + Source parentSource + ) { + return deserializeSource(sourceProtobuf, Optional.of(parentSource)); + } + + public static Source deserializeSource( + SourcesProtobuf.Source sourceProtobuf, + Optional<Source> parentSource + ) { + return switch (sourceProtobuf.getValueCase()) { + case NULL_SOURCE -> + Sources.NULL; + + case RUNTIME_SOURCE -> + Sources.RUNTIME; + + case FILE_FRAGMENT_SOURCE -> + deserializeFileFragmentSource( + sourceProtobuf.getFileFragmentSource(), + parentSource + ); + + case VALUE_NOT_SET -> { + throw new UnsupportedOperationException("TODO"); + } + }; + } + + private static SourcesProtobuf.FileFragmentSource serializeFileFragmentSource( FileFragmentSource source, Optional<Source> parentSource ) { @@ -65,6 +94,7 @@ public class SourceToProtobuf { builder.setSourceText( SourcesProtobuf.SourceText.newBuilder() .setName(sourceTextName) + .setContents(source.sourceRange().sourceText().charSequence().toString()) .build() ); } @@ -74,4 +104,24 @@ public class SourceToProtobuf { return builder.build(); } + + private static FileFragmentSource deserializeFileFragmentSource( + SourcesProtobuf.FileFragmentSource sourceProtobuf, + Optional<Source> parentSource + ) { + SourceText sourceText; + if (sourceProtobuf.hasSourceText()) { + sourceText = SourceText.fromString( + sourceProtobuf.getSourceText().getName(), + sourceProtobuf.getSourceText().getContents() + ); + } else { + // TODO: handle missing parent + sourceText = ((FileFragmentSource) parentSource.orElseThrow()).sourceRange().sourceText(); + } + + var start = new SourcePosition(sourceText, sourceProtobuf.getStartCharacterIndex()); + var end = new SourcePosition(sourceText, sourceProtobuf.getEndCharacterIndex()); + return new FileFragmentSource(new SourceRange(sourceText, start, end)); + } } |
