Example #1
0
 public DjangoLoadBlock(BlockParseInfo parseInfo, int fromStart, int nameStart, int fromNameStart)
     : base(parseInfo)
 {
     _fromStart     = fromStart;
     _nameStart     = nameStart;
     _fromNameStart = fromNameStart;
 }
Example #2
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            // TODO: Need to handle whitespace better
            // TODO: Need to split identifiers into individual components
            var words         = parseInfo.Args.Split(' ');
            int fromNameStart = -1;
            int fromStart     = -1;
            int nameStart     = parseInfo.Start + 1;

            for (int i = 1; i < words.Length; i++)
            {
                if (String.IsNullOrWhiteSpace(words[i]))
                {
                    nameStart += words[i].Length + 1;
                }
                else
                {
                    break;
                }
            }

            if (words.Length >= 4 && words[words.Length - 2] == "from")
            {
                // load fob from oar
            }

            return(new DjangoLoadBlock(parseInfo, fromStart, nameStart, fromNameStart));
        }
Example #3
0
 public static DjangoBlock Parse(BlockParseInfo parseInfo)
 {
     return(new DjangoIfOrIfNotEqualBlock(
                parseInfo,
                ParseVariables(parseInfo.Args.Split(' '), parseInfo.Start + parseInfo.Command.Length, 2)
                ));
 }
Example #4
0
 public DjangoUrlBlock(BlockParseInfo parseInfo, BlockClassification[] args, string urlName = null, string[] definedNamedParameters = null, uint nbDefinedParameters = 0)
     : base(parseInfo)
 {
     Args     = args;
     _urlName = urlName;
     _definedNamedParameters = definedNamedParameters != null ? definedNamedParameters : Array.Empty <string>();
     _nbDefinedParameters    = nbDefinedParameters;
 }
Example #5
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            string[] words = parseInfo.Args.Split(' ');
            IList <BlockClassification> argClassifications = new List <BlockClassification>();
            IList <string> usedNamedParameters             = new List <string>();
            uint           nbParameters = 0;
            bool           afterUrl = false, beforeAsKeyword = true;

            int    wordStart = parseInfo.Start + parseInfo.Command.Length;
            string urlName = null;

            foreach (string word in words)
            {
                if (!string.IsNullOrEmpty(word))
                {
                    Classification currentArgKind;
                    if (word.Equals("as"))
                    {
                        currentArgKind  = Classification.Keyword;
                        beforeAsKeyword = false;
                    }
                    else
                    {
                        currentArgKind = Classification.Identifier;
                    }
                    argClassifications.Add(new BlockClassification(new Span(wordStart, word.Length), currentArgKind));

                    if (afterUrl && beforeAsKeyword)
                    {
                        ++nbParameters;
                    }

                    // Get url name
                    if (urlName == null)
                    {
                        if (word.StartsWithOrdinal("'"))
                        {
                            urlName  = word.TrimStart('\'').TrimEnd('\'');
                            afterUrl = true;
                        }
                        else if (word.StartsWithOrdinal("\""))
                        {
                            urlName  = word.TrimStart('"').TrimEnd('"');
                            afterUrl = true;
                        }
                    }

                    // Test if word is a named parameter (but not in a string)
                    if (word.Contains('=') && !word.StartsWithOrdinal("'") && !word.StartsWithOrdinal("\""))
                    {
                        usedNamedParameters.Add(word.Split('=').First());
                    }
                }
                wordStart += word.Length + 1;
            }

            return(new DjangoUrlBlock(parseInfo, argClassifications.ToArray(), urlName, usedNamedParameters.ToArray(), nbParameters));
        }
Example #6
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            var words = parseInfo.Args.Split(' ');
            List <BlockClassification> argClassifications = new List <BlockClassification>();

            int wordStart = parseInfo.Start + parseInfo.Command.Length;

            return(new DjangoMultiVariableArgumentBlock(parseInfo, ParseVariables(words, wordStart)));
        }
