public static bool StartsWith(this LexingContext context, string start)
        {
            if (start == null)
            {
                throw new ArgumentNullException(nameof(start));
            }

            var remaining = context.GetRemainingCharCount();

            if (remaining < start.Length)
            {
                return(false);
            }

            var text         = context.Text;
            var initialIndex = context.Index;

            for (var i = 0; i < start.Length; i++)
            {
                var c1 = text[initialIndex + i];
                var c2 = start[i];
                if (c1 != c2)
                {
                    return(false);
                }
            }

            return(true);
        }
        public static void AdvanceByChar(this LexingContext context)
        {
            context.Index++;
            context.Column++;

            context.IncreaseVersion();
        }
        public static void Advance(this LexingContext context, int indexShift, int lineShift, int column)
        {
            context.Index += indexShift;
            context.Line  += lineShift;
            context.Column = column;

            context.IncreaseVersion();
        }
Ejemplo n.º 4
0
        public IList <IToken> Lexize(string input)
        {
            var context = new LexingContext(input);
            var tokens  = new List <IToken>();

            foreach (var producer in this.Producers)
            {
                producer.Context = context;
            }

            var length = input.Length;

            while (context.Index < length)
            {
                var indexBeforeProducing = context.Index;

                foreach (var producer in this.Producers)
                {
                    var oldVersion = context.Version;
                    var oldIndex   = context.Index;
                    var oldLine    = context.Line;

                    var token = producer.Produce();
                    if (token != null)
                    {
                        if (context.Version <= oldVersion)
                        {
                            throw new LexingException(
                                      $"Producer '{producer.GetType().FullName}' has produced a token of type '{token.GetType().FullName}' ('{token}'), but context version has not increased.",
                                      new Position(context.Length, context.Column));
                        }

                        if (context.Index <= oldIndex)
                        {
                            throw new LexingException(
                                      $"Producer '{producer.GetType().FullName}' has produced a token of type '{token.GetType().FullName}' ('{token}'), but context index has not increased.",
                                      new Position(context.Length, context.Column));
                        }

                        if (context.Line < oldLine)
                        {
                            throw new LexingException(
                                      $"Producer '{producer.GetType().FullName}' has produced a token of type '{token.GetType().FullName}' ('{token}'), but context line has decreased.",
                                      new Position(context.Length, context.Column));
                        }

                        if (token is IEmptyToken && this.IgnoreEmptyTokens)
                        {
                            // do nothing
                        }
                        else
                        {
                            tokens.Add(token);
                        }

                        break;
                    }

                    if (context.Version > oldVersion)
                    {
                        break;
                    }
                }

                if (context.Index == indexBeforeProducing)
                {
                    var position = new Position(context.Line, context.Column);
                    var c        = input[context.Index];
                    throw new LexingException($"Could not lexize starting from char '{c}'. See '{nameof(LexingException.Position)}' property to get more information.", position);
                }
            }

            return(tokens);
        }
 public static int GetRemainingCharCount(this LexingContext context)
 {
     return(context.Length - context.Index);
 }