Exemplo n.º 1
0
        public void ParseSetDirectiveSimpleNumExpression()
        {
            Parser       parser       = GetNewParser("#set($x = 10)");
            TemplateNode templateNode = parser.ParseTemplate();

            // Check NVDirective
            NVDirective nvDirective = (NVDirective)templateNode.Content[0];

            Assert.AreEqual("set", nvDirective.Name);

            AssertNoErrors(parser);
        }
Exemplo n.º 2
0
        private NVDirective ParseNVDirective()
        {
            // NVDirective -> "#" NVRestDirective.

            Position startPos = new Position(GetCurrentTokenPosition().Start);

            MatchToken(TokenType.NVDirectiveHash);

            NVDirective nvDirective = ParseNVRestDirective(startPos);

            return(nvDirective);
        }
Exemplo n.º 3
0
        public void ParseComponentWithNoParameters()
        {
            Parser       parser       = GetNewParser("#component()");
            TemplateNode templateNode = parser.ParseTemplate();

            // Check NVDirective
            Assert.AreEqual(1, templateNode.Content.Count);
            NVDirective nvDirective = (NVDirective)templateNode.Content[0];

            Assert.AreEqual("component", nvDirective.Name);

            AssertNoErrors(parser);
        }
Exemplo n.º 4
0
        public void InsideComponent()
        {
            ScannerOptions scannerOptions = new ScannerOptions();

            scannerOptions.EnableIntelliSenseTriggerTokens = true;
            Parser parser = GetNewParser(scannerOptions,
                                         "#component(");
            //              ^
            TemplateNode templateNode = parser.ParseTemplate();

            // Check template
            NVDirective nvDirective = (NVDirective)templateNode.Content[0];

            Assert.AreEqual("component", nvDirective.Name);

            // Get node at position
            AstNode astNode = templateNode.GetNodeAt(1, 12);

            Assert.AreEqual(typeof(NVDirective), astNode.GetType());
            AssertPosition(new Position(1, 1, 1, 12), astNode.Position);
        }
Exemplo n.º 5
0
        public void InsideBodyOfComponentAtEndOfStartedDirective()
        {
            ScannerOptions scannerOptions = new ScannerOptions();

            scannerOptions.EnableIntelliSenseTriggerTokens = true;
            Parser parser = GetNewParser(scannerOptions,
                                         "#foreach($item in $items)\n" +
                                         "    #\n" +
                                         //    ^
                                         "#end");
            TemplateNode templateNode = parser.ParseTemplate();

            // Check the inner directive
            NVDirective nvDirective = (NVDirective)((NVForeachDirective)templateNode.Content[0]).Content[1];

            Assert.AreEqual("", nvDirective.Name);

            // Get the node at the location and check its position
            AstNode astNode = templateNode.GetNodeAt(2, 6);

            Assert.IsInstanceOfType(typeof(NVDirective), astNode);
            AssertPosition(new Position(2, 5, 3, 1), astNode.Position);
        }
