Exemplo n.º 1
0
        public static string PeekIdentifier(this IPreprocessorReader rdr)
        {
            var first = true;

            return(rdr.PeekUntil(ch =>
            {
                if (first)
                {
                    first = false;
                    return ch.IsWordChar(true);
                }
                else
                {
                    return ch.IsWordChar(false);
                }
            }));
        }
Exemplo n.º 2
0
        public static string PeekToken(this IPreprocessorReader rdr, bool stayOnSameLine)
        {
            bool lineChange;

            if (rdr.IgnoreComments(out lineChange))
            {
                if (lineChange && stayOnSameLine)
                {
                    return(null);
                }
                return(string.Empty);
            }

            var ch = rdr.Peek();

            if ((ch == '\r' || ch == '\n') && stayOnSameLine)
            {
                return(null);
            }

            if (ch == ' ' || ch == '\t')
            {
                return(rdr.PeekUntil(c => c == ' ' || c == '\t'));
            }

            if (ch.IsWordChar(true))
            {
                return(rdr.PeekIdentifier());
            }

            if (char.IsDigit(ch))
            {
                var gotDecimal = false;
                return(rdr.PeekUntil(c =>
                {
                    if (char.IsDigit(c))
                    {
                        return true;
                    }
                    if (c == '.')
                    {
                        if (gotDecimal)
                        {
                            return false;
                        }
                        gotDecimal = true;
                        return true;
                    }
                    return false;
                }));
            }

            if (ch == '\"' || ch == '\'')
            {
                var sb = new StringBuilder();

                var lastCh = '\0';
                var first  = true;
                var gotEnd = false;
                sb.Append(rdr.PeekUntil(c =>
                {
                    if (gotEnd)
                    {
                        return(false);
                    }

                    if (first)
                    {
                        first = false;
                        return(true);
                    }

                    if (c == ch && lastCh != '\\')
                    {
                        gotEnd = true;
                        return(true);
                    }

                    if (c == '\\' && lastCh == '\\')
                    {
                        lastCh = '\0';
                    }
                    else
                    {
                        lastCh = c;
                    }

                    return(true);
                }));
                return(sb.ToString());
            }

            if (ch == '#')
            {
                var index = -1;
                return(rdr.PeekUntil(c =>
                {
                    index++;
                    if (index == 0)
                    {
                        return c == '#';
                    }
                    else if (index == 1)
                    {
                        return c.IsWordChar(true);
                    }
                    else
                    {
                        return c.IsWordChar(false);
                    }
                }));
            }

            return(ch.ToString());
        }