コード例 #1
0
        public override Lexem ReadNextLexem(SourceCodeIterator iterator)
        {
            if (iterator.CurrentSymbol == '<')
            {
                char next = iterator.PeekNext();

                if (next == '>' || next == '=')
                {
                    iterator.MoveNext();
                    iterator.MoveNext();
                    return(ExtractOperatorContent(iterator));
                }
            }
            else if (iterator.CurrentSymbol == '>')
            {
                char next = iterator.PeekNext();

                if (next == '=')
                {
                    iterator.MoveNext();
                    iterator.MoveNext();
                    return(ExtractOperatorContent(iterator));
                }
            }

            var lex = ExtractOperatorContent(iterator);

            iterator.MoveNext();

            return(lex);
        }
コード例 #2
0
        public void IteratorReturnsCorrectLine()
        {
            var code     = "\r\nF = 1;\r\nD = 2;\r\nX = 3;\r\n";
            var iterator = new SourceCodeIterator(code);

            Assert.Equal(1, iterator.CurrentLine);
            Assert.Equal(-1, iterator.CurrentColumn);

            iterator.MoveToContent();

            Assert.Equal(2, iterator.CurrentLine);
            Assert.Equal(1, iterator.CurrentColumn);
            Assert.Equal("F = 1;\r", iterator.GetCodeLine(2));

            iterator.MoveNext();
            Assert.Equal(2, iterator.CurrentColumn);
            iterator.MoveNext();
            Assert.Equal(3, iterator.CurrentColumn);
            Assert.Equal('=', iterator.CurrentSymbol);
            while (iterator.CurrentSymbol != 'D' && iterator.MoveNext())
            {
                ;
            }

            Assert.Equal(1, iterator.CurrentColumn);
            Assert.Equal(3, iterator.CurrentLine);
        }
コード例 #3
0
        public override Lexem ReadNextLexem(SourceCodeIterator iterator)
        {
            iterator.MoveNext();
            System.Diagnostics.Debug.Assert(iterator.CurrentSymbol == '/');

            while (iterator.MoveNext())
            {
                if (iterator.CurrentSymbol == '\n')
                {
                    return(CreateCommentLexem(iterator));
                }
            }

            return(CreateCommentLexem(iterator));
        }
コード例 #4
0
        public void Whitespace_Skipping()
        {
            string code     = "Б \t\t=\n 1; ";
            string check    = "Б=1;";
            var    iterator = new SourceCodeIterator(code);

            for (int i = 0; i < check.Length; i++)
            {
                iterator.MoveToContent();
                Assert.True(check[i] == iterator.CurrentSymbol);
                iterator.MoveNext();
            }

            Assert.True(iterator.CurrentLine == 2);
            Assert.False(iterator.MoveNext());
        }
コード例 #5
0
        public override Lexem ReadNextLexem(SourceCodeIterator iterator)
        {
            iterator.MoveNext();
            System.Diagnostics.Debug.Assert(iterator.CurrentSymbol == '/');

            return(new Lexem()
            {
                Content = iterator.ReadToLineEnd(),
                Type = LexemType.Comment,
                Token = Token.NotAToken
            });
        }
コード例 #6
0
        public void Retrieve_Line_Of_Code()
        {
            string code = @"А = 1;
            Б = 2;
            // comment
            В = 7-11;
            Г = 8";

            var iterator = new SourceCodeIterator(code);

            while (iterator.CurrentLine < 4)
            {
                if (!iterator.MoveNext())
                {
                    throw new Xunit.Sdk.XunitException("Code is less than 4 lines");
                }
            }

            Assert.True(iterator.CurrentLine == 4);
            Assert.True(iterator.CurrentSymbol == '\n');
            iterator.MoveNext();
            Assert.True(iterator.GetCodeLine(4).Trim() == "В = 7-11;");
        }