Exemplo n.º 6
0
        public override Declarations GetDeclarations(IVsTextView view, int line, int col,
                                                     TokenInfo info, ParseReason reason)
        {
            // If the intellisense parse reason was called before the template could be parsed
            if (_templateNode == null)
            {
                return(null);
            }

            AstNode astNode = _templateNode.GetNodeAt(line + 1, col + 1);

            if (astNode == null)
            {
                Debug.Fail("Null AstNode when attempting to provide IntelliSense in NVelocityAuthoringScope.");
                return(null);
            }

            NVelocityDeclarations declarations = new NVelocityDeclarations();

            if (astNode is NVDesignator)
            {
                List <NVLocalNode> localNodes = _templateNode.GetLocalNodesFromScope(line, col);
                localNodes.Sort(new Comparison <NVLocalNode>(
                                    delegate(NVLocalNode x, NVLocalNode y)
                {
                    return(x.Name.CompareTo(y.Name));
                }));
                foreach (NVLocalNode localNode in localNodes)
                {
                    declarations.Add(new NVelocityDeclaration(localNode.Name, localNode, IntelliSenseIcon.Variable));
                }
            }
            else if (astNode is NVSelector)
            {
                NVTypeNode typeNode = ((NVSelector)astNode).GetParentType();
                if (typeNode is NVClassNode)
                {
                    NVClassNode classNode = (NVClassNode)typeNode;

                    Dictionary <string, NVMethodNode> uniqueMethods = new Dictionary <string, NVMethodNode>();
                    foreach (NVMethodNode methodNode in classNode.Methods)
                    {
                        if (!uniqueMethods.ContainsKey(methodNode.Name))
                        {
                            uniqueMethods.Add(methodNode.Name, methodNode);
                        }
                    }

                    List <NVMethodNode> uniqueMethodsList = new List <NVMethodNode>();
                    foreach (KeyValuePair <string, NVMethodNode> pair in uniqueMethods)
                    {
                        uniqueMethodsList.Add(pair.Value);
                    }

                    uniqueMethodsList.Sort(new Comparison <NVMethodNode>(
                                               delegate(NVMethodNode x, NVMethodNode y)
                    {
                        return(x.Name.CompareTo(y.Name));
                    }));
                    foreach (NVMethodNode methodNode in uniqueMethodsList)
                    {
                        declarations.Add(new NVelocityDeclaration(methodNode.Name, methodNode, IntelliSenseIcon.Method));
                    }
                }
                else
                {
                    if (typeNode == null)
                    {
                        declarations.Add(new NVelocityDeclaration("Error: TypeNode is null", null, IntelliSenseIcon.Error));
                    }
                    else
                    {
                        declarations.Add(new NVelocityDeclaration("Error: Unsupported type for NVSelector " + typeNode.GetType().Name,
                                                                  null, IntelliSenseIcon.Error));
                    }
                }
            }
            else if (astNode is NVDirective)
            {
                NVDirective nvDirective = (NVDirective)astNode;
                if ((col + 1) - astNode.Position.StartPos == 1)
                {
                    // TODO: change the if expression so that it checks if the col is between the # and (
                    //       because you can bring up the intellisense list in the middle of the identifier
                    //       and it should display the list of the directives instead of the view components.
                    List <string> directivesList = new List <string>();
                    directivesList.AddRange(new string[]
                    {
                        "if", "elseif", "else", "end", "foreach", "set", "stop", "component",
                        "blockcomponent", "literal", "macro"
                    });
                    directivesList.Sort();

                    foreach (string directive in directivesList)
                    {
                        declarations.Add(new NVelocityDeclaration(directive, null, IntelliSenseIcon.Macro));
                    }
                }
                else if (nvDirective.Name == "component" || nvDirective.Name == "blockcomponent")
                {
                    List <NVClassNode> viewComponents = _templateNode.GetViewComponentsFromScope();
                    viewComponents.Sort(new Comparison <NVClassNode>(
                                            delegate(NVClassNode x, NVClassNode y)
                    {
                        return(x.Name.CompareTo(y.Name));
                    }));
                    foreach (NVClassNode classNode in viewComponents)
                    {
                        declarations.Add(new NVelocityDeclaration(classNode.Name, classNode, IntelliSenseIcon.Class));
                    }
                }
            }
            else if (astNode is XmlElement)
            {
                string xhtmlSchemaFileName = GetXhtmlSchemaFileName();
                if (string.IsNullOrEmpty(xhtmlSchemaFileName))
                {
                    Debug.Fail("Could not find XHTML schema.");
                    return(declarations);
                }
                XhtmlSchemaProvider xhtmlSchemaProvider = new XhtmlSchemaProvider(xhtmlSchemaFileName);

                XmlElement xmlElement = (XmlElement)astNode;
                if (string.IsNullOrEmpty(xmlElement.Name))
                {
                    if (xmlElement.Parent != null)
                    {
                        if (!xmlElement.Parent.IsSelfClosing && !xmlElement.IsComplete && !xmlElement.Parent.IsComplete)
                        {
                            declarations.Add(new NVelocityDeclaration(string.Format("/{0}>", xmlElement.Parent.Name),
                                                                      null, IntelliSenseIcon.XmlElement));
                        }
                    }

                    foreach (string xhtmlElement in xhtmlSchemaProvider.GetElements())
                    {
                        declarations.Add(new NVelocityDeclaration(xhtmlElement, null, IntelliSenseIcon.XmlElement));
                    }
                }
                else
                {
                    // Retrieve attributes
                    List <string> xhtmlAttributes = xhtmlSchemaProvider.GetAttributes(xmlElement.Name);

                    // Remove attributes that are already used
                    foreach (AstNode attribute in xmlElement.Attributes)
                    {
                        if (attribute is XmlAttribute)
                        {
                            XmlAttribute xmlAttribute = (XmlAttribute)attribute;
                            if (xhtmlAttributes.Contains(xmlAttribute.Name))
                            {
                                xhtmlAttributes.Remove(xmlAttribute.Name);
                            }
                        }
                    }

                    // Add the declarations for the attributes to show
                    foreach (string xhtmlAttribute in xhtmlAttributes)
                    {
                        declarations.Add(new NVelocityDeclaration(xhtmlAttribute, null, IntelliSenseIcon.XmlAttribute));
                    }
                }
            }
            else
            {
                declarations.Add(new NVelocityDeclaration("Error: Context unknown, type is " + astNode.GetType().Name,
                                                          null, IntelliSenseIcon.Error));
            }

            return(declarations);
        }
