Пример #1
0
        public TokenMatch Read(ICharReader Reader)
        {
            char          input;
            int?          index;
            IState        currentState;
            StringBuilder sb;
            IRule         reductionRule;
            string        lastGoodValue    = null;
            long          lastGoodPosition = 0;
            IRule         lastGoodRule     = null;
            TokenMatch    result;

            if (Reader == null)
            {
                throw new ArgumentNullException("Reader");
            }

            sb = new StringBuilder();

            currentState = states[0];
            while (true)
            {
                if (Reader.EOF)
                {
                    if (lastGoodPosition != 0)
                    {
                        break;
                    }
                    throw new Exceptions.EndOfStreamException(Reader.Position);
                }
                else
                {
                    input = Reader.Read();
                    index = currentState.GetNextStateIndex(input);
                }

                if (index == null)
                {
                    if (lastGoodPosition != 0)
                    {
                        break;
                    }
                    throw new InvalidInputException(Reader.Position, input);
                }

                sb.Append(input);
                currentState = states[index.Value];

                reductionRule = currentState.Reductions.FirstOrDefault();
                if (reductionRule != null)
                {
                    lastGoodPosition = Reader.Position;
                    lastGoodRule     = reductionRule;
                    lastGoodValue    = sb.ToString();
                }
            }

            Reader.Seek(lastGoodPosition);

            result         = new TokenMatch();
            result.Success = true;
            result.Token   = new Token(lastGoodRule.Name, lastGoodValue);
            result.Tags    = lastGoodRule.Tags.ToArray();
            return(result);
        }