コード例 #1
0
        // =======================================================================================
        // CLASS INITIALISATION
        // =======================================================================================
        /// <summary>
        /// A statement should be solely an expression with no return value or whose return value is ignored - eg. a function call. Tokens that
        /// represent a statement where the return value is used to set another variable's value should be described by a ValueSettingStatement.
        /// It is recommended to pass any tokens that are thought to be one Statement through the StatementHandler, which will break down the
        /// content into multiple Statements (if there are any AbstractEndOfStatementToken tokens).
        /// </summary>
        public Statement(IEnumerable <IToken> tokens, CallPrefixOptions callPrefix)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }
            if (!Enum.IsDefined(typeof(CallPrefixOptions), callPrefix))
            {
                throw new ArgumentOutOfRangeException("callPrefix");
            }

            Tokens = tokens.ToList().AsReadOnly();
            if (!Tokens.Any())
            {
                throw new ArgumentException("Statements must contain at least one token");
            }
            if (!Tokens.Any())
            {
                throw new ArgumentException("Empty tokens specified - invalid");
            }
            if (Tokens.Any(t => t == null))
            {
                throw new ArgumentException("Null token passed into Statement constructor");
            }
            var firstTokenAsAtom = tokens.First() as AtomToken;

            if ((firstTokenAsAtom != null) && firstTokenAsAtom.Content.Equals("Call", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException("The first token may not be the Call keyword, that must be specified through the CallPrefixOption value where present");
            }

            CallPrefix = callPrefix;
        }
コード例 #2
0
 public static bool NextTokenIs(object obj)
 {
     if (Tokens != null && Tokens.Any())
     {
         return(Tokens.Peek().Equals(obj));
     }
     return(false);
 }
コード例 #3
0
 public static Token NextToken()
 {
     if (Tokens != null && Tokens.Any())
     {
         return(Tokens.Dequeue());
     }
     return(null);
 }
コード例 #4
0
 private TokenOutput PeekToken()
 {
     if (Tokens.Any())
     {
         return(Tokens.Peek());
     }
     return(null);
 }
コード例 #5
0
        public void AddToken(Token token)
        {
            if (Tokens.Any(t => t.Colour == token.Colour))
            {
                throw new ArgumentException($"This token is already on the board: { token.Colour }");
            }

            token.Position = StartPosition;
            Tokens         = Tokens.Union(new Token[] { token });
        }
コード例 #6
0
ファイル: AndTerms.cs プロジェクト: intenvy/BagherZade
        public override HashSet <Document> GetResults(IInvertedIndex index)
        {
            var results = new HashSet <Document>();

            if (Tokens.Any())
            {
                results.UnionWith(index.GetDocumentsOfToken(Tokens[0]));
                Tokens.ForEach(t => results.IntersectWith(index.GetDocumentsOfToken(t)));
            }
            return(results);
        }
コード例 #7
0
        public Node Parse()
        {
            if (!Tokens.Any())
            {
                return(null);
            }

            if (Tokens.Last().TokenType != FilterTokenType.ExprEnd)
            {
                Tokens.Add(new Token(FilterTokenType.ExprEnd, null, null));
            }

            return(ParseExpression());
        }
コード例 #8
0
        /// <summary>
        /// Releases all resources consumed by the current <see cref="ConcurrencyControl" />.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether or not managed resources should be released.
        /// </param>
        protected virtual void Dispose(Boolean disposing)
        {
            if (disposing)
            {
                if (Tokens.Any())
                {
                    // Reject new entries and wait for pending exits.
                    IsDisposed = true;
                    SpinWait.SpinUntil(() => Tokens.Any() == false);
                }
            }

            IsDisposed = true;
        }
コード例 #9
0
        public PartialNumberContent(IEnumerable <IToken> tokens)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException("tokens");
            }

            Tokens = tokens.ToList().AsReadOnly();
            if (Tokens.Any(t => t == null))
            {
                throw new ArgumentException("Null reference encountered in tokens set");
            }
            if (Tokens.Any(t => !IsValidToken(t)))
            {
                throw new ArgumentException("The only allowable tokens are minus sign OperatorTokens, numeric AtomTokens and MemberAccessorOrDecimalPointTokens");
            }
        }
