Exemplo n.º 1
0
        public void Parse()
        {
            _sourceEnumeratorField?.Dispose();

            _sourceEnumeratorField = Source.GetEnumerator();
            _inputTokenField = GetNextToken();
            Begin("Expression");
            LogicalExpression();
            Match("EOP");
            End();
        }
Exemplo n.º 2
0
        public ParseResult Parse()
        {
            _sourceEnumerator?.Dispose();

            _sourceEnumerator = Source.GetEnumerator();
            _inputToken = GetNextToken();
            Begin("Expression");
            LogicalExpression();
            Match("EOP");
            End();

            return _result;
        }
Exemplo n.º 3
0
 public IEnumerable<Token> Scan(IEnumerable<char> source)
 {
     State current = StatesTable.InitialState;
     int position = 0;
     bool @continue = false;
     var currentValue = new StringBuilder();
     char symbol = ' ';
     Func<Token> markPartialStop = () =>
                                       {
                                           if (current.IsStop)
                                           {
                                               var result = new Token(current.ResultingTokenId,
                                                                      currentValue.ToString());
                                               current = StatesTable.InitialState;
                                               currentValue.Clear();
                                               return result;
                                           }
                                           else
                                               throw new ArgumentException(
                                                   string.Format(
                                                       "Invalid source (construction {0}; symbol '{1}' pos {2})",
                                                       currentValue, (@continue ? symbol.ToString() : "EOP"),
                                                       position),
                                                   "source");
                                       };
     using (IEnumerator<char> enumerator = source.GetEnumerator())
     {
         @continue = enumerator.MoveNext();
         while (@continue)
         {
             symbol = @enumerator.Current;
             if (current.Accepts(symbol))
             {
                 current = StatesTable.Accept(current.ChangeTo(symbol));
                 currentValue.Append(symbol);
                 @continue = enumerator.MoveNext();
                 position++;
             }
             else
                 yield return markPartialStop();
         }
     }
     yield return markPartialStop();
 }
Exemplo n.º 4
0
        public IEnumerable<Token> Scan(IEnumerable<char> source)
        {
            State[] current = {StatesTable.InitialState};
            var position = 0;
            var @continue = false;
            var currentValue = new StringBuilder();
            var symbol = ' ';

            Func<Token> markPartialStop = () =>
            {
                if (current[0].IsStop)
                {
                    var result = new Token(current[0].ResultingTokenId,
                                            currentValue.ToString());
                    current[0] = StatesTable.InitialState;
                    currentValue.Clear();
                    return result;
                }
                else
                    throw new ArgumentException(
                        $"Invalid source (construction {currentValue}; symbol '{(@continue ? symbol.ToString() : "EOP")}' pos {position})",
                        nameof(source));
            };

            using (var enumerator = source.GetEnumerator())
            {
                @continue = enumerator.MoveNext();
                while (@continue)
                {
                    symbol = @enumerator.Current;
                    if (current[0].Accepts(symbol))
                    {
                        current[0] = StatesTable.Accept(current[0].ChangeTo(symbol));
                        currentValue.Append(symbol);
                        @continue = enumerator.MoveNext();
                        position++;
                    }
                    else
                        yield return markPartialStop();
                }
            }
            yield return markPartialStop();
        }
Exemplo n.º 5
0
 internal UnexpectedTokenException(Token unexpected)
     : base($"Unexpected token {unexpected}")
 {
 }
Exemplo n.º 6
0
 private void Match(string tokenType)
 {
     if (_inputToken.Id == tokenType)
     {
         Begin(_inputToken.ToString());
         _inputToken = GetNextToken();
         End();
     }
     else
         throw new UnexpectedTokenException(_inputToken);
 }
Exemplo n.º 7
0
 internal UnexpectedTokenException(Token unexpected)
     : base(string.Format("Unexpected token {0}", unexpected))
 {
 }
Exemplo n.º 8
0
 private void Match(string tokenType)
 {
     if (inputTokenField.Id == tokenType)
     {
         Begin(inputTokenField.ToString());
         inputTokenField = GetNextToken();
         End();
     }
     else
         throw new UnexpectedTokenException(inputTokenField);
 }