Example #7
0
 public DjangoForBlock(BlockParseInfo parseInfo, int inStart, DjangoVariable variable, int argsEnd, int reversedStart, Tuple <string, int>[] definedVars)
     : base(parseInfo)
 {
     InStart       = inStart;
     Variable      = variable;
     ArgsEnd       = argsEnd;
     ReversedStart = reversedStart;
     _definedVars  = definedVars;
 }
Example #8
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            var words = parseInfo.Args.Split(' ');
            List <BlockClassification> argClassifications = new List <BlockClassification>();

            int wordStart = parseInfo.Start + parseInfo.Command.Length;

            foreach (var word in words)
            {
                bool hasNewline = false;
                if (word.Contains('\r') || word.Contains('\n'))
                {
                    hasNewline = true;
                    if (word.Trim().Length == 0)
                    {
                        break;
                    }
                }
                if (!String.IsNullOrEmpty(word))
                {
                    Classification curKind;
                    switch (word)
                    {
                    case "and":
                    case "or":
                    case "not":
                        curKind = Classification.Keyword;
                        break;

                    default:
                        curKind = Classification.Identifier;
                        break;
                    }

                    argClassifications.Add(
                        new BlockClassification(
                            new Span(wordStart, word.Length),
                            curKind
                            )
                        );
                }

                if (hasNewline)
                {
                    break;
                }

                wordStart += word.Length + 1;
            }

            return(new DjangoIfBlock(parseInfo, argClassifications.ToArray()));
        }
Example #9
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            int start = 0;

            for (int i = 0; i < parseInfo.Args.Length && parseInfo.Args[i] == ' '; i++, start++)
            {
            }

            var variable = DjangoVariable.Parse(
                "var|" + parseInfo.Args.Substring(start),
                parseInfo.Start + start + parseInfo.Command.Length
                );

            return(new DjangoFilterBlock(parseInfo, variable));
        }
Example #10
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            var    words    = parseInfo.Args.Split(' ');
            int    argStart = parseInfo.Command.Length + parseInfo.Start;
            string tagType  = null;

            foreach (var word in words)
            {
                if (!String.IsNullOrEmpty(word))
                {
                    tagType = word;
                    break;
                }
                argStart += 1;
            }
            // TODO: It'd be nice to report an error if we have more than one word
            // or if it's an unrecognized tag
            return(new DjangoTemplateTagBlock(parseInfo, argStart, tagType));
        }
Example #11
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo)
        {
            var args = parseInfo.Args.Split(' ');
            int argStart = -1, argLength = -1;

            for (int i = 0; i < args.Length; i++)
            {
                var word = args[i];
                if (!String.IsNullOrEmpty(word))
                {
                    if (word.StartsWithOrdinal("\r") || word.StartsWithOrdinal("\n"))
                    {
                        // unterminated tag
                        break;
                    }
                    argStart  = parseInfo.Start + parseInfo.Command.Length + i;
                    argLength = args[i].Length;
                    break;
                }
            }

            return(new DjangoAutoEscapeBlock(parseInfo, argStart, argLength));
        }
Example #12
0
 public static DjangoBlock Parse(BlockParseInfo parseInfo)
 {
     return(new DjangoUnknownBlock(parseInfo));
 }
Example #13
0
 public DjangoUnknownBlock(BlockParseInfo parseInfo)
     : base(parseInfo)
 {
 }
Example #14
0
 public static DjangoBlock Parse(BlockParseInfo parseInfo)
 {
     return(new DjangoArgumentlessBlock(parseInfo));
 }
Example #15
0
 public DjangoArgumentlessBlock(BlockParseInfo parseInfo)
     : base(parseInfo)
 {
 }
Example #16
0
 public static DjangoBlock Parse(BlockParseInfo parseInfo)
 {
     return(new DjangoWidthRatioBlock(parseInfo,
                                      ParseVariables(parseInfo.Args.Split(' '), parseInfo.Command.Length + parseInfo.Start, 3)));
 }
Example #17
0
 public DjangoMultiVariableArgumentBlock(BlockParseInfo parseInfo, params DjangoVariable[] variables)
     : base(parseInfo)
 {
     _variables = variables;
 }
