implements PLN-0093

This commit is contained in:
2026-07-15 06:57:19 +01:00
parent 917b0e1b9a
commit 2b25364252
7 changed files with 93 additions and 4 deletions
@@ -50,7 +50,7 @@ public enum PbsSemanticKind {
public static PbsSemanticKind forToken(final PbsToken token) {
return switch (token.kind()) {
case COMMENT -> COMMENT;
case STRING_LITERAL -> STRING;
case STRING_LITERAL, DOC_TEXT_BLOCK -> STRING;
case INT_LITERAL, FLOAT_LITERAL, BOUNDED_LITERAL -> NUMBER;
case TRUE, FALSE, NONE, SOME, OK, ERR -> LITERAL;
case VOID, OPTIONAL, RESULT -> BUILTIN_TYPE;
@@ -3,5 +3,6 @@ package p.studio.compiler.pbs.lexer;
public enum LexErrors {
E_LEX_INVALID_CHAR,
E_LEX_UNTERMINATED_STRING,
E_LEX_UNTERMINATED_DOC_TEXT_BLOCK,
E_LEX_INVALID_STRING_ESCAPE,
}
@@ -17,6 +17,7 @@ public final class PbsLexer {
IDENTIFIER,
NUMBER,
STRING,
DOC_TEXT_BLOCK,
LINE_COMMENT
}
@@ -52,6 +53,7 @@ public final class PbsLexer {
case IDENTIFIER -> scanIdentifierState();
case NUMBER -> scanNumberState();
case STRING -> scanStringState();
case DOC_TEXT_BLOCK -> scanDocTextBlockState();
case LINE_COMMENT -> scanLineCommentState();
}
}
@@ -123,7 +125,15 @@ public final class PbsLexer {
}
addToken(match('=') ? PbsTokenKind.SLASH_EQUAL : PbsTokenKind.SLASH);
}
case '"' -> state = LexerState.STRING;
case '"' -> {
if (peek() == '"' && peekNext() == '"') {
advance();
advance();
state = LexerState.DOC_TEXT_BLOCK;
return;
}
state = LexerState.STRING;
}
default -> {
if (isDigit(c)) {
state = LexerState.NUMBER;
@@ -206,6 +216,23 @@ public final class PbsLexer {
state = LexerState.DEFAULT;
}
private void scanDocTextBlockState() {
while (!isAtEnd()) {
if (peek() == '"' && peekNext() == '"' && peekAfterNext() == '"') {
advance();
advance();
advance();
addToken(PbsTokenKind.DOC_TEXT_BLOCK);
state = LexerState.DEFAULT;
return;
}
advance();
}
report(LexErrors.E_LEX_UNTERMINATED_DOC_TEXT_BLOCK, "Unterminated documentation text block");
state = LexerState.DEFAULT;
}
private void scanLineCommentState() {
while (!isAtEnd() && peek() != '\n') {
advance();
@@ -250,6 +277,17 @@ public final class PbsLexer {
return source.codePointAt(nextIndex);
}
private int peekAfterNext() {
if (isAtEnd()) return '\0';
final int first = source.codePointAt(current);
final int secondIndex = current + Character.charCount(first);
if (secondIndex >= source.length()) return '\0';
final int second = source.codePointAt(secondIndex);
final int thirdIndex = secondIndex + Character.charCount(second);
if (thirdIndex >= source.length()) return '\0';
return source.codePointAt(thirdIndex);
}
private boolean isAtEnd() {
return current >= source.length();
}
@@ -17,6 +17,7 @@ public enum PbsTokenKind {
FLOAT_LITERAL,
BOUNDED_LITERAL,
STRING_LITERAL,
DOC_TEXT_BLOCK,
COMMENT,
// Import keywords.