コード例 #7
0
        public void SourceCode_Iterator_Basics()
        {
            string code     = "Б = 1;";
            var    iterator = new SourceCodeIterator(code);

            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == '\0');
            Assert.True(iterator.PeekNext() == 'Б');

            Assert.True(iterator.MoveNext());
            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == 'Б');
            Assert.True(iterator.PeekNext() == ' ');
            Assert.True(iterator.Position == 0);

            Assert.True(iterator.MoveNext());
            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == ' ');
            Assert.True(iterator.PeekNext() == '=');
            Assert.True(iterator.Position == 1);

            Assert.True(iterator.MoveNext());
            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == '=');
            Assert.True(iterator.PeekNext() == ' ');
            Assert.True(iterator.Position == 2);

            Assert.True(iterator.MoveNext());
            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == ' ');
            Assert.True(iterator.PeekNext() == '1');
            Assert.True(iterator.Position == 3);

            Assert.True(iterator.MoveNext());
            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == '1');
            Assert.True(iterator.PeekNext() == ';');
            Assert.True(iterator.Position == 4);

            Assert.True(iterator.MoveNext());
            Assert.True(iterator.CurrentLine == 1);
            Assert.True(iterator.CurrentSymbol == ';');
            Assert.True(iterator.PeekNext() == '\0');
            Assert.True(iterator.Position == 5);

            Assert.False(iterator.MoveNext());
            Assert.True(iterator.CurrentSymbol == '\0');
            Assert.True(iterator.PeekNext() == '\0');
        }
コード例 #8
0
        public override Lexem ReadNextLexem(SourceCodeIterator iterator)
        {
            iterator.MoveNext();

            if (!iterator.MoveToContent())
            {
                throw CreateExceptionOnCurrentLine(MESSAGE_ANNOTATION_EXPECTED, iterator);
            }

            if (!char.IsLetter(iterator.CurrentSymbol))
            {
                throw CreateExceptionOnCurrentLine(MESSAGE_ANNOTATION_EXPECTED, iterator);
            }

            var lexem = _wordExtractor.ReadNextLexem(iterator);

            lexem.Type = LexemType.Annotation;
            return(lexem);
        }
コード例 #9
0
        public void CurrentColumn_Calculation()
        {
            string code = @"А = 1;
            Б = 2;
            В = 7-11;
            Г = 8";

            var iterator = new SourceCodeIterator(code);

            while (iterator.CurrentLine < 3)
            {
                if (!iterator.MoveNext())
                {
                    throw new Xunit.Sdk.XunitException("Code is less than 3 lines");
                }
            }

            Assert.True(iterator.CurrentLine == 3);
            iterator.MoveToContent();
            Assert.True(iterator.CurrentSymbol == 'В');
            Assert.True(iterator.CurrentColumn == 13);
        }
コード例 #10
0
        public override Lexem ReadNextLexem(SourceCodeIterator iterator)
        {
            bool isEndOfText = false;
            char cs          = '\0';
            int  currentLine = iterator.CurrentLine;

            while (true)
            {
                if (!isEndOfText)
                {
                    cs = iterator.CurrentSymbol;
                }
                if (SpecialChars.IsDelimiter(cs) || isEndOfText)
                {
                    var content = iterator.GetContents();

                    Lexem lex;

                    if (LanguageDef.IsLogicalOperatorString(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.Operator,
                            Token      = LanguageDef.GetToken(content),
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else if (LanguageDef.IsBooleanLiteralString(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.BooleanLiteral,
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else if (LanguageDef.IsUndefinedString(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.UndefinedLiteral,
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else if (LanguageDef.IsNullString(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.NullLiteral,
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.Identifier,
                            Content    = content,
                            Token      = LanguageDef.GetToken(content),
                            LineNumber = currentLine
                        };

                        if (LanguageDef.IsBuiltInFunction(lex.Token))
                        {
                            iterator.SkipSpaces();
                            if (iterator.CurrentSymbol != '(')
                            {
                                lex.Token = Token.NotAToken;
                            }
                        }
                    }

                    return(lex);
                }

                if (!iterator.MoveNext())
                {
                    isEndOfText = true;
                }
            }
        }