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)); } }
/// <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); } } }