aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/tokeniser
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-01-08 19:14:03 +0000
committerMichael Williamson <mike@zwobble.org>2025-01-09 20:36:42 +0000
commit3666a4af2eb35177eecd557d2a75f4bac9da75b9 (patch)
treea85230de832bc7f970dd89aaa6bb2da6ea5875f4 /src/main/java/org/shedlang/compiler/tokeniser
parent1e6e1f7bb8776fc013f76828ed8df3f9297d095b (diff)
Use source text to implement FileFragmentSource
Diffstat (limited to 'src/main/java/org/shedlang/compiler/tokeniser')
-rw-r--r--src/main/java/org/shedlang/compiler/tokeniser/InfinitelyPaddedIterator.java4
-rw-r--r--src/main/java/org/shedlang/compiler/tokeniser/RegexTokeniser.java13
-rw-r--r--src/main/java/org/shedlang/compiler/tokeniser/Token.java8
-rw-r--r--src/main/java/org/shedlang/compiler/tokeniser/TokenIterator.java22
-rw-r--r--src/main/java/org/shedlang/compiler/tokeniser/UnexpectedTokenException.java3
5 files changed, 35 insertions, 15 deletions
diff --git a/src/main/java/org/shedlang/compiler/tokeniser/InfinitelyPaddedIterator.java b/src/main/java/org/shedlang/compiler/tokeniser/InfinitelyPaddedIterator.java
index 534020d9..54548610 100644
--- a/src/main/java/org/shedlang/compiler/tokeniser/InfinitelyPaddedIterator.java
+++ b/src/main/java/org/shedlang/compiler/tokeniser/InfinitelyPaddedIterator.java
@@ -28,4 +28,8 @@ public class InfinitelyPaddedIterator<T> {
public void moveNext() {
index++;
}
+
+ public boolean isAtStart() {
+ return this.index == 0;
+ }
}
diff --git a/src/main/java/org/shedlang/compiler/tokeniser/RegexTokeniser.java b/src/main/java/org/shedlang/compiler/tokeniser/RegexTokeniser.java
index 942960a3..a34ae3d5 100644
--- a/src/main/java/org/shedlang/compiler/tokeniser/RegexTokeniser.java
+++ b/src/main/java/org/shedlang/compiler/tokeniser/RegexTokeniser.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.tokeniser;
-import org.shedlang.compiler.sources.Source;
+import org.zwobble.sourcetext.SourceText;
import java.util.ArrayList;
import java.util.List;
@@ -47,8 +47,8 @@ public class RegexTokeniser<T> {
.collect(Collectors.toList());
}
- public List<Token<T>> tokenise(Source source) {
- var matcher = pattern.matcher(source.contents());
+ public List<Token<T>> tokenise(SourceText sourceText) {
+ var matcher = pattern.matcher(sourceText.charSequence());
var tokens = new ArrayList<Token<T>>();
while (matcher.lookingAt()) {
@@ -58,12 +58,15 @@ public class RegexTokeniser<T> {
.getAsInt();
var tokenType = this.rules.get(groupIndex - 1);
+ var startPosition = sourceText.characterPosition(matcher.regionStart());
+ var endPosition = sourceText.characterPosition(matcher.end());
tokens.add(new Token<>(
- source.at(matcher.regionStart(), matcher.end()),
+ startPosition.to(endPosition),
tokenType,
+ // TODO: the text value is redundant, can be gotten from the source range?
matcher.group()
));
- matcher.region(matcher.end(), source.contents().length());
+ matcher.region(matcher.end(), sourceText.characterLength());
}
return tokens;
diff --git a/src/main/java/org/shedlang/compiler/tokeniser/Token.java b/src/main/java/org/shedlang/compiler/tokeniser/Token.java
index e79d89eb..7e131304 100644
--- a/src/main/java/org/shedlang/compiler/tokeniser/Token.java
+++ b/src/main/java/org/shedlang/compiler/tokeniser/Token.java
@@ -1,10 +1,10 @@
package org.shedlang.compiler.tokeniser;
-import org.shedlang.compiler.sources.Source;
+import org.zwobble.sourcetext.SourceRange;
-public record Token<T>(Source source, T tokenType, String value) {
- public static <T> Token<T> token(Source source, T tokenType, String value) {
- return new Token<T>(source, tokenType, value);
+public record Token<T>(SourceRange sourceRange, T tokenType, String value) {
+ public static <T> Token<T> token(SourceRange sourceRange, T tokenType, String value) {
+ return new Token<T>(sourceRange, tokenType, value);
}
public String describe() {
diff --git a/src/main/java/org/shedlang/compiler/tokeniser/TokenIterator.java b/src/main/java/org/shedlang/compiler/tokeniser/TokenIterator.java
index 3462b4b7..9b3467a8 100644
--- a/src/main/java/org/shedlang/compiler/tokeniser/TokenIterator.java
+++ b/src/main/java/org/shedlang/compiler/tokeniser/TokenIterator.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.tokeniser;
-import org.shedlang.compiler.sources.Source;
+import org.zwobble.sourcetext.SourcePosition;
import java.util.List;
@@ -15,11 +15,11 @@ public class TokenIterator<TTokenType> {
nextValue(tokenType);
}
- public String nextValue(TTokenType tokenType) {
+ public Token<TTokenType> next(TTokenType tokenType) {
var token = tokens.get();
if (token.tokenType().equals(tokenType)) {
tokens.moveNext();
- return token.value();
+ return token;
} else {
throw new UnexpectedTokenException(
tokenType.toString(),
@@ -28,6 +28,10 @@ public class TokenIterator<TTokenType> {
}
}
+ public String nextValue(TTokenType tokenType) {
+ return this.next(tokenType).value();
+ }
+
public boolean isNext(TTokenType tokenType) {
return tokens.get().tokenType().equals(tokenType);
}
@@ -54,7 +58,15 @@ public class TokenIterator<TTokenType> {
return tokens.get(offset);
}
- public Source position() {
- return tokens.get().source().start();
+ public SourcePosition startPosition() {
+ return tokens.get().sourceRange().start();
+ }
+
+ public SourcePosition endPosition() {
+ if (tokens.isAtStart()) {
+ return startPosition();
+ } else {
+ return tokens.get(-1).sourceRange().end();
+ }
}
}
diff --git a/src/main/java/org/shedlang/compiler/tokeniser/UnexpectedTokenException.java b/src/main/java/org/shedlang/compiler/tokeniser/UnexpectedTokenException.java
index 75fa19dc..714edf6b 100644
--- a/src/main/java/org/shedlang/compiler/tokeniser/UnexpectedTokenException.java
+++ b/src/main/java/org/shedlang/compiler/tokeniser/UnexpectedTokenException.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.tokeniser;
import org.shedlang.compiler.errors.SourceError;
+import org.shedlang.compiler.sources.FileFragmentSource;
public class UnexpectedTokenException extends SourceError {
private final String expected;
@@ -12,7 +13,7 @@ public class UnexpectedTokenException extends SourceError {
) {
super(
"Expected: " + expected + "\nBut got: " + actual.tokenType() + " \"" + actual.value() + "\"",
- actual.source()
+ new FileFragmentSource(actual.sourceRange())
);
this.expected = expected;
this.actual = actual;