Exemple #1
0
 private void AdvanceUntil(LexemeKind kind)
 {
     while (Current.Kind != kind)
     {
         Advance();
     }
 }
Exemple #2
0
 public Lexeme(LexemeKind kind, SourceLocation begin, SourceLocation end, string content)
 {
     this.Kind    = kind;
     this.Begin   = begin;
     this.End     = end;
     this.Content = content;
 }
Exemple #3
0
        private Lexeme Lexeme(LexemeKind kind)
        {
            var content = Builder.ToString();

            Builder.Clear();

            return(Lexeme(kind, content));
        }
Exemple #4
0
        private bool Peek(LexemeKind kind, bool ignoreWhitespace = true)
        {
            if (ignoreWhitespace)
            {
                SkipWhitespaces();
            }

            return(Index < Lexemes.Length - 1 && Lexemes[Index + 1].Kind == kind);
        }
Exemple #5
0
        private Lexeme Lexeme(LexemeKind kind, string content = null)
        {
            content = content ?? Buffer;
            Buffer  = "";

            var begin = Location;
            var end   = new SourceLocation(Line, Column + content.Length, Index + content.Length);

            return(new Lexeme(kind, begin, end, content));
        }
Exemple #6
0
        private Lexeme Lexeme(LexemeKind kind, string content = null)
        {
            if (kind == LexemeKind.NewLine)
            {
                Column = 0;
                Line++;
            }

            content = content ?? kind.GetCharacter() ?? Buffer.ToString();
            Buffer.Clear();

            return(new Lexeme(kind, Location, content));
        }
Exemple #7
0
        private Lexeme Take(LexemeKind lexemeKind, bool ignoreWhitespace = true)
        {
            if (ignoreWhitespace)
            {
                SkipWhitespaces();
            }

            if (Current.Kind != lexemeKind)
            {
                Error($"Unexpected lexeme: expected {lexemeKind}, found {Current}");
            }

            return(TakeAny());
        }
Exemple #8
0
        private Lexeme Take(LexemeKind lexemeKind, string expected = null, bool ignoreWhitespace = true)
        {
            if (ignoreWhitespace)
            {
                SkipWhitespaces();
            }

            if (Current.Kind != lexemeKind)
            {
                var lexemeChar = lexemeKind.GetCharacter();
                Error($"expected {expected ?? lexemeKind.ToString()}{(lexemeChar != null ? $" '{lexemeChar}'" : "")}, found '{Current.Content}' ({Current.Kind})");
            }

            return(TakeAny());
        }
Exemple #9
0
        private bool Take(LexemeKind lexemeKind, out Lexeme lexeme, bool ignoreWhitespace = true)
        {
            if (ignoreWhitespace)
            {
                SkipWhitespaces();
            }

            if (Current.Kind == lexemeKind)
            {
                lexeme = TakeAny();
                return(true);
            }

            lexeme = null;
            return(false);
        }
Exemple #10
0
 private Lexeme Lexeme(LexemeKind kind, string content) => new Lexeme(kind, content, new SourceLocation(Line, Column - (content?.Length ?? 0)));
Exemple #11
0
 public static string GetCharacter(this LexemeKind kind)
 {
     return(CharCache.TryGetValue(kind, out var c)
         ? c
         : CharCache[kind] = typeof(LexemeKind).GetField(kind.ToString()).GetCustomAttribute <DescriptionAttribute>()?.Description);
 }
Exemple #12
0
 public Lexeme(LexemeKind kind)
 {
     Kind = kind;
 }
Exemple #13
0
 public Lexeme(LexemeKind kind, string content, SourceLocation location)
 {
     this.Kind     = kind;
     this.Content  = content;
     this.Location = location;
 }