implements PR-10.5
This commit is contained in:
parent
49fdcb58ca
commit
3dca7396bb
@ -3,21 +3,13 @@ package p.studio.compiler.pbs.parser;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsToken;
|
||||
import p.studio.compiler.pbs.lexer.PbsTokenKind;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* High-level manual parser for PBS source files.
|
||||
*
|
||||
* <p>This class focuses on declarations, blocks, statements, and error recovery.
|
||||
* Expression precedence is delegated to {@link PbsExprParser}, while raw token
|
||||
* navigation is delegated to {@link PbsTokenCursor}.
|
||||
* High-level manual parser facade for PBS source files.
|
||||
*/
|
||||
public final class PbsParser {
|
||||
private static final boolean REQUIRE_SEMICOLON = true;
|
||||
public enum ParseMode {
|
||||
ORDINARY,
|
||||
INTERFACE_MODULE
|
||||
@ -25,10 +17,6 @@ public final class PbsParser {
|
||||
|
||||
private final PbsParserContext context;
|
||||
private final PbsTokenCursor cursor;
|
||||
private final PbsExprParser exprParser;
|
||||
private final PbsAttributeParser attributeParser;
|
||||
private final PbsTypeParser typeParser;
|
||||
private final PbsDeclarationParser declarationParser;
|
||||
private final PbsTopLevelParser topLevelParser;
|
||||
private final PbsBlockParser blockParser;
|
||||
|
||||
@ -39,11 +27,14 @@ public final class PbsParser {
|
||||
final ParseMode parseMode) {
|
||||
this.context = new PbsParserContext(new PbsTokenCursor(tokens), fileId, diagnostics, parseMode);
|
||||
this.cursor = context.cursor();
|
||||
this.exprParser = new PbsExprParser(context, this::parseExpressionSurfaceBlock);
|
||||
this.attributeParser = new PbsAttributeParser(context, this::isTopLevelRestartToken);
|
||||
this.typeParser = new PbsTypeParser(context);
|
||||
|
||||
final var exprParser = new PbsExprParser(context, this::parseExpressionSurfaceBlock);
|
||||
final var attributeParser = new PbsAttributeParser(context, this::isTopLevelRestartToken);
|
||||
final var typeParser = new PbsTypeParser(context);
|
||||
|
||||
this.blockParser = new PbsBlockParser(context, exprParser, typeParser);
|
||||
this.declarationParser = new PbsDeclarationParser(
|
||||
|
||||
final var declarationParser = new PbsDeclarationParser(
|
||||
context,
|
||||
exprParser,
|
||||
attributeParser,
|
||||
@ -54,9 +45,6 @@ public final class PbsParser {
|
||||
this.topLevelParser = new PbsTopLevelParser(context, attributeParser, declarationParser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a token stream into a PBS file AST.
|
||||
*/
|
||||
public static PbsAst.File parse(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final FileId fileId,
|
||||
@ -72,253 +60,14 @@ public final class PbsParser {
|
||||
return new PbsParser(tokens, fileId, diagnostics, parseMode).parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a full file as a sequence of imports and top-level declarations.
|
||||
*/
|
||||
private PbsAst.File parseFile() {
|
||||
return topLevelParser.parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses import syntax and stores it in the AST.
|
||||
*/
|
||||
private PbsAst.ImportDecl parseImport(final PbsToken importToken) {
|
||||
return declarationParser.parseImport(importToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a module reference such as {@code @core:math/tools}.
|
||||
*/
|
||||
private PbsAst.ModuleRef parseModuleRef() {
|
||||
return declarationParser.parseModuleRef();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses declarations introduced by 'declare'.
|
||||
*/
|
||||
private PbsAst.TopDecl parseDeclareTopDecl(
|
||||
final PbsToken declareToken,
|
||||
final ReadOnlyList<PbsAst.Attribute> pendingAttributes) {
|
||||
return declarationParser.parseDeclareTopDecl(declareToken, pendingAttributes);
|
||||
}
|
||||
|
||||
private PbsAst.HostDecl parseHostDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.BuiltinTypeDecl parseBuiltinTypeDeclaration(
|
||||
final PbsToken declareToken,
|
||||
final ReadOnlyList<PbsAst.Attribute> declarationAttributes) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private ArrayList<PbsAst.BuiltinFieldDecl> parseBuiltinTypeFields() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private void rejectAttributesBeforeUnsupportedTopDecl(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final String targetSurface) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private void reportAttributesNotAllowed(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final String message) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private boolean isNotInterfaceMode() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private boolean isOrdinaryMode() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.StructDecl parseStructDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private ArrayList<PbsAst.StructField> parseStructFields() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private ReadOnlyList<PbsAst.Attribute> parseAttributeList() {
|
||||
return attributeParser.parseAttributeList();
|
||||
}
|
||||
|
||||
private StructBodyParse parseStructBodyAndConsumeRightBrace(final PbsToken leftBrace) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.CtorDecl parseCtorDeclarationInBody(final PbsToken ctorToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.ContractDecl parseContractDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.ServiceDecl parseServiceDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.ErrorDecl parseErrorDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.EnumDecl parseEnumDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a top-level function declaration.
|
||||
*/
|
||||
private PbsAst.FunctionDecl parseFunction(final PbsToken fnToken) {
|
||||
return declarationParser.parseFunction(fnToken);
|
||||
}
|
||||
|
||||
private PbsAst.FunctionDecl parseFunctionLike(final PbsToken fnToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.FunctionSignature parseFunctionSignature(
|
||||
final PbsToken fnToken,
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsToken consumeCallableName() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses `declare callback` declaration.
|
||||
*/
|
||||
private PbsAst.CallbackDecl parseCallbackDeclaration(final PbsToken declareToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses `declare const` declaration.
|
||||
*/
|
||||
private PbsAst.ConstDecl parseConstDeclaration(
|
||||
final PbsToken declareToken,
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses `implements Contract for Owner using binder { ... }`.
|
||||
*/
|
||||
private PbsAst.ImplementsDecl parseImplementsDeclaration(final PbsToken implementsToken) {
|
||||
return declarationParser.parseImplementsDeclaration(implementsToken);
|
||||
}
|
||||
|
||||
private ArrayList<PbsAst.Parameter> parseParametersUntilRightParen() {
|
||||
return typeParser.parseParametersUntilRightParen();
|
||||
}
|
||||
|
||||
private PbsTypeParser.ParsedReturnSpec parseCallableReturnSpec(final PbsToken anchorToken) {
|
||||
return typeParser.parseCallableReturnSpec(anchorToken);
|
||||
}
|
||||
|
||||
private PbsAst.TypeRef parseTypeRef() {
|
||||
return typeParser.parseTypeRef();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a brace-delimited block.
|
||||
*/
|
||||
private PbsAst.Block parseBlock() {
|
||||
return blockParser.parseBlock();
|
||||
}
|
||||
|
||||
private PbsAst.Block parseExpressionSurfaceBlock(final String message) {
|
||||
return blockParser.parseBlock(message);
|
||||
}
|
||||
|
||||
private PbsAst.Block parseBlock(final String message) {
|
||||
return blockParser.parseBlock(message);
|
||||
}
|
||||
|
||||
private boolean startsStructuredStatement() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses one statement inside a block.
|
||||
*/
|
||||
private PbsAst.Statement parseStatement() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a local binding statement.
|
||||
*/
|
||||
private PbsAst.Statement parseLetStatement(final PbsToken letToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseAssignStatement() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.LValue parseLValue() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsToken consumeAssignOperator() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.AssignOperator parseAssignOperator(final PbsToken token) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.IfStatement parseIfStatement(final PbsToken ifToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseForStatement(final PbsToken forToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsToken consumeForToken(final PbsTokenKind kind, final String message) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseWhileStatement(final PbsToken whileToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseBreakStatement(final PbsToken breakToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private PbsAst.Statement parseContinueStatement(final PbsToken continueToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a return statement with an optional returned value.
|
||||
*/
|
||||
private PbsAst.Statement parseReturnStatement(final PbsToken returnToken) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an expression statement terminated by a semicolon.
|
||||
*/
|
||||
private PbsAst.Statement parseExpressionStatement() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private boolean isAssignmentStatementStart() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private long consumeDeclarationTerminator() {
|
||||
var end = cursor.previous().end();
|
||||
var parenDepth = 0;
|
||||
@ -355,14 +104,11 @@ public final class PbsParser {
|
||||
}
|
||||
}
|
||||
if (depth > 0) {
|
||||
report(cursor.peek(), ParseErrors.E_PARSE_EXPECTED_TOKEN, "Expected '}' to close declaration body");
|
||||
context.report(cursor.peek(), ParseErrors.E_PARSE_EXPECTED_TOKEN, "Expected '}' to close declaration body");
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips tokens until a safe top-level restart point is reached.
|
||||
*/
|
||||
private void synchronizeTopLevel() {
|
||||
while (!cursor.isAtEnd()) {
|
||||
if (isTopLevelRestartToken(cursor.peek().kind())) {
|
||||
@ -382,47 +128,4 @@ public final class PbsParser {
|
||||
|| kind == PbsTokenKind.IMPLEMENTS
|
||||
|| kind == PbsTokenKind.LEFT_BRACKET;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes a required token and reports an error if it is missing.
|
||||
*/
|
||||
private PbsToken consume(final PbsTokenKind kind, final String message) {
|
||||
if (cursor.check(kind)) {
|
||||
return cursor.advance();
|
||||
}
|
||||
final var token = cursor.peek();
|
||||
report(token, ParseErrors.E_PARSE_EXPECTED_TOKEN, message + ", found " + token.kind());
|
||||
if (!cursor.isAtEnd()) {
|
||||
return cursor.advance();
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
private Long parseLongOrNull(final String text) {
|
||||
try {
|
||||
return Long.parseLong(text);
|
||||
} catch (NumberFormatException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a source span for the current file.
|
||||
*/
|
||||
private Span span(final long start, final long end) {
|
||||
return context.span(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a parser diagnostic at the given token span.
|
||||
*/
|
||||
private void report(final PbsToken token, final ParseErrors parseErrors, final String message) {
|
||||
context.report(token, parseErrors, message);
|
||||
}
|
||||
|
||||
private record StructBodyParse(
|
||||
ReadOnlyList<PbsAst.FunctionDecl> methods,
|
||||
ReadOnlyList<PbsAst.CtorDecl> ctors,
|
||||
long end) {
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user