From 3666a4af2eb35177eecd557d2a75f4bac9da75b9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 8 Jan 2025 19:14:03 +0000 Subject: Use source text to implement FileFragmentSource --- .../tokeniser/InfinitelyPaddedIterator.java | 4 ++++ .../compiler/tokeniser/RegexTokeniser.java | 13 ++++++++----- .../org/shedlang/compiler/tokeniser/Token.java | 8 ++++---- .../shedlang/compiler/tokeniser/TokenIterator.java | 22 +++++++++++++++++----- .../tokeniser/UnexpectedTokenException.java | 3 ++- 5 files changed, 35 insertions(+), 15 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/tokeniser') 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 { 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 { .collect(Collectors.toList()); } - public List> tokenise(Source source) { - var matcher = pattern.matcher(source.contents()); + public List> tokenise(SourceText sourceText) { + var matcher = pattern.matcher(sourceText.charSequence()); var tokens = new ArrayList>(); while (matcher.lookingAt()) { @@ -58,12 +58,15 @@ public class RegexTokeniser { .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(Source source, T tokenType, String value) { - public static Token token(Source source, T tokenType, String value) { - return new Token(source, tokenType, value); +public record Token(SourceRange sourceRange, T tokenType, String value) { + public static Token token(SourceRange sourceRange, T tokenType, String value) { + return new Token(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 { nextValue(tokenType); } - public String nextValue(TTokenType tokenType) { + public Token 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 { } } + 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 { 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; -- cgit v1.2.3