From 01bdba234e32a800b3bdc86de5d8e226902684b0 Mon Sep 17 00:00:00 2001 From: NikitaBogoslovskiy <nbog@sfedu.ru> Date: Thu, 15 Sep 2022 04:51:58 +0300 Subject: [PATCH] do module 1 --- Module1/Lexer.cs | 389 +++++++++++++++++++++++++++++++++++++++++++-- TestLexer/Tests.cs | 16 +- 2 files changed, 383 insertions(+), 22 deletions(-) diff --git a/Module1/Lexer.cs b/Module1/Lexer.cs index 6ea76e6..47e5cbb 100644 --- a/Module1/Lexer.cs +++ b/Module1/Lexer.cs @@ -120,26 +120,70 @@ namespace Lexer public override bool Parse() { - if (builder.Length == 0) - Error(); - if (!(char.IsLetter(builder[0]) || builder[0] == '_')) - Error(); - return true; - //throw new NotImplementedException(); + NextCh(); + if (currentCh == '_' || char.IsLetter(currentCh)) + { + builder.Append(currentCh); + NextCh(); + } + else + Error(); + + while (currentCharValue > 0) + { + if (currentCh == '_' || char.IsLetter(currentCh) || char.IsDigit(currentCh)) + { + builder.Append(currentCh); + NextCh(); + } + else + Error(); + } + + parseResult = builder.ToString(); + return true; } } public class IntNoZeroLexer : IntLexer { + private string parseResult; + protected StringBuilder builder; public IntNoZeroLexer(string input) : base(input) { + builder = new StringBuilder(); } public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + var res = char.IsDigit(currentCh); + if (!char.IsDigit(currentCh)) + { + if (currentCh == '+' || currentCh == '-') + { + builder.Append(currentCh); + NextCh(); + } + else + { + Error(); + } + } + + if (currentCh == '0') + Error(); + + while (currentCharValue > 0) + { + builder.Append(currentCh); + NextCh(); + } + + parseResult = builder.ToString(); + return true; } } @@ -161,7 +205,40 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + var next_letter = true; + NextCh(); + while (currentCharValue > 0) + { + if (next_letter) + { + if (char.IsLetter(currentCh)) + { + builder.Append(currentCh); + NextCh(); + next_letter = false; + } + else + { + Error(); + } + } + else + { + if (char.IsDigit(currentCh)) + { + builder.Append(currentCh); + NextCh(); + next_letter = true; + } + else + { + Error(); + } + } + } + + parseResult = builder.ToString(); + return true; } } @@ -183,7 +260,39 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + var next_letter = true; + while (currentCharValue > 0) + { + if (next_letter) + { + if (char.IsLetter(currentCh)) + { + parseResult.Add(currentCh); + NextCh(); + next_letter = false; + } + else + { + Error(); + } + } + else + { + if (currentCh == ';' || currentCh == ',') + { + NextCh(); + next_letter = true; + } + else + { + Error(); + } + } + } + if (next_letter) + Error(); + return true; } } @@ -204,7 +313,58 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + var next_digit = true; + var next_space = false; + while (currentCharValue > 0) + { + if (next_digit) + { + if (char.IsDigit(currentCh)) + { + parseResult.Add(currentCh - '0'); + NextCh(); + next_digit = false; + next_space = true; + } + else + { + Error(); + } + } + else if (next_space) + { + if (currentCh == ' ') + { + NextCh(); + next_space = false; + } + else + { + Error(); + } + } + else + { + if (char.IsDigit(currentCh)) + { + parseResult.Add(currentCh - '0'); + NextCh(); + next_space = true; + } + else if (currentCh == ' ') + { + NextCh(); + } + else + { + Error(); + } + } + } + if (!next_space) + Error(); + return true; } } @@ -226,7 +386,82 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + var state = 0; + if (currentCharValue < 0) + Error(); + while (currentCharValue > 0) + { + switch (state) + { + case 0: + if (char.IsLetter(currentCh)) + { + builder.Append(currentCh); + NextCh(); + state = 2; + } + else + { + Error(); + } + + break; + case 1: + if (char.IsDigit(currentCh)) + { + builder.Append(currentCh); + NextCh(); + state = 3; + } + else + { + Error(); + } + + break; + case 2: + if (char.IsLetter(currentCh)) + { + builder.Append(currentCh); + NextCh(); + state = 1; + } + else if (char.IsDigit(currentCh)) + { + builder.Append(currentCh); + NextCh(); + state = 3; + } + else + { + Error(); + } + + break; + case 3: + if (char.IsLetter(currentCh)) + { + builder.Append(currentCh); + NextCh(); + state = 2; + } + else if (char.IsDigit(currentCh)) + { + builder.Append(currentCh); + NextCh(); + state = 0; + } + else + { + Error(); + } + + break; + } + } + parseResult = builder.ToString(); + return true; } } @@ -250,7 +485,40 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + if (currentCharValue < 0) + Error(); + var int_part = true; + var coef = 0.1; + parseResult = 0; + while (currentCharValue > 0) + { + if (currentCh == '.') + { + if (position == 1 || !int_part) + Error(); + int_part = false; + NextCh(); + if (currentCharValue < 0) + Error(); + } + else if (char.IsDigit(currentCh)) + { + if (int_part) + parseResult = parseResult * 10 + (currentCh - '0'); + else + { + parseResult += (currentCh - '0') * coef; + coef /= 10; + } + NextCh(); + } + else + { + Error(); + } + } + return true; } } @@ -274,7 +542,41 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + var state = 0; + while (currentCharValue > 0) + { + switch (state) + { + case 0: + if (currentCh == '\'') + { + state = 1; + builder.Append(currentCh); + NextCh(); + } + else + { + Error(); + } + + break; + case 1: + builder.Append(currentCh); + if (currentCh == '\'') + state = 2; + NextCh(); + break; + case 2: + Error(); + break; + } + } + + parseResult = builder.ToString(); + if (state != 2) + Error(); + return true; } } @@ -297,7 +599,66 @@ namespace Lexer public override bool Parse() { - throw new NotImplementedException(); + NextCh(); + if (currentCharValue < 0) + Error(); + var state = 0; + while (currentCharValue > 0) + { + switch (state) + { + case 0: + if (currentCh == '/') + { + state = 1; + builder.Append(currentCh); + NextCh(); + } + else + { + Error(); + } + + break; + case 1: + if (currentCh == '*') + { + state = 2; + builder.Append(currentCh); + NextCh(); + } + else + { + Error(); + } + + break; + case 2: + builder.Append(currentCh); + if (currentCh == '*') + { + state = 3; + } + NextCh(); + + break; + case 3: + builder.Append(currentCh); + if (currentCh != '*' && currentCh != '/') + state = 2; + else if (currentCh == '/') + state = 4; + NextCh(); + + break; + case 4: + Error(); + break; + } + } + if (state != 4) + Error(); + return true; } } diff --git a/TestLexer/Tests.cs b/TestLexer/Tests.cs index 27411c1..7c9897a 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 { -- GitLab