18
Canon.Tests/Utils/CompilerHelpers.cs
Normal file
18
Canon.Tests/Utils/CompilerHelpers.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Canon.Core.Abstractions;
|
||||
using Canon.Core.GrammarParser;
|
||||
using Canon.Core.LexicalParser;
|
||||
using Canon.Core.SyntaxNodes;
|
||||
|
||||
namespace Canon.Tests.Utils;
|
||||
|
||||
public static class CompilerHelpers
|
||||
{
|
||||
public static ProgramStruct Analyse(string program)
|
||||
{
|
||||
ILexer lexer = new Lexer();
|
||||
IGrammarParser grammarParser = GeneratedGrammarParser.Instance;
|
||||
|
||||
IEnumerable<SemanticToken> tokens = lexer.Tokenize(new StringSourceReader(program));
|
||||
return grammarParser.Analyse(tokens);
|
||||
}
|
||||
}
|
85
Canon.Tests/Utils/SampleSyntaxTreeVisitor.cs
Normal file
85
Canon.Tests/Utils/SampleSyntaxTreeVisitor.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Text;
|
||||
using Canon.Core.Abstractions;
|
||||
using Canon.Core.SyntaxNodes;
|
||||
|
||||
namespace Canon.Tests.Utils;
|
||||
|
||||
public class SampleSyntaxTreeVisitor : SyntaxNodeVisitor
|
||||
{
|
||||
private readonly StringBuilder _builder = new();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _builder.ToString();
|
||||
}
|
||||
|
||||
public override void PreVisit(ProgramStruct programStruct)
|
||||
{
|
||||
_builder.Append(programStruct).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(ProgramStruct programStruct)
|
||||
{
|
||||
_builder.Append(programStruct).Append('\n');
|
||||
}
|
||||
|
||||
public override void PreVisit(ProgramHead programHead)
|
||||
{
|
||||
_builder.Append(programHead).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(ProgramHead programHead)
|
||||
{
|
||||
_builder.Append(programHead).Append('\n');
|
||||
}
|
||||
|
||||
public override void PreVisit(ProgramBody programBody)
|
||||
{
|
||||
_builder.Append(programBody).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(ProgramBody programBody)
|
||||
{
|
||||
_builder.Append(programBody).Append('\n');
|
||||
}
|
||||
|
||||
public override void PreVisit(SubprogramDeclarations subprogramDeclarations)
|
||||
{
|
||||
_builder.Append(subprogramDeclarations).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(SubprogramDeclarations subprogramDeclarations)
|
||||
{
|
||||
_builder.Append(subprogramDeclarations).Append('\n');
|
||||
}
|
||||
|
||||
public override void PreVisit(CompoundStatement compoundStatement)
|
||||
{
|
||||
_builder.Append(compoundStatement).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(CompoundStatement compoundStatement)
|
||||
{
|
||||
_builder.Append(compoundStatement).Append('\n');
|
||||
}
|
||||
|
||||
public override void PreVisit(StatementList statementList)
|
||||
{
|
||||
_builder.Append(statementList).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(StatementList statementList)
|
||||
{
|
||||
_builder.Append(statementList).Append('\n');
|
||||
}
|
||||
|
||||
public override void PreVisit(Statement statement)
|
||||
{
|
||||
_builder.Append(statement).Append('\n');
|
||||
}
|
||||
|
||||
public override void PostVisit(Statement statement)
|
||||
{
|
||||
_builder.Append(statement).Append('\n');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user