From d6e286f8b595d07fd3d2656a235fe49e3843faf9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 6 Jun 2023 18:33:29 +0100 Subject: Add tokeniser --- .../compiler/sources/FileFragmentSource.java | 53 ++++++++++++++++++++++ .../org/shedlang/compiler/sources/NullSource.java | 24 ++++++++++ .../java/org/shedlang/compiler/sources/Source.java | 7 +++ .../org/shedlang/compiler/sources/Sources.java | 9 ++++ 4 files changed, 93 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java create mode 100644 src/main/java/org/shedlang/compiler/sources/NullSource.java create mode 100644 src/main/java/org/shedlang/compiler/sources/Source.java create mode 100644 src/main/java/org/shedlang/compiler/sources/Sources.java (limited to 'src/main/java/org/shedlang/compiler/sources') diff --git a/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java b/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java new file mode 100644 index 00000000..4fc8889a --- /dev/null +++ b/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java @@ -0,0 +1,53 @@ +package org.shedlang.compiler.sources; + +record FileFragmentSource( + String filename, + String contents, + int characterIndexStart, + int characterIndexEnd +) implements Source { + static FileFragmentSource file(String filename, String contents) { + return new FileFragmentSource(filename, contents, 0, contents.length()); + } + + @Override + public String describe() { + var lines = contents.split("\n"); + var position = 0; + + for (var lineIndex = 0; lineIndex < lines.length; lineIndex++) { + var line = lines[lineIndex]; + var nextLinePosition = position + line.length() + 1; + if (nextLinePosition > characterIndexStart || nextLinePosition >= contents.length()) { + return context( + line, + lineIndex, + characterIndexStart - position + ); + } + position = nextLinePosition; + } + throw new RuntimeException("should be impossible (but evidently isn't)"); + } + + @Override + public Source at(int characterIndexStart, int characterIndexEnd) { + return new FileFragmentSource( + filename, + contents, + this.characterIndexStart + characterIndexStart, + // TODO: check this is less than characterIndexEnd + this.characterIndexStart + characterIndexEnd + ); + } + + public Source end() { + return at(contents.length(), contents.length()); + } + + private String context(String line, int lineIndex, int columnIndex) { + var lineNumber = lineIndex + 1; + var columnNumber = columnIndex + 1; + return filename + ":" + lineNumber + ":" + columnNumber + "\n" + line + "\n" + " ".repeat(columnIndex) + "^"; + } +} diff --git a/src/main/java/org/shedlang/compiler/sources/NullSource.java b/src/main/java/org/shedlang/compiler/sources/NullSource.java new file mode 100644 index 00000000..e4f0948f --- /dev/null +++ b/src/main/java/org/shedlang/compiler/sources/NullSource.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.sources; + +class NullSource implements Source { + private NullSource() { + + } + + static final NullSource INSTANCE = new NullSource(); + + @Override + public String contents() { + return ""; + } + + @Override + public String describe() { + return "(null)"; + } + + @Override + public Source at(int characterIndexStart, int characterIndexEnd) { + return this; + } +} diff --git a/src/main/java/org/shedlang/compiler/sources/Source.java b/src/main/java/org/shedlang/compiler/sources/Source.java new file mode 100644 index 00000000..b40f3b63 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/sources/Source.java @@ -0,0 +1,7 @@ +package org.shedlang.compiler.sources; + +public interface Source { + String contents(); + String describe(); + Source at(int characterIndexStart, int characterIndexEnd); +} diff --git a/src/main/java/org/shedlang/compiler/sources/Sources.java b/src/main/java/org/shedlang/compiler/sources/Sources.java new file mode 100644 index 00000000..c815d1e0 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/sources/Sources.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.sources; + +public class Sources { + public static Source NULL = NullSource.INSTANCE; + + public static Source file(String filename, String contents) { + return FileFragmentSource.file(filename, contents); + } +} -- cgit v1.2.3