Пример #1
0
        private bool TryParseEscapeSequence(ref StringPart part)
        {
            int start = this.text.Position;

            text.Next();
            if (escapeChar.IndexOf(text.Char()) >= 0)
            {
                text.Next();
                part = new StringPart(new Span(text.Position - 2, 2));
                return(true);
            }
            if (Char.IsDigit(text.Char()) && Char.IsDigit(text.NChar()) && Char.IsDigit(text.NNChar()))
            {
                // a trigraph
                text.Skip(3);
                part = new StringPart(new Span(text.Position - 4, 4));
                return(true);
            }
            if (text.Char() == '0' && !Char.IsDigit(text.NChar()))
            {
                // \0
                text.Next();
                part = new StringPart(new Span(text.Position - 2, 2));
                return(true);
            }
            if (text.Char() == 'u')
            {
                text.Next();
                text.Mark();
                Span?span = TryParseShortUnicode();
                if (span.HasValue)
                {
                    part = new StringPart(span.Value);
                    return(true);
                }
                text.BackToMark();
            }
            if (text.Char() == 'U')
            {
                text.Next();
                text.Mark();
                Span?span = TryParseLongUnicode();
                if (span.HasValue)
                {
                    part = new StringPart(span.Value);
                    return(true);
                }
                text.BackToMark();
            }
            // unrecognized sequence, return it as error
            text.Next();
            int length = text.Position - start;

            part = new StringPart(new Span(start, length), StringPartType.EscapeSequenceError);
            return(true);
        }
Пример #2
0
 public Span?Next()
 {
     while (!text.EndOfLine)
     {
         if (text.Char() == '\\')
         {
             text.Next();
             if (escapeChar.IndexOf(text.Char()) >= 0)
             {
                 text.Next();
                 return(new Span(text.Position - 2, 2));
             }
             if (Char.IsDigit(text.Char()) && Char.IsDigit(text.NChar()) && Char.IsDigit(text.NNChar()))
             {
                 // a trigraph
                 text.Skip(3);
                 return(new Span(text.Position - 4, 4));
             }
             if (text.Char() == '0' && !Char.IsDigit(text.NChar()))
             {
                 // \0
                 text.Next();
                 return(new Span(text.Position - 2, 2));
             }
             if (text.Char() == 'u')
             {
                 text.Next();
                 text.Mark();
                 Span?span = TryParseShortUnicode();
                 if (span.HasValue)
                 {
                     return(span.Value);
                 }
                 text.BackToMark();
             }
             if (text.Char() == 'U')
             {
                 text.Next();
                 text.Mark();
                 Span?span = TryParseLongUnicode();
                 if (span.HasValue)
                 {
                     return(span.Value);
                 }
                 text.BackToMark();
             }
         }
         else
         {
             text.Next();
         }
     }
     return(null);
 }
Пример #3
0
 private bool TryParseEscapeSequence(ref StringPart part)
 {
     text.Next();
     if (escapeChar.IndexOf(text.Char()) >= 0)
     {
         text.Next();
         part = new StringPart(new Span(text.Position - 2, 2));
         return(true);
     }
     if (Char.IsDigit(text.Char()) && Char.IsDigit(text.NChar()) && Char.IsDigit(text.NNChar()))
     {
         // a trigraph
         text.Skip(3);
         part = new StringPart(new Span(text.Position - 4, 4));
         return(true);
     }
     if (text.Char() == '0' && !Char.IsDigit(text.NChar()))
     {
         // \0
         text.Next();
         part = new StringPart(new Span(text.Position - 2, 2));
         return(true);
     }
     if (text.Char() == 'u')
     {
         text.Next();
         text.Mark();
         Span?span = TryParseShortUnicode();
         if (span.HasValue)
         {
             part = new StringPart(span.Value);
             return(true);
         }
         text.BackToMark();
     }
     if (text.Char() == 'U')
     {
         text.Next();
         text.Mark();
         Span?span = TryParseLongUnicode();
         if (span.HasValue)
         {
             part = new StringPart(span.Value);
             return(true);
         }
         text.BackToMark();
     }
     return(false);
 }