diff --git a/Module1/Lexer.cs b/Module1/Lexer.cs index ad63b0c02c35c920c094852297e4d89e85571d21..50df070da708fcd7a81b7da3d5c363217b8bf00f 100644 --- a/Module1/Lexer.cs +++ b/Module1/Lexer.cs @@ -43,7 +43,7 @@ namespace Lexer protected void NextCh() { this.currentCharValue = this.inputReader.Read(); - this.currentCh = (char) currentCharValue; + this.currentCh = (char)currentCharValue; this.position += 1; } @@ -68,13 +68,20 @@ namespace Lexer public override bool Parse() { NextCh(); + + int sign = 1; if (currentCh == '+' || currentCh == '-') { - NextCh(); //gsdfgdfgsdfg + if (currentCh == '-') + { + sign = -1; + } + NextCh(); } - + if (char.IsDigit(currentCh)) { + parseResult += currentCh - '0'; NextCh(); } else @@ -84,40 +91,65 @@ namespace Lexer while (char.IsDigit(currentCh)) { + parseResult = parseResult * 10 + (currentCh - '0'); NextCh(); } - if (currentCharValue != -1) { Error(); } + parseResult *= sign; + return true; } } - + public class IdentLexer : Lexer { private string parseResult; protected StringBuilder builder; - + public string ParseResult { get { return parseResult; } } - + public IdentLexer(string input) : base(input) { builder = new StringBuilder(); } public override bool Parse() - { - throw new NotImplementedException(); + { + NextCh(); + + if (char.IsLetter(currentCh)) + { + parseResult += currentCh; + NextCh(); + } + else + { + Error(); + } + + while (char.IsDigit(currentCh) || char.IsLetter(currentCh) || currentCh == '_') + { + parseResult += currentCh; + NextCh(); + } + + if (currentCharValue != -1) + { + Error(); + } + + return true; } - + } public class IntNoZeroLexer : IntLexer @@ -129,7 +161,42 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + int sign = 1; + if (currentCh == '+' || currentCh == '-') + { + if (currentCh == '-') + { + sign = -1; + } + NextCh(); + } + + if (currentCh != '0') + { + parseResult += currentCh - '0'; + NextCh(); + } + else + { + Error(); + } + + while (char.IsDigit(currentCh)) + { + parseResult = parseResult * 10 + (currentCh - '0'); + NextCh(); + } + + if (currentCharValue != -1) + { + Error(); + } + + parseResult *= sign; + + return true; } } @@ -151,9 +218,50 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + + NextCh(); + + while (true) + { + if (char.IsLetter(currentCh)) + { + parseResult += currentCh; + NextCh(); + + if (currentCharValue == -1) + { + break; + } + } + else + { + Error(); + } + + if (char.IsDigit(currentCh)) + { + parseResult += currentCh; + NextCh(); + + if (currentCharValue == -1) + { + break; + } + } + else + { + Error(); + } + } + + if (currentCharValue != -1) + { + Error(); + } + + return true; } - + } public class LetterListLexer : Lexer @@ -173,7 +281,62 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + if (char.IsLetter(currentCh)) + { + parseResult.Add(currentCh); + NextCh(); + } + else + { + Error(); + } + + while (true) + { + if (currentCh == ',' || currentCh == ';') + { + NextCh(); + if (currentCharValue == -1) + { + Error(); + } + } + else + { + if (currentCharValue != -1) + { + Error(); + } + } + + if (char.IsLetter(currentCh)) + { + parseResult.Add(currentCh); + NextCh(); + } + else if (currentCharValue == -1) + { + break; + } + else + { + Error(); + } + + if (currentCharValue == -1) + { + break; + } + } + + if (currentCharValue != -1) + { + Error(); + } + + return true; } } @@ -194,7 +357,55 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + if (char.IsDigit(currentCh)) + { + parseResult.Add(currentCh - '0'); + NextCh(); + } + else + { + Error(); + } + + + while (true) + { + if (char.IsWhiteSpace(currentCh)) + { + while (char.IsWhiteSpace(currentCh)) + { + NextCh(); + } + } + else if (currentCharValue == -1) + { + break; + } + else + { + Error(); + } + + if (char.IsDigit(currentCh)) + { + parseResult.Add(currentCh - '0'); + NextCh(); + } + else + { + Error(); + } + } + + + if (currentCharValue != -1) + { + Error(); + } + + return true; } } @@ -207,7 +418,7 @@ namespace Lexer { get { return parseResult; } } - + public LetterDigitGroupLexer(string input) : base(input) { @@ -216,9 +427,47 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + while (currentCharValue != -1) + { + if (char.IsLetter(currentCh)) + { + parseResult += currentCh; + NextCh(); + + if (char.IsLetter(currentCh)) + { + parseResult += currentCh; + NextCh(); + } + } + else + { + Error(); + } + + if (char.IsDigit(currentCh)) + { + parseResult += currentCh; + NextCh(); + + if (char.IsDigit(currentCh)) + { + parseResult += currentCh; + NextCh(); + } + } + } + + if (currentCharValue != -1 || String.IsNullOrEmpty(parseResult)) + { + Error(); + } + + return true; } - + } public class DoubleLexer : Lexer @@ -240,9 +489,49 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + if (char.IsDigit(currentCh)) + { + while (char.IsDigit(currentCh)) + { + parseResult = parseResult * 10 + (currentCh - '0'); + NextCh(); + } + } + else + { + Error(); + } + + if (currentCh == '.') + { + NextCh(); + + double pow = 0.1; + if (char.IsDigit(currentCh)) + { + while (char.IsDigit(currentCh)) + { + parseResult += pow * (currentCh - '0'); + pow /= 10; + NextCh(); + } + } + else + { + Error(); + } + } + + if (currentCharValue != -1) + { + Error(); + } + + return true; } - + } public class StringLexer : Lexer @@ -264,7 +553,36 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + if (currentCh == '\'') + { + NextCh(); + } + else + { + Error(); + } + + bool close = false; + while (currentCharValue != -1) + { + if (currentCh == '\'') + { + close = true; + NextCh(); + break; + } + parseResult += currentCh; + NextCh(); + } + + if (currentCharValue != -1 || !close) + { + Error(); + } + + return true; } } @@ -287,7 +605,53 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + + if (currentCh == '/') + { + parseResult += currentCh; + NextCh(); + + if (currentCh == '*') + { + parseResult += currentCh; + NextCh(); + } + else + { + Error(); + } + } + else + { + Error(); + } + + bool close = false; + + while (currentCharValue != -1) + { + parseResult += currentCh; + if (currentCh == '*') + { + NextCh(); + parseResult += currentCh; + if (currentCh == '/') + { + NextCh(); + close = true; + break; + } + } + NextCh(); + } + + if (currentCharValue != -1 || !close) + { + Error(); + } + + return true; } } @@ -311,7 +675,46 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + string id = ""; + + while (true) + { + if (char.IsLetter(currentCh)) + { + id += currentCh; + NextCh(); + } + else + { + Error(); + } + + while (char.IsDigit(currentCh) || char.IsLetter(currentCh) || currentCh == '_') + { + id += currentCh; + NextCh(); + } + + parseResult.Add(id); + id = ""; + + if (currentCh == '.') + { + NextCh(); + } + else + { + break; + } + } + + if (currentCharValue != -1) + { + Error(); + } + + return true; } } diff --git a/Module1/Lexer.csproj b/Module1/Lexer.csproj index f50a02f192b735a9b4611c8749472030bc06754e..3d0633c40c7959a3865179a9f9462d0d283375db 100644 --- a/Module1/Lexer.csproj +++ b/Module1/Lexer.csproj @@ -1,5 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit3TestAdapter.4.2.1\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.4.2.1\build\net35\NUnit3TestAdapter.props')" /> + <Import Project="..\packages\NUnit.3.13.3\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.13.3\build\NUnit.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> @@ -13,6 +15,8 @@ <FileAlignment>512</FileAlignment> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <TargetFrameworkProfile /> + <NuGetPackageImportStamp> + </NuGetPackageImportStamp> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> @@ -40,6 +44,9 @@ <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> </Reference> + <Reference Include="nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> + <HintPath>..\packages\NUnit.3.13.3\lib\net40\nunit.framework.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> @@ -53,8 +60,16 @@ </ItemGroup> <ItemGroup> <None Include="App.config" /> + <None Include="packages.config" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>Данный проект ÑÑылаетÑÑ Ð½Ð° пакеты NuGet, отÑутÑтвующие на Ñтом компьютере. ИÑпользуйте воÑÑтановление пакетов NuGet, чтобы Ñкачать их. Дополнительную информацию Ñм. по адреÑу: http://go.microsoft.com/fwlink/?LinkID=322105. ОтÑутÑтвует Ñледующий файл: {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.13.3\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.13.3\build\NUnit.props'))" /> + <Error Condition="!Exists('..\packages\NUnit3TestAdapter.4.2.1\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.4.2.1\build\net35\NUnit3TestAdapter.props'))" /> + </Target> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild"> diff --git a/Module1/packages.config b/Module1/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module1/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/TestLexer/Tests.cs b/TestLexer/Tests.cs index 27411c1e0da1bbd0b69ae594c288df1df65db811..b072a2ab0c2bf20e84ddf92f06095875ae4e3ad2 100644 --- a/TestLexer/Tests.cs +++ b/TestLexer/Tests.cs @@ -133,7 +133,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + // [Ignore("This test is disabled")] public class TestIntNotZeroLexer { [Test] @@ -178,7 +178,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestLetterDigitLexer { @@ -218,7 +218,7 @@ namespace TestLexer [TestFixture] - [Ignore("This test is disabled")] + // [Ignore("This test is disabled")] public class TestLetterListLexer { [Test] @@ -281,7 +281,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestDigitListLexer { [Test] @@ -330,7 +330,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + // [Ignore("This test is disabled")] public class TestLetterDigitGroupLexer { [Test] @@ -381,7 +381,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + // [Ignore("This test is disabled")] public class TestDoubleLexer { public TestDoubleLexer() @@ -424,7 +424,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + // [Ignore("This test is disabled")] public class TestQuotedStringLexer { @@ -460,7 +460,7 @@ namespace TestLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestCommentLexer { @@ -495,7 +495,7 @@ namespace TestLexer } - [Ignore("This test is disabled")] + // [Ignore("This test is disabled")] [TestFixture] public class TestIdChainLexer {