Esempio n. 1
0
 public DjangoFilter(string filterName, int filterStart, DjangoVariableValue arg = null, int groupStart = 0)
 {
     Filter      = filterName;
     FilterStart = filterStart;
     ArgStart    = groupStart;
     Arg         = arg;
 }
        private static DjangoFilter GetFilterFromMatch(Match match, int start)
        {
            var filterName = match.Groups["filter_name"];

            if (!filterName.Success)
            {
                // TODO: Report error
            }
            var filterStart         = filterName.Index;
            DjangoVariableValue arg = null;
            int argStart            = 0;

            var constantGroup = match.Groups["constant_arg"];

            if (constantGroup.Success)
            {
                arg      = new DjangoVariableValue(constantGroup.Value, DjangoVariableKind.Constant);
                argStart = constantGroup.Index;
            }
            else
            {
                var varGroup = match.Groups["var_arg"];
                if (varGroup.Success)
                {
                    arg      = new DjangoVariableValue(varGroup.Value, DjangoVariableKind.Variable);
                    argStart = varGroup.Index;
                }
                else
                {
                    var numGroup = match.Groups["num_arg"];
                    if (numGroup.Success)
                    {
                        arg      = new DjangoVariableValue(numGroup.Value, DjangoVariableKind.Number);
                        argStart = numGroup.Index;
                    }
                }
            }
            return(new DjangoFilter(filterName.Value, filterStart + start, arg, argStart + start));
        }
        public static DjangoVariable Parse(string filterText, int start = 0)
        {
            if (filterText.StartsWith("{{"))
            {
                filterText = GetTrimmedFilterText(filterText, ref start);
                if (filterText == null)
                {
                    return(null);
                }
            }
            else
            {
                int i = 0;
                while (i < filterText.Length && char.IsWhiteSpace(filterText[i]))
                {
                    ++i;
                    ++start;
                }
                if (i < filterText.Length)
                {
                    filterText = filterText.Substring(i).TrimEnd();
                }
            }

            int varStart = start;
            DjangoVariableValue variable = null;
            List <DjangoFilter> filters  = new List <DjangoFilter>();

            foreach (Match match in _filterRegex.Matches(filterText))
            {
                if (variable == null)
                {
                    var constantGroup = match.Groups["constant"];
                    if (constantGroup.Success)
                    {
                        varStart = constantGroup.Index;
                        variable = new DjangoVariableValue(constantGroup.Value, DjangoVariableKind.Constant);
                    }
                    else
                    {
                        var varGroup = match.Groups["var"];
                        if (!varGroup.Success)
                        {
                            var numGroup = match.Groups["num"];
                            if (!numGroup.Success)
                            {
                                return(null);
                            }
                            varStart = numGroup.Index;
                            variable = new DjangoVariableValue(numGroup.Value, DjangoVariableKind.Number);
                        }
                        else
                        {
                            varStart = varGroup.Index;
                            variable = new DjangoVariableValue(varGroup.Value, DjangoVariableKind.Variable);
                        }
                    }
                }
                else
                {
                    filters.Add(GetFilterFromMatch(match, start));
                }
            }

            return(new DjangoVariable(variable, varStart + start, filters.ToArray()));
        }
 public DjangoVariable(DjangoVariableValue expression, int expressionStart, params DjangoFilter[] filters)
 {
     Expression      = expression;
     ExpressionStart = expressionStart;
     Filters         = filters;
 }
Esempio n. 5
0
 public DjangoFilter(string filterName, int filterStart, DjangoVariableValue arg = null, int groupStart = 0) {
     Filter = filterName;
     FilterStart = filterStart;
     ArgStart = groupStart;
     Arg = arg;
 }