refactoring and reducing complexity
This commit is contained in:
+1
-1
@@ -233,7 +233,7 @@ public final class PbsReservedMetadataExtractor {
|
||||
return normalizedTypeName + "." + normalizedIntrinsicName;
|
||||
}
|
||||
|
||||
private String typeSurfaceKey(final PbsAst.TypeRef typeRef) {
|
||||
public static String typeSurfaceKey(final PbsAst.TypeRef typeRef) {
|
||||
if (typeRef == null) {
|
||||
return "unit";
|
||||
}
|
||||
|
||||
+21
-39
@@ -5,8 +5,8 @@ import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.diagnostics.RelatedSpan;
|
||||
import p.studio.compiler.source.diagnostics.Severity;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.CallableShapeId;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.NameId;
|
||||
import p.studio.compiler.source.identifiers.TypeSurfaceId;
|
||||
import p.studio.compiler.source.tables.CallableShapeTable;
|
||||
@@ -14,12 +14,9 @@ import p.studio.compiler.source.tables.NameTable;
|
||||
import p.studio.compiler.source.tables.TypeSurfaceTable;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
import static p.studio.compiler.pbs.PbsReservedMetadataExtractor.typeSurfaceKey;
|
||||
|
||||
public final class PbsModuleVisibilityValidator {
|
||||
|
||||
@@ -91,23 +88,26 @@ public final class PbsModuleVisibilityValidator {
|
||||
final Map<FunctionSymbolKey, Span> seenFunctionEntries = new HashMap<>();
|
||||
|
||||
for (final var item : barrel.ast().items()) {
|
||||
if (item instanceof PbsAst.BarrelFunctionItem functionItem) {
|
||||
if (item instanceof PbsAst.BarrelFunctionItem(
|
||||
PbsAst.Visibility visibility, String name, ReadOnlyList<PbsAst.TypeRef> parameterTypes,
|
||||
PbsAst.ReturnKind returnKind, PbsAst.TypeRef returnType, PbsAst.TypeRef resultErrorType, Span span
|
||||
)) {
|
||||
final var functionKey = functionKey(
|
||||
functionItem.name(),
|
||||
functionItem.parameterTypes().asList(),
|
||||
functionItem.returnKind(),
|
||||
functionItem.returnType(),
|
||||
functionItem.resultErrorType(),
|
||||
name,
|
||||
parameterTypes.asList(),
|
||||
returnKind,
|
||||
returnType,
|
||||
resultErrorType,
|
||||
nameTable,
|
||||
signatureInterning);
|
||||
final var firstFunctionEntry = seenFunctionEntries.putIfAbsent(functionKey, functionItem.span());
|
||||
final var firstFunctionEntry = seenFunctionEntries.putIfAbsent(functionKey, span);
|
||||
if (firstFunctionEntry != null) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
PbsLinkErrors.E_LINK_DUPLICATE_BARREL_ENTRY.name(),
|
||||
"Duplicate barrel function entry '%s' in module %s".formatted(
|
||||
functionItem.name(),
|
||||
name,
|
||||
displayModule(module.coordinates())),
|
||||
functionItem.span(),
|
||||
span,
|
||||
List.of(new RelatedSpan("First barrel function entry is here", firstFunctionEntry)));
|
||||
continue;
|
||||
}
|
||||
@@ -117,23 +117,23 @@ public final class PbsModuleVisibilityValidator {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
PbsLinkErrors.E_LINK_UNRESOLVED_BARREL_ENTRY.name(),
|
||||
"Barrel function entry '%s' in module %s does not resolve to a declaration".formatted(
|
||||
functionItem.name(),
|
||||
name,
|
||||
displayModule(module.coordinates())),
|
||||
functionItem.span());
|
||||
span);
|
||||
continue;
|
||||
}
|
||||
if (matches.size() > 1) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
PbsLinkErrors.E_LINK_AMBIGUOUS_BARREL_ENTRY.name(),
|
||||
"Barrel function entry '%s' in module %s resolves ambiguously".formatted(
|
||||
functionItem.name(),
|
||||
name,
|
||||
displayModule(module.coordinates())),
|
||||
functionItem.span(),
|
||||
span,
|
||||
List.of(new RelatedSpan("One matching function declaration is here", matches.getFirst())));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (functionItem.visibility() == PbsAst.Visibility.PUB) {
|
||||
if (visibility == PbsAst.Visibility.PUB) {
|
||||
exports.publicNameIds.add(functionKey.nameId());
|
||||
}
|
||||
continue;
|
||||
@@ -337,24 +337,6 @@ public final class PbsModuleVisibilityValidator {
|
||||
};
|
||||
}
|
||||
|
||||
private String typeSurfaceKey(final PbsAst.TypeRef typeRef) {
|
||||
if (typeRef == null) {
|
||||
return "unit";
|
||||
}
|
||||
return switch (typeRef.kind()) {
|
||||
case SIMPLE -> "simple:" + typeRef.name();
|
||||
case SELF -> "self";
|
||||
case OPTIONAL -> "optional(" + typeSurfaceKey(typeRef.inner()) + ")";
|
||||
case UNIT -> "unit";
|
||||
case GROUP -> "group(" + typeSurfaceKey(typeRef.inner()) + ")";
|
||||
case NAMED_TUPLE -> "tuple(" + typeRef.fields().stream()
|
||||
.map(field -> typeSurfaceKey(field.typeRef()))
|
||||
.reduce((left, right) -> left + "," + right)
|
||||
.orElse("") + ")";
|
||||
case ERROR -> "error";
|
||||
};
|
||||
}
|
||||
|
||||
private NonFunctionKind nonFunctionKind(final PbsAst.BarrelItem item) {
|
||||
if (item instanceof PbsAst.BarrelStructItem) {
|
||||
return NonFunctionKind.STRUCT;
|
||||
|
||||
+18
-17
@@ -22,6 +22,8 @@ import java.util.List;
|
||||
*/
|
||||
public final class PbsParser {
|
||||
private static final int MAX_NAMED_TUPLE_ARITY = 6;
|
||||
private static final boolean REQUIRE_SEMICOLON = true;
|
||||
private static final boolean ALLOW_TAIL_EXPRESSION = true;
|
||||
|
||||
public enum ParseMode {
|
||||
ORDINARY,
|
||||
@@ -230,7 +232,7 @@ public final class PbsParser {
|
||||
}
|
||||
if (cursor.match(PbsTokenKind.HOST)) {
|
||||
rejectAttributesBeforeUnsupportedTopDecl(pendingAttributes, "host declarations");
|
||||
if (!isInterfaceMode()) {
|
||||
if (isNotInterfaceMode()) {
|
||||
final var end = consumeDeclarationTerminator();
|
||||
report(cursor.previous(), ParseErrors.E_PARSE_RESERVED_DECLARATION,
|
||||
"'declare host' is reserved and not supported in ordinary source modules");
|
||||
@@ -240,7 +242,7 @@ public final class PbsParser {
|
||||
}
|
||||
if (cursor.match(PbsTokenKind.BUILTIN)) {
|
||||
consume(PbsTokenKind.TYPE, "Expected 'type' in 'declare builtin type' declaration");
|
||||
if (!isInterfaceMode()) {
|
||||
if (isNotInterfaceMode()) {
|
||||
final var end = consumeDeclarationTerminator();
|
||||
report(cursor.previous(), ParseErrors.E_PARSE_RESERVED_DECLARATION,
|
||||
"'declare builtin type' is reserved and not supported in ordinary source modules");
|
||||
@@ -275,7 +277,7 @@ public final class PbsParser {
|
||||
cursor.advance();
|
||||
continue;
|
||||
}
|
||||
signatures.add(parseFunctionSignature(cursor.previous(), true, attributes));
|
||||
signatures.add(parseFunctionSignature(cursor.previous(), attributes));
|
||||
}
|
||||
final var rightBrace = consume(PbsTokenKind.RIGHT_BRACE, "Expected '}' to end host declaration body");
|
||||
return new PbsAst.HostDecl(
|
||||
@@ -305,7 +307,7 @@ public final class PbsParser {
|
||||
cursor.advance();
|
||||
continue;
|
||||
}
|
||||
signatures.add(parseFunctionSignature(cursor.previous(), true, attributes));
|
||||
signatures.add(parseFunctionSignature(cursor.previous(), attributes));
|
||||
}
|
||||
final var rightBrace = consume(PbsTokenKind.RIGHT_BRACE, "Expected '}' to end builtin type body");
|
||||
return new PbsAst.BuiltinTypeDecl(
|
||||
@@ -379,8 +381,8 @@ public final class PbsParser {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInterfaceMode() {
|
||||
return parseMode == ParseMode.INTERFACE_MODULE;
|
||||
private boolean isNotInterfaceMode() {
|
||||
return parseMode != ParseMode.INTERFACE_MODULE;
|
||||
}
|
||||
|
||||
private boolean isOrdinaryMode() {
|
||||
@@ -627,7 +629,7 @@ public final class PbsParser {
|
||||
cursor.advance();
|
||||
continue;
|
||||
}
|
||||
signatures.add(parseFunctionSignature(cursor.previous(), true, attributes));
|
||||
signatures.add(parseFunctionSignature(cursor.previous(), attributes));
|
||||
}
|
||||
final var rightBrace = consume(PbsTokenKind.RIGHT_BRACE, "Expected '}' to end contract body");
|
||||
return new PbsAst.ContractDecl(name.lexeme(), ReadOnlyList.wrap(signatures), span(declareToken.start(), rightBrace.end()));
|
||||
@@ -730,7 +732,7 @@ public final class PbsParser {
|
||||
}
|
||||
|
||||
private PbsAst.FunctionDecl parseFunctionLike(final PbsToken fnToken) {
|
||||
final var name = consumeCallableName("Expected function name");
|
||||
final var name = consumeCallableName();
|
||||
consume(PbsTokenKind.LEFT_PAREN, "Expected '(' after function name");
|
||||
final var parameters = parseParametersUntilRightParen();
|
||||
final var rightParen = consume(PbsTokenKind.RIGHT_PAREN, "Expected ')' after parameter list");
|
||||
@@ -750,9 +752,8 @@ public final class PbsParser {
|
||||
|
||||
private PbsAst.FunctionSignature parseFunctionSignature(
|
||||
final PbsToken fnToken,
|
||||
final boolean requireSemicolon,
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes) {
|
||||
final var name = consumeCallableName("Expected function name");
|
||||
final var name = consumeCallableName();
|
||||
consume(PbsTokenKind.LEFT_PAREN, "Expected '(' after function name");
|
||||
final var parameters = parseParametersUntilRightParen();
|
||||
final var rightParen = consume(PbsTokenKind.RIGHT_PAREN, "Expected ')' after parameter list");
|
||||
@@ -760,7 +761,7 @@ public final class PbsParser {
|
||||
final var returnSpec = parseCallableReturnSpec(rightParen);
|
||||
|
||||
long end = returnSpec.returnType.span().getEnd();
|
||||
if (requireSemicolon) {
|
||||
if (REQUIRE_SEMICOLON) {
|
||||
end = consume(PbsTokenKind.SEMICOLON, "Expected ';' after function signature").end();
|
||||
}
|
||||
|
||||
@@ -774,12 +775,12 @@ public final class PbsParser {
|
||||
span(fnToken.start(), end));
|
||||
}
|
||||
|
||||
private PbsToken consumeCallableName(final String message) {
|
||||
private PbsToken consumeCallableName() {
|
||||
if (cursor.check(PbsTokenKind.IDENTIFIER) || cursor.check(PbsTokenKind.ERROR)) {
|
||||
return cursor.advance();
|
||||
}
|
||||
final var token = cursor.peek();
|
||||
report(token, ParseErrors.E_PARSE_EXPECTED_TOKEN, message + ", found " + token.kind());
|
||||
report(token, ParseErrors.E_PARSE_EXPECTED_TOKEN, "Expected a function name, but found " + token.kind());
|
||||
if (!cursor.isAtEnd()) {
|
||||
return cursor.advance();
|
||||
}
|
||||
@@ -1018,14 +1019,14 @@ public final class PbsParser {
|
||||
* Parses a brace-delimited block.
|
||||
*/
|
||||
private PbsAst.Block parseBlock() {
|
||||
return parseBlock("Expected '{' to start block", true);
|
||||
return parseBlock("Expected '{' to start block");
|
||||
}
|
||||
|
||||
private PbsAst.Block parseExpressionSurfaceBlock(final String message) {
|
||||
return parseBlock(message, true);
|
||||
return parseBlock(message);
|
||||
}
|
||||
|
||||
private PbsAst.Block parseBlock(final String message, final boolean allowTailExpression) {
|
||||
private PbsAst.Block parseBlock(final String message) {
|
||||
final var leftBrace = consume(PbsTokenKind.LEFT_BRACE, message);
|
||||
final var statements = new ArrayList<PbsAst.Statement>();
|
||||
PbsAst.Expression tailExpression = null;
|
||||
@@ -1043,7 +1044,7 @@ public final class PbsParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (allowTailExpression && cursor.check(PbsTokenKind.RIGHT_BRACE)) {
|
||||
if (ALLOW_TAIL_EXPRESSION && cursor.check(PbsTokenKind.RIGHT_BRACE)) {
|
||||
tailExpression = expression;
|
||||
break;
|
||||
}
|
||||
|
||||
-4
@@ -31,10 +31,6 @@ public final class PbsDeclarationSemanticsValidator {
|
||||
private final NameTable nameTable;
|
||||
private final PbsConstSemanticsValidator constSemanticsValidator = new PbsConstSemanticsValidator();
|
||||
|
||||
public PbsDeclarationSemanticsValidator() {
|
||||
this(new NameTable());
|
||||
}
|
||||
|
||||
public PbsDeclarationSemanticsValidator(final NameTable nameTable) {
|
||||
this.nameTable = nameTable == null ? new NameTable() : nameTable;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user