Exemplo n.º 1
0
        private static void PrintRight(IEnumerable <Token> tokens, LineNumberFixer fixer)
        {
            foreach (var token in tokens)
            {
                var beginPosition = fixer.GetPosition(token.TokenBeginIdx).TidyPosition();
                var endPosition   = fixer.GetPosition(token.TokenEndIdx).TidyPosition();

                Console.WriteLine($"类型:“{token.TokenType}”\n词素:“{token.Lexeme}”\n" +
                                  $"起始位置:{beginPosition}\n终止位置:{endPosition}\n");
            }
        }
Exemplo n.º 2
0
        private static void PrintLeft(IEnumerable <WrongTokenException> exceptions, LineNumberFixer fixer)
        {
            Console.Error.WriteLine("无法识别的语法单元:");
            Console.Error.WriteLine();

            foreach (var exception in exceptions)
            {
                var beginPosition = fixer.GetPosition(exception.TokenBegin);
                var endPosition   = fixer.GetPosition(exception.CurrentIdx);
#if DEBUG
                beginPosition.line.ShouldBe(endPosition.line);
#endif
                Console.Error.WriteLine($"在 {endPosition.TidyPosition()} 处:");
                Console.Error.WriteLine();
                Console.Error.WriteLine(fixer.GetPositionRangeMap(beginPosition.line, beginPosition.column, endPosition.column));
                Console.Error.WriteLine();
            }
        }
Exemplo n.º 3
0
        private static void HandleParseError(ICollection <ParseException> parseExceptions, LineNumberFixer fixer)
        {
            Console.Error.WriteLine("语法错误:");
            Console.Error.WriteLine();

            foreach (var exception in parseExceptions)
            {
                Console.Error.WriteLine(exception.Message);
                if (exception.Token != null)
                {
                    var beginPosition = fixer.GetPosition(exception.Token.TokenBeginIdx);
                    var endPosition   = fixer.GetPosition(exception.Token.TokenEndIdx);
#if DEBUG
                    beginPosition.line.ShouldBe(endPosition.line);
#endif
                    Console.Error.WriteLine($"在 {beginPosition.TidyPosition()}-{endPosition.TidyPosition()} 处:");
                    Console.Error.WriteLine();
                    Console.Error.WriteLine(fixer.GetPositionRangeMap(beginPosition.line, beginPosition.column,
                                                                      endPosition.column));
                }

                Console.Error.WriteLine();
            }
        }
Exemplo n.º 4
0
 public void GetPosition_能够返回正确的位置(int idx, int expectLine, int expectColumn)
 {
     var(line, column) = _lineNumberFixer.GetPosition(idx);
     line.ShouldBe(expectLine);
     column.ShouldBe(expectColumn);
 }