Beispiel #1
0
            /// <summary>
            /// Determines if this snippet belongs to <paramref name="task"/> or one of its subtasks.
            /// Returns true if <paramref name="task"/> or <see cref="TaskNumber"/> are <c>null</c>.
            /// </summary>
            public bool IsInTask(HierarchicalNumber?task)
            {
                if (task == null || TaskNumber == null)
                {
                    return(true);
                }

                HierarchicalNumber refTask = task.Value;
                HierarchicalNumber myTask  = TaskNumber.Value;

                if (myTask.Major != refTask.Major)
                {
                    return(false);
                }

                if (myTask.Minor == null || refTask.Minor == null)
                {
                    return(true);
                }
                if (myTask.Minor != refTask.Minor)
                {
                    return(false);
                }

                if (myTask.SubMinor == null || refTask.SubMinor == null)
                {
                    return(true);
                }
                if (myTask.SubMinor != refTask.SubMinor)
                {
                    return(false);
                }

                return(true);
            }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var strValue = value as string;

            if (strValue == null || strValue == "")
            {
                return(null);
            }
            try
            {
                return(HierarchicalNumber.ParseFromString((string)value));
            }
            catch (Exception x)
            {
                return(new ValidationResult(false, x.Message));
            }
        }
Beispiel #3
0
            /// <summary>
            /// Determines if this snippet belongs to <paramref name="task"/> or one of its subtasks or any task before <paramref name="task"/>.
            /// Returns true if <paramref name="task"/> or <see cref="TaskNumber"/> are <c>null</c>.
            /// </summary>
            public bool IsInOrBeforeTask(HierarchicalNumber?task)
            {
                if (task == null || TaskNumber == null)
                {
                    return(true);
                }

                HierarchicalNumber refTask = task.Value;
                HierarchicalNumber myTask  = TaskNumber.Value;

                //go through the number level by level

                if (myTask.Major > refTask.Major)
                {
                    return(false);
                }

                if (myTask.Minor == null || refTask.Minor == null)
                {
                    return(true);
                }
                if (myTask.Minor > refTask.Minor)
                {
                    return(false);
                }

                if (myTask.SubMinor == null || refTask.SubMinor == null)
                {
                    return(true);
                }
                if (myTask.SubMinor > refTask.SubMinor)
                {
                    return(false);
                }

                return(true);
            }
Beispiel #4
0
        /// <summary>
        /// Interprets the abstract syntax of the TML document and generates semantic node representatives.
        /// </summary>
        /// <exception cref="FormatException">Thrown if an unexpected tag or a task number in an invalid format is encountered and if more than one subsnippet is active.</exception>
        private void ParseTML()
        {
            nodes.Clear();

            //process all nodes of the abstract syntax tree
            foreach (var node in syntaxTree.Nodes)
            {
                var contentNode = node as TMLTerminalNode;
                var tagNode     = node as TMLTagNode;

                //if the node is just content, add a new text node
                if (contentNode != null)
                {
                    nodes.Add(new TextNode(contentNode));
                }

                //if the node is a tag node, try to interpret it as a snippet
                else if (tagNode != null)
                {
                    if (tagNode.TagName != "snippet")
                    {
                        throw new FormatException($"Did not expect tag {tagNode.TagName} here.");
                    }

                    //Create the semantic snippet node
                    var snippetNode = new SnippetNode();

                    //Try to find the indentation of this snippet (everything that is a whitespace in front of the snippet tag)
                    var startIndentIncl             = tagNode.OpeningTagBeginIndex;
                    Func <char, bool> isIndentation = (char c) => Char.IsWhiteSpace(c) && c != '\r' && c != '\n';
                    while (startIndentIncl > 0 && isIndentation(text[startIndentIncl - 1]))
                    {
                        --startIndentIncl;
                    }
                    snippetNode.Indentation = text.Substring(startIndentIncl, tagNode.OpeningTagBeginIndex - startIndentIncl);

                    //Try to parse the task number
                    string taskNumber;
                    if (tagNode.Attributes.TryGetValue("task", out taskNumber))
                    {
                        try
                        {
                            snippetNode.TaskNumber = HierarchicalNumber.ParseFromString(taskNumber);
                        }
                        catch (Exception x)
                        {
                            throw new FormatException($"Error parsing hierarchical number from {taskNumber}.", x);
                        }
                    }

                    //Assign the existing subsnippets
                    foreach (var subsnippet in subsnippets)
                    {
                        subsnippet.Key.SetValue(snippetNode, FindSubSnippet(tagNode, subsnippet.Value.TagName));
                    }

                    //Check if there are invalid subtags
                    if (tagNode.InnerNodes.Where(n => n is TMLTagNode).Select(n => (TMLTagNode)n).Any(n => !subsnippetTags.Contains(n.TagName)))
                    {
                        throw new FormatException("The snippet tag contains invalid subtags.");
                    }

                    if (!snippetNode.HasAtMostOneActiveSubSnippet)
                    {
                        throw new FormatException("More than one subsnippet have uncommented lines.");
                    }

                    nodes.Add(snippetNode);
                }
            }
        }