2024-03-13 23:58:06 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
2024-04-11 22:41:43 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.GrammarParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
using Canon.Core.LexicalParser;
|
|
|
|
|
using Canon.Core.SyntaxNodes;
|
2024-04-08 19:46:24 +08:00
|
|
|
|
using Canon.Tests.GeneratedParserTests;
|
2024-04-18 16:34:32 +08:00
|
|
|
|
using Canon.Tests.Utils;
|
2024-03-13 23:58:06 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.GrammarParserTests;
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class PascalGrammarTests
|
2024-03-13 23:58:06 +08:00
|
|
|
|
{
|
2024-04-12 19:01:37 +08:00
|
|
|
|
private readonly IGrammarParser _parser = GeneratedGrammarParser.Instance;
|
2024-04-18 16:34:32 +08:00
|
|
|
|
private readonly ILexer _lexer = new Lexer();
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DoNothingTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program DoNothing;
|
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(program));
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
2024-04-12 19:01:37 +08:00
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
2024-04-07 16:47:28 +08:00
|
|
|
|
Assert.Equal("DoNothing", root.Head.ProgramName.LiteralValue);
|
2024-04-12 19:01:37 +08:00
|
|
|
|
Assert.Equal(15, root.Count());
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|
2024-04-11 22:41:43 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AddTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program Add;
|
|
|
|
|
var a : Integer;
|
|
|
|
|
begin
|
|
|
|
|
a := 1 + 1
|
|
|
|
|
end.
|
|
|
|
|
""";
|
2024-04-18 16:34:32 +08:00
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(program));
|
2024-04-11 22:41:43 +08:00
|
|
|
|
|
2024-04-12 19:01:37 +08:00
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
Assert.Equal("Add", root.Head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void WriteLnTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program exFunction;
|
|
|
|
|
const str = 'result is : ';
|
|
|
|
|
var a, b : Integer;
|
|
|
|
|
begin
|
|
|
|
|
writeln( str, ret );
|
|
|
|
|
end.
|
|
|
|
|
""";
|
2024-04-18 16:34:32 +08:00
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(program));
|
2024-04-11 22:41:43 +08:00
|
|
|
|
|
2024-04-12 19:01:37 +08:00
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
Assert.Equal("exFunction", root.Head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 12:37:17 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void SubprogramTest()
|
2024-04-11 22:41:43 +08:00
|
|
|
|
{
|
2024-04-13 12:37:17 +08:00
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
procedure test;
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
2024-04-18 16:34:32 +08:00
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(program));
|
2024-04-13 12:37:17 +08:00
|
|
|
|
|
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
|
|
|
|
Assert.Equal("main", root.Head.ProgramName.LiteralValue);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
}
|
2024-04-20 22:23:29 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void CharacterTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program varTest;
|
2024-04-21 16:30:44 +08:00
|
|
|
|
var a : integer;
|
2024-04-20 22:23:29 +08:00
|
|
|
|
begin
|
2024-04-21 16:30:44 +08:00
|
|
|
|
a := 9 div 1;
|
2024-04-20 22:23:29 +08:00
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(program));
|
|
|
|
|
|
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
|
|
|
|
Assert.Equal("vartest", root.Head.ProgramName.IdentifierName);
|
|
|
|
|
}
|
2024-03-13 23:58:06 +08:00
|
|
|
|
}
|