implements PR006.3

This commit is contained in:
2026-03-24 13:42:20 +00:00
parent 42392f3d02
commit e94f76db56
13 changed files with 232 additions and 51 deletions
@@ -3,4 +3,5 @@ package p.studio.compiler.pbs.lexer;
public enum LexErrors {
E_LEX_INVALID_CHAR,
E_LEX_UNTERMINATED_STRING,
E_LEX_INVALID_STRING_ESCAPE,
}
@@ -173,10 +173,18 @@ public final class PbsLexer {
private void scanStringState() {
while (!isAtEnd() && peek() != '"') {
if (peek() == '\\' && !isAtEnd()) {
if (peek() == '\\') {
final var escapeStart = current;
advance();
if (!isAtEnd()) {
advance();
final var escaped = advance();
if (!isValidStringEscape(escaped)) {
report(
LexErrors.E_LEX_INVALID_STRING_ESCAPE,
"Invalid string escape '\\%s'".formatted(escaped),
escapeStart,
current);
}
}
continue;
}
@@ -245,8 +253,24 @@ public final class PbsLexer {
return c == '_' || Character.isAlphabetic(c) || Character.isDigit(c);
}
private boolean isValidStringEscape(final char escaped) {
return escaped == '\\'
|| escaped == '"'
|| escaped == 'n'
|| escaped == 'r'
|| escaped == 't';
}
private void report(final LexErrors lexErrors, final String message) {
diagnostics.error(lexErrors.name(), message, new Span(fileId, start, current));
report(lexErrors, message, start, current);
}
private void report(
final LexErrors lexErrors,
final String message,
final long spanStart,
final long spanEnd) {
diagnostics.error(lexErrors.name(), message, new Span(fileId, spanStart, spanEnd));
}
private static Map<String, PbsTokenKind> buildKeywords() {
@@ -2,6 +2,7 @@ package p.studio.compiler.pbs.linking;
public enum PbsLinkErrors {
E_LINK_MISSING_BARREL,
E_LINK_BARREL_WITHOUT_SOURCE,
E_LINK_INVALID_BARREL_FILENAME,
E_LINK_DUPLICATE_BARREL_FILE,
E_LINK_DUPLICATE_BARREL_ENTRY,
@@ -42,6 +42,12 @@ public final class PbsModuleVisibilityValidator {
final var exports = new ModuleExports();
if (module.sourceFiles().isEmpty()) {
if (!module.barrelFiles().isEmpty()) {
diagnostics.error(
PbsLinkErrors.E_LINK_BARREL_WITHOUT_SOURCE.name(),
"Module %s has mod.barrel but no .pbs source files".formatted(displayModule(module.coordinates())),
module.barrelFiles().getFirst().ast().span());
}
return exports;
}
@@ -19,4 +19,5 @@ public enum ParseErrors {
E_PARSE_INVALID_HANDLE_FORM,
E_PARSE_INVALID_TUPLE_LITERAL,
E_PARSE_INVALID_PROPAGATE_OPERATOR,
E_PARSE_ATTRIBUTES_NOT_ALLOWED,
}
@@ -14,6 +14,8 @@ import java.util.ArrayList;
* Parser for `mod.barrel` files.
*/
public final class PbsBarrelParser {
private static final int MAX_NAMED_TUPLE_ARITY = 6;
private final PbsTokenCursor cursor;
private final FileId fileId;
private final DiagnosticSink diagnostics;
@@ -242,6 +244,10 @@ public final class PbsBarrelParser {
consume(PbsTokenKind.COLON, "Expected ':' after tuple field label");
final var type = parseTypeRef();
fields.add(new PbsAst.NamedTypeField(label.lexeme(), type, span(label.start(), type.span().getEnd())));
if (fields.size() == MAX_NAMED_TUPLE_ARITY + 1) {
report(label, ParseErrors.E_PARSE_INVALID_TYPE_SURFACE,
"Named tuple type arity must be between 1 and 6 fields");
}
} while (cursor.match(PbsTokenKind.COMMA) && !cursor.check(PbsTokenKind.RIGHT_PAREN));
final var close = consume(PbsTokenKind.RIGHT_PAREN, "Expected ')' after named tuple type");
@@ -14,6 +14,8 @@ import java.util.ArrayList;
* Dedicated expression parser for PBS.
*/
final class PbsExprParser {
private static final int MAX_TUPLE_LITERAL_ARITY = 6;
@FunctionalInterface
interface BlockParserDelegate {
PbsAst.Block parse(String message);
@@ -388,6 +390,10 @@ final class PbsExprParser {
report(close, ParseErrors.E_PARSE_INVALID_TUPLE_LITERAL,
"Single-slot tuple literal is not allowed in PBS core syntax");
}
if (items.size() > MAX_TUPLE_LITERAL_ARITY) {
report(close, ParseErrors.E_PARSE_INVALID_TUPLE_LITERAL,
"Tuple literal arity must be between 2 and 6 items");
}
return new PbsAst.TupleExpr(ReadOnlyList.wrap(items), span(open.start(), close.end()));
}
@@ -417,6 +423,10 @@ final class PbsExprParser {
}
final var close = consume(PbsTokenKind.RIGHT_PAREN, "Expected ')' after tuple literal");
if (items.size() > MAX_TUPLE_LITERAL_ARITY) {
report(close, ParseErrors.E_PARSE_INVALID_TUPLE_LITERAL,
"Tuple literal arity must be between 2 and 6 items");
}
if (hasLabels) {
for (final var item : items) {
if (item.label() == null) {
@@ -666,7 +676,7 @@ final class PbsExprParser {
case 't' -> sb.append('\t');
case '"' -> sb.append('"');
case '\\' -> sb.append('\\');
default -> sb.append(next);
default -> sb.append('\\').append(next);
}
}
return sb.toString();
@@ -19,6 +19,8 @@ import java.util.HashSet;
* navigation is delegated to {@link PbsTokenCursor}.
*/
public final class PbsParser {
private static final int MAX_NAMED_TUPLE_ARITY = 6;
private final PbsTokenCursor cursor;
private final PbsExprParser exprParser;
private final FileId fileId;
@@ -53,6 +55,10 @@ public final class PbsParser {
final var topDecls = new ArrayList<PbsAst.TopDecl>();
while (!cursor.isAtEnd()) {
if (cursor.check(PbsTokenKind.LEFT_BRACKET)) {
parseRejectedAttributeList();
}
if (cursor.match(PbsTokenKind.IMPORT)) {
imports.add(parseImport(cursor.previous()));
continue;
@@ -265,11 +271,98 @@ public final class PbsParser {
isPublic,
isMutable,
span(start.start(), typeRef.span().getEnd())));
} while (cursor.match(PbsTokenKind.COMMA));
} while (cursor.match(PbsTokenKind.COMMA) && !cursor.check(PbsTokenKind.RIGHT_PAREN));
return fields;
}
private void parseRejectedAttributeList() {
while (cursor.check(PbsTokenKind.LEFT_BRACKET)) {
final var attributeSpan = parseAttribute();
diagnostics.error(
ParseErrors.E_PARSE_ATTRIBUTES_NOT_ALLOWED.name(),
"Attributes are not allowed in ordinary .pbs source modules",
attributeSpan);
}
}
private Span parseAttribute() {
final var leftBracket = consume(PbsTokenKind.LEFT_BRACKET, "Expected '[' to start attribute");
final var name = consume(PbsTokenKind.IDENTIFIER, "Expected attribute identifier");
if (cursor.match(PbsTokenKind.LEFT_PAREN)) {
parseAttributeArguments();
}
long end = Math.max(leftBracket.end(), name.end());
if (cursor.match(PbsTokenKind.RIGHT_BRACKET)) {
end = cursor.previous().end();
} else {
report(cursor.peek(), ParseErrors.E_PARSE_EXPECTED_TOKEN, "Expected ']' to close attribute");
end = recoverUntilAttributeCloseOrTopLevel(end);
}
return span(leftBracket.start(), end);
}
private void parseAttributeArguments() {
if (!cursor.check(PbsTokenKind.RIGHT_PAREN)) {
do {
parseAttributeArgument();
} while (cursor.match(PbsTokenKind.COMMA) && !cursor.check(PbsTokenKind.RIGHT_PAREN));
}
if (cursor.match(PbsTokenKind.RIGHT_PAREN)) {
return;
}
report(cursor.peek(), ParseErrors.E_PARSE_EXPECTED_TOKEN, "Expected ')' after attribute arguments");
recoverUntilAttributeArgumentClose();
}
private void parseAttributeArgument() {
consume(PbsTokenKind.IDENTIFIER, "Expected attribute argument name");
consume(PbsTokenKind.EQUAL, "Expected '=' in attribute argument");
parseAttributeValue();
}
private void parseAttributeValue() {
if (cursor.match(PbsTokenKind.STRING_LITERAL, PbsTokenKind.INT_LITERAL, PbsTokenKind.TRUE, PbsTokenKind.FALSE)) {
return;
}
report(cursor.peek(), ParseErrors.E_PARSE_EXPECTED_TOKEN, "Expected attribute value (string, int, or bool)");
if (!cursor.isAtEnd()
&& !cursor.check(PbsTokenKind.COMMA)
&& !cursor.check(PbsTokenKind.RIGHT_PAREN)
&& !cursor.check(PbsTokenKind.RIGHT_BRACKET)) {
cursor.advance();
}
}
private long recoverUntilAttributeCloseOrTopLevel(final long fallbackEnd) {
long end = fallbackEnd;
while (!cursor.isAtEnd()) {
if (cursor.match(PbsTokenKind.RIGHT_BRACKET)) {
return cursor.previous().end();
}
if (isTopLevelRestartToken(cursor.peek().kind())) {
return end;
}
end = cursor.advance().end();
}
return end;
}
private void recoverUntilAttributeArgumentClose() {
while (!cursor.isAtEnd()) {
if (cursor.match(PbsTokenKind.RIGHT_PAREN)) {
return;
}
if (cursor.check(PbsTokenKind.RIGHT_BRACKET) || isTopLevelRestartToken(cursor.peek().kind())) {
return;
}
cursor.advance();
}
}
private StructBodyParse parseStructBodyAndConsumeRightBrace(final PbsToken leftBrace) {
final var methods = new ArrayList<PbsAst.FunctionDecl>();
final var ctors = new ArrayList<PbsAst.CtorDecl>();
@@ -654,6 +747,10 @@ public final class PbsParser {
label.lexeme(),
typeRef,
span(label.start(), typeRef.span().getEnd())));
if (fields.size() == MAX_NAMED_TUPLE_ARITY + 1) {
report(label, ParseErrors.E_PARSE_INVALID_TYPE_SURFACE,
"Named tuple type arity must be between 1 and 6 fields");
}
} while (cursor.match(PbsTokenKind.COMMA) && !cursor.check(PbsTokenKind.RIGHT_PAREN));
return fields;
}
@@ -1013,10 +1110,7 @@ public final class PbsParser {
*/
private void synchronizeTopLevel() {
while (!cursor.isAtEnd()) {
if (cursor.check(PbsTokenKind.FN)
|| cursor.check(PbsTokenKind.IMPORT)
|| cursor.check(PbsTokenKind.DECLARE)
|| cursor.check(PbsTokenKind.IMPLEMENTS)) {
if (isTopLevelRestartToken(cursor.peek().kind())) {
return;
}
if (cursor.match(PbsTokenKind.SEMICOLON)) {
@@ -1026,6 +1120,14 @@ public final class PbsParser {
}
}
private boolean isTopLevelRestartToken(final PbsTokenKind kind) {
return kind == PbsTokenKind.FN
|| kind == PbsTokenKind.IMPORT
|| kind == PbsTokenKind.DECLARE
|| kind == PbsTokenKind.IMPLEMENTS
|| kind == PbsTokenKind.LEFT_BRACKET;
}
/**
* Consumes a required token and reports an error if it is missing.
*/