Example #18
0
 public DjangoTemplateTagBlock(BlockParseInfo parseInfo, int argStart, string tagType)
     : base(parseInfo)
 {
     _argStart = argStart;
     _tagType  = tagType;
 }
Example #19
0
 public DjangoFilterBlock(BlockParseInfo parseInfo, DjangoVariable variable)
     : base(parseInfo)
 {
     _variable = variable;
 }
Example #20
0
 public DjangoWidthRatioBlock(BlockParseInfo parseInfo, params DjangoVariable[] variables)
     : base(parseInfo)
 {
     _variables = variables;
 }
Example #21
0
 public DjangoAutoEscapeBlock(BlockParseInfo parseInfo, int argStart, int argLength)
     : base(parseInfo)
 {
     _argStart  = argStart;
     _argLength = argLength;
 }
Example #22
0
 public DjangoSpacelessBlock(BlockParseInfo parseInfo)
     : base(parseInfo)
 {
 }
Example #23
0
 public static DjangoBlock Parse(BlockParseInfo parseInfo)
 {
     return(new DjangoSpacelessBlock(parseInfo));
 }
Example #24
0
        public static DjangoForBlock Parse(BlockParseInfo parseInfo)
        {
            var words   = parseInfo.Args.Split(' ');
            int inStart = -1;

            int inOffset = 0, inIndex = -1;
            var definitions = new List <Tuple <string, int> >();

            for (int i = 0; i < words.Length; i++)
            {
                var word = words[i];
                if (word == "in")
                {
                    inStart = inOffset + parseInfo.Start + parseInfo.Command.Length;
                    inIndex = i;
                    break;
                }
                else if (words[i].IndexOfAny(NewLines) != -1)
                {
                    // unterminated tag
                    break;
                }

                if (!String.IsNullOrEmpty(word))
                {
                    definitions.Add(new Tuple <string, int>(word, inOffset + parseInfo.Start + parseInfo.Command.Length));
                }
                inOffset += words[i].Length + 1;
            }

            // parse the arguments...
            int            reversedStart = -1;
            DjangoVariable variable      = null;
            int            argsEnd       = -1;

            if (inIndex != -1)
            {
                string filterText = "";
                argsEnd = inStart + "in".Length + 1;
                for (int i = inIndex + 1; i < words.Length; i++)
                {
                    int    nlStart = words[i].IndexOfAny(NewLines);
                    string trimmed = words[i];
                    if (nlStart != -1)
                    {
                        trimmed = words[i].Substring(0, nlStart);
                    }

                    if (i != inIndex + 1 && trimmed == words[i])   // if we trimmed we don't have an extra space
                    {
                        filterText += " ";
                        argsEnd    += 1;
                    }

                    if (trimmed == "reversed")
                    {
                        reversedStart = argsEnd;
                        break;
                    }

                    filterText += trimmed;
                    argsEnd    += trimmed.Length;
                    if (trimmed != words[i])
                    {
                        // unterminated tag
                        break;
                    }
                }

                var trimmedFilter = filterText.TrimStart(' ');

                variable = DjangoVariable.Parse(trimmedFilter,
                                                inStart + "in".Length + 1 + filterText.Length - trimmedFilter.Length);
            }

            return(new DjangoForBlock(parseInfo, inStart, variable, argsEnd, reversedStart, definitions.ToArray()));
        }
Example #25
0
 /// <summary>
 /// Creates a new DjangoBlock capturing the start index of the block command (for, debug, etc...).
 /// </summary>
 /// <param name="blockStart"></param>
 public DjangoBlock(BlockParseInfo parseInfo) {
     ParseInfo = parseInfo;
 }
Example #26
0
 public DjangoIfBlock(BlockParseInfo parseInfo, params BlockClassification[] args)
     : base(parseInfo)
 {
     Args = args;
 }
Example #27
0
 public DjangoIfOrIfNotEqualBlock(BlockParseInfo parseInfo, params DjangoVariable[] args)
     : base(parseInfo)
 {
     _args = args;
 }