Пример #1
0
        public ParsingResult Parse(string expression)
        {
            InputStream input          = new InputStream(expression);
            var         tokens         = grammar.Parse(input);
            var         filteredTokens = grammar.Filter(tokens);

            var tokensList = filteredTokens.ToList();

            var sortedParameters = (from token in tokensList
                                    where token is ParameterToken
                                    let i = tokensList.IndexOf(token)
                                            orderby i
                                            select token).Distinct(new NameParameterEqueliatyComparer()).ToList();

            var mixedTokens = grammar.ConvertToMixed(filteredTokens);
            var ast         = grammar.CreateAST(mixedTokens);

            if (mixedTokens.Count != 1)
            {
                throw new ParserException("Wrong expression.");
            }

            var optimizedAst = ast.Optimize();

            var paramsDict = Parameters.ToDictionary(s => Parameters.IndexOf(s));

            var dict = new Dictionary <string, int>();

            foreach (var item in paramsDict)
            {
                dict.Add(item.Value, item.Key);
            }

            var result = new ParsingResult
            {
                Tree                 = optimizedAst,
                ParameterNames       = sortedParameters.Select(token => ((ParameterToken)token).ParameterName),
                ParameterExpressions = grammar.ParameterExpressions.Values.OrderBy(p => dict[p.Name]).ToArray()
            };

            return(result);
        }