Пример #1
0
        public override Production Parse(List <Token> tokens)
        {
            var symbolToken = tokens.First();

            if (symbolToken.TokenClass != TokenClass.Symbol)
            {
                return(new Production(null, tokens));
            }
            var symbol    = new SymbolExpression().Parse(tokens);
            var remaining = symbol.RemainingTokens;

            //function has to have parameters
            if (symbol.Expression == null || remaining.None())
            {
                return(new Production(null, tokens));
            }
            Constituents.Add(symbol.Expression);
            var separated = new SeparatedExpression().Parse(symbol.RemainingTokens);

            if (separated.Expression != null)
            {
                Constituents.Add(separated.Expression);
                return(new Production(this, separated.RemainingTokens));
            }
            var scoped = new ScopedExpression().Parse(symbol.RemainingTokens);

            if (scoped.Expression == null)
            {
                return(new Production(null, tokens));
            }
            Constituents.Add(scoped.Expression);
            return(new Production(this, scoped.RemainingTokens));
        }
Пример #2
0
        public override Production Parse(List <Token> tokens)
        {
            var firstParen = tokens.First();

            if (firstParen.TokenClass != TokenClass.StartScope)
            {
                return(new Production(null, tokens));
            }
            var current   = new ScopeExpression().Parse(tokens);
            var remaining = current.RemainingTokens.ToList();

            while (remaining.Any() && remaining.FirstOrDefault() != null && remaining[0].TokenClass != TokenClass.EndScope)
            {
                Expression parsed;
                switch (remaining.First().TokenClass)
                {
                case TokenClass.ReplacementSymbol:
                    current = new OperationExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    if (Constituents.Count > 0)
                    {
                        return(new Production(null, tokens));                            //cannot have multiple value types in a row
                    }
                    current = new ReplacementSymbolExpression().Parse(remaining);
                    break;

                case TokenClass.Integer:
                case TokenClass.Float:
                    current = new OperationExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    if (Constituents.Count > 0)
                    {
                        return(new Production(null, tokens));
                    }
                    current = NumericExpression.TryParseNumeric(remaining);
                    break;

                case TokenClass.String:
                    current = new OperationExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    if (Constituents.Count > 0)
                    {
                        return(new Production(null, tokens));
                    }
                    current = new StringExpression().Parse(remaining);
                    break;

                case TokenClass.Boolean:
                    current = new OperationExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    if (Constituents.Count > 0)
                    {
                        return(new Production(null, tokens));
                    }
                    current = new BooleanExpression().Parse(remaining);
                    break;

                case TokenClass.Symbol:
                    current = new FunctionExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    if (Constituents.Count > 0)
                    {
                        return(new Production(null, tokens));
                    }
                    current = new SymbolExpression().Parse(remaining);
                    break;

                case TokenClass.StartScope:
                    current = new SeparatedExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    if (Constituents.Count > 0)
                    {
                        return(new Production(null, tokens));
                    }
                    current = new ScopedExpression().Parse(remaining);
                    break;

                case TokenClass.Whitespace:
                    current = new WhitespaceExpression().Parse(remaining);
                    break;

                case TokenClass.Operator:
                    if (Constituents.Count != 1)
                    {
                        return(new Production(null, remaining));
                    }
                    current = new OperationExpression().Parse(Constituents.First(), remaining);
                    if (current.Expression == null)
                    {
                        break;
                    }
                    Constituents.Clear();
                    break;

                default:
                    return(new Production(null, remaining));

                    break;
                }
                if (current.Expression == null)
                {
                    return(new Production(null, tokens));
                }
                if (!(current.Expression is WhitespaceExpression))
                {
                    Constituents.Add(current.Expression);
                }
                remaining = current.RemainingTokens;
            }
            if (remaining.None())
            {
                return(new Production(null, tokens));
            }
            if (remaining[0].TokenClass != TokenClass.EndScope)
            {
                return(new Production(null, tokens));
            }
            return(new Production(this, remaining.Skip(1).ToList()));
        }