Exemplo n.º 7
0
        private NVDirective ParseNVRestDirective(Position startPos)
        {
            // NVRestDirective -> "if" "(" NVExpression ")" Content { NVElseIfStatement } [ NVElseStatement ] NVEnd
            //                 -> "set" "(" NVReference "=" NVExpression ")"
            //                 -> "stop"
            //                 -> "foreach" "(" "$" nvIdentifier "in" NVExpression ")" Content NVEnd.
            //                 -> nvDirectiveName [ "(" { nvIdentifier | NVExpression } ")" [ Content NVEnd ] ].

            NVDirective nvDirective;

            if (_scanner.CurrentToken != null && _scanner.CurrentToken.Type == TokenType.NVDirectiveName)
            {
                if (_scanner.CurrentToken.Image == "foreach")
                {
                    nvDirective = new NVForeachDirective();
                }
                else
                {
                    nvDirective = new NVDirective(_scanner.CurrentToken.Image);
                }
                _scanner.GetToken();
            }
            else
            {
                AddError("Expected directive name");
                nvDirective          = new NVDirective("");
                nvDirective.Position = new Position(startPos, GetCurrentTokenPosition());
                return(nvDirective);
            }

            // Match directive parameters
            if (CurrentTokenType == TokenType.NVDirectiveLParen)
            {
                _scanner.GetToken();
                if (nvDirective is NVForeachDirective)
                {
                    MatchToken(TokenType.NVDollar);
                    if (_scanner.CurrentToken != null && _scanner.CurrentToken.Type == TokenType.NVIdentifier)
                    {
                        ((NVForeachDirective)nvDirective).Iterator = _scanner.CurrentToken.Image;
                        _scanner.GetToken();
                    }
                    else
                    {
                        AddError("Foreach variable declaration expected");
                    }

                    MatchToken(TokenType.NVIn);

                    ((NVForeachDirective)nvDirective).Collection = ParseNVExpression();
                }
                else
                {
                    while (!_scanner.EOF && !CurrentTokenIn(TokenType.NVDirectiveRParen, TokenType.NVDirectiveHash))
                    {
                        _scanner.GetToken();

                        // If the tokens are errors break out
                        if (CurrentTokenType == TokenType.Error)
                        {
                            AddError("Unable to parse all directive contents.");
                            break;
                        }
                    }

                    // If a new directive starts in the middle of an unfinished directive params then stop parsing this directive
                    if (CurrentTokenType != TokenType.NVDirectiveRParen)
                    {
                        AddError("Incomplete directive.");

                        nvDirective.Position = new Position(startPos, GetCurrentTokenPosition());
                        return(nvDirective);
                    }
                }
                MatchToken(TokenType.NVDirectiveRParen);
            }

            // Match directive content and #end if required
            if (nvDirective is NVForeachDirective)
            {
                while (!(CurrentTokenType == TokenType.NVDirectiveHash &&
                         _scanner.PeekToken(1) != null &&
                         _scanner.PeekToken(1).Type == TokenType.NVDirectiveName &&
                         _scanner.PeekToken(1).Image == "end"))
                {
                    List <AstNode> content = ParseContent();
                    ((NVForeachDirective)nvDirective).Content.AddRange(content);

                    // If this is the end of the file then return what has been build
                    if (_scanner.EOF)
                    {
                        AddError("Expected #end directive.");
                        nvDirective.Position = new Position(startPos, _scanner.CurrentPos);
                        return(nvDirective);
                    }

                    // Break out of the loop if no content was found because the directive is incomplete
                    if (content.Count == 0)
                    {
                        AddError("The parser has been pre-emptively terminated because it appears " +
                                 "as if the parser is stuck. [In ParseNVForeachDirective()]");
                        break;
                    }
                }

                // Match #end
                MatchToken(TokenType.NVDirectiveHash);
                if (CurrentTokenType == TokenType.NVDirectiveName)
                {
                    if (_scanner.CurrentToken.Image != "end")
                    {
                        AddError("Expected #end directive.");
                    }

                    nvDirective.Position = new Position(startPos, _scanner.CurrentPos);
                    _scanner.GetToken();
                }
                else
                {
                    _scanner.GetToken();
                    nvDirective.Position = new Position(startPos, _scanner.CurrentPos);
                }
            }
            else
            {
                nvDirective.Position = new Position(startPos, GetCurrentTokenPosition());
            }

            return(nvDirective);
        }