コード例 #10
0
        /// <summary>
        /// Evaluates formula
        /// </summary>
        /// <param name="argument">The argument to take variables values from</param>
        /// <returns></returns>
        /// <exception cref="MathException">Formula is not valid, cannot evaluate it</exception>
        /// <exception cref="UnknownEnumException{TEnum}"></exception>
        public double Evaluate(object argument = null)
        {
            if (!IsValid)
            {
                throw new MathException("Formula is not valid, cannot evaluate it");
            }

            if (argument == null && Tokens.Any(x => x.Type == TokenType.Variable))
            {
                throw new MathException("Encountered variable, yet argument is empty");
            }

            var stack = new Stack <double>();
            var negateNextStatement = false;
            var argumentProperties  = argument?.GetType().GetTypeInfo().DeclaredProperties;

            foreach (var token in Tokens)
            {
                switch (token.Type)
                {
                case TokenType.Number:
                    stack.Push(negateNextStatement ? -((NumberToken)token).Number : ((NumberToken)token).Number);
                    negateNextStatement = false;
                    break;

                case TokenType.Operator:
                    stack.Push(Compute(stack.Pop(), stack.Pop(), ((OperatorToken)token).Operator));
                    break;

                case TokenType.Negation:
                    negateNextStatement = true;
                    break;

                case TokenType.Variable:
                    var value = GetValueForVariable((VariableToken)token, argument, argumentProperties);
                    stack.Push(negateNextStatement ? -value : value);
                    negateNextStatement = false;
                    break;

                default:
                    throw new UnknownEnumException <TokenType>(token.Type);
                }
            }

            return(stack.Pop());
        }
コード例 #11
0
ファイル: Deck.cs プロジェクト: Rikshy/MTGPrint
        public Deck(bool isTemp)
        {
            if (isTemp)
            {
                return;
            }

            Cards.CollectionChanged += delegate(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs ne)
            {
                HasChanges = true;
                OnPropertyChanged(nameof(HasChanges));
                OnPropertyChanged(nameof(CardCount));

                if (ne.NewItems != null)
                {
                    foreach (var i in ne.NewItems)
                    {
                        (i as DeckCard).PropertyChanged += delegate(object s, PropertyChangedEventArgs pe)
                        {
                            if (pe.PropertyName == "SelectPrint" || pe.PropertyName == "Count" || pe.PropertyName == "CanPrint")
                            {
                                HasChanges = true;
                                OnPropertyChanged(nameof(HasChanges));
                                if (pe.PropertyName == "Count")
                                {
                                    OnPropertyChanged(nameof(CardCount));
                                }
                            }
                        };
                    }
                }
            };
            Tokens.CollectionChanged += delegate
            {
                HasTokens = Tokens.Any();
                OnPropertyChanged(nameof(HasTokens));
            };
        }
コード例 #12
0
 public bool IsEmpty()
 {
     return(!Tokens.Any() && !TokensList.Any());
 }
コード例 #13
0
ファイル: Figure.cs プロジェクト: Alex-Malik/VeDraw
        public void Parse(string markup)
        {
            // Prepare markup for parsing.
            // Replace multiply spaces and commas with a single space.
            markup = Regex.Replace(markup, "[ ]{2,}", " ", RegexOptions.None);
            markup = Regex.Replace(markup, "[,]", " ", RegexOptions.None);
            markup = markup.Trim();

            // We will change current tokens collection only
            // after the parsing process finishes successful.
            var newTokens = new List <Token>();

            // Markup is ready for parsing. Now we can
            // analyse the syntax and get tokens.
            try
            {
                for (int i = 0; i < markup.Length; i++)
                {
                    // Parse markup and create token. Also we can get
                    // start (s) and end (e) points of the token in given markup.
                    char symbol = markup[i];
                    int  s = 0, e = 0;

                    if (symbol == 'M' || symbol == 'm')
                    {
                        // Create token and save it to the tokens collection.
                        newTokens.Add(Move.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'L' || symbol == 'l')
                    {
                        newTokens.Add(Line.Parse(markup.Substring(i), out s, out e));
                        i += e;
                    }
                    else if (symbol == 'H' || symbol == 'h')
                    {
                        newTokens.Add(HLine.Parse(markup.Substring(i), out s, out e));
                        i += e;
                    }
                    else if (symbol == 'V' || symbol == 'v')
                    {
                        newTokens.Add(VLine.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'C' || symbol == 'c')
                    {
                        newTokens.Add(CubicBezierCurve.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'Q' || symbol == 'q')
                    {
                        newTokens.Add(QuadraticBezierCurve.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'S' || symbol == 's')
                    {
                        newTokens.Add(SmoothCubicBezierCurve.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'T' || symbol == 't')
                    {
                        newTokens.Add(SmoothQuadraticBezierCurve.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'A' || symbol == 'a')
                    {
                        newTokens.Add(EllipticalArc.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == 'Z' || symbol == 'z')
                    {
                        newTokens.Add(Close.Parse(markup.Substring(i), out s, out e));
                    }
                    else if (symbol == ' ')
                    {
                        continue;
                    }
                    else
                    {
                        // TODO: Throw an exception or log error and finish parsing.
                    }

                    // Continue loop from the end point of the token.
                    i += e;
                }

                // Parsing process finshed successful so we can update tokens collection.
                if (Tokens.Any())
                {
                    Tokens.Clear();
                }

                foreach (var token in newTokens)
                {
                    Tokens.Add(token);
                }
            }
            catch (Exception e)
            {
                // TODO: Log exception.
                // Left collection not changed.
            }
        }
コード例 #14
0
 public bool Contains(string contents)
 {
     return(Tokens.Any(tok => tok.Contents == contents));
 }