72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
|
using CanonSharp.Common.LexicalAnalyzer;
|
||
|
using StringReader = CanonSharp.Common.Reader.StringReader;
|
||
|
|
||
|
namespace CanonSharp.Tests.LexicalAnalyzerTests;
|
||
|
|
||
|
public class ScanTests
|
||
|
{
|
||
|
[Fact]
|
||
|
public void ScanTest1()
|
||
|
{
|
||
|
LexicalScannerBuilder builder = LexicalScanner.CreateEmptyBuilder();
|
||
|
|
||
|
LexicalToken token1 = new(RegularExpression.String("ab"), 1);
|
||
|
builder.DefineToken(token1);
|
||
|
|
||
|
StringReader reader = new("ab");
|
||
|
LexicalScanner scanner = builder.Build(reader);
|
||
|
|
||
|
Assert.True(scanner.TryRead(out LexicalToken? result));
|
||
|
Assert.Equal(token1, result);
|
||
|
Assert.Equal("ab", result.LiteralValue);
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void ScanTest2()
|
||
|
{
|
||
|
LexicalScannerBuilder builder = LexicalScanner.CreateDefaultBuilder();
|
||
|
|
||
|
LexicalToken stringKeyword = new(RegularExpression.String("string"), 100);
|
||
|
LexicalToken periodDelimiter = new(RegularExpression.Single('.'), 100);
|
||
|
LexicalToken semiColonDelimiter = new(RegularExpression.Single(';'), 100);
|
||
|
LexicalToken identifier = new(RegularExpression.Concatenate(RegularExpression.Range('a', 'z'),
|
||
|
RegularExpression.Kleene(RegularExpression.Range('a', 'z'))), 0);
|
||
|
LexicalToken assigner = new(RegularExpression.String(":="), 100);
|
||
|
builder.DefineToken(stringKeyword);
|
||
|
builder.DefineToken(periodDelimiter);
|
||
|
builder.DefineToken(semiColonDelimiter);
|
||
|
builder.DefineToken(identifier);
|
||
|
builder.DefineToken(assigner);
|
||
|
|
||
|
StringReader reader = new("""
|
||
|
string value := origin;
|
||
|
string abc := value.
|
||
|
""");
|
||
|
LexicalScanner scanner = builder.Build(reader);
|
||
|
Validate(scanner, [
|
||
|
stringKeyword,
|
||
|
identifier,
|
||
|
assigner,
|
||
|
identifier,
|
||
|
semiColonDelimiter,
|
||
|
stringKeyword,
|
||
|
identifier,
|
||
|
assigner,
|
||
|
identifier,
|
||
|
periodDelimiter
|
||
|
]);
|
||
|
|
||
|
Assert.False(scanner.TryRead(out _));
|
||
|
}
|
||
|
|
||
|
private static void Validate(LexicalScanner scanner, IEnumerable<LexicalToken> expectedTokens)
|
||
|
{
|
||
|
foreach (LexicalToken token in expectedTokens)
|
||
|
{
|
||
|
Assert.True(scanner.TryRead(out LexicalToken? outToken));
|
||
|
Assert.NotNull(outToken);
|
||
|
Assert.Equal(token, outToken);
|
||
|
}
|
||
|
}
|
||
|
}
|