Esempio n. 1
0
 internal static void SkipWhitespace(CharacterStream cs)
 {
     while (!cs.IsEndOfStream() && cs.IsWhiteSpace())
     {
         cs.MoveToNextChar();
     }
 }
Esempio n. 2
0
        public static void SkipIdentifier(CharacterStream cs, Func <CharacterStream, bool> isIdentifierLeadCharacter, Func <CharacterStream, bool> isIdentifierCharacter)
        {
            if (!isIdentifierLeadCharacter(cs))
            {
                return;
            }

            if (cs.IsEndOfStream())
            {
                return;
            }

            while (!cs.IsWhiteSpace())
            {
                if (!isIdentifierCharacter(cs))
                {
                    break;
                }

                if (!cs.MoveToNextChar())
                {
                    break;
                }
            }
        }
Esempio n. 3
0
        public static string NormalizeWhitespace(this string s)
        {
            if (s == null || s.Length == 0)
            {
                return(s);
            }

            var cs = new CharacterStream(new TextStream(s));
            var sb = new StringBuilder();

            while (!cs.IsEndOfStream())
            {
                var current = cs.Position;
                cs.SkipWhitespace();
                if (cs.Position - current > 0)
                {
                    sb.Append(' ');
                }

                while (!cs.IsEndOfStream() && !cs.IsWhiteSpace())
                {
                    sb.Append(cs.CurrentChar);
                    cs.MoveToNextChar();
                }
            }
            return(sb.ToString().Trim());
        }
Esempio n. 4
0
        /// <summary>
        /// Checks file whitespace (typically Lint-type or style type checkers.
        /// </summary>
        /// <returns>A collection of validation errors</returns>
        public IReadOnlyCollection <IValidationError> ValidateWhitespace(ITextProvider tp)
        {
            if (!_linterEnabled)
            {
                return(Enumerable.Empty <IValidationError>().ToList());
            }

            var warnings = _whitespaceFileCheckers
                           .SelectMany(c => c(tp, _settings.LintOptions, _projectedBuffer))
                           .ToList();

            var cs = new CharacterStream(tp);

            while (!cs.IsEndOfStream())
            {
                if (cs.IsWhiteSpace())
                {
                    // Unrolled since most return nulls.
                    warnings.AddRange(_whitespaceCharCheckers
                                      .Select(c => c(cs, _settings.LintOptions))
                                      .Where(result => result != null));
                }
                cs.MoveToNextChar();
            }
            return(warnings.ToList());
        }
Esempio n. 5
0
        /// <summary>
        /// Checks file whitespace (typically Lint-type or style type checkers.
        /// </summary>
        /// <returns>A collection of validation errors</returns>
        public IReadOnlyCollection <IValidationError> ValidateWhitespace(ITextProvider tp)
        {
            if (!_settings.LintOptions.Enabled)
            {
                return(Enumerable.Empty <IValidationError>().ToList());
            }

            var warnings = _whitespaceFileCheckers.SelectMany(c => c(tp, _settings.LintOptions)).ToList();
            var cs       = new CharacterStream(tp);

            while (!cs.IsEndOfStream())
            {
                if (cs.IsWhiteSpace())
                {
                    // Unrolled since most return nulls.
                    foreach (var c in _whitespaceCharCheckers)
                    {
                        var result = c(cs, _settings.LintOptions);
                        if (result != null)
                        {
                            warnings.Add(result);
                        }
                    }
                }
                cs.MoveToNextChar();
            }
            return(warnings.ToList());
        }
Esempio n. 6
0
 private static IValidationError TrailingWhitespaceCheck(CharacterStream cs, LintOptions options)
 {
     if (options.TrailingWhitespace)
     {
         if (cs.IsWhiteSpace() && !cs.CurrentChar.IsLineBreak() && (cs.NextChar.IsLineBreak() || cs.Position == cs.Length - 1))
         {
             // trailing_whitespace_linter: check there are no trailing whitespace characters.
             return(new ValidationWarning(new TextRange(cs.Position, 1), Resources.Lint_TrailingWhitespace, ErrorLocation.Token));
         }
     }
     return(null);
 }
Esempio n. 7
0
        public void SkipWhitespace()
        {
            if (_cs.IsEndOfStream())
            {
                return;
            }

            while (_cs.IsWhiteSpace())
            {
                if (!_cs.MoveToNextChar())
                {
                    break;
                }
            }
        }
Esempio n. 8
0
        internal static int HandleExponent(CharacterStream cs, int start)
        {
            Debug.Assert(cs.CurrentChar == 'E' || cs.CurrentChar == 'e');

            bool hasSign = false;

            cs.MoveToNextChar();
            if (cs.IsWhiteSpace() || cs.IsEndOfStream())
            {
                // 0.1E or 1e
                return(0);
            }

            if (cs.CurrentChar == '-' || cs.CurrentChar == '+')
            {
                hasSign = true;
                cs.MoveToNextChar();
            }

            int digitsStart = cs.Position;

            // collect decimals
            while (cs.IsDecimal())
            {
                cs.MoveToNextChar();
            }

            if (hasSign && digitsStart == cs.Position)
            {
                return(0); // NaN like 1.0E-
            }

            // Technically if letter or braces follows this is not
            // a number but we'll leave it alone for now.

            // TODO: This code is not language specific and yet it currently
            // handles complex 'i' as well as R-specific 'L' suffix.
            // Ideally this needs to be extended in a way so language-specific
            // tokenizer can specify options or control number format.
            if (char.IsLetter(cs.CurrentChar) && cs.CurrentChar != 'i' && cs.CurrentChar != 'L')
            {
                return(0);
            }

            return(cs.Position - start);
        }
Esempio n. 9
0
        private static int GetNCharOperatorLength(CharacterStream cs)
        {
            // R allows user-defined infix operators. These have the form of
            // a string of characters delimited by the ‘%’ character. The string
            // can contain any printable character except ‘%’.
            if (cs.CurrentChar == '%' && !char.IsWhiteSpace(cs.NextChar))
            {
                // In case of broken or partially typed operators
                // make sure we terminate at whitespace or end of the line
                // so in 'x <- y % z' '% z' is not an operator.
                int start = cs.Position;
                int length;

                cs.MoveToNextChar();

                while (!cs.IsEndOfStream() && !cs.IsWhiteSpace())
                {
                    if (cs.CurrentChar == '%')
                    {
                        cs.MoveToNextChar();

                        length      = cs.Position - start;
                        cs.Position = start;

                        return(length);
                    }

                    if (cs.IsAtNewLine())
                    {
                        // x <- y %abcd
                        cs.Position = start;
                        return(1);
                    }

                    cs.MoveToNextChar();
                }
            }

            return(Get3CharOrShorterOperatorLength(cs));
        }