implements PR002
This commit is contained in:
+82
@@ -27,6 +27,8 @@ class PbsParserTest {
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), "Parser should not report diagnostics for valid function");
|
||||
assertEquals(0, ast.imports().size());
|
||||
assertEquals(1, ast.topDecls().size());
|
||||
assertEquals(1, ast.functions().size());
|
||||
|
||||
final var fn = ast.functions().getFirst();
|
||||
@@ -35,5 +37,85 @@ class PbsParserTest {
|
||||
assertEquals("int", fn.returnType().name());
|
||||
assertEquals(1, fn.body().statements().size());
|
||||
assertInstanceOf(PbsAst.ReturnStatement.class, fn.body().statements().getFirst());
|
||||
|
||||
assertEquals(fileId, fn.span().getFileId());
|
||||
assertTrue(fn.span().getEnd() > fn.span().getStart());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPreserveImportsAndTopDeclOrdering() {
|
||||
final var source = """
|
||||
import { Vec2 as V2 } from @core:math;
|
||||
|
||||
fn first(): int { return 1; }
|
||||
declare struct Point(x: int, y: int);
|
||||
declare callback Tick(dt: int): int;
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var fileId = new FileId(0);
|
||||
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
|
||||
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), "Parser should accept valid declarations in this slice");
|
||||
assertEquals(1, ast.imports().size());
|
||||
assertEquals(3, ast.topDecls().size());
|
||||
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(0));
|
||||
assertInstanceOf(PbsAst.StructDecl.class, ast.topDecls().get(1));
|
||||
assertInstanceOf(PbsAst.CallbackDecl.class, ast.topDecls().get(2));
|
||||
|
||||
final var importDecl = ast.imports().getFirst();
|
||||
assertEquals("core", importDecl.moduleRef().project());
|
||||
assertEquals("math", importDecl.moduleRef().pathSegments().getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRepresentMandatoryDeclarationFamilies() {
|
||||
final var source = """
|
||||
fn f(): int { return 1; }
|
||||
declare struct S(x: int);
|
||||
declare contract C { fn run(x: int): int; }
|
||||
declare service Game { fn tick(x: int): int { return x; } }
|
||||
declare error Err { Fail; }
|
||||
declare enum Mode(Idle, Run);
|
||||
declare callback TickCb(x: int): int;
|
||||
declare const LIMIT: int = 10;
|
||||
implements C for S using s { fn run(x: int): int { return x; } }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var fileId = new FileId(0);
|
||||
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
|
||||
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), "Parser should represent all mandatory declaration families");
|
||||
assertEquals(9, ast.topDecls().size());
|
||||
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(0));
|
||||
assertInstanceOf(PbsAst.StructDecl.class, ast.topDecls().get(1));
|
||||
assertInstanceOf(PbsAst.ContractDecl.class, ast.topDecls().get(2));
|
||||
assertInstanceOf(PbsAst.ServiceDecl.class, ast.topDecls().get(3));
|
||||
assertInstanceOf(PbsAst.ErrorDecl.class, ast.topDecls().get(4));
|
||||
assertInstanceOf(PbsAst.EnumDecl.class, ast.topDecls().get(5));
|
||||
assertInstanceOf(PbsAst.CallbackDecl.class, ast.topDecls().get(6));
|
||||
assertInstanceOf(PbsAst.ConstDecl.class, ast.topDecls().get(7));
|
||||
assertInstanceOf(PbsAst.ImplementsDecl.class, ast.topDecls().get(8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRecoverWithInvalidDeclarationNode() {
|
||||
final var source = """
|
||||
declare ;
|
||||
fn ok(): int { return 1; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var fileId = new FileId(0);
|
||||
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
|
||||
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.hasErrors(), "Parser should report malformed declaration");
|
||||
assertEquals(2, ast.topDecls().size());
|
||||
assertInstanceOf(PbsAst.InvalidDecl.class, ast.topDecls().get(0));
|
||||
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user