public void ParseForeachDirective() { Parser parser = GetNewParser( "#foreach($item in $collection)text here#end text"); TemplateNode templateNode = parser.ParseTemplate(); // Do semantic checks so the scope is populated templateNode.DoSemanticChecks(new ErrorHandler()); // Check Template Assert.AreEqual(2, templateNode.Content.Count); Assert.AreEqual(" text", ((XmlTextNode)templateNode.Content[1]).Text); // Check NVForeachDirective NVForeachDirective foreachDirective = (NVForeachDirective)templateNode.Content[0]; AssertPosition(new Position(1, 1, 1, 44), foreachDirective.Position); Assert.AreEqual("item", foreachDirective.Iterator); //TODO: Assert.AreEqual("collection", ((NVDesignatorExpression)foreachDirective.Collection).Designator.Name); // Check directive content Assert.AreEqual(1, foreachDirective.Content.Count); Assert.AreEqual("text here", ((XmlTextNode)foreachDirective.Content[0]).Text); // Check iterator variable is only in the foreach scope Assert.IsFalse(templateNode.Scope.Exists("item")); Assert.IsTrue(foreachDirective.Scope.Exists("item")); AssertNoErrors(parser); }
public override AuthoringScope ParseSource(ParseRequest req) { if (req == null) { throw new ArgumentNullException("req"); } Trace.WriteLine(string.Format("NVelocityLanguage.ParseSource(). Reason:{0}", req.Reason)); // Parse the input if required if (req.Reason == ParseReason.Check || req.Reason == ParseReason.DisplayMemberList || req.Reason == ParseReason.MemberSelect || req.Reason == ParseReason.MethodTip) { ErrorHandler errors = new ErrorHandler(); try { Scanner scanner = new Scanner(errors); scanner.Options.EnableIntelliSenseTriggerTokens = true; scanner.SetSource(req.Text); Parser parser = new Parser(scanner, errors); _templateNode = parser.ParseTemplate(); // Prepare the template node so that all the helpers are available PrepareTemplateNode(req.FileName); _templateNode.DoSemanticChecks(errors); } catch (ScannerError se) { req.Sink.AddError(req.FileName, "Scanner Error: " + se.Message, new TextSpan(), Severity.Error); } catch (Exception ex) { req.Sink.AddError(req.FileName, "FATAL: " + ex, new TextSpan(), Severity.Error); } finally { for (int i = 0; i < errors.Count; i++) { Error error = errors[i]; TextSpan textSpan = new TextSpan(); textSpan.iStartLine = error.Position.StartLine - 1; textSpan.iStartIndex = error.Position.StartPos - 1; textSpan.iEndLine = error.Position.EndLine - 1; textSpan.iEndIndex = error.Position.EndPos - 1; Severity severity = Severity.Fatal; if (error.Severity == ErrorSeverity.Error) { severity = Severity.Error; } else if (error.Severity == ErrorSeverity.Warning) { severity = Severity.Warning; } else if (error.Severity == ErrorSeverity.Message) { severity = Severity.Hint; } req.Sink.AddError(req.FileName, error.Description, textSpan, severity); } } #if DEBUG int line; int column; req.View.GetCaretPos(out line, out column); _debugForm.UpdateUI(line + 1, column + 1, _templateNode); #endif } else { //MessageBox.Show("Unparsed ParseReason: " + req.Reason); } // Perform other operations if (req.Reason == ParseReason.MethodTip) { TextSpan textSpan = new TextSpan(); textSpan.iStartLine = req.Line; textSpan.iStartIndex = req.Col - 1; textSpan.iEndLine = req.Line; textSpan.iEndIndex = req.Col; req.Sink.StartName(textSpan, ""); req.Sink.StartParameters(textSpan); Trace.WriteLine("MethodTip at line " + req.Line + " col " + req.Col); } if (req.Sink.HiddenRegions) { AddHiddenRegions(req.Sink, _templateNode.Content); req.Sink.ProcessHiddenRegions = true; } NVelocityAuthoringScope scope = new NVelocityAuthoringScope(_templateNode, req.FileName); //if (req.Sink.BraceMatching && req.Col > 30) //{ // TextSpan startBrace = new TextSpan(); // startBrace.iStartLine = req.Line; // startBrace.iStartIndex = 20; // startBrace.iEndLine = req.Line; // startBrace.iEndIndex = 21; // TextSpan endBrace = new TextSpan(); // endBrace.iStartLine = req.Line; // endBrace.iStartIndex = req.Col - 1; // endBrace.iEndLine = req.Line; // endBrace.iEndIndex = req.Col; // req.Sink.MatchPair(startBrace, endBrace, 0); //} return(scope); }
public override AuthoringScope ParseSource(ParseRequest req) { if (req == null) { throw new ArgumentNullException("req"); } Trace.WriteLine(string.Format("NVelocityLanguage.ParseSource(). Reason:{0}", req.Reason)); // Parse the input if required if (req.Reason == ParseReason.Check || req.Reason == ParseReason.DisplayMemberList || req.Reason == ParseReason.MemberSelect || req.Reason == ParseReason.MethodTip) { ErrorHandler errors = new ErrorHandler(); try { Scanner scanner = new Scanner(errors); scanner.Options.EnableIntelliSenseTriggerTokens = true; scanner.SetSource(req.Text); Parser parser = new Parser(scanner, errors); _templateNode = parser.ParseTemplate(); // Prepare the template node so that all the helpers are available PrepareTemplateNode(req.FileName); _templateNode.DoSemanticChecks(errors); } catch (ScannerError se) { req.Sink.AddError(req.FileName, "Scanner Error: " + se.Message, new TextSpan(), Severity.Error); } catch (Exception ex) { req.Sink.AddError(req.FileName, "FATAL: " + ex, new TextSpan(), Severity.Error); } finally { for (int i = 0; i < errors.Count; i++) { Error error = errors[i]; TextSpan textSpan = new TextSpan(); textSpan.iStartLine = error.Position.StartLine - 1; textSpan.iStartIndex = error.Position.StartPos - 1; textSpan.iEndLine = error.Position.EndLine - 1; textSpan.iEndIndex = error.Position.EndPos - 1; Severity severity = Severity.Fatal; if (error.Severity == ErrorSeverity.Error) severity = Severity.Error; else if (error.Severity == ErrorSeverity.Warning) severity = Severity.Warning; else if (error.Severity == ErrorSeverity.Message) severity = Severity.Hint; req.Sink.AddError(req.FileName, error.Description, textSpan, severity); } } #if DEBUG int line; int column; req.View.GetCaretPos(out line, out column); _debugForm.UpdateUI(line + 1, column + 1, _templateNode); #endif } else { //MessageBox.Show("Unparsed ParseReason: " + req.Reason); } // Perform other operations if (req.Reason == ParseReason.MethodTip) { TextSpan textSpan = new TextSpan(); textSpan.iStartLine = req.Line; textSpan.iStartIndex = req.Col - 1; textSpan.iEndLine = req.Line; textSpan.iEndIndex = req.Col; req.Sink.StartName(textSpan, ""); req.Sink.StartParameters(textSpan); Trace.WriteLine("MethodTip at line " + req.Line + " col " + req.Col); } if (req.Sink.HiddenRegions) { AddHiddenRegions(req.Sink, _templateNode.Content); req.Sink.ProcessHiddenRegions = true; } NVelocityAuthoringScope scope = new NVelocityAuthoringScope(_templateNode, req.FileName); //if (req.Sink.BraceMatching && req.Col > 30) //{ // TextSpan startBrace = new TextSpan(); // startBrace.iStartLine = req.Line; // startBrace.iStartIndex = 20; // startBrace.iEndLine = req.Line; // startBrace.iEndIndex = 21; // TextSpan endBrace = new TextSpan(); // endBrace.iStartLine = req.Line; // endBrace.iStartIndex = req.Col - 1; // endBrace.iEndLine = req.Line; // endBrace.iEndIndex = req.Col; // req.Sink.MatchPair(startBrace, endBrace, 0); //} return scope; }