feat: 语法分析的详细错误信息 (#63)

Reviewed-on: PostGuard/Canon#63
This commit is contained in:
2024-04-28 15:13:09 +08:00
parent 73dba8b1db
commit d84b254716
4 changed files with 219 additions and 21 deletions

View File

@@ -0,0 +1,71 @@
using Canon.Core.Exceptions;
using Canon.Tests.Utils;
using Xunit.Abstractions;
namespace Canon.Tests.GrammarParserTests;
public class PascalGrammarFailedTests(ITestOutputHelper testOutputHelper)
{
[Fact]
public void StructTest()
{
const string program = """
program main;
begin
end
""";
CatchException(program);
}
[Fact]
public void AssignTest()
{
const string program = """
program main;
begin
a := 'a';
end.
""";
CatchException(program);
}
[Fact]
public void StatementTest()
{
const string program = """
program main;
begin
if a = 1 then
doSomething;
else
doSomething;
end.
""";
CatchException(program);
}
[Fact]
public void ForTest()
{
const string program = """
program main;
begin
for a = 1 to 100 do
doSomething
end.
""";
CatchException(program);
}
private void CatchException(string program)
{
GrammarException exception = Assert.Throws<GrammarException>(() =>
CompilerHelpers.Analyse(program));
testOutputHelper.WriteLine(exception.Message);
}
}

View File

@@ -50,22 +50,6 @@ public class PascalGrammarTests
Assert.Equal("exFunction", root.Head.ProgramName.LiteralValue);
}
[Fact]
public void SubprogramTest()
{
const string program = """
program main;
procedure test;
begin
end;
begin
end.
""";
ProgramStruct root = CompilerHelpers.Analyse(program);
Assert.Equal("main", root.Head.ProgramName.LiteralValue);
}
[Fact]
public void CharacterTest()
{
@@ -94,4 +78,87 @@ public class PascalGrammarTests
CompilerHelpers.Analyse(program);
}
[Fact]
public void MultiplyArrayTest()
{
const string program = """
program arrayTest;
var a : array [10..100, 0..10] of integer;
begin
a[10,0] := 1;
end.
""";
CompilerHelpers.Analyse(program);
}
[Fact]
public void ProcedureTest()
{
const string program = """
program main;
procedure test;
begin
end;
begin
end.
""";
CompilerHelpers.Analyse(program);
}
[Fact]
public void FunctionTest()
{
const string program = """
program main;
function test(a, b : integer) : integer;
begin
test := 1;
end;
begin
end.
""";
CompilerHelpers.Analyse(program);
}
[Fact]
public void ForLoopTest()
{
const string program = """
program main;
var i : integer;
begin
for i := 1 to 100 do
begin
doSomething(i);
end;
end.
""";
CompilerHelpers.Analyse(program);
}
[Fact]
public void IfConditionTest()
{
const string program = """
program main;
begin
if 1 = 2 then
begin
test1;
test2 := a;
end
else
begin
doSomething;
end;
end.
""";
CompilerHelpers.Analyse(program);
}
}