Esempio n. 1
0
        public static ValueProviderBase TryParse(string type, string token, string variable, Type modelType, PropertyInfo modelProperty, QueryDescription qd, ScopedDictionary <string, ValueProviderBase> variables, Action <bool, string> addError)
        {
            switch (type)
            {
            case "":
            {
                if (token.StartsWith("$"))
                {
                    string v = token.TryBefore('.') ?? token;

                    if (!variables.TryGetValue(v, out ValueProviderBase vp))
                    {
                        addError(false, "Variable '{0}' is not defined at this scope".FormatWith(v));
                        return(null);
                    }

                    if (!(vp is TokenValueProvider))
                    {
                        return(new ContinueValueProvider(token.TryAfter('.'), vp, addError));
                    }
                }

                ParsedToken result = ParsedToken.TryParseToken(token, SubTokensOptions.CanElement, qd, variables, addError);

                if (result.QueryToken != null && TranslateInstanceValueProvider.IsTranslateInstanceCanditate(result.QueryToken))
                {
                    return new TranslateInstanceValueProvider(result, false, addError)
                           {
                               Variable = variable
                           }
                }
                ;
                else
                {
                    return new TokenValueProvider(result, false)
                           {
                               Variable = variable
                           }
                };
            }

            case "q":
            {
                ParsedToken result = ParsedToken.TryParseToken(token, SubTokensOptions.CanElement, qd, variables, addError);

                return(new TokenValueProvider(result, true)
                    {
                        Variable = variable
                    });
            }

            case "t":
            {
                ParsedToken result = ParsedToken.TryParseToken(token, SubTokensOptions.CanElement, qd, variables, addError);

                return(new TranslateInstanceValueProvider(result, true, addError)
                    {
                        Variable = variable
                    });
            }

            case "m":
                return(new ModelValueProvider(token, modelType, modelProperty, addError)
                {
                    Variable = variable
                });

            case "g":
                return(new GlobalValueProvider(token, addError)
                {
                    Variable = variable
                });

            case "d":
                return(new DateValueProvider(token, addError)
                {
                    Variable = variable
                });

            default:
                addError(false, "{0} is not a recognized value provider (q:Query, t:Translate, m:Model, g:Global or just blank)");
                return(null);
            }
        }
Esempio n. 2
0
        public static ValueProviderBase?TryParse(string typeToken, string variable, ITemplateParser tp)
        {
            var type  = typeToken.TryBefore(":") ?? "";
            var token = typeToken.TryAfter(":") ?? typeToken;

            switch (type)
            {
            case "":
            {
                if (token.StartsWith("$"))
                {
                    string v = token.TryBefore('.') ?? token;

                    if (!tp.Variables.TryGetValue(v, out ValueProviderBase? vp))
                    {
                        tp.AddError(false, "Variable '{0}' is not defined at this scope".FormatWith(v));
                        return(null);
                    }

                    if (!(vp is TokenValueProvider))
                    {
                        return(new ContinueValueProvider(token.TryAfter('.'), vp, tp.AddError));
                    }
                }

                ParsedToken result = ParsedToken.TryParseToken(token, SubTokensOptions.CanElement, tp.QueryDescription, tp.Variables, tp.AddError);

                if (result.QueryToken != null && TranslateInstanceValueProvider.IsTranslateInstanceCanditate(result.QueryToken))
                {
                    return new TranslateInstanceValueProvider(result, false, tp.AddError)
                           {
                               Variable = variable
                           }
                }
                ;
                else
                {
                    return new TokenValueProvider(result, false)
                           {
                               Variable = variable
                           }
                };
            }

            case "q":
            {
                ParsedToken result = ParsedToken.TryParseToken(token, SubTokensOptions.CanElement, tp.QueryDescription, tp.Variables, tp.AddError);

                return(new TokenValueProvider(result, true)
                    {
                        Variable = variable
                    });
            }

            case "t":
            {
                ParsedToken result = ParsedToken.TryParseToken(token, SubTokensOptions.CanElement, tp.QueryDescription, tp.Variables, tp.AddError);

                return(new TranslateInstanceValueProvider(result, true, tp.AddError)
                    {
                        Variable = variable
                    });
            }

            case "m":
                return(new ModelValueProvider(token, tp.ModelType, tp.AddError)
                {
                    Variable = variable
                });

            case "g":
                return(new GlobalValueProvider(token, tp.AddError)
                {
                    Variable = variable
                });

            case "d":
                return(new DateValueProvider(token, tp.AddError)
                {
                    Variable = variable
                });

            default:
                tp.AddError(false, "{0} is not a recognized value provider (q:Query, t:Translate, m:Model, g:Global or just blank)");
                return(null);
            }
        }