Пример #3
0
        public override Production Parse(List <Token> tokens)
        {
            var firstParen = tokens.First();

            if (firstParen.TokenClass != TokenClass.StartScope)
            {
                return(new Production(null, tokens));
            }
            var current   = new ScopeExpression().Parse(tokens);
            var remaining = current.RemainingTokens.ToList();

            while (remaining.Any() && remaining.FirstOrDefault() != null && remaining[0].TokenClass != TokenClass.EndScope)
            {
                switch (remaining.First().TokenClass)
                {
                case TokenClass.ReplacementSymbol:
                    if (Constituents.Count > 1 && !(Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, tokens));
                    }
                    current = new ReplacementSymbolExpression().Parse(remaining);
                    break;

                case TokenClass.Integer:
                case TokenClass.Float:
                    if (Constituents.Count > 1 && !(Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, tokens));
                    }
                    current = NumericExpression.TryParseNumeric(remaining);
                    break;

                case TokenClass.String:
                    if (Constituents.Count > 1 && !(Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, tokens));
                    }
                    current = new StringExpression().Parse(remaining);
                    break;

                case TokenClass.Boolean:
                    if (Constituents.Count > 1 && !(Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, tokens));
                    }
                    current = new BooleanExpression().Parse(remaining);
                    break;

                case TokenClass.Symbol:
                    if (Constituents.Count > 1 && !(Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, tokens));
                    }
                    current = new FunctionExpression().Parse(remaining);;
                    if (current.Expression != null)
                    {
                        break;
                    }
                    current = new SymbolExpression().Parse(remaining);
                    break;

                case TokenClass.StartScope:
                    if (Constituents.Count > 1 && !(Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, tokens));
                    }
                    current = new SeparatedExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    current = new ScopedExpression().Parse(remaining);
                    if (current.Expression != null)
                    {
                        break;
                    }
                    break;

                case TokenClass.EndScope:
                    current = new ScopeExpression().Parse(remaining);
                    break;

                case TokenClass.Whitespace:
                    current = new WhitespaceExpression().Parse(remaining);
                    break;

                case TokenClass.Separator:
                    current = new SeparatorExpression().Parse(remaining);
                    break;

                case TokenClass.Operator:
                    if (Constituents.Count < 1 || (Constituents.Last() is SeparatorExpression))
                    {
                        return(new Production(null, remaining));
                    }
                    current = new OperationExpression().Parse(Constituents.Last(), remaining);
                    if (current.Expression == null)
                    {
                        break;
                    }
                    Constituents.RemoveAt(Constituents.Count - 1);
                    break;

                default:
                    return(new Production(null, remaining));
                }
                if (current.Expression == null)
                {
                    return(new Production(null, tokens));
                }
                if (!(current.Expression is WhitespaceExpression))
                {
                    Constituents.Add(current.Expression);
                }
                remaining = current.RemainingTokens;
            }
            //parenthesis must be closed
            if (remaining.FirstOrDefault() == null || remaining.First().TokenClass != TokenClass.EndScope)
            {
                return(new Production(null, remaining));
            }
            var separators = Constituents.Count(c => c is SeparatorExpression);
            var values     = Constituents.Count(c => !(c is SeparatorExpression));

            //cannot be a separated expression without separators
            if (separators == 0)
            {
                return(new Production(null, tokens));
            }
            //separated expressions require exactly n - 1 separators to values
            if (separators != (values - 1))
            {
                return(new Production(null, tokens));
            }
            var valuesOnly = Constituents.ToList().Aggregate(new List <Expression>(), (acc, i) =>
            {
                if (i is ScopeExpression || i is SeparatorExpression)
                {
                    return(acc);
                }
                acc.Add(i);
                return(acc);
            }).ToList();

            Constituents = valuesOnly;
            return(new Production(this, remaining.Skip(1).ToList